Repository navigation
Expand file tree
/
Copy pathwater.html
More file actions
153 lines (141 loc) · 4.27 KB
/
Copy pathwater.html
File metadata and controls
153 lines (141 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<html>
<head>
<title>Shallow Water Model</title>
<script src="../lib/agentscript.js"></script>
<script src="../tools/coffee-script.js"></script>
<script src="../tools/dat.gui.min.js"></script>
<script src="../lib/as.dat.gui.js"></script>
<script type="text/coffeescript">
# This is a contributed model by Cody Smith (cody@redfish.com).
# It is a shallow water simulation with a few boats
# causing a disturbance in the water.
# This is a clear discription:
# http://freespace.virgin.net/hugo.elias/graphics/x_water.htm
# This is a MatLab discription from our friend Cleve Moler:
# http://www.mathworks.com/moler/exm/chapters/water.pdf
u = ABM.Util # ABM.Util alias
cmaps = ABM.ColorMaps
class MyModel extends ABM.Model
# GUI setters:
setOmega: (@omega) ->
setRoundWake: (@useRoundWake) ->
setSpeed: (@speed) ->
setGradientSize: (@gradientSize) ->
@colorMap = cmaps.gradientColorMap @gradientSize, ["blue","aqua"]
setRadius: (@radius) ->
# Keep a rect of our radius size for performance
@patches.cacheRect @radius # helps a bit, but not sure how much
resetPopulation: (@population) ->
change = @population - @turtles.length
if change > 0
@turtles.create change # start at origin
else
t.die() for t in @turtles.nOf(-change)
change # CS always returns something, this made some sense
setup: ->
# globals
@population = 4
@speed = 1
@omega = 1.95
@smoothing = 0.999
@useRoundWake = true
@setRadius 4
@setGradientSize 32
@anim.setRate 60 #, true
# defaults
@turtles.setDefault "size", 3
# only a couple of turtles, sprites may not be needed here
# @turtles.setDefault "useSprites", true
@turtles.create @population
for p in @patches
# Cleve Moler sets his initial value to 0.14223
# on his shallow water chapter
p.depA = 0.14223
p.depB = p.depA
step: ->
for p in @patches
nsum = 0
for nei in p.n4
nsum += nei.depA
depth = @omega * nsum/p.n4.length + (1-@omega)*p.depB
p.depB = depth * @smoothing
for p in @patches # swap buffers
tmp = p.depA
p.depA = p.depB
p.depB = tmp
for p in @patches
# 0-2 seems a good max/min range via trial/error
p.color = @colorMap.scaleColor p.depA, 0, 2
for a in @turtles
if Math.sqrt( a.x*a.x + a.y*a.y) > @world.maxX*0.9
a.rotate u.degToRad(120)
else
a.rotate u.randomCentered(u.degToRad(20))
a.forward @speed
if @useRoundWake
then ps = @patches.inRadius a.p, @radius
else ps = @patches.inRect a.p, @radius
p.depA = 0.9 for p in ps
null # return something to avoid CS returning array of results
model = new MyModel {
div: "layers",
size: 5,
minX: -60,
maxX: 60,
minY: -60,
maxY: 60,
isTorus: false
}
model.debug() # Debug: Put Model vars in global name space
model.start() # Run model immediately after startup initialization
gui = new ABM.DatGUI model,
"Population":
type: "slider"
min: 1
max: 10
step: 1
val: model.population
smooth: true
setter: "resetPopulation"
"Omega":
type: "slider"
min: 0
max: 2
step: 0.1
val: model.omega
smooth: true
setter: "setOmega"
"Speed":
type: "slider"
min: .2
max: 2
step: 0.2
val: model.speed
smooth: true
setter: "setSpeed"
"Gradient Colors":
type: "slider"
min: 8
max: 64
step: 2
val: model.gradientSize
smooth: true
setter: "setGradientSize"
"Wake Radius":
type: "slider"
min: 1
max: 10
step: 1
val: model.radius
smooth: true
setter: "setRadius"
"Round Wake":
type: "switch"
val: model.useRoundWake
setter: "setRoundWake"
</script>
</head>
<body>
<div id="layers"></div>
</body>
</html>