Are you sure you want to delete all jobs from 'WorkshopPotential'? y/(n) y

%% Cell type:code id:finite-planner tags:

``` python

job=pr.create_job(Atomicrex,"PotentialDF1")

```

%% Cell type:markdown id:fiscal-advocate 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.

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 are not directly physically motivated and can lead to unphysical oscillations or overfitting.

In this case a generalized morse function is used for the pair interaction, while the electronic density and embedding function will be splines. Depending on the properties that are calculated other functional forms could give better results.

The parameters in D0=3.5 and r0=1.8 are the approximate cohesive energy and the equilibrium lattice constant. Beta and S can also be derived from physical quantities but are chosen randomly in a typical range in this case. Delta is a parameter that shifts the whole function up or down. The initial parameter choices should not matter too much as long as they are somewhat reasonable since they will be optimized in the fitting process anyway.

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.

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:double-engineering 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:handled-housing 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:developing-apache tags:

``` python

decaying_exp=lambdar:np.exp(-r)

rho_initial=decaying_exp(rho_nodes)

```

%% Cell type:markdown id:separated-journal 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.

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:lasting-reply tags:

``` python

rho.parameters["node_7.0"].start_val=0

rho.parameters["node_7.0"].enabled=False

```

%% Cell type:markdown id:saving-vegetable 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.

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.

The final parameters of the 100 structure fit can be used for the 1000 Structure fit. This speeds up the fitting process and often leads to better results.

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

%% Cell type:code id:preceding-march tags:

``` python

j=pr.create_job(Atomicrex,"PotentialDF2")

j.potential=job.potential.copy()

## Use the final parameters as starting values for the new fit