Skip to content

Commit

Permalink
split atmospheric and hydrological forcing
Browse files Browse the repository at this point in the history
  • Loading branch information
hboisgon committed Dec 4, 2024
1 parent faaf49b commit 70e3516
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 113 deletions.
19 changes: 12 additions & 7 deletions src/erosion.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"Total soil erosion model"
@with_kw struct SoilLoss{RE, OFE, SE, T}
hydrometeo_forcing::HydrometeoForcing
geometry::LandParameters
atmospheric_forcing::AtmosphericForcing{T}
hydrological_forcing::HydrologicalForcing{T}
geometry::LandParameters{T}
rainfall_erosion::RE
overland_flow_erosion::OFE
soil_erosion::SE
Expand All @@ -9,7 +11,8 @@ end
function SoilLoss(nc, config, inds)
n = length(inds)

hydrometeo_forcing = HydrometeoForcing(n)
atmospheric_forcing = AtmosphericForcing(n)
hydrological_forcing = HydrologicalForcing(n)
geometry = LandParameters(nc, config, inds)

# Rainfall erosion
Expand Down Expand Up @@ -40,7 +43,8 @@ function SoilLoss(nc, config, inds)
typeof(soil_erosion_model),
Float,
}(;
hydrometeo_forcing = hydrometeo_forcing,
atmospheric_forcing = atmospheric_forcing,
hydrological_forcing = hydrological_forcing,
geometry = geometry,
rainfall_erosion = rainfall_erosion_model,
overland_flow_erosion = overland_flow_erosion_model,
Expand All @@ -51,7 +55,8 @@ end

function update!(model::SoilLoss, dt)
(;
hydrometeo_forcing,
atmospheric_forcing,
hydrological_forcing,
geometry,
rainfall_erosion,
overland_flow_erosion,
Expand All @@ -63,10 +68,10 @@ function update!(model::SoilLoss, dt)
#need SBM refactor

# Rainfall erosion
update_boundary_conditions!(rainfall_erosion, hydrometeo_forcing)
update_boundary_conditions!(rainfall_erosion, atmospheric_forcing, hydrological_forcing)
update!(rainfall_erosion, geometry, ts)
# Overland flow erosion
update_boundary_conditions!(overland_flow_erosion, hydrometeo_forcing)
update_boundary_conditions!(overland_flow_erosion, hydrological_forcing)
update!(overland_flow_erosion, geometry, ts)
# Total soil erosion and particle differentiation
update_boundary_conditions!(soil_erosion, rainfall_erosion, overland_flow_erosion)
Expand Down
23 changes: 11 additions & 12 deletions src/forcing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ function AtmosphericForcing(
return AtmosphericForcing{T}(; precipitation, potential_evaporation, temperature)
end

@get_units @grid_loc @with_kw struct HydrometeoForcing{T}
# Precipitation [mm Δt⁻¹]
precipitation::Vector{T}
"Struct to store hydrological forcing variables"
@get_units @grid_loc @with_kw struct HydrologicalForcing{T}
# Overland flow depth [m]
waterlevel_land::Vector{T} | "m"
# Overland flow discharge [m3 s-1]
Expand All @@ -31,13 +30,13 @@ end
q_river::Vector{T} | "m3 s-1"
end

function HydrometeoForcing(n)
hydrometeo_forcing = HydrometeoForcing(;
precipitation = fill(mv, n),
waterlevel_land = fill(mv, n),
q_land = fill(mv, n),
waterlevel_river = fill(mv, n),
q_river = fill(mv, n),
)
return hydrometeo_forcing
"Initialize hydrological forcing"
function HydrologicalForcing(
n;
waterlevel_land::Vector{T} = fill(mv, n),
q_land::Vector{T} = fill(mv, n),
waterlevel_river::Vector{T} = fill(mv, n),
q_river::Vector{T} = fill(mv, n),
) where {T}
return HydrologicalForcing{T}(; waterlevel_land, q_land, waterlevel_river, q_river)
end
22 changes: 3 additions & 19 deletions src/sediment/erosion/overland_flow_erosion.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
abstract type AbstractOverlandFlowErosionModel{T} end

struct NoOverlandFlowErosionModel{T} <: AbstractOverlandFlowErosionModel{T} end

## Overland flow structs and functions
@get_units @grid_loc @with_kw struct OverlandFlowErosionVariables{T}
# Total soil erosion from overland flow
Expand Down Expand Up @@ -85,20 +83,13 @@ end

function update_boundary_conditions!(
model::OverlandFlowErosionAnswersModel,
hydrometeo_forcing::HydrometeoForcing,
hydrological_forcing::HydrologicalForcing,
)
(; q) = model.boundary_conditions
(; q_land) = hydrometeo_forcing
(; q_land) = hydrological_forcing
@. q = q_land
end

function update_boundary_conditions!(
model::NoOverlandFlowErosionModel,
hydrometeo_forcing::HydrometeoForcing,
)
return nothing
end

function update!(model::OverlandFlowErosionAnswersModel, geometry::LandParameters, ts)
(; q) = model.boundary_conditions
(; usle_k, usle_c, answers_k) = model.parameters
Expand All @@ -116,11 +107,4 @@ function update!(model::OverlandFlowErosionAnswersModel, geometry::LandParameter
ts,
)
end
end

function update!(model::NoOverlandFlowErosionModel)
return nothing
end

get_overland_flow_erosion(model::NoOverlandFlowErosionModel) = 0.0
get_overland_flow_erosion(model::OverlandFlowErosionAnswersModel) = model.variables.amount
end
48 changes: 26 additions & 22 deletions src/sediment/erosion/rainfall_erosion.jl
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
abstract type AbstractRainfallErosionModel{T} end

struct NoRainfallErosionModel{T} <: AbstractRainfallErosionModel{T} end

## General rainfall erosion functions and structs
"Struct for storing rainfall erosion model variables"
@get_units @grid_loc @with_kw struct RainfallErosionModelVariables{T}
# Total soil erosion from rainfall (splash)
amount::Vector{T} | "t dt-1"
end

"Initialize rainfall erosion model variables"
function RainfallErosionModelVariables(n; amount::Vector{T} = fill(mv, n)) where {T}
return RainfallErosionModelVariables{T}(; amount = amount)
end

## EUROSEM specific structs and functions for rainfall erosion
"Struct for storing EUROSEM rainfall erosion model boundary conditions"
@get_units @grid_loc @with_kw struct RainfallErosionEurosemBC{T}
# precipitation
precipitation::Vector{T} | "mm dt-1"
Expand All @@ -22,6 +23,7 @@ end
waterlevel::Vector{T} | "m"
end

"Initialize EUROSEM rainfall erosion model boundary conditions"
function RainfallErosionEurosemBC(
n;
precipitation::Vector{T} = fill(mv, n),
Expand All @@ -35,6 +37,7 @@ function RainfallErosionEurosemBC(
)
end

"Struct for storing EUROSEM rainfall erosion model parameters"
@get_units @grid_loc @with_kw struct RainfallErosionEurosemParameters{T}
# Soil detachability factor
soil_detachability::Vector{T} | "g J-1"
Expand All @@ -48,6 +51,7 @@ end
soilcover_fraction::Vector{T} | "-"
end

"Initialize EUROSEM rainfall erosion model parameters"
function RainfallErosionEurosemParameters(nc, config, inds)
soil_detachability = ncread(
nc,
Expand Down Expand Up @@ -99,12 +103,14 @@ function RainfallErosionEurosemParameters(nc, config, inds)
return eurosem_parameters
end

"EUROSEM rainfall erosion model"
@with_kw struct RainfallErosionEurosemModel{T} <: AbstractRainfallErosionModel{T}
boundary_conditions::RainfallErosionEurosemBC{T}
parameters::RainfallErosionEurosemParameters{T}
variables::RainfallErosionModelVariables{T}
end

"Initialize EUROSEM rainfall erosion model"
function RainfallErosionEurosemModel(nc, config, inds)
n = length(inds)
vars = RainfallErosionModelVariables(n)
Expand All @@ -118,15 +124,18 @@ function RainfallErosionEurosemModel(nc, config, inds)
return model
end

"Update EUROSEM rainfall erosion model boundary conditions for a single timestep"
function update_boundary_conditions!(
model::RainfallErosionEurosemModel,
hydrometeo_forcing::HydrometeoForcing,
atmospheric_forcing::AtmosphericForcing,
hydrological_forcing::HydrologicalForcing,
)
(; precipitation, waterlevel) = model.boundary_conditions
@. precipitation = hydrometeo_forcing.precipitation
@. waterlevel = model.boundary_conditions.waterlevel_land
@. precipitation = atmospheric_forcing.precipitation
@. waterlevel = hydrological_forcing.waterlevel_land
end

"Update EUROSEM rainfall erosion model for a single timestep"
function update!(model::RainfallErosionEurosemModel, geometry::LandParameters, ts)
(; precipitation, interception, waterlevel) = model.boundary_conditions
(;
Expand Down Expand Up @@ -156,22 +165,26 @@ function update!(model::RainfallErosionEurosemModel, geometry::LandParameters, t
end

### ANSWERS specific structs and functions for rainfall erosion
"Struct for storing ANSWERS rainfall erosion model boundary conditions"
@get_units @grid_loc @with_kw struct RainfallErosionAnswersBC{T}
# precipitation
precipitation::Vector{T} | "mm dt-1"
end

"Initialize ANSWERS rainfall erosion model boundary conditions"
function RainfallErosionAnswersBC(n; precipitation::Vector{T} = fill(mv, n)) where {T}
return RainfallErosionAnswersBC{T}(; precipitation = precipitation)
end

"Struct for storing ANSWERS rainfall erosion model parameters"
@get_units @grid_loc @with_kw struct RainfallErosionAnswersParameters{T}
# Soil erodibility factor
usle_k::Vector{T} | "-"
# Crop management factor
usle_c::Vector{T} | "-"
end

"Initialize ANSWERS rainfall erosion model parameters"
function RainfallErosionAnswersParameters(nc, config, inds)
usle_k = ncread(
nc,
Expand All @@ -194,12 +207,14 @@ function RainfallErosionAnswersParameters(nc, config, inds)
return answers_parameters
end

"ANSWERS rainfall erosion model"
@with_kw struct RainfallErosionAnswersModel{T} <: AbstractRainfallErosionModel{T}
boundary_conditions::RainfallErosionAnswersBC{T}
parameters::RainfallErosionAnswersParameters{T}
variables::RainfallErosionModelVariables{T}
end

"Initialize ANSWERS rainfall erosion model"
function RainfallErosionAnswersModel(nc, config, inds)
n = length(inds)
bc = RainfallErosionAnswersBC(n)
Expand All @@ -213,14 +228,17 @@ function RainfallErosionAnswersModel(nc, config, inds)
return model
end

"Update ANSWERS rainfall erosion model boundary conditions for a single timestep"
function update_boundary_conditions!(
model::RainfallErosionAnswersModel,
hydrometeo_forcing::HydrometeoForcing,
atmospheric_forcing::AtmosphericForcing,
hydrological_forcing::HydrologicalForcing,
)
(; precipitation) = model.boundary_conditions
@. precipitation = hydrometeo_forcing.precipitation
@. precipitation = atmospheric_forcing.precipitation
end

"Update ANSWERS rainfall erosion model for a single timestep"
function update!(model::RainfallErosionAnswersModel, geometry::LandParameters, ts)
(; precipitation) = model.boundary_conditions
(; usle_k, usle_c) = model.parameters
Expand All @@ -236,18 +254,4 @@ function update!(model::RainfallErosionAnswersModel, geometry::LandParameters, t
ts,
)
end
end

function update_boundary_conditions!(
model::NoRainfallErosionModel,
hydrometeo_forcing::HydrometeoForcing,
)
return nothing
end

function update!(model::NoRainfallErosionModel)
return nothing
end

get_rainfall_erosion(model::NoRainfallErosionModel) = 0.0
get_rainfall_erosion(model::AbstractRainfallErosionModel) = model.variables.amount
end
4 changes: 2 additions & 2 deletions src/sediment/erosion/river_erosion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ end

function update_boundary_conditions!(
model::RiverErosionJulianTorresModel,
hydrometeo_forcing::HydrometeoForcing,
hydrological_forcing::HydrologicalForcing,
)
(; waterlevel) = model.boundary_conditions
(; waterlevel_river) = hydrometeo_forcing
(; waterlevel_river) = hydrological_forcing
@. waterlevel = waterlevel_river
end

Expand Down
4 changes: 2 additions & 2 deletions src/sediment/erosion/soil_erosion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ function update_boundary_conditions!(
rainfall_erosion::AbstractRainfallErosionModel,
overland_flow_erosion::AbstractOverlandFlowErosionModel,
)
re = get_rainfall_erosion(rainfall_erosion)
ole = get_overland_flow_erosion(overland_flow_erosion)
re = rainfall_erosion.variables.amount
ole = overland_flow_erosion.variables.amount
(; rainfall_erosion, overland_flow_erosion) = model.boundary_conditions
@. rainfall_erosion = re
@. overland_flow_erosion = ole
Expand Down
12 changes: 6 additions & 6 deletions src/sediment/sediment_transport/river_transport.jl
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ end

function update_boundary_conditions!(
model::SedimentRiverTransportModel,
hydrometeo_forcing::HydrometeoForcing,
hydrological_forcing::HydrologicalForcing,
transport_capacity_model::AbstractTransportCapacityModel,
to_river_model::SedimentToRiverDifferentiationModel,
potential_erosion_model::AbstractRiverErosionModel,
Expand All @@ -364,8 +364,8 @@ function update_boundary_conditions!(
potential_erosion_river_bank,
) = model.boundary_conditions

# HydroMeteo forcing
(; q_river, waterlevel_river) = hydrometeo_forcing
# Hydrological forcing
(; q_river, waterlevel_river) = hydrological_forcing
@. q = q_river
@. waterlevel = waterlevel_river
# Transport capacity
Expand Down Expand Up @@ -935,12 +935,12 @@ end

function update_boundary_conditions!(
model::SedimentConcentrationsRiverModel,
hydrometeo_forcing::HydrometeoForcing,
hydrological_forcing::HydrologicalForcing,
sediment_flux_model::AbstractSedimentRiverTransportModel,
)
(; q, waterlevel, clay, silt, sand, sagg, lagg, gravel) = model.boundary_conditions
# Hydrometeo forcing
(; q_river, waterlevel_river) = hydrometeo_forcing
# Hydrological forcing
(; q_river, waterlevel_river) = hydrological_forcing
@. q = q_river
@. waterlevel = waterlevel_river
# Sediment flux per particle
Expand Down
4 changes: 2 additions & 2 deletions src/sediment/sediment_transport/transport_capacity.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ end

function update_boundary_conditions!(
model::AbstractTransportCapacityModel,
hydrometeo_forcing::HydrometeoForcing,
hydrological_forcing::HydrologicalForcing,
model_type::Symbol,
)
(; q, waterlevel) = model.boundary_conditions
(; q_land, waterlevel_land, q_river, waterlevel_river) = hydrometeo_forcing
(; q_land, waterlevel_land, q_river, waterlevel_river) = hydrological_forcing

if model_type == :land
@. q = q_land
Expand Down
Loading

0 comments on commit 70e3516

Please sign in to comment.