# Initialize MaZe algorithm

As described in the [NVE page](NVE-ensemble) and in the [NVT page](NVT-ensemble), the MaZe algorithm solves the equations of motion in presence of adiabatic constraints for the generic additional variables (electrode charges for constant potential simulations or electrolyte dipoles for polarizable force fields) using a combination of Verlet algorithm to evolve the additional variables and the SHAKE method to find the Lagrange multipliers that satisfy the constraints.
The Verlet algorithm, in particular, requires the values of the additional variables at two previous timesteps, say 0 and $`-\Delta t`$ to be able to yield a solution for the new value at the next timestep, say $`\Delta t`$.
In Metalwalls this can be accomplished in many ways.

## Using a python script provided in `./tools`

This is definitely the recommended way to run a simulation using the MaZe algorithm. Prepare the `runtime.inpt` and the `data.inpt` files as if the simulation was to be run with any other method, regardless of how many steps are provided in the `data.inpt` file.
Once the input files are ready, launch the script in the folder where the simulation will be performed. The script will:

   1. Read the input files to check for possible errors in the number of previous values provided for the additional variables.

   2. If some of the necessary values are not provided, the script will launch a short simulation of two timesteps to obtain the necessary quantities to properly start a simulation with MaZe.

   3. As the initial configuration will change, a backup copy of the original `data.inpt` file will be saved in the working folder with the name `data.inpt.bck`.

A few warnings are in order when the script is used: first, note that, depending on the size of the system, the generation of the initial configuration can take time. Second, as already mentioned in point 3. of the previous list, the initial configuration of the actual simulation will be slightly different from the original one (more precisely, it will be two timesteps *in the future* with respect to the original one).

Once the script has successfully run, the simulation with MaZe can be safely launched.

## With a restart file from a previous simulation

If a restart file is available from a previous simulation, it is important to check the following things:

   + For simulations with polarizable force fields, that two previous values of the dipoles (keyword `# dipoles` in the `data.inpt` input file) are available.
   + For constant potential simulations **without the charge neutrality constraint**, that two previous values of the charges (keyword `# charges` in the `data.inpt` input file) are available.
   + For constant potential simulations **with the charge neutrality constraint**, that two previous values of the charges (keyword `# charges` in the `data.inpt` input file) and two previous values of the potential shift (keyword `# potential_shift` in the `data.inpt` input file) are available.

Anything different from what is specified above will result in an error. In addition, it is fundamental that the simulation that has produced the `data.inpt` file from which the values at previous timesteps are read had the same timestep size of the simulation to be performed with MaZe.

**WARNING:** If the simulation that is run with the MaZe algorithm and the one from which the `data.inpt` file has been produced have different timestep sizes, no error is raised by Metalwalls and the simulation would start normally, but artifacts or errors could be generated as the simulation proceed. The python script described in the previous section, on the other hand, is able to check this aspect in most of the situations (in particular, whenever the `data.inpt` file has been generated by MW itself). **Therefore, whenever possible, use the script!**

## Manually

If, for some reason, one of the above options cannot be used, Metalwalls could be employed manually to produce a restart file that meets the above criteria.

It is indeed sufficient, before launching the real simulation, to run a short simulation saving the restart file at the last step.
For this preliminary simulation, the additional variables should be computed using another method among the ones available in the code.
If the chosen method is the conjugate gradient, the tolerance specified should be very strict (usually $`10^{-12}`$ is strict enough), in order to satisfy the adiabatic constraint at numerical precision from the beginning of the simulation.

In particular, the file `runtime.inpt` of this preliminary simulation should have the following parameter specified
```
nsteps    2
output
   restart   2
```
and the specification of the method to compute the additional variable should be given, for example, by
```
species
   dipole_minimization   cg   1.0e-12
```
for simulations with polarizable force fields or
```
electrodes
   electrode_charges   cg   1.0e-12
```
for constant potential simulations. The use of the keyword `matrix_inversion` is also allowed, but not recommended for polarizable force field.

Every other simulation parameter should be the same as the real simulation which is going to be performed using MaZe. At the end of the process, remember to rename the file `restart.000000002.out` as `data.inpt` and to change the keyword for the minimization method in the file `runtime.inpt` accordingly to the particular MaZe algorithm to be used for the actual run.