Simulation
This class provides some boilerplate code for creating simulations easily. It comes with defaults for seeding cells, drawing, logging of statistics, saving output images, and running the simulation. Each of these default methods can be overwritten by the user while keeping the other default methods intact. See the Simulation#constructor for details on how to configure a simulation.
See:
Constructor Summary
Public Constructor | ||
public |
constructor(config: object, simsettings: object) The constructor of class Simulation takes two arguments. |
Member Summary
Public Members | ||
public |
C: * |
|
public |
Attached Canvas object. |
|
public |
[m]: * Any function suplied in the custommethods argument to the constructor is bound to the object. |
|
public |
Configuration of the simulation environment |
|
public |
constraints: * |
|
public |
Custom methods added to / overwriting the default Simulation class. |
|
public |
Attached GridManipulator object. |
|
public |
See if objects of class Canvas and GridManipulator already exist. |
|
public |
Draw the canvas every [rate] MCS. |
|
public |
To check from outside if an object is a Simulation; doing this with instanceof doesn't work in some cases. Any other object will not have this variable and return 'undefined', which in an if-statement equates to a 'false'. @type{boolean} |
|
public |
Log stats every [rate] MCS. |
|
public |
Log stats or not. |
|
public |
Log stats or not, specified for both browser and node mode. |
|
public |
See if code is run in browser or via node, which will be used below to determine what the output should be. |
|
public |
Saving images or not. |
|
public |
Where to save images. |
|
public |
Track the time of the simulation. |
Private Members | ||
private |
Should the simulation be running? Change this to pause; see the toggleRunning method. |
Method Summary
Public Methods | ||
public |
Adds a Canvas object when required. |
|
public |
Add additional constraints to the model before running; this method is automatically called and adds constraints given in the config object. |
|
public |
Adds a GridManipulator object when required. |
|
public |
This automatically creates all outputs (images and logged stats) at the correct rates. |
|
public |
Methods drawBelow and drawOnTop allow you to draw extra stuff below and on top of the output from drawCanvas, respectively. |
|
public |
Method to draw the canvas. |
|
public |
Methods drawBelow and drawOnTop allow you to draw extra stuff below and on top of the output from drawCanvas, respectively. |
|
public |
Method to initialize the Grid should be implemented in each simulation. |
|
public |
logStats() Method to log statistics. |
|
public |
Listener for something that needs to be done after every monte carlo step. |
|
public |
run() Run the entire simulation. |
|
public |
Run the brunin period as defined by simsettings.BURNIN : run this number of MCS before the time of this simulation object starts ticking, and before we start drawing etc. |
|
public |
step() Run a montecarlostep, produce outputs if required, run any postMCSListener, and update the time. |
|
public |
Use this to pause or restart the simulation from an HTML page. |
Public Constructors
public constructor(config: object, simsettings: object) source
The constructor of class Simulation takes two arguments.
Params:
Name | Type | Attribute | Description |
config | object | overall configuration settings. This is an object with multiple entries, see below. |
|
config.field_size | GridSize | size of the CPM to build. |
|
config.constraints | Constraint[] | array of additional constraints to add to the CPM model. |
|
config.conf | object | configuration settings for the CPM; see its CPM#constructor for details. |
|
simsettings | object | configuration settings for the simulation itself and for controlling the outputs. See the parameters below for details. |
|
simsettings.NRCELLS | number[] | array with number of cells to seed for every non-background CellKind. |
|
simsettings.BURNIN | number | number of MCS to run before the actual simulation starts (let cells get their target volume before starting). |
|
simsettings.RUNTIME | number | number of MCS the simulation should run. Only necessary if you plan to use the run method. |
|
simsettings.IMGFRAMERATE | number |
|
draw the grid every [x] MCS. |
simsettings.LOGRATE | number |
|
log stats every [x] MCS. |
simsettings.LOGSTATS | object |
|
whether stats should be logged in browser and node. |
simsettings.SAVEIMG | boolean |
|
should images be saved? (node only). |
simsettings.SAVEPATH | string |
|
where should images be saved? You only have to give this argument when SAVEIMG = true. |
simsettings.EXPNAME | string |
|
string used to construct the filename of any saved image. |
simsettings.CANVASCOLOR | HexColor |
|
color to draw the background in; defaults to white. |
simsettings.CELLCOLOR | HexColor[] |
|
color to draw each non-background CellKind in. If left unspecified, the Canvas will use black. |
simsettings.ACTCOLOR | boolean[] |
|
should activities of the ActivityConstraint be drawn for each CellKind? If left unspecified, these are not drawn. |
simsettings.SHOWBORDERS | boolean[] |
|
should borders of each CellKind be drawn? Defaults to false. |
simsettings.BORDERCOL | HexColor[] |
|
color to draw cellborders of each CellKind in. Defaults to black. |
Public Members
public C: * source
public [m]: * source
Any function suplied in the custommethods argument to the constructor is bound to the object.
public constraints: * source
public custommethods: object source
Custom methods added to / overwriting the default Simulation class. These are stored so that the ArtistooImport can check them.
public helpClasses: object source
See if objects of class Canvas and GridManipulator already exist. These are added automatically when required. This will set their values in helpClasses to 'true', so they don't have to be added again.
public isSimulation: boolean source
To check from outside if an object is a Simulation; doing this with instanceof doesn't work in some cases. Any other object will not have this variable and return 'undefined', which in an if-statement equates to a 'false'. @type{boolean}
public mode: string source
See if code is run in browser or via node, which will be used below to determine what the output should be.
Private Members
private running: boolean source
Should the simulation be running? Change this to pause; see the toggleRunning method.
Public Methods
public addConstraints() source
Add additional constraints to the model before running; this method is automatically called and adds constraints given in the config object.
public createOutputs() source
This automatically creates all outputs (images and logged stats) at the correct rates. See the constructor documentation for options on how to control these outputs.
public drawBelow() source
Methods drawBelow and drawOnTop allow you to draw extra stuff below and on top of the output from drawCanvas, respectively. You can use them if you wish to visualize additional properties but don't want to remove the standard visualization. They are called at the beginning and end of drawCanvas, so they do not work if you overwrite this method.
public drawCanvas() source
Method to draw the canvas. The default method draws the canvas, cells, cellborders, and activityvalues as specified in the simsettings object (see the constructor for details).
This will be enough for most scenarios, but if you want to draw more complicated stuff, you can use the custommethods argument of the constructor to overwrite this with your own drawCanvas method.
public drawOnTop() source
Methods drawBelow and drawOnTop allow you to draw extra stuff below and on top of the output from drawCanvas, respectively. You can use them if you wish to visualize additional properties but don't want to remove the standard visualization. They are called at the beginning and end of drawCanvas, so they do not work if you overwrite this method.
public initializeGrid() source
Method to initialize the Grid should be implemented in each simulation. The default method checks in the simsettings.NRCELLS array how many cells to seed for each {@CellKind}, and does this (at random positions).
Often you'll want to do other things here. In that case you can use the custommethods argument of the constructor to overwrite this with your own initializeGrid method.
public logStats() source
Method to log statistics. The default method logs time, CellId, CellKind, and the {@ArrayCoordinate} of the cell's centroid to the console.
If you want to compute other stats (see subclasses of Stat for options) you can use the custommethods argument of the constructor to overwrite this with your own drawCanvas method.
public postMCSListener() source
Listener for something that needs to be done after every monte carlo step. This method is empty but can be overwritten via the custommethods argument of the constructor.
public run() source
Run the entire simulation. This function is meant for nodejs, as you'll want to perform individual steps in a requestAnimationFrame for an animation in a HTML page.
public runBurnin() source
Run the brunin period as defined by simsettings.BURNIN : run this number of MCS before the time of this simulation object starts ticking, and before we start drawing etc.
public step() source
Run a montecarlostep, produce outputs if required, run any postMCSListener, and update the time.