Project

General

Profile

Python user guide, writing data

Prerequisite

Install python wrapper using pip from your prefered python interpreter :

pip install python-unsio --user

unsio module

Unsio python wrapperis a module called unsio that you have to import to your python program. Example :

from unsio import *            

or

import unsio

Writing data

Writing UNSIO data from python language is pretty simple and is done in 3 steps:

  1. UNSIO object instantiation
  2. Setting data to save
  3. Saving data

Create/instantiate Cunsout object

Unsio python wrapper is based on C++ API with slight modifications to works with numpy arrays. Module py_unsio provides an easy way to save snapshot data, in different format, from a set of methods implemented in CunsOut object. The first step is to instantiate a CunsOut object.

simname="/home/jcl/gtr119_1600" 
components="disk,gas,stars" 
times="all" 
verbose=False
# input
uns = CunsIn(simname,components,times,verbose) # instantiate input objet
# output
outfile="mysnap.g2"     # output filename
type="gadget2"             # we choose to save file in gadget2 format
unsout=CunsOut(outfile,type)  # instantiate output object

CunsIn constructor accept 2 parameters :

  1. outfile, is a string, which contain the output file you want to save
  2. type, is a string which contain the format type in which you want to save (see the list of supported output files )

Setting data

CunsOut class from py_unsio module provides several functions to set data into an output snapshot

Setting numpy arrays

As we can see from reading data, there are same "set" methods for setting data from numpy 1D array in float or integer format

  • setArrayF(comp,prop,numpy_float_array)
  • setArrayI(comp,prop,numpy_integer_array)

comp specify which component we want to save
prop specify which array data we want to set

From argument comp , we specify the component to be treated.

component value description
gas Gas particles
halo Dark matter particles
dm Dark matter particles
disk Old stars particles
stars Stars particles
bulge Bulge particles
bndry bndry particles

From argument prop , we specify which data we want to set.

prop string descripton numpy array passed as parameter
pos particles positions numpy 1D array of particles position (size=n*3)
vel particles velocities numpy 1D array of particles velocitie (size=n*3)
mass particles masses numpy 1D array of particles velocitie (size=n)
acc particles accelerations numpy 1D array of particles acceleration (size=n*3)
pot particles potential numpy 1D array of particles potential (size=n)
rho particles densities numpy 1D array of particles density (size=n)
hsml particles hsml numpy 1D array of particles hydro smooth length (size=n)
temp particles temperatures numpy 1D array of particles temperature (size=n)
age particles ages numpy 1D array of particles age (size=n)
metal particles metallicity numpy 1D array of particles metallicity (size=n)
u particles internal energy numpy 1D array of particles internal energy (size=n)
aux particles auxilliary (NEMO) numpy 1D auxiliary array (size=n)
keys particles keys (NEMO) numpy 1D keys array (size=n)
id particles indexes (NEMO) numpy 1D keys array (size=n)

Setting values

As we can see from reading data, there are same "set" methods for setting values in float or integer format

  • setValueF(prop,float_value)
  • setValueI(prop,integer_value)

prop specify which data value we want to set

Example

comp="gas" 
# positions
ok,pos =uns.getArrayF(comp,"pos") # get gas positions
if ok:
    ok=unsout.setArrayF(comp,"pos",pos)  # set positions from 1D numpy array pos

# masses
ok,mass=uns.getArrayF(comp,"mass") # get gas mass
if ok:
    ok=unsout.setArrayF(comp,"mass",mass) # set masses from 1D numpy array mass

# indexes
ok,id=uns.getArrayI(comp, "id")  # get gas indexes
if ok:
    ok=unsout.setArrayI(comp,"id",id) # set indexes from 1D numpy array id

# simulation time
ok,time=uns.getValueF("time") # get simulation time
ok=unsout.setValueF("time",time) # set simulation time

# save
unsout.save()  # write data on hard drive

A complete example

To see a complete example, follow this link.