mooseherd.py

exception MooseHerdError[source]

Bases: Exception

MooseHerdError: custom error class for flagging errors with the moose herd.

class MooseHerd(sim_runners, input_mods, dir_manager, num_para_sims=1)[source]

Bases: object

MooseHerd class that can run parametric sweeps of simulation chains in parallel with configurable parallelisation options. Takes a list of SimRunner objects and a corresponding list of InputModifiers to insert the variables into the input scripts for the SimRunners. When calling run_* the herd will first call all InputModifiers in the specified order and then call run on all the SimRunners in order. Uses the DirectoryManager class to create/clear and log the directories in which each parallel worker is creating input files and running simulations.

__init__(sim_runners, input_mods, dir_manager, num_para_sims=1)[source]
Args:
sim_runners (list[SimRunner]): list of objects that inherit from

the SimRunner ABC in the order they need to be run. The mesh needs to be created before runnning moose so a common chain would be [GmshRunner,MooseRunner].

input_mods (list[InputModifier]): list of InputModifiers to create

the required input scripts for the SimRunners.

dir_manager (DirectoryManager): used to control how many and

which directories are used to run the simulations.

set_input_copy_names(input_names=None)[source]

set_input_copy_name: sets the name that will be used when copying input files to the working directories for the sweep. The defualt name is ‘sim-i’ so the first combination of variables in the simulation chain will be called ‘sim-1-1’.

Parameters:

input_names (list[str] | None) – List of name prefixes to be used for the simulation files. Defaults to None.

Raises:

MooseHerdError – The lengths of the sim runner list and the input modifier lists are not the same.

set_keep_flag(keep_all=True)[source]

set_keep_flag: flag used for allowing multiple calls to run_para or run_seq to keep everything or to overwrite with every call to run_*.

Parameters:

keep_all (bool) – True = keep all inputs and outputs with multiple calls to run_*. False = overwrite inputs and outputs with multiple calls to run_*. Defaults to True.

set_num_para_sims(n_para=1)[source]

set_num_para_sims: sets the number of simulation chains to run in parallel. Limits the number

Parameters:

n_para (int) – Number of parallel simulation to run. Defaults to 1.

get_sim_iter()[source]

get_sim_iter: returns the current simulation iteration corresponding to the combination of variables being analysed. This number will accumulate with multiple calls to run_* is keep_all=true.

Returns:

int – current simulation iteration number.

get_sweep_iter()[source]

get_sweep_iter: gets the current sweep iteration. The sweep iteration is incremented with every call to run_* if keep_all = true. If keep_all = false then sweep_iter is held at 1.

Returns:

int – current sweep iteration number.

reset_iter_counts()[source]

reset_iter_counts: resets the simulation iteration and the sweep iteration counters to zero.

run_once(sim_iter, var_list)[source]

run_once: runs a specific simulation chain with the given variable list once and returns a list of paths to the output files. Used by run_seq and run_para for parallelisation.

Parameters:
  • sim_iter (int) – current simulation iteration which is the index of the var_list from the var_sweep.

  • var_list (list[dict  |  None]) – list of dictionaries that contain the variables that will be run for this iteration.

Returns:

list[Path | None] – list of paths to the simulation output. If there is no useful output from the runner in the simulation chain it returns None in the list.

run_sequential(var_sweep)[source]

run_sequential: runs the variable sweep given in var_sweep sequentially and returns the paths to the simulation outputs.

Parameters:

var_sweep (list[list[dict | None]]) – outer list is the simulation iteration, inner list is the position in the simulation chain that the variable dictionary corresponds to. The dictionary contains the variables that will be inserted into the input file before calling run on the SimRunner. If None instead of a dictionary then the input file is copied with no modification

Returns:

list[list[Path | None]] – outer list is the simulation iteration and the inner list corresponds to the position of the SimRunner in the cimulation chain. Gives the path to the simulation output or None if no useful output is produced.

run_para(var_sweep)[source]

run_para: runs the variable sweep with the simulation chain in parallel.

Parameters:

var_sweep (list[list[dict | None]]) – outer list is the simulation iteration, inner list is the position in the simulation chain that the variable dictionary corresponds to. The dictionary contains the variables that will be inserted into the input file before calling run on the SimRunner. If None instead of a dictionary then the input file is copied with no modification

Returns:

list[list[Path | None]] – outer list is the simulation iteration and the inner list corresponds to the position of the SimRunner in the cimulation chain. Gives the path to the simulation output or None if no useful output is produced.

get_sweep_time()[source]
Returns:

float – the time taken for the variable sweep to run based on the performance counter.

get_iter_time()[source]
Returns

float: the time taken for the current simulation iteration to run.