Skip to content

Commit e41c3a0

Browse files
authored
Merge pull request #604 from gridap/improve_divergence
Implemented divergence for rank 2 tensor-valued functions
2 parents 28671b9 + 8d3e8ec commit e41c3a0

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

src/Fields/AutoDiff.jl

+43-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,49 @@ function symmetric_gradient(f::Function,x::Point)
6868
end
6969

7070
function divergence(f::Function,x::Point)
71-
tr(gradient(f,x))
71+
divergence(f,x,return_value(f,x))
72+
end
73+
74+
function divergence(f::Function,x::Point,fx)
75+
tr(gradient(f,x,fx))
76+
end
77+
78+
function divergence(f::Function,x::Point,fx::TensorValue{2,2})
79+
g(x) = SVector(f(x).data)
80+
a = ForwardDiff.jacobian(g,get_array(x))
81+
VectorValue(
82+
a[1,1]+a[2,2],
83+
a[3,1]+a[4,2],
84+
)
85+
end
86+
87+
function divergence(f::Function,x::Point,fx::TensorValue{3,3})
88+
g(x) = SVector(f(x).data)
89+
a = ForwardDiff.jacobian(g,get_array(x))
90+
VectorValue(
91+
a[1,1]+a[2,2]+a[3,3],
92+
a[4,1]+a[5,2]+a[6,3],
93+
a[7,1]+a[8,2]+a[9,3],
94+
)
95+
end
96+
97+
function divergence(f::Function,x::Point,fx::SymTensorValue{2})
98+
g(x) = SVector(f(x).data)
99+
a = ForwardDiff.jacobian(g,get_array(x))
100+
VectorValue(
101+
a[1,1]+a[2,2],
102+
a[2,1]+a[3,2],
103+
)
104+
end
105+
106+
function divergence(f::Function,x::Point,fx::SymTensorValue{3})
107+
g(x) = SVector(f(x).data)
108+
a = ForwardDiff.jacobian(g,get_array(x))
109+
VectorValue(
110+
a[1,1]+a[2,2]+a[3,3],
111+
a[2,1]+a[4,2]+a[5,3],
112+
a[3,1]+a[5,2]+a[6,3],
113+
)
72114
end
73115

74116
function curl(f::Function,x::Point)

test/FieldsTests/DiffOperatorsTests.jl

+21
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ for x in xs
9393
@test (∇×u)(x) == grad2curl(∇u(x))
9494
@test Δ(u)(x) == Δu(x)
9595
@test ε(u)(x) == εu(x)
96+
@test (u)(x) == ∇u(x)
97+
@test Δ(u)(x) == (∇∇u)(x)
98+
@test (∇εu)(x) == VectorValue(4.,-1.)
9699
end
97100

98101
u(x) = VectorValue( x[1]^2 + 2*x[2]^2, 0 )
@@ -105,6 +108,24 @@ for x in xs
105108
@test (∇×u)(x) == grad2curl(∇u(x))
106109
@test Δ(u)(x) == Δu(x)
107110
@test ε(u)(x) == εu(x)
111+
@test Δ(u)(x) == (∇∇u)(x)
112+
@test (∇εu)(x) == VectorValue(4.,0.)
113+
end
114+
115+
u(x) = VectorValue( x[1]^2 + 2*x[3]^2, -x[1]^2, -x[2]^2 + x[3]^2 )
116+
∇u(x) = TensorValue( 2*x[1],0,4*x[3], -2*x[1],0,0, 0,-2*x[2],2*x[3] )
117+
Δu(x) = VectorValue( 6, -2, 0 )
118+
εu(x) = symmetric_part(∇u(x))
119+
120+
xs = [ Point(1.,1.,2.0), Point(2.,0.,1.), Point(0.,3.,0.), Point(-1.,3.,2.)]
121+
for x in xs
122+
@test (u)(x) == ∇u(x)
123+
@test (∇u)(x) == tr(∇u(x))
124+
@test (∇×u)(x) == grad2curl(∇u(x))
125+
@test Δ(u)(x) == Δu(x)
126+
@test ε(u)(x) == εu(x)
127+
@test Δ(u)(x) == (∇∇u)(x)
128+
@test (∇εu)(x) == VectorValue(4.,-1.,1.)
108129
end
109130

110131
end # module

0 commit comments

Comments
 (0)