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

Passes grid argument to NetCDFOutputWriter #3576

Merged
merged 25 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
48aa15c
Update netcdf_output_writer.jl
tomchor May 2, 2024
f0143e1
Add grid as property to NetCDFOutputWriter and use it during file ini…
glwagner May 3, 2024
fdee401
Merge branch 'main' into tc/grid-to-netcdfwriter
glwagner May 3, 2024
296ab22
paragraph explaining that netcdf writer also accepts an output grid
iuryt May 3, 2024
1ce9908
change wording and add jldoctest
iuryt May 6, 2024
fa97a72
Update docs/src/model_setup/output_writers.md
iuryt May 6, 2024
077fea6
Do a little better with function output for NetCDF output writer
glwagner May 6, 2024
c61729b
Fixes interpolate! plus some improvements to NetCDFOutputWriter
glwagner May 7, 2024
4134b5b
add new example for docs
iuryt May 7, 2024
ed5ce3e
simplify example
iuryt May 7, 2024
373f15a
simplify example in the docstring
iuryt May 7, 2024
6f0b9fb
Update docs/src/model_setup/output_writers.md
iuryt May 8, 2024
42ccd03
Make grid a kwarg and update language
glwagner May 8, 2024
8a7d7c0
Merge branch 'main' into tc/grid-to-netcdfwriter
glwagner May 8, 2024
14c94d8
Merge branch 'main' into tc/grid-to-netcdfwriter
tomchor May 8, 2024
70e459a
Update src/OutputWriters/netcdf_output_writer.jl
iuryt May 8, 2024
9403243
Update docs/src/model_setup/output_writers.md
iuryt May 8, 2024
45f0293
Update netcdf_output_writer.jl
iuryt May 8, 2024
8c141de
Bugfix plus better error message
glwagner May 8, 2024
c4357e5
Correctly define_output_variable for LagrangianParticles
glwagner May 8, 2024
4a3bd34
Merge branch 'main' into tc/grid-to-netcdfwriter
glwagner May 8, 2024
aa5e65c
Fix znodes in output writers docs
glwagner May 9, 2024
ab73354
fix znodes
iuryt May 9, 2024
a3a2f7e
fix double semicolon
iuryt May 9, 2024
4850ddc
14.5 kB
glwagner May 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 13 additions & 24 deletions docs/src/model_setup/output_writers.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,38 +163,27 @@ To use this functionality, the `output_grid` must be passed explicitly when cons

```jldoctest
using Oceananigans
using Oceananigans.Fields: interpolate!

grid = RectilinearGrid(size = (1, 1, 8), extent = (1,1,1));
model = NonhydrostaticModel(; grid, closure = ScalarDiffusivity(ν=1e-2))

set!(model, u=(x, y, z,) -> z)

simulation = Simulation(model,
Δt=0.5*maximum(zspacings(grid, Center())) / maximum(abs, model.velocities.u),
stop_time=20)

simulation.output_writers[:fullfields] = NetCDFOutputWriter(model, (; model.velocities.u),
filename = "fullfields.nc",
schedule = TimeInterval(5),
overwrite_existing = true,)
grid = RectilinearGrid(size=(1, 1, 8), extent=(1, 1, 1));
model = NonhydrostaticModel(; grid)
simulation = Simulation(model, Δt=1, stop_iteration=1)

coarse_grid = RectilinearGrid(size = (grid.Nx, grid.Ny, grid.Nz÷2), extent = (grid.Lx, grid.Ly, grid.Lz))
coarse_grid = RectilinearGrid(size=(grid.Nx, grid.Ny, grid.Nz÷2), extent=(grid.Lx, grid.Ly, grid.Lz))
coarse_u = Field{Face, Center, Center}(coarse_grid)

using Oceananigans.Fields: interpolate!
update_coarse_u(simulation) = interpolate!(coarse_u, simulation.model.velocities.u)
simulation.callbacks[:update_interp] = Callback(update_coarse_u)
interpolate_u(model) = interpolate!(coarse_u, model.velocities.u)
outputs = (; u = interpolate_u)

simulation.output_writers[:coarsefields] = NetCDFOutputWriter(model, (; coarse_u,), coarse_grid;
filename="coarsefields.nc",
schedule=TimeInterval(5),
overwrite_existing=true,)
simulation.output_writers[:coarse_u] = NetCDFOutputWriter(model, outputs, coarse_grid;
filename = "coarse_u.nc",
schedule = IterationInterval(1))
glwagner marked this conversation as resolved.
Show resolved Hide resolved

# output
NetCDFOutputWriter scheduled on TimeInterval(5 seconds):
├── filepath: ./coarsefields.nc
NetCDFOutputWriter scheduled on IterationInterval(1):
├── filepath: ./coarse_u.nc
├── dimensions: zC(4), zF(5), xC(1), yF(1), xF(1), yC(1), time(0)
├── 1 outputs: coarse_u
├── 1 outputs: u
└── array type: Array{Float64}
├── file_splitting: NoFileSplitting
└── file size: 14.6 KiB
Expand Down
35 changes: 33 additions & 2 deletions src/OutputWriters/netcdf_output_writer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ function add_schedule_metadata!(global_attributes, schedule::AveragedTimeInterva
end

"""
NetCDFOutputWriter(model, outputs; filename, schedule
NetCDFOutputWriter(model, outputs, grid; filename, schedule
dir = ".",
array_type = Array{Float64},
indices = nothing,
Expand All @@ -181,7 +181,7 @@ Construct a `NetCDFOutputWriter` that writes `(label, output)` pairs in `outputs
be a `Dict`) to a NetCDF file, where `label` is a string that labels the output and `output` is
either a `Field` (e.g. `model.velocities.u`) or a function `f(model)` that
returns something to be written to disk. Custom output requires the spatial `dimensions` (a
`Dict`) to be manually specified (see examples).
`Dict`) or `grid` to be manually specified (see examples).

Keyword arguments
=================
Expand Down Expand Up @@ -355,6 +355,37 @@ NetCDFOutputWriter scheduled on IterationInterval(1):
├── file_splitting: NoFileSplitting
└── file size: 17.8 KiB
```

`NetCDFOutputWriter` can also be configured for `outputs` that are interpolated or regridded to a different grid than `model.grid`.
To use this functionality, the `output_grid` must be passed explicitly when constructing `NetCDFOutputWriter` along with the regridded / interpolated `outputs`.

```jldoctest
using Oceananigans
using Oceananigans.Fields: interpolate!

grid = RectilinearGrid(size=(1, 1, 8), extent=(1, 1, 1));
model = NonhydrostaticModel(; grid)
simulation = Simulation(model, Δt=1, stop_iteration=1)

coarse_grid = RectilinearGrid(size=(grid.Nx, grid.Ny, grid.Nz÷2), extent=(grid.Lx, grid.Ly, grid.Lz))
coarse_u = Field{Face, Center, Center}(coarse_grid)

interpolate_u(model) = interpolate!(coarse_u, model.velocities.u)
outputs = (; u = interpolate_u)

simulation.output_writers[:coarse_u] = NetCDFOutputWriter(model, outputs, coarse_grid;
filename = "coarse_u.nc",
schedule = IterationInterval(1))

# output
NetCDFOutputWriter scheduled on IterationInterval(1):
├── filepath: ./coarse_u.nc
├── dimensions: zC(4), zF(5), xC(1), yF(1), xF(1), yC(1), time(0)
├── 1 outputs: u
└── array type: Array{Float64}
├── file_splitting: NoFileSplitting
└── file size: 14.6 KiB
```
"""
function NetCDFOutputWriter(model, outputs, grid=model.grid;
filename,
Expand Down