Difference between revisions of "Fire model tutorial"

From Nova Documentation
Jump to: navigation, search
(Creating the cell submodel)
(Creating the cell submodel)
Line 19: Line 19:
 
Drag a '''Stock''' called <tt>tree</tt> onto the canvas. This Stock will assume one of these 4 values throughout the simulation. Now drag a '''Flow''' called next and attach its output to <tt>tree</tt>. Right-click on </tt>tree</tt> and check the Discrete checkbox. Next drag an '''DataInput''' pin called <tt>Init</tt> and an '''DataOutput''' pin called <tt>State</tt> attaching them to <tt>tree</tt> as shown.
 
Drag a '''Stock''' called <tt>tree</tt> onto the canvas. This Stock will assume one of these 4 values throughout the simulation. Now drag a '''Flow''' called next and attach its output to <tt>tree</tt>. Right-click on </tt>tree</tt> and check the Discrete checkbox. Next drag an '''DataInput''' pin called <tt>Init</tt> and an '''DataOutput''' pin called <tt>State</tt> attaching them to <tt>tree</tt> as shown.
  
Right click on tree and make its initial value equal to Input; similarly make State’s value equal to tree. It remains to program the logic that determines how tree’s state changes. This is complicated by the need to consult with the cell’s neighbors to determine if any are burning. Consequently, for each cell we will use a Term called neighbors to hold an array consisting of state objects from that cell’s immediate neighborhood. Fortunately a cell’s neighborhood doesn’t change, so we only need compute this array once, and so we can check the Property checkbox in neighbors<tt>Checking the Property checkbox in a Term it becomes a property; i.e., it computes its value only once. See [[Component Guide 1: Atomic Components | Terms]]</tt>. Add the Term neighbors and check its Property box. The list of neighbors can be created a single line of code:
+
Right click on <tt>tree</tt> and make its initial value equal to Input; similarly make State’s value equal to tree. It remains to program the logic that determines how <tt>tree</tt>’s state changes. This is complicated by the need to consult with the cell’s neighbors to determine if any are burning. Consequently, for each cell we will use a ''Term'' called neighbors to hold an array consisting of state objects from that cell’s immediate neighborhood. Fortunately a cell’s neighborhood doesn’t change, so we only need compute this array once, and so we can check the Property checkbox in neighbors<ref>Checking the Property checkbox in a ''Term'' it becomes a property; i.e., it computes its value only once. See [[Component Guide 1: Overview; Atomic Components#Term | Term]]</ref>. Add the ''Term'' neighbors and check its Property box. The list of neighbors can be created a single line of code:
 
  _.map(RING(1), function(coords){return CELL(coords);});
 
  _.map(RING(1), function(coords){return CELL(coords);});
  
 
;Notes
 
;Notes
 
<references>
 
<references>

Revision as of 19:03, 4 January 2015

Introduction

In this tutorial we will use the Firespread model found in the Model Library as our example, however our implementation will differ slightly from the one in the Model Library. Recall that this model views each cell either as a tree or firewall. Trees can be in one of 3 possible states: unburned, burning or burned. The firewall is represented as the fourth cell state. At each iteration each tree cell determines its next state based on the state of its 8 (Moore[1]) neighbors, and are summarized with the following rules:

  • If the current cell is unburned, determine if any of its neighbors are burning. If so, then the current cell will randomly either burn or remain unburned;
  • If the current cell is burning, it becomes burned;
  • If the current cell is burned, it remains burned
  • A cell that is part of the firewall remains unchanged.

This model has a main level and one submodel called Treecell. The latter will be the Capsule type used to populate the Forest CellMatrix. Create this submodel by clicking the New Sub Model button on the toolbar. Save the project as Firespread, so that the main model has that name.

Creating the cell submodel

Next we will develop the Treecell submodel, which represents a single tree. We’ll let the 4 possible cell states be represented by the integers 0 through 3. It is convenient to assign these values as constants so that we can refer to them in a meaningful way. In the Programming Window insert the following code:

const unburned = 0,
burning = 1,
burned = 2,
firewall = 3;
Error creating thumbnail: File missing
Treecell model

Drag a Stock called tree onto the canvas. This Stock will assume one of these 4 values throughout the simulation. Now drag a Flow called next and attach its output to tree. Right-click on </tt>tree</tt> and check the Discrete checkbox. Next drag an DataInput pin called Init and an DataOutput pin called State attaching them to tree as shown.

Right click on tree and make its initial value equal to Input; similarly make State’s value equal to tree. It remains to program the logic that determines how tree’s state changes. This is complicated by the need to consult with the cell’s neighbors to determine if any are burning. Consequently, for each cell we will use a Term called neighbors to hold an array consisting of state objects from that cell’s immediate neighborhood. Fortunately a cell’s neighborhood doesn’t change, so we only need compute this array once, and so we can check the Property checkbox in neighbors[2]. Add the Term neighbors and check its Property box. The list of neighbors can be created a single line of code:

_.map(RING(1), function(coords){return CELL(coords);});
Notes
  1. Moore neighborhoods consist of the 8 immediate neighbors of a cell in a cartesian lattice; the Von Neumann neighborhood only contains neighbors directly above, below, or to the left or right.
  2. Checking the Property checkbox in a Term it becomes a property; i.e., it computes its value only once. See Term