@@ -35,16 +35,18 @@ def test_assembly():
3535 test = TestFunction (space )
3636
3737 with reverse_over_forward ():
38- u = Function (space , name = "u" ).interpolate (X [0 ])
38+ u = Function (space , name = "u" ).interpolate (X [0 ] - 0.5 )
39+ u_ref = u .copy (deepcopy = True )
3940 zeta = Function (space , name = "zeta" ).interpolate (X [0 ])
4041 u .block_variable .tlm_value = zeta .copy (deepcopy = True )
4142
42- J = assemble (u * u * dx )
43+ J = assemble (( u ** 3 ) * dx )
4344
4445 _ = compute_gradient (J .block_variable .tlm_value , Control (u ))
4546 adj_value = u .block_variable .adj_value
46- assert np .allclose (adj_value .dat .data_ro ,
47- assemble (2 * inner (zeta , test ) * dx ).dat .data_ro )
47+ assert np .allclose (
48+ adj_value .dat .data_ro ,
49+ assemble (6 * inner (u_ref * zeta , test ) * dx ).dat .data_ro )
4850
4951
5052@pytest .mark .skipcomplex
@@ -72,16 +74,18 @@ def test_function_assignment():
7274
7375 with reverse_over_forward ():
7476 u = Function (space , name = "u" ).interpolate (X [0 ] - 0.5 )
77+ u_ref = u .copy (deepcopy = True )
7578 zeta = Function (space , name = "zeta" ).interpolate (X [0 ])
7679 u .block_variable .tlm_value = zeta .copy (deepcopy = True )
7780
7881 v = Function (space , name = "v" ).assign (u )
79- J = assemble (v * v * dx )
82+ J = assemble (( v ** 3 ) * dx )
8083
8184 _ = compute_gradient (J .block_variable .tlm_value , Control (u ))
8285 adj_value = u .block_variable .adj_value
83- assert np .allclose (adj_value .dat .data_ro ,
84- assemble (2 * inner (zeta , test ) * dx ).dat .data_ro )
86+ assert np .allclose (
87+ adj_value .dat .data_ro ,
88+ assemble (6 * inner (u_ref * zeta , test ) * dx ).dat .data_ro )
8589
8690
8791@pytest .mark .skipcomplex
@@ -94,15 +98,17 @@ def test_function_assignment_expr():
9498 with reverse_over_forward ():
9599 u = Function (space , name = "u" ).interpolate (X [0 ] - 0.5 )
96100 zeta = Function (space , name = "zeta" ).interpolate (X [0 ])
101+ u_ref = u .copy (deepcopy = True )
97102 u .block_variable .tlm_value = zeta .copy (deepcopy = True )
98103
99104 v = Function (space , name = "v" ).assign (- 3 * u )
100- J = assemble (v * v * dx )
105+ J = assemble (( v ** 3 ) * dx )
101106
102107 _ = compute_gradient (J .block_variable .tlm_value , Control (u ))
103108 adj_value = u .block_variable .adj_value
104- assert np .allclose (adj_value .dat .data_ro ,
105- assemble (18 * inner (zeta , test ) * dx ).dat .data_ro )
109+ assert np .allclose (
110+ adj_value .dat .data_ro ,
111+ assemble (- 162 * inner (u_ref * zeta , test ) * dx ).dat .data_ro )
106112
107113
108114@pytest .mark .skipcomplex
@@ -116,18 +122,20 @@ def test_subfunction(idx):
116122 with reverse_over_forward ():
117123 u = Function (space , name = "u" )
118124 u .sub (idx ).interpolate (- 2 * X [0 ])
125+ u_ref = u .copy (deepcopy = True )
119126 zeta = Function (space , name = "zeta" )
120127 zeta .sub (idx ).interpolate (X [0 ])
121128 u .block_variable .tlm_value = zeta .copy (deepcopy = True )
122129
123130 v = Function (space , name = "v" )
124131 v .sub (idx ).assign (u .sub (idx ))
125- J = assemble (u .sub (idx ) * v .sub (idx ) * dx )
132+ J = assemble (( u .sub (idx ) ** 2 ) * v .sub (idx ) * dx )
126133
127134 _ = compute_gradient (J .block_variable .tlm_value , Control (u ))
128135 adj_value = u .block_variable .adj_value
129- assert np .allclose (adj_value .sub (idx ).dat .data_ro ,
130- assemble (2 * inner (zeta [idx ], test [idx ]) * dx ).dat .data_ro [idx ])
136+ assert np .allclose (
137+ adj_value .sub (idx ).dat .data_ro ,
138+ assemble (6 * inner (u_ref [idx ] * zeta [idx ], test [idx ]) * dx ).dat .data_ro [idx ])
131139
132140
133141@pytest .mark .skipcomplex
@@ -140,16 +148,18 @@ def test_interpolate():
140148
141149 with reverse_over_forward ():
142150 u = Function (space_a , name = "u" ).interpolate (X [0 ] - 0.5 )
151+ u_ref = u .copy (deepcopy = True )
143152 zeta = Function (space_a , name = "zeta" ).interpolate (X [0 ])
144153 u .block_variable .tlm_value = zeta .copy (deepcopy = True )
145154
146155 v = Function (space_b , name = "v" ).interpolate (u )
147- J = assemble (v * v * dx )
156+ J = assemble (v ** 3 * dx )
148157
149158 _ = compute_gradient (J .block_variable .tlm_value , Control (u ))
150159 adj_value = u .block_variable .adj_value
151- assert np .allclose (adj_value .dat .data_ro ,
152- assemble (2 * inner (zeta , test_a ) * dx ).dat .data_ro )
160+ assert np .allclose (
161+ adj_value .dat .data_ro ,
162+ assemble (6 * inner (u_ref * zeta , test_a ) * dx ).dat .data_ro )
153163
154164
155165@pytest .mark .skipcomplex
@@ -162,16 +172,18 @@ def test_project():
162172
163173 with reverse_over_forward ():
164174 u = Function (space_a , name = "u" ).interpolate (X [0 ] - 0.5 )
175+ u_ref = u .copy (deepcopy = True )
165176 zeta = Function (space_a , name = "zeta" ).interpolate (X [0 ])
166177 u .block_variable .tlm_value = zeta .copy (deepcopy = True )
167178
168179 v = Function (space_b , name = "v" ).project (u )
169- J = assemble (v * v * dx )
180+ J = assemble (v ** 3 * dx )
170181
171182 _ = compute_gradient (J .block_variable .tlm_value , Control (u ))
172183 adj_value = u .block_variable .adj_value
173- assert np .allclose (adj_value .dat .data_ro ,
174- assemble (2 * inner (Function (space_b ).project (zeta ), test_a ) * dx ).dat .data_ro )
184+ assert np .allclose (
185+ adj_value .dat .data_ro ,
186+ assemble (6 * inner (Function (space_b ).project (u_ref ) * Function (space_b ).project (zeta ), test_a ) * dx ).dat .data_ro )
175187
176188
177189@pytest .mark .skipcomplex
@@ -183,6 +195,7 @@ def test_project_overwrite():
183195
184196 with reverse_over_forward ():
185197 u = Function (space , name = "u" ).interpolate (X [0 ] - 0.5 )
198+ u_ref = u .copy (deepcopy = True )
186199 zeta = Function (space , name = "zeta" ).interpolate (X [0 ])
187200 u .block_variable .tlm_value = zeta .copy (deepcopy = True )
188201
@@ -194,12 +207,13 @@ def test_project_overwrite():
194207 - 2 * zeta .dat .data_ro )
195208 assert np .allclose (w .block_variable .tlm_value .dat .data_ro ,
196209 - 2 * zeta .dat .data_ro )
197- J = assemble (w * w * dx )
210+ J = assemble (w ** 3 * dx )
198211
199212 _ = compute_gradient (J .block_variable .tlm_value , Control (u ))
200213 adj_value = u .block_variable .adj_value
201- assert np .allclose (adj_value .dat .data_ro ,
202- assemble (8 * inner (zeta , test ) * dx ).dat .data_ro )
214+ assert np .allclose (
215+ adj_value .dat .data_ro ,
216+ assemble (- 48 * inner (u_ref * zeta , test ) * dx ).dat .data_ro )
203217
204218
205219@pytest .mark .skipcomplex
@@ -217,12 +231,13 @@ def test_supermesh_project():
217231 u .block_variable .tlm_value = zeta .copy (deepcopy = True )
218232
219233 v = Function (space_b , name = "v" ).project (u )
220- J = assemble (v * v * dx )
234+ J = assemble (v ** 2 * dx )
221235
222236 _ = compute_gradient (J .block_variable .tlm_value , Control (u ))
223237 adj_value = u .block_variable .adj_value
224- assert np .allclose (adj_value .dat .data_ro ,
225- assemble (2 * inner (Function (space_a ).project (Function (space_b ).project (zeta )), test_a ) * dx ).dat .data_ro )
238+ assert np .allclose (
239+ adj_value .dat .data_ro ,
240+ assemble (2 * inner (Function (space_a ).project (Function (space_b ).project (zeta )), test_a ) * dx ).dat .data_ro )
226241
227242
228243@pytest .mark .skipcomplex
@@ -238,7 +253,7 @@ def test_dirichletbc():
238253 bc = DirichletBC (space , u , "on_boundary" )
239254
240255 v = project (Constant (0.0 ), space , bcs = bc )
241- J = assemble (v * v * v * dx )
256+ J = assemble (v ** 3 * dx )
242257
243258 J_hat = ReducedFunctional (J , Control (u ))
244259 assert taylor_test (J_hat , u , zeta , dJdm = J .block_variable .tlm_value ) > 1.9
0 commit comments