-
Notifications
You must be signed in to change notification settings - Fork 282
Add FieldTimeSeries round-trip test harness across writers and grids #5654
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
glwagner
wants to merge
2
commits into
main
Choose a base branch
from
glw/fts-round-trip-tests
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.
+138
−0
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,137 @@ | ||
| include("dependencies_for_runtests.jl") | ||
|
|
||
| using Oceananigans.Grids: topology | ||
| using Oceananigans.OrthogonalSphericalShellGrids: TripolarGrid, ConformalCubedSpherePanelGrid | ||
|
|
||
| # Load only the writer extension packages needed for the selected writers, so a | ||
| # subset run (e.g. FTS_ROUND_TRIP_WRITERS=jld2) needs neither Zarr nor NCDatasets. | ||
| const FTS_ROUND_TRIP_WRITER_NAMES = split(get(ENV, "FTS_ROUND_TRIP_WRITERS", "jld2,netcdf,zarr"), ",") | ||
| "netcdf" in FTS_ROUND_TRIP_WRITER_NAMES && @eval using NCDatasets | ||
| "zarr" in FTS_ROUND_TRIP_WRITER_NAMES && @eval using Zarr | ||
|
|
||
| ##### | ||
| ##### Round-trip harness: write output, reconstruct a FieldTimeSeries, compare | ||
| ##### | ||
| ##### One axis is the grid (the four categories that lacked coverage: stretched | ||
| ##### rectilinear, immersed, stretched lat-lon, orthogonal spherical shell); the | ||
| ##### other axis is the output writer (JLD2, NetCDF, Zarr). Every pair is exercised | ||
| ##### with a plain `@test`; pairs the writer/reader cannot yet round-trip simply | ||
| ##### fail, which makes the remaining gaps explicit. | ||
| ##### | ||
|
|
||
| cᵢ(x, y, z) = exp(-(z / 10)^2) * cos(deg2rad(x)) * cos(deg2rad(y)) | ||
|
|
||
| # Grids span the four reconstruction-sensitive categories. Each is built small to | ||
| # keep CI cheap; tripolar uses sizes known to be well-behaved over a couple steps. | ||
| function round_trip_grids(arch) | ||
| stretched_z = [-1.0, -0.7, -0.4, -0.1, 0.0] | ||
|
|
||
| stretched_rectilinear = RectilinearGrid(arch, size=(4, 4, 4), | ||
| x=(0, 1), y=(0, 1), z=stretched_z, | ||
| topology=(Periodic, Periodic, Bounded)) | ||
|
|
||
| underlying_rectilinear = RectilinearGrid(arch, size=(4, 4, 4), | ||
| x=(0, 1), y=(0, 1), z=(-1, 0), | ||
| topology=(Periodic, Periodic, Bounded)) | ||
| immersed_rectilinear = ImmersedBoundaryGrid(underlying_rectilinear, GridFittedBottom((x, y) -> -0.5)) | ||
|
|
||
| stretched_z_deep = [-1000.0, -600.0, -300.0, -100.0, 0.0] | ||
| stretched_latlon = LatitudeLongitudeGrid(arch, size=(4, 4, 4), | ||
| longitude=(0, 60), latitude=(-10, 10), z=stretched_z_deep, | ||
| topology=(Bounded, Bounded, Bounded)) | ||
|
|
||
| tripolar = TripolarGrid(arch, size=(12, 10, 3), z=(-100, 0)) | ||
| immersed_tripolar = ImmersedBoundaryGrid(TripolarGrid(arch, size=(12, 10, 3), z=(-100, 0)), | ||
| GridFittedBottom((λ, φ) -> -50)) | ||
|
|
||
| cubed_sphere_panel = ConformalCubedSpherePanelGrid(arch, size=(8, 8, 3), z=(-100, 0), halo=(2, 2, 2)) | ||
|
|
||
| return [("stretched_rectilinear", stretched_rectilinear), | ||
| ("immersed_rectilinear", immersed_rectilinear), | ||
| ("stretched_latlon", stretched_latlon), | ||
| ("tripolar", tripolar), | ||
| ("immersed_tripolar", immersed_tripolar), | ||
| ("cubed_sphere_panel", cubed_sphere_panel)] | ||
| end | ||
|
|
||
| # Each writer needs its own filename convention and on-disk path to read back. | ||
| # Set ENV["FTS_ROUND_TRIP_WRITERS"] (e.g. "jld2" or "jld2,netcdf") to run a subset. | ||
| function round_trip_writers() | ||
| writers = [(name="jld2", Writer=JLD2Writer, filename=base -> base, path=base -> base * ".jld2"), | ||
| (name="netcdf", Writer=NetCDFWriter, filename=base -> base * ".nc", path=base -> base * ".nc"), | ||
| (name="zarr", Writer=ZarrWriter, filename=base -> base, path=base -> base * ".zarr")] | ||
| return filter(w -> w.name in FTS_ROUND_TRIP_WRITER_NAMES, writers) | ||
| end | ||
|
|
||
| function round_trip_model(grid) | ||
| underlying = grid isa ImmersedBoundaryGrid ? grid.underlying_grid : grid | ||
| if underlying isa RectilinearGrid | ||
| return NonhydrostaticModel(grid; tracers=:c) | ||
| else | ||
| free_surface = SplitExplicitFreeSurface(grid; substeps=10) | ||
| return HydrostaticFreeSurfaceModel(grid; free_surface, tracers=:c) | ||
| end | ||
| end | ||
|
|
||
| function grids_match(reconstructed, original) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here we test only size and other metadata of the grid. We do not test the coordinate values themselves, shouldn't we? Otherwise the stretching of coordinates will not be detected anywhere. |
||
| size(reconstructed) == size(original) || return false | ||
| topology(reconstructed) == topology(original) || return false | ||
| typeof(reconstructed).name === typeof(original).name || return false | ||
| if original isa ImmersedBoundaryGrid | ||
| typeof(reconstructed.underlying_grid).name === typeof(original.underlying_grid).name || return false | ||
| end | ||
| return true | ||
| end | ||
|
|
||
| # Masked cells come back as NaN on immersed grids, so compare NaN-to-NaN as equal. | ||
| function values_match(fts, snapshots; atol=1e-4) | ||
| length(fts.times) == length(snapshots) || return false | ||
| for k in eachindex(snapshots) | ||
| reconstructed = Array(interior(fts[k])) | ||
| reference = snapshots[k] | ||
| size(reconstructed) == size(reference) || return false | ||
| for (a, b) in zip(reconstructed, reference) | ||
| ((isnan(a) && isnan(b)) || isapprox(a, b; atol)) || return false | ||
| end | ||
| end | ||
| return true | ||
| end | ||
|
|
||
| function field_time_series_round_trips(grid_name, grid, writer_spec, arch) | ||
| base = "fts_round_trip_$(grid_name)_$(writer_spec.name)_$(typeof(arch))" | ||
| path = writer_spec.path(base) | ||
| isfile(path) && rm(path; force=true) | ||
| ispath(path) && rm(path; force=true, recursive=true) | ||
|
|
||
| model = round_trip_model(grid) | ||
| set!(model, c=cᵢ) | ||
|
|
||
| snapshots = Array{eltype(grid), 3}[] | ||
| sim = Simulation(model; Δt=1, stop_iteration=2) | ||
| sim.callbacks[:save] = Callback(s -> push!(snapshots, Array(interior(s.model.tracers.c))), IterationInterval(1)) | ||
| sim.output_writers[:writer] = writer_spec.Writer(model, (; c=model.tracers.c); | ||
| filename=writer_spec.filename(base), | ||
| schedule=IterationInterval(1), | ||
| overwrite_existing=true) | ||
| run!(sim) | ||
|
|
||
| fts = FieldTimeSeries(path, "c"; architecture=arch) | ||
| matches = grids_match(fts.grid, grid) && values_match(fts, snapshots) | ||
|
|
||
| isfile(path) && rm(path; force=true) | ||
| ispath(path) && rm(path; force=true, recursive=true) | ||
|
|
||
| return matches | ||
| end | ||
|
|
||
| @testset "FieldTimeSeries round-trip across writers and grids" begin | ||
| @info "Testing FieldTimeSeries round-trip across writers and grids..." | ||
| for arch in archs | ||
| for (grid_name, grid) in round_trip_grids(arch) | ||
| for writer_spec in round_trip_writers() | ||
| @info " $grid_name × $(writer_spec.name) [$(typeof(arch))]..." | ||
| @test field_time_series_round_trips(grid_name, grid, writer_spec, arch) | ||
| end | ||
| end | ||
| end | ||
| end | ||
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.
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.
Is that all that "stretched" means? Non-equidistant in z? Or am I missing something? That would make almost all configurations I came across "stretched"