src.pydasa.workflows.practical ============================== .. py:module:: src.pydasa.workflows.practical .. autoapi-nested-parse:: Module practical.py =========================================== 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 ------- .. autoapisummary:: src.pydasa.workflows.practical.MonteCarloSimulation Module Contents --------------- .. py:class:: MonteCarloSimulation Bases: :py:obj:`pydasa.core.basic.Foundation`, :py:obj:`pydasa.workflows.basic.WorkflowBase` **MonteCarloSimulation** class for managing Monte Carlo simulations in *PyDASA*. Manages the creation, configuration, and execution of Monte Carlo simulations of dimensionless coefficients. :param Foundation: Inherits common validation logic. :type Foundation: Foundation :param WorkflowBase: Inherits workflow basic functionalities. :type WorkflowBase: WorkflowBase .. attribute:: # 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 .. attribute:: # 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 .. attribute:: # 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. .. py:method:: __post_init__() *__post_init__()* Initializes the Monte Carlo handler. .. py:method:: configure_distributions() *configure_distributions()* Set up distribution specs (type, params, functions) for all variables. :raises ValueError: If variables are not defined. :raises ValueError: If distribution specifications are invalid. .. py:method:: configure_simulations() *configure_simulations()* Create MonteCarlo objects for each coefficient with distributions and dependencies. :raises ValueError: If distributions are not configured. :raises ValueError: If coefficients or variables are not defined. :raises ValueError: If coefficient variables are invalid. :raises RuntimeError: If simulation creation fails for a coefficient. .. py:method:: create_simulations() *create_simulations()* Convenience method to configure distributions and simulations in sequence. .. py:method:: run_simulation(iters = None, mode = None) *run_simulation()* Configure and execute Monte Carlo simulations for all coefficients. :param iters: Number of iterations (experiments) to run. If None, uses self._experiments value. Defaults to None. :type iters: Optional[int], optional :param mode: Simulation mode to set before running. If None, uses existing self._cat value. Defaults to None. :type mode: Optional[str], optional :raises ValueError: If simulations are not configured. :raises ValueError: If a required simulation is not found. :raises ValueError: If the number of experiments is not positive. :raises RuntimeError: If a simulation fails during execution. .. py:method:: get_simulation(name) *get_simulation()* Get a simulation by name. :param name: Name of the simulation. :type name: str :returns: The requested simulation. :rtype: MonteCarlo :raises ValueError: If the simulation doesn't exist. .. py:method:: get_distribution(name) *get_distribution()* Get the distribution by name. :param name: Name of the distribution. :type name: str :returns: The requested distribution. :rtype: Dict[str, Any] :raises ValueError: If the distribution doesn't exist. .. py:method:: get_results(name) *get_results()* Get the results of a simulation by name. :param name: Name of the simulation. :type name: str :returns: The results of the requested simulation. :rtype: Dict[str, Any] :raises ValueError: If the results for the simulation don't exist. .. py:property:: cat :type: str *cat* Get the Monte Carlo Simulation category. :returns: Category (DIST, DATA). :rtype: str .. py:property:: experiments :type: int *experiments* Get the number of experiments. :returns: Number of experiments to run. :rtype: int .. py:property:: simulations :type: Dict[str, pydasa.analysis.simulation.MonteCarlo] *simulations* Get the dictionary of Monte Carlo simulations. :returns: Dictionary of Monte Carlo simulations. :rtype: Dict[str, MonteCarlo] .. py:property:: is_configured :type: bool *is_configured* Check if simulations are configured and ready to run. :returns: True if both distributions and simulations are configured. :rtype: bool .. py:property:: has_results :type: bool *has_results* Check if simulation results are available. :returns: True if simulations have been run and results exist. :rtype: bool .. py:method:: 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. .. py:method:: clear() *clear()* Clear all attributes to default values. Resets all simulation properties to their initial state, including variables, coefficients, and results from WorkflowBase. .. py:method:: to_dict() *to_dict()* Convert the handler's state to a dictionary. :returns: Dictionary representation of the handler's state. :rtype: Dict[str, Any] .. py:method:: from_dict(data) :classmethod: *from_dict()* Create a MonteCarloSimulation instance from a dictionary. :param data: Dictionary containing the handler's state. :type data: Dict[str, Any] :returns: New instance of MonteCarloSimulation. :rtype: MonteCarloSimulation