On this page
First model: a spreading fire
Manual contents | Neighborhood reference | Tools and utilities | Project settings | Next: Wandering turtles
This tutorial assumes you can edit a small NetLogo model: you understand
variables, if, and a rule repeated each tick. You do not need previous
experience with JavaScript classes or the NL API.
We will build a deliberately simple model, not a realistic fire prediction:
- Every patch starts as a tree, except one burning patch in the center.
- A tree catches fire if any of its eight neighbors is burning.
- A burning patch becomes burnt on the next tick.
- A burnt patch stays burnt.
There are no moving turtles yet. The patches themselves hold the model state.
1. Find the three pieces
| Piece | What you put there | Rough NetLogo comparison |
|---|---|---|
| Project | World size, palette, inputs, displays, execution options | Interface and world settings |
| Main | Create populations; count and publish results | Observer procedures |
| Patch | The rule each patch follows | Code inside ask patches [...] |
class FirePatch extends NLPatch means "make our patch type using the supplied
patch behavior." this means the object currently executing the code.
this.main is that patch's route to the model-wide Main object.
super calls the supplied base behavior rather than replacing it.
In Designer or Studio:
- Download a backup of your current app if you want to keep it. New App replaces the current workspace.
- Choose New App, enter Fire, select Cellular Automaton, and choose OK. The editor opens; Turtle is not needed for this pattern.
- Replace the Project, Patch, and Main tab contents with the corresponding definitions below. Do not paste all three into one tab.
- Choose Save All, then Launch. Allow the app window to open if asked. The editor's Test command checks syntax only; use the observations below to check the model itself.
The code uses the module layout produced for an NL patch/agent app.
Main class Fire and Patch class FirePatch match the app name.
Keep the generated app shell/import wiring. Main and Patch below have default
exports (as do all Designer- or Studio-generated classes). Project is configuration file, and does not define a class.
Existing standalone apps sometimes use named exports instead; preserve their
entry-point imports if adapting one.
All applications now use the aligned lifecycle, described below. Keep initialization in the
runtime hooks (init() and setup), and do not call them from the constructors. If the editor
retains a Turtle source, it is unused: be sure that the Project agent section and
Main's initAgents() are not present.
Investigate the Project Manager by double-clicking the Project tab, or clicking the text on the tab bar when the Project is in the edit window. The Project Manager uses a form-based entry that facilitates the UI and parameter configuration. It will become handy later when you attempt more complex models. Note in particular that the cell tab presents a visual initialization canvas with which you can graphically specify the initial cell configuration.
See Using the Project Manager for the controls and a two-fire canvas walkthrough.
2. Configure the world
Use this Project definition:
const Project = {
version: 0.2,
name: "Fire",
title: "A spreading fire",
frameWidth: 1170,
frameHeight: 1000,
connect: true,
visitMode: "C",
dt: 1,
cell: {
cellSize: 12,
rowDim: 21,
colDim: 21,
boundary: "Edge",
palette: ["#228b22", "#ff4500", "#303030"],
start: [10, 10, 1]
},
displays: [
{
code: "BURNING",
site: "display_01",
type: "linechart",
dom: [0, 25],
ran: [0, 100],
width: 460,
height: 275,
legend: ["Burning patches"],
palette: ["#ff4500"]
}
]
};
- The aligned lifecycle governs initialization and reset.
"C"tells the runtime to execute patch rules only, not turtle rules.21rows and columns give us 441 patches. Coordinates run from (0, 0) (top-left) to (20, 20) (bottom-right)."Edge"means the fire does not jump from one edge to the opposite edge.- A patch's numeric
colorselects a palette entry:0is #228b22 (green),1is #ff4500 (orange),2is #303030 (dark gray). Here color doubles as the model's three-state variable. startcontains column/row/color triples.[10, 10, 1]makes the center patch burning; all other patches start at color zero.- The display entry defines a single linegraph to record the number of burning patchs at each point in time. The code
"BURNING"is the name Main will use to send its count. There are 2 rows of 4 display slots each on the display canvas, addressed asdisplay_01, …display_04,display_10, …display_14. (display_00is the double row slot in which the cell canvas is displayed.)display_01must be one of these displays slot in the generated app layout.
3. Give each patch its rule
Use this Patch source:
import {NLPatch} from "@nwk/api/nlpatch.js";
import {Enum} from "@nwk/api/tools.js";
const FireState = Enum(
{name: "Tree"},
{name: "Burning"},
{name: "Burnt"}
);
class FirePatch extends NLPatch {
setup() {
this.main.visitAlways(this);
}
patchStep() {
super.patchStep();
let nextState = this.color;
if (this.color === FireState.Tree.ord) {
let burningNearby = false;
this.neighborVisit(neighbor => {
if (neighbor.color === FireState.Burning.ord) {
burningNearby = true;
}
});
if (burningNearby) nextState = FireState.Burning.ord;
} else if (this.color === FireState.Burning.ord) {
nextState = FireState.Burnt.ord;
}
this.nextColor = nextState;
}
}
export default FirePatch;
Read it a little at a time
Enum is a Javascript class duplicating the functionality of the Java enum. It is a programming convenience that makes a program more readible, especially when returning to it after some time away. Each entry in an Enum list is a Javascript object, and the list itself determines its members' ordinality. (There is a second enum-like class, EnumSym, that creates an ordered list of symbols, useful, for example, for indexing.) Here, the
Enum definition gives our states names and numbers: Tree has .ord 0, Burning 1, and
Burnt 2, matching the palette. There is no corresponding NetLogo entity.
See Enum for more examples.
setup() runs when the world is ready and again after Reset. This is a consequence of the aligned life cycle.
NuWebKit gives you control over which patches are visited on each cycle, allowing you to optimize when it is possible to determine which patches definitely will not undergo a state change in the next cycle. For simplicity, this model visits each patch in the grid regardless of its proximity to a burning patch. this.main.visitAlways(this) puts this patch in the set that gets visited each tick.
Note that selecting "C" alone does not activate every patch. Also, repeating this registration
is safe because the runtime uses a Set: the same patch is not added twice. An alternate approach to asserting this.main.visitAlways(this) is to assert main.visitAll = true in Main.init. For this example, do not also enable visitAll.
neighbor => { ... } is a small function (a callback) intended to be applied to each of the current patch's neighbors. neighborVisit
calls it once for each neighboring position. In this square Edge world it sees
up to eight neighboring patches, (excluding the calling patch itself).
This is similar to examining NetLogo neighbors.
this.color reads the patch's current state. this.nextColor = ... proposes
its state for the end of the tick, for the next cycle. All patches therefore read the same old
world, and the fire travels only one neighborhood outward per tick. Since color is a synchronized store, the
runtime commits the proposals automatically; you need not call shift() yourself.
Do not replace the last line with this.color = nextState: that would change
state immediately and replace the Reset baseline. Likewise, currentColor
would change state immediately, making the fire spread depend on visit order.
super.patchStep() preserves the base step's behavior, which is currently empty. Retaining super calls allows for future migration where those calls are in fact active.
4. Create the patches and count the fire
Use this Main source:
import {NLSimMain} from "@nwk/api/nlsimmain.js";
class Fire extends NLSimMain {
constructor(Patch) {
super();
this.Patch = Patch;
}
initCells() {
this.initializePatches(this.Patch);
}
setup() {
this.publishBurning();
}
update() {
this.publishBurning();
}
publishBurning() {
const patches = this.patches.toList();
const burning = patches.filter(patch => patch.color === 1).length;
this.show("BURNING", burning);
}
}
export default Fire;
The runtime supplies the Patch class to Main's constructor. We save it as
this.Patch and create the world in initCells(), not in the constructor.
initializePatches creates the patches and applies Project's initial colors.
toList() gives us an ordinary JavaScript array of patches.
filter(patch => patch.color === 1) keeps only burning patches; .length
counts them. This is like NetLogo's count patches with [pcolor = ...].
show("BURNING", burning) publishes the number to the configured chart.
Main's setup() publishes the initial count after patch setup is finished.
update() publishes again after each tick has committed its state.
We do not need to override init() or reset() for this model.
5. Step, run, and reset
Launch the saved app and wait for connection. Click Step several times and check for these milestones:
| Time | What to see | Burning | Burnt |
|---|---|---|---|
| 0 | One orange patch in a green world | 1 | 0 |
| 1 | Dark center, orange ring around it | 8 | 1 |
| 2 | A larger orange square ring | 16 | 9 |
| 10 | The orange ring reaches the world's edges | 80 | 361 |
| 11 | The whole world is dark | 0 | 441 |
Now try Start and pause (you may have to lower the simulation speed). Reset should restore time zero and the one burning center patch without reloading the page. Step twice, reset again, and check that the first two counts are still 8 and 16. This simple model has no randomness, so these checks should be exact.
If nothing changes, check visitMode: "C", Main's initCells(), and the patch
setup() registration. If the whole forest burns in one step, check that the
rule writes nextColor rather than color or currentColor.
6. Understand the lifecycle without learning the plumbing
The aligned lifecycle defines the roles of constructor, init(), setup() and reset() in Main, Patch and Turtle classes.
| Place | Job in an aligned model | This tutorial |
|---|---|---|
| Constructor | Allocate/save stable objects; no manual init/setup calls | Main saves Patch class |
init() |
Initialize once after defaults/context exist | No custom work needed |
setup() |
Work requiring the completed world; repeat after reset | Activate patches; publish count |
reset() |
Restore the existing object's local state | Inherited patch reset restores color |
To summarize, the constructor does no initialization, neither does it call init(), which is called by the runtime.
setup() is called in each turtle only after all turtles have been allocated; similarly for setup() in patches. setup() in Main is called once all turtles and patches are allocated. reset() also calls all 3 setup() methods.
Do not call init() or setup() direcly in your code.
The browser Reset button invokes reset() in Main.
In a simple reset, Main and patch objects survive (however turtles, when present, are recreated).
If reinitializing your app requires a total rebuild, use main.resetWorld(), not super.reset() as your super call.
NetLogo's usual setup procedure often does several of these jobs together.
Here the runtime owns creation and reset order. You supply hooks for your work,
not calls to replay that order yourself.
7. Small experiments
Make one change at a time and predict the result before running:
- Two fires. Set
start: [7, 10, 1, 13, 10, 1]. (Alternatively, use the Initialization Canvas walkthrough under the Cell tab in the Project Manager to "set" your fires.) Where do the fronts meet? Reset restores both seeds. - Four neighbors. Replace the
neighborVisit(...)call withthis.main.nhood4(this).forEach(...), retaining the same callback body. The front becomes diamond-shaped. - Uncertain spread. Import
NLRootfrom@nwk/api/nlroot.jsin Patch and change the ignition line toif (burningNearby && NLRoot.random() < 0.6) nextState = FireState.Burning.ord;. This is one ignition chance per exposed tree per tick, not per burning neighbor. Some trees survive; repeated runs will likely differ. Now change boundary behavior from"Edge"to"Wrap". With"Edge"it stops at the boundaries; with"Wrap"it crosses to the opposite side. See the neighborhood reference.
Continue with Wandering turtles for movement, live sliders, and agent counts. Read Managing model state when a variable needs more than a color, and the aligned lifecycle contract when adding turtles, births, or relationships between objects.