Skip to content

Simulator

simulator

Simulation orchestration for flepimop2.

Simulator(system, engine, backend, *, target=None, simulate_config=None, system_config=None, engine_config=None, backend_config=None)

Build and run a single simulation from configuration models.

Attributes:

Name Type Description
system SystemABC

The built system instance.

engine EngineABC

The built engine instance.

backend BackendABC

The built backend instance.

target str | None

Optional target simulate config to use.

simulate_config SimulateSpecificationModel | None

The resolved simulation specification.

system_config ModuleModel | None

The resolved system configuration dict.

engine_config ModuleModel | None

The resolved engine configuration dict.

backend_config ModuleModel | None

The resolved backend configuration dict.

Initialize the simulator with resolved components.

Parameters:

Name Type Description Default
system SystemABC

The built system instance.

required
engine EngineABC

The built engine instance.

required
backend BackendABC

The built backend instance.

required
target str | None

Optional target simulate config to use.

None
simulate_config SimulateSpecificationModel | None

The resolved simulation specification, if available.

None
system_config ModuleModel | None

The resolved system configuration model, if available.

None
engine_config ModuleModel | None

The resolved engine configuration model, if available.

None
backend_config ModuleModel | None

The resolved backend configuration model, if available.

None

Raises:

Type Description
Flepimop2ValidationError

If the engine is incompatible with the system.

Source code in src/flepimop2/simulator.py
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
def __init__(  # noqa: PLR0913
    self,
    system: SystemABC,
    engine: EngineABC,
    backend: BackendABC,
    *,
    target: str | None = None,
    simulate_config: SimulateSpecificationModel | None = None,
    system_config: ModuleModel | None = None,
    engine_config: ModuleModel | None = None,
    backend_config: ModuleModel | None = None,
) -> None:
    """
    Initialize the simulator with resolved components.

    Args:
        system: The built system instance.
        engine: The built engine instance.
        backend: The built backend instance.
        target: Optional target simulate config to use.
        simulate_config: The resolved simulation specification, if available.
        system_config: The resolved system configuration model, if available.
        engine_config: The resolved engine configuration model, if available.
        backend_config: The resolved backend configuration model, if available.

    Raises:
        Flepimop2ValidationError: If the engine is incompatible with the system.

    """
    self.target = target
    self.simulate_config = simulate_config
    self.system_config = system_config
    self.engine_config = engine_config
    self.backend_config = backend_config
    self.system = system
    self.engine = engine
    self.backend = backend

    if issues := self.engine.validate_system(self.system):
        raise Flepimop2ValidationError(issues)

from_configuration_model(config_model, target=None) classmethod

Build a simulator from a configuration model.

Returns:

Type Description
Simulator

The constructed simulator instance.

Source code in src/flepimop2/simulator.py
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
@classmethod
def from_configuration_model(
    cls,
    config_model: ConfigurationModel,
    target: str | None = None,
) -> "Simulator":
    """
    Build a simulator from a configuration model.

    Returns:
        The constructed simulator instance.

    """
    simulate_config = _get_config_target(
        config_model.simulate,
        target,
        "simulate",
    )

    system_config = config_model.systems[simulate_config.system]
    engine_config = config_model.engines[simulate_config.engine]
    backend_config = config_model.backends[simulate_config.backend]

    system = build_system(system_config)
    engine = build_engine(engine_config)
    backend = build_backend(backend_config)

    return cls(
        system,
        engine,
        backend,
        target=target,
        simulate_config=simulate_config,
        system_config=system_config,
        engine_config=engine_config,
        backend_config=backend_config,
    )

run(initial_state, params)

Run the simulation and persist results via the backend.

Returns:

Type Description
Float64NDArray

The simulation result array.

Raises:

Type Description
ValueError

If simulate_config is not set.

Source code in src/flepimop2/simulator.py
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
def run(
    self,
    initial_state: Float64NDArray,
    params: dict[str, float],
) -> Float64NDArray:
    """
    Run the simulation and persist results via the backend.

    Returns:
        The simulation result array.

    Raises:
        ValueError: If `simulate_config` is not set.

    """
    if self.simulate_config is None:
        msg = "simulate_config must be set before running the simulator."
        raise ValueError(msg)
    res = self.engine.run(
        self.system,
        self.simulate_config.t_eval,
        initial_state,
        params,
    )
    self.backend.save(res, RunMeta())
    return res