LCOV - code coverage report
Current view: top level - src/timeintegrators - ExplicitTimeIntegrator.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 419b9d Lines: 110 130 84.6 %
Date: 2025-08-08 20:01:16 Functions: 9 9 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //* This file is part of the MOOSE framework
       2             : //* https://mooseframework.inl.gov
       3             : //*
       4             : //* All rights reserved, see COPYRIGHT for full restrictions
       5             : //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
       6             : //*
       7             : //* Licensed under LGPL 2.1, please see LICENSE for details
       8             : //* https://www.gnu.org/licenses/lgpl-2.1.html
       9             : 
      10             : // MOOSE includes
      11             : #include "ExplicitTimeIntegrator.h"
      12             : #include "NonlinearSystem.h"
      13             : #include "FEProblem.h"
      14             : #include "PetscSupport.h"
      15             : #include "KernelBase.h"
      16             : #include "DGKernelBase.h"
      17             : #include "ScalarKernelBase.h"
      18             : #include "FVElementalKernel.h"
      19             : #include "FVFluxKernel.h"
      20             : #include "NodalKernelBase.h"
      21             : 
      22             : // libMesh includes
      23             : #include "libmesh/enum_convergence_flags.h"
      24             : 
      25             : using namespace libMesh;
      26             : 
      27             : InputParameters
      28       43791 : ExplicitTimeIntegrator::validParams()
      29             : {
      30       43791 :   InputParameters params = TimeIntegrator::validParams();
      31             : 
      32       43791 :   MooseEnum solve_type("consistent lumped lump_preconditioned", "consistent");
      33             : 
      34       43791 :   params.addParam<MooseEnum>(
      35             :       "solve_type",
      36             :       solve_type,
      37             :       "The way to solve the system.  A 'consistent' solve uses the full mass matrix and actually "
      38             :       "needs to use a linear solver to solve the problem.  'lumped' uses a lumped mass matrix with "
      39             :       "a simple inversion - incredibly fast but may be less accurate.  'lump_preconditioned' uses "
      40             :       "the lumped mass matrix as a preconditioner for the 'consistent' solve");
      41             : 
      42       87582 :   return params;
      43       43791 : }
      44             : 
      45         664 : ExplicitTimeIntegrator::ExplicitTimeIntegrator(const InputParameters & parameters)
      46             :   : TimeIntegrator(parameters),
      47             :     MeshChangedInterface(parameters),
      48             : 
      49         664 :     _solve_type(getParam<MooseEnum>("solve_type")),
      50         664 :     _explicit_residual(addVector("explicit_residual", false, PARALLEL)),
      51         664 :     _solution_update(addVector("solution_update", true, PARALLEL)),
      52        1328 :     _mass_matrix_diag_inverted(addVector("mass_matrix_diag_inverted", true, GHOSTED))
      53             : {
      54         664 :   _Ke_time_tag = _fe_problem.getMatrixTagID("TIME");
      55             : 
      56             :   // This effectively changes the default solve_type to LINEAR instead of PJFNK,
      57             :   // so that it is valid to not supply solve_type in the Executioner block:
      58         664 :   if (_nl)
      59         332 :     _fe_problem.solverParams(_nl->number())._type = Moose::ST_LINEAR;
      60             : 
      61         664 :   if (_solve_type == LUMPED || _solve_type == LUMP_PRECONDITIONED)
      62         164 :     _ones = addVector("ones", true, PARALLEL);
      63             :   // don't set any of the common SNES-related petsc options to prevent unused option warnings
      64         664 :   Moose::PetscSupport::dontAddCommonSNESOptions(_fe_problem);
      65         664 : }
      66             : 
      67             : void
      68         324 : ExplicitTimeIntegrator::initialSetup()
      69             : {
      70         324 :   meshChanged();
      71             : 
      72         324 :   if (_nl)
      73             :   {
      74         324 :     std::unordered_set<unsigned int> vars_to_check;
      75         324 :     if (!_vars.empty())
      76           0 :       vars_to_check = _vars;
      77             :     else
      78         661 :       for (const auto i : make_range(_nl->nVariables()))
      79         337 :         vars_to_check.insert(i);
      80             : 
      81         324 :     const auto mass_matrix_tag_id = massMatrixTagID();
      82         324 :     std::set<TagID> matrix_tags = {mass_matrix_tag_id};
      83         324 :     auto fv_object_starting_query = _fe_problem.theWarehouse()
      84         324 :                                         .query()
      85         324 :                                         .template condition<AttribMatrixTags>(matrix_tags)
      86         324 :                                         .template condition<AttribSysNum>(_nl->number());
      87             : 
      88         657 :     for (const auto var_id : vars_to_check)
      89             :     {
      90         337 :       const auto & var_name = _nl->system().variable_name(var_id);
      91         337 :       const bool field_var = _nl->hasVariable(var_name);
      92         337 :       if (!field_var)
      93             :         mooseAssert(_nl->hasScalarVariable(var_name),
      94             :                     var_name << " should be either a field or scalar variable");
      95         337 :       if (field_var)
      96             :       {
      97         281 :         const auto & field_var = _nl->getVariable(/*tid=*/0, var_name);
      98         281 :         if (field_var.isFV())
      99             :         {
     100           0 :           std::vector<FVElementalKernel *> fv_elemental_kernels;
     101           0 :           auto var_query = fv_object_starting_query.clone().template condition<AttribVar>(var_id);
     102           0 :           auto var_query_clone = var_query.clone();
     103           0 :           var_query.template condition<AttribSystem>("FVElementalKernel")
     104           0 :               .queryInto(fv_elemental_kernels);
     105           0 :           if (fv_elemental_kernels.size())
     106           0 :             continue;
     107             : 
     108           0 :           std::vector<FVFluxKernel *> fv_flux_kernels;
     109           0 :           var_query_clone.template condition<AttribSystem>("FVFluxKernel")
     110           0 :               .queryInto(fv_flux_kernels);
     111           0 :           if (fv_flux_kernels.size())
     112           0 :             continue;
     113           0 :         }
     114             :         else
     115             :         {
     116             :           // We are a finite element variable
     117         281 :           if (_nl->getKernelWarehouse()
     118         281 :                   .getMatrixTagObjectWarehouse(mass_matrix_tag_id, 0)
     119         281 :                   .hasVariableObjects(var_id))
     120         277 :             continue;
     121           4 :           if (_nl->getDGKernelWarehouse()
     122           4 :                   .getMatrixTagObjectWarehouse(mass_matrix_tag_id, 0)
     123           4 :                   .hasVariableObjects(var_id))
     124           0 :             continue;
     125           4 :           if (_nl->getNodalKernelWarehouse()
     126           4 :                   .getMatrixTagObjectWarehouse(mass_matrix_tag_id, 0)
     127           4 :                   .hasVariableObjects(var_id))
     128           0 :             continue;
     129             :         }
     130             :       }
     131          56 :       else if (_nl->getScalarKernelWarehouse()
     132          56 :                    .getMatrixTagObjectWarehouse(mass_matrix_tag_id, 0)
     133          56 :                    .hasVariableObjects(var_id))
     134          56 :         continue;
     135             : 
     136           4 :       mooseError("No objects contributing to the mass matrix were found for variable '",
     137             :                  var_name,
     138             :                  "'. Did you, e.g., forget a time derivative term?");
     139             :     }
     140         320 :   }
     141         320 : }
     142             : 
     143             : void
     144         298 : ExplicitTimeIntegrator::init()
     145             : {
     146         298 :   if (_nl && _fe_problem.solverParams(_nl->number())._type != Moose::ST_LINEAR)
     147           4 :     mooseError(
     148             :         "The chosen time integrator requires 'solve_type = LINEAR' in the Executioner block.");
     149         294 : }
     150             : 
     151             : void
     152        4716 : ExplicitTimeIntegrator::preSolve()
     153             : {
     154        4716 : }
     155             : 
     156             : void
     157         372 : ExplicitTimeIntegrator::meshChanged()
     158             : {
     159             :   // Can only be done after the system is initialized
     160         372 :   if (_solve_type == LUMPED || _solve_type == LUMP_PRECONDITIONED)
     161          82 :     *_ones = 1.;
     162             : 
     163         372 :   if (_solve_type == CONSISTENT || _solve_type == LUMP_PRECONDITIONED)
     164         320 :     _linear_solver = LinearSolver<Number>::build(comm());
     165             : 
     166         372 :   if (_solve_type == LUMP_PRECONDITIONED)
     167             :   {
     168          30 :     _preconditioner = std::make_unique<LumpedPreconditioner>(*_mass_matrix_diag_inverted);
     169          30 :     _linear_solver->attach_preconditioner(_preconditioner.get());
     170          30 :     _linear_solver->init();
     171             :   }
     172             : 
     173         372 :   if (_solve_type == CONSISTENT || _solve_type == LUMP_PRECONDITIONED)
     174         320 :     Moose::PetscSupport::setLinearSolverDefaults(_fe_problem, *_linear_solver);
     175         372 : }
     176             : 
     177             : bool
     178        4863 : ExplicitTimeIntegrator::performExplicitSolve(SparseMatrix<Number> & mass_matrix)
     179             : {
     180        4863 :   bool converged = false;
     181             : 
     182        4863 :   switch (_solve_type)
     183             :   {
     184        1899 :     case CONSISTENT:
     185             :     {
     186        1899 :       converged = solveLinearSystem(mass_matrix);
     187             : 
     188        1899 :       break;
     189             :     }
     190        2659 :     case LUMPED:
     191             :     {
     192             :       // Computes the sum of each row (lumping)
     193             :       // Note: This is actually how PETSc does it
     194             :       // It's not "perfectly optimal" - but it will be fast (and universal)
     195        2659 :       mass_matrix.vector_mult(*_mass_matrix_diag_inverted, *_ones);
     196             : 
     197             :       // "Invert" the diagonal mass matrix
     198        2659 :       _mass_matrix_diag_inverted->reciprocal();
     199             : 
     200             :       // Multiply the inversion by the RHS
     201        2659 :       _solution_update->pointwise_mult(*_mass_matrix_diag_inverted, *_explicit_residual);
     202             : 
     203             :       // Check for convergence by seeing if there is a nan or inf
     204        2659 :       auto sum = _solution_update->sum();
     205        2659 :       converged = std::isfinite(sum);
     206             : 
     207             :       // The linear iteration count remains zero
     208        2659 :       _n_linear_iterations = 0;
     209             : 
     210        2659 :       break;
     211             :     }
     212         305 :     case LUMP_PRECONDITIONED:
     213             :     {
     214         305 :       mass_matrix.vector_mult(*_mass_matrix_diag_inverted, *_ones);
     215         305 :       _mass_matrix_diag_inverted->reciprocal();
     216             : 
     217         305 :       converged = solveLinearSystem(mass_matrix);
     218             : 
     219         305 :       break;
     220             :     }
     221           0 :     default:
     222           0 :       mooseError("Unknown solve_type in ExplicitTimeIntegrator.");
     223             :   }
     224             : 
     225        4863 :   return converged;
     226             : }
     227             : 
     228             : bool
     229        2204 : ExplicitTimeIntegrator::solveLinearSystem(SparseMatrix<Number> & mass_matrix)
     230             : {
     231        2204 :   auto & es = _fe_problem.es();
     232             : 
     233             :   const auto num_its_and_final_tol =
     234        2628 :       _linear_solver->solve(mass_matrix,
     235        2204 :                             *_solution_update,
     236        2204 :                             *_explicit_residual,
     237         212 :                             es.parameters.get<Real>("linear solver tolerance"),
     238         212 :                             es.parameters.get<unsigned int>("linear solver maximum iterations"));
     239             : 
     240        2204 :   _n_linear_iterations += num_its_and_final_tol.first;
     241             : 
     242        2204 :   const bool converged = checkLinearConvergence();
     243             : 
     244        2204 :   return converged;
     245             : }
     246             : 
     247             : bool
     248        2204 : ExplicitTimeIntegrator::checkLinearConvergence()
     249             : {
     250        2204 :   auto reason = _linear_solver->get_converged_reason();
     251             : 
     252        2204 :   switch (reason)
     253             :   {
     254        2060 :     case CONVERGED_RTOL_NORMAL:
     255             :     case CONVERGED_ATOL_NORMAL:
     256             :     case CONVERGED_RTOL:
     257             :     case CONVERGED_ATOL:
     258             :     case CONVERGED_ITS:
     259             :     case CONVERGED_CG_NEG_CURVE:
     260             :     case CONVERGED_CG_CONSTRAINED:
     261             :     case CONVERGED_STEP_LENGTH:
     262             :     case CONVERGED_HAPPY_BREAKDOWN:
     263        2060 :       return true;
     264         144 :     case DIVERGED_NULL:
     265             :     case DIVERGED_ITS:
     266             :     case DIVERGED_DTOL:
     267             :     case DIVERGED_BREAKDOWN:
     268             :     case DIVERGED_BREAKDOWN_BICG:
     269             :     case DIVERGED_NONSYMMETRIC:
     270             :     case DIVERGED_INDEFINITE_PC:
     271             :     case DIVERGED_NAN:
     272             :     case DIVERGED_INDEFINITE_MAT:
     273             :     case CONVERGED_ITERATING:
     274             :     case DIVERGED_PCSETUP_FAILED:
     275         144 :       return false;
     276           0 :     default:
     277           0 :       mooseError("Unknown convergence reason in ExplicitTimeIntegrator.");
     278             :   }
     279             : }

Generated by: LCOV version 1.14