From d02c60cbb26a6e1a8d6eba5648c412db140c2076 Mon Sep 17 00:00:00 2001 From: Stefano Zampini Date: Tue, 1 Aug 2023 16:43:50 +0200 Subject: [PATCH] implement Wolfgang's comments --- include/deal.II/lac/petsc_ts.h | 17 ++++++----- include/deal.II/lac/petsc_ts.templates.h | 39 +++++++++--------------- tests/petsc/petsc_ts_03.cc | 4 +-- 3 files changed, 26 insertions(+), 34 deletions(-) diff --git a/include/deal.II/lac/petsc_ts.h b/include/deal.II/lac/petsc_ts.h index 72b6e818f7..a994e8b146 100644 --- a/include/deal.II/lac/petsc_ts.h +++ b/include/deal.II/lac/petsc_ts.h @@ -269,19 +269,20 @@ namespace PETScWrappers * and TimeStepper::explicit_jacobian. * - deal.II style using TimeStepper::setup_jacobian and * TimeStepper::solve_with_jacobian. - * The preconditioning matrix can be specified using TimeStepper::set_matrix. - * In case both approaches are implemented, the deal.II style will be used. + * The preconditioning matrix can be specified using + * TimeStepper::set_matrix(). In case both approaches are implemented, the + * deal.II style will be used. * - * TimeStepper::set_matrices must be used in case the user wants to provide + * TimeStepper::set_matrices() must be used in case the user wants to provide * the iteration matrix of the tangent system in the deal.II style approach, * thus replacing the matrix-free linearization. * * The correctness of the constructed Jacobians passed using - * TimeStepper::set_matrix can be checked using + * TimeStepper::set_matrix() can be checked using * @code * ./myApp -snes_test_jacobian * @endcode - * See TimeStepper::set_matrix and TimeStepper::set_matrices for + * See TimeStepper::set_matrix() and TimeStepper::set_matrices() for * additional details. * * The deal.II style approach still allows command line customization, like @@ -618,7 +619,7 @@ namespace PETScWrappers std::function distribute; /** - * Callback to setup mesh adaption. + * Callback to set up mesh adaption. * * Implementation of this function is optional. * @p resize must be set to true if mesh adaption is to be performed, false @@ -637,13 +638,13 @@ namespace PETScWrappers const unsigned int step, const VectorType & y, bool & resize)> - prepare_for_coarsening_and_refinement; + decide_for_coarsening_and_refinement; /** * Callback to interpolate vectors and perform mesh adaption. * * Implementation of this function is mandatory if - * TimeStepper::prepare_for_coarsening_and_refinement is used. + * TimeStepper::decide_for_coarsening_and_refinement is used. * This function must perform mesh adaption and interpolate the discrete * functions that are stored in @p all_in onto the refined and/or coarsenend grid. * Output vectors must be created inside the callback. diff --git a/include/deal.II/lac/petsc_ts.templates.h b/include/deal.II/lac/petsc_ts.templates.h index 2b8fadd5da..747b23583c 100644 --- a/include/deal.II/lac/petsc_ts.templates.h +++ b/include/deal.II/lac/petsc_ts.templates.h @@ -439,7 +439,7 @@ namespace PETScWrappers const int lineno = __LINE__; const int err = call_and_possibly_capture_ts_exception( - user->prepare_for_coarsening_and_refinement, + user->decide_for_coarsening_and_refinement, user->pending_exception, []() -> void {}, t, @@ -451,7 +451,7 @@ namespace PETScWrappers return PetscError( PetscObjectComm((PetscObject)ts), lineno + 1, - "prepare_for_coarsening_and_refinement", + "decide_for_coarsening_and_refinement", __FILE__, PETSC_ERR_LIB, PETSC_ERROR_INITIAL, @@ -532,9 +532,13 @@ namespace PETScWrappers PetscCall(TSGetApplicationContext(ts, &ctx)); auto user = static_cast(ctx); - PetscErrorCode ( - *transfer_setup)(TS, PetscInt, PetscReal, Vec, PetscBool *, void *); - PetscErrorCode (*transfer)(TS, PetscInt, Vec[], Vec[], void *); + using transfer_setup_fn = + PetscErrorCode (*)(TS, PetscInt, PetscReal, Vec, PetscBool *, void *); + using transfer_fn = + PetscErrorCode (*)(TS, PetscInt, Vec[], Vec[], void *); + transfer_setup_fn transfer_setup; + transfer_fn transfer; + AssertPETSc(PetscObjectQueryFunction((PetscObject)ts, "__dealii_ts_resize_setup__", &transfer_setup)); @@ -546,7 +550,7 @@ namespace PETScWrappers Vec x; PetscCall(TSGetSolution(ts, &x)); PetscCall(PetscObjectReference((PetscObject)x)); - PetscCall((*transfer_setup)( + PetscCall(transfer_setup( ts, user->get_step_number(), user->get_time(), x, &resize, user)); if (resize) @@ -558,7 +562,7 @@ namespace PETScWrappers // Reinitialize DM. Currently it is not possible to do with public API ts_reset_dm(ts); - PetscCall((*transfer)(ts, 1, &x, &new_x, user)); + PetscCall(transfer(ts, 1, &x, &new_x, user)); PetscCall(TSSetSolution(ts, new_x)); PetscCall(VecDestroy(&new_x)); } @@ -1035,7 +1039,7 @@ namespace PETScWrappers AssertPETSc(TSMonitorSet(ts, ts_monitor, this, nullptr)); // Handle AMR. - if (prepare_for_coarsening_and_refinement) + if (decide_for_coarsening_and_refinement) { AssertThrow(interpolate, StandardExceptions::ExcFunctionNotProvided("interpolate")); @@ -1129,14 +1133,8 @@ namespace PETScWrappers unsigned int TimeStepper::solve( VectorType &y) { - try - { - setup_callbacks(); - } - catch (...) - { - throw; - } + // Set up PETSc data structure. + setup_callbacks(); // Set solution vector. AssertPETSc(TSSetSolution(ts, y.petsc_vector())); @@ -1147,14 +1145,7 @@ namespace PETScWrappers // Handle algebraic components. This must be done when we know the layout // of the solution vector. - try - { - setup_algebraic_constraints(y); - } - catch (...) - { - throw; - } + setup_algebraic_constraints(y); // Having set everything up, now do the actual work // and let PETSc do the time stepping. If there is diff --git a/tests/petsc/petsc_ts_03.cc b/tests/petsc/petsc_ts_03.cc index d98fd0b03e..88f73d0d9d 100644 --- a/tests/petsc/petsc_ts_03.cc +++ b/tests/petsc/petsc_ts_03.cc @@ -178,7 +178,7 @@ public: }; // This callback is used to decide to remesh. - time_stepper.prepare_for_coarsening_and_refinement = + time_stepper.decide_for_coarsening_and_refinement = [&](const real_type t, const unsigned int step, const VectorType &, @@ -187,7 +187,7 @@ public: resize = (step && step % 5 == 0); }; - // This callback is called if prepare_for_coarsening_and_refinement sets + // This callback is called if decide_for_coarsening_and_refinement sets // resize to true. time_stepper.interpolate = [&](const std::vector &all_in, std::vector &all_out) -> void { -- 2.39.5