Skip to content

Commit

Permalink
Fix reading/writing purely vertical spaces
Browse files Browse the repository at this point in the history
This previously was not supported
  • Loading branch information
Sbozzolo committed Dec 18, 2024
1 parent 6ad8294 commit 8cc1b50
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 11 deletions.
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ ClimaCore.jl Release Notes
main
-------


- We've added new convenience constructors for spaces PR [2082](https://github.com/CliMA/ClimaCore.jl/pull/2082). Here are links to the new constructors:
- [ExtrudedCubedSphereSpace]()
- [CubedSphereSpace]()
Expand All @@ -12,7 +13,12 @@ main
- [SliceXZSpace]()
- [RectangleXYSpace]()

### ![][badge-🐛bugfix] Bug fixes

- Fixed writing/reading purely vertical spaces

v0.14.20
--------

- We've added new convenience constructors for grids PR [1848](https://github.com/CliMA/ClimaCore.jl/pull/1848). Here are links to the new constructors:
- [ExtrudedCubedSphereGrid](https://github.com/CliMA/ClimaCore.jl/blob/cbb193042fac3b4bef33251fbc0f232427bfe506/src/CommonGrids/CommonGrids.jl#L85-L144)
Expand Down
13 changes: 10 additions & 3 deletions src/InputOutput/readers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -152,17 +152,19 @@ function _scan_data_layout(layoutstring::AbstractString)
"IFH",
"IHF",
"IF",
"VF",
"VIJFH",
"VIJHF",
"VIFH",
"VIHF",
)
) "datalayout is $layoutstring"
layoutstring == "IJFH" && return DataLayouts.IJFH
layoutstring == "IJHF" && return DataLayouts.IJHF
layoutstring == "IJF" && return DataLayouts.IJF
layoutstring == "IFH" && return DataLayouts.IFH
layoutstring == "IHF" && return DataLayouts.IHF
layoutstring == "IF" && return DataLayouts.IF
layoutstring == "VF" && return DataLayouts.VF
layoutstring == "VIJFH" && return DataLayouts.VIJFH
layoutstring == "VIJHF" && return DataLayouts.VIJHF
return DataLayouts.VIFH
Expand Down Expand Up @@ -482,8 +484,10 @@ function read_field(reader::HDF5Reader, name::AbstractString)
topology = Spaces.topology(space)
ArrayType = ClimaComms.array_type(topology)
data_layout = attrs(obj)["data_layout"]
has_horizontal = occursin('I', data_layout)
DataLayout = _scan_data_layout(data_layout)
h_dim = DataLayouts.h_dim(DataLayouts.singleton(DataLayout))
has_horizontal &&
(h_dim = DataLayouts.h_dim(DataLayouts.singleton(DataLayout)))
if topology isa Topologies.Topology2D
nd = ndims(obj)
localidx =
Expand All @@ -492,7 +496,7 @@ function read_field(reader::HDF5Reader, name::AbstractString)
else
data = ArrayType(read(obj))
end
Nij = size(data, findfirst("I", data_layout)[1])
has_horizontal && (Nij = size(data, findfirst("I", data_layout)[1]))
# For when `Nh` is added back to the type space
# Nhd = Nh_dim(data_layout)
# Nht = Nhd == -1 ? () : (size(data, Nhd),)
Expand All @@ -501,6 +505,9 @@ function read_field(reader::HDF5Reader, name::AbstractString)
Nv = size(data, 1)
# values = DataLayout{ElType, Nv, Nij, Nht...}(data) # when Nh is in type-domain
values = DataLayout{ElType, Nv, Nij}(data)
elseif data_layout in ("VF",)
Nv = size(data, 1)
values = DataLayout{ElType, Nv}(data)
else
# values = DataLayout{ElType, Nij, Nht...}(data) # when Nh is in type-domain
values = DataLayout{ElType, Nij}(data)
Expand Down
49 changes: 49 additions & 0 deletions test/InputOutput/unit_finitedifference.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using Test
import ClimaCore

using ClimaComms
const comms_ctx = ClimaComms.context(ClimaComms.CPUSingleThreaded())
pid, nprocs = ClimaComms.init(comms_ctx)
filename = ClimaComms.bcast(comms_ctx, tempname(pwd()))
if ClimaComms.iamroot(comms_ctx)
@info "Comms context" comms_ctx nprocs filename
end

@testset "HDF5 restart test for 1d finite difference space" begin
FT = Float32

z_min = FT(0)
z_max = FT(30e3)
z_elem = 10
center_staggering = ClimaCore.Grids.CellCenter()
face_staggering = ClimaCore.Grids.CellFace()

center_space = ClimaCore.CommonSpaces.ColumnSpace(;
z_min,
z_max,
z_elem,
staggering = center_staggering,
)

face_space = ClimaCore.CommonSpaces.ColumnSpace(;
z_min,
z_max,
z_elem,
staggering = face_staggering,
)

center_field = Fields.local_geometry_field(center_space)
face_field = Fields.local_geometry_field(face_space)

Y = ClimaCore.Fields.FieldVector(; c = center_field, f = face_field)

# write field vector to hdf5 file
writer = ClimaCore.InputOutput.HDF5Writer(filename, comms_ctx)
ClimaCore.InputOutput.write!(writer, Y, "Y")
close(writer)

reader = ClimaCore.InputOutput.HDF5Reader(filename, comms_ctx)
restart_Y = ClimaCore.InputOutput.read_field(reader, "Y") # read fieldvector from hdf5 file
close(reader)
@test restart_Y == Y # test if restart is exact
end
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
17 changes: 9 additions & 8 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,15 @@ UnitTest("Hypsography - 2d" ,"Hypsography/2d.jl"),
UnitTest("Hypsography - 3d sphere" ,"Hypsography/3dsphere.jl"),
UnitTest("Remapping" ,"Operators/remapping.jl"),
UnitTest("Limiter" ,"Limiters/limiter.jl"),
UnitTest("InputOutput - hdf5" ,"InputOutput/hdf5.jl"),
UnitTest("InputOutput - spectralelement2d" ,"InputOutput/spectralelement2d.jl"),
UnitTest("InputOutput - hybrid2dbox" ,"InputOutput/hybrid2dbox.jl"),
UnitTest("InputOutput - hybrid2dbox_topography" ,"InputOutput/hybrid2dbox_topography.jl"),
UnitTest("InputOutput - hybrid2dbox_stretched" ,"InputOutput/hybrid2dbox_stretched.jl"),
UnitTest("InputOutput - hybrid3dbox" ,"InputOutput/hybrid3dbox.jl"),
UnitTest("InputOutput - hybrid3dcubedsphere" ,"InputOutput/hybrid3dcubedsphere.jl"),
UnitTest("InputOutput - hybrid3dcubedsphere_topo" ,"InputOutput/hybrid3dcubedsphere_topography.jl"),
UnitTest("InputOutput - hdf5" ,"InputOutput/unit_hdf5.jl"),
UnitTest("InputOutput - spectralelement2d" ,"InputOutput/unit_spectralelement2d.jl"),
UnitTest("InputOutput - hybrid2dbox" ,"InputOutput/unit_hybrid2dbox.jl"),
UnitTest("InputOutput - hybrid2dbox_topography" ,"InputOutput/unit_hybrid2dbox_topography.jl"),
UnitTest("InputOutput - hybrid2dbox_stretched" ,"InputOutput/unit_hybrid2dbox_stretched.jl"),
UnitTest("InputOutput - hybrid3dbox" ,"InputOutput/unit_hybrid3dbox.jl"),
UnitTest("InputOutput - hybrid3dcubedsphere" ,"InputOutput/unit_hybrid3dcubedsphere.jl"),
UnitTest("InputOutput - hybrid3dcubedsphere_topo" ,"InputOutput/unit_hybrid3dcubedsphere_topography.jl"),
UnitTest("InputOutput - finitedifferences" ,"InputOutput/unit_finitedifference.jl"),
UnitTest("Array interpolation" ,"Remapping/interpolate_array.jl"),
UnitTest("Array interpolation" ,"Remapping/distributed_remapping.jl"),
UnitTest("Aqua" ,"aqua.jl"),
Expand Down

0 comments on commit 8cc1b50

Please sign in to comment.