Example project

In this small tutorial we will show following:

  • Setup a new project called “examples”

  • Create initial conditions for a H2 region expansion

  • Run the simulations

  • Analyze data and create several plots and tables

New project

First of all we need to setup a new project:

apy --init-project examples

The above command will create a new project directory and a project class where you will store all general simulation settings:

./python/scripy/examples (project directory)
./python/scripy/examples/__init__.py. (project class)

Newly created project is now a standard Python module that can be in principle imported from any Python script. However, we will use it only indirectly in the following scripts.

class scripy.examples.project(name)

Project class ‘example’

This project class inherits arepy.scripy.project class.

init()

Project initialization

This method should include initial settings of all simulations within the project.

A default simulation directory should be set here:

        self.dirSim = "/home/hd/hd_hd/hd_wd148/wsexamples"

A first simulation called ‘001’ is set in a following way:

        self.sims['001'] = {
            'name':'hiiregion','setup':'emptybox',
            'job':{'nodes':1,'proc':40,'time':'1:00:00','type':'fat'},
            'units':{'length':apy.const.pc,'time':apy.const.yr},
            'opt':{}
        }

Download the source code of the project class.

Simple initial conditions

You may have noticed that the simulation ‘001’ with name ‘hiiregion’ is initialized by setup ‘emptybox’. Therefore, lets go to our simulation directory and create a new setup:

cd /my/simulation/directory
apy --init-setup emptybox

The above script will create several new files:

./python/scripy/examples/setups
./python/scripy/examples/setups/__init__.py
./python/scripy/examples/setups/emptybox (setup directory)
./python/scripy/examples/setups/emptybox/__init__.py (setup class)

The most important is the last file, because it contains all the setup directives for the simulation.

In order to make simple initial conditions for our HII region we can modify the setup class as follows:

class scripy.examples.setups.emptybox.setup(proj, *args, **opt)

Setup class of a project ‘example’

This setup class inherits arepy.scripy.setup class.

init()

Initial settings

Here we set the resolution of a desired grid:

        self.opt['nRes'] = 64
setupConfig(fileName, defValues)

Setup a configuration file

Download this configuration file (Config.sh) and copy it to the setup directory. We will use a class arepy.files.config to read the configuration file from the setup directory, modify and save it to the simulation directory. All the parameters are already preset, so we will use it as it is:

        with apy.files.config(self.dirSetup+'/Config.sh') as f:
            f.setValue(defValues)
            f.write(fileName)
setupParam(fileName, defValues)

Setup a parameter file

Do the same with the parameter file (param.txt) and update the corresponding setup function. In this case we will use a class arepy.files.param:

        with apy.files.param(self.dirSetup+'/param.txt') as f:
            f.setValue(defValues)
            f.write(fileName)
setupRun(fileName, defValues)

Setup a job script file

Optionally you can add also a run script that is used to store job parameters. For this refer to the class arepy.files.runsh and modify the setup scirpt as follows:

        with apy.files.runsh() as f:
            f.setValue(defValues)
            f.write(fileName)
setupSources(fileName)

Setup a file with sources

The simulation will have only a one ideal source (with emission of 1e50 photons per second) in the center (coordinates [0.5,0.5,0.5]) of the box. We can easily setup a corresponding file with sources using the class arepy.files.sources:

        coord = [0.5,0.5,0.5]              # in code units (BoxSize=1)
        sed = [ 0.0, 0.0, 1e50, 0.0, 0.0 ] # in photons per second
        with apy.files.sources() as f:
            f.addSource(coord,sed)
            f.write(fileName)
setupIcs(fileName)

Setup initial conditions

Finally, we have to create file with initial conditions and particle grid using the class arepy.files.snap. The corresponding part in the setup will look like this:

        with apy.files.snap(fileIcs,fmode='w') as f:

            # set a file header
            ngas = self.opt['nRes']**3
            f.setHeader({
                'NumPart_ThisFile':         [ngas,0,0,0,0,0],
                'NumPart_Total':            [ngas,0,0,0,0,0],
                'NumPart_Total_HighWord':   [0]*6,
                'MassTable':                [0.0]*6,
                'Redshift':                 0.0,
                'BoxSize':                  1.0,
                'NumFilesPerSnapshot':      1,
                'Omega0':                   0.0,
                'OmegaLambda':              0.0,
                'OmegaBaryon':              0.0,
                'HubbleParam':              1.0,
                'Flag_Sfr':                 0,
                'Flag_Cooling':             0,
                'Flag_StellarAge':          0,
                'Flag_Metals':              0,
                'Flag_Feedback':            0,
                'Flag_DoublePrecision':     1,
                'Composition_vector_length':0,
                'UnitLength_in_cm':         self.units['length'],
                'UnitMass_in_g':            self.units['mass'],
                'UnitVelocity_in_cm_per_s': self.units['velocity'],
                'Time':0,
            })

            # set cell properties
            grid = apy.coord.gridCube(
                [ self.opt['nRes'] ]*3,       # number of bins in each direction
                points='centers',             # get centers of the grid cubes
                scatter=0.2/self.opt['nRes'], # add an artificial scatter
            )
            f.setProperty('Coordinates', np.array(grid.coords,dtype=np.float64) )
            f.setProperty('Masses',      np.full(ngas,1,dtype=np.float64) )
            f.setProperty('Velocities',  np.zeros((ngas,3),dtype=np.float64) )
            f.setProperty('ParticleIDs', np.arange(1,1+ngas,dtype=np.uint32) )

Download the source code of the setup class.

Create simulation files

Now we are ready to create the simulation directory with all its files. This step is also very easy, the simulation can be created using the following call in the command line:

apy --setup 001

Here we use the simulation name ‘001’ that was used with settings in the project class.

The above command will create following files in your simulation directory:

  • 001_hiiregion

  • 001_hiiregion/Config.sh

  • 001_hiiregion/ics_32.hdf5

  • 001_hiiregion/output

  • 001_hiiregion/param.txt

  • 001_hiiregion/rad_sources.bin

  • 001_hiiregion/run.sh

It is also possible to update only selected parts of the setup, by adding some extra arguments to the call above:

.. code:: bash

apy –setup 001 param config