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

Catch "symbol piracy" with LinearAlgebra #1771

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion src/MatrixFields/MatrixFields.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ multiples of `LinearAlgebra.I`. This comes with the following functionality:
"""
module MatrixFields

import LinearAlgebra: I, UniformScaling, Adjoint, AdjointAbsVec, mul!, inv, norm
import LinearAlgebra: I, UniformScaling, Adjoint, AdjointAbsVec, mul!, inv, norm, dot
import StaticArrays: SMatrix, SVector
import BandedMatrices: BandedMatrix, band, _BandedMatrix
import RecursiveArrayTools: recursive_bottom_eltype
Expand Down
14 changes: 14 additions & 0 deletions src/MatrixFields/matrix_multiplication.jl
Original file line number Diff line number Diff line change
Expand Up @@ -478,3 +478,17 @@ if hasfield(Method, :recursion_relation)
m.recursion_relation = dont_limit
end
end

# TODO: Explain this
function Base.Broadcast.broadcasted(
::typeof(dot), # This is LinearAlgebra.dot
arg1,
arg2,
)
args_bced = (Base.Broadcast.broadcastable(arg1), Base.Broadcast.broadcastable(arg2))
if eltype(args_bced[1]) <: BandMatrixRow
error("Detected usage of LinearAlgebra.dot in MatrixFields. Did you mean to `import ClimaCore.MatrixFields: ⋅`?")
else
Base.Broadcast.broadcasted(Base.Broadcast.combine_styles(args_bced...), dot, args_bced...)
end
end
1 change: 1 addition & 0 deletions src/Operators/finitedifference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3432,3 +3432,4 @@ Base.@propagate_inbounds function apply_stencil!(
end
return field_out
end

29 changes: 29 additions & 0 deletions test/MatrixFields/linearalgebra_dot.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# LinearAlgebra exports ⋅ and it is easy to get confused with MatrixFields.⋅.
# This test checks that we have a mechanism in place to inform the user when
# they are using the wrong ⋅.

import ClimaCore: Domains, Geometry, Meshes, Spaces, MatrixFields
import ClimaCore.MatrixFields: @name

import LinearAlgebra: ⋅, I

@testset "LinearAlgebra dot" begin
n_elem_z = 2

domain = Domains.IntervalDomain(Geometry.ZPoint(0.0), Geometry.ZPoint(1.0), boundary_names = (:bottom, :top))
mesh = Meshes.IntervalMesh(domain, nelems = 2)
space = Spaces.FaceFiniteDifferenceSpace(mesh)

diverg = Operators.DivergenceC2F(; bottom = Operators.SetDivergence(0.0), top = Operators.SetDivergence(0.0))
grad = Operators.GradientF2C()

diverg_matrix = MatrixFields.operator_matrix(diverg)
grad_matrix = MatrixFields.operator_matrix(grad)

name = @name(u)
jacobian = MatrixFields.FieldMatrix(
(@name(u), @name(u)) => similar(zeros(space), ClimaCore.MatrixFields.TridiagonalMatrixRow{Float64}),
)

@test_throws ErrorException @. jacobian[name, name] = diverg_matrix() ⋅ grad_matrix() - (I,)
end
Loading