Unverified Commit 50fed00c authored by Jan Janssen's avatar Jan Janssen Committed by GitHub
Browse files

Update WorkshopPotentialEAM.ipynb

parent 9ab648ee
......@@ -182,9 +182,9 @@
}
],
"source": [
"data_pr = Project(\"../../datasets/imported_datasets/\")\n",
"data_pr = Project(\"../../datasets/\")\n",
"if len(data_pr.job_table()) == 0:\n",
" data_pr.unpack(\"../../datasets/Cu_training_archive\")\n",
" data_pr.unpack(\"Cu_training_archive\")\n",
"data_pr.job_table()"
]
},
......
%% Cell type:markdown id:ranking-inside tags:
# Fitting an EAM potential
EAM potentials are pair functionals.
In a generalised form they are equal to Finnis-Sinclair, effective medium theory or glue potentials. Their total energy can be written as
$E = \sum_{ij}V(r_{ij}) + \sum_i F(\rho_i)$
with
$\rho_i = \sum_j \rho(r_{ij})$
The original functions for V, $\rho$ and F were derived from different theories, but they can be chosen freely.
Fitting is done using atomicrex https://atomicrex.org. In the fit process an objective or cost function is minimized. The objective function is defined as
$\chi^2 = \sum_i w_i r_i$
where $w_i$ is a weight and $r_i$ is a residual that describes the difference to target values. This residual can be defined in different ways, so it is not possible to simply compare the residual for different fitting processes or codes. A more in depth explanation and some examples can be found on https://atomicrex.org/overview.html#objective-function.
%% Cell type:code id:honey-element tags:
``` python
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from pyiron import Project, ase_to_pyiron
```
%% Cell type:markdown id:governing-madagascar tags:
### Import the training data
%% Cell type:code id:constant-respect tags:
``` python
data_pr = Project("../../datasets/imported_datasets/")
data_pr = Project("../../datasets/")
if len(data_pr.job_table()) == 0:
data_pr.unpack("../../datasets/Cu_training_archive")
data_pr.unpack("Cu_training_archive")
data_pr.job_table()
```
%%%% Output: execute_result
id status chemicalformula job \
0 759 finished None df1_A1_A2_A3_EV_elast_phon
1 760 finished None df3_10k
2 761 finished None df2_1k
subjob projectpath \
0 /df1_A1_A2_A3_EV_elast_phon /home/niklas/pyiron/projects/
1 /df3_10k /home/niklas/pyiron/projects/
2 /df2_1k /home/niklas/pyiron/projects/
project \
0 pyiron_potentialfit/datasets/imported_datasets/Cu_database/
1 pyiron_potentialfit/datasets/imported_datasets/Cu_database/
2 pyiron_potentialfit/datasets/imported_datasets/Cu_database/
timestart timestop totalcputime computer \
0 2021-02-08 10:33:52.341472 None None zora@cmti001#1
1 2021-02-08 10:33:53.993230 None None zora@cmti001#1
2 2021-02-08 10:33:54.435308 None None zora@cmti001#1
hamilton hamversion parentid masterid
0 TrainingContainer 0.4 None None
1 TrainingContainer 0.4 None None
2 TrainingContainer 0.4 None None
%% Cell type:code id:dirty-measurement tags:
``` python
data_job = data_pr.load("df1_A1_A2_A3_EV_elast_phon")
df = data_job.to_pandas()
```
%% Cell type:code id:referenced-julian tags:
``` python
pr = Project("WorkshopPotential")
pr.remove_jobs()
```
%%%% Output: stream
Are you sure you want to delete all jobs from 'WorkshopPotential'? y/(n) y
%% Cell type:markdown id:voluntary-limit tags:
### Create an atomicrex job
%% Cell type:code id:entertaining-jacksonville tags:
``` python
job = pr.create_job(pr.job_type.Atomicrex, "PotentialDF1")
```
%% Cell type:markdown id:raising-clear tags:
### Add the structures that should be fitted.
It is possible to assign different weights to certain structures or properties, depending on what should be investigated using the potential. Here every structure has the same weight, but the force vector with N*3 values is normalized to have the same total weight as the single value energy. Therefore it is divided by the number of atoms.
%% Cell type:code id:located-individual tags:
``` python
for id, row in df.iterrows():
struct = ase_to_pyiron(row.atoms)
s = job.structures.add_structure(struct, f"id{id}", relative_weight=1)
s.fit_properties.add_FitProperty("atomic-energy", target_value=row.energy/row.number_of_atoms, relative_weight=1)
s.fit_properties.add_FitProperty("atomic-forces", target_value=row.forces, relative_weight=1/row.number_of_atoms)
```
%% Cell type:markdown id:angry-leader tags:
### Define the type of potential and necessary functions.
In this case an eam potential is fitted.
%% Cell type:code id:functional-formation tags:
``` python
job.potential = job.factories.potentials.eam_potential()
```
%% Cell type:markdown id:realistic-karaoke tags:
It is necessary to define a pair potential, an electronic density function and an embedding function.
For all of those it is possible to choose between different functional forms.
Classic pair potentials are physically motivated and have a very limited number of paramaters that are derived from a experimentally measured quantity.
Splines or polynomials offer more flexibility, but can lead to unphysical oscillations or overfitting. Compared with the machine learning potentials shown later the number of parameters is very low no matter which functions you choose and the problem is highly non linear.
In this case a generalized morse function is used for the pair interaction. It has the form
$(\frac{D_0}{S-1}exp(-\beta \sqrt{2S}(r-r_0))-\frac{D_0S}{S-1}exp(-\beta\sqrt{2/S}(r-r_0)))+\delta $
The parameters in the morse potential can be derived from phyiscal quantities, but in this case they are just educated guesses. For example $r_0$ is the equilibrium distance of a dimer. The nearest neighbor distance in fcc Cu is about 2.5 $\mathring A$ so it is taken as initial value.
In the case of analytic functions the initial parameter choices should not matter too much, since the functional form is constrained.
The electronic density and embedding function will be splines. Depending on the properties that are calculated other functional forms could give better results. The inital parameters require more testing and hand tuning than the parameters of analytic functions.
%% Cell type:code id:interpreted-orange tags:
``` python
V = job.factories.functions.morse_B(identifier="V_CuCu", D0=0.35, r0=2.5, beta=2, S=2, delta=0)
```
%% Cell type:code id:mathematical-gasoline tags:
``` python
V.parameters.D0.min_val = 0
V.parameters.D0.max_val = 2
V.parameters.r0.min_val = 1.5
V.parameters.r0.max_val = 3.0
V.parameters.S.min_val = 1.1
V.parameters.S.max_val = 10.0
V.parameters.delta.min_val = -1
V.parameters.delta.max_val = 1
V.parameters.beta.min_val = 0.1
V.parameters.beta.max_val = 10
```
%% Cell type:markdown id:written-commission tags:
Additionally a screening function needs to be defined for the morse potential
%% Cell type:code id:discrete-terminology tags:
``` python
V.screening = job.factories.functions.exp_A_screening(identifier="V_cutoff", cutoff=7)
```
%% Cell type:markdown id:wireless-parts tags:
The electron density is chosen to be a spline function. The cutoff has to be defined. Derivatives left and right are optional, they default to 0. For the right cutoff this is fine, since the forces should smoothly go to 0. For the left this is not necessarily the best choice, since the function value should increase at very close distances. Very large absolute values will lead to osciallations and should be avoided.
%% Cell type:code id:authentic-expression tags:
``` python
rho = job.factories.functions.spline(identifier="rho_CuCu", cutoff=7, derivative_left=-1)
```
%% Cell type:markdown id:bored-afternoon tags:
For a spline function it is necessary to define node points. They can be equally spaced or sampled with higher density around turning points, f.e. the first neighbor distance.
Too few nodepoints mean low flexibilty, too many lead to overfitting. This requires some testing to find an optimal choice.
%% Cell type:code id:hidden-wildlife tags:
``` python
rho_nodes = np.linspace(0.5, 7.0, 7).round(2)
rho_nodes
```
%%%% Output: execute_result
array([0.5 , 1.58, 2.67, 3.75, 4.83, 5.92, 7. ])
%% Cell type:markdown id:binary-devil tags:
The nodes need initial values. The electron density should be proportional to $e^{-r}$, so this function is chosen to calculate them.
%% Cell type:code id:comparative-brush tags:
``` python
decaying_exp = lambda r: np.exp(-r)
rho_initial = decaying_exp(rho_nodes)
```
%% Cell type:markdown id:gentle-infrastructure tags:
Additionally it is a good idea to define limits for the node points. This is optional for local minimizers, but the fit can quickly run away without limits. Global optimizers typically require them to constrain the sampled space.
A density can't be negative so the lower limit is set to 0. The upper limit is chosen to be 3 times the initial values. These choices aswell as the choice for $e^{-r}$ as initial values are somewhat arbitrary, but don't matter much. The electron density from single atoms does not directly influence the calculated energies and forces, instead the summed up density at some place is used in the embedding function, so the final numerical values are an interplay between electron density and embedding function. Since the latter will also be a spline function it can only be defined for a certain range of rho values as node points. Therefore it is better to limit the range of electron density values and define larger limits for the embedding function instead.
%% Cell type:code id:funny-trinidad tags:
``` python
rho_mins = np.zeros((len(rho_nodes)))
rho_maxs = 3*rho_initial.round(6)
rho.parameters.create_from_arrays(rho_nodes, rho_initial, min_vals=rho_mins, max_vals=rho_maxs)
```
%% Cell type:raw id:promising-draft tags:
Finally the last node point at the cutoff range is set to 0 and fitting is disabled to prevent a discontinuous change of energy at the cutoff.
%% Cell type:code id:mexican-absence tags:
``` python
rho.parameters["node_7.0"].start_val = 0
rho.parameters["node_7.0"].enabled = False
```
%% Cell type:markdown id:standard-relative tags:
$-\sqrt(\rho)$ can be used as initial guess for the embedding energy, which is taken from second moment approximation tight binding.
The node points have to be chosen in a range compatible to the electron density. This can be estimated by calculating it for a densely packed structure.
Alternatively atomicrex writes the maximum electron density of all structures to the output. This can be used as a hint for the node points for consequent fits.
Everything else is similar to the electron density.
%% Cell type:code id:large-rating tags:
``` python
F = job.factories.functions.spline(identifier="F_CuCu", cutoff=5)
F_nodes = np.linspace(0.0, 5.0, 7).round(2) #9 is worse 11 is worse 7 is best
F_init = -np.sqrt(F_nodes)
F_maxs = np.zeros(len(F_nodes))
F_mins = -np.ones(len(F_nodes))*5
F.parameters.create_from_arrays(F_nodes, F_init, F_mins, F_maxs)
F.parameters["node_0.0"].enabled=False
F.parameters["node_0.0"].start_val = 0
F.parameters["node_5.0"].enabled=False
F.parameters["node_5.0"].start_val = 0
```
%% Cell type:markdown id:several-mercy tags:
The functions have to be assigned to the potential
%% Cell type:code id:heavy-acoustic tags:
``` python
job.potential.pair_interactions[V.identifier] = V
job.potential.electron_densities[rho.identifier] = rho
job.potential.embedding_energies[F.identifier] = F
```
%% Cell type:markdown id:alien-chancellor tags:
### Define fitting procedure
Finally a few parameters need to be set that influence the fitting process.
The minimization can be done with different algorithms. Atomicrex itself implements the BFGS algorithm. Additionally the algorithms from the nlopt library can be used.
%% Cell type:code id:enormous-segment tags:
``` python
## Define the atom types of the potential
job.input.atom_types.Cu = None
## Limited number of steps for the workshop
job.input.fit_algorithm = job.factories.algorithms.ar_lbfgs(max_iter=2000)
job.run()
job.output
```
%%%% Output: stream
The job PotentialDF1 was saved and received the ID: 819
%%%% Output: execute_result
Output({'error': None, 'residual': array([1.39371e+03, 1.39371e+03, 1.39371e+03, ..., 1.52231e-01,
1.52231e-01, 1.52231e-01]), 'iterations': array([ 1, 2, 3, ..., 1998, 1999, 2000], dtype=uint32)})
%% Cell type:markdown id:vanilla-chocolate tags:
Plot the resiudal over steps to see how the calculation converges
%% Cell type:code id:aging-backing tags:
``` python
plt.plot(job.output.iterations, job.output.residual)
#plt.ylim(0,5)
```
%%%% Output: execute_result
[<matplotlib.lines.Line2D at 0x7fdb336c5ee0>]
%%%% Output: display_data
%% Cell type:markdown id:informed-formula tags:
Finally it is a good idea to have a look at the final potential. This can reveal unphysical behavior
%% Cell type:code id:resident-gnome tags:
``` python
job.plot_final_potential()
```
%%%% Output: execute_result
(<Figure size 576x1296 with 3 Axes>,
array([[<AxesSubplot:title={'center':'Cu F'}, xlabel='$\\rho $ [a.u.]'>],
[<AxesSubplot:title={'center':'Cu rho_CuCu'}, xlabel='r [$\\AA$]'>],
[<AxesSubplot:title={'center':'Cu V_CuCu'}, xlabel='r [$\\AA$]'>]],
dtype=object))
%%%% Output: display_data
%% Cell type:code id:adult-democracy tags:
``` python
lmp = pr.create_job("Lammps", "template", delete_existing_job=True)
lmp.structure = pr.create_ase_bulk("Cu", cubic=True)
lmp.potential = job.lammps_potential
murn = lmp.create_job("Murnaghan", "PotentialTest")
murn.run(delete_existing_job=True)
```
%%%% Output: stream
The job PotentialTest was saved and received the ID: 820
The job strain_0_9 was saved and received the ID: 821
The job strain_0_92 was saved and received the ID: 822
The job strain_0_94 was saved and received the ID: 823
The job strain_0_96 was saved and received the ID: 824
The job strain_0_98 was saved and received the ID: 825
The job strain_1_0 was saved and received the ID: 826
The job strain_1_02 was saved and received the ID: 827
The job strain_1_04 was saved and received the ID: 828
The job strain_1_06 was saved and received the ID: 829
The job strain_1_08 was saved and received the ID: 830
The job strain_1_1 was saved and received the ID: 831
job_id: 821 finished
job_id: 822 finished
job_id: 823 finished
job_id: 824 finished
job_id: 825 finished
job_id: 826 finished
job_id: 827 finished
job_id: 828 finished
job_id: 829 finished
job_id: 830 finished
job_id: 831 finished
%% Cell type:code id:vocal-heather tags:
``` python
murn.plot()
```
%%%% Output: display_data
%% Cell type:code id:attached-palestinian tags:
``` python
murn["output/equilibrium_bulk_modulus"]
```
%%%% Output: execute_result
140.0186998321462
%% Cell type:markdown id:injured-rainbow tags:
### Same cane be done for the 1000 structures dataset
The final parameters of the 100 structure fit can be used for the 1000 Structure fit. This can speed up the fitting process and often leads to better results, especially if the initially guessed values are far from the optimum.
In general it is a good idea to start with few structures and try around with different functions and initial parameters. This is much faster than using all structures from the beginning and gives good guesses for the initial values of the parameters. It also allows to use global optimization with millions of steps in short time spans.
### This can take long and writes 1000 seperate POSCAR files, TAKE CARE
Run these jobs with more cores or more time after the workshop. Also increase the number of iterations for better results
%% Cell type:code id:focal-rehabilitation tags:
``` python
pr = Project("PotentialDF2")
pr.remove_jobs()
j = pr.create_job(pr.job_type.Atomicrex, "PotentialDF2")
j.potential = job.potential.copy()
## Use the final parameters as starting values for the new fit
j.potential.copy_final_to_initial_params()
```
%%%% Output: stream
Are you sure you want to delete all jobs from 'PotentialDF2'? y/(n) n
%%%% Output: stream
No jobs removed from 'PotentialDF2'.
%% Cell type:code id:bibliographic-wonder tags:
``` python
df = data_pr.load("df2_1k").to_pandas()
for id, row in df.iterrows():
struct = ase_to_pyiron(row.atoms)
s = j.structures.add_structure(struct, f"id{id}", relative_weight=1)
s.fit_properties.add_FitProperty("atomic-energy", target_value=row.energy/row.number_of_atoms, relative_weight=1)
s.fit_properties.add_FitProperty("atomic-forces", target_value=row.forces, relative_weight=1/row.number_of_atoms)
```
%% Cell type:code id:taken-remove tags:
``` python
import time
```
%% Cell type:code id:loved-princess tags:
``` python
j.input.atom_types.Cu = None
j.input.fit_algorithm = j.factories.algorithms.ar_lbfgs(max_iter=100000)
## if possible increase number of cores
#j.server.cores = 16
t1 = time.time()
## Uncomment if you want to run the job
#j.run()
t2 = time.time()
```
%% Cell type:code id:sunrise-brother tags:
``` python
t2-t1
```
%%%% Output: execute_result
2.1457672119140625e-05
%% Cell type:code id:therapeutic-treasure tags:
``` python
j.output
```
%%%% Output: execute_result
Output({'error': None, 'iterations': array([ 1, 2, 3, ..., 5594, 5595, 5596], dtype=uint32), 'residual': array([758.612 , 758.612 , 758.612 , ..., 58.7461, 58.7461, 58.7461])})
%% Cell type:markdown id:composite-porter tags:
This is the result if the initilly guessed values are taken instead of the fitted ones.
%% Cell type:code id:regulated-document tags:
``` python
j2 = pr.create_job(pr.job_type.Atomicrex, "PotentialDF2_BadStartParams", delete_existing_job=True)
j2.potential = job.potential.copy()
j2.input.atom_types.Cu = None
j2.input.fit_algorithm = j.factories.algorithms.ar_lbfgs(max_iter=100000)
j2.structures = j.structures
## if possible increase number of cores
j2.server.cores = 16
## Uncomment if you want to run the job
j2.run()
```
%%%% Output: stream
The job PotentialDF2_BadStartParams was saved and received the ID: 832
%% Cell type:code id:greek-infrastructure tags:
``` python
j2.output
```
%%%% Output: execute_result
Output({'error': None, 'residual': array([5717.28 , 5717.28 , 5717.28 , ..., 58.7461, 58.7461,
58.7461]), 'iterations': array([ 1, 2, 3, ..., 5289, 5290, 5291], dtype=uint32)})
%% Cell type:markdown id:twenty-collins tags:
With this choice of functions and initial parameters starting directly from all structures gives the same residual. In a previous iteration of the potential it was about 7 times worse, so it is a good idea to test this.
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment