-
Notifications
You must be signed in to change notification settings - Fork 201
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
Yet another ZStar implementation #3956
base: main
Are you sure you want to change the base?
Conversation
…jl into ss/new-bottom-height
Co-authored-by: Gregory L. Wagner <[email protected]>
@simone-silvestri @glwagner thanks for clarifying the use of the wrapper functions to set up the z-star coordinate. To finish the discussion point on the drift in tracer conservation, I ran the experiment not just for 1 year, but for 10 years. The conclusions are:
TLDR: I think we can feel good about the tracer conservation of the z-star coordinate. |
@NoraLoose another test could release the tracer at depth, then we can see if there is a specific issue with the surface or not. I sort of agree that this is completely negligible compared to a real signal so for all intents and purposes tracers are conserved. But we should still try to wrap our heads around this eventually I think. |
# We upwind the discrete divergence `δx(Ax u) + δy(Ay v)` and then divide by the volume, | ||
# therefore, the correct term to be added to the divergence transport due to the moving grid is: | ||
# | ||
# Azᶜᶜᶜ Δrᶜᶜᶜ ∂t_σ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what happens if the grid is stationary
∂t_σ = _symmetric_interpolate_xᶠᵃᵃ(i, j, k, grid, scheme, cross_scheme, Az_Δr_∂t_σ) | ||
|
||
return û * δᴿ | ||
return û * (δᴿ + ∂t_σ) | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
confused about this. It makes it look like we always have a moving grid
|
||
@inline function upwinded_divergence_flux_Uᶠᶜᶜ(i, j, k, grid, scheme::VectorInvariantSelfVerticalUpwinding, u, v) | ||
|
||
δU_stencil = scheme.upwinding.δU_stencil | ||
cross_scheme = scheme.upwinding.cross_scheme | ||
|
||
@inbounds û = u[i, j, k] | ||
δvˢ = _symmetric_interpolate_xᶠᵃᵃ(i, j, k, grid, scheme, cross_scheme, δy_V, u, v) | ||
δvˢ = _symmetric_interpolate_xᶠᵃᵃ(i, j, k, grid, scheme, cross_scheme, δy_V_plus_metric, u, v) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what metric?
generate_coordinate(FT, ::Periodic, N, H, ::ZStarVerticalCoordinate, coordinate_name, arch, args...) = | ||
throw(ArgumentError("Periodic domains are not supported for ZStarVerticalCoordinate")) | ||
|
||
# Generate a moving coordinate with evolving scaling (`σ`) for spacings and znodes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this function does what the comment says. A moving coordinate is presumably implemented by a model. The comment should be updated to state exactly what this function does. PResumably, it makes it possible to have a moving coordinate, somehow.
fill!(σ, 1) | ||
end | ||
|
||
return Lr, ZStarVerticalCoordinate(rᵃᵃᶠ, rᵃᵃᶜ, Δrᵃᵃᶠ, Δrᵃᵃᶜ, ηⁿ, σᶜᶜⁿ, σᶠᶜⁿ, σᶜᶠⁿ, σᶠᶠⁿ, σᶜᶜ⁻, ∂t_σ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why does this return two things
σᶠᶠⁿ :: FF | ||
σᶜᶜ⁻ :: CC | ||
∂t_σ :: CC | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this more general than z*? It's just a vertical coordinate that can evolve in time
or an `AbstractArray`. A `ZStarVerticalCoordinate` is a vertical coordinate that evolves in time | ||
following the surface. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think any code in Grids
has to do with a free surface. It must be the case that this code is more general than simply something that follows the free surface. I'm guessing that this is simply an abstraction for a vertical coordinate that can change in time, and it can change according to whatever rule a particular model wants to implement.
coordinate_summary(topo, z::StaticVerticalCoordinate, name) = coordinate_summary(topo, z.Δᵃᵃᶜ, name) | ||
|
||
coordinate_summary(::Bounded, z::RegularZStarVerticalCoordinate, name) = | ||
@sprintf("Free-surface following with Δr=%s", prettysummary(z.Δᵃᵃᶜ)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this has to follow a free surface. That is just a particular choice that is made in another module (HydrostaticFreeSurfaceModels)
@sprintf("Free-surface following with Δr=%s", prettysummary(z.Δᵃᵃᶜ)) | ||
|
||
coordinate_summary(::Bounded, z::ZStarVerticalCoordinate, name) = | ||
@sprintf("Free-surface following with min(Δr)=%s, max(Δr)=%s", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same comment as above
const ZStarImmersedGrid = ImmersedBoundaryGrid{<:Any, <:Any, <:Any, <:Any, <:AbstractZStarGrid} | ||
const ZStarGridOfSomeKind = Union{ZStarImmersedGrid, AbstractZStarGrid} | ||
|
||
@inline dynamic_column_depthᶜᶜᵃ(i, j, k, grid::ZStarGridOfSomeKind, η) = static_column_depthᶜᶜᵃ(i, j, grid) + @inbounds η[i, j, k] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@inline dynamic_column_depthᶜᶜᵃ(i, j, k, grid::ZStarGridOfSomeKind, η) = static_column_depthᶜᶜᵃ(i, j, grid) + @inbounds η[i, j, k] | |
@inline column_depthᶜᶜᵃ(i, j, k, grid::ZStarGridOfSomeKind, η) = static_column_depthᶜᶜᵃ(i, j, grid) + @inbounds η[i, j, k] |
This is just the "column depth" isn't it? The concept of dynamic vs static doesn't make sense for a fixed grid. Better to refer to this as something that generalizes to all grids. Then we have "static column depth" for the specific case that it's needed.
Hᶜᶠ = dynamic_column_depthᶜᶠᵃ(i, j, k_top, grid, η) | ||
|
||
σᶠᶜ = ifelse(hᶠᶜ == 0, one(grid), Hᶠᶜ / hᶠᶜ) | ||
σᶜᶠ = ifelse(hᶜᶠ == 0, one(grid), Hᶜᶠ / hᶜᶠ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is this 1 for the case that hits the conditional?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking that a zero dz
might create some NaNs where we don't want them. This assumes that if the height is zero (which basically means that we do not have active cells in the column) we revert to a z-coordinate representation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's good to define that behavior. Maybe we will learn more about the implications when we have tests. Should we make a comment about it?
@@ -18,12 +19,38 @@ compute_w_from_continuity!(model; kwargs...) = | |||
compute_w_from_continuity!(velocities, arch, grid; parameters = w_kernel_parameters(grid)) = | |||
launch!(arch, grid, parameters, _compute_w_from_continuity!, velocities, grid) | |||
|
|||
###################################################### | |||
# The derivative of the moving grid is: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment says something about a moving grid, but I think the code here is general and also works for static grids. This comment is very confusing, it will make people think that we always have a moving grid
function update_grid!(model, grid::ZStarGridOfSomeKind; parameters = :xy) | ||
|
||
# Scalings and free surface | ||
σᶜᶜ⁻ = grid.z.σᶜᶜ⁻ | ||
σᶜᶜⁿ = grid.z.σᶜᶜⁿ | ||
σᶠᶜⁿ = grid.z.σᶠᶜⁿ | ||
σᶜᶠⁿ = grid.z.σᶜᶠⁿ | ||
σᶠᶠⁿ = grid.z.σᶠᶠⁿ | ||
∂t_σ = grid.z.∂t_σ | ||
ηⁿ = grid.z.ηⁿ | ||
η = model.free_surface.η |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code only works for a HydrostaticFreeSurfaceModel?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this be time_variable_grid_operators.jl
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just "variable" grid is confusing because one might interpret this as referring to a stretched grid.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what are the results of this test?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It calculates the drift in a lock-exchange simulation with an sloping immersed boundary.
The drift stays within 3e-16
julia> @show drift
drift = Any[0.0, -1.734723475976807e-16, -2.1510571102112408e-16, -3.469446951953614e-17, -8.326672684688674e-17, -1.0408340855860843e-16, -6.245004513516506e-17, -1.942890293094024e-16, -2.5673907444456745e-16, -1.734723475976807e-16, -9.020562075079397e-17, 4.163336342344337e-17, -2.498001805406602e-16, -1.1796119636642288e-16, -2.498001805406602e-16, -2.7755575615628914e-17, -1.5959455978986625e-16, -1.457167719820518e-16, -4.163336342344337e-17, -2.3592239273284576e-16, -2.0816681711721685e-16, -8.326672684688674e-17, -1.5959455978986625e-16, -1.942890293094024e-16, -1.734723475976807e-16, -1.8735013540549517e-16, 6.938893903907228e-18, -1.3877787807814457e-17, -3.608224830031759e-16, -1.8735013540549517e-16, -1.3183898417423734e-16, -2.498001805406602e-16, -1.3183898417423734e-16, -2.914335439641036e-16, -1.457167719820518e-16, -3.0531133177191805e-16, -9.020562075079397e-17, -3.0531133177191805e-16, -1.1102230246251565e-16, -1.5959455978986625e-16, -6.245004513516506e-17, -7.632783294297951e-17, -5.551115123125783e-17, -2.0122792321330962e-16, -1.942890293094024e-16, -7.632783294297951e-17, -1.942890293094024e-16, -6.245004513516506e-17, -1.1102230246251565e-16, -1.249000902703301e-16, -1.942890293094024e-16, -1.8735013540549517e-16, -1.5265566588595902e-16, -2.5673907444456745e-16, -1.0408340855860843e-16, -1.0408340855860843e-16, -1.5265566588595902e-16, -1.3183898417423734e-16, -9.71445146547012e-17, -2.2898349882893854e-16, -5.551115123125783e-17, -3.469446951953614e-17]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The names and comments need to be changed to reflect the notions that:
- grids can be static. Where time-depednent metrics enter, we need a comment / explanation for what happens when the grid is not changing in time
- The data structures for the grid are likely general for any time-dependent vertical coordinate
- The "z*" approximation is a physical / modeling approximation that is independent of the data structures. Specifically, this is a rule for updating the time-dependent grid, plus an approximation that neglects some slope terms, assuming the free surface displacement is small compared to the depth of the domain. However, the "z*" prescription and data structures are properly viewed as independent. This is important for understanding how the code works, plus also for grasping how one would implement other vertical coordinates.
Co-authored-by: Gregory L. Wagner <[email protected]>
Co-authored-by: Gregory L. Wagner <[email protected]>
Co-authored-by: Gregory L. Wagner <[email protected]>
Refactoring
Following #3411 it is clear that the grids require some refactor to allow ZStar.
This PR implements another proposal for ZStar which hinges on changing the grids to have just one
z
field.The case of a static z (the only allowed in Oceananigans at the moment) is covered with a
StaticVerticalCoordinate
type that contains all the specifications that were in the grid before:Oceananigans.jl/src/Grids/vertical_coordinate.jl
Lines 14 to 19 in 9e05e21
This refactor should change nothing in the user interface nor the internals of the code, since the whole API should depend on
znode
andΔz
, but just reorganizes the vertical coordinate in a compact type that can be modified.ZStar Implementation
With this refactor, a ZStar coordinate is implemented through a
ZStarVerticalCoordinate
typeOceananigans.jl/src/Grids/vertical_coordinate.jl
Lines 36 to 48 in 9e05e21
e₃
is the vertical scaling of the grid spacing defined aswith$z$ the actual spatial coordinate (moving with the free surface) and $r$ is the reference vertical coordinate also called $z^\star$ (equivalent to a static coordinate, or $z$ when $\eta = 0$ ).
ηⁿ
is the value of the free surface at the current time step, needed to calculateznodes
Finally$H$ is the output of $z$ properties ($r$ ($z$ properties for a zstar vertical coordinate are calculated appropriately in the Operators module (https://github.com/CliMA/Oceananigans.jl/blob/ss/new-zstar/src/Operators/variable_grid_operators.jl)
static_column_depth
.As a consequence of the definitions, all
znodes
,zspacings
,Δz
, etc...) are replaced withrnodes
,rspacings
,Δr
, etc...) and theAn example of the proposed user interface is shown in the
validation/z_star_coordinate
examplesChanges in the model timestepping
Another requirement is changing the internals of the
HydrostaticFreeSurfaceModel
to solve the correct equations, in particular, the notable changes are:(1) Updating the grid at each timestep in the
update_state!
step(2) including the gradient of the grid in the momentum equations, calculated as:
Oceananigans.jl/src/Models/HydrostaticFreeSurfaceModels/z_star_vertical_spacing.jl
Lines 85 to 100 in 9e05e21
(3) Changing the computation of the vertical velocity to include the movement of the grid
Oceananigans.jl/src/Models/HydrostaticFreeSurfaceModels/compute_w_from_continuity.jl
Lines 22 to 35 in 9e05e21
(4) Advancing$e_3\theta$ instead of $\theta$ in the tracer equations and subsequently unscale it back after the grid scaling factor at the new time step $e_3^{n+1}$ is known
Oceananigans.jl/src/Models/HydrostaticFreeSurfaceModels/hydrostatic_free_surface_ab2_step.jl
Lines 111 to 128 in 9e05e21
(5) Adding a non-linear free surface by changing the
static_column_depth
in the split explicit free surface implementation to adynamic_column_depth
defined asOceananigans.jl/src/ImmersedBoundaries/zstar_immersed_grid.jl
Lines 14 to 17 in 9e05e21
Note: these changes are valid only for the QuasiAdamsBashort2 timestepper. Support for the SplitRungeKutta3 timestepper requires a bit more work and can be done when all the infrastructure is in place
OutputWriters?
The last piece of the puzzle (not implemented in this PR) would be to change the
OutputWriters
to include by default the variable grid properties in thetimeseries
field of the jld2 / netcdf writer.Possibility for improvements
I got a bit carried away and completed a working implementation to make sure it could be done this way, but I am happy to change just about everything in here. I would like to know what people think about this implementation and what people suggest especially in terms of
(1) Implementation
(2) Variable naming
I can also split this PR in a couple of ones, probably one that includes the refactor to the grids (implementation of a
StaticVerticalCoordinate
) and one that implements a working version of aZStarVerticalCoordinate
.