@@ -21,8 +21,10 @@ def test_sin_weak_spatial():
21
21
computed = Jhat .derivative ().vector ().get_local ()
22
22
23
23
V = TestFunction (S )
24
- dJV = div (V )* sin (x [0 ])* dx + V [0 ]* cos (x [0 ])* dx
25
- actual = assemble (dJV ).vector ().get_local ()
24
+ # Derivative (Cofunction)
25
+ dJV = assemble (div (V )* sin (x [0 ])* dx + V [0 ]* cos (x [0 ])* dx )
26
+ # Apply L2 riesz representation to obtain the gradient.
27
+ actual = dJV .riesz_representation ().vector ().get_local ()
26
28
assert np .allclose (computed , actual , rtol = 1e-14 )
27
29
28
30
@@ -71,7 +73,7 @@ def test_shape_hessian():
71
73
s = Function (S ,name = "deform" )
72
74
73
75
mesh .coordinates .assign (mesh .coordinates + s )
74
- J = assemble (sin (x [1 ])* dx (domain = mesh ))
76
+ J = assemble (cos (x [1 ])* dx (domain = mesh ))
75
77
c = Control (s )
76
78
Jhat = ReducedFunctional (J , c )
77
79
@@ -80,12 +82,13 @@ def test_shape_hessian():
80
82
h .interpolate (as_vector ((cos (x [2 ]), A * cos (x [1 ]), A * x [1 ])))
81
83
82
84
# Second order taylor
83
- dJdm = Jhat .derivative (). vector (). inner ( h . vector () )
84
- Hm = compute_hessian (J , c , h ). vector (). inner ( h . vector () )
85
+ dJdm = assemble ( inner ( Jhat .derivative (), h ) * dx )
86
+ Hm = assemble ( inner ( compute_hessian (J , c , h ), h ) * dx )
85
87
r2 = taylor_test (Jhat , s , h , dJdm = dJdm , Hm = Hm )
88
+ print (r2 )
86
89
assert (r2 > 2.9 )
87
90
Jhat (s )
88
- dJdmm_exact = derivative (derivative (sin (x [1 ])* dx (domain = mesh ),x , h ), x , h )
91
+ dJdmm_exact = derivative (derivative (cos (x [1 ]) * dx (domain = mesh ), x , h ), x , h )
89
92
assert (np .isclose (assemble (dJdmm_exact ), Hm ))
90
93
91
94
@@ -108,8 +111,8 @@ def test_PDE_hessian_neumann():
108
111
l = f * v * dx
109
112
u = Function (V )
110
113
solve (a == l , u , solver_parameters = {'ksp_type' :'preonly' , 'pc_type' :'lu' ,
111
- "mat_type" : "aij" ,
112
- "pc_factor_mat_solver_type" : "mumps" })
114
+ "mat_type" : "aij" ,
115
+ "pc_factor_mat_solver_type" : "mumps" })
113
116
J = assemble (u * dx (domain = mesh ))
114
117
c = Control (s )
115
118
Jhat = ReducedFunctional (J , c )
@@ -136,8 +139,8 @@ def test_PDE_hessian_neumann():
136
139
Jhat (s )
137
140
138
141
# # Second order taylor
139
- dJdm = Jhat .derivative (). vector (). inner ( h . vector () )
140
- Hm = compute_hessian (J , c , h ). vector (). inner ( h . vector () )
142
+ dJdm = assemble ( inner ( Jhat .derivative (), h ) * dx )
143
+ Hm = assemble ( inner ( compute_hessian (J , c , h ), h ) * dx )
141
144
r2 = taylor_test (Jhat , s , h , dJdm = dJdm , Hm = Hm )
142
145
assert (r2 > 2.95 )
143
146
@@ -190,8 +193,8 @@ def test_PDE_hessian_dirichlet():
190
193
Jhat (s )
191
194
192
195
# # Second order taylor
193
- dJdm = Jhat .derivative (). vector (). inner ( h . vector () )
194
- Hm = compute_hessian (J , c , h ). vector (). inner ( h . vector () )
196
+ dJdm = assemble ( inner ( Jhat .derivative (), h ) * dx )
197
+ Hm = assemble ( inner ( compute_hessian (J , c , h ), h ) * dx )
195
198
r2 = taylor_test (Jhat , s , h , dJdm = dJdm , Hm = Hm )
196
199
assert (r2 > 2.95 )
197
200
0 commit comments