.. index:: Tutorial
.. _QuickStartTutorial:
Quickstart Tutorial
===================
In this tutorial, we will create an XMDS2 script to solve the Lorenz Attractor, an example of a dynamical system that exhibits chaos. The equations describing this problem are
.. math::
\frac{dx}{dt} &= \sigma (y - x)\\
\frac{dy}{dt} &= x (\rho - z) - y\\
\frac{dz}{dt} &= xy - \beta z
where we will solve with the parameters :math:`\sigma=10`, :math:`\rho=28`, :math:`\beta = \frac{8}{3}` and the initial condition :math:`x(0) = y(0) = z(0) = 1`.
Below is a script that solves this problem (it's also saved as examples/lorenz.xmds in your XMDS2 directory). Don't worry if it doesn't make sense yet, soon we'll break it down into easily digestible parts.
.. code-block:: xpdeint
lorenz
Graham Dennis
The Lorenz Attractor, an example of chaos.
t
x y z
5000
position
You can compile and run this script with **XMDS2**. To compile the script, just pass the name of the script as an argument to **XMDS2**.
.. code-block:: none
$ xmds2 lorenz.xmds
xmds2 version 2.1 "Happy Mollusc" (r2680)
Copyright 2000-2012 Graham Dennis, Joseph Hope, Mattias Johnsson
and the xmds team
Generating source code...
... done
Compiling simulation...
... done. Type './lorenz' to run.
Now we can execute the generated program 'lorenz'.
.. code-block:: none
$ ./lorenz
Sampled field (for moment group #1) at t = 0.000000e+00
Sampled field (for moment group #1) at t = 4.000000e-03
Current timestep: 4.000000e-03
Sampled field (for moment group #1) at t = 8.000000e-03
Current timestep: 4.000000e-03
... many lines omitted ...
Current timestep: 4.000000e-03
Sampled field (for moment group #1) at t = 1.999600e+01
Current timestep: 4.000000e-03
Sampled field (for moment group #1) at t = 2.000000e+01
Current timestep: 4.000000e-03
Segment 1: minimum timestep: 9.997900e-06 maximum timestep: 4.000000e-03
Attempted 7386 steps, 0.00% steps failed.
Generating output for lorenz
.. index:: Exporting data
The program generated by **XMDS2** has now integrated your equations and produced two files. The first is the XML file "lorenz.xsil", which contains the all the information used to generate the simulation (including the XMDS2 code) and the metadata description of the output. The second file is named "lorenz.h5", which is a `HDF5 `_ file containing all of the output data. You can analysing these files yourself, or import them into your favourite visualisation/postprocessing tool. Here we will use the example of importing it into Mathematica. We run the included utility 'xsil2graphics2'.
.. code-block:: none
$ xsil2graphics2 -e lorenz.xsil
xsil2graphics2 from xmds2 version 2.1 "Happy Mollusc" (r2680)
Generating output for Mathematica 6+.
Writing import script for 'lorenz.xsil' to 'lorenz.nb'.
This has now generated the file 'lorenz.nb', which is a Mathematica notebook that loads the output data of the simulation. Loading it into Mathematica allows us to plot the points {xR1, yR1, zR1}:
.. code-block:: none
ll = Transpose[{xR1, yR1, zR1}];
ListPointPlot3D[ll]
.. image:: images/lorenz.*
:align: center
...and we see the lobes of the strange attractor. Now let us examine the code that produced this simulation.
First, we have the top level description of the code.
.. code-block:: xpdeint
lorenz
Graham Dennis
The Lorenz Attractor, an example of chaos.
One of the advantages of an XML format is that these tags are almost entirely self-explanatory. XMDS2 files follow full XML syntax, so elements can be commented out using the ```` brackets, and we have an example of that here.
The first line, ````, just specifies the encoding and XML version. It is optional, but its presence helps some text editors perform the correct syntax highlighting.
The ```` element is mandatory, and encloses the entire simulation script.
The ```` element is optional, but recommended. It defines the name of the executable program that will be generated, as well as the default name of the output data files (although this can be over-ridden in the ``