Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge NeuralSnow module into main #946

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Merge NeuralSnow module into main #946

wants to merge 1 commit into from

Conversation

a-charbon
Copy link
Contributor

Purpose

To-do

Content


  • I have read and checked the items on the review checklist.

Copy link
Member

@kmdeck kmdeck left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for your hard work on this! You can merge once the doc comments are addressed. 🎉

@@ -42,6 +42,23 @@ and is used as a bulk snow model.
"""
abstract type AbstractSnowModel{FT} <: ClimaLand.AbstractExpModel{FT} end

"""
AbstractDensityModel{FT}
Defines the model type for density and depth parameterizations
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Defines the model type for density and depth parameterizations
Defines the model type for density and depth parameterizations
for use within an `AbstractSnowModel` type. Current examples include the
`ConstantDensityModel` models.
Since depth and bulk density are related via SWE, and SWE is a prognostic variable
of the snow model, only depth or density can be independently modeled at one time.
This is why they are treated as a single "density model" even if the parameterization is actually
a model for snow depth.
The snow depth/density model can be diagnostic or introduce additional prognostic variables.

prognostic_vars(::SnowModel) = (:S, :U)
prognostic_vars(m::SnowModel) =
(:S, :U, density_prog_vars(m.parameters.density)...)
density_prog_vars(::AbstractDensityModel) = ()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
density_prog_vars(::AbstractDensityModel) = ()
"""
density_prog_vars(::AbstractDensityModel)
A default method for adding prognostic variables to the snow model as required
by the density model choice.
"""
density_prog_vars(::AbstractDensityModel) = ()

@@ -272,8 +299,15 @@ function ClimaLand.make_update_aux(model::SnowModel{FT}) where {FT}

@. p.snow.T_sfc = snow_surface_temperature(p.snow.T)

@. p.snow.water_runoff =
compute_water_runoff(Y.snow.S, p.snow.q_l, p.snow.T, parameters)
p.snow.water_runoff .=
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
p.snow.water_runoff .=
# Note that the `snow_depth` call below allocates a field. Return to this in the future.
p.snow.water_runoff .=

@@ -253,7 +253,7 @@ function update_soil_snow_ground_heat_flux!(
κ_soil = ClimaLand.Domains.top_center_to_surface(p.soil.κ)

# Depth of snow and soil layers interacting thermally at interface
Δz_snow = p.snow.z # Snow depth
Δz_snow = Snow.snow_depth(snow_params.density, Y, p, snow_params) # Snow depth
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Δz_snow = Snow.snow_depth(snow_params.density, Y, p, snow_params) # Snow depth
# Note that the `snow_depth` call below allocates a field. Return to this in the future.
Δz_snow = Snow.snow_depth(snow_params.density, Y, p, snow_params) # Snow depth

is calculated from a neural network determining the rate of change of snow depth, dzdt.
"""
struct NeuralDepthModel{FT} <: AbstractDensityModel{FT}
z_model::Flux.Chain
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
z_model::Flux.Chain
" The Flux neural network for compute dz/dt"
z_model::Flux.Chain

"""
struct NeuralDepthModel{FT} <: AbstractDensityModel{FT}
z_model::Flux.Chain
α::FT
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
α::FT
"The inverse of the averaging window time (1/s)"
α::FT


"""
get_znetwork()
Return the snow-depth neural network from the paper.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Return the snow-depth neural network from the paper.
Return the snow-depth neural network from Charbonneau et al (2024; https://arxiv.org/abs/2412.06819), and sets the timescale [@a-charbon explain what this timescale is]
Note that because we are loading a pre-trained model, the number of features input, the size of the output, etc, are hard coded in the function below.


"""
NeuralDepthModel{FT <: AbstractFloat} <: AbstractDensityModel{FT}
Establishes the density parameterization where snow density
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Establishes the density parameterization where snow density
Establishes the density parameterization where snow density is calculated from a prognostic snow depth variable,
along with the prognostic SWE variable, using a neural network for the rate of change of snow depth, dz/dt.
The input to the network are temporally averaged, which we achieve using an exponentially moving average, with a rate of `\alpha`. Therefore, to achieve approximately daily averages, use `\alpha = 1/86400 s^{-1}`.


"""
NeuralDepthModel(FT::DataType; Δt::Union{Nothing, FT}=nothing; model::Flux.Chain, α::Union{FT, Nothing})
An outer constructor for the `NeuralDepthModel` density parameterization for usage in a snow model.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"An outer constructor for the NeuralDepthModel density parameterization for usage in a snow model.

By default, the neural network weights trained in Charbonneau et al 2015 are used, but one can also supply their own model by keyword argument. Since the neural network models are trained using temporally averaged input, we also must supply information about how to compute these averaged inputs [using an exponential-weighting scheme]. By default, an averaging window of 43200 seconds is used. "

Is Delta t really optional? I thought the lower constraint was baked into the model.

Can you explain the instability better?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, Delta t not used anymore maybe?

p,
)

dY.snow.Z .=
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can comment that dzdt(density,Y) allocates a field here as a reminder

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants