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
21adebe2
Commit
21adebe2
authored
2 years ago
by
xo30xoqa
Browse files
Options
Downloads
Patches
Plain Diff
Started rewriting the parameter system.
This is not yet in a working state.
parent
4f528d26
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/core/input.jl
+20
-41
20 additions, 41 deletions
src/core/input.jl
src/core/output.jl
+1
-1
1 addition, 1 deletion
src/core/output.jl
src/core/simulation.jl
+8
-6
8 additions, 6 deletions
src/core/simulation.jl
with
29 additions
and
48 deletions
src/core/input.jl
+
20
−
41
View file @
21adebe2
...
...
@@ -6,55 +6,32 @@
## Note: much of this code was adapted from the GeMM model by Leidinger et al.
## (https://github.com/CCTB-Ecomods/gemm/blob/master/src/input.jl)
##XXX Does it make sense to move the settings dict into the model object itself?
## (It could be integrated into the properties dict.) Advantage: we have less
## global state lying around, and serialising becomes easier (if we want to do
## that). Also, during testing we often need to change individual parameters,
## which is currently impossible. Disadvantage: immutability of parameters at
## runtime is intended, and some start-up functions may have problems if they need
## access to parameters before the model object has been created.
##TODO -> yes, changing this is probably sensible
let
settings
::
Dict
{
String
,
Dict
{
String
,
Any
}}
"""
initsettings(configfile)
Initialise the global model settings for this run.
"""
global
function
initsettings
(
configfile
::
String
)
settings
=
getsettings
(
configfile
)
end
"""
param(domainparam)
Return a configuration parameter from the global settings.
The argument should be in the form `"
<
domain
>.<
parameter
>
"`,
for example `param("
core
.
outdir
")`. Possible values for
<domain> are "
core
", "
nature
", "
farm
", or "
crop
". For a full
list of parameters, see `src/parameters.toml`.
"""
global
function
param
(
domainparam
::
String
)
domain
,
paramname
=
split
(
domainparam
,
"."
)
settings
[
domain
][
paramname
]
end
"""
@param(domainparam)
"""
printparameters(io)
Return a configuration parameter from the global settings.
The argument should be in the form `<domain>.<parameter>`,
for example `@param(core.outdir)`. Possible values for
<domain> are `core`, `nature`, `farm`, or `crop`. For a full
list of parameters, see `src/parameters.toml`.
Print all settings in TOML format to the given output stream.
"""
global
function
printparameters
(
stream
::
IO
=
stdout
)
TOML
.
print
(
stream
,
settings
)
end
Note that this macro only works in a context where the `model`
object is available.
"""
macro
param
(
domainparam
)
domain
=
String
(
domainparam
.
args
[
1
])
paramname
=
String
(
domainparam
.
args
[
2
]
.
value
)
#domain, paramname = split(domainparam, ".")
:
(
$
(
esc
(
:
model
))
.
settings
[
$
domain
][
$
paramname
])
end
"""
getsettings(configfile)
getsettings(configfile
, seed=nothing
)
Combines all configuration options to produce a single settings dict.
Precedence: commandline parameters - user config file - default values
"""
function
getsettings
(
configfile
::
String
)
function
getsettings
(
configfile
::
String
,
seed
::
Union
{
Int64
,
Nothing
)
=
nothing
)
# read in and merge configurations from the commandline, the default config file
# and a user-supplied config file
defaults
=
TOML
.
parsefile
(
configfile
)
...
...
@@ -75,7 +52,9 @@ function getsettings(configfile::String)
end
end
# pre-process certain parameters
if
settings
[
"core"
][
"seed"
]
==
0
if
!
isnothing
(
seed
)
settings
[
"core"
][
"seed"
]
=
seed
elseif
settings
[
"core"
][
"seed"
]
==
0
settings
[
"core"
][
"seed"
]
=
abs
(
rand
(
RandomDevice
(),
Int32
))
end
defaultoutdir
=
defaults
[
"core"
][
"outdir"
]
...
...
This diff is collapsed.
Click to expand it.
src/core/output.jl
+
1
−
1
View file @
21adebe2
...
...
@@ -51,7 +51,7 @@ function setupdatadir()
println
(
f
,
"# This file was generated automatically."
)
println
(
f
,
"# Simulation run on
$
(string(Dates.format(Dates.now(), "
d
u
Y
HH
:
MM
:
SS
"))),"
)
println
(
f
,
"# with git commit
$
(read(`git rev-parse HEAD`, String))#
\n
"
)
printparameters
(
f
)
TOML
.
print
(
f
,
settings
)
end
# Copy the map files to the output folder
lcmap
=
param
(
"core.landcovermap"
)
...
...
This diff is collapsed.
Click to expand it.
src/core/simulation.jl
+
8
−
6
View file @
21adebe2
...
...
@@ -4,23 +4,25 @@
###
"""
initialise(config, seed)
initialise(config, seed
=nothing
)
Initialise the model: read in parameters, create the output data directory,
and instantiate the AgentBasedModel object.
and instantiate the AgentBasedModel object. Optionally allows overriding the
`seed` parameter.
"""
function
initialise
(
config
::
String
=
PARAMFILE
)
function
initialise
(
config
::
String
=
PARAMFILE
,
seed
::
Union
{
Int64
,
Nothing
)
=
nothing
)
@info
"Simulation run started at
$
(Dates.now())."
#TODO add a seed parameter - requires mutable parameters
# do some housekeeping
initsettings
(
config
)
Random
.
seed!
(
param
(
"core
.
seed"
)
)
settings
=
initsettings
(
config
,
seed
)
Random
.
seed!
(
settings
[
"core
"
][
"
seed"
]
)
setupdatadir
()
# initialise world-level properties
landscape
=
initlandscape
()
events
=
Vector
{
FarmEvent
}()
space
=
GridSpace
(
size
(
landscape
),
periodic
=
false
)
properties
=
Dict
{
Symbol
,
Any
}(
:
date
=>
param
(
"core.startdate"
),
properties
=
Dict
{
Symbol
,
Any
}(
:
settings
=>
settings
,
:
date
=>
settings
[
"core"
][
"startdate"
],
:
landscape
=>
landscape
,
:
events
=>
events
)
@debug
"Setting up 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