Skip to content
This repository has been archived by the owner on Mar 1, 2023. It is now read-only.

Commit

Permalink
Merge #2172
Browse files Browse the repository at this point in the history
2172: Add mutating flux-source knl fallbacks in BL r=charleskawczynski a=charleskawczynski

### Description

This PR adds

 - `flux_first_order!`
 - `flux_second_order!`
 - and `source!`

fallbacks in `BalanceLaws/`, and removes the implementation in `AtmosModel.jl`. We can also remove the implementations in the land model once #2136 lands.

I guess one benefit of keeping these implementations would be for simpler debugging when we start coupling models, so I'd like to hear feedback about this idea.



Co-authored-by: Charles Kawczynski <[email protected]>
  • Loading branch information
bors[bot] and charleskawczynski authored Apr 20, 2021
2 parents 1354678 + d77c009 commit 6ea38fc
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 165 deletions.
1 change: 1 addition & 0 deletions docs/src/APIs/BalanceLaws/BalanceLaws.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Source
TendencyDef
eq_tends
prognostic_vars
get_prog_state
projection
precompute
prognostic_var_source_map
Expand Down
107 changes: 0 additions & 107 deletions src/Atmos/Model/AtmosModel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -602,39 +602,6 @@ function precompute(atmos::AtmosModel, args, tt::Flux{FirstOrder})
return (; ts, turbconv)
end

"""
flux_first_order!(
m::AtmosModel,
flux::Grad,
state::Vars,
aux::Vars,
t::Real
)
Computes and assembles non-diffusive fluxes in the model
equations.
"""
@inline function flux_first_order!(
atmos::AtmosModel,
flux::Grad,
state::Vars,
aux::Vars,
t::Real,
direction,
)

tend = Flux{FirstOrder}()
_args = (; state, aux, t, direction)
args = merge(_args, (precomputed = precompute(atmos, _args, tend),))

map(prognostic_vars(atmos)) do prog
var, name = get_prog_state(flux, prog)
val = Σfluxes(prog, eq_tends(prog, atmos, tend), atmos, args)
setproperty!(var, name, val)
end
nothing
end

function compute_gradient_argument!(
atmos::AtmosModel,
transform::Vars,
Expand Down Expand Up @@ -787,41 +754,6 @@ function precompute(atmos::AtmosModel, args, tt::Flux{SecondOrder})
return (; ts, turbconv, turbulence)
end

"""
flux_second_order!(
atmos::AtmosModel,
flux::Grad,
state::Vars,
diffusive::Vars,
hyperdiffusive::Vars,
aux::Vars,
t::Real
)
Diffusive fluxes in AtmosModel. Viscosity, diffusivity are calculated
in the turbulence subcomponent and accessed within the diffusive flux
function. Contributions from subcomponents are then assembled (pointwise).
"""
@inline function flux_second_order!(
atmos::AtmosModel,
flux::Grad,
state::Vars,
diffusive::Vars,
hyperdiffusive::Vars,
aux::Vars,
t::Real,
)
tend = Flux{SecondOrder}()
_args = (; state, aux, t, diffusive, hyperdiffusive)
args = merge(_args, (precomputed = precompute(atmos, _args, tend),))

map(prognostic_vars(atmos)) do prog
var, name = get_prog_state(flux, prog)
val = Σfluxes(prog, eq_tends(prog, atmos, tend), atmos, args)
setproperty!(var, name, val)
end
nothing
end

soundspeed_air(ts::ThermodynamicState, ::Anelastic1D) = 0
soundspeed_air(ts::ThermodynamicState, ::Compressible) = soundspeed_air(ts)
@inline function wavespeed(
Expand Down Expand Up @@ -982,45 +914,6 @@ function precompute(atmos::AtmosModel, args, tt::Source)
return (; ts, turbconv, precipitation)
end

"""
source!(
m::AtmosModel,
source::Vars,
state::Vars,
diffusive::Vars,
aux::Vars,
t::Real,
direction::Direction,
)
Computes (and assembles) source terms `S(Y)` in:
```
∂Y
-- = - ∇ • F + S(Y)
∂t
```
"""
function source!(
atmos::AtmosModel,
source::Vars,
state::Vars,
diffusive::Vars,
aux::Vars,
t::Real,
direction,
)
tend = Source()
_args = (; state, aux, t, direction, diffusive)
args = merge(_args, (precomputed = precompute(atmos, _args, tend),))

map(prognostic_vars(atmos)) do prog
var, name = get_prog_state(source, prog)
val = Σsources(prog, eq_tends(prog, atmos, tend), atmos, args)
setproperty!(var, name, val)
end
nothing
end

"""
init_state_prognostic!(
m::AtmosModel,
Expand Down
1 change: 1 addition & 0 deletions src/BalanceLaws/BalanceLaws.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@ include("show_tendencies.jl")
include("sum_tendencies.jl")
include("prog_prim_conversion.jl")
include("vars_wrappers.jl")
include("kernels.jl")

end
144 changes: 144 additions & 0 deletions src/BalanceLaws/kernels.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@

"""
flux_first_order!(
bl::BalanceLaw,
flux::Grad,
state::Vars,
aux::Vars,
t::Real
)
Computes (and assembles) flux terms `F¹(Y)` in:
```
∂Y
-- + ∇ • F¹(Y) + ∇ • F²(Y,G) = S(Y, G), G = ∇Y
∂t
```
Computes and assembles non-diffusive
fluxes in the model equations.
For this fallback to work, several methods must be defined:
- [`prognostic_vars`](@ref)
- [`eq_tends`](@ref)
- [`get_prog_state`](@ref)
optionally,
- [`precompute`](@ref)
and individual [`flux`](@ref) kernels that
are defined for each type that `eq_tends` returns.
"""
@inline function flux_first_order!(
bl::BalanceLaw,
flux,
state,
aux,
t,
direction,
)

tend = Flux{FirstOrder}()
_args = (; state, aux, t, direction)
args = merge(_args, (precomputed = precompute(bl, _args, tend),))

map(prognostic_vars(bl)) do prog
var, name = get_prog_state(flux, prog)
val = Σfluxes(prog, eq_tends(prog, bl, tend), bl, args)
setproperty!(var, name, val)
end
nothing
end

"""
flux_second_order!(
bl::BalanceLaw,
flux::Grad,
state::Vars,
diffusive::Vars,
hyperdiffusive::Vars,
aux::Vars,
t::Real
)
Computes (and assembles) flux terms `F²(Y, G)` in:
```
∂Y
-- + ∇ • F¹(Y) + ∇ • F²(Y,G) = S(Y, G), G = ∇Y
∂t
```
Diffusive fluxes in BalanceLaw. Viscosity, diffusivity are calculated
in the turbulence subcomponent and accessed within the diffusive flux
function. Contributions from subcomponents are then assembled (pointwise).
For this fallback to work, several methods must be defined:
- [`prognostic_vars`](@ref)
- [`eq_tends`](@ref)
- [`get_prog_state`](@ref)
optionally,
- [`precompute`](@ref)
and individual [`flux`](@ref) kernels that
are defined for each type that `eq_tends` returns.
"""
@inline function flux_second_order!(
bl::BalanceLaw,
flux,
state,
diffusive,
hyperdiffusive,
aux,
t,
)
tend = Flux{SecondOrder}()
_args = (; state, aux, t, diffusive, hyperdiffusive)
args = merge(_args, (precomputed = precompute(bl, _args, tend),))

map(prognostic_vars(bl)) do prog
var, name = get_prog_state(flux, prog)
val = Σfluxes(prog, eq_tends(prog, bl, tend), bl, args)
setproperty!(var, name, val)
end
nothing
end

"""
source!(
bl::BalanceLaw,
source::Vars,
state::Vars,
diffusive::Vars,
aux::Vars,
t::Real,
direction::Direction,
)
Computes (and assembles) source terms `S(Y)` in:
```
∂Y
-- + ∇ • F¹(Y) + ∇ • F²(Y,G) = S(Y, G), G = ∇Y
∂t
```
For this fallback to work, several methods must be defined:
- [`prognostic_vars`](@ref)
- [`eq_tends`](@ref)
- [`get_prog_state`](@ref)
optionally,
- [`precompute`](@ref)
and individual [`source`](@ref) kernels that
are defined for each type that `eq_tends` returns.
"""
function source!(bl::BalanceLaw, source, state, diffusive, aux, t, direction)
tend = Source()
_args = (; state, aux, t, direction, diffusive)
args = merge(_args, (precomputed = precompute(bl, _args, tend),))

map(prognostic_vars(bl)) do prog
var, name = get_prog_state(source, prog)
val = Σsources(prog, eq_tends(prog, bl, tend), bl, args)
setproperty!(var, name, val)
end
nothing
end
59 changes: 1 addition & 58 deletions src/Land/Model/LandModel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@ using ..BalanceLaws
import ..BalanceLaws:
BalanceLaw,
prognostic_vars,
get_prog_state,
flux,
source,
eq_tends,
precompute,
vars_state,
flux_first_order!,
flux_second_order!,
source!,
boundary_conditions,
parameter_set,
boundary_state!,
Expand Down Expand Up @@ -139,16 +137,6 @@ function nodal_init_state_auxiliary!(
land_init_aux!(land, land.soil, aux, geom)
end

function flux_first_order!(
land::LandModel,
flux::Grad,
state::Vars,
aux::Vars,
t::Real,
directions,
) end


function compute_gradient_argument!(
land::LandModel,
transform::Vars,
Expand Down Expand Up @@ -181,28 +169,6 @@ function compute_gradient_flux!(

end

function flux_second_order!(
land::LandModel,
flux::Grad,
state::Vars,
diffusive::Vars,
hyperdiffusive::Vars,
aux::Vars,
t::Real,
)
tend = Flux{SecondOrder}()
_args = (; state, aux, t, diffusive, hyperdiffusive)
args = merge(_args, (precomputed = precompute(land, _args, tend),))

map(prognostic_vars(land)) do prog
var, name = get_prog_state(flux, prog)
val = Σfluxes(prog, eq_tends(prog, land, tend), land, args)
setproperty!(var, name, val)
end
nothing

end

function nodal_update_auxiliary_state!(
land::LandModel,
state::Vars,
Expand All @@ -212,29 +178,6 @@ function nodal_update_auxiliary_state!(
land_nodal_update_auxiliary_state!(land, land.soil, state, aux, t)
end


function source!(
land::LandModel,
source::Vars,
state::Vars,
diffusive::Vars,
aux::Vars,
t::Real,
direction,
)
tend = Source()
_args = (; state, aux, t, direction, diffusive)
args = merge(_args, (precomputed = precompute(land, _args, tend),))

map(prognostic_vars(land)) do prog
var, name = get_prog_state(source, prog)
val = Σsources(prog, eq_tends(prog, land, tend), land, args)
setproperty!(var, name, val)
end
nothing
end


function init_state_prognostic!(
land::LandModel,
state::Vars,
Expand Down

0 comments on commit 6ea38fc

Please sign in to comment.