Skip to content

Commit

Permalink
update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
apetri committed May 12, 2016
1 parent 3e2bd62 commit 6da8762
Showing 1 changed file with 35 additions and 27 deletions.
62 changes: 35 additions & 27 deletions docs/source/pipeline.rst
Original file line number Diff line number Diff line change
Expand Up @@ -713,15 +713,15 @@ This section shows an example on how to do some post processing on the products

::

>>> def methodThatMeasuresSomething(pool,batch,settings,id,**kwargs):
>>> def methodThatMeasuresSomething(pool,batch,settings,node_id,**kwargs):
...

where

- pool is a :py:class:`~lenstools.utils.MPIWhirlPool` instance that will take care of the parallelization of the code
- batch is the simulation batch object, i.e. an instance of :py:class:`~lenstools.pipeline.SimulationBatch`
- settings are the tunable settings of the code
- id is the particular batch subset to process, for example "Om0.300_Ol0.700|512b240|ic1"
- node_id is the particular batch subset to process, for example "Om0.300_Ol0.700|512b240|ic1"
- kwargs are any other keyword arguments you may want to pass to the methodThatMeasuresSomething method

lenstools will take care of distributing the methodThatMeasuresSomething calls on the computer cluster you are running on. Below is a working example of how to measure the 3D matter power spectrum out of the simulation boxes.
Expand All @@ -747,39 +747,28 @@ Create a file "matter_power_spectrum.py"
from lenstools.utils import MPIWhirlPool

from lenstools.simulations.nbody import NbodySnapshot
from lenstools.simulations.gadget2 import Gadget2Snapshot
from lenstools.simulations.gadget2 import Gadget2SnapshotDE
from lenstools.simulation.logs import logdriver

from lenstools.pipeline.simulation import SimulationBatch

import numpy as np
import astropy.units as u

################################################
###########Loggers##############################
################################################

console = logging.StreamHandler(sys.stdout)
formatter = logging.Formatter("%(asctime)s:%(name)-12s:%(levelname)-4s: %(message)s",datefmt='%m-%d %H:%M')
console.setFormatter(formatter)

logdriver = logging.getLogger("lenstools.driver")
logdriver.addHandler(console)
logdriver.propagate = False

#Orchestra director of the execution
def powerSpectrumExecution():

script_to_execute = matterPowerSpectrum
settings_handler = PowerSpectrumSettings
kwargs = {"fmt":Gadget2Snapshot}
kwargs = {"fmt":Gadget2SnapshotDE}

return script_to_execute,settings_handler,kwargs

################################################################
################Snapshot power spectrum#########################
################################################################

def matterPowerSpectrum(pool,batch,settings,id,**kwargs):
def matterPowerSpectrum(pool,batch,settings,node_id,**kwargs):

assert "fmt" in kwargs.keys()
fmt = kwargs["fmt"]
Expand All @@ -791,7 +780,7 @@ Create a file "matter_power_spectrum.py"
assert isinstance(settings,PowerSpectrumSettings)

#Split the id into the model,collection and realization parts
cosmo_id,geometry_id = id.split("|")
cosmo_id,geometry_id = node_id.split("|")

#Get a handle on the simulation model
model = batch.getModel(cosmo_id)
Expand Down Expand Up @@ -824,6 +813,14 @@ Create a file "matter_power_spectrum.py"
#Construct the array of bin edges
k_egdes = np.linspace(settings.kmin,settings.kmax,settings.num_k_bins+1).to(model.Mpc_over_h**-1)

#Placeholder for the density MPI communications
density_placeholder = np.empty((settings.fft_grid_size,)*3,dtype=np.float32)
if pool is not None:
pool.openWindow(density_placeholder)

if pool.is_master():
logdriver.debug("Opened density window of type {0}".format(pool._window_type))

#Cycle over snapshots
for n in range(settings.first_snapshot,settings.last_snapshot+1):

Expand All @@ -832,15 +829,15 @@ Create a file "matter_power_spectrum.py"

#Log to user
if (pool is None) or (pool.is_master()):
logdriver.info("Processing snapshot {0} of model {1}".format(n,id))
logdriver.info("Processing snapshot {0} of model {1}".format(n,node_id))
logdriver.info("Allocated memory for power spectrum Ensemble {0}".format(power_ensemble.shape))

for r,ic in enumerate(settings.nbody_realizations):

#Log to user
if (pool is None) or (pool.is_master()):
logdriver.info("Processing N-body realization {0}".format(ic))
#Instantiate the appropriate SimulationIC object
realization = collection.getRealization(ic)

Expand All @@ -851,7 +848,7 @@ Create a file "matter_power_spectrum.py"
sys.exit(1)

snap = fmt.open(realization.snapshotPath(n,sub=None),pool=pool)
k,power_ensemble[r],hits = snap.powerSpectrum(k_egdes,resolution=settings.fft_grid_size,return_num_modes=True)
k,power_ensemble[r],hits = snap.powerSpectrum(k_egdes,resolution=settings.fft_grid_size,return_num_modes=True,density_placeholder=density_placeholder)
snap.close()

#Safety barrier sync
Expand All @@ -871,7 +868,7 @@ Create a file "matter_power_spectrum.py"

#Save the ensemble
if (pool is None) or (pool.is_master()):
savename = os.path.join(collection.home_subdir,settings.ensemble_name,settings.ensemble_name+"_snap{0:03d}.npy".format(n))
logdriver.info("Saving power spectrum Ensemble ({0}) to {1}".format(power_ensemble.unit.to_string(),savename))
np.save(savename,power_ensemble.value)
Expand All @@ -881,8 +878,19 @@ Create a file "matter_power_spectrum.py"
if pool is not None:
pool.comm.Barrier()

###########
#Completed#
###########

#Close the RMA window
if pool is not None:
pool.comm.Barrier()
pool.closeWindow()
if pool.is_master():
logdriver.debug("Closed density window of type {0}".format(pool._window_type))


#Completed
if pool is None or pool.is_master():
logdriver.info("DONE!!")

Expand Down Expand Up @@ -935,22 +943,22 @@ Create a file "matter_power_spectrum.py"
#Read in the nbody realizations that make up the ensemble
settings.nbody_realizations = list()
for r in options.get(section,"nbody_realizations").split(","):
try:
l,h = r.split("-")
settings.nbody_realizations.extend(range(int(l),int(h)+1))
except ValueError:
settings.nbody_realizations.extend([int(r)])
settings.first_snapshot = options.getint(section,"first_snapshot")
settings.last_snapshot = options.getint(section,"last_snapshot")
settings.fft_grid_size = options.getint(section,"fft_grid_size")

settings.length_unit = getattr(u,options.get(section,"length_unit"))
settings.kmin = options.getfloat(section,"kmin") * settings.length_unit**-1
settings.kmax = options.getfloat(section,"kmax") * settings.length_unit**-1
settings.num_k_bins = options.getint(section,"num_k_bins")

#Return to user
Expand Down

0 comments on commit 6da8762

Please sign in to comment.