Simulation Module¶
penred simulation module
- pyPenred.simulation.configFile2YAML(arg0: str) str¶
Reads a configuration file and returns a YAML string with the read information.
- Parameters:
filename (str) – File to be read.
- Returns:
String containing the information in YAML format
- pyPenred.simulation.dict2SectionString(conf: dict) str¶
Converts a dictionary to a compatible penRed configuration section string.
- Parameters:
conf (dict) – configuration dictionary to be converted.
- Returns:
String containing the converted dictionary
- pyPenred.simulation.errorMessage(code: SupportsInt) str¶
Produces the error message associated to the specified error code.
- Parameters:
code (int) – Error code.
- Returns:
String containing the error message
- pyPenred.simulation.setConfigurationLog(filename: str) None¶
Specify the file where the configuraiton logs will be redirected.
- Parameters:
filename (str) – Configuration log file name.
- Returns:
None
- pyPenred.simulation.setSimulationLog(filename: str) None¶
Specify the file where the simulation logs will be redirected.
- Parameters:
filename (str) – Simulation log file name.
- Returns:
None
- class pyPenred.simulation.simulator¶
Bases:
pybind11_objectA simulator class for penRed simulations.
This class handles particle transport and interaction modeling in the penRed framework. It provides methods to configure and run simulations, track particles, and retrieve results.
- configContext(self: simulator, config: dict) None¶
Saves the context configuration from the provided dictionary.
- Parameters:
config (dict) – Dictionary with the context configuration.
- Returns:
None
- Raises:
ValueError – If configuraiton set fails.
- configFromFile(self: simulator, filename: str) None¶
Sets the entire configuration from a file matching the penRed internal data format.
- Parameters:
filename (str) – Configuration file.
- Returns:
None
- Raises:
ValueError – If file parsing fails.
- configFromString(self: simulator, config: str) None¶
Sets the entire configuration from a string matching the penRed internal data format.
- Parameters:
config (str) – Text containing the configuration.
- Returns:
None
- Raises:
ValueError – If file parsing fails.
- configGeometry(self: simulator, config: dict) None¶
Saves the geometry configuration from the provided dictionary.
- Parameters:
config (dict) – Dictionary with the geometry configuration.
- Returns:
None
- Raises:
ValueError – If configuraiton set fails.
- configSource(self: simulator, config: dict) None¶
Saves the particle sources configuration from the provided dictionary.
- Parameters:
config (dict) – Dictionary with the source configuration.
- Returns:
None
- Raises:
ValueError – If configuraiton set fails.
- configTally(self: simulator, config: dict) None¶
Saves the tallies configuration from the provided dictionary.
- Parameters:
config (dict) – Dictionary with the tallies configuration.
- Returns:
None
- Raises:
ValueError – If configuraiton set fails.
- configVR(self: simulator, config: dict) None¶
Saves the variance reduction configuration from the provided dictionary.
- Parameters:
config (dict) – Dictionary with the variance reduction configuration.
- Returns:
None
- Raises:
ValueError – If configuraiton set fails.
- configure(self: simulator, config: dict) None¶
Saves the entire simulation configuration from the provided dictionary.
- Parameters:
config (dict) – Dictionary with the simulation configuration.
- Returns:
None
- Raises:
ValueError – If configuraiton set fails.
- forceFinish(self: simulator) None¶
Forces the simulation finish. Only works for non interactive simulations.
- Parameters:
None
- Returns:
None
- Raises:
ValueError – Simulation running in interactive mode.
- getResults(self: simulator, tally_name: str, extract_info: bool = False) tuple¶
Gets the simulation results from the specified tally.
- Parameters:
- Returns:
On success, a tuple of numpy vectors storing the tally’s specific results is returned. If the requested tally does not support retrieving results in that format, returned vectors will be empty.
- instructionClear(self: simulator) int¶
Clears all pending instructions in queue.
- Parameters:
None
- Returns:
The last queued instruction numerical ID.
- instructionEnd(self: simulator) int¶
Appends a finish simulation instruction to the instructions queue. Once executed, the simulation is finished and the tally postprocessing starts.
- Parameters:
None
- Returns:
The numerical ID of the enqueued instruction.
- instructionEndAndWait(self: simulator, timeout: SupportsInt) bool¶
Enqueue an end instruction and waits until its completion or until reaching the timeout. In anycase, the end instruction sill enqueued until it is processed or cleared using a different call.
- Parameters:
timeout (unsigned) – Timeout in seconds.
- Returns:
Returns True if the end instruction is processed before the timeout or False otherwise.
- instructionSimulate(self: simulator, source_name: str, hists: SupportsFloat, translation: Sequence[SupportsFloat] = [], rotZYZ: Sequence[SupportsFloat] = []) int¶
Appends a simulate instruction to the instructions queue. Once executed, the specified source is simulated. If both, a rotation and a translation are provided, the former is applied first and the latter seconth. If provided, the three components must be specified.
- Parameters:
source_name (str) – Name of the source to be simulated
hists (float) – Number of histories to simulate
translation (list[float,float,float]) – Specify the translation, in cm, to be applied to the original source position before the simulation starts. If no specified, no translation will be applied
rotZYZ (list[float,float,float]) –
- Specify the rotation angles, in rad, to be applied to the original source before the simulation starts. The resulting rotation is the product of three rotations around the Z,Y and Z axis. The angles are, in order:
omega -> rotation angle around the z axis (rad)
theta -> rotation angle around the y axis (rad)
phi -> rotation angle around the z axis (rad)
If not provided, no extra rotation is applied to the original source.
- Returns:
The numerical ID of the enqueued instruction.
- instructionWait(self: simulator, id: SupportsInt, timeout: SupportsInt) bool¶
Waits until the specified instruction ID is processed or the timeout is reached
- Parameters:
id (unsigned) – ID of the instruction to wait.
timeout (unsigned) – Timeout in seconds.
- Returns:
Returns True if the instruction is processed before the timeout or False otherwise.
- isSimulating(self: simulator) bool¶
Checks if a simulation is running.
- Parameters:
None
- Returns:
True if a simulation is running, false otherwise.
- Return type:
Bool
- printResults(self: simulator, prefix: str = '', sigma: SupportsInt = 2, coordinates: bool = True, bins: bool = True, effective: bool = False) None¶
Prints the latest available results to different files.
- Parameters:
prefix (str) – Prefix applied to the filenames.
sigma (unsigned) – Number of printed sigmas for the uncertainties.
coordinates (bool) – Enable/disable coordinates printing.
bins (bool) – Enable/disable bin numbering printing.
effective (bool) – If enabled dimensions with a single bin will be ignored for printing.
- Returns:
Returns True if the end instruction is processed before the timeout or False otherwise.
- setSimConfig(self: simulator, config: dict) None¶
Saves the simulation parameters from the provided dictionary.
- Parameters:
config (dict) – Dictionary with the variance simulation configuration.
- Returns:
None
- Raises:
ValueError – If configuraiton set fails.
- simSpeeds(self: simulator) tuple¶
If a simulation is running, returns the simulation speeds, in histories per second, for each used thread
- Parameters:
None
- Returns:
A tuple containing the simulation speed of each thead
- Return type:
- simulate(self: pyPenred.simulation.simulator, async: bool = False, interactive: bool = False) None¶
Executes the configured simulation in either blocking or non-blocking mode.
- Parameters:
async (bool, optional) – Simulation execution mode. - False (default): Blocking mode (waits for completion) - True: Non-blocking mode (returns immediately)
interactive (bool, optional) – Simulation executes in interactive mode. Only available on asynchronous executions - False (default): The simulation starts and finish according to the configuration parameters. - True: Interactive. The simulation is configured and waits further instructions.
- Returns:
None
- Raises:
ValueError – If invalid configurations are detected pre-simulation.
Example
#!/usr/bin/env python3 import pyPenred import time #Define configuration file configFile = "config.in" #Create simulation object simu = pyPenred.simulation.create() #Try to configure the simulation from the configuration file simu.configFromFile(configFile) print("Configuration set\n") #Start the simulation asynchronously simu.simulate(True) #Simulation started, check status every 20 seconds print("Simulation started\n") while simu.isSimulating(): time.sleep(20) status = simu.stringifyStatus() for e in status: print(e) print("Simulation finished")