|
1 | | -Customizing the SSCHA |
2 | | -===================== |
| 1 | +RUN THE SSCHA |
| 2 | +============= |
| 3 | + |
| 4 | + |
| 5 | +The python-sscha code can be runned both as a stand-alone application with an input file and as a python library, writing a python script. |
| 6 | + |
| 7 | +We will cover the python scripting as it is more general. |
| 8 | + |
| 9 | +The SSCHA calculation is divided into 3 main steps: |
| 10 | + - The generation of a random ensemble of ionic configurations |
| 11 | + - Calculations of energies and forces on the ensemble |
| 12 | + - The SSCHA free energy minimization |
| 13 | + |
| 14 | +Then this steps are iterated until convergence is achieved. |
| 15 | +This iteration can either be done manually or automatically by the code. |
| 16 | + |
| 17 | +It is always suggested to use the automatic way, however, for completness, here we cover both aspects. |
| 18 | + |
| 19 | +Manual submission |
| 20 | +----------------- |
| 21 | + |
| 22 | +The manual submission means that the user manually computes the energies and forces of all the configurations in the ensemble. The code stops after generating the random ensemble, and the user is requested to provide data files that contain the forces and total energies for each configuration. |
| 23 | + |
| 24 | +Thus, the code works in two steps. |
| 25 | +In the first step we generate the ensemble. Here is the code |
| 26 | + |
| 27 | +.. code-block:: python |
| 28 | +
|
| 29 | + import sscha, sscha.Ensemble, sscha.SchaMinimizer |
| 30 | + import cellconstructor as CC, cellconstructor.Phonons |
| 31 | + |
| 32 | + # Load the harmonic dynamical matrix |
| 33 | + dyn = CC.Phonons.Phonons('dyn', nqirr = 4) |
| 34 | + |
| 35 | + # If the dynamical matrix contains imaginary frequencies |
| 36 | + # we get rid of them with |
| 37 | + dyn.ForcePositiveDefinite() |
| 38 | +
|
| 39 | + # Now we initialize the ensemble at the target temperature |
| 40 | + TEMPERATURE = 300 # Kelvin |
| 41 | + ensemble = sscha.Ensemble.Ensemble(dyn, TEMPERATURE) |
| 42 | +
|
| 43 | + # We generate the random ensemble with N_CONFIGS configurations |
| 44 | + N_CONFIGS = 1000 |
| 45 | + ensemble.generate(N_CONFIGS) |
| 46 | +
|
| 47 | + # We save the ensemble on the disk (inside directory data) |
| 48 | + # We specify an integer 'population' which distinguish several ensembles |
| 49 | + # inside the same directory |
| 50 | + ensemble.save('data', population = 1) |
| 51 | +
|
| 52 | + |
| 53 | +
|
| 54 | +To start, we need an initial guess of the dynamical matrix (the dyn file). |
| 55 | +The default format is the one of Quantum ESPRESSO, but also phonopy and |
| 56 | +ASE formats are supported (refer to the CellConstructor documentation to load these formats). Here we assume that the dynamical matrices are 4 (4 irreducible q points) called 'dyn1', 'dyn2', 'dyn3' and 'dyn4', as the standard quantum espresso format. |
| 57 | + |
| 58 | +The dynamical matrix contain both the information about the atomic structure |
| 59 | +and the ionic fluctuations. These can be obtained with a linear response |
| 60 | +calculation from DFT. |
| 61 | + |
| 62 | +The previous code generates the ensemble which is stored in the disk. |
| 63 | +Inside the data directory you will find a lot of files |
| 64 | + |
| 65 | +The files named 'scf_population1_X.dat' with X going over all the configurations contain the atomic structure in cartesian coordinates. It uses the standard espresso formalism. |
| 66 | + |
| 67 | +You need to compute total energies and forces of each configuration, with your favourite code. |
| 68 | +The total energies are written in column inside the file 'total_energies_population1.dat', in Rydberg atomic units and ordered with the index of the configurations. |
| 69 | +The forces for each configuration should be inside 'forces_population1_X.dat' in Ry/Borh (Rydberg atomic units). |
| 70 | + |
| 71 | +When you compute energies and forces, you can load them and run the SSCHA minimization: |
| 72 | + |
| 73 | +.. code-block:: python |
| 74 | +
|
| 75 | + import sscha, sscha.Ensemble, sscha.SchaMinimizer |
| 76 | + import cellconstructor as CC, cellconstructor.Phonons |
| 77 | + |
| 78 | + # Load the harmonic dynamical matrix |
| 79 | + dyn = CC.Phonons.Phonons('dyn', nqirr = 4) |
| 80 | + |
| 81 | + # If the dynamical matrix contains imaginary frequencies |
| 82 | + # we get rid of them with |
| 83 | + dyn.ForcePositiveDefinite() |
| 84 | +
|
| 85 | + # Now we initialize the ensemble at the target temperature |
| 86 | + TEMPERATURE = 300 # Kelvin |
| 87 | + ensemble = sscha.Ensemble.Ensemble(dyn, TEMPERATURE) |
| 88 | +
|
| 89 | + # We load the ensemble |
| 90 | + N_CONFIGS = 1000 |
| 91 | + ensemble.load('data', population = 1, N = N_CONFIGS) |
| 92 | +
|
| 93 | + # Now we can run the sscha minimization |
| 94 | + minim = sscha.SchaMinimizer.SSCHA_Minimizer(ensemble) |
| 95 | + minim.init() |
| 96 | + minim.run() |
| 97 | +
|
| 98 | + # Print on stdout the final results |
| 99 | + minim.finalize() |
| 100 | +
|
| 101 | + # Save the output dynamical matrix |
| 102 | + minim.dyn.save_qe('final_dyn') |
| 103 | + |
| 104 | +
|
| 105 | +And that's it. You run your first manual calculation. |
| 106 | + |
| 107 | + |
| 108 | +Keep track of free energy, gradients and frequencies during minimization |
| 109 | +------------------------------------------------------------------------ |
| 110 | + |
| 111 | +It is convenient to store on the file the information during the minimization, as the Free Energy, its gradient values and the frequencies. |
| 112 | + |
| 113 | +To do this, we need to tell the code to save them into a file. |
| 114 | + |
| 115 | +Let us replace the 'minim.run()' line in the previous example with the following code: |
| 116 | + |
| 117 | +.. code-block:: python |
| 118 | +
|
| 119 | + import sscha.Utilities |
| 120 | + IO = sscha.Utilities.IOinfo() |
| 121 | + IO.SetupSaving('minim_data') |
| 122 | +
|
| 123 | + minim.run(custom_function_post = IO.CFP_SaveAll) |
| 124 | +
|
| 125 | +
|
| 126 | +If you run it again, the code produces (starting from verison 1.2) two data files: minim_data.dat and minim_data.freqs. |
| 127 | +You can plot all the minimization path (frequencies, free energy, gradients) calling the program: |
| 128 | + |
| 129 | +.. code-block:: bash |
| 130 | +
|
| 131 | + $ sscha-plot-data.py minim_data |
| 132 | +
|
| 133 | +The sscha-plot-data.py script is automatically installed within the SSCHA code. |
| 134 | + |
| 135 | + |
| 136 | +Automatic submission |
| 137 | +-------------------- |
| 138 | + |
| 139 | +TODO |
| 140 | + |
| 141 | + |
| 142 | +Cluster configuration |
| 143 | +--------------------- |
| 144 | + |
| 145 | +TODO |
| 146 | + |
| 147 | + |
| 148 | +Employ a custom function |
| 149 | +------------------------ |
3 | 150 |
|
4 | 151 | An interesting feature provided by the SSCHA code is the customization of the algorithm. The user has access to all the variables at each iteration of the minimization. |
5 | 152 | In this way, the user can print on files additional info or introduce constraints on the structure or on the dynamical matrix. |
|
0 commit comments