pyCBD.simulator module¶
- class pyCBD.simulator.Simulator(model)[source]¶
Bases:
objectSimulator for a CBD model. Allows for execution of the simulation. This class implements the semantics of CBDs.
- Parameters
model (CBD.Core.CBD) – A
CBDmodel to simulate.- Defaults
The following properties further define the internal mechanisms of the simulator. As a handy look-up table, all defaults are listed in the table below. Take a look at the
See Alsocolumn for more info on the property.
Property
Default
See Also
real-time?
Falsereal-time scale
1.0
termination time
float('inf')termination condition
Nonescheduler
threading platform (subsystem)
pyCBD.realtime.threadingPython.threadingPythonsetRealTimePlatform(),setRealTimePlatformThreading(),setRealTimePlatformTk(),setRealTimePlatformGameLoop()progress bar?
Falsestrong component system solver
pyCBD.linearsolver.LinearSolverstate event locator
- run(term_time=None)[source]¶
Simulates the model.
- Parameters
term_time (float) – When not
None, overwrites the termination time with the new value. This value will be accurate upto 8 decimal points.
- is_running()[source]¶
Returns
Trueas long as the simulation is running. This is a convenience function to keep real-time simulations alive, or to interact from external sources.
- setDeltaT(delta_t)[source]¶
Sets the delta in-between iteration steps.
- Parameters
delta_t (float) – The delta.
Note
If the model has a
pyCBD.lib.std.Clockblock instance, calling this function will have no effect. It is merely meant to be used for fixed step simulations.Note
While unnecessary, this function is kept for backwards compatibility.
See also
- setCommunicationInterval(delta)[source]¶
Sets the time delta at which the information is communicated to the user. When
None, the integration interval (i.e., delta t) will be used for this.- Parameters
delta (float) – The delta. When
None, this value will be unset.
Note
This function will only influence the tracers that make use of this feature. This has to do with what is communicated to the user and not to when the actual computations happen.
New in version 1.6.
See also
- setScheduler(scheduler)[source]¶
Sets the scheduler for the simulation. It will identify the order of the components in a computation.
- Parameters
scheduler (CBD.scheduling.Scheduler) – The scheduler to use.
- setAlgebraicLoopSolver(solver)[source]¶
Sets the algebraic loop solver for the simulation. It will identify the order of the components in a computation.
- Parameters
solver (CBD.loopsolvers.solver.Solver) – The loop solver to use.
- setStateEventLocator(stel)[source]¶
Sets the state event locator to use when level crossings occur.
- Parameters
stel (CBD.state_events.locators.StateEventLocator) – The new state event to use.
- registerStateEvent(event)[source]¶
Registers a state event to the current simulator.
- Parameters
event (CBD.state_events.StateEvent) – The event to register.
- setBlockRate(block_path, rate)[source]¶
Sets the rate for a specific block. Independent of the stepsize, the rate will identify that a certain block must only execute every
rtime.Note
Blocks for which no rate has been set will always be computed.
- Parameters
block_path (str) – The path of the block to set a rate of.
rate (float) – The rate of the block.
- setRealTime(enabled=True, scale=1.0)[source]¶
Makes the simulation run in (scaled) real time.
- Parameters
enabled (bool) – When
True, realtime simulation will be enabled. Otherwise, it will be disabled. Defaults toTrue.scale (float) – Optional scaling for the simulation time. When greater than 1, the simulation will run slower than the actual time. When < 1, it will run faster. E.g.
scale = 2.0will run twice as long. Defaults to1.0.
- setProgressBar(enabled=True)[source]¶
Uses the tqdm package to display a progress bar of the simulation.
- Parameters
enabled (bool) – Whether or not to enable/disable the progress bar. Defaults to
True(= show progress bar).
Note
A progressbar hijacks printing to the console, hence no output shall be shown.
- Raises
AssertionError – if the
tqdmmodule cannot be located.
- setTerminationCondition(func)[source]¶
Sets the system’s termination condition.
- Parameters
func – A function that takes the model and the current iteration as input and produces
Trueif the simulation needs to terminate.
Note
When set, the progress bars (see
setProgressBar()) may not work as intended.See also
- setTerminationTime(term_time)[source]¶
Sets the termination time of the system.
Note
This is accurate upto 8 decimal points.
- Parameters
term_time (float) – Termination time for the simulation.
- setRealTimePlatform(subsystem, *args)[source]¶
Sets the realtime platform to a platform of choice. This allows more complex/efficient simulations.
Calling this function automatically sets the simulation to realtime.
- Parameters
subsystem (Platform) – The platform to use.
args – Optional arguments for this platform. Currently, only the TkInter platform makes use of these arguments.
Note
To prevent misuse of the function, please use one of the wrapper functions when you have no idea what you’re doing.
- setRealTimePlatformThreading()[source]¶
Wrapper around the
setRealTimePlatform()call to automatically set the Python Threading backend.Calling this function automatically sets the simulation to realtime.
- setRealTimePlatformGameLoop()[source]¶
Wrapper around the
setRealTimePlatform()call to automatically set the Game Loop backend. Using this backend, it is expected the user will periodically call therealtime_gameloop_call()method to update the simulation step. Timing is still maintained internally.Calling this function automatically sets the simulation to realtime.
- setRealTimePlatformTk(root)[source]¶
Wrapper around the
setRealTimePlatform()call to automatically set the TkInter backend.Calling this function automatically sets the simulation to realtime.
- Parameters
root – TkInter root window object (tkinter.Tk)
- realtime_gameloop_call(time=None)[source]¶
Do a step in the realtime-gameloop platform.
- Parameters
time (float) – Simulation time to be passed on. Only to be used for the alternative gameloop backend.
Note
This function will only work for a
Platform.GAMELOOPor aPlatform.GLAsimulation, after therun()method has been called.
- connect(name, function)[source]¶
Connect an event with an additional, user-defined function. These functions will be executed on a separate thread and polled every 50 milliseconds.
Warning
It is expected that the passed function terminates at a certain point in time, to prevent an infinitely running process.
Warning
There is no guarantee that these functions will be executed in-order of connection. For safety, it is best to see these events as not thread-safe.
The functions will be called in the order they were connected to the events, with the associated arguments. The accepted signals are:
started: Raised whenever the simulation setup has completed,but before the actual simulation begins.
finished: Raised whenever the simulation finishes.prestep(t, st): Raised before a step is done.tis the realtime before the step and
stis the simulation time.
poststep(o, t, st): Raised after a step is done.ois the realtime before the step,
tis the real time after the step andstis the simulation time of the step.
clock_update(delta): Raised whenever the clock updates. It takes the (new)delta for the simulation.
- Parameters
name (str) – The name of the signal to raise.
function – A function that will be called with the optional arguments whenever the event is raised.
- signal(name, *args)[source]¶
Raise a signal with a specific name and arguments.
The accepted signals are:
started: Raised whenever the simulation setup hascompleted, but before the actual simulation begins.
finished: Raised whenever the simulation finishes.prestep: Raised before a step is done.poststep: Raised after a step is done.clock_update(delta): Raised whenever the clock updates. It takesthe (new) delta for the simulation.
Note
Normally, users do not need to call this function.
- Parameters
name (str) – The name of the signal to raise.
*args – Additional arguments to pass to the connected events.
See also
- setCustomTracer(*tracer)[source]¶
Sets a custom tracer.
- Parameters
*tracer – Either a single instance of a subclass of
pyCBD.tracers.baseTracer.BaseTraceror three elementsfilename(str),classname(str) andargs(tuple) to allow instantiation similar to PythonPDEVS.
Note
Calling this function multiple times with the same arguments will continuously add new tracers. Thus output to multiple files is possible, though more inefficient than simply (manually) copying the file at the end.
- setVerbose(filename=None)[source]¶
Sets the verbose tracer.
- Parameters
filename (Union[str, None]) – The file to which the trace must be written. When
None, the trace will be written to the console.
Note
Calling this function multiple times will continuously add new tracers. Thus output to multiple files is possible, though more inefficient than simply (manually) copying the file at the end.
Warning
Using multiple verbose tracers with the same filename will yield errors and undefined behaviour.