Skip to content

Commit

Permalink
Try without bumpers (convert transform to project)
Browse files Browse the repository at this point in the history
Apply formatter

Fix inexact tests
  • Loading branch information
charleskawczynski committed Aug 24, 2022
1 parent 696081f commit dc53a36
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 56 deletions.
31 changes: 19 additions & 12 deletions src/Geometry/axistensors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,9 @@ end
x
end

#= Set `assert_exact_transform() = true` for debugging=#
assert_exact_transform() = false

@generated function _transform(
ato::Ato,
x::Axis2Tensor{T, Tuple{Afrom, A2}},
Expand All @@ -534,12 +537,14 @@ end
} where {Ito, Ifrom, J, T}
N = length(Ifrom)
M = length(J)
errcond = false
for n in 1:N
i = Ifrom[n]
if i Ito
for m in 1:M
errcond = :($errcond || x[$n, $m] != zero(T))
if assert_exact_transform()
errcond = false
for n in 1:N
i = Ifrom[n]
if i Ito
for m in 1:M
errcond = :($errcond || x[$n, $m] != zero(T))
end
end
end
end
Expand All @@ -558,8 +563,10 @@ end
end
quote
Base.@_propagate_inbounds_meta
if $errcond
throw(InexactError(:transform, Ato, x))
if assert_exact_transform()
if $errcond
throw(InexactError(:transform, Ato, x))
end
end
@inbounds Axis2Tensor(
(ato, axes(x, 2)),
Expand Down Expand Up @@ -597,11 +604,11 @@ end
))
end

@inline transform(ato::CovariantAxis, v::CovariantTensor) = _transform(ato, v)
@inline transform(ato::CovariantAxis, v::CovariantTensor) = _project(ato, v)
@inline transform(ato::ContravariantAxis, v::ContravariantTensor) =
_transform(ato, v)
@inline transform(ato::CartesianAxis, v::CartesianTensor) = _transform(ato, v)
@inline transform(ato::LocalAxis, v::LocalTensor) = _transform(ato, v)
_project(ato, v)
@inline transform(ato::CartesianAxis, v::CartesianTensor) = _project(ato, v)
@inline transform(ato::LocalAxis, v::LocalTensor) = _project(ato, v)

@inline project(ato::CovariantAxis, v::CovariantTensor) = _project(ato, v)
@inline project(ato::ContravariantAxis, v::ContravariantTensor) =
Expand Down
52 changes: 23 additions & 29 deletions src/Geometry/conversions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ LocalVector(
(::Type{<:ContravariantVector{<:Any, I}})(
u::ContravariantVector,
::LocalGeometry,
) where {I} = transform(ContravariantAxis{I}(), u)
) where {I} = project(ContravariantAxis{I}(), u)

(::Type{<:ContravariantVector{<:Any, I}})(
u::AxisVector,
local_geometry::LocalGeometry,
) where {I} =
transform(ContravariantAxis{I}(), ContravariantVector(u, local_geometry))
project(ContravariantAxis{I}(), ContravariantVector(u, local_geometry))

(::Type{<:CovariantVector{<:Any, I}})(
u::CovariantVector{<:Any, I},
Expand All @@ -50,67 +50,61 @@ LocalVector(
(::Type{<:CovariantVector{<:Any, I}})(
u::CovariantVector,
::LocalGeometry,
) where {I} = transform(CovariantAxis{I}(), u)
) where {I} = project(CovariantAxis{I}(), u)

(::Type{<:CovariantVector{<:Any, I}})(
u::AxisVector,
local_geometry::LocalGeometry,
) where {I} = transform(CovariantAxis{I}(), CovariantVector(u, local_geometry))
) where {I} = project(CovariantAxis{I}(), CovariantVector(u, local_geometry))

(::Type{<:LocalVector{<:Any, I}})(
u::LocalVector{<:Any, I},
::LocalGeometry{I},
) where {I} = u

(::Type{<:LocalVector{<:Any, I}})(u::LocalVector, ::LocalGeometry) where {I} =
transform(LocalAxis{I}(), u)
project(LocalAxis{I}(), u)

(::Type{<:LocalVector{<:Any, I}})(
u::AxisVector,
local_geometry::LocalGeometry,
) where {I} = transform(LocalAxis{I}(), LocalVector(u, local_geometry))
) where {I} = project(LocalAxis{I}(), LocalVector(u, local_geometry))

# Generic N-axis conversion functions,
# Convert to specific local geometry dimension then convert vector type
LocalVector(u::CovariantVector, local_geometry::LocalGeometry{I}) where {I} =
transform(LocalAxis{I}(), transform(CovariantAxis{I}(), u), local_geometry)
project(LocalAxis{I}(), project(CovariantAxis{I}(), u), local_geometry)

LocalVector(
u::ContravariantVector,
local_geometry::LocalGeometry{I},
) where {I} = transform(
LocalAxis{I}(),
transform(ContravariantAxis{I}(), u),
local_geometry,
)
) where {I} =
project(LocalAxis{I}(), project(ContravariantAxis{I}(), u), local_geometry)

CovariantVector(u::LocalVector, local_geometry::LocalGeometry{I}) where {I} =
transform(CovariantAxis{I}(), transform(LocalAxis{I}(), u), local_geometry)
project(CovariantAxis{I}(), project(LocalAxis{I}(), u), local_geometry)

CovariantVector(
u::ContravariantVector,
local_geometry::LocalGeometry{I},
) where {I} = transform(
) where {I} = project(
CovariantAxis{I}(),
transform(ContravariantAxis{I}(), u),
project(ContravariantAxis{I}(), u),
local_geometry,
)

ContravariantVector(
u::LocalVector,
local_geometry::LocalGeometry{I},
) where {I} = transform(
ContravariantAxis{I}(),
transform(LocalAxis{I}(), u),
local_geometry,
)
) where {I} =
project(ContravariantAxis{I}(), project(LocalAxis{I}(), u), local_geometry)

ContravariantVector(
u::CovariantVector,
local_geometry::LocalGeometry{I},
) where {I} = transform(
) where {I} = project(
ContravariantAxis{I}(),
transform(CovariantAxis{I}(), u),
project(CovariantAxis{I}(), u),
local_geometry,
)

Expand Down Expand Up @@ -172,7 +166,7 @@ Base.@propagate_inbounds Jcontravariant3(
)
u₁, v, u₃ = components(vector)
vector2 = Covariant13Vector(u₁, u₃)
u, w = components(transform(LocalAxis{(1, 3)}(), vector2, local_geometry))
u, w = components(project(LocalAxis{(1, 3)}(), vector2, local_geometry))
return UVWVector(u, v, w)
end
@inline function contravariant1(
Expand All @@ -181,25 +175,25 @@ end
)
u₁, _, u₃ = components(vector)
vector2 = Covariant13Vector(u₁, u₃)
return transform(Contravariant13Axis(), vector2, local_geometry).
return project(Contravariant13Axis(), vector2, local_geometry).
end
@inline function contravariant3(
vector::CovariantVector{<:Any, (1, 2)},
local_geometry::LocalGeometry{(1, 3)},
)
u₁, _ = components(vector)
vector2 = Covariant13Vector(u₁, zero(u₁))
return transform(Contravariant13Axis(), vector2, local_geometry).
return project(Contravariant13Axis(), vector2, local_geometry).
end
@inline function ContravariantVector(
vector::CovariantVector{<:Any, (1, 2)},
local_geometry::LocalGeometry{(1, 3)},
)
u₁, v = components(vector)
vector2 = Covariant1Vector(u₁)
vector3 = transform(
vector3 = project(
ContravariantAxis{(1, 3)}(),
transform(CovariantAxis{(1, 3)}(), vector2),
project(CovariantAxis{(1, 3)}(), vector2),
local_geometry,
)
u¹, u³ = components(vector3)
Expand Down Expand Up @@ -333,11 +327,11 @@ end
ax::ContravariantAxis,
v::CovariantTensor,
local_geometry::LocalGeometry,
) = transform(
) = project(
ax,
local_geometry.∂ξ∂x *
local_geometry.∂ξ∂x' *
transform(dual(axes(local_geometry.∂ξ∂x, 1)), v),
project(dual(axes(local_geometry.∂ξ∂x, 1)), v),
)

@generated function project(
Expand Down
2 changes: 1 addition & 1 deletion src/Geometry/globalgeometry.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Cartesian123Point(pt::AbstractPoint, global_geometry::AbstractGlobalGeometry) =
u::AxisVector,
global_geometry::AbstractGlobalGeometry,
local_geometry::LocalGeometry,
) where {I} = transform(
) where {I} = project(
CartesianAxis{I}(),
CartesianVector(u, global_geometry, local_geometry),
)
Expand Down
8 changes: 4 additions & 4 deletions src/Operators/finitedifference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2288,7 +2288,7 @@ boundary_width(::GradientF2C, ::Extrapolate, arg) = 1
)
space = axes(arg)
@assert idx == left_center_boundary_idx(arg)
Geometry.transform(
Geometry.project(
Geometery.Covariant3Axis(),
stencil_interior(op, loc, idx + 1, hidx, arg),
Geometry.LocalGeometry(space, idx, hidx),
Expand All @@ -2304,7 +2304,7 @@ end
)
space = axes(arg)
@assert idx == right_center_boundary_idx(arg)
Geometry.transform(
Geometry.project(
Geometry.Covariant3Axis(),
stencil_interior(op, loc, idx - 1, hidx, arg),
Geometry.LocalGeometry(space, idx, hidx),
Expand Down Expand Up @@ -2389,7 +2389,7 @@ boundary_width(::GradientC2F, ::SetGradient, arg) = 1
@assert idx == left_face_boundary_idx(arg)
space = axes(arg)
# imposed flux boundary condition at left most face
Geometry.transform(
Geometry.project(
Geometry.Covariant3Axis(),
getidx(bc.val, loc, nothing, hidx),
Geometry.LocalGeometry(space, idx, hidx),
Expand All @@ -2406,7 +2406,7 @@ end
@assert idx == right_face_boundary_idx(arg)
space = axes(arg)
# imposed flux boundary condition at right most face
Geometry.transform(
Geometry.project(
Geometry.Covariant3Axis(),
getidx(bc.val, loc, nothing, hidx),
Geometry.LocalGeometry(space, idx, hidx),
Expand Down
4 changes: 2 additions & 2 deletions src/Spaces/dss.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,15 @@ end
if ax isa Geometry.UWAxis && axfrom isa Geometry.Covariant12Axis
# return Geometry.transform(Geometry.UVWAxis(), arg, local_geometry)
u₁, v = Geometry.components(arg)
uw_vector = Geometry.transform(
uw_vector = Geometry.project(
Geometry.UWAxis(),
Geometry.Covariant13Vector(u₁, zero(u₁)),
local_geometry,
)
u, w = Geometry.components(uw_vector)
return Geometry.UVWVector(u, v, w) * weight
end
Geometry.transform(ax, arg, local_geometry) * weight
Geometry.project(ax, arg, local_geometry) * weight
end

"""
Expand Down
18 changes: 10 additions & 8 deletions test/Geometry/axistensors.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Test
using ClimaCore.Geometry, ClimaCore.DataLayouts
using LinearAlgebra, StaticArrays
import ClimaCore
ClimaCore.Geometry.assert_exact_transform() = true

@testset "AxisTensors" begin
x = Geometry.Covariant12Vector(1.0, 2.0)
Expand Down Expand Up @@ -83,10 +85,10 @@ end
Geometry.Covariant12Axis(),
Geometry.Covariant13Vector(2.0, 0.0),
) == Geometry.Covariant12Vector(2.0, 0.0)
@test_throws InexactError Geometry.transform(
Geometry.Covariant12Axis(),
Geometry.Covariant13Vector(2.0, 2.0),
)
# @test_throws InexactError Geometry.transform(
# Geometry.Covariant12Axis(),
# Geometry.Covariant13Vector(2.0, 2.0),
# )


@test Geometry.transform(
Expand All @@ -97,10 +99,10 @@ end
Geometry.Covariant12Axis(),
Geometry.Covariant13Vector(2.0, 0.0) * Geometry.Cartesian1Vector(1.0)',
) == Geometry.Covariant12Vector(2.0, 0.0) * Geometry.Cartesian1Vector(1.0)'
@test_throws InexactError Geometry.transform(
Geometry.Covariant12Axis(),
Geometry.Covariant13Vector(2.0, 2.0) * Geometry.Cartesian1Vector(1.0)',
)
# @test_throws InexactError Geometry.transform(
# Geometry.Covariant12Axis(),
# Geometry.Covariant13Vector(2.0, 2.0) * Geometry.Cartesian1Vector(1.0)',
# )
end

@testset "project" begin
Expand Down

0 comments on commit dc53a36

Please sign in to comment.