Atomsk

The Swiss-army knife of atomic simulations

Tutorial: Work with bash scripts

In this tutorial, you will learn how to use Atomsk as a part of bash scripts. It is recommended to be already familiar with bash to proceed with this tutorial.

It is considered good practice to keep a trace of your work. In case of atomistic simulations, you may want to write down how you constructed your system, how you designed it and so on, especially if it is a very complicated system. This way, if you or someone else wish to construct the same atomic system, you can run again the same script.

This can be achieved by writing down your commands in scripts. Bash is a powerful interpreter that can handle variables, conditions, loops, and much more.

NOTE: this tutorial does not aim to teach you how to use bash, but simply how to take advantage of bash when constructing atomic systems with Atomsk. If you wish to learn bash or have questions about bash, please search for information elsewhere before proceeding with this tutorial.

1. Write commands in a bash script

A bash script is nothing else but a text file, that contains commands that can be executed by the bash interpreter. When writing a bash script, at least two things should be respected:

As an example, you may create a unit cell of aluminium by writing the following lines in a text file called "aluminum.sh":

aluminum.sh

#!/bin/bash

atomsk --create fcc 4.046 Al xsf

And then, run this script by calling bash:

bash aluminum.sh

Bash will execute the commands written in the script sequentially, i.e. one after the other. In the script above, there is only one command to execute (which is atomsk).

You may (or may not, at your convenience) make the bash script executable with the command:

chmod +x aluminum.sh

If you do, you can directly execute the script:

./aluminum.sh

2. Use bash variables

In a bash script, it is possible to define variables, and then use these variables when calling Atomsk. For instance, in the script above we can define a variable called a to store the value of the lattice constant. Then, when running Atomsk we can use this variable in place of the actual number:

aluminum.sh

#!/bin/bash

a=4.046
atomsk --create fcc $a Al xsf

When running this script, the bash interpreter will automatically replace the $a by the value of the variable, before actually running Atomsk. So, Atomsk will never know that a variable was used, as it will just receive a number.

Variables can be used as substitutes in all modes or options of Atomsk. It may replace a lattice constant, a position, a Burgers vector when constructing dislocations, and so on. The advantage is that you can run exactly the same script, changing only the value of the relevant variable, instead of modifying the Atomsk command-line.

3. Use bash loops

Another useful feature of bash is the capability to repeat operations with loops.

For example, imagine that you wish to construct unit cells of aluminium with different lattice parameters. You can write a loop where you increase the lattice constant from 3.5 to 4.5 Å:

aluminum.sh

#!/bin/bash

for i in {0..10..1} ; do
  a=$(echo "3.5 + 0.1*$i" | bc -l)
  atomsk --create fcc $a Al Al_${a}.xsf
done

This time, the names of the output files will also contain the lattice parameter, to differentiate them: this script will create files named "Al_3.5.xsf", "Al_3.6.xsf", and so on up to "Al_4.5.xsf".

4. Other possibilities

Bash is much more versatile than the few examples above show. You can design very complex systems, run simulations, and perform analysis, all into one bash script. You may have a look at the script named "sto.sh" contained in the folder "/examples/SrTiO3_gamma_surface/" provided with Atomsk. This script constructs atomic systems of strontium titanate with different stacking faults, performs LAMMPS calculations on each of them, and outputs the γ-surface in a data file.

Of course, it is also possible to use other programs than bash, like zsh, Python, or other languages. Users of Microsoft® Windows® systems may write scripts using the batch language. This is all left to your preferences.