]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Properly treat exceptions in a user callback in NOX. 15237/head
authorWolfgang Bangerth <bangerth@colostate.edu>
Thu, 18 May 2023 04:42:58 +0000 (22:42 -0600)
committerWolfgang Bangerth <bangerth@colostate.edu>
Thu, 18 May 2023 04:42:58 +0000 (22:42 -0600)
include/deal.II/trilinos/nox.templates.h

index 57d8974504356a9b1053d62ce55d2ac7baa67c84..ef7b94e875e096aed07548212d12361a1c53a388 100644 (file)
@@ -1054,18 +1054,23 @@ namespace TrilinosWrappers
           }
         else if (solve_with_jacobian_and_track_n_linear_iterations)
           {
-            // with tracking of linear iterations
-            const int n_linear_iterations =
-              solve_with_jacobian_and_track_n_linear_iterations(f,
-                                                                x,
-                                                                tolerance);
-
-            if (n_linear_iterations == -1)
-              return 1;
-
-            this->n_last_linear_iterations = n_linear_iterations;
-
-            return 0;
+            // With tracking of linear iterations. The callback function we
+            // have here is using an integer return type, which is not
+            // what internal::NOXWrappers::call_and_possibly_capture_exception
+            // knows how to deal with. As a consequence, package the call
+            // and assignment of the return value into a lambda function;
+            // if the call to the user-defined callback fails, this will
+            // trigger an exception that will propagate through the lambda
+            // function and be treated correctly by the logic in
+            // internal::NOXWrappers::call_and_possibly_capture_exception.
+            return internal::NOXWrappers::call_and_possibly_capture_exception(
+              [&]() {
+                this->n_last_linear_iterations =
+                  solve_with_jacobian_and_track_n_linear_iterations(f,
+                                                                    x,
+                                                                    tolerance);
+              },
+              pending_exception);
           }
         else
           {

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.