-
Notifications
You must be signed in to change notification settings - Fork 4
/
helperfunctions.jl
410 lines (327 loc) · 11 KB
/
helperfunctions.jl
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#####################
#####################
"""
$(TYPEDSIGNATURES)
"""
@inline function _set_pos!(agent::Agent3D{<:Float64}, xdim, ydim, zdim)
setfield!(agent, :pos, Vect(rand()*xdim, rand()*ydim, rand()*zdim) )
end
"""
$(TYPEDSIGNATURES)
"""
@inline function _set_pos!(agent::Agent3D{Int}, xdim, ydim, zdim)
setfield!(agent, :pos, Vect(rand(1:xdim), rand(1:ydim), rand(1:zdim)))
end
"""
$(TYPEDSIGNATURES)
"""
@inline function _setup_grid!(agent::Agent3D{S, P, T}, model::SpaceModel3D{T,S,P}, i, xdim, ydim, zdim) where {T<:MType,S<:Float64,P<:PeriodicType}
patches = model.patches
x,y,z = agent.pos
a = mod1(x, xdim)
b = mod1(y, ydim)
c = mod1(z, zdim)
setfield!(agent, :pos, Vect(a,b,c))
a,b,c = Int(ceil(a)), Int(ceil(b)), Int(ceil(c))
push!(patches[a,b,c].agents, i)
setfield!(agent, :last_grid_loc, (a,b,c))
end
"""
$(TYPEDSIGNATURES)
"""
@inline function _setup_grid!(agent::Agent3D{S, P, T}, model::SpaceModel3D{T,S,P}, i, xdim, ydim, zdim) where {T<:MType,S<:Float64,P<:NPeriodicType}
patches = model.patches
x,y,z = agent.pos
if (x>0 && x<=xdim && y>0 && y<=ydim && z>0 && z<=zdim)
a,b,c = Int(ceil(x)), Int(ceil(y)), Int(ceil(z))
push!(patches[a,b,c].agents, i)
setfield!(agent, :last_grid_loc, (a,b,c))
else
p = S(eps())
setfield!(agent, :pos, Vect(p,p,p))
push!(patches[1,1,1].agents, i)
setfield!(agent, :last_grid_loc, (1,1,1))
end
end
"""
$(TYPEDSIGNATURES)
"""
@inline function _setup_grid!(agent::Agent3D{S, P, T}, model::SpaceModel3D{T,S,P}, i, xdim, ydim, zdim) where {T<:MType,S<:Int,P<:PeriodicType}
patches = model.patches
x,y,z = agent.pos
a = mod1(x, xdim)
b = mod1(y, ydim)
c = mod1(z, zdim)
setfield!(agent, :pos, Vect(a,b,c))
push!(patches[a,b,c].agents, i)
end
"""
$(TYPEDSIGNATURES)
"""
@inline function _setup_grid!(agent::Agent3D{S, P, T}, model::SpaceModel3D{T,S,P}, i, xdim, ydim, zdim) where {T<:MType,S<:Int,P<:NPeriodicType}
patches = model.patches
x,y,z = agent.pos
if (x>0 && x<=xdim && y>0 && y<=ydim && z>0 && z<=zdim)
push!(patches[x,y,z].agents, i)
else
setfield!(agent, :pos, Vect(1,1,1))
push!(patches[1,1,1].agents, i)
end
end
"""
$(TYPEDSIGNATURES)
"""
@inline function _set_patches3d(grid_size)
xdim, ydim, zdim = grid_size
patches = [ContainerDataDict(Dict{Symbol, Any}(:color => Col(1,1,1,0.1))) for i in 1:xdim, j in 1:ydim, k in 1:zdim]
return patches
end
"""
$(TYPEDSIGNATURES)
"""
@inline function _set_parameters3d(grid_size, n, random_positions; kwargs...)
xdim, ydim, zdim = grid_size
dict_parameters = Dict{Symbol, Any}(kwargs)
properties = PropDataDict(dict_parameters)
properties._extras._random_positions = random_positions
properties._extras._show_space = true
properties._extras._num_agents = n # number of active agents
properties._extras._len_model_agents = n #number of agents in model.agents
properties._extras._num_patches = xdim*ydim*zdim
return properties
end
"""
$(TYPEDSIGNATURES)
This function is for use from within the module and is not exported.
It takes an agent as first argument, and if `graphics` is true, some
graphics related properties are added to the agent if not already defined.
"""
@inline function manage_default_graphics_data!(agent::Agent3D, graphics, size)
if graphics
if !haskey(agent, :shape)
agent.shape = :sphere
end
if !haskey(agent, :color)
agent.color = Col("red")
end
if !haskey(agent, :size)
agent.size = 0.25 # absolute size like position from 0 to xdim ...
end
if !haskey(agent, :orientation)
agent.orientation = Vect(0.0,0.0,1.0)
end
end
end
"""
$(TYPEDSIGNATURES)
Adds the agent to the model.
"""
function add_agent!(agent, model::SpaceModel3D{MortalType})
if (agent._extras._active::Bool)&&(agent._extras._new::Bool)
_manage_default_data!(agent, model)
manage_default_graphics_data!(agent, model.graphics, model.size)
xdim = model.size[1]
ydim = model.size[2]
zdim = model.size[3]
_setup_grid!(agent, model, getfield(agent, :id), xdim, ydim, zdim)
setfield!(agent, :model, model)
_create_agent_record!(agent, model)
_init_agent_record!(agent)
getfield(model,:max_id)[] += 1
model.properties._extras._num_agents::Int += 1
end
end
"""
$(TYPEDSIGNATURES)
This function is for use from within the module and is not exported.
"""
@inline function update_agents_record!(model::SpaceModel3D)
@threads for agent in model.agents
_update_agent_record!(agent)
end
end
"""
$(TYPEDSIGNATURES)
This function is for use from within the module and is not exported.
"""
@inline function update_patches_record!(model::SpaceModel3D)
if length(model.record.pprops)>0
@threads for k in 1:model.size[3]
@threads for j in 1:model.size[2]
@threads for i in 1:model.size[1]
patch_dict = unwrap(model.patches[i, j, k])
patch_data = unwrap_data(model.patches[i,j, k])
for key in model.record.pprops
push!(patch_data[key]::Vector, patch_dict[key])
end
end
end
end
end
end
"""
$(TYPEDSIGNATURES)
This function is for use from within the module and is not exported.
"""
@inline function do_after_model_step!(model::SpaceModel3D{MortalType})
_permanently_remove_inactive_agents!(model)
commit_add_agents!(model)
update_agents_record!(model)
update_patches_record!(model)
_update_model_record!(model)
getfield(model, :tick)[] += 1
end
"""
$(TYPEDSIGNATURES)
This function is for use from within the module and is not exported.
"""
@inline function do_after_model_step!(model::SpaceModel3D{StaticType})
update_agents_record!(model)
update_patches_record!(model)
_update_model_record!(model)
getfield(model, :tick)[] += 1
end
#################
"""
$(TYPEDSIGNATURES)
"""
@inline function _draw_agents_interact_frame(vis, model::SpaceModel3D{MortalType}, frame, scl)
xlen = gparams3d.xlen+0.0
ylen = gparams3d.ylen+0.0
zlen = gparams3d.zlen+0.0
xdim = model.size[1]
ydim = model.size[2]
zdim = model.size[3]
w = xlen/xdim
l = ylen/ydim
h = zlen/zdim
all_agents = vcat(model.agents, model.agents_killed)
for agent in all_agents
if (agent._extras._birth_time::Int<= frame)&&(frame<= agent._extras._death_time::Int)
draw_agent_interact_frame(vis, agent, model, frame- agent._extras._birth_time::Int+1, scl, w, l, h)
end
end
end
@inline function _draw_agents_interact_frame(vis, model::SpaceModel3D{StaticType}, frame, scl)
xlen = gparams3d.xlen+0.0
ylen = gparams3d.ylen+0.0
zlen = gparams3d.zlen+0.0
xdim = model.size[1]
ydim = model.size[2]
zdim = model.size[3]
w = xlen/xdim
l = ylen/ydim
h = zlen/zdim
for agent in model.agents
draw_agent_interact_frame(vis, agent, model, frame, scl, w, l, h)
end
end
"""
$(TYPEDSIGNATURES)
"""
@inline function _if_alive_draw_agent(vis, agent, model, frame, scl, tail_length::Int, tail_condition::Function, w, l, h)
if (agent._extras._birth_time<= frame)&&(frame<= agent._extras._death_time)
draw_agent(vis, agent, model, frame - agent._extras._birth_time::Int +1, scl, tail_length, tail_condition, w, l, h)
else
#if haskey(agent._extras, :_shapes) && haskey(agent._extras, :_colors) # this will be true if all agents are drawn initially which is true for animate_sim and create_interactive_app
clrs = agent._extras._colors::Vector{Col}
shps = agent._extras._shapes::Vector{Symbol}
for sh in shps
for cl in clrs
setvisible!(vis["agents"]["$(getfield(agent, :id))"*string(sh)][string(cl)], false)
end
end
if tail_condition(agent)
for i in 1:tail_length
setvisible!(vis["tails"]["$(getfield(agent, :id))"]["$i"], false)
end
end
#end
end
end
"""
$(TYPEDSIGNATURES)
"""
@inline function draw_agents_and_patches(vis, model::SpaceModel3D{MortalType}, frame, scl::Number=1.0, tail_length = 1, tail_condition = agent->false)
show_patches = model.properties._extras._show_space
xlen = gparams3d.xlen+0.0
ylen = gparams3d.ylen+0.0
zlen = gparams3d.zlen+0.0
xdim = model.size[1]
ydim = model.size[2]
zdim = model.size[3]
w = xlen/xdim
l = ylen/ydim
h = zlen/zdim
if show_patches
if :color in model.record.pprops
draw_patches(vis, model, frame)
end
end
all_agents = vcat(model.agents, model.agents_killed)
for agent in all_agents
_if_alive_draw_agent(vis, agent, model, frame, scl, tail_length, tail_condition, w, l, h)
end
end
"""
$(TYPEDSIGNATURES)
"""
@inline function draw_agents_and_patches(vis, model::SpaceModel3D{StaticType}, frame, scl::Number=1.0, tail_length = 1, tail_condition = agent->false)
show_patches = model.properties._extras._show_space::Bool
xlen = gparams3d.xlen+0.0
ylen = gparams3d.ylen+0.0
zlen = gparams3d.zlen+0.0
xdim = model.size[1]
ydim = model.size[2]
zdim = model.size[3]
w = xlen/xdim
l = ylen/ydim
h = zlen/zdim
if show_patches
if :color in model.record.pprops
draw_patches(vis, model, frame)
end
end
for agent in model.agents
draw_agent(vis, agent, model, frame, scl, tail_length, tail_condition, w, l, h)
end
end
#########################
#Thebes
#########################
#################
# """
# $(TYPEDSIGNATURES)
# """
# @inline function draw_agents_and_patches(model::SpaceModel3D{MortalType}, frame, scl, ep, tail_length = 1, tail_condition = agent-> false)
# # eyepoint(Point3D(ep.xe,ep.ye,ep.ze))
# # perspective(ep.zoom)
# show_patches = model.properties._extras._show_space::Bool
# if show_patches
# if :color in model.record.pprops
# draw_patches(model, frame)
# end
# end
# all_agents = vcat(model.agents, model.agents_killed)
# @sync for agent in all_agents
# if (agent._extras._birth_time::Int <= frame)&&(frame<= agent._extras._death_time::Int)
# @async draw_agent(agent, model, scl, frame - agent._extras._birth_time::Int +1, tail_length, tail_condition)
# end
# end
# end
# """
# $(TYPEDSIGNATURES)
# """
# @inline function draw_agents_and_patches(model::SpaceModel3D{StaticType}, frame, scl, ep, tail_length = 1, tail_condition = agent-> false)
# # eyepoint(Point3D(ep.xe,ep.ye,ep.ze))
# # perspective(ep.zoom)
# show_patches = model.properties._extras._show_space::Bool
# if show_patches
# if :color in model.record.pprops
# draw_patches(model, frame)
# end
# end
# @sync for agent in model.agents
# @async draw_agent(agent, model, scl, frame, tail_length, tail_condition)
# end
# end