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

A fast netcdf output writer #433

Merged
merged 23 commits into from
Oct 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
0fe221a
Introduces machinary for getting dimensions from fields
suyashbire1 Sep 28, 2019
28af45c
Introduces a new netcdf outputwriter
suyashbire1 Sep 28, 2019
5e2e910
A working netcdf output writer!
suyashbire1 Sep 28, 2019
04ac85b
Adds comments
suyashbire1 Sep 28, 2019
bcf9b7b
Consolidates NetCDFOutputwriter in output_writer.jl
suyashbire1 Sep 29, 2019
3966930
Moves dims from fields to output_writers
suyashbire1 Sep 29, 2019
2e637c6
Provides a simple example of new netcdfoutputwriter
suyashbire1 Sep 29, 2019
ad2763a
Some cleanup to the example
suyashbire1 Sep 29, 2019
2bc7426
Adds support for attributes, some testing, improves example
suyashbire1 Sep 30, 2019
b16197f
Changes to slice, needs to be reviewed
suyashbire1 Sep 30, 2019
55b04e5
Restricts face diemensions which solves the size mismatch due to halo…
suyashbire1 Sep 30, 2019
2f26c9a
NetCDFoutputwriter tests now pass!
suyashbire1 Sep 30, 2019
a543373
Implements a test for sliced output
suyashbire1 Sep 30, 2019
83e16f1
Can now specify integers instead of slices, this allows getting sections
suyashbire1 Sep 30, 2019
4393817
Testing of integer slices
suyashbire1 Sep 30, 2019
9e7e09f
Replaces get_dtype_array function with the base.eltype
suyashbire1 Oct 1, 2019
4a1d80b
Improves docstrings
suyashbire1 Oct 1, 2019
728ce9a
Small change to the docstring
suyashbire1 Oct 1, 2019
c4cc074
Replaces OWClose with close, removes Netcdf.jl dep
suyashbire1 Oct 2, 2019
88625d8
Merge branch 'master' into outputwriters_with_assoc_dimensions
suyashbire1 Oct 21, 2019
15edb32
Fixes regression tests
suyashbire1 Oct 21, 2019
163a538
Minor fixes
suyashbire1 Oct 21, 2019
3f772d9
Closes netcdf file in tests
suyashbire1 Oct 21, 2019
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
29 changes: 17 additions & 12 deletions Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ git-tree-sha1 = "62847acab40e6855a9b5905ccb99c2b5cf6b3ebb"
uuid = "fa961155-64e5-5f13-b03f-caf6b980ea82"
version = "0.2.0"

[[CFTime]]
deps = ["Dates", "Printf"]
git-tree-sha1 = "90eddf050a4d4b35520f87b9ba82302643706a1e"
uuid = "179af706-886a-5703-950a-314cd64e0468"
version = "0.0.3"

[[CSTParser]]
deps = ["Tokenize"]
git-tree-sha1 = "c69698c3d4a7255bc1b4bc2afc09f59db910243b"
Expand Down Expand Up @@ -135,12 +141,6 @@ git-tree-sha1 = "8fba6ddaf66b45dec830233cea0aae43eb1261ad"
uuid = "1a297f60-69ca-5386-bcde-b61e274b549b"
version = "0.6.4"

[[Formatting]]
deps = ["Printf"]
git-tree-sha1 = "aef8983c0f77c84ae77ced1bb3ecf5ab7d687700"
uuid = "59287772-0a20-5a39-b81b-1366585eb4c0"
version = "0.4.0"

[[GPUArrays]]
deps = ["Adapt", "FFTW", "FillArrays", "LinearAlgebra", "Printf", "Random", "Serialization", "StaticArrays", "Test"]
git-tree-sha1 = "77e27264276fe97a7e7fb928bf8999a145abc018"
Expand Down Expand Up @@ -198,21 +198,26 @@ version = "0.5.1"
deps = ["Base64"]
uuid = "d6f4376e-aef5-505a-96c1-9c027394607a"

[[Missings]]
git-tree-sha1 = "29858ce6c8ae629cf2d733bffa329619a1c843d0"
uuid = "e1d29d7a-bbdc-5cf2-9ac0-f12de2c33e28"
version = "0.4.2"

[[Mmap]]
uuid = "a63ad114-7e13-5084-954f-fe012c677804"

[[NCDatasets]]
deps = ["BinDeps", "CFTime", "Compat", "CondaBinDeps", "DataStructures", "Dates", "Libdl", "Missings", "Printf", "Random"]
git-tree-sha1 = "21f833cf06f4e0128726b25aa2fc662b7d66d1bd"
uuid = "85f8d34a-cbdd-5861-8df4-14fed0d494ab"
version = "0.9.3"

[[NNlib]]
deps = ["Libdl", "LinearAlgebra", "Requires", "Statistics", "TimerOutputs"]
git-tree-sha1 = "0c667371391fc6bb31f7f12f96a56a17098b3de8"
uuid = "872c559c-99b0-510c-b3b7-b6c96a88d5cd"
version = "0.6.0"

[[NetCDF]]
deps = ["BinDeps", "CondaBinDeps", "Formatting", "Libdl"]
git-tree-sha1 = "fce0c7962fb14b8cf2f33ea77885f3b7c7e1485e"
uuid = "30363a11-5582-574a-97bb-aa9a979735b9"
version = "0.8.0"

[[OffsetArrays]]
git-tree-sha1 = "1af2f79c7eaac3e019a0de41ef63335ff26a0a57"
uuid = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ FFTW = "7a1cc6ca-52ef-59f5-83cd-3a7055c09341"
GPUifyLoops = "ba82f77b-6841-5d2e-bd9f-4daf811aec27"
JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
NetCDF = "30363a11-5582-574a-97bb-aa9a979735b9"
NCDatasets = "85f8d34a-cbdd-5861-8df4-14fed0d494ab"
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
Expand Down
78 changes: 78 additions & 0 deletions examples/netcdf_ouput_example.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using Oceananigans, Printf

####
#### Model set-up
####

Nx, Ny, Nz = 16, 16, 16 # No. of grid points in x, y, and z, respectively.
Lx, Ly, Lz = 100, 100, 100 # Length of the domain in x, y, and z, respectively (m).
tf = 5000 # Length of the simulation (s)

model = Model(grid=RegularCartesianGrid(N=(Nx, Ny, Nz), L=(Lx, Ly, Lz)),
closure=ConstantIsotropicDiffusivity())

# Add a cube-shaped warm temperature anomaly that takes up the middle 50%
# of the domain volume.
i1, i2 = round(Int, Nx/4), round(Int, 3Nx/4)
j1, j2 = round(Int, Ny/4), round(Int, 3Ny/4)
k1, k2 = round(Int, Nz/4), round(Int, 3Nz/4)
model.tracers.T.data[i1:i2, j1:j2, k1:k2] .+= 0.01

####
#### Set up output
####

write_grid(model)

outputs = Dict("u" => model.velocities.u,
"v" => model.velocities.v,
"w" => model.velocities.w,
"T" => model.tracers.T,
"S" => model.tracers.S)

outputattrib = Dict("u" => ["longname" => "Velocity in the x-direction", "units" => "m/s"],
"v" => ["longname" => "Velocity in the y-direction", "units" => "m/s"],
"w" => ["longname" => "Velocity in the z-direction", "units" => "m/s"],
"T" => ["longname" => "Temperature", "units" => "K"],
"S" => ["longname" => "Salinity", "units" => "g/kg"])

globalattrib = Dict("f" => 1e-4, "name" => "Thermal bubble expt 1")

# The following writer saves a yz plane at xC[5] for all fields that
# have xC as their dimension and at xF[6] for all fields that have xF
# as their dimension. Ranges also can be specified (e.g. xC=2:10)
subsetwriter = NetCDFOutputWriter(model, outputs;
interval=10, filename="dump_subset.nc",
outputattrib=outputattrib,
globalattrib=globalattrib,
xC=5, xF=6)
push!(model.output_writers, subsetwriter)

# The following writer saves a data from the entire domain
globalwriter = NetCDFOutputWriter(model, outputs, interval=10,
filename="dump_global.nc")
push!(model.output_writers, globalwriter)

####
#### Run the simulation
####

function terse_message(model, walltime, Δt)
cfl = Δt / Oceananigans.cell_advection_timescale(model)
return @sprintf("i: %d, t: %.4f s, Δt: %.1f s, cfl: %.3f, wall time: %s\n",
model.clock.iteration, model.clock.time, Δt, cfl, prettytime(walltime))
end

# A wizard for managing the simulation time-step.
wizard = TimeStepWizard(cfl=0.2, Δt=10.0, max_change=1.1, max_Δt=50.0)

# Run the model
while model.clock.time < tf
update_Δt!(wizard, model)
walltime = @elapsed time_step!(model, 10, wizard.Δt)
@printf "%s" terse_message(model, walltime, wizard.Δt)
end

# Close the NetCDFOutputWriter
OWClose(subsetwriter)
OWClose(globalwriter)
Comment on lines +77 to +78
Copy link
Member

Choose a reason for hiding this comment

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

This is fine for now but I wonder if there's a way to automatically close NetCDF files so the user doesn't have to worry about this stuff.

Unfortunately Julia doesn't have destructors.

Copy link
Member

Choose a reason for hiding this comment

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

Huh nevermind, Julia does have finalizers:

  finalizer(f, x)

Register a function f(x) to be called when there are no program-accessible
references to x, and return x. The type of x must be a mutable struct,
otherwise the behavior of this function is unpredictable.

but not sure if this would be a proper use of them. See JuliaLang/julia#11207

Copy link
Member

Choose a reason for hiding this comment

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

It looks like this function should also be changed to snake_case? Let's make sure we use the proper style for all new functions in this PR.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don't understand what finalizers/destructors do. Sounds like garbage collection.

Anyway, we already have a list of outputwriters. Could we iterate over it and have close(outputwriter) in a try catch block at the end of the run (may be at the end of the proposed function that will replace the time-step loop)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It looks like this function should also be changed to snake_case? Let's make sure we use the proper style for all new functions in this PR.

Done.

4 changes: 2 additions & 2 deletions src/Oceananigans.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ export
Model, BasicModel, ChannelModel, BasicChannelModel,

# Model output writers
NetCDFOutputWriter,
Checkpointer, restore_from_checkpoint, read_output,
JLD2OutputWriter, FieldOutput, FieldOutputs,
write_grid, NetCDFOutputWriter,

# Model diagnostics
HorizontalAverage, NaNChecker,
Expand All @@ -77,7 +77,7 @@ using
StaticArrays,
OffsetArrays,
JLD2,
NetCDF
NCDatasets

import
CUDAapi,
Expand Down
Loading