From: Wolfgang Bangerth Date: Wed, 31 Mar 2021 15:51:53 +0000 (-0600) Subject: Use an easier-to-read do...while loop. X-Git-Tag: v9.3.0-rc1~248^2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=afbdd848e1af9b6148dbb4fca6a5d80a06532186;p=dealii.git Use an easier-to-read do...while loop. While there, also rename a variable again. --- diff --git a/examples/step-15/step-15.cc b/examples/step-15/step-15.cc index aedf27f930..5a8b673d11 100644 --- a/examples/step-15/step-15.cc +++ b/examples/step-15/step-15.cc @@ -603,13 +603,15 @@ namespace Step15 setup_system(/*first time=*/true); set_boundary_values(); - // The Newton iteration starts next. During the first step we do not have - // information about the residual prior to this step and so we continue - // the Newton iteration until we have reached at least one iteration and - // until residual is less than $10^{-3}$. - double previous_residual = 0; - unsigned int refinement_cycle = 0; - while ((refinement_cycle == 0) || (previous_residual > 1e-3)) + // The Newton iteration starts next. We iterate until the (norm of the) + // residual computed at the end of the previous iteration is less than + // $10^{-3}$, as checked at the end of the `do { ... } while` loop that + // starts here. Because we don't have a reasonable value to initialize + // the variable, we just use the largest value that can be represented + // as a `double`. + double last_residual_norm = std::numeric_limits::max(); + unsigned int refinement_cycle = 0; + do { std::cout << "Mesh refinement step " << refinement_cycle << std::endl; @@ -631,7 +633,7 @@ namespace Step15 ++inner_iteration) { assemble_system(); - previous_residual = system_rhs.l2_norm(); + last_residual_norm = system_rhs.l2_norm(); solve(); @@ -656,6 +658,7 @@ namespace Step15 ++refinement_cycle; std::cout << std::endl; } + while (last_residual_norm > 1e-3); } } // namespace Step15