Skip to content

Commit

Permalink
add special cases for projection from Covariant => Contravariant
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbyrne committed Aug 2, 2022
1 parent b1daf53 commit d1244c7
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 1 deletion.
90 changes: 90 additions & 0 deletions src/Geometry/conversions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,96 @@ for op in (:transform, :project)
end


@generated function project(
ax::ContravariantAxis{Ito},
v::CovariantVector{T, Ifrom},
local_geometry::LocalGeometry{J},
) where {T, Ito, Ifrom, J}
Nfrom = length(Ifrom)
Nto = length(Ito)
NJ = length(J)

vals = []
for i in Ito
if i J
# e.g. i = 2, J = (1,2,3)
IJ = intersect(J, Ifrom)
if isempty(IJ)
val = 0
else
niJ = findfirst(==(i), J)
val = Expr(
:call,
:+,
[
:(
local_geometry.gⁱʲ[$niJ, $(findfirst(==(j), J))] * v[$(findfirst(==(j), Ifrom))]
) for j in IJ
]...,
)
end
elseif i Ifrom
# e.g. i = 2, J = (1,3), Ifrom = (2,)
ni = findfirst(==(i), Ifrom)
val = :(v[$ni])
else
# e.g. i = 2, J = (1,3), Ifrom = (1,)
val = 0
end
push!(vals, val)
end
:(AxisVector(ContravariantAxis{$Ito}(), SVector{$Nto, $T}(($vals...))))
end
@generated function project(
ax::ContravariantAxis{Ito},
v::Covariant2Tensor{T, Tuple{CovariantAxis{Ifrom}, A}},
local_geometry::LocalGeometry{J},
) where {T, Ito, Ifrom, A, J}
Nfrom = length(Ifrom)
Nto = length(Ito)
NJ = length(J)
NA = length(A.instance)

vals = []
for na in 1:NA
for i in Ito
if i J
# e.g. i = 2, J = (1,2,3)
IJ = intersect(J, Ifrom)
if isempty(IJ)
val = 0
else
niJ = findfirst(==(i), J)
val = Expr(
:call,
:+,
[
:(
local_geometry.gⁱʲ[
$niJ,
$(findfirst(==(j), J)),
] * v[$(findfirst(==(j), Ifrom)), $na]
) for j in IJ
]...,
)
end
elseif i Ifrom
# e.g. i = 2, J = (1,3), Ifrom = (2,)
ni = findfirst(==(i), Ifrom)
val = :(v[$ni, $na])
else
# e.g. i = 2, J = (1,3), Ifrom = (1,)
val = 0
end
push!(vals, val)
end
end
:(AxisTensor(
(ContravariantAxis{$Ito}(), A.instance),
SMatrix{$Nto, $NA, $T, $(Nto * NA)}(($vals...)),
))
end

"""
divergence_result_type(V)
Expand Down
1 change: 0 additions & 1 deletion test/Geometry/ref_funcs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,3 @@
CovariantVector(u, local_geometry).u₂
@inline ref_covariant3(u::AxisVector, local_geometry::LocalGeometry) =
CovariantVector(u, local_geometry).u₃

0 comments on commit d1244c7

Please sign in to comment.