-
Notifications
You must be signed in to change notification settings - Fork 3
/
run_cmdl.jl
133 lines (93 loc) · 2.84 KB
/
run_cmdl.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
# Copyright (C) 2020 Martin Hinsch <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
include("setup.jl")
### run simulation with given setup and parameters
function run_events(sim, t_stop, logfile)
model = sim.model
t = 1.0
step = 1.0
last = 0
while t_stop <= 0 || t < t_stop
step_until!(sim, t) # run internal scheduler up to the next time step
# we want the analysis to happen at every integral time step
if (now = trunc(Int, t)) >= last
# in case we skipped a step (shouldn't happen, but just in case...)
for i in last:now
# print all stats to file
data = observe(Data, model, i)
log_results(logfile, data)
end
# remember when we did the last data output
last = now
end
t += step
# println(t)
end
end
function run_steps(sim, t_stop, logfile, ord)
model = sim.model
for t in 1:t_stop
update_model!(model, ord)
# print all stats to file
data = observe(Data, model, t)
log_results(logfile, data)
end
end
### setup, run, cleanup
## parameters
# parse command line args
using ArgParse
# translate params to args and vice versa
using Params2Args
const arg_settings = ArgParseSettings("run simulation", autofix_names=true)
@add_arg_table! arg_settings begin
"--rand-seed", "-r"
help = "random seed"
arg_type = Int
default = 42
"--stop-time", "-t"
help = "at which time to stop the simulation"
arg_type = Float64
default = 0.0
"--step-wise", "-s"
help = "run the model step-wise instead of event-based"
arg_type = Bool
default = false
"--shuffle"
help = "if running step-wise shuffle the population"
arg_type = Bool
default = false
end
# new group of arguments
add_arg_group!(arg_settings, "simulation parameters")
# translate Params into args
include("params.jl")
fields_as_args!(arg_settings, Params)
# parse cmdl args
const args = parse_args(arg_settings, as_symbols=true)
# and create a Params object from them
const p = @create_from_args(args, Params)
## setup
const sim = setup(p, args[:rand_seed])
const logf = prepare_outfiles("log_file.txt")
## run
if args[:step_wise]
@time run_steps(sim, trunc(Int, args[:stop_time]), logf, args[:shuffle])
else
init_events(sim)
@time run_events(sim, args[:stop_time], logf)
end
## cleanup
close(logf)