Class: Gridmodel

Gridmodel(name, config, rng)

Gridmodel is the main (currently only) type of model in Cacatoo. Most of these models will look and feel like CAs, but GridModels can also contain ODEs with diffusion, making them more like PDEs.

Constructor

new Gridmodel(name, config, rng)

The constructor function for a @Gridmodel object. Takes the same config dictionary as used in @Simulation
Parameters:
Name Type Description
name string The name of your model. This is how it will be listed in @Simulation 's properties
config dictionary A dictionary (object) with all the necessary settings to setup a Cacatoo GridModel.
rng MersenneTwister A random number generator (MersenneTwister object)
Source:

Methods

apply_async()

Analogous to apply_sync(func), but asynchronous
Source:

apply_sync()

Like the synchronous function above, but can not take a custom user-defined function rather than the default next-state function. Technically one should be able to refarctor this by making the default function of synchronous "nextstate". But this works. :)
Source:

asynchronous()

Asynchronously apply the nextState function (defined by user) to the entire grid Asynchronous means that all grid points will be updated in a random order. For this first the update_order will be determined (this.set_update_order). Afterwards, the nextState will be applied in that order. This means that some cells may update while all their neighours are still un-updated, and other cells will update while all their neighbours are already done.
Source:

attachODE(eq, conf)

Attaches an ODE to all GPs in the model. Each gridpoint has it's own ODE.
Parameters:
Name Type Description
eq function Function that describes the ODEs, see examples starting with "ode"
conf Object dictionary style configuration of your ODEs (initial state, parameters, etc.)
Source:

colourGradient(property, n)

Initiate a gradient of colours for a property.
Parameters:
Name Type Description
property string The name of the property to which the colour is assigned
n int How many colours the gradient consists off For example usage, see colourViridis below
Source:

colourViridis(property, n, rev)

Initiate a gradient of colours for a property, using the Viridis colour scheme (purpleblue-ish to green to yellow)
Parameters:
Name Type Default Description
property string The name of the property to which the colour is assigned
n int How many colours the gradient consists off
rev bool false Reverse the viridis colour gradient
Source:

countMoore8(grid, col, row, val, property)

Count the number of grid points in the Moore (8) neighbourhood which have a certain value (val) for a certain property.
Parameters:
Name Type Description
grid GridModel The gridmodel used to check neighbours. Usually the gridmodel itself (i.e., this), but can be mixed to make grids interact.
col int position (column) for the focal gridpoint
row int position (row) for the focal gridpoint
val int what value should the GP have to be counted
property string the property that is counted For example, if one wants to count all the "cheater" surrounding a gridpoint in cheater.js, one needs to look for value '3' in the property 'species': this.countMoore8(this,10,10,3,'species');
Source:

countMoore9(grid, col, row, val, property)

Count the number of grid points in the Moore (9) neighbourhood which have a certain value (val) for a certain property.
Parameters:
Name Type Description
grid GridModel The gridmodel used to check neighbours. Usually the gridmodel itself (i.e., this), but can be mixed to make grids interact.
col int position (column) for the focal gridpoint
row int position (row) for the focal gridpoint
val int what value should the GP have to be counted
property string the property that is counted For example, if one wants to count all the "cheater" surrounding a gridpoint in cheater.js, one needs to look for value '3' in the property 'species': this.countMoore9(this,10,10,3,'species');
Source:

diffuseODEstates()

Diffuse ODE states on the grid. Because ODEs are stored by reference inside gridpoint, the states of the ODEs have to be first stored (copied) into a 4D array (x,y,ODE,state-vector), which is then used to update the grid.
Source:

getGridpoint(i, j)

Get the gridpoint at coordinates i,j Makes sure wrapping is applied if necessary
Parameters:
Name Type Description
i int position (column) for the focal gridpoint
j int position (row) for the focal gridpoint
Source:

getNeighXY()

Get the x,y coordinates of a neighbour in an array. Makes sure wrapping is applied if necessary
Source:

getPopsizes(property, values)

Returns an array with the population sizes of different types
Parameters:
Name Type Description
property String Return popsizes for this property (needs to exist in your model, e.g. "species" or "alive")
values Array Which values are counted and returned (e.g. [1,3,4,6])
Source:

MargolusDiffusion()

Apply diffusion algorithm for grid-based models described in Toffoli & Margolus' book "Cellular automata machines" The idea is to subdivide the grid into 2x2 neighbourhoods, and rotate them (randomly CW or CCW). To avoid particles simply being stuck in their own 2x2 subspace, different 2x2 subspaces are taken each iteration (CW in even iterations, CCW in odd iterations)
Source:

nextState(i, j)

The most important function in GridModel: how to determine the next state of a gridpoint? By default, nextState is empty. It should be defined by the user (see examples)
Parameters:
Name Type Description
i int Position of grid point to update (column)
j int Position of grid point to update (row)
Source:

perfectMix()

Assign each gridpoint a new random position on the grid. This simulated mixing, but does not guarantee a "well-mixed" system per se (interactions are still) calculated based on neighbourhoods.
Source:

plotArray(graph_labels, graph_values, cols, title, opts)

Adds a dygraph-plot to your DOM (if the DOM is loaded)
Parameters:
Name Type Description
graph_labels Array Array of strings for the graph legend
graph_values Array Array of floats to plot (here plotted over time)
cols Array Array of colours to use for plotting
title String Title of the plot
opts Object dictionary-style list of opts to pass onto dygraphs
Source:

plotPopsizes(property, values)

Easy function to add a pop-sizes plot (wrapper for plotArrays)
Parameters:
Name Type Description
property String What property to plot (needs to exist in your model, e.g. "species" or "alive")
values Array Which values are plotted (e.g. [1,3,4,6])
Source:

plotXY(graph_labels, graph_values, cols, title, opts)

Adds a dygraph-plot to your DOM (if the DOM is loaded)
Parameters:
Name Type Description
graph_labels Array Array of strings for the graph legend
graph_values Array Array of 2 floats to plot (first value for x-axis, second value for y-axis)
cols Array Array of colours to use for plotting
title String Title of the plot
opts Object dictionary-style list of opts to pass onto dygraphs
Source:

printGrid(property, fract)

Print the entire grid to the console. Not always recommended, but useful for debugging :D
Parameters:
Name Type Description
property float What property is printed
fract float Subset to be printed (from the top-left)
Source:

printgrid()

Print the entire grid to the console
Source:

randomMoore8(grid, col, row)

Return a random neighbour from the Moore (8) neighbourhood
Parameters:
Name Type Description
grid GridModel The gridmodel used to check neighbours. Usually the gridmodel itself (i.e., this), but can be mixed to make grids interact.
col int position (column) for the focal gridpoint
row int position (row) for the focal gridpoint
Source:

randomMoore9(grid, col, row)

Return a random neighbour from the Moore (9) neighbourhood (self inclusive)
Parameters:
Name Type Description
grid GridModel The gridmodel used to check neighbours. Usually the gridmodel itself (i.e., this), but can be mixed to make grids interact.
col int position (column) for the focal gridpoint
row int position (row) for the focal gridpoint
Source:

set_update_order()

If called for the first time, make an update order (list of ints), otherwise just shuffle it.
Source:

setGridpoint(i, j, @Gridpoint)

Change the gridpoint at position i,j into gp Makes sure wrapping is applied if necessary
Parameters:
Name Type Description
i int position (column) for the focal gridpoint
j int position (row) for the focal gridpoint
@Gridpoint Gridpoint object to set the gp to
Source:

setupColours(object)

Initiate a dictionary with colour arrays [R,G,B] used by Graph and Canvas classes
Parameters:
Name Type Description
object statecols given object can be in two forms | either {state:colour} tuple (e.g. 'alive':'white', see gol.html) | or {state:object} where objects are {val:'colour}, | e.g. {'species':{0:"black", 1:"#DDDDDD", 2:"red"}}, see cheater.html
Source:

solve_all_odes(delta_t, opt_pos)

Numerically solve the ODEs for each grid point
Parameters:
Name Type Default Description
delta_t float 0.1 Step size
opt_pos bool false When enabled, negative values are set to 0 automatically
Source:

synchronous()

Synchronously apply the nextState function (defined by user) to the entire grid Synchronous means that all grid points will be updated simultaneously. This is ensured by making a back-up grid, which will serve as a reference to know the state in the previous time step. First all grid points are updated based on the back-up. Only then will the actual grid be changed.
Source:

update()

The update is, like nextState, user-defined (hence, empty by default). It should contains all functions that one wants to apply every time step (e.g. grid manipulations and printing statistics) For example, and update function could look like: this.synchronous() // Update all cells this.MargolusDiffusion() // Apply Toffoli Margolus diffusion algorithm this.plotPopsizes('species',[1,2,3]) // Plot the population sizes
Source: