Running a script

To get started with BornAgain scripting, run a first example.

We assume that BornAgain and Python are installed and that you verified that the Python interpreter can find the module bornagain.

We shall now run a first example script. This and all other example scripts can also be found in the BornAgain distribution, in directory Examples.

Download the following example script, using the link just below the code frame. Save the script under the name AlternatingLayers.py.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/env python3
"""
Basic example of specular reflectometry simulation with BornAgain.
The sample consists of 20 alternating Ti and Ni layers.
"""

from bornagain import ba_plot as bp, std_samples, std_simulations


def get_sample():
    return std_samples.alternating_layers()


def get_simulation(sample):
    n = bp.simargs['n']
    return std_simulations.specular(sample, n)


if __name__ == '__main__':
    bp.parse_args(sim_n=500)
    sample = get_sample()
    simulation = get_simulation(sample)
    result = simulation.simulate()
    bp.plot_simulation_result(result)
Examples/specular/AlternatingLayers.py

For a discussion of the content of this script, see simulation/reflectometry.

From the command line

At the command prompt in a terminal, launch the script with the command

python AlternatingLayers.py

(on systems that still have Python2, the command may rather be python3).

As result, a MatPlotLib window should pop up, and display this reflectometry curve:

Short call (Linux, Mac)

Under a Unix shell, the script can also be launched without typing python at the command prompt:

AlternatingLayers.py

This is made possible by the line “#!/usr/bin/env python3” on top of the script. It may be necessary to make the script executable by

chmod a+x AlternatingLayers.py