Simulation¶
Schema for a simulation¶
- class pigreads.schema.simulation.Simulation(**data)¶
Bases:
BaseModelDefinition of a simulation.
The simulation is defined on a three dimensional cuboid with
Nz,Ny, andNxgrid points in the z, y, and x directions respectively. The grid points are separated bydz,dy, anddxunits in the z, y, and x directions. The simulation is run forNfrframes withNttime steps per frame. Each time step isdtunits long.The simulation definion contains a list of
pigreads.schema.model.ModelEntryobjects (models) which define the reaction terms in the reaction-diffusion equation. See alsopigreads.models.Models.The diffusivity matrix is defined by a
pigreads.schema.diffusivity.Diffusivityobject.The inhomogeneity (
inhom) of the medium, i.e., which parts of the simulation are active and which model to use where, is defined by a list ofpigreads.schema.setter.Setterobjects.The initial state of the simulation is defined by a dictionary of
pigreads.schema.setter.Setterobjects. The keys of the dictionary are the variable names and the values are lists of setters.A stimulus protocol is defined by a list of
pigreads.schema.stimulus.Stimulusobjects.The flag
double_precisioncan be set to True to use double precision floating point numbers in the simulation.The required key
pigreadsis used to identify the simulation file format.The simulation can be run by calling the
run()method.Example:
pigreads: 1 Nfr: 100 Nt: 200 Nz: 1 Ny: 200 Nx: 200 dt: 0.025 dz: 1 dy: 0.1 dx: 0.1 inhom: - spherical: outside: 0 diffusivity: 0.03 models: - marcotte2017dynamical: diffusivity_u: 1.0 diffusivity_v: 0.05 init: u: slices: value: 1 axis: -1 end: 10 v: slices: value: 2 start: 100 axis: -2
Read this and run a simulation like this:
from pigreads.schema import Simulation data = yaml.safe_load(yaml_string) sim = Simulation(**data) sim.run(path="result.npy")
Or use the CLI:
$ pigreads run config.yaml result.npy
- Parameters:
data (
Any)
- diffusivity: DiffusivityFile | DiffusivityParams¶
Definition of the diffusivity matrix.
- inhom: list[SetterFile | SetterSlices | SetterSpherical]¶
Definition of the inhomogeneity of the medium.
It defines which parts of the simulation are active and which model to use where.
- init: dict[str, list[SetterFile | SetterSlices | SetterSpherical]] | SetterFile¶
Definition of the initial state of the simulation.
The initial state is defined by a dictionary of variable names and lists of setters.
- static is_dict_of_dicts(d, excluded_keys=None)¶
Check if a value of a dictionary is also a dictionary.
- model_config: ClassVar[ConfigDict]¶
Configuration for
pydantic.BaseModel.
- models: list[ModelEntry]¶
Definition of the models to use in the simulation.
The models define the reaction terms in the reaction-diffusion equation.
- classmethod normalise_diffusivity(diffusivity)¶
Normalise the diffusivity.
Normally, the diffusivity is given as an instance of
pigreads.schema.diffusivity.Diffusivity.Alternatively, if the diffusivity is given as a string, read the diffusivity matrix from the file. If the diffusivity is given as a float, set the diffusivity matrix to a constant value.
- classmethod normalise_inhom(inhom)¶
Normalise the inhomogeneity.
The inhomogeneity is given as lists of
pigreads.schema.setter.Setterobjects.The setters are normalised using
pigreads.schema.setter.normalise_list_of_setters().
- classmethod normalise_init(init)¶
Normalise the initial state.
The initial state is given as lists of
pigreads.schema.setter.Setterobjects.The setters are normalised using
pigreads.schema.setter.normalise_list_of_setters().
- classmethod normalise_models(models)¶
Normalise the models.
Normally, the models are given as a list of
pigreads.schema.model.ModelEntry.Alternatively, if the models are given as a string, it is converted to a list with a single model entry. If the models are given as a dictionary, it is converted to a list of model entries. If the models are given as a list of strings, they are converted to a list of model entries with empty parameter sets.
- prepare_inhom()¶
Get the inhomogeneity array.
The inhomogeneity array is created by applying the setters in the
inhomlist to an array of ones with the shape of the grid.The inhomogeneity array is used to define the active parts of the simulation and which model to use where.
- prepare_models()¶
Create a models object from the model entries.
The model entries are used to create the models object which is used to run the simulation.
- Return type:
- Returns:
Models object.
- prepare_space()¶
Get arrays with the coordinates of the grid points.
The arrays are returned as a tuple of three arrays with the z, y, and x coordinates of the grid points.
- prepare_states(models, varidx, inhom, path=None)¶
Prepare the state array.
The state array is created as a five dimensional array with the shape
(Nfr, Nz, Ny, Nx, Nv)whereNvis the number of variables in the model, see alsopigreads.models.Models.Nv().This array is created in three steps:
Create a new array with the correct shape and dtype using
pigreads.helper.prepare_array().Set the first frame of the array to the resting state of the model, see
pigreads.models.Models.resting_states().Apply the initial state setters to the first frame of the array.
The array is returned as a memory map if a path is given, otherwise as a normal numpy array.
- Parameters:
- Return type:
- Returns:
State array.
- prepare_stim(varidx)¶
Prepare the stimulus signal and shape.
The stimulus signal is created as a three dimensional array with the shape
((Nfr - 1)*Nt, Ns, Nv)whereNsis the number of stimuli in the simulation andNvis the number of variables in the model, see alsopigreads.models.Models.Nv(). This array is first set to zero and then modified by the signal steps in the stimulus objects, seepigreads.schema.stimulus.SignalStepandpigreads.schema.stimulus.Stimulus.The stimulus shape is created as a four dimensional array with the shape
(Ns, Nz, Ny, Nx)whereNsis the number of stimuli in the simulation. This array is first set to zero and then modified by the shape setters in the stimulus objects, seepigreads.schema.setter.Setterandpigreads.schema.stimulus.Stimulus.The stimulus signal and shape are returned as a tuple.
- run(path=None, start_frame=0, progress=<function Simulation.<lambda>>, callback=None, infinite=False)¶
Run the simulation.
- Parameters:
start_frame (
int) – The frame to start the simulation at, default is 0. (default:0)path (
Path|str|None) – The path to save the state array to, default isNone, seepigreads.helper.prepare_array(). (default:None)progress (
Callable[[Any],Any]) – A function to show progress, default is no progress updates. (default:<function Simulation.<lambda> at 0x758a35cc4880>)callback (
Callable[[ndarray[Any,Any],int],bool] |None) – A function to call after each frame, default is no callback. If it returnsFalse, stop iteration early. (default:None)infinite (
bool) – Whether to run the simulation in an infinite loop, default isFalse. (default:False)
- Return type:
- Returns:
The results in the state array with the shape
(Nfr, Nz, Ny, Nx, Nv).
The simulation calls the following methods to prepare the simulation:
The simulation is then run frame by frame using the
pigreads.models.Models.run()function. The results are saved to the state array and returned.
- stim: list[Stimulus]¶
Definition of the stimulus protocol.
The stimulus protocol is defined by a list of
pigreads.schema.stimulus.Stimulusobjects.