Skip to content

Commit f379719

Browse files
authored
fix method and add a couple tests (#1160)
1 parent 458c00e commit f379719

File tree

8 files changed

+44
-12
lines changed

8 files changed

+44
-12
lines changed

src/ClimaLand.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,18 @@ abstract type AbstractLandModel{FT} <: AbstractModel{FT} end
5454

5555
ClimaLand.name(::AbstractLandModel) = :land
5656

57+
"""
58+
get_domain(model::AbstractLandModel)
59+
60+
Return the ClimaLand domain of the model - for integrated models,
61+
this is the soil domain (e.g., Column, SphericalShell) since this
62+
includes both the surface and subsurface.
63+
64+
If your model does not have a soil component, you will need to define
65+
a more specific method.
66+
"""
67+
get_domain(model::ClimaLand.AbstractLandModel) = model.soil.domain
68+
5769
"""
5870
Domains.coordinates(model::AbstractLandModel)
5971

src/shared_utilities/models.jl

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export AbstractModel,
2525
total_liq_water_vol_per_area!,
2626
total_energy_per_area!
2727

28-
import ClimaComms
28+
import ClimaComms: context, device
2929
import .Domains: coordinates
3030
## Default methods for all models - to be in a seperate module at some point.
3131
"""
@@ -460,21 +460,23 @@ this is the soil domain (e.g., Column, SphericalShell).
460460
get_domain(model::ClimaLand.AbstractModel) = model.domain
461461

462462
function ClimaComms.context(model::AbstractModel)
463-
if :domain propertynames(model)
464-
return ClimaComms.context(get_domain(model))
465-
else
463+
try
464+
domain = get_domain(model)
465+
return ClimaComms.context(domain)
466+
catch
466467
error(
467-
"Your model does not contain a domain. If this is intended, you will need a new method of ClimaComms.context.",
468+
"Your model does not have a get_domain() method. If this is intended, you will need a new method of ClimaComms.context.",
468469
)
469470
end
470471
end
471472

472473
function ClimaComms.device(model::AbstractModel)
473-
if :domain propertynames(model)
474-
return ClimaComms.device(get_domain(model))
475-
else
474+
try
475+
domain = get_domain(model)
476+
return ClimaComms.device(domain)
477+
catch
476478
error(
477-
"Your model does not contain a domain. If this is intended, you will need a new method of ClimaComms.device.",
479+
"Your model does not have a get_domain() method. If this is intended, you will need a new method of ClimaComms.device.",
478480
)
479481
end
480482
end

test/integrated/full_land.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ land = global_land_model(
6363
context = context,
6464
domain = domain,
6565
);
66-
66+
@test domain == ClimaLand.get_domain(land)
67+
@test ClimaComms.context(land) == ClimaComms.context()
68+
@test ClimaComms.device(land) == ClimaComms.device()
6769
@test ClimaLand.land_components(land) == (:soil, :snow, :soilco2, :canopy)
6870

6971
@testset "Total energy and water" begin

test/integrated/soil_energy_hydrology_biogeochemistry.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ for FT in (Float32, Float64)
111111
soil_args = soil_args,
112112
soilco2_args = soilco2_args,
113113
)
114-
@test_throws ErrorException ClimaComms.context(model)
115-
@test_throws ErrorException ClimaComms.device(model)
114+
@test ClimaComms.context(model) == ClimaComms.context()
115+
@test ClimaComms.device(model) == ClimaComms.device()
116116

117117
@test model.soilco2.drivers.met.ν == model.soil.parameters.ν
118118
@test model.soilco2.drivers.met isa ClimaLand.PrognosticMet

test/shared_utilities/domains.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ FT = Float32
7070
npoly_sphere,
7171
(; surface = shell.space.surface),
7272
)
73+
@test ClimaComms.context(shell) == ClimaComms.context()
74+
@test ClimaComms.device(shell) == ClimaComms.device()
7375
shell_stretch = SphericalShell(;
7476
radius = radius,
7577
depth = FT(1.0),
@@ -109,6 +111,8 @@ FT = Float32
109111
zlim = zlim,
110112
nelements = nelements,
111113
)
114+
@test ClimaComms.context(box) == ClimaComms.context()
115+
@test ClimaComms.device(box) == ClimaComms.device()
112116
@test box.fields.depth == zlim[2] - zlim[1]
113117
@test box.fields.z ==
114118
ClimaCore.Fields.coordinate_field(box.space.subsurface).z
@@ -165,6 +169,8 @@ FT = Float32
165169
nelements = nelements[1:2],
166170
periodic = (true, true),
167171
)
172+
@test ClimaComms.context(xy_plane) == ClimaComms.context()
173+
@test ClimaComms.device(xy_plane) == ClimaComms.device()
168174
plane_coords = coordinates(xy_plane).surface
169175
@test eltype(plane_coords) == ClimaCore.Geometry.XYPoint{FT}
170176
@test typeof(plane_coords) <: ClimaCore.Fields.Field
@@ -264,6 +270,8 @@ FT = Float32
264270
# Column
265271

266272
z_column = Column(; zlim = zlim, nelements = nelements[3])
273+
@test ClimaComms.context(z_column) == ClimaComms.context()
274+
@test ClimaComms.device(z_column) == ClimaComms.device()
267275
@test z_column.fields.z ==
268276
ClimaCore.Fields.coordinate_field(z_column.space.subsurface).z
269277
@test z_column.fields.depth == zlim[2] - zlim[1]
@@ -310,6 +318,8 @@ end
310318
@testset "Point Domain, FT = $FT" begin
311319
zmin = FT(1.0)
312320
point = Point(; z_sfc = zmin)
321+
@test ClimaComms.context(point) == ClimaComms.context()
322+
@test ClimaComms.device(point) == ClimaComms.device()
313323
@test point.z_sfc == zmin
314324
point_space = point.space.surface
315325
@test point_space isa ClimaCore.Spaces.PointSpace

test/standalone/Snow/snow.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ import ClimaLand.Parameters as LP
4848
domain = domain,
4949
boundary_conditions = ClimaLand.Snow.AtmosDrivenSnowBC(atmos, rad),
5050
)
51+
@test ClimaComms.context(model) == ClimaComms.context()
52+
@test ClimaComms.device(model) == ClimaComms.device()
5153
drivers = ClimaLand.get_drivers(model)
5254
@test drivers == (atmos, rad)
5355
Y, p, coords = ClimaLand.initialize(model)

test/standalone/Soil/climate_drivers.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ for FT in (Float32, Float64)
114114
boundary_conditions = boundary_fluxes,
115115
sources = (),
116116
)
117+
@test ClimaComms.context(model) == ClimaComms.context()
118+
@test ClimaComms.device(model) == ClimaComms.device()
117119
drivers = ClimaLand.get_drivers(model)
118120
@test drivers == (atmos, radiation)
119121
Y, p, coords = initialize(model)

test/standalone/Vegetation/canopy_model.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,8 @@ import ClimaParams
220220
soil_driver,
221221
),
222222
)
223+
@test ClimaComms.context(canopy) == ClimaComms.context()
224+
@test ClimaComms.device(canopy) == ClimaComms.device()
223225
drivers = ClimaLand.get_drivers(canopy)
224226
@test drivers == (atmos, radiation)
225227
Y, p, coords = ClimaLand.initialize(canopy)

0 commit comments

Comments
 (0)