|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +from __future__ import print_function |
| 3 | +from __future__ import division |
| 4 | + |
| 5 | +import sys, os |
| 6 | +import numpy as np |
| 7 | +import cellconstructor as CC |
| 8 | +import cellconstructor.Phonons |
| 9 | + |
| 10 | +import sscha, sscha.Ensemble |
| 11 | +import sscha.SchaMinimizer |
| 12 | + |
| 13 | +""" |
| 14 | +This test makes a simple relaxation of the sample ensemble |
| 15 | +provided within this distribution |
| 16 | +""" |
| 17 | + |
| 18 | +def test_update_weights(verbose = False): |
| 19 | + total_path = os.path.dirname(os.path.abspath(__file__)) |
| 20 | + os.chdir(total_path) |
| 21 | + |
| 22 | + rho_final = np.loadtxt("final_rho.dat") |
| 23 | + DATA_PATH = "../../Examples/ensemble_data_test/" |
| 24 | + |
| 25 | + dyn_start = CC.Phonons.Phonons(os.path.join(DATA_PATH, "dyn")) |
| 26 | + dyn_target = CC.Phonons.Phonons(os.path.join(DATA_PATH, "dyn1_population2"), full_name = True) |
| 27 | + |
| 28 | + # Perform the minimization |
| 29 | + ens = sscha.Ensemble.Ensemble(dyn_start, 0, dyn_start.GetSupercell()) |
| 30 | + ens.load(DATA_PATH, 2, 1000) |
| 31 | + |
| 32 | + #Update the ensemble |
| 33 | + ens.update_weights(dyn_target, 0) |
| 34 | + |
| 35 | + delta_rho = np.max(np.abs(ens.rho - rho_final)) |
| 36 | + EPS = 1e-7 |
| 37 | + |
| 38 | + if verbose: |
| 39 | + print("Maximum difference on rho:", delta_rho) |
| 40 | + |
| 41 | + if delta_rho > EPS: |
| 42 | + print("Python RHO:") |
| 43 | + print(ens.rho) |
| 44 | + |
| 45 | + assert delta_rho < 2e-5 |
| 46 | + |
| 47 | +def test_simple_relax(verbose = False): |
| 48 | + total_path = os.path.dirname(os.path.abspath(__file__)) |
| 49 | + os.chdir(total_path) |
| 50 | + |
| 51 | + DATA_PATH = "../../Examples/ensemble_data_test/" |
| 52 | + |
| 53 | + dyn_start = CC.Phonons.Phonons(os.path.join(DATA_PATH, "dyn")) |
| 54 | + |
| 55 | + # We do not use the dyn1_population2 matrix (the fortran one) because it seems it has problem with the wyckoff minimization. |
| 56 | + # It is printing the structure slightly displaced |
| 57 | + dyn_target = CC.Phonons.Phonons(os.path.join(DATA_PATH, "dyn1_population2"), full_name = True) |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | + # Perform the minimization |
| 63 | + ens = sscha.Ensemble.Ensemble(dyn_start, 0, dyn_start.GetSupercell()) |
| 64 | + ens.load(DATA_PATH, 2, 1000) |
| 65 | + |
| 66 | + minim = sscha.SchaMinimizer.SSCHA_Minimizer(ens) |
| 67 | + minim.minim_struct = True |
| 68 | + minim.min_step_dyn = 0.5 |
| 69 | + minim.min_step_struc = 0.5 |
| 70 | + minim.meaningful_factor = 1e-10 |
| 71 | + minim.init() |
| 72 | + minim.run() |
| 73 | + minim.finalize() |
| 74 | + |
| 75 | + # Check the differences in the atomic positions |
| 76 | + delta_s = np.max(np.abs(dyn_target.structure.coords - minim.dyn.structure.coords)) |
| 77 | + starting_delta_s = np.max(np.abs(minim.dyn.structure.coords - dyn_start.structure.coords)) |
| 78 | + starting_delta = np.max(np.abs(minim.dyn.dynmats[0] - dyn_start.dynmats[0])) |
| 79 | + |
| 80 | + |
| 81 | + # Compare the final dynamical matrices |
| 82 | + delta = np.max(np.abs(dyn_target.dynmats[0] - minim.dyn.dynmats[0])) |
| 83 | + |
| 84 | + if verbose: |
| 85 | + print("The difference with the original struct is {}".format(starting_delta_s)) |
| 86 | + print("The difference on the structure is {}".format(delta_s)) |
| 87 | + print("The difference on the original FC is {}".format(starting_delta)) |
| 88 | + print("The difference on FC is {}".format(delta)) |
| 89 | + |
| 90 | + EPS = 1e-5 |
| 91 | + assert delta_s < EPS, "Error, the structure is displaced by {} > {}".format(delta_s, EPS) |
| 92 | + assert delta < EPS, "Error, the difference is: {} > {}".format(delta, EPS) |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | +if __name__ == "__main__": |
| 97 | + test_update_weights(True) |
| 98 | + test_simple_relax(True) |
0 commit comments