src.pydasa.workflows.practical#
Module for MonteCarloSimulation to manage the Monte Carlo experiments in PyDASA.
This module provides classes for managing Monte Carlo simulations of the dimensionless coefficients.
- Classes:
MonteCarloSimulation: Manages Monte Carlo simulation runs, including configuration and execution of the experiments.
IMPORTANT: Based on the theory from:
# H.Gorter, Dimensionalanalyse: Eine Theoririe der physikalischen Dimensionen mit Anwendungen
Classes#
MonteCarloSimulation class for managing Monte Carlo simulations in PyDASA. |
Module Contents#
- class src.pydasa.workflows.practical.MonteCarloSimulation#
Bases:
pydasa.core.basic.Foundation,pydasa.workflows.basic.WorkflowBaseMonteCarloSimulation class for managing Monte Carlo simulations in PyDASA.
Manages the creation, configuration, and execution of Monte Carlo simulations of dimensionless coefficients.
- Parameters:
Foundation (Foundation) – Inherits common validation logic.
WorkflowBase (WorkflowBase) – Inherits workflow basic functionalities.
- # From Foundation
_name (str): User-friendly name of the Monte Carlo simulation handler. description (str): Brief summary of the Monte Carlo simulation handler. _idx (int): Index/precedence of the simulation handler. _sym (str): Symbol representation (LaTeX or alphanumeric). _alias (str): Python-compatible alias for use in code. _fwk (str): Frameworks context (PHYSICAL, COMPUTATION, SOFTWARE, CUSTOM).
- Type:
Identification and Classification
- # From WorkflowBase
_variables (Dict[str, Variable]): All available parameters/variables in the model (Variable). Accepts Variable instances or dicts. _schema (Optional[Schema]): Dimensional framework schema for the workflow. After __post_init__, this will always be a Schema instance (inherited but not actively used in MonteCarloSimulation). _coefficients (Dict[str, Coefficient]): All available coefficients in the model (Coefficient). Accepts Coefficient instances or dicts. _results (Dict[str, Dict[str, Any]]): Consolidated results from Monte Carlo simulations. _is_solved (bool): Flag indicating if the simulation workflow has been completed.
- Type:
Common Workflow Components
- # Specific to MonteCarloSimulation
_cat (str): Category of simulation run (DIST, DATA). _distributions (Dict[str, Dict[str, Any]]): All distribution functions used in the simulations (specific name, parameters, and function). _experiments (int): Number of simulations to run. Default is -1. _simulations (Dict[str, MonteCarlo]): All Monte Carlo simulations performed for each coefficient. _shared_cache (Dict[str, NDArray[np.float64]]): In-memory cache for simulation data shared between coefficients.
- _distributions: Dict[str, Dict[str, Any]]#
Variable sampling distributions and specifications for simulations (specific name, parameters, and function).
In-memory cache for simulation data between coefficients.
- _simulations: Dict[str, pydasa.analysis.simulation.MonteCarlo]#
Dictionary of Monte Carlo simulations.
- configure_distributions()#
configure_distributions() Configures sampling distributions for all variables. Sets up distribution specifications (type, parameters, functions) for each variable that will be used during Monte Carlo simulation.
- Raises:
ValueError – If variables are not defined.
ValueError – If distribution specifications are invalid.
- Return type:
None
- configure_simulations()#
configure_simulations() Configures Monte Carlo simulation objects for each coefficient. Creates MonteCarlo instances for each coefficient with appropriate distributions and dependencies. Requires distributions to be configured first.
- Raises:
ValueError – If distributions are not configured.
ValueError – If coefficients or variables are not defined.
- Return type:
None
- create_simulations()#
create_simulations() Configures distributions and simulations if not already set. This is a convenience method that orchestrates the full configuration process. It calls configure_distributions() and configure_simulations() in sequence.
- Raises:
ValueError – If variables or coefficients are not defined.
ValueError – If distribution specifications are invalid.
- Return type:
None
- _validate_coefficient_vars(coef, pi_sym)#
_validate_coefficient_vars() Validates and returns coefficient’s var_dims.
- Parameters:
coef (Coefficient) – The coefficient to validate.
pi_sym (str) – The coefficient symbol for error messages.
- Returns:
The validated var_dims dictionary.
- Return type:
Dict[str, Any]
- Raises:
ValueError – If var_dims is None or missing.
- _config_distributions()#
_config_distributions() Creates the Monte Carlo distributions for each variable.
- Raises:
ValueError – If the distribution specifications are invalid.
- Return type:
None
- _get_distributions(var_keys)#
_get_distributions() Retrieves the distribution specifications for a list of variable keys.
- Parameters:
var_keys (List[str]) – List of variable keys.
- Returns:
Dictionary of distribution specifications.
- Return type:
- Raises:
ValueError – If required distributions are missing.
- _get_dependencies(var_keys)#
_get_dependencies() Retrieves variable dependencies for a list of variable keys.
_init_shared_cache() Initialize shared cache for all variables.
- Return type:
None
- _config_simulations()#
_config_simulations() Sets up Monte Carlo simulation objects for each coefficient to be analyzed.
Creates a MonteCarlo instance for each coefficient with appropriate distributions and dependencies.
- Raises:
ValueError – If coefficients or variables are not properly configured.
- Return type:
None
- run(iters=None)#
run() Runs the Monte Carlo simulations.
- Parameters:
iters (Optional[int]) – Number of iterations (experiments) to run. If None, uses self._experiments value. Defaults to None.
- Raises:
ValueError – If simulations are not configured.
ValueError – If a required simulation is not found.
- Return type:
None
- run_simulation(iters=None)#
run_simulation() Convenience method to configure and run Monte Carlo simulations.
This method combines create_simulations() and run() into a single call. It automatically sets up distributions and simulations if needed, then runs the simulation.
- Parameters:
iters (Optional[int]) – Number of iterations (experiments) to run. If None, uses self._experiments value. Defaults to None.
- Return type:
None
Example:
>>> mc_handler = MonteCarloSimulation(...) >>> mc_handler.run_simulation(iters=10000)
- get_simulation(name)#
get_simulation() Get a simulation by name.
- Parameters:
name (str) – Name of the simulation.
- Returns:
The requested simulation.
- Return type:
- Raises:
ValueError – If the simulation doesn’t exist.
- get_distribution(name)#
get_distribution() Get the distribution by name.
- Parameters:
name (str) – Name of the distribution.
- Returns:
The requested distribution.
- Return type:
Dict[str, Any]
- Raises:
ValueError – If the distribution doesn’t exist.
- get_results(name)#
get_results() Get the results of a simulation by name.
- Parameters:
name (str) – Name of the simulation.
- Returns:
The results of the requested simulation.
- Return type:
Dict[str, Any]
- Raises:
ValueError – If the results for the simulation don’t exist.
- property cat: str#
cat Get the Monte Carlo Simulation category.
- Returns:
Category (DIST, DATA).
- Return type:
- property experiments: int#
experiments Get the number of experiments.
- Returns:
Number of experiments to run.
- Return type:
- property simulations: Dict[str, pydasa.analysis.simulation.MonteCarlo]#
simulations Get the dictionary of Monte Carlo simulations.
- Returns:
Dictionary of Monte Carlo simulations.
- Return type:
Dict[str, MonteCarlo]
- property is_configured: bool#
is_configured Check if simulations are configured and ready to run.
- Returns:
True if both distributions and simulations are configured.
- Return type:
- property has_results: bool#
has_results Check if simulation results are available.
- Returns:
True if simulations have been run and results exist.
- Return type:
- reset()#
reset() Reset simulation state while preserving input configuration.
Clears results, solved state, and simulation-specific attributes (simulations, distributions, cache) while keeping variables, schema, and coefficients.
- Return type:
None
- clear()#
clear() Clear all attributes to default values.
Resets all simulation properties to their initial state, including variables, coefficients, and results from WorkflowBase.
- Return type:
None
- to_dict()#
to_dict() Convert the handler’s state to a dictionary.
- Returns:
Dictionary representation of the handler’s state.
- Return type:
Dict[str, Any]