Skip to content

Commit

Permalink
update ... > import..._fields
Browse files Browse the repository at this point in the history
  • Loading branch information
LenkaNovak committed Jun 13, 2023
1 parent 9567e08 commit f8e65a5
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 27 deletions.
8 changes: 4 additions & 4 deletions docs/src/fieldexchanger.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
This module contains general functions for the exchange of fields between the atmospheric and surface component models.

The `FieldExchanger` needs to populate the coupler with
- atmospheric fields (mostly fluxes), via the `update_combined_atmos_fluxes!` function
- average surface properties of each coupler gridpoint, via the `update_combined_surfaces!` function
- atmospheric fields (mostly fluxes), via the `import_atmos_fields!` function
- average surface properties of each coupler gridpoint, via the `import_combined_surface_fields!` function

The component models are updated by broadcasting the coupler fields, via the `update_model_sims!` function. For an update, this function requires that `update_field!` is defined for the particular variable and component model. Currently, we support the:
- `AtmosModelSimulation`: `albedo`, `surface_temperature`
Expand All @@ -17,8 +17,8 @@ If an `update_field!` function is not defined for a particular component model,
## FieldExchanger API

```@docs
ClimaCoupler.FieldExchanger.update_combined_atmos_fluxes!
ClimaCoupler.FieldExchanger.update_combined_surfaces!
ClimaCoupler.FieldExchanger.import_atmos_fields!
ClimaCoupler.FieldExchanger.import_combined_surface_fields!
ClimaCoupler.FieldExchanger.update_model_sims!
ClimaCoupler.FieldExchanger.update_sim!
ClimaCoupler.FieldExchanger.reinit_model_sims!
Expand Down
21 changes: 10 additions & 11 deletions experiments/AMIP/modular/coupler_driver_modular.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ if !(@isdefined parsed_args)
parsed_args = parse_commandline(argparse_settings())
end

# modify parsed args for fast testing from REPL #hide
## modify parsed args for fast testing from REPL #hide
if isinteractive()
parsed_args["coupled"] = true #hide
parsed_args["surface_scheme"] = "monin_obukhov" #hide
Expand Down Expand Up @@ -127,8 +127,8 @@ import ClimaCoupler.Interfacer:
update_field!
import ClimaCoupler.FluxCalculator: PartitionedComponentModelGrid, CombinedAtmosGrid, compute_combined_turbulent_fluxes!
import ClimaCoupler.FieldExchanger:
update_combined_atmos_fluxes!,
update_combined_surfaces!,
import_atmos_fields!,
import_combined_surface_fields!,
update_sim!,
update_model_sims!,
reinit_model_sims!,
Expand Down Expand Up @@ -362,17 +362,17 @@ cs = CoupledSimulation{FT}(


#=
## Initial States Exchange
## Initial Component Model Exchange
=#

## share states and fluxes between models
turbulent_fluxes = CombinedAtmosGrid()
update_surface_fractions!(cs)
update_combined_surfaces!(cs.fields, cs.model_sims, cs.boundary_space, turbulent_fluxes)
import_combined_surface_fields!(cs.fields, cs.model_sims, cs.boundary_space, turbulent_fluxes)
update_sim!(cs.model_sims.atmos_sim, cs.fields, turbulent_fluxes) # would be good to rm dep in cs
compute_combined_turbulent_fluxes!(cs.model_sims, cs.fields, turbulent_fluxes) # here computed using atmos functions
parsed_args["ode_algo"] == "ARS343" ? step!(atmos_sim, Δt_cpl) : nothing
update_combined_atmos_fluxes!(cs.fields, cs.model_sims, cs.boundary_space, turbulent_fluxes)
import_atmos_fields!(cs.fields, cs.model_sims, cs.boundary_space, turbulent_fluxes)
update_model_sims!(cs.model_sims, cs.fields, turbulent_fluxes)

## reinitialize (TODO: avoid with interfaces)
Expand Down Expand Up @@ -432,14 +432,13 @@ function solve_coupler!(cs)
update_model_sims!(cs.model_sims, cs.fields, turbulent_fluxes)

## step sims

step_model_sims!(cs.model_sims, t)

# exchange of combined fields and (if specified) calculation of fluxes using combined states
# exchange combined fields and (if specified) calculate fluxes using combined states
update_surface_fractions!(cs)
update_combined_surfaces!(cs.fields, cs.model_sims, cs.boundary_space, turbulent_fluxes)
compute_combined_turbulent_fluxes!(cs.model_sims, cs.fields, turbulent_fluxes) # combine or partial
update_combined_atmos_fluxes!(cs.fields, cs.model_sims, cs.boundary_space, turbulent_fluxes) # radiative and/or turbulent
import_combined_surface_fields!(cs.fields, cs.model_sims, cs.boundary_space, turbulent_fluxes)
compute_combined_turbulent_fluxes!(cs.model_sims, cs.fields, turbulent_fluxes)
import_atmos_fields!(cs.fields, cs.model_sims, cs.boundary_space, turbulent_fluxes) # radiative and/or turbulent

# TODO: compute_and_send_partitioned_turbulent_fluxes!(cs)

Expand Down
12 changes: 6 additions & 6 deletions src/FieldExchanger.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ atmospheric and surface component models.
module FieldExchanger

import SciMLBase: step!, reinit!
export update_combined_atmos_fluxes!,
update_combined_surfaces!, update_sim!, update_model_sims!, reinit_model_sims!, step_model_sims!
export import_atmos_fields!,
import_combined_surface_fields!, update_sim!, update_model_sims!, reinit_model_sims!, step_model_sims!

using ClimaCoupler: Interfacer, FluxCalculator, Regridder, Utilities

"""
update_combined_atmos_fluxes!(csf, model_sims, boundary_space, turbulent_fluxes)
import_atmos_fields!(csf, model_sims, boundary_space, turbulent_fluxes)
Updates the coupler with the atmospheric fluxes. The `Interfacer.get_field` functions
(`:turbulent_energy_flux`, `:turbulent_moisture_flux`, `:radiative_energy_flux`, `:liquid_precipitation`, `:snow_precipitation`)
Expand All @@ -25,7 +25,7 @@ have to be defined for the amtospheric component model type.
- `boundary_space`: [Spaces.AbstractSpace] the space of the coupler surface.
- `turbulent_fluxes`: [TurbulentFluxPartition] denotes a flag for turbulent flux calculation.
"""
function update_combined_atmos_fluxes!(csf, model_sims, boundary_space, turbulent_fluxes)
function import_atmos_fields!(csf, model_sims, boundary_space, turbulent_fluxes)
(; atmos_sim) = model_sims

# from atmos
Expand All @@ -40,7 +40,7 @@ function update_combined_atmos_fluxes!(csf, model_sims, boundary_space, turbulen
end

"""
update_combined_surfaces!(csf, model_sims, boundary_space, turbulent_fluxes)
import_combined_surface_fields!(csf, model_sims, boundary_space, turbulent_fluxes)
Updates the coupler with the surface properties. The `Interfacer.get_field` functions for
(`:surface_temperature`, `:albedo`, `:roughness_momentum`, `:roughness_buoyancy`, `:beta`)
Expand All @@ -53,7 +53,7 @@ need to be specified for each surface model.
- `turbulent_fluxes`: [TurbulentFluxPartition] denotes a flag for turbulent flux calculation.
"""
function update_combined_surfaces!(csf, model_sims, boundary_space, turbulent_fluxes)
function import_combined_surface_fields!(csf, model_sims, boundary_space, turbulent_fluxes)

combined_field = zeros(boundary_space)

Expand Down
12 changes: 6 additions & 6 deletions test/field_exchanger_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import ClimaCoupler.Interfacer:
update_field!, AtmosModelSimulation, SurfaceModelSimulation, SurfaceStub, get_field, update_field!
import ClimaCoupler.FluxCalculator: CombinedAtmosGrid, PartitionedComponentModelGrid
import ClimaCoupler.FieldExchanger:
update_combined_atmos_fluxes!,
update_combined_surfaces!,
import_atmos_fields!,
import_combined_surface_fields!,
update_model_sims!,
reinit_model_sims!,
reinit!,
Expand All @@ -26,7 +26,7 @@ get_field(sim::DummySimulation, ::Val{:radiative_energy_flux}) = sim.cache.radia
get_field(sim::DummySimulation, ::Val{:liquid_precipitation}) = sim.cache.liquid_precipitation
get_field(sim::DummySimulation, ::Val{:snow_precipitation}) = sim.cache.snow_precipitation

@testset "update_combined_atmos_fluxes!" begin
@testset "import_atmos_fields!" begin

boundary_space = TestHelper.create_space(FT)
coupler_names = (:F_A, :F_E, :F_R, :P_liq, :P_snow)
Expand All @@ -45,7 +45,7 @@ get_field(sim::DummySimulation, ::Val{:snow_precipitation}) = sim.cache.snow_pre
results = [FT(1), FT(0)]
for (i, t) in enumerate(flux_types)
coupler_fields = NamedTuple{coupler_names}(ntuple(i -> Fields.zeros(boundary_space), length(coupler_names)))
update_combined_atmos_fluxes!(coupler_fields, model_sims, boundary_space, t)
import_atmos_fields!(coupler_fields, model_sims, boundary_space, t)
@test parent(coupler_fields.F_A)[1] == results[i]
@test parent(coupler_fields.F_E)[1] == results[i]
@test parent(coupler_fields.F_R)[1] == results[1]
Expand Down Expand Up @@ -74,7 +74,7 @@ get_field(sim::Union{TestSurfaceSimulation1, TestSurfaceSimulation2}, ::Val{:bet
get_field(sim::TestSurfaceSimulation1, ::Val{:area_fraction}) = sim.cache_field .* 0.0
get_field(sim::TestSurfaceSimulation2, ::Val{:area_fraction}) = sim.cache_field .* 0.5

@testset "update_combined_surfaces!" begin
@testset "import_combined_surface_fields!" begin
# coupler cache setup
boundary_space = TestHelper.create_space(FT)
coupler_names = (:T_S, :z0m_S, :z0b_S, :albedo, :beta)
Expand All @@ -89,7 +89,7 @@ get_field(sim::TestSurfaceSimulation2, ::Val{:area_fraction}) = sim.cache_field
for (i, t) in enumerate(flux_types)
coupler_fields = NamedTuple{coupler_names}(ntuple(i -> Fields.zeros(boundary_space), length(coupler_names)))

update_combined_surfaces!(coupler_fields, sims, boundary_space, t)
import_combined_surface_fields!(coupler_fields, sims, boundary_space, t)
@test parent(coupler_fields.T_S)[1] == results[2]
@test parent(coupler_fields.albedo)[1] == results[2]
if t !== PartitionedComponentModelGrid()
Expand Down

0 comments on commit f8e65a5

Please sign in to comment.