directorymanager.py

class DirectoryManager(n_dirs=1)[source]

Bases: object

Manages directories for running simulations in parallel with the

mooseherd.

__init__(n_dirs=1)[source]
Args:

n_dirs (int, optional): number of directories to be created. Defaults to 1.

set_sub_dir_name(sub_dir_name)[source]

set_sub_dir_name: used to set the string used at the start of the created sub-directores. default on creation is ‘sim-workdir’. Populates the list of run directories using the new sub directory name.

Parameters:
  • sub_dir_name (str :) – string to be used to name the created sub-directories within the base directory.

  • sub_dir_name

set_base_dir(base_dir, clear_old_dirs=False)[source]

set_base_dir: sets the base directory to create sub-directors for running the simulations. The base directory must exist.

Parameters:
  • base_dir (Path) – directory in which the new working directories will be created.

  • clear_old_dirs (bool) – deletes previous directories in the base directory and their contents if they exist. Defaults to False.

Raises:

FileExistsError – the selected base directory does no exist.

get_output_key_tag()[source]

get_output_key_tag: returns the string used to name the output key files that map the simulation outputs to the various directories that are being managed.

Returns:

str – common string used to name the output key json files.

get_sweep_var_tag()[source]

get_sweep_var_tag: returns ths string used to name the sweep variable json file that contains a copy of the dictionary the user provided as part of the parameter sweep.

Returns:

str – common string used to name the sweep variable json files.

create_dirs()[source]

Creates the specified number of directories based on the sub_dir name within the base directory.

Returns:

list[Path] – list of paths to the directories to create.

clear_dirs()[source]

Deletes all working directories in the base directory that have the corresponding sub-directory name and their contents.

reset_dirs()[source]

Helper function that first clears the working directories if they exist and then creates new working directories ready for a simulation sweep.

Returns:

list[Path] – List of paths to the created working directories

get_all_run_dirs()[source]

Returns the list of paths to all the directories that can be used to run simulations.

Returns:

list[Path] – paths to all created directories.

get_run_dir(dir_num)[source]

get_run_dir: returns the path to the run directory based on the input directory number. The directory number can be greater than the number of managed directories in the run_dirs list in this case the directory number will wrap and point at an existing run directory allowing multiple simulations to be run in the same directory.

Parameters:

dir_num (int) – number of the directory path to be retrieved. Can be greater than the specified number of directories and will wrap appropriately

Returns:

Path – path to the directory

set_output_paths(output_paths)[source]

set_output_paths: sets the list of lists to the simulation output based on herder input.

Parameters:

output_paths (list[list[Path | None]]) – paths to all outputs from the variable sweep. Outer list is the variable combination run inner list is based on the order the simulations were called.

get_output_paths()[source]

get_output_paths: returns the list of lists to the simulation output files.

Returns:

list[list[Path | None]] – paths to all outputs from the variable sweep. Outer list is the variable combination run inner list is based on the order the simulations were called.

get_output_key_file(sweep_iter=1)[source]

get_output_key_file: gets the path to the output key file created during the variable sweep mapping directories to given combinations of variables that were run.

Parameters:

sweep_iter (int) – number corresponding to the sweep iteration to retrieve. Defaults to 1.

Returns:

Path – path to the output key file that maps output paths to the combinations of variables in the sweep.

write_output_key(sweep_iter)[source]

write_output_key: converts the output paths to strings and saves them in json format.

Parameters:

sweep_iter (int) – number corresponing to the sweep iteration to write. The sweep iteration is used to number the output key files.

get_sweep_var_file(sweep_iter=1)[source]

get_sweep_var_file: path to the json file which contains the dictionary of variables that were analysed at the given sweep iteration

Parameters:

sweep_iter (int) – Sweep iteration (call number to herd run_para). Defaults to 1.

Returns:

Path – path to the json sweep variable file.

write_sweep_vars(sweep_vars, sweep_iter=1)[source]

write_sweep_vars: writes the sweep variable dictionary to a json file to log the variables used for each simulation.

Parameters:
  • sweep_vars (list[list[dict | None]]) – sweep variables as passed to the herd to run.

  • sweep_iter (int) – iteration number for number of calls to the herd. Defaults to 1.

output_paths_to_str(output_files)[source]

output_paths_to_str: helper function for converting the output paths to strings to allow them to be saved as json.

Parameters:

output_files (list[list[Path | None]])

Returns:

list[list[str]] – as input with Path converted to str

output_str_to_paths(output_files)[source]

output_str_to_paths: helper function to convert strings read from output key json to paths.

Parameters:

output_files (list[list[str]]) – output file list of path strings as in the output key file.

Returns:

list[list[Path]] – as input with str converted to Path.