-
Notifications
You must be signed in to change notification settings - Fork 7
NestedSimulation abstraction + ERA5 regional hindcast example #233
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
Draft
ewquon
wants to merge
25
commits into
main
Choose a base branch
from
eq/era5_breeze_land
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
b0b2b79
Add ERA5 regional hindcast example
ewquon 18b6320
Cleanup
ewquon ffc3670
Construct compressible Breeze model via atmosphere_simulation
ewquon 5463396
Export download_dataset
ewquon 62ca809
Use Oceananigans' ReferenceToStretchedDiscretization for vertical grid
ewquon 8e48f4b
Revert "Export download_dataset"
ewquon 71150bc
Cleanup, remove download_dataset calls
ewquon a59f15b
Minor code cleanup
ewquon d1dfc9b
Add per-column ERA5 interpolation with terrain workaround
ewquon 925bd12
Merge branch 'main' into eq/era5_breeze_land
ewquon 83a76a1
Fix intermediate_grid coordinate alignment with ERA5 native cells
ewquon 5e73f8e
Add profile plots at three sites of varying terrain elevation
ewquon b3d9426
Merge branch 'main' into eq/era5_breeze_land
glwagner 020d821
Literate
ewquon 6e62bdc
Add NestedSimulations module + 3D PrescribedAtmosphere defaults
glwagner 6a74afe
Merge main into eq/era5_breeze_land
glwagner 9230a06
Add child_simulation + parent_forcings for one-call nesting
glwagner ee973ff
parent_forcings: pass FieldTimeSeries directly to Relaxation
glwagner fd46c98
Native FieldTimeSeries boundary condition: InterpolatedFTSBoundary
glwagner e91ed42
Wrap FTS as `Interpolated(fts)` instead of dispatching on FieldTimeSe…
glwagner ee94c5c
Merge branch 'main' into eq/era5_breeze_land
glwagner 76756db
Refactor nesting: NestedModel handles parent sync, NestedSimulation i…
glwagner bdca8b5
Rename child_model → child_simulation; route through atmosphere_simul…
glwagner 84d23c3
Merge branch 'main' into eq/era5_breeze_land
glwagner 4bfd084
parent_variables: pair-dispatch + AbstractField source + NestedSimula…
glwagner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -68,3 +68,6 @@ CondaPkg.toml | |
| # claude | ||
| .claude | ||
| test/Manifest.toml | ||
|
|
||
| # Throwaway scratch scripts | ||
| scratch/ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| ##### | ||
| ##### child_simulation dispatch for Breeze.AtmosphereModel | ||
| ##### | ||
| # | ||
| # Two extensions to NumericalEarth.NestedSimulations: | ||
| # | ||
| # 1. `parent_variables(::Type{<:AtmosphereModel}, ::PrescribedAtmosphere)` — | ||
| # declares the (child, parent) pair mapping. Returns a NamedTuple naming | ||
| # the parent FTSs that drive the child's momentum BCs. | ||
| # | ||
| # Density convention (important): Breeze's prognostic is density-weighted | ||
| # momentum (ρu, ρv, ρw). The BC value Oceananigans writes into ρu at the | ||
| # boundary is exactly the value of the source we hand it; so the parent's | ||
| # velocity FTS slots are interpreted as **momentum** values (ρ̄·u). The user | ||
| # populating a `PrescribedAtmosphere` for use with Breeze is expected to | ||
| # pre-multiply by ρ̄ when calling `set!`. See [[nested-simulations-design]] | ||
| # and the deferred PrescribedAtmosphere refactor (issue #266) for the | ||
| # slot-naming followup. | ||
| # | ||
| # 2. `_build_child_model(::Type{<:AtmosphereModel}, grid; …)` — routes | ||
| # `child_simulation(AtmosphereModel, …)` through `atmosphere_simulation` | ||
| # instead of constructing `AtmosphereModel` directly, so Breeze's default | ||
| # advection / microphysics settings apply unless the user overrides them. | ||
|
|
||
| import NumericalEarth.EarthSystemModels.NestedSimulations: parent_variables, | ||
| _build_child_model | ||
|
|
||
| parent_variables(::Type{<:Breeze.AtmosphereModel}, parent::NumericalEarth.PrescribedAtmosphere) = | ||
| (ρu = parent.velocities.u, ρv = parent.velocities.v) | ||
|
|
||
| _build_child_model(::Type{<:Breeze.AtmosphereModel}, grid; kwargs...) = | ||
| NumericalEarth.Atmospheres.atmosphere_simulation(grid; kwargs...) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/EarthSystemModels/NestedSimulations/NestedSimulations.jl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| module NestedSimulations | ||
|
|
||
| export NestedModel, NestedSimulation, | ||
| parent_boundary_conditions, parent_forcings, | ||
| child_simulation, parent_variables | ||
|
|
||
| using Oceananigans | ||
| using Oceananigans.Fields: Field, Center, Face, instantiated_location | ||
| using Oceananigans.Forcings: Relaxation | ||
| using Oceananigans.Grids: AbstractGrid, xnode, ynode, znode | ||
| using Oceananigans.BoundaryConditions: OpenBoundaryCondition, FieldBoundaryConditions | ||
| using Oceananigans.OutputReaders: FieldTimeSeries | ||
| using Oceananigans.Simulations: Simulation | ||
| using Oceananigans.TimeSteppers: time_step! | ||
| using Oceananigans.Units: Time | ||
|
|
||
| include("nested_model.jl") | ||
| include("nested_simulation.jl") | ||
| include("interpolated_fts_boundary.jl") | ||
| include("parent_boundary_conditions.jl") | ||
| include("parent_forcings.jl") | ||
| include("child_simulation.jl") | ||
|
|
||
| end # module |
124 changes: 124 additions & 0 deletions
124
src/EarthSystemModels/NestedSimulations/child_simulation.jl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| ##### | ||
| ##### child_simulation: one-call construction of a parent-driven child model | ||
| ##### | ||
| # | ||
| # Despite the name (chosen for consistency with `ocean_simulation` / | ||
| # `atmosphere_simulation`), `child_simulation` returns an `AbstractModel` — the | ||
| # Breeze `atmosphere_simulation` helper returns a model, not a `Simulation`, | ||
| # and we follow that convention so the call sites compose cleanly: | ||
| # | ||
| # child = child_simulation(modeltype, grid, parent; …) | ||
| # nested = NestedSimulation(parent, child; Δt, stop_time, …) | ||
| # | ||
| # Variable mapping (which parent FTS drives which child boundary) dispatches on | ||
| # `parent_variables(modeltype, parent)` — multi-dispatch on the (child, parent) | ||
| # pair. Methods live wherever both types are visible (often a package | ||
| # extension). Cross-realm pairs (e.g. ocean child × atmosphere parent) are | ||
| # intentionally undefined; the generic fallback throws an explanatory | ||
| # `ArgumentError` and the user must pass `variables=` explicitly to override. | ||
| # | ||
| # The underlying model constructor is selected by `_build_child_model(modeltype, grid; …)` | ||
| # — also extended via package extensions for new model types. | ||
|
|
||
| """ | ||
| parent_variables(child_modeltype, parent) → NamedTuple | ||
|
|
||
| Return the `child_variable_name => interpolatable_source` mapping naming | ||
| the parent fields that drive each child boundary, where the source may be | ||
| a `FieldTimeSeries` (prescribed parents) or an `AbstractField` (prognostic | ||
| parents). | ||
|
|
||
| Dispatch on the **(child_modeltype, parent_type)** pair — define a method | ||
| per pair, in whichever package can see both types. The generic fallback | ||
| errors so cross-realm pairs (e.g. an ocean child with an atmosphere parent) | ||
| fail loudly at construction time. Users can always sidestep by passing | ||
| `variables=` directly to [`child_simulation`](@ref). | ||
|
|
||
| The exact contents of the NamedTuple — what fields, whether they are | ||
| density-weighted, etc. — are determined by what the child consumes and what | ||
| the parent provides. For example, Breeze `AtmosphereModel` consumes | ||
| prognostic momentum (`ρu`, `ρv`, …), so a `PrescribedAtmosphere` parent | ||
| populated for that use case must store ρ·u in its velocity slots; the | ||
| method body documents the convention. | ||
| """ | ||
| function parent_variables end | ||
|
|
||
| # Generic fallback — errors with a message that explains the situation and | ||
| # suggests the two ways out. | ||
| function parent_variables(child_modeltype, parent) | ||
| throw(ArgumentError(string( | ||
| "No `parent_variables` mapping is defined for ", | ||
| "child=$(child_modeltype) with parent=$(typeof(parent)).\n", | ||
| "\n", | ||
| "Either:\n", | ||
| " • define `parent_variables(::Type{<:$(child_modeltype)}, ", | ||
| "parent::$(typeof(parent)))` in the package where both types are visible, or\n", | ||
| " • pass `variables = (...)` explicitly to `child_simulation`.\n", | ||
| "\n", | ||
| "Note: cross-realm nesting (e.g. an ocean child driven by an atmosphere ", | ||
| "parent) is intentionally unsupported as a default — the variable units ", | ||
| "do not match. Define a custom mapping only if you own the semantics." | ||
| ))) | ||
| end | ||
|
|
||
| """ | ||
| _build_child_model(modeltype, grid; kwargs...) | ||
|
|
||
| Construct the child model. Defaults to `modeltype(grid; kwargs...)`; package | ||
| extensions override this for specific model types (e.g. the Breeze ext routes | ||
| `AtmosphereModel` through `atmosphere_simulation` so Breeze's default | ||
| advection / microphysics apply). | ||
| """ | ||
| _build_child_model(modeltype, grid; kwargs...) = modeltype(grid; kwargs...) | ||
|
|
||
| """ | ||
| child_simulation(modeltype, grid, parent; | ||
| sides = (:west, :east, :south, :north), | ||
| schemes = NamedTuple(), | ||
| variables = parent_variables(modeltype, parent), | ||
| relaxation_rate = nothing, | ||
| relaxation_mask = 1, | ||
| model_kwargs...) | ||
|
|
||
| Construct a child model (of type `modeltype`) on `grid` with Open boundary | ||
| conditions driven by `parent`. Returns the constructed `AbstractModel`. | ||
|
|
||
| `parent_boundary_conditions` wires the BCs from `variables`; `model_kwargs` | ||
| are forwarded to the underlying constructor (see `_build_child_model`). | ||
|
|
||
| If `relaxation_rate` is supplied, [`parent_forcings`](@ref) builds an | ||
| Oceananigans `Relaxation` per variable that nudges the child interior toward | ||
| the parent at rate `relaxation_rate`, weighted by `relaxation_mask` (default | ||
| uniform). Per-variable values are supported via `NamedTuple`s. The resulting | ||
| forcings are merged with any `forcing` already passed in `model_kwargs`. | ||
|
|
||
| Wrap with `NestedSimulation(parent, child; Δt, stop_time, …)` to integrate. | ||
| """ | ||
| function child_simulation(modeltype, grid, parent; | ||
| sides = (:west, :east, :south, :north), | ||
| schemes = NamedTuple(), | ||
| variables = parent_variables(modeltype, parent), | ||
| relaxation_rate = nothing, | ||
| relaxation_mask = 1, | ||
| model_kwargs...) | ||
|
|
||
| bcs = parent_boundary_conditions(grid; variables, sides, schemes) | ||
|
|
||
| # Pull any user-supplied `forcing` out so we can merge in the relaxation | ||
| # forcings on the same field names without colliding. | ||
| model_kwargs_nt = NamedTuple(model_kwargs) | ||
| user_forcing = get(model_kwargs_nt, :forcing, NamedTuple()) | ||
| rest_kwargs = Base.structdiff(model_kwargs_nt, NamedTuple{(:forcing,)}) | ||
|
|
||
| forcing = if relaxation_rate === nothing | ||
| user_forcing | ||
| else | ||
| relaxation = parent_forcings(; variables, rate = relaxation_rate, mask = relaxation_mask) | ||
| merge(user_forcing, relaxation) | ||
| end | ||
|
|
||
| return _build_child_model(modeltype, grid; | ||
| boundary_conditions = bcs, | ||
| forcing, | ||
| rest_kwargs...) | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.