Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
CryoGrid.jl
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
CryoGrid
CryoGrid.jl
Commits
4c1ef9a0
Commit
4c1ef9a0
authored
4 months ago
by
Brian Groenke
Browse files
Options
Downloads
Patches
Plain Diff
Update parameter ensemble script
parent
dfc609de
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
examples/cglite_parameter_ensembles.jl
+13
-12
13 additions, 12 deletions
examples/cglite_parameter_ensembles.jl
with
13 additions
and
12 deletions
examples/cglite_parameter_ensembles.jl
+
13
−
12
View file @
4c1ef9a0
...
@@ -14,7 +14,9 @@ if Threads.nthreads() == 1
...
@@ -14,7 +14,9 @@ if Threads.nthreads() == 1
@warn
"Only one thread is available. Ensemble execution will run sequentially. Did you start julia with `--threads=auto` ?"
@warn
"Only one thread is available. Ensemble execution will run sequentially. Did you start julia with `--threads=auto` ?"
end
end
# Load forcings and build stratigraphy like before.
# Load forcings and build stratigraphy like before, except this time we assign
# `Param` values to the quantiies which we want to vary in the ensemble. Here
# we vary the porosity in each layer as well as the n-factors.
forcings
=
loadforcings
(
CryoGrid
.
Forcings
.
Samoylov_ERA_MkL3_CCSM4_long_term
);
forcings
=
loadforcings
(
CryoGrid
.
Forcings
.
Samoylov_ERA_MkL3_CCSM4_long_term
);
soilprofile
=
SoilProfile
(
soilprofile
=
SoilProfile
(
0.0
u
"m"
=>
SimpleSoil
(
por
=
Param
(
0.80
,
prior
=
Uniform
(
0.65
,
0.95
)),
sat
=
1.0
,
org
=
0.75
),
0.0
u
"m"
=>
SimpleSoil
(
por
=
Param
(
0.80
,
prior
=
Uniform
(
0.65
,
0.95
)),
sat
=
1.0
,
org
=
0.75
),
...
@@ -27,7 +29,7 @@ soilprofile = SoilProfile(
...
@@ -27,7 +29,7 @@ soilprofile = SoilProfile(
z_top
=
-
2.0
u
"m"
z_top
=
-
2.0
u
"m"
z_bot
=
1000.0
u
"m"
z_bot
=
1000.0
u
"m"
upperbc
=
TemperatureBC
(
upperbc
=
TemperatureBC
(
forcings
.
Tair
,
Input
(
:
Tair
)
,
NFactor
(
NFactor
(
nf
=
Param
(
0.5
,
prior
=
Beta
(
1
,
1
)),
nf
=
Param
(
0.5
,
prior
=
Beta
(
1
,
1
)),
nt
=
Param
(
0.9
,
prior
=
Beta
(
1
,
1
)),
nt
=
Param
(
0.9
,
prior
=
Beta
(
1
,
1
)),
...
@@ -43,27 +45,25 @@ strat = Stratigraphy(
...
@@ -43,27 +45,25 @@ strat = Stratigraphy(
z_bot
=>
Bottom
(
GeothermalHeatFlux
(
0.053
u
"W/m^2"
))
z_bot
=>
Bottom
(
GeothermalHeatFlux
(
0.053
u
"W/m^2"
))
);
);
modelgrid
=
CryoGrid
.
DefaultGrid_2cm
modelgrid
=
CryoGrid
.
DefaultGrid_2cm
tile
=
Tile
(
strat
,
modelgrid
,
ssinit
);
tile
=
Tile
(
strat
,
modelgrid
,
forcings
,
ssinit
);
# Since the solver can take daily timesteps, we can easily specify longer simulation time spans at minimal cost.
# Since the solver can take daily timesteps, we can easily specify longer simulation time spans at minimal cost.
# Here we specify a time span of 10 years.
# Here we specify a time span of 10 years.
tspan
=
(
DateTime
(
2000
,
1
,
1
),
DateTime
(
2010
,
12
,
31
))
tspan
=
(
DateTime
(
2000
,
1
,
1
),
DateTime
(
2010
,
12
,
31
))
u0
,
du0
=
initialcondition!
(
tile
,
tspan
);
u0
,
du0
=
initialcondition!
(
tile
,
tspan
);
prob
=
CryoGridProblem
(
tile
,
u0
,
tspan
,
saveat
=
24
*
3600.0
,
savevars
=
(
:
T
,))
prob
=
CryoGridProblem
(
tile
,
u0
,
tspan
,
saveat
=
24
*
3600.0
,
savevars
=
(
:
T
,))
# Here we retrieve the `CryoGridParams` from the `CryoGridProblem` constructed above.
# The CryoGridParams type behaves like a table and can be easily converted
# The CryoGridParams type behaves like a table and can be easily converted
# to a DataFrame with DataFrame(params) when DataFrames.jl is loaded.
# to a DataFrame with DataFrame(params) when DataFrames.jl is loaded.
params
=
CryoGrid
.
parameters
(
tile
)
params
=
prob
.
p
# you can use Julia's `vec` method to convert `CryoGridParams` into a `ComponentVector`
#
Note that
you can
also
use Julia's `vec` method to convert `CryoGridParams` into a `ComponentVector`
with labels.
p0
=
vec
(
params
)
p0
=
vec
(
params
)
# extract prior distributions and collect them into a multivariate Product distribution;
#
Here we
extract prior distributions and collect them into a multivariate Product distribution;
# note that this assumes each parameter to be independent from the others
# note that this assumes each parameter to be independent from the others
prior
=
Product
(
collect
(
params
[
:
prior
]))
prior
=
Product
(
collect
(
params
[
:
prior
]))
# declare
const
rng
=
Random
.
MersenneTwister
(
1234
)
# Method 1: SciML EnsembleProblem
# Method 1: SciML EnsembleProblem
function
make_prob_func
(
ensmeble
::
AbstractMatrix
)
function
make_prob_func
(
ensmeble
::
AbstractMatrix
)
...
@@ -77,11 +77,12 @@ function output_func(sol, i)
...
@@ -77,11 +77,12 @@ function output_func(sol, i)
return
CryoGridOutput
(
sol
),
false
return
CryoGridOutput
(
sol
),
false
end
end
# sample parameter values from prior with fixed RNG;
#
Now we
sample parameter values from prior with fixed RNG;
# the number of samples determines the size of the ensemble
# the number of samples determines the size of the ensemble
const
rng
=
Random
.
MersenneTwister
(
1234
)
prior_ensemble
=
rand
(
rng
,
prior
,
64
)
prior_ensemble
=
rand
(
rng
,
prior
,
64
)
# create EnsembleProblem from CryoGridProblem and prob/output functions;
#
We
create
an `
EnsembleProblem
`
from
`
CryoGridProblem
`
and prob/output functions;
# note that we use safetycopy=true because we're using multithreading;
# note that we use safetycopy=true because we're using multithreading;
# this prevents the different threads from using the same state caches
# this prevents the different threads from using the same state caches
prob_func
=
make_prob_func
(
prior_ensemble
)
prob_func
=
make_prob_func
(
prior_ensemble
)
...
@@ -90,7 +91,7 @@ ensprob = EnsembleProblem(prob; prob_func, output_func, safetycopy=true)
...
@@ -90,7 +91,7 @@ ensprob = EnsembleProblem(prob; prob_func, output_func, safetycopy=true)
# alternatively, one can specify EnsembleDistributed() for process or slurm parallelization or EnsembleSerial() for sequential execution.
# alternatively, one can specify EnsembleDistributed() for process or slurm parallelization or EnsembleSerial() for sequential execution.
enssol
=
@time
solve
(
ensprob
,
LiteImplicitEuler
(),
EnsembleThreads
(),
trajectories
=
size
(
prior_ensemble
,
2
))
enssol
=
@time
solve
(
ensprob
,
LiteImplicitEuler
(),
EnsembleThreads
(),
trajectories
=
size
(
prior_ensemble
,
2
))
# extract permafrost temperatures at 20m depth and plot the ensemble
#
Now we will
extract permafrost temperatures at 20m depth and plot the ensemble
.
T20m_ens
=
reduce
(
hcat
,
map
(
out
->
out
.
T
[
Z
(
Near
(
20.0
u
"m"
))],
enssol
))
T20m_ens
=
reduce
(
hcat
,
map
(
out
->
out
.
T
[
Z
(
Near
(
20.0
u
"m"
))],
enssol
))
Plots
.
plot
(
T20m_ens
,
leg
=
nothing
,
c
=:
black
,
alpha
=
0.5
,
ylabel
=
"Permafrost temperature"
)
Plots
.
plot
(
T20m_ens
,
leg
=
nothing
,
c
=:
black
,
alpha
=
0.5
,
ylabel
=
"Permafrost temperature"
)
...
...
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