Skip to content

Commit

Permalink
Merge pull request #575 from CliMA/kd/cleanup_set_initial_cache_methods
Browse files Browse the repository at this point in the history
initial cache method removal
  • Loading branch information
kmdeck authored May 9, 2024
2 parents 017809a + eba150e commit f36042f
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 144 deletions.
27 changes: 0 additions & 27 deletions src/ClimaLand.jl
Original file line number Diff line number Diff line change
Expand Up @@ -197,33 +197,6 @@ function make_update_boundary_fluxes(land::AbstractLandModel)
return update_boundary_fluxes!
end

"""
make_set_initial_cache(land::AbstractLandModel)
Creates and returns the function which sets the initial cache
with the correct values given the initial conditions Y0 and initial
time t0.
Note that this will call update_drivers! multiple times, once
per component model.
"""
function make_set_initial_cache(land::AbstractLandModel)
components = land_components(land)
# These functions also call update_aux
(atmos, radiation) = get_drivers(land)
update_drivers! = make_update_drivers(atmos, radiation)
set_initial_cache_function_list =
map(x -> make_set_initial_cache(getproperty(land, x)), components)

function set_initial_cache!(p, Y0, t0)
update_drivers!(p, t0)
for f! in set_initial_cache_function_list
f!(p, Y0, t0)
end
end
return set_initial_cache!
end

"""
land_components(land::AbstractLandModel)
Expand Down
17 changes: 0 additions & 17 deletions src/standalone/Bucket/Bucket.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ import ClimaLand:
initialize_vars,
initialize,
initialize_auxiliary,
make_set_initial_cache,
surface_temperature,
surface_air_density,
surface_evaporative_scaling,
Expand Down Expand Up @@ -318,22 +317,6 @@ auxiliary_vars(::BucketModel) =
auxiliary_domain_names(::BucketModel) =
(:surface, :surface, :surface, :surface, :surface, :surface, :surface)

"""
ClimaLand.make_set_initial_cache(model::BucketModel{FT}) where{FT}
Returns the set_initial_cache! function, which updates the auxiliary
state `p` in place with the initial values corresponding to Y(t=t0) = Y0.
In this case, we also use this method to update the initial values for the
spatially varying parameter fields, read in from data files.
"""
function ClimaLand.make_set_initial_cache(model::BucketModel)
update_cache! = make_update_cache(model)
function set_initial_cache!(p, Y0, t0)
update_cache!(p, Y0, t0)
end
return set_initial_cache!
end

"""
make_compute_exp_tendency(model::BucketModel{FT}) where {FT}
Expand Down
25 changes: 2 additions & 23 deletions src/standalone/Vegetation/Canopy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@ import ClimaLand:
make_update_boundary_fluxes,
make_update_aux,
make_compute_exp_tendency,
make_set_initial_cache,
get_drivers

using ClimaLand.Domains: Point, Plane, SphericalSurface
export SharedCanopyParameters,
CanopyModel, set_canopy_prescribed_field!, update_canopy_prescribed_field!
export SharedCanopyParameters, CanopyModel, set_canopy_prescribed_field!
include("./component_models.jl")
include("./soil_drivers.jl")
include("./PlantHydraulics.jl")
Expand Down Expand Up @@ -358,25 +356,6 @@ function initialize_auxiliary(model::CanopyModel{FT}, coords) where {FT}
return p
end

"""
ClimaLand.make_set_initial_cache(model::CanopyModel)
Returns the set_initial_cache! function, which updates the auxiliary
state `p` in place with the initial values corresponding to Y(t=t0) = Y0.
In this case, we also use this method to update the initial values for the
spatially and temporally varying canopy parameter fields,
read in from data files or otherwise prescribed.
"""
function ClimaLand.make_set_initial_cache(model::CanopyModel)
update_cache! = make_update_cache(model)
function set_initial_cache!(p, Y0, t0)
set_canopy_prescribed_field!(model.hydraulics, p, t0)
update_cache!(p, Y0, t0)
end
return set_initial_cache!
end

"""
ClimaLand.make_update_aux(canopy::CanopyModel{FT,
<:AutotrophicRespirationModel,
Expand Down Expand Up @@ -416,7 +395,7 @@ function ClimaLand.make_update_aux(
# Update the prescribed fields to the current time `t`,
# prior to updating the rest of the auxiliary state to
# the current time, as they depend on prescribed fields.
update_canopy_prescribed_field!(canopy.hydraulics, p, t)
set_canopy_prescribed_field!(canopy.hydraulics, p, t)

# Other auxiliary variables being updated:
Ra = p.canopy.autotrophic_respiration.Ra
Expand Down
29 changes: 4 additions & 25 deletions src/standalone/Vegetation/PlantHydraulics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import ClimaUtilities.TimeVaryingInputs:
import NCDatasets, ClimaCore, Interpolations # Needed to load TimeVaryingInputs
using ..ClimaLand.Canopy:
AbstractCanopyComponent,
update_canopy_prescribed_field!,
set_canopy_prescribed_field!,
AbstractSoilDriver,
PrescribedSoil
Expand Down Expand Up @@ -309,45 +308,25 @@ ClimaLand.auxiliary_domain_names(::PlantHydraulicsModel) =
"""
set_canopy_prescribed_field!(component::PlantHydraulics{FT},
p,
t0,
t,
) where {FT}
Sets the canopy prescribed fields pertaining to the PlantHydraulics
component (the area indices) with their initial values at time t0.
component (the area indices) with their values at time t.
"""
function ClimaLand.Canopy.set_canopy_prescribed_field!(
component::PlantHydraulicsModel{FT},
p,
t0,
t,
) where {FT}
(; LAIfunction, SAI, RAI) = component.parameters.ai_parameterization
evaluate!(p.canopy.hydraulics.area_index.leaf, LAIfunction, t0)

evaluate!(p.canopy.hydraulics.area_index.leaf, LAIfunction, t)
@. p.canopy.hydraulics.area_index.stem = SAI
@. p.canopy.hydraulics.area_index.root = RAI
end


"""
update_canopy_prescribed_field!(component::PlantHydraulics{FT},
p,
t,
) where {FT}
Updates the canopy prescribed fields pertaining to the PlantHydraulics
component (the LAI only in this case) with their values at time t.
"""
function ClimaLand.Canopy.update_canopy_prescribed_field!(
component::PlantHydraulicsModel{FT},
p,
t,
) where {FT}
(; LAIfunction) = component.parameters.ai_parameterization
evaluate!(p.canopy.hydraulics.area_index.leaf, LAIfunction, t)
end


"""
flux(
z1,
Expand Down
42 changes: 6 additions & 36 deletions src/standalone/Vegetation/component_models.jl
Original file line number Diff line number Diff line change
Expand Up @@ -132,45 +132,15 @@ end
"""
set_canopy_prescribed_field!(component::AbstractCanopyComponent,
p,
t0,
t,
) end
Sets the spatially and temporally varying prescribed fields of the `component`
with their initial values.
These fields are stored in the aux-state and *should not* depend on the prognostic
state `Y` or other diagnostic variables stored in `p`; this allows them
to be updated first, prior to updating the rest of the aux state and prognostic state.
However, there is no
guarantee on the order of operations in terms of when diagnostic auxiliary
variables are updated vs. prescribed field auxiliary variables; any required
order of operations must be enforced by the developer who writes the update_aux
function.
"""
function set_canopy_prescribed_field!(component::AbstractCanopyComponent, p, t0) end

"""
update_canopy_prescribed_field!(component::AbstractCanopyComponent,
p,
t,
) end
Updates the spatially and temporally varying prescribed fields of the `component`
Updates the spatio-temporally varying prescribed fields of the `component`
with their values at time `t`.
These fields are stored in the aux-state and *should not* depend on the prognostic
state `Y` or other diagnostic variables stored in `p`; this allows them
to be updated first, prior to updating the rest of the aux state and prognostic state.
However, there is no
guarantee on the order of operations in terms of when diagnostic auxiliary
variables are updated vs. prescribed field auxiliary variables; any required
order of operations must be enforced by the developer who writes the update_aux
These fields are stored in the aux-state, and currently are updated at the
beginning of the `update_aux!` function. Any required
order of operations must be enforced by the developer who writes the `update_aux!`
function.
"""
function update_canopy_prescribed_field!(
component::AbstractCanopyComponent,
p,
t,
) end
function set_canopy_prescribed_field!(component::AbstractCanopyComponent, p, t) end
15 changes: 1 addition & 14 deletions test/integrated/lsms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ for FT in (Float32, Float64)
@test propertynames(p) == (:m1, :m2)
function ClimaLand.make_update_aux(::DummyModel3{FT}) where {FT}
function update_aux!(p, Y, t)
p.m1.a .= FT(2.0)
p.m1.b .= FT(10.0)
end
return update_aux!
Expand All @@ -153,20 +154,6 @@ for FT in (Float32, Float64)
return update_aux!
end

function ClimaLand.make_set_initial_cache(m::DummyModel3{FT}) where {FT}
update_cache! = ClimaLand.make_update_cache(m)
function set_initial_cache!(p, Y, t)
p.m1.a .= FT(2.0)
update_cache!(p, Y, t)
end
return set_initial_cache!
end

# The scenario here is that model 1 has a single prescribed but constant
# variable (a), with another that could get updated each step (b).
# DummyModel4 has only variables that get updated each step.
# Test that the land model function properly calls the individual
# model's functions
set_initial_cache! = ClimaLand.make_set_initial_cache(m)
set_initial_cache!(p, Y, FT(0.0))
@test all(parent(p.m1.a) .== FT(2))
Expand Down
4 changes: 2 additions & 2 deletions test/standalone/Vegetation/canopy_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -445,15 +445,15 @@ for FT in (Float32, Float64)
)

# Test that LAI is updated
update_canopy_prescribed_field!(plant_hydraulics, p, FT(200))
set_canopy_prescribed_field!(plant_hydraulics, p, FT(200))
@test all(
Array(parent(p.canopy.hydraulics.area_index.leaf)) .==
FT(LAI * sin(200 * 2π / 365)),
)

struct Default{FT} <: ClimaLand.Canopy.AbstractCanopyComponent{FT} end
set_canopy_prescribed_field!(Default{FT}(), p, t0)
update_canopy_prescribed_field!(Default{FT}(), p, t0)
set_canopy_prescribed_field!(Default{FT}(), p, t0)
# Test that they are unchanged
@test all(
parent(p.canopy.hydraulics.area_index.leaf) .==
Expand Down

0 comments on commit f36042f

Please sign in to comment.