From 3f6940f4cdc376a1a3930ec1636d7b8b0028197d Mon Sep 17 00:00:00 2001 From: Knut Andreas Meyer Date: Fri, 24 May 2024 21:44:17 +0200 Subject: [PATCH 1/3] Simplify and speedup shape_gradient_and_value for embedded --- Project.toml | 3 ++- src/Ferrite.jl | 2 ++ src/interpolations.jl | 19 +++++-------------- 3 files changed, 9 insertions(+), 15 deletions(-) diff --git a/Project.toml b/Project.toml index 69476ec25b..39d3674628 100644 --- a/Project.toml +++ b/Project.toml @@ -4,6 +4,7 @@ version = "0.3.14" [deps] EnumX = "4e289a0a-7415-4d19-859d-a7e5c4648b56" +ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" NearestNeighbors = "b8a86587-4115-5ab1-83bc-aa920d37bbce" OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d" @@ -35,7 +36,6 @@ WriteVTK = "1.13" julia = "1.6" [extras] -Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" BlockArrays = "8e7c35d0-a365-5155-bbbb-fb81a777f24e" Downloads = "f43a241f-c20a-4ad4-852c-f6b1247861c6" FerriteGmsh = "4f95f4f8-b27c-4ae5-9a39-ea55e634e36b" @@ -45,6 +45,7 @@ IterativeSolvers = "42fd0dbc-a981-5370-80f2-aaf504508153" Logging = "56ddb016-857b-54e1-b83d-db4d58db5568" Metis = "2679e427-3c69-5b7f-982b-ece356f1e94b" NBInclude = "0db19996-df87-5ea3-a455-e3a50d440464" +Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" ProgressMeter = "92933f4c-e287-5a05-a399-4b506db050ca" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" SHA = "ea8e919c-243c-51af-8825-aaa63cd721ce" diff --git a/src/Ferrite.jl b/src/Ferrite.jl index 2b0a286522..d1b44276aa 100644 --- a/src/Ferrite.jl +++ b/src/Ferrite.jl @@ -23,6 +23,8 @@ using WriteVTK: using Tensors: Tensors, AbstractTensor, SecondOrderTensor, SymmetricTensor, Tensor, Vec, gradient, rotation_tensor, symmetric, tovoigt! +using ForwardDiff: + ForwardDiff include("exports.jl") diff --git a/src/interpolations.jl b/src/interpolations.jl index 4cad56d16b..1ec25994b5 100644 --- a/src/interpolations.jl +++ b/src/interpolations.jl @@ -1524,20 +1524,11 @@ function shape_gradient_and_value(ipv::VectorizedInterpolation{dim, shape}, ξ:: end # vdim != refdim function shape_gradient_and_value(ipv::VectorizedInterpolation{vdim, shape}, ξ::V, I::Int) where {vdim, refdim, shape <: AbstractRefShape{refdim}, T, V <: Vec{refdim, T}} - # Load with dual numbers and compute the value - f = x -> shape_value(ipv, x, I) - ξd = Tensors._load(ξ, Tensors.Tag(f, V)) - value_grad = f(ξd) - # Extract the value and gradient - val = Vec{vdim, T}(i -> Tensors.value(value_grad[i])) - grad = zero(MMatrix{vdim, refdim, T}) - for (i, vi) in pairs(value_grad) - p = Tensors.partials(vi) - for (j, pj) in pairs(p) - grad[i, j] = pj - end - end - return SMatrix(grad), val + tosvec(v::Vec) = SVector((v...,)) + tovec(sv::SVector) = Vec((sv...)) + val = Ferrite.shape_value(ipv, ξ, I) + grad = ForwardDiff.jacobian(sv -> tosvec(Ferrite.shape_value(ipv, tovec(sv), I)), tosvec(ξ)) + return grad, val end reference_coordinates(ip::VectorizedInterpolation) = reference_coordinates(ip.ip) From b8191b3eeeef2a727a8cd0c7dc97dd91f2d4e6fe Mon Sep 17 00:00:00 2001 From: Knut Andreas Meyer Date: Sat, 25 May 2024 13:25:13 +0200 Subject: [PATCH 2/3] Resolve docs/Manifest --- docs/Manifest.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/Manifest.toml b/docs/Manifest.toml index 4e78ca719a..9a11f00c40 100644 --- a/docs/Manifest.toml +++ b/docs/Manifest.toml @@ -429,7 +429,7 @@ uuid = "29a986be-02c6-4525-aec4-84b980013641" version = "2.0.0" [[deps.Ferrite]] -deps = ["EnumX", "LinearAlgebra", "NearestNeighbors", "OrderedCollections", "Preferences", "Reexport", "SparseArrays", "StaticArrays", "Tensors", "WriteVTK"] +deps = ["EnumX", "ForwardDiff", "LinearAlgebra", "NearestNeighbors", "OrderedCollections", "Preferences", "Reexport", "SparseArrays", "StaticArrays", "Tensors", "WriteVTK"] path = ".." uuid = "c061ca5d-56c9-439f-9c0e-210fe06d3992" version = "0.3.14" From 6e2b4a88e4e4fc87ee585ab113b7db001b0090d5 Mon Sep 17 00:00:00 2001 From: Knut Andreas Meyer Date: Sat, 25 May 2024 13:41:22 +0200 Subject: [PATCH 3/3] Remove qualified Ferrite names --- src/interpolations.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/interpolations.jl b/src/interpolations.jl index 1ec25994b5..60f58a4f83 100644 --- a/src/interpolations.jl +++ b/src/interpolations.jl @@ -1526,8 +1526,8 @@ end function shape_gradient_and_value(ipv::VectorizedInterpolation{vdim, shape}, ξ::V, I::Int) where {vdim, refdim, shape <: AbstractRefShape{refdim}, T, V <: Vec{refdim, T}} tosvec(v::Vec) = SVector((v...,)) tovec(sv::SVector) = Vec((sv...)) - val = Ferrite.shape_value(ipv, ξ, I) - grad = ForwardDiff.jacobian(sv -> tosvec(Ferrite.shape_value(ipv, tovec(sv), I)), tosvec(ξ)) + val = shape_value(ipv, ξ, I) + grad = ForwardDiff.jacobian(sv -> tosvec(shape_value(ipv, tovec(sv), I)), tosvec(ξ)) return grad, val end