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

Simplify deduction of eltype in FunctionValues constructor #857

Closed
KnutAM opened this issue Dec 7, 2023 · 7 comments · Fixed by #858
Closed

Simplify deduction of eltype in FunctionValues constructor #857

KnutAM opened this issue Dec 7, 2023 · 7 comments · Fixed by #858

Comments

@KnutAM
Copy link
Member

KnutAM commented Dec 7, 2023

In #764, a potential simplification of the code linked below was discussed. Would be good to investigate, but this should also become simpler if Ferrite-FEM/Tensors.jl#188 is finalized. CC @termi-official

# Helpers to get the correct types for FunctionValues for the given function and, if needed, geometric interpolations.
struct SInterpolationDims{rdim,sdim} end
struct VInterpolationDims{rdim,sdim,vdim} end
function InterpolationDims(::ScalarInterpolation, ip_geo::VectorizedInterpolation{sdim}) where sdim
return SInterpolationDims{getdim(ip_geo),sdim}()
end
function InterpolationDims(::VectorInterpolation{vdim}, ip_geo::VectorizedInterpolation{sdim}) where {vdim,sdim}
return VInterpolationDims{getdim(ip_geo),sdim,vdim}()
end
typeof_N(::Type{T}, ::SInterpolationDims) where T = T
typeof_N(::Type{T}, ::VInterpolationDims{<:Any,dim,dim}) where {T,dim} = Vec{dim,T}
typeof_N(::Type{T}, ::VInterpolationDims{<:Any,<:Any,vdim}) where {T,vdim} = SVector{vdim,T} # Why not ::Vec here?
typeof_dNdx(::Type{T}, ::SInterpolationDims{dim,dim}) where {T,dim} = Vec{dim,T}
typeof_dNdx(::Type{T}, ::SInterpolationDims{<:Any,sdim}) where {T,sdim} = SVector{sdim,T} # Why not ::Vec here?
typeof_dNdx(::Type{T}, ::VInterpolationDims{dim,dim,dim}) where {T,dim} = Tensor{2,dim,T}
typeof_dNdx(::Type{T}, ::VInterpolationDims{<:Any,sdim,vdim}) where {T,sdim,vdim} = SMatrix{vdim,sdim,T} # If vdim=sdim!=rdim Tensor would be possible...
typeof_dNdξ(::Type{T}, ::SInterpolationDims{dim,dim}) where {T,dim} = Vec{dim,T}
typeof_dNdξ(::Type{T}, ::SInterpolationDims{rdim}) where {T,rdim} = SVector{rdim,T} # Why not ::Vec here?
typeof_dNdξ(::Type{T}, ::VInterpolationDims{dim,dim,dim}) where {T,dim} = Tensor{2,dim,T}
typeof_dNdξ(::Type{T}, ::VInterpolationDims{rdim,<:Any,vdim}) where {T,rdim,vdim} = SMatrix{vdim,rdim,T} # If vdim=rdim!=sdim Tensor would be possible...

@termi-official
Copy link
Member

termi-official commented Dec 7, 2023

Why not ::Vec here?

The only reason here is literally that I did not want to mix Vec and SMatrix , because of missing support for mixed tensors. :)

@KnutAM
Copy link
Member Author

KnutAM commented Dec 7, 2023

I think for now that is ok, but I talked to @fredrikekre that before releasing 1.0 we should probably add a note in the docs since this will be a breaking change for users of embedded elements when the output changes from SArray to Tensor/MixedTensor.

E.g. in interface elements (#767 ), they currently return SArrays when they would in the future return Tensor.

@termi-official
Copy link
Member

termi-official commented Dec 7, 2023

I am not sure if there are users for this feature, because it is not really exposed in the docs and I struggle with meshgen. But yes, we should mention the breaking change anyway.

fredrikekre added a commit that referenced this issue Dec 7, 2023
This patch simplifies the initialization of types for `N`, `dNdx`, and
`dNdξ` used in the `FunctionValues` constructor. The change in LOC isn't
significant but I think it is easier to follow when there are no
auxiliary types and when they are grouped by "case" instead of function
name. Fixes #857.
@fredrikekre
Copy link
Member

There is also the possibility of vdim > 3 which Tensors don't handle(?). I think it is nice if we stick to the same type for each "case" and don't mix Tensors/StaticArrays.

fredrikekre added a commit that referenced this issue Dec 8, 2023
This patch simplifies the initialization of types for `N`, `dNdx`, and
`dNdξ` used in the `FunctionValues` constructor. The change in LOC isn't
significant but I think it is easier to follow when there are no
auxiliary types and when they are grouped by "case" instead of function
name. Fixes #857.
fredrikekre added a commit that referenced this issue Dec 8, 2023
This patch simplifies the initialization of types for `N`, `dNdx`, and
`dNdξ` used in the `FunctionValues` constructor. The change in LOC isn't
significant but I think it is easier to follow when there are no
auxiliary types and when they are grouped by "case" instead of function
name. Fixes #857.
@KnutAM
Copy link
Member Author

KnutAM commented Dec 8, 2023

There is also the possibility of vdim > 3 which Tensors don't handle(?)

What is the application of this?
If a generalized Tensor for higher dimensions is needed, that would be easy to add support for using the macro in Ferrite-FEM/Tensors.jl#211.

I would find it better to have a flag on FEValues construction, e.g. returntype=SArray (default being e.g. AbstractTensor) for such special cases to be very explicit. I find that getting SArray for some combinations of dimensions and Tensor for others is confusing/unexpected.

@termi-official
Copy link
Member

The change from SArray to MixedTensor should be non-breaking, because tensors are just SArrays with defined size + some ops, right?

@KnutAM
Copy link
Member Author

KnutAM commented Dec 8, 2023

The change from SArray to MixedTensor should be non-breaking, because tensors are just SArrays with defined size + some ops, right?

Not fully...

julia> at = rand(Tensor{2,3});

julia> as = rand(SMatrix{3,3});

julia> as * as
3×3 SMatrix{3, 3, Float64, 9} with indices SOneTo(3)×SOneTo(3):
 1.70716  1.13599   1.33628
 1.84993  1.49189   1.3397
 1.42037  0.676446  1.24042

julia> at * at
ERROR: use `` (`\cdot`) for single contraction and `` (`\boxdot`) for double contraction instead of `*`
Stacktrace:
 [1] error(s::String)
   @ Base .\error.jl:35
 [2] *(S1::Tensor{2, 3, Float64, 9}, S2::Tensor{2, 3, Float64, 9})
   @ Tensors C:\Users\meyer\.julia\packages\Tensors\DZnYM\src\tensor_ops_errors.jl:3
 [3] top-level scope
   @ REPL[6]:1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants