Skip to content

Commit 9295f23

Browse files
Sbozzolocharleskawczynski
authored andcommitted
Add face/center_space
As I started using the `CommonSpaces`, I realized that specifying the staggering was a pain point. It required importing `Grids`, and I pretty much always wanted to build CenterSpaces anyways. In addition to this, I realized that going from center to faces and viceversa was not trivial. This required carrying around both the center and the face space when only one is really needed. In this commit, I reexport the staggering in `CommonSpaces` and a function to go from center to face spaces and back.
1 parent 406bfeb commit 9295f23

File tree

6 files changed

+103
-22
lines changed

6 files changed

+103
-22
lines changed

NEWS.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ main
1818
- Fixed writing/reading purely vertical spaces. PR [2102](https://github.com/CliMA/ClimaCore.jl/pull/2102)
1919
- Fixed correctness bug in reductions on GPUs. PR [2106](https://github.com/CliMA/ClimaCore.jl/pull/2106)
2020

21+
### ![][badge-✨feature/enhancement] `face_space`, `center_space` functions
22+
23+
`ClimaCore.Spaces` now comes with two functions, `face_space` and
24+
`center_space`, to convert a `Space` from being cell-centered to be
25+
face-centered (and viceversa). These functions only work for vertical and
26+
extruded spaces.
27+
2128
v0.14.20
2229
--------
2330

src/CommonSpaces/CommonSpaces.jl

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,20 @@ argument, `staggering::Staggering` to construct the desired space.
88
module CommonSpaces
99

1010
export ExtrudedCubedSphereSpace,
11-
CubedSphereSpace, ColumnSpace, Box3DSpace, SliceXZSpace, RectangleXYSpace
11+
CubedSphereSpace,
12+
ColumnSpace,
13+
Box3DSpace,
14+
SliceXZSpace,
15+
RectangleXYSpace,
16+
CellCenter,
17+
CellFace
1218

13-
export Grids
1419
import ClimaComms
1520

1621
import ..DataLayouts,
1722
..Meshes, ..Topologies, ..Geometry, ..Domains, ..Quadratures, ..Grids
1823

19-
import ..Grids: Staggering
24+
import ..Grids: Staggering, CellCenter, CellFace
2025
import ..Spaces
2126
import ..CommonGrids
2227
import ..CommonGrids:
@@ -78,6 +83,19 @@ Note that these arguments are all the same as
7883
7984
# Example usage
8085
86+
```julia
87+
using ClimaCore.CommonSpaces
88+
space = ExtrudedCubedSphereSpace(;
89+
z_elem = 10,
90+
z_min = 0,
91+
z_max = 1,
92+
radius = 10,
93+
h_elem = 10,
94+
n_quad_points = 4
95+
staggering = CellCenter()
96+
)
97+
```
98+
This will construct a cell-center space. If you wish to create a face centered space:
8199
```julia
82100
using ClimaCore.CommonSpaces
83101
space = ExtrudedCubedSphereSpace(;
@@ -87,9 +105,10 @@ space = ExtrudedCubedSphereSpace(;
87105
radius = 10,
88106
h_elem = 10,
89107
n_quad_points = 4,
90-
staggering = Grids.CellCenter()
108+
staggering = CellFace()
91109
)
92110
```
111+
alternatively, you can use the `Spaces.face_space` function.
93112
"""
94113
function ExtrudedCubedSphereSpace end
95114

@@ -186,7 +205,7 @@ space = ColumnSpace(;
186205
z_elem = 10,
187206
z_min = 0,
188207
z_max = 10,
189-
staggering = Grids.CellCenter()
208+
staggering = CellCenter()
190209
)
191210
```
192211
"""
@@ -270,7 +289,7 @@ space = Box3DSpace(;
270289
n_quad_points = 4,
271290
x_elem = 3,
272291
y_elem = 4,
273-
staggering = Grids.CellCenter()
292+
staggering = CellCenter()
274293
)
275294
```
276295
"""
@@ -319,8 +338,7 @@ configuration, given:
319338
- `quad` the quadrature style (defaults to `Quadratures.GLL{n_quad_points}`)
320339
- `staggering` vertical staggering, can be one of [[`Grids.CellFace`](@ref), [`Grids.CellCenter`](@ref)]
321340
322-
Note that these arguments are all the same
323-
as [`CommonGrids.SliceXZGrid`](@ref),
341+
Note that these arguments are all the same as [`CommonGrids.SliceXZGrid`](@ref),
324342
except for `staggering`.
325343
326344
# Example usage
@@ -336,7 +354,7 @@ space = SliceXZSpace(;
336354
periodic_x = false,
337355
n_quad_points = 4,
338356
x_elem = 4,
339-
staggering = Grids.CellCenter()
357+
staggering = CellCenter()
340358
)
341359
```
342360
"""
@@ -382,9 +400,6 @@ configuration, given:
382400
- `global_geometry` the global geometry (defaults to [`Geometry.CartesianGlobalGeometry`](@ref))
383401
- `quad` the quadrature style (defaults to `Quadratures.GLL{n_quad_points}`)
384402
385-
Note that these arguments are all the same as [`CommonGrids.RectangleXYGrid`]
386-
(@ref), except for `staggering`.
387-
388403
# Example usage
389404
390405
```julia

src/Spaces/Spaces.jl

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,6 @@ global_geometry(space::AbstractSpace) = global_geometry(grid(space))
8080
space(refspace::AbstractSpace, staggering::Staggering) =
8181
space(grid(refspace), staggering)
8282

83-
84-
85-
86-
8783
issubspace(::AbstractSpace, ::AbstractSpace) = false
8884

8985
undertype(space::AbstractSpace) =
@@ -103,6 +99,14 @@ include("triangulation.jl")
10399
include("dss.jl")
104100

105101

102+
function center_space(space::AbstractSpace)
103+
error("`center_space` can only be called with vertical/extruded spaces")
104+
end
105+
106+
function face_space(space::AbstractSpace)
107+
error("`center_space` can only be called with vertical/extruded spaces")
108+
end
109+
106110
weighted_jacobian(space::Spaces.AbstractSpace) = local_geometry_data(space).WJ
107111

108112
"""

src/Spaces/extruded.jl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,29 @@ const FaceExtrudedFiniteDifferenceSpace{G} =
3333
const CenterExtrudedFiniteDifferenceSpace{G} =
3434
ExtrudedFiniteDifferenceSpace{G, CellCenter}
3535

36+
"""
37+
face_space(space::ExtrudedFiniteDifferenceSpace)
38+
39+
Return face-centered space corresponding to `space`.
40+
41+
If `space` is already face-centered, return itself.
42+
"""
43+
function face_space(space::ExtrudedFiniteDifferenceSpace)
44+
return ExtrudedFiniteDifferenceSpace(grid(space), CellFace())
45+
end
46+
47+
"""
48+
center_space(space::ExtrudedFiniteDifferenceSpace)
49+
50+
Return center-centered space corresponding to `space`.
51+
52+
If `space` is already center-centered, return itself.
53+
"""
54+
function center_space(space::ExtrudedFiniteDifferenceSpace)
55+
return ExtrudedFiniteDifferenceSpace(grid(space), CellCenter())
56+
end
57+
58+
3659
#=
3760
ExtrudedFiniteDifferenceSpace{S}(
3861
grid::Grids.ExtrudedFiniteDifferenceGrid,

src/Spaces/finitedifference.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,27 @@ CenterFiniteDifferenceSpace(
8181
Adapt.adapt_structure(to, space::FiniteDifferenceSpace) =
8282
FiniteDifferenceSpace(Adapt.adapt(to, grid(space)), staggering(space))
8383

84+
"""
85+
face_space(space::FiniteDifferenceSpace)
86+
87+
Return face-centered space corresponding to `space`.
88+
89+
If `space` is already face-centered, return itself.
90+
"""
91+
function face_space(space::FiniteDifferenceSpace)
92+
return FiniteDifferenceSpace(grid(space), CellFace())
93+
end
8494

95+
"""
96+
center_space(space::FiniteDifferenceSpace)
97+
98+
Return center-centered space corresponding to `space`.
99+
100+
If `space` is already center-centered, return itself.
101+
"""
102+
function center_space(space::FiniteDifferenceSpace)
103+
return FiniteDifferenceSpace(grid(space), CellCenter())
104+
end
85105

86106
nlevels(space::FiniteDifferenceSpace) = length(space)
87107
# TODO: deprecate?

test/Spaces/unit_spaces.jl

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@ on_gpu || @testset "extruded (2d 1×3) finite difference space" begin
112112
# Extrusion
113113
f_space = Spaces.ExtrudedFiniteDifferenceSpace(hspace, vert_face_space)
114114
c_space = Spaces.CenterExtrudedFiniteDifferenceSpace(f_space)
115+
116+
@test f_space == Spaces.face_space(f_space)
117+
@test c_space == Spaces.center_space(f_space)
118+
@test f_space == Spaces.face_space(c_space)
119+
@test c_space == Spaces.center_space(c_space)
120+
115121
s = DataLayouts.farray_size(Spaces.coordinates_data(c_space))
116122
z = Fields.coordinate_field(c_space).z
117123
@test s == (10, 4, 2, 5) # 10V, 4I, 2F(x,z), 5H
@@ -145,19 +151,25 @@ end
145151
mesh = Meshes.IntervalMesh(domain; nelems = 1)
146152
topology = Topologies.IntervalTopology(context, mesh)
147153

148-
space = Spaces.CenterFiniteDifferenceSpace(topology)
149-
@test repr(space) == """
154+
c_space = Spaces.CenterFiniteDifferenceSpace(topology)
155+
f_space = Spaces.FaceFiniteDifferenceSpace(topology)
156+
@test repr(c_space) == """
150157
CenterFiniteDifferenceSpace:
151158
context: SingletonCommsContext using CPUSingleThreaded
152159
mesh: 1-element IntervalMesh of IntervalDomain: z ∈ [0.0,5.0] (:bottom, :top)"""
153160

154-
coord_data = Spaces.coordinates_data(space)
155-
point_space = Spaces.level(space, 1)
161+
@test f_space == Spaces.face_space(f_space)
162+
@test c_space == Spaces.center_space(f_space)
163+
@test f_space == Spaces.face_space(c_space)
164+
@test c_space == Spaces.center_space(c_space)
165+
166+
coord_data = Spaces.coordinates_data(c_space)
167+
point_space = Spaces.level(c_space, 1)
156168
@test point_space isa Spaces.PointSpace
157169
@test Spaces.coordinates_data(point_space)[] ==
158170
Spaces.level(coord_data, 1)[]
159171

160-
@test Spaces.local_geometry_type(typeof(space)) <: Geometry.LocalGeometry
172+
@test Spaces.local_geometry_type(typeof(c_space)) <: Geometry.LocalGeometry
161173

162174
x_max = FT(1)
163175
y_max = FT(1)
@@ -186,7 +198,7 @@ end
186198
@test length(Spaces.all_nodes(hspace)) == 4
187199

188200
if on_gpu
189-
adapted_space = adapt(space)(space)
201+
adapted_space = adapt(c_space)(c_space)
190202
@test ClimaComms.context(adapted_space) == DeviceSideContext()
191203
@test ClimaComms.device(adapted_space) == DeviceSideDevice()
192204

0 commit comments

Comments
 (0)