def test_invalid_nb_runs(tmp_path): from pylbo.exceptions import ParfileGenerationError gen = ParfileGenerator({ "gridpoints": [50, 100], "number_of_runs": 1 }, output_dir=tmp_path) with pytest.raises(ParfileGenerationError): gen.create_namelist_from_dict()
def test_unknown_namelist_item(tmp_path): from pylbo.exceptions import ParfileGenerationError gen = ParfileGenerator({ "gridpoints": 10, "unknown_item": 5 }, output_dir=tmp_path) with pytest.raises(ParfileGenerationError): gen.create_namelist_from_dict()
def test_savelist_not_present(tmp_path): import f90nml gen = ParfileGenerator( { "gridpoints": 100, "equilibrium_type": "adiabatic_homo" }, output_dir=tmp_path) gen.create_namelist_from_dict() parfile = gen.generate_parfiles() parfile_dict = f90nml.read(*parfile) assert "savelist" in parfile_dict.keys()
def generate_parfiles(parfile_dict, basename=None, output_dir=None): """ Generates parfiles based on a given configuration dictionary. The separate namelists do not have to be taken into account, and a normal dictionary should be supplied where the keys correspond to the namelist items that are required. Typechecking is done automatically during parfile generation. Parameters ---------- parfile_dict : dict Dictionary containing the keys to be placed in the parfile. basename : str The basename for the parfile, the `.par` suffix is added automatically and is not needed. If multiple parfiles are generated, these will be prepended by a 4-digit number (e.g. 0003myparfile.par). If not provided, the basename will default to `parfile`. output_dir : str, ~os.PathLike Output directory where the parfiles are saved, defaults to the current working directory if not specified. A subdirectory called `parfiles` will be created in which the parfiles will be saved. Notes ----- This routine is quite flexible and specifically designed for parametric studies. You can specify both single values and list-like items as dictionary items. This routine will automatically generate multiple parfiles if lists/numpy arrays are present. Returns ------- parfiles : list A list with the paths to the parfiles that were generated. This list can be passed immediately to :func:`pylbo.run_legolas`. Examples -------- This will generate a single parfile in a subdirectory `parfile` of the current working directory. >>> import pylbo >>> config = { >>> "geometry": "Cartesian", >>> "x_start": 0, >>> "x_end": 1, >>> "equilibrium_type": "resistive_homo", >>> "gridpoints": 100, >>> "write_eigenfunctions": True, >>> "basename_datfile": "my_run", >>> "output_folder": "output", >>> } >>> parfile = pylbo.generate_parfiles(config) This will generate 10 parfiles in the directory `my_parfiles` relative to the current working directory. The first parfile will have `x_end = 1.0` and 100 gridpoints, the second one will have `x_end = 2.0` and 150 gridpoints, etc. >>> import pylbo >>> import numpy as np >>> config = { >>> "geometry": "Cartesian", >>> "x_start": 0, >>> "x_end": np.arange(1, 11) >>> "number_of_runs": 10 >>> "equilibrium_type": "resistive_homo", >>> "gridpoints": np.arange(100, 600, 50), >>> "write_eigenfunctions": True, >>> "basename_datfile": "my_run", >>> "output_folder": "output", >>> } >>> parfile_list = pylbo.generate_parfiles(config, output_dir="my_parfiles") """ pfgen = ParfileGenerator(parfile_dict, basename, output_dir) pfgen.create_namelist_from_dict() parfiles = pfgen.generate_parfiles() return parfiles
def test_basename_none(tmp_path): gen = ParfileGenerator({}, output_dir=tmp_path) assert gen.basename == "parfile"
def test_invalid_dict_type(tmp_path): gen = ParfileGenerator({"gridpoints": 100.5}, output_dir=tmp_path) with pytest.raises(TypeError): gen.create_namelist_from_dict()
def test_outputdir_invalid(): with pytest.raises(NotADirectoryError): ParfileGenerator({}, output_dir="invalid_dir")
def test_outputdir_none(): parfiledir = Path.cwd() / "parfiles" gen = ParfileGenerator({}) assert gen.output_dir == parfiledir parfiledir.rmdir()