wsssss.inlists package
Submodules
wsssss.inlists.create_grid module
- class wsssss.inlists.create_grid.MesaGrid(mesa_dir='', inlist_filename='inlist', inlists_index=5, starjob_filename='inlist_project', controls_filename='inlist_project', eos_filename='inlist_project', kap_filename='inlist_project', pgstar_filename='inlist_project', add_base_workdir=False)
Bases:
object- __init__(mesa_dir='', inlist_filename='inlist', inlists_index=5, starjob_filename='inlist_project', controls_filename='inlist_project', eos_filename='inlist_project', kap_filename='inlist_project', pgstar_filename='inlist_project', add_base_workdir=False)
MesaGrid class which contains all inlist settings for a grid. When the create_grid method is called, a copy of the script which called it is copied into the grid directory.
- Parameters:
mesa_dir (str) –
$MESA_DIRroot directory to be used with this grid.inlist_filename (str, optional) – Defaults to ‘inlist’.
inlists_index (int, optional) – Which read_extra_*_inlist(inlists_index) to use. Defaults to 5.
starjob_filename (str, optional) – Cannot be the same as inlist_filename. Defaults to ‘inlist_project’.
controls_filename (str, optional) – Cannot be the same as inlist_filename. Defaults to ‘inlist_project’.
eos_filename (str, optional) – Cannot be the same as inlist_filename. Defaults to ‘inlist_project’.
kap_filename (str, optional) – Cannot be the same as inlist_filename. Defaults to ‘inlist_project’.
pgstar_filename (str, optional) – Cannot be the same as inlist_filename. Defaults to ‘inlist_project’.
add_base_workdir (bool, optional) – Add minimum required files from $MESA_DIR/star/work to each run directory (make/, src/, mk, re, rn, clean).
- star_job
Options for
star_job.- Type:
dict
- controls
Options for
controls.- Type:
dict
- pgstar
Options for
pgstar.- Type:
dict
- kap
Options for
kap. Only exists if mesa_dir version is later than or equal to 15140.- Type:
dict
- eos
Options for
eos. Only exists if mesa_dir version is later than or equal to 15140.- Type:
dict
Examples
Creating a grid of MESA runs with initial masses of 1 and 2 Msol.
>>> import os >>> from wsssss.inlists import create_grid as cg >>> grid = cg.MesaGrid() >>> grid.controls['initial_mass'] = [1, 2] >>> grid.create_grid('path/to/grid') >>> os.listdir('path/to/grid') ['0000', '0001]
- add_file(path)
Add a file which will be copied into each run directory. Will raise a FileNotFoundError if the file at path does not exist.
- Parameters:
path (str) – Path to file to be added to every run directory.
Examples
Copy the file at
path/to/fileto each run directory when the grid is created.>>> from wsssss.inlists import create_grid as cg >>> grid = cg.MesaGrid() >>> grid.add_file('path/to/file')
- add_dir(path)
Add a directory which will be copied into each run directory.
- Parameters:
path (str) – Path to directory to be added to every run directory.
Examples
Copy the directory at
path/to/directoryto each run directory when the grid is created.>>> from wsssss.inlists import create_grid as cg >>> grid = cg.MesaGrid() >>> grid.add_dir('path/to/directory')
- add_inlist_option_file_check(namelist, option)
Add a check that the file specified in the namelist[option] must exist in each run directory, e.g. controls[‘history_columns_filename’], which is included by default as well as
profile_columns_filename.- Parameters:
namelist (str)
option (str)
Examples
>>> from wsssss.inlists import create_grid as cg >>> grid = cg.MesaGrid() >>> grid.add_inlist_option_file_check('controls', 'history_columns_file')
- add_no_expand_key(namelist, key)
Add a key which will not be expanded. For example &kap’s
user_kap_Xs:param namelist: Which namelist the key is part of. E.g. ‘kap’. :type namelist: str :param key: Name of parameter. E.g. ‘user_kap_Xs’. :type key: strExamples
>>> from wsssss.inlists import create_grid as cg >>> grid = cg.MesaGrid() >>> grid.add_no_expand_key('kap', f'user_kap_Xs')
- add_non_mesa_key(namelist, key)
Add a key which is not in MESA’s keys and expand it. For example including a parameter which is used in
inlist_finalize_function. :param namelist: Which namelist the key is part of. E.g. controls. :type namelist: str :param key: Name of parameter. Must start with'!PY_KEY_'. :type key: str
- set_inlist_finalize_function(function)
Set the function which is applied to all unpacked inlists.
The function must accept a single inlist and return a single inlist.
- Parameters:
function (function)
Examples
Change an option in star_job depending on
initial_massincontrols. This could also be accomplished using thef'{non_mesa_key_start}group_unpack'key.>>> import numpy as np >>> from wsssss.inlists import create_grid as cg >>> grid = cg.MesaGrid() >>> grid.star_job['change_initial_net'] = True >>> grid.star_job['new_net_name'] = 'pp_extras.net' >>> grid.controls['initial_mass'] = np.linspace(1, 2, 6) >>> def finalize_function(unpacked_inlist): ... if unpacked_inlist['controls']['initial_mass'] > 1.3: ... unpacked_inlist['star_job']['new_net_name'] = 'pp_cno_extras.net' ... return unpacked_inlist >>> grid.set_inlist_finalize_function(finalize_function) >>> grid.unpack_inlists() >>> grid.unpacked[0]['star_job']['new_net_name'] 'pp_extras.net' >>> grid.unpacked[-1]['star_job']['new_net_name'] 'pp_cno_extras.net'
- set_griddir_finalize_function(function)
Set the function which is called in each grid directory. The arguments to the function are a MesaGrid object and the run index.
- Parameters:
function (function)
Examples
>>> import os >>> from wsssss.inlists import create_grid as cg >>> grid = cg.MesaGrid() >>> grid.controls['initial_mass'] = [1, 2] >>> def griddir_finalize_function(grid, i): ... os.system("pwd") >>> grid.set_griddir_finalize_function(griddir_finalize_function) >>> grid.create_grid('path/to/grid') /home/walter/Github/wsssss/path/to/grid/0000 /home/walter/Github/wsssss/path/to/grid/0001
- set_name_function(function)
Set the function which is used to generate the name of each run directory. It recieves a fully unpacked inlist (dict of dicts) or grid index and unpacked inlist and returns a string. It should generate a unique name for each inlist.
- Parameters:
function (function)
Examples
>>> import os >>> from wsssss.inlists import create_grid as cg >>> grid = cg.MesaGrid() >>> grid.controls['initial_mass'] = [1, 2] >>> def name_function(unpacked_inlist): ... return f'm{unpacked_inlist["controls"]["initial_mass"]:.3f}' >>> grid.set_name_function(name_function) >>> grid.create_grid('path/to/grid') >>> os.listdir('path/to/grid') ['m1.000', 'm2.000']
- create_grid(grid_path, rm_dir=True)
Create the MESA grid in grid_path. The inlist options are validated and extra files and directories copied to each run directory. :param grid_path: Path to grid directory. :param rmdir: Remove grid directory if it exists. Defaults to True. :type rmdir: bool, optional
Examples
>>> import os >>> from wsssss.inlists import create_grid as cg >>> grid = cg.MesaGrid() >>> grid.controls['initial_mass'] = [1, 2] >>> grid.create_grid('path/to/grid') >>> os.listdir('path/to/grid') ['0000', '0001']
- validate_inlists(mesa_dir=None)
Check if all options in the inlists are valid MESA keys.
- Parameters:
mesa_dir (str, optional) – MESA root directory to check against. Defaults to None, which will use
$MESA_DIR.
Examples
>>> from wsssss.inlists import create_grid as cg >>> grid = cg.MesaGrid() >>> grid.controls['this_option_does_not_exist'] = 1 KeyError: 'Option(s) not in available controls keys: this_option_does_not_exist.'
- _validate_files(grid_path)
Check if all specified files are accounted for. :param grid_path: Grid directory. :type grid_path: str
- _get_available_options(path)
TODO: Replace with inlists.get_mesa_defaults :param path:
Returns:
- check_copy()
Check if all files and directories which are to be copied exist.
- unpack_inlists()
Unpack all inlists. Will place them in the unpacked attribute as a tuple.
- _make_inlist_generator(inlist_dict)
Create a generator which yields all unique inlists with all combinations of any lists, tuples, or numpy arrays of length greater than 1. Can handle groups of variables which must change together by appending a list of dict`s in the `group_unpack key. If multiple lists of `dict`s are appended, they are treated as separate variables. :param inlist_dict: :type inlist_dict: dict
- Yields:
dict – Unpacked inlist.
- _setup_directories(grid_path, rm_dir)
Create the directory structure for a grid at
grid_path.- Parameters:
grid_path (str) – Path which will contain the grid.
rm_dir (bool) – If it exists, first remove the grid directory.
- _write_inlists(grid_path)
Write the unpacked inlists to
grid_pathin their respective directories.- Parameters:
grid_path (str) – Path which will contain the grid.
- _generate_inlist_string(inlist_dict)
Generates an inlist string from inlist_dict. Which is an unpacked controls etc dict.
- Parameters:
inlist_dict (dict) – Dictionary containing a set of inlist options.
- Returns:
A string representation of
inlist_dictreadable by MESA.- Return type:
inlist_str (str)
- _copy_extra_files_and_dirs(grid_path)
Copy extra files and directories specified in
MesaGrid.extra_filesandMesaGrid.extra_dirsto each run directory.- Parameters:
grid_path
- summary()
Print a summary of which variables change.
wsssss.inlists.inlists module
- wsssss.inlists.inlists.inlist_diff(dict1, dict2)
Compare two inlists and show which items change or remain the same.
- Parameters:
dict1 (dict)
dict2 (dict)
- Returns:
- Items which remain the same are in
result['same'], and those which changed in
result['changed'].
- Items which remain the same are in
- Return type:
dict
- wsssss.inlists.inlists.evaluate_inlist_str(inlist_str, inlist_dir)
Fully evaluate inlist_str as if it were written in inlist_dir, following all
read_extra_*_inlistif they are.true...- Parameters:
inlist_str (str) – str form of an inlist.
inlist_dir (str) – Reference directory for reading other files.
- Returns:
Evaluated inlist.
- Return type:
dict
- wsssss.inlists.inlists.evaluate_inlist(path)
Fully evaluate the inlist at path, following all
read_extra_*_inlistif they are.true... :param path: Path to inlist. :type path: str- Returns:
Evaluated inlist.
- Return type:
dict
- wsssss.inlists.inlists.get_inlist_type(inlist)
- wsssss.inlists.inlists._evaluate_inlist(inlist, inlist_dir)
Fully evaluate inlist as if it were written in inlist_dir, following all
read_extra_*_inlistif they are.true...- Parameters:
inlist (dict) – Inlist dictionary.
inlist_dir (str) – Directory which contains inlist.
- Returns:
Evaluated inlist.
- Return type:
dict
- wsssss.inlists.inlists.variable_to_string(value)
Convert value to a fortran-compatible string. Can be a string, bool, int, or float.
- Parameters:
value (str, bool, int, or float) – Value to convert.
- Returns:
Fortran-compatible string.
- Return type:
str
- wsssss.inlists.inlists.write_inlist(inlist, path, header='', mode='w')
Write the inlist to path. header is prepended to the string which is written. mode is passed to
open.- Parameters:
inlist (dict)
path (str) – Inlist file path.
header (str)
mode (str) – File mode passed to
open.
- wsssss.inlists.inlists.print_dict(to_print)
Print the dict to_print with a key and value on every line.
- Parameters:
to_print (dict)
- wsssss.inlists.inlists.load_MESA_defaults(mesa_dir)
- wsssss.inlists.inlists.compare_inlist(path1, path2, show_same=False)
- wsssss.inlists.inlists.get_mesa_defaults(mesa_dir)
Get the available MESA inlist options for the version of MESA installed in mesa_dir.
- Parameters:
mesa_dir (str) –
$``MESA_DIRroot directory.- Returns:
List of keys for each inlist type (e.g.
star_job,controls, etc.).- Return type:
dict
- wsssss.inlists.inlists.check_inlist(path, mesa_dir)
Check whether the options in the inlist at path are available in the version of mesa specified in mesa_dir. Will try to detect if the options have been moved to
eosorkapfromstar_joborcontrols.- Parameters:
path (str) – Path to an inlist.
mesa_dir (str) –
$``MESA_DIRroot directory.
- Returns:
For each inlist option, states whether it is available in the version of mesa specified in mesa_dir.
- Return type:
dict