Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Persefone.jl
Manage
Activity
Members
Plan
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Package registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Repository analytics
Insights
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Persefone
Persefone.jl
Commits
855124ec
Commit
855124ec
authored
1 year ago
by
xo30xoqa
Browse files
Options
Downloads
Patches
Plain Diff
Rewrote initmodel() and stepsimulation!() functions
parent
d2a260f8
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/core/simulation.jl
+22
-27
22 additions, 27 deletions
src/core/simulation.jl
with
22 additions
and
27 deletions
src/core/simulation.jl
+
22
−
27
View file @
855124ec
...
@@ -24,7 +24,7 @@ mutable struct SimulationModel
...
@@ -24,7 +24,7 @@ mutable struct SimulationModel
crops
::
Dict
{
String
,
CropType
}
crops
::
Dict
{
String
,
CropType
}
farmers
::
Vector
{
Farmer
}
farmers
::
Vector
{
Farmer
}
farmplots
::
Vector
{
FarmPlot
}
farmplots
::
Vector
{
FarmPlot
}
animals
::
Vector
{
Animal
}
animals
::
Vector
{
Union
{
Animals
,
Nothing
}
}
migrants
::
Vector
{
Pair
{
Animal
,
Date
}}
migrants
::
Vector
{
Pair
{
Animal
,
Date
}}
events
::
Vector
{
FarmEvent
}
events
::
Vector
{
FarmEvent
}
end
end
...
@@ -85,7 +85,6 @@ a helper function for `initialise()`.
...
@@ -85,7 +85,6 @@ a helper function for `initialise()`.
"""
"""
function
initmodel
(
settings
::
Dict
{
String
,
Any
})
function
initmodel
(
settings
::
Dict
{
String
,
Any
})
#TODO catch exceptions and print them to the log file
#TODO catch exceptions and print them to the log file
#TODO remove Agents.jl-related code, reimplement this more cleanly (#72)
@debug
"Initialising model object."
@debug
"Initialising model object."
createdatadir
(
settings
[
"core.outdir"
],
settings
[
"core.overwrite"
])
createdatadir
(
settings
[
"core.outdir"
],
settings
[
"core.overwrite"
])
logger
=
modellogger
(
settings
[
"core.loglevel"
],
settings
[
"core.outdir"
])
logger
=
modellogger
(
settings
[
"core.loglevel"
],
settings
[
"core.outdir"
])
...
@@ -97,20 +96,20 @@ function initmodel(settings::Dict{String, Any})
...
@@ -97,20 +96,20 @@ function initmodel(settings::Dict{String, Any})
settings
[
"core.enddate"
])
settings
[
"core.enddate"
])
crops
=
readcropparameters
(
settings
[
"crop.cropfile"
],
crops
=
readcropparameters
(
settings
[
"crop.cropfile"
],
settings
[
"crop.growthfile"
])
settings
[
"crop.growthfile"
])
space
=
GridSpace
(
size
(
landscape
),
periodic
=
false
)
model
=
SimulationModel
(
settings
,
properties
=
Dict
{
Symbol
,
Any
}(
:
settings
=>
settings
,
StableRNG
(
settings
[
"core.seed"
])
,
:
logger
=>
logger
,
logger
,
:
date
=>
settings
[
"core.startdate"
]
,
Vector
{
DataOutput
}()
,
:
landscape
=>
landscape
,
Dict
{
String
,
DataFrame
}()
,
:
weather
=>
weather
,
settings
[
"core.startdate"
]
,
:
crops
=>
crops
,
landscape
,
:
migrants
=>
Vector
{
Pair
{
Animal
,
Date
}}()
,
weather
,
:
dataoutputs
=>
Vector
{
DataOutput
}()
,
crops
,
:
datatables
=>
Dict
{
String
,
DataFra
me
}(),
Vector
{
Far
me
r
}(),
:
events
=>
Vector
{
Farm
Even
t
}()
)
Vector
{
Farm
Plo
t
}()
,
model
=
AgentBasedModel
(
Union
{
Farmer
,
Animal
,
FarmPlot
}
,
Vector
{
Union
{
Animals
,
Nothing
}}()
,
space
,
properties
=
properties
,
Vector
{
Pair
{
Animal
,
Date
}}()
,
rng
=
StableRNG
(
settings
[
"core.seed"
]),
warn
=
false
)
Vector
{
FarmEvent
}()
)
saveinputfiles
(
model
)
saveinputfiles
(
model
)
initfields!
(
model
)
initfields!
(
model
)
initfarms!
(
model
)
initfarms!
(
model
)
...
@@ -156,19 +155,15 @@ function stepsimulation!(model::AgentBasedModel)
...
@@ -156,19 +155,15 @@ function stepsimulation!(model::AgentBasedModel)
#TODO catch exceptions and print them to the log file
#TODO catch exceptions and print them to the log file
with_logger
(
model
.
logger
)
do
with_logger
(
model
.
logger
)
do
@info
"Simulating day
$
(model.date)."
@info
"Simulating day
$
(model.date)."
#TODO remove Agents.jl-related code, reimplement this more cleanly (#72)
#XXX move the two loops into the relevant submodels?
for
a
in
Schedulers
.
ByType
((
Farmer
,
FarmPlot
,
Animal
),
true
)(
model
)
for
f
in
model
.
farmers
try
#The animal may have been killed
stepagent!
(
f
,
model
)
stepagent!
(
model
[
a
],
model
)
end
catch
exc
for
p
in
model
.
farmplots
# check if the KeyError comes from the `model[a]` or the function call
stepagent!
(
p
,
model
)
#FIXME this also silences KeyErrors caused in the species code (e.g.
# by accessing dead mates) - will be fixed once I reorganise the code
isa
(
exc
,
KeyError
)
&&
isa
(
exc
.
key
,
Int
)
?
continue
:
throw
(
exc
)
end
end
end
updateevents!
(
model
)
updatenature!
(
model
)
updatenature!
(
model
)
updateevents!
(
model
)
outputdata
(
model
)
outputdata
(
model
)
model
.
date
+=
Day
(
1
)
model
.
date
+=
Day
(
1
)
model
model
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment