Skip to content

Commit 273d8f3

Browse files
author
Fabian Bernhard
committed
feat: Allow remake of IC_soil and IC_scalar
1 parent 5e94d1a commit 273d8f3

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

src/LWFBrook90.jl

+29-2
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,9 @@ function remakeSPAC(parametrizedSPAC::SPAC;
212212
# dump(kwargs) # kwargs contains NamedTuples to be modified
213213
modifiedSPAC = deepcopy(parametrizedSPAC)
214214
for curr_change in keys(kwargs)
215-
@assert values(kwargs)[curr_change] isa NamedTuple """
215+
@assert values(kwargs)[curr_change] isa NamedTuple ||
216+
# expect a NamedTuple of values to ovewrite, or in the case of IC_scalar a DataFrame
217+
(curr_change == :IC_scalar && values(kwargs)[curr_change] isa DataFrame) """
216218
Argument $curr_change is not a NamedTuple. Append single values with comma, e.g. (beta=12,)
217219
"""
218220
if (curr_change == :soil_toplayer)
@@ -223,6 +225,10 @@ function remakeSPAC(parametrizedSPAC::SPAC;
223225
modifiedSPAC = remake_params( modifiedSPAC, values(kwargs)[curr_change])
224226
elseif (curr_change == :root_distribution)
225227
modifiedSPAC = remake_root_distribution(modifiedSPAC, values(kwargs)[curr_change])
228+
elseif (curr_change == :IC_soil)
229+
modifiedSPAC = remake_IC_soil(modifiedSPAC, values(kwargs)[curr_change])
230+
elseif (curr_change == :IC_scalar)
231+
modifiedSPAC = remake_IC_scalar(modifiedSPAC, values(kwargs)[curr_change])
226232
else
227233
error("Unknown argument provided to remake(): $curr_change")
228234
end
@@ -263,7 +269,7 @@ function remake_root_distribution(spac, changesNT)
263269
for (key, val) in zip(keys(changesNT), changesNT)
264270
@assert key allowed_names "Unclear how to remake '$key' provided to params."
265271
end
266-
# create new params reusing the old no and only overwriting whats defined by changesNT
272+
# create new root_distribution reusing the old no and only overwriting whats defined by changesNT
267273
new_root_distribution = (;spac.pars.root_distribution..., changesNT...) # https://stackoverflow.com/a/60883705
268274
spac.pars = (;spac.pars..., root_distribution = new_root_distribution)
269275
return spac
@@ -278,6 +284,27 @@ function remake_params(spac, changesNT)
278284
spac.pars = (;spac.pars..., params = new_params)
279285
return spac
280286
end
287+
function remake_IC_soil(spac, changesNT)
288+
allowed_names = keys(spac.pars.IC_soil)
289+
for (key, val) in zip(keys(changesNT), changesNT)
290+
@assert key allowed_names "Unclear how to remake '$key' provided to params."
291+
end
292+
# create new IC_soil reusing the old no and only overwriting whats defined by changesNT
293+
new_IC_soil = (;spac.pars.IC_soil..., changesNT...) # https://stackoverflow.com/a/60883705
294+
spac.pars = (;spac.pars..., IC_soil = new_IC_soil)
295+
return spac
296+
end
297+
function remake_IC_scalar(spac, newICsoil_DF)
298+
@assert all(
299+
names(newICsoil_DF) .==
300+
["u_GWAT_init_mm", "u_INTS_init_mm", "u_INTR_init_mm", "u_SNOW_init_mm", "u_CC_init_MJ_per_m2", "u_SNOWLQ_init_mm"]) """
301+
Unexpected column names in remake_IC_scalar().
302+
"""
303+
# overwrite the entire DataFrame
304+
# create new pars with an overwritten IC_scalar
305+
spac.pars = (;spac.pars..., IC_scalar = newICsoil_DF)
306+
return spac
307+
end
281308

282309
"""
283310
function setup(parametrizedSPAC::SPAC;

test/01-unit-tests.jl

+26-1
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,32 @@ end
559559

560560
# Remake (modifies and (re)-discretizes)
561561

562-
# TODO: below tests for comprisons of Dicts are flawed... see: https://discourse.julialang.org/t/julian-way-of-comparing-two-dictionaries/5693/7
562+
# TEST CHANGES TO INITIAL CONDITIONS ################################################################
563+
ICscalar_tochange = copy(discrSPAC.parametrizedSPAC.pars.IC_scalar)
564+
ICscalar_tochange.u_INTR_init_mm = [1.3, -10, -90.]
565+
ICscalar_tochange.u_GWAT_init_mm = [10, -10, -90.]
566+
remSPAC_1 = remakeSPAC(discrSPAC,
567+
IC_soil = (PSIM_init_kPa = -3.0, delta18O_init_permil = -15.55, ),
568+
IC_scalar = ICscalar_tochange);
569+
# test parametrizedSPAC:
570+
@test remSPAC_1.parametrizedSPAC.pars.IC_scalar.u_INTR_init_mm == [1.3, -10, -90.]
571+
@test remSPAC_1.parametrizedSPAC.pars.IC_scalar.u_GWAT_init_mm == [10, -10, -90.]
572+
@test remSPAC_1.parametrizedSPAC.pars.IC_soil.PSIM_init_kPa == -3.0
573+
@test remSPAC_1.parametrizedSPAC.pars.IC_soil.delta18O_init_permil == -15.55
574+
# test ODEProblem:
575+
@test remSPAC_1.ODEProblem.u0.INTR.mm == 1.3
576+
@test remSPAC_1.ODEProblem.u0.INTR.d18O == -10
577+
@test remSPAC_1.ODEProblem.u0.INTR.d2H == -90
578+
@test remSPAC_1.ODEProblem.u0.GWAT.mm == 10
579+
@test remSPAC_1.ODEProblem.u0.GWAT.d18O == -10
580+
@test remSPAC_1.ODEProblem.u0.GWAT.d2H == -90
581+
(u_aux_WETNES, u_aux_PSIM, u_aux_PSITI, u_aux_θ, p_fu_KK) =
582+
LWFBrook90.KPT.derive_auxiliary_SOILVAR(
583+
remSPAC_1.ODEProblem.u0.SWATI.mm,
584+
remSPAC_1.ODEProblem.p.p_soil);
585+
@test all(u_aux_PSIM .≈ -3.0)
586+
@test all(remSPAC_1.ODEProblem.u0.SWATI.d18O .≈ -15.55)
587+
563588
# TEST CHANGES TO SOIL HYDRAULICS ################################################################
564589
remSPAC_1 = remakeSPAC(discrSPAC, soil_toplayer = (ths_ = 0.4,))
565590
# test parametrizedSPAC:

0 commit comments

Comments
 (0)