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

Add test and improve inference of monotonic_check #1810

Merged
merged 2 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions .buildkite/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ steps:
key: unit_meshes_rectangle
command: "julia --color=yes --check-bounds=yes --project=.buildkite test/Meshes/rectangle.jl"

- label: "Unit: meshes opt"
key: unit_meshes_opt
command: "julia --color=yes --check-bounds=yes --project=.buildkite test/Meshes/opt_meshes.jl"

- label: "Unit: meshes cubedsphere"
key: unit_meshes_cubed_sphere
command: "julia --color=yes --check-bounds=yes --project=.buildkite test/Meshes/cubedsphere.jl"
Expand Down
32 changes: 23 additions & 9 deletions src/Meshes/interval.jl
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,29 @@ function boundary_face_name(mesh::IntervalMesh, elem::Integer, face)
return nothing
end

function monotonic_check(faces)
if !(hasproperty(first(faces), :z) || eltype(faces) <: Real)
return nothing
end
z(face::AbstractFloat) = face
z(face::Geometry.AbstractPoint) = face.z
monotonic_check(
faces::Union{
LinRange{<:Geometry.AbstractPoint},
Vector{<:Geometry.AbstractPoint},
},
) = :no_check

function monotonic_check(
faces::Union{
LinRange{<:Geometry.ZPoint},
LinRange{<:Real},
Vector{<:Geometry.ZPoint},
Vector{<:Real},
},
)
n = length(faces) - 1
monotonic_incr = all(map(i -> z(faces[i]) < z(faces[i + 1]), 1:n))
monotonic_decr = all(map(i -> z(faces[i]) > z(faces[i + 1]), 1:n))
if eltype(faces) <: Geometry.AbstractPoint
monotonic_incr = all(i -> faces[i].z < faces[i + 1].z, 1:n)
monotonic_decr = all(i -> faces[i].z > faces[i + 1].z, 1:n)
else
monotonic_incr = all(i -> faces[i] < faces[i + 1], 1:n)
monotonic_decr = all(i -> faces[i] > faces[i + 1], 1:n)
end
if !(monotonic_incr || monotonic_decr)
error(
string(
Expand All @@ -106,7 +120,7 @@ function monotonic_check(faces)
),
)
end
return nothing
return :pass
end

abstract type StretchingRule end
Expand Down
24 changes: 24 additions & 0 deletions test/Meshes/interval.jl
Original file line number Diff line number Diff line change
Expand Up @@ -314,3 +314,27 @@ end
@test Meshes.coordinates(trunc_mesh, 1, length(trunc_mesh.faces)) ==
Geometry.ZPoint(z_top)
end

@testset "monotonic_check - dispatch" begin
faces = range(Geometry.XPoint(0), Geometry.XPoint(10); length = 11)
@test Meshes.monotonic_check(faces) == :no_check
@test Meshes.monotonic_check(collect(faces)) == :no_check
end

@testset "monotonic_check" begin
faces = range(Geometry.ZPoint(0), Geometry.ZPoint(10); length = 11)
@test Meshes.monotonic_check(faces) == :pass # monotonic increasing
@test Meshes.monotonic_check(collect(faces)) == :pass # monotonic increasing
@test Meshes.monotonic_check(map(x -> x.z, faces)) == :pass # monotonic increasing

faces = range(Geometry.ZPoint(0), Geometry.ZPoint(-10); length = 11)
@test Meshes.monotonic_check(faces) == :pass # monotonic decreasing
@test Meshes.monotonic_check(collect(faces)) == :pass # monotonic decreasing
@test Meshes.monotonic_check(map(x -> x.z, faces)) == :pass # monotonic decreasing

faces = map(z -> Geometry.ZPoint(1), 1:10)
@test_throws ErrorException Meshes.monotonic_check(faces) # non-monotonic

faces = range(Geometry.ZPoint(0), Geometry.ZPoint(10); length = 11)
cfaces = collect(faces)
end
17 changes: 17 additions & 0 deletions test/Meshes/opt_meshes.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#=
julia --project
using Revise; include(joinpath("test", "Meshes", "opt_meshes.jl"))
=#
using Test
using SparseArrays
using JET

import ClimaCore: ClimaCore, Domains, Meshes, Geometry

@testset "monotonic_check" begin
faces = range(Geometry.ZPoint(0), Geometry.ZPoint(10); length = 11)
cfaces = collect(faces)
@test_opt Meshes.monotonic_check(faces)
@test_opt Meshes.monotonic_check(cfaces)
@test 0 == @allocated Meshes.monotonic_check(cfaces)
end
26 changes: 13 additions & 13 deletions test/Spaces/opt_spaces.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,22 @@ end
#! format: off
if ClimaComms.device(context) isa ClimaComms.CUDADevice
test_n_failures(86, TU.PointSpace, context)
test_n_failures(142, TU.SpectralElementSpace1D, context)
test_n_failures(1082, TU.SpectralElementSpace2D, context)
test_n_failures(26, TU.ColumnCenterFiniteDifferenceSpace, context)
test_n_failures(27, TU.ColumnFaceFiniteDifferenceSpace, context)
test_n_failures(1082, TU.SphereSpectralElementSpace, context)
test_n_failures(1097, TU.CenterExtrudedFiniteDifferenceSpace, context)
test_n_failures(1097, TU.FaceExtrudedFiniteDifferenceSpace, context)
test_n_failures(144, TU.SpectralElementSpace1D, context)
test_n_failures(1103, TU.SpectralElementSpace2D, context)
test_n_failures(4, TU.ColumnCenterFiniteDifferenceSpace, context)
test_n_failures(5, TU.ColumnFaceFiniteDifferenceSpace, context)
test_n_failures(1104, TU.SphereSpectralElementSpace, context)
test_n_failures(1109, TU.CenterExtrudedFiniteDifferenceSpace, context)
test_n_failures(1109, TU.FaceExtrudedFiniteDifferenceSpace, context)
else
test_n_failures(0, TU.PointSpace, context)
test_n_failures(138, TU.SpectralElementSpace1D, context)
test_n_failures(280, TU.SpectralElementSpace2D, context)
test_n_failures(26, TU.ColumnCenterFiniteDifferenceSpace, context)
test_n_failures(27, TU.ColumnFaceFiniteDifferenceSpace, context)
test_n_failures(137, TU.SpectralElementSpace1D, context)
test_n_failures(279, TU.SpectralElementSpace2D, context)
test_n_failures(4, TU.ColumnCenterFiniteDifferenceSpace, context)
test_n_failures(5, TU.ColumnFaceFiniteDifferenceSpace, context)
test_n_failures(280, TU.SphereSpectralElementSpace, context)
test_n_failures(305, TU.CenterExtrudedFiniteDifferenceSpace, context)
test_n_failures(305, TU.FaceExtrudedFiniteDifferenceSpace, context)
test_n_failures(285, TU.CenterExtrudedFiniteDifferenceSpace, context)
test_n_failures(285, TU.FaceExtrudedFiniteDifferenceSpace, context)
end
#! format: on
end
Expand Down
Loading