@@ -24,7 +24,7 @@ def mesh_2D():
2424
2525
2626@pytest .mark .parametrize ("use_mixed_space" , [True , False ])
27- def test_solver (use_mixed_space : bool , mesh_2D ):
27+ def test_solver (use_mixed_space : bool , mesh_2D , assert_hessian_matches_finite_difference ):
2828 pyadjoint .get_working_tape ().clear_tape ()
2929 mesh = mesh_2D
3030 el_u = basix .ufl .element ("P" , mesh .basix_cell (), 2 , shape = (mesh .geometry .dim ,))
@@ -116,13 +116,10 @@ def L1(mesh, q):
116116 f"Expected convergence rate close to 2.0, got { min_rate } "
117117 )
118118
119- # Scale perturbation for hessian
120- Jh (d )
121- dJdm = Jh .derivative ()._ad_dot (e )
122- hessian = Jh .hessian (e )
123- dHddu = hessian ._ad_dot (e )
124- min_rate = pyadjoint .taylor_test (Jh , d , e , dJdm = dJdm , Hm = dHddu )
125- assert np .isclose (min_rate , 3.0 , rtol = 0.1 , atol = 0.1 ), f"Expected convergence rate close to 3.0, got { min_rate } "
119+ # This is a saddle-point (velocity/pressure) system -- the standard rate-3
120+ # taylor_test's cubic remainder is not a robust check on it (see
121+ # assert_hessian_matches_finite_difference's own docstring in conftest.py).
122+ assert_hessian_matches_finite_difference (Jh , d , e )
126123
127124 z = Function (Z )
128125 z .interpolate (
@@ -133,16 +130,11 @@ def L1(mesh, q):
133130 lambda x : (0.8 * baseline * x [1 ] ** 2 , 2 * baseline * x [0 ] ** 2 )
134131 ) # NOTE: Has to be divergence free
135132 f .x .scatter_forward ()
136- Jh (z )
137- dJdm = Jh .derivative ()._ad_dot (f )
138- hessian = Jh .hessian (f )
139- dHddu = hessian ._ad_dot (f )
140- min_rate = pyadjoint .taylor_test (Jh , z , f , dJdm = dJdm , Hm = dHddu )
141- assert np .isclose (min_rate , 3.0 , rtol = 0.1 , atol = 0.1 ), f"Expected convergence rate close to 3.0, got { min_rate } "
133+ assert_hessian_matches_finite_difference (Jh , z , f )
142134
143135
144136@pytest .mark .parametrize ("use_mixed_space" , [True , False ])
145- def test_nonlinear_solver (use_mixed_space : bool , mesh_2D ):
137+ def test_nonlinear_solver (use_mixed_space : bool , mesh_2D , assert_hessian_matches_finite_difference ):
146138 """As ``test_solver``, but for a blocked ``NonlinearProblem``: a Navier-Stokes-like
147139 velocity/pressure system with a viscosity control, genuinely nonlinear in the state
148140 via the convective term, exercising the same forward/adjoint/TLM/Hessian paths as
@@ -241,12 +233,10 @@ def test_nonlinear_solver(use_mixed_space: bool, mesh_2D):
241233 f"Expected convergence rate close to 2.0, got { min_rate } "
242234 )
243235
244- Jh (d )
245- dJdm = Jh .derivative ()._ad_dot (e )
246- hessian = Jh .hessian (e )
247- dHddu = hessian ._ad_dot (e )
248- min_rate = pyadjoint .taylor_test (Jh , d , e , dJdm = dJdm , Hm = dHddu )
249- assert np .isclose (min_rate , 3.0 , rtol = 0.1 , atol = 0.1 ), f"Expected convergence rate close to 3.0, got { min_rate } "
236+ # This is a saddle-point (velocity/pressure) system -- the standard rate-3
237+ # taylor_test's cubic remainder is not a robust check on it (see
238+ # assert_hessian_matches_finite_difference's own docstring in conftest.py).
239+ assert_hessian_matches_finite_difference (Jh , d , e )
250240
251241 # A second, independent evaluation point/direction: a cached-but-unrefreshed
252242 # adjoint/TLM/Hessian operator (see tests/test_tlm_update.py) could pass the
@@ -258,9 +248,4 @@ def test_nonlinear_solver(use_mixed_space: bool, mesh_2D):
258248 assert np .min (mu2 .x .array - h2 .x .array ) > 0.0 , (
259249 "Taylor test perturbation must not violate positivity of viscosity"
260250 )
261- Jh (mu2 )
262- dJdm = Jh .derivative ()._ad_dot (h2 )
263- hessian = Jh .hessian (h2 )
264- dHddu = hessian ._ad_dot (h2 )
265- min_rate = pyadjoint .taylor_test (Jh , mu2 , h2 , dJdm = dJdm , Hm = dHddu )
266- assert np .isclose (min_rate , 3.0 , rtol = 0.1 , atol = 0.1 ), f"Expected convergence rate close to 3.0, got { min_rate } "
251+ assert_hessian_matches_finite_difference (Jh , mu2 , h2 )
0 commit comments