Skip to content

simulator.worker

The worker of a scan: the points of a chunk on one plan and one model.

run_chunk is the same function serially and in a worker process: for every point of a chunk it applies the values of the point to the plan of the chunk, Plan.with_values, and runs the plan with execute; the native solutions of the points which ran are stacked into arrays padded with NaN, the observables of the run are evaluated on them, see sbmlsim.simulator.observables, and the kept timecourses are interpolated onto the grid of times of the chunk, if it has one. A run without observables needs no native solution: the kept selections of every point are interpolated right after its simulation, so the memory of a chunk is bounded by its grid. A point fails exactly when its own simulation or observables fail, not because of the other points of its chunk. Nothing in here uses pint or xarray, and a chunk and its result are numbers, strings, a plan and the graph of the observables, so they pickle.

In a worker process the model of a chunk is loaded once from its ModelSpec and kept, see sbmlsim.parallel.worker_cache, with the integrator of the model of the parent and every one of its settings, those of the simulator and those set on the model, so a worker integrates as the parent does and the result does not depend on the number of workers.

A point which fails is reported once, by its error: roadrunner logs the error of CVODE as well, so its log level is lowered while a chunk runs. SUNDIALS writes its own messages, a dozen lines or more per failing point, which a worker process silences for the models it loads, see quiet_sundials; a serial run keeps them, as the model of the user is not changed.

ScanPointError

ScanPointError(index, message)

Bases: RuntimeError

A point of a scan failed and the run raises, see Chunk.on_error.

Attributes:

Name Type Description
index

the flat index of the point in the scan.

message

the error of the point.

Create the error of a point, the arguments pickle.

ModelSpec dataclass

ModelSpec(key, source, parameters, integrator, settings)

What a worker needs to load a model as the parent loaded it.

Attributes:

Name Type Description
key str

identifies the model, its integrator and the settings of the integrator, the key of the model in the cache of a worker.

source str

the SBML of the model as the parent loaded it, see RoadrunnerSBMLModel.sbml, or the path of the file parallel.shared_payloads shares it in. It is flattened and the path is absolute, so nothing is resolved against a directory.

parameters tuple[tuple[str, float], ...]

the parameters added to the model, see AbstractModel.

integrator str

the name of the integrator, e.g. cvode.

settings tuple[tuple[str, Any], ...]

every setting of the integrator by its name, the absolute tolerance as the AbsoluteTolerance of the model.

of classmethod

of(model)

Get the spec of a loaded model and of the state of its integrator.

The integrator and its settings are read from the model after the simulator set its own, see integrator_state, so a setting of the model which the simulator does not set, e.g. one of RoadrunnerSBMLModel(settings=...) or of set_integrator_settings, holds in a worker as well.

load

load()

Load the model with the integrator and its settings.

Chunk dataclass

Chunk(
    indices,
    plan,
    model,
    graph,
    values,
    timed,
    time,
    on_error="raise",
)

Points of a scan which share a plan and a model.

Attributes:

Name Type Description
indices ndarray

the flat indices of the points in the scan, in C order.

plan Plan

the plan of the points.

model int

the index of the model of the points among the models of the run.

graph ObservableGraph

the observables of the run, which give the selections.

values dict[str, ndarray]

target -> value of every point, which replaces the target wherever the plan sets it and is a change before the initialization otherwise.

timed dict[float, dict[str, ndarray]]

time -> target -> value of every point, a change at that time.

time ndarray | None

the grid of times to interpolate the kept timecourses onto, None for the time points of the simulation.

on_error OnError

what to do about a point which fails.

selections property

selections

Get the selections of roadrunner, time first.

plan_of

plan_of(k)

Get the plan of the k-th point of the chunk, see point_plan.

ChunkResult dataclass

ChunkResult(indices, values, scalars, status, errors)

The answer of a chunk.

Attributes:

Name Type Description
indices ndarray

the flat indices of the points, those of the chunk.

values ndarray

the kept timecourses, (point, row, column) with the time first and the timecourses of the graph, padded with NaN.

scalars ndarray

the kept values per simulation, (point, column) with the scalars of the graph.

status ndarray

0 for a point which ran, 1 for one which failed.

errors tuple[tuple[int, str], ...]

the flat index and the error of the first MAX_ERRORS points which failed, in the order of the scan.

integrator_state

integrator_state(model)

Get the name of the integrator of a model and every one of its settings.

The absolute tolerance is the AbsoluteTolerance of the model, from which RoadrunnerSBMLModel.set_integrator_settings computes the tolerance of every state, and not the vector of the integrator.

Parameters:

Name Type Description Default
model RoadrunnerSBMLModel

the loaded model.

required

Returns:

Type Description
tuple[str, tuple[tuple[str, Any], ...]]

The name of the integrator and its settings, sorted by name.

point_plan

point_plan(plan, values, timed, k)

Get the plan of the k-th point of values.

The values of the dimensions without a time are applied first, then the changes of the dimensions with one in time order, so a target in both has the value of the change at its time.

Parameters:

Name Type Description Default
plan Plan

the plan of the points.

required
values Mapping[str, ndarray]

target -> value of every point.

required
timed Mapping[float, Mapping[str, ndarray]]

time -> target -> value of every point.

required
k int

the point.

required

Returns:

Type Description
Plan

The plan with the values of the point.

run_chunk

run_chunk(chunk, model)

Run the points of a chunk on a loaded model, see the module.

Parameters:

Name Type Description Default
chunk Chunk

the points.

required
model RoadrunnerSBMLModel

the loaded model of the chunk.

required

Returns:

Type Description
ChunkResult

The values of every point.

Raises:

Type Description
ScanPointError

for the first point which fails, with on_error="raise".

observe

observe(graph, selections, solutions, plans)

Evaluate the observables on the native solutions of points.

The graph is evaluated on all solutions at once. If an observable fails the points are evaluated one at a time, each alone on its native solution without padding, so a point fails exactly when its own evaluation fails, whatever the other points are; the outputs of the points which succeed are stacked again, padded with NaN to the longest of them.

Parameters:

Name Type Description Default
graph ObservableGraph

the observables.

required
selections Sequence[str]

the columns of the solutions, the time first.

required
solutions Sequence[ndarray]

the native solutions of the points.

required
plans Sequence[Plan]

their plans.

required

Returns:

Type Description
ndarray

The time points (n_points, n_rows) padded with NaN, the kept

dict[str, ndarray]

outputs (a timecourse (n_points, n_rows), a value per simulation

list[tuple[int, str, BaseException]]

(n_points,)) of the points which are left, in their order, and the

tuple[ndarray, dict[str, ndarray], list[tuple[int, str, BaseException]]]

points whose observable failed (the position in solutions) with

tuple[ndarray, dict[str, ndarray], list[tuple[int, str, BaseException]]]

their message and error.

run_chunk_in_worker

run_chunk_in_worker(spec, chunk)

Run a chunk in a worker process, on the model the worker keeps.

Parameters:

Name Type Description Default
spec ModelSpec

the model of the chunk and the settings of its integrator.

required
chunk Chunk

the points.

required

Returns:

Type Description
ChunkResult

The values of every point, see run_chunk.

quiet_roadrunner

quiet_roadrunner()

Lower the log level of roadrunner to critical messages, then restore it.

roadrunner logs the error of CVODE of a point which fails, which the run reports as the error of the point.

quiet_sundials

quiet_sundials()

Silence the messages of SUNDIALS of the models a worker process loads.

SUNDIALS reads the files of its messages from the environment when roadrunner creates the integrator of a model, so this applies to the models loaded after it and is no setting of a loaded model. Only a worker process of a pool is changed, never the process of the user, and a file the environment names already stays.

The change is permanent for the worker: a kept worker of sbmlsim.parallel.pool never prints a message of SUNDIALS again, for every model it loads later, e.g. the cases of testsuite.map_cases, also for a scan which raises and for a point which succeeds. A failure is reported through its exception or the variable status.