# -*- coding: utf-8 -*-
# -------------------------------------------------------------
# Bulk Configuration
# -------------------------------------------------------------

# Set up lattice
vector_a = [10.8612, 0.0, 0.0]*Angstrom
vector_b = [0.0, 10.8612, 0.0]*Angstrom
vector_c = [0.0, 0.0, 10.8612]*Angstrom
lattice = UnitCell(vector_a, vector_b, vector_c)

# Define elements
elements = [Silicon, Silicon, Silicon, Silicon, Silicon, Silicon, Silicon,
            Silicon, Silicon, Silicon, Silicon, Silicon, Silicon, Silicon,
            Silicon, Silicon, Silicon, Silicon, Silicon, Silicon, Silicon,
            Silicon, Silicon, Silicon, Silicon, Silicon, Silicon, Silicon,
            Silicon, Silicon, Silicon, Silicon, Silicon, Silicon, Silicon,
            Silicon, Silicon, Silicon, Silicon, Silicon, Silicon, Silicon,
            Silicon, Silicon, Silicon, Silicon, Silicon, Silicon, Silicon,
            Silicon, Silicon, Silicon, Silicon, Silicon, Silicon, Silicon,
            Silicon, Silicon, Silicon, Silicon, Silicon, Silicon, Silicon,
            Silicon, Germanium]

# Define coordinates
fractional_coordinates = [[-0.000438773566, -0.000719753172, -0.000719753172],
                          [-0.002567048046, -0.001436533687,  0.50015398949 ],
                          [-0.008628223743,  0.500646899944,  0.005181769839],
                          [-0.0075336859  ,  0.497981444601,  0.486430774944],
                          [ 0.500947655116,  0.000236291602,  0.000236291602],
                          [ 0.502080702598, -0.001824159557,  0.500665469201],
                          [ 0.518447274743,  0.514068709243, -0.006784361843],
                          [ 0.511361534119,  0.500099626911,  0.503369081493],
                          [ 0.126304289587,  0.120387604013,  0.126194042761],
                          [ 0.119803525236,  0.12140203436 ,  0.625643203194],
                          [ 0.121637174382,  0.640795577302,  0.107830922561],
                          [ 0.132228965017,  0.599709200801,  0.632560083983],
                          [ 0.627996589671,  0.121347176542,  0.12739156124 ],
                          [ 0.626723079448,  0.123615776954,  0.634476381168],
                          [ 0.638449772262,  0.62809984021 ,  0.129458995897],
                          [ 0.623996092286,  0.626673326408,  0.63053904907 ],
                          [ 0.247732322928,  0.247759417158,  0.004033366315],
                          [ 0.255813345952,  0.234052465179,  0.502071751904],
                          [ 0.256924293433,  0.757864733252, -0.004023557598],
                          [ 0.244043574545,  0.763392164623,  0.499390018214],
                          [ 0.757660078175,  0.252993826265,  0.01242439574 ],
                          [ 0.747275861645,  0.245805761335,  0.497488191514],
                          [ 0.748752048167,  0.754616111697,  0.006968396435],
                          [ 0.749465186978,  0.750778035671,  0.507097984283],
                          [ 0.383297090569,  0.378411943075,  0.117078347839],
                          [ 0.386895271843,  0.361603875376,  0.665954534059],
                          [ 0.375768933433,  0.881381989707,  0.134128894587],
                          [ 0.375450534638,  0.884870867409,  0.624301009322],
                          [ 0.877267610466,  0.375600169127,  0.134683434724],
                          [ 0.880017948779,  0.374726354547,  0.622983560028],
                          [ 0.876934361933,  0.876309323754,  0.124004249147],
                          [ 0.880190661062,  0.868783642201,  0.627818056869],
                          [ 0.249334530799, -0.001824159557,  0.247919297402],
                          [ 0.24984601051 , -0.001436533687,  0.752567048046],
                          [ 0.252382932107,  0.507317270523,  0.226019615003],
                          [ 0.254034523691,  0.486863176266,  0.780703223454],
                          [ 0.749763708398,  0.000236291602,  0.249052344884],
                          [ 0.750719753172, -0.000719753172,  0.750438773566],
                          [ 0.744409970307,  0.500248028737,  0.261677208202],
                          [ 0.753224929162,  0.492908956765,  0.750065335811],
                          [ 0.386243582096,  0.120577430322,  0.36912588949 ],
                          [ 0.37104959195 ,  0.120496097796,  0.878816392485],
                          [ 0.373859204094,  0.630988069354,  0.370842085067],
                          [ 0.383936750184,  0.640407295974,  0.8807181055  ],
                          [ 0.874472280166,  0.123065685306,  0.373792120428],
                          [ 0.881336896269,  0.124737714042,  0.875651089351],
                          [ 0.87040350098 ,  0.631757056472,  0.375732222511],
                          [ 0.873444355028,  0.62875525013 ,  0.879987148288],
                          [ 0.000547060768,  0.244865594023,  0.245135009438],
                          [-0.002370899126,  0.249077176039,  0.747221218407],
                          [ 0.000785605524,  0.751563758165,  0.248855845989],
                          [ 0.003820039246,  0.741458808826,  0.746663154452],
                          [ 0.501791393177,  0.254559526194,  0.244945866766],
                          [ 0.514179054395,  0.234997867078,  0.772582018458],
                          [ 0.509142333917,  0.759435774818,  0.24910901912 ],
                          [ 0.496488935193,  0.756656132044,  0.743838608308],
                          [ 0.12783948924 ,  0.374562229753,  0.355849101595],
                          [ 0.111806210406,  0.370994314737,  0.884575319845],
                          [ 0.118594912684,  0.876396904879,  0.379030927804],
                          [ 0.131713793498,  0.876581832571,  0.870688416455],
                          [ 0.623406366889,  0.371467869653,  0.376299967382],
                          [ 0.637884519093,  0.37277842172 ,  0.888032135701],
                          [ 0.634029995114,  0.873114427964,  0.375773533933],
                          [ 0.624840105207,  0.873606322327,  0.874844769765],
                          [ 0.272667812509,  0.473592064854,  0.503720963815]]

# Set up configuration
bulk_configuration = BulkConfiguration(
    bravais_lattice=lattice,
    elements=elements,
    fractional_coordinates=fractional_coordinates,
    )

# -------------------------------------------------------------
# Calculator
# -------------------------------------------------------------

potentialSet = Tersoff_SiGe_1989()
calculator = TremoloXCalculator(parameters=potentialSet)
calculator.setVerletListsDelta(0.25*Angstrom)

bulk_configuration.setCalculator(calculator)
bulk_configuration.update()

# -------------------------------------------------------------
# Molecular Dynamics
# -------------------------------------------------------------

initial_velocity = MaxwellBoltzmannDistribution(
    temperature=300.0*Kelvin,
    remove_center_of_mass_momentum=True
)

method = Langevin(
    time_step=1*femtoSecond,
    reservoir_temperature=300*Kelvin,
    friction=0.01*femtoSecond**-1,
    initial_velocity=initial_velocity,
    heating_rate=0*Kelvin/picoSecond,
)

# The bottom layer of atoms will be fixed to provide an absolute reference frame.
fix_atom_indices_0 = [0, 1, 4, 5, 32, 33, 36, 37]
constraints = [FixAtomConstraints(fix_atom_indices_0)]

# Set up the PLUMED command
plumed_command = """\
# Change the units to Ang, fs, eV
UNITS LENGTH=A TIME=fs ENERGY=96.48533645956869
# Use the cartesian position of the Ge interstitial atom as CV.
p: POSITION ATOM=65
# Put a metadynamics bias on the x component of the position CV p.
METAD ARG=p.x SIGMA=0.2 HEIGHT=0.05 PACE=50 LABEL=restraint
# Print the current position and bias potential value every 10 steps.
PRINT ARG=p.x,p.y,p.z,restraint.bias STRIDE=10 FILE=COLVAR
"""

# Set up the Plumed hook-class.
plumed_hook = PlumedMetadynamics(
    configuration=bulk_configuration,
    timestep=1.0*fs,
    plumed_commands=plumed_command,
    logfile='plumed_example.log',
)

md_trajectory = MolecularDynamics(
    bulk_configuration,
    constraints=constraints,
    trajectory_filename='si_ge_metadynamics_md.hdf5',
    steps=30000,
    log_interval=200,
    post_step_hook=plumed_hook,
    method=method
)

bulk_configuration = md_trajectory.lastImage()
