bamboost.core.simulation.base
Simulation management module for bamboost.
This module provides classes and utilities for managing simulations, including reading and writing simulation data, handling metadata and parameters, interfacing with HDF5 files, managing simulation status, and supporting HPC job submission and MPI parallelism.
Attributes
- log=
BAMBOOST_LOGGER.getChild('simulation')
Classes
Status
Enum representing the status of a simulation.
Attributes:
- INITIALIZED=
'initialized'
The simulation has been initialized but not yet started.
- STARTED=
'started'
The simulation is currently running.
- FINISHED=
'finished'
The simulation has completed successfully.
- FAILED=
'failed'
The simulation has failed.
- UNKNOWN=
'unknown'
The status of the simulation is unknown.
(self) -> str
Return the string representation of the status.
StatusInfo
(self, status, message=None) -> None
Detailed status information for a simulation.
Arguments:
- status:
Status
- message:
typing.Optional[str]
=None
Attributes:
- status:
Status
The current status of the simulation.
- message:
typing.Optional[str]
=None
An optional message providing additional details about the status.
(cls, status) -> StatusInfo
Arguments:
- cls
- status:
str
(self) -> str
SimulationName
Utility class for generating unique simulation names.
The SimulationName
class provides a convenient way to generate or validate
unique names for simulation runs. If no name is provided, a random unique
identifier is generated using UUID, truncated to the specified length.
Examples:
>>> sim_name = SimulationName() # Generates a unique name of length 10
>>> sim_name = SimulationName("my_simulation")
>>> print(sim_name)
my_simulation
Note
The generated name is guaranteed to be unique across MPI ranks by broadcasting the generated UUID from the root rank.
(cls, name=None, length=10)
Arguments:
- cls
- name:
typing.Optional[str]
=None
- length:
int
=10
(length) -> str
Arguments:
- length:
int
_Simulation
(self, name, parent, comm=None, index=None, **kwargs)
Abstract base class for simulation objects.
This class should not be instantiated directly. Use Simulation
for read-only access
or SimulationWriter
for writable access to simulation data.
Arguments:
- name:
str
- parent:
StrPath
- kwargs=
{}
Attributes:
- name:
str
=bamboost.core.simulation.base._Simulation(name)
- path:
pathlib.Path
=Path(parent).joinpath(name).absolute()
- _psize:
int
=bamboost.core.simulation.base._Simulation(self).bamboost.core.simulation.base._Simulation(self)._comm.bamboost.core.simulation.base._Simulation(self)._comm.size
- _prank:
int
=bamboost.core.simulation.base._Simulation(self).bamboost.core.simulation.base._Simulation(self)._comm.bamboost.core.simulation.base._Simulation(self)._comm.rank
- _ranks=
np.array([i for i in range(self._psize)])
- _index:
Index
=bamboost.core.simulation.base._Simulation(index) or bamboost.index.Index(comm=bamboost.core.simulation.base._Simulation(self).bamboost.core.simulation.base._Simulation(self)._comm)
- collection_uid:
CollectionUID
=bamboost.core.simulation.base._Simulation(kwargs).bamboost.core.simulation.base._Simulation(kwargs).pop('collection_uid', None) or bamboost.core.simulation.base._Simulation(self).bamboost.core.simulation.base._Simulation(self)._index.bamboost.core.simulation.base._Simulation(self)._index.resolve_uid(bamboost.core.simulation.base._Simulation(self).bamboost.core.simulation.base._Simulation(self).path.bamboost.core.simulation.base._Simulation(self).path.parent)
- _data_file:
pathlib.Path
=self.path.joinpath(constants.HDF_DATA_FILE_NAME)
- _xdmf_file:
pathlib.Path
=self.path.joinpath(constants.XDMF_FILE_NAME)
- _bash_file:
pathlib.Path
=self.path.joinpath(constants.RUN_FILE_NAME)
- root:
Group[bamboost._typing._MT]
- mutable:
bool
- _orm:
SimulationORM
- uid:
str
Returns the full unique identifier (UID) of the simulation.
The UID is constructed as "<collection_uid>:<simulation_name>", where
collection_uid
is the unique identifier of the collection containing the simulation, andsimulation_name
is the name of the simulation. - parameters:
Parameters[bamboost._typing._MT]
Returns the parameters associated with this simulation.
- metadata:
Metadata[bamboost._typing._MT]
Returns the metadata associated with this simulation.
- status:
StatusInfo
Returns the current status of the simulation.
- created_at:
datetime.datetime
Returns the creation timestamp of the simulation.
- description:
str
Returns the description of the simulation.
- links:
Links[bamboost._typing._MT]
Returns the links associated with this simulation.
- files
Returns a file picker utility for the simulation directory.
- git:
GroupGit[bamboost._typing._MT]
Returns the Git group associated with this simulation.
- data:
Series[bamboost._typing._MT]
Returns the default data series for this simulation.
- meshes:
GroupMeshes[bamboost._typing._MT]
- mesh:
GroupMesh
Bases
(self, other) -> bool
Arguments:
- other:
_Simulation
(cls, uid, **kwargs) -> Self
Return the Simulation
instance corresponding to the given UID.
Arguments:
- cls
- uid:
str
The full simulation UID in the format "<collection_uid>:<simulation_name>".
- kwargs=
{}
Returns
typing_extensions.Self
An instance of the simulation class corresponding to the UID.Examples:
>>> sim = Simulation.from_uid("abc123:mysim")
(self) -> SimulationWriter
Return a mutable SimulationWriter
object for editing the simulation.
This method provides an interface to obtain a mutable version of the current simulation, allowing modifications to simulation data, metadata, and parameters.
Returns
SimulationWriter
An object with write access to the simulation.Examples:
>>> with sim.edit() as sim_writer:
... sim_writer.parameters["new_param"] = 42
(self, *, metadata=None, parameters=None) -> None
Push update to sqlite database.
Arguments:
- metadata:
typing.Optional[typing.Mapping]
=None
metadata dictionary to insert
- parameters:
typing.Optional[typing.Mapping]
=None
parameter dictionary to insert
(self) -> None
Open the xdmf file in paraview.
(self)
A context manager for changing the working directory to this simulations' path.
>>> with sim.working_directory():
>>> ...
(self, path) -> Series[_MT]
Return a Series object for the given path.
A "series" in bamboost is a logical group in the HDF5 file that stores time-dependent or indexed simulation data, such as fields, scalars, or other arrays. Each series is identified by its path and can contain multiple fields and steps.
Arguments:
- path:
str
Path to the series group within the simulation HDF5 file.
Returns
Series[bamboost._typing._MT]
Series[_MT]: The Series object for the specified path.(self, field_names=None, timesteps=None, *, series=None, filename=None, mesh_name=constants.DEFAULT_MESH_NAME)
Generate an XDMF file for visualization of simulation data.
This method creates an XDMF file that references the simulation's mesh and time-dependent field data, enabling visualization in tools such as ParaView.
Arguments:
- field_names:
typing.Optional[typing.Iterable[str]]
=None
Names of the fields to include in the XDMF file. If None, all available fields in the series are included.
- timesteps:
typing.Optional[typing.Iterable[float]]
=None
List of timesteps to include. If None, all timesteps in the series are included.
The data series to use for field and timestep information. If None, uses the default data series (
self.data
).Path to the output XDMF file. If None, defaults to "<simulation_path>/data.xdmf".
- mesh_name:
str
=bamboost.constants.bamboost.constants.DEFAULT_MESH_NAME
Name of the mesh to reference in the XDMF file. Defaults to the constant
DEFAULT_MESH_NAME
.
Examples:
>>> sim.create_xdmf(field_names=["velocity", "pressure"])
>>> sim.create_xdmf(timesteps=[0.0, 0.1, 0.2], filename="custom.xdmf")
(self)
Simulation
(self, name, parent, comm=None, index=None, **kwargs)
Read-only simulation object.
The Simulation
class provides read-only access to simulation data, metadata,
and parameters. It is intended for inspecting and analyzing existing simulations
without modifying their contents. For editing or managing simulations, use
SimulationWriter
(or sim_writer = sim.edit()
).
Arguments:
- name:
str
- parent:
StrPath
- kwargs=
{}
Attributes:
- _file:
HDF5File[bamboost._typing.Immutable]
=HDF5File(self._data_file, comm=self._comm, mutable=False)
Examples:
>>> sim = Simulation("mysim", "/path/to/collection")
>>> print(sim.parameters)
>>> print(sim.metadata)
Bases
_Simulation.mutable
_Simulation.name
_Simulation.path
_Simulation._psize
_Simulation._prank
_Simulation._ranks
_Simulation._index
_Simulation.collection_uid
_Simulation._data_file
_Simulation._xdmf_file
_Simulation._bash_file
_Simulation.__eq__()
_Simulation._repr_html_()
_Simulation.root
_Simulation._orm
_Simulation.from_uid()
_Simulation.uid
_Simulation.edit()
_Simulation.update_database()
_Simulation.parameters
_Simulation.metadata
_Simulation.status
_Simulation.created_at
_Simulation.description
_Simulation.links
_Simulation.files
_Simulation.git
_Simulation.data
_Simulation.meshes
_Simulation.mesh
_Simulation.open_in_paraview()
_Simulation.enter_path()
_Simulation.require_series()
_Simulation.create_xdmf()
SimulationWriter
(self, name, parent, comm=None, index=None, **kwargs)
Mutable simulation object for editing and managing simulations.
The SimulationWriter
class provides write access to simulation data, metadata,
and parameters. It is intended for creating, editing, and managing simulations.
Use this class when you need to modify the contents of a simulation, such as
updating parameters, metadata, or simulation data.
Arguments:
- name:
str
- parent:
StrPath
- kwargs=
{}
Attributes:
Examples:
>>> with SimulationWriter("mysim", "/path/to/collection") as sim_writer:
... sim_writer.parameters["new_param"] = 42
... sim_writer.metadata["description"] = "Updated simulation"
Bases
_Simulation.mutable
_Simulation.name
_Simulation.path
_Simulation._psize
_Simulation._prank
_Simulation._ranks
_Simulation._index
_Simulation.collection_uid
_Simulation._data_file
_Simulation._xdmf_file
_Simulation._bash_file
_Simulation.__eq__()
_Simulation._repr_html_()
_Simulation.root
_Simulation._orm
_Simulation.from_uid()
_Simulation.uid
_Simulation.edit()
_Simulation.update_database()
_Simulation.parameters
_Simulation.metadata
_Simulation.created_at
_Simulation.links
_Simulation.files
_Simulation.git
_Simulation.data
_Simulation.meshes
_Simulation.mesh
_Simulation.open_in_paraview()
_Simulation.enter_path()
_Simulation.create_xdmf()
(self, exc_type, exc_val, exc_tb)
Arguments:
- exc_type
- exc_val
- exc_tb
(self) -> SimulationWriter
(self) -> None
Initialize the simulation.
This method sets up the simulation's HDF5 data file and required groups, and records initial metadata such as status and creation time.
(self, value) -> None
Arguments:
- value:
typing.Union[StatusInfo, bamboost.core.simulation.base.Status]
(self, value) -> None
Arguments:
- value:
str
(self, path) -> Series[Mutable]
Arguments:
- path:
str
(self, files) -> None
Copy files to the simulation folder.
Arguments:
- files:
typing.Iterable[StrPath]
list of files/directories to copy
(self, commands, euler=False, sbatch_kwargs=None) -> None
Create a batch job and put it into the folder.
Arguments:
- commands:
list[str]
A list of strings being the user defined commands to run
- euler:
bool
=False
If false, a local bash script will be written
Additional sbatch arguments. This parameter allows you to provide additional arguments to the
sbatch
command when submitting jobs to a Slurm workload manager. The arguments should be provided in the format of a dict of sbatch option name and values.Use this parameter to specify various job submission options such as the number of tasks, CPU cores, memory requirements, email notifications, and other sbatch options that are not covered by default settings. By default, the following sbatch options are set:
--output
: The output file is set to<uid>.out
.--job-name
: The job name is set to<full_uid>
.
The following arguments should bring you far:
--ntasks
: The number of tasks to run. This is the number of MPI processes to start.--mem-per-cpu
: The memory required per CPU core.--time
: The maximum time the job is allowed to run.--tmp
: Temporary scratch space to use for the job.
(self, executable='bash') -> None
Run the simulation using the specified executable and the default run file in the simulation directory.
This method executes the simulation's run script using the provided executable. By default, it uses "bash" to run the script locally. If "sbatch" is specified, the script will be submitted to a Slurm workload manager.
Arguments:
- executable:
str
='bash'
The executable to use for running the simulation script. Defaults to "bash". Use "sbatch" to submit to a Slurm scheduler.
Examples:
>>> sim_writer.run_simulation() # Runs locally with bash
>>> sim_writer.run_simulation(executable="sbatch") # Submits to Slurm
(self) -> None
Submit the simulation to a job scheduler.
This method submits the simulation's run script to a job scheduler using "sbatch".
It is a convenience wrapper around run_simulation(executable="sbatch")
.
Examples:
>>> sim_writer.submit_simulation()
(self, path) -> None
Create the groups for a series. Does not manage file state.
Arguments:
- path:
str
path of the series