LCOV - code coverage report
Current view: top level - src/actions - SetupResidualDebugAction.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 2bf808 Lines: 45 47 95.7 %
Date: 2025-07-17 01:28:37 Functions: 3 3 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             : #include "SetupResidualDebugAction.h"
      11             : 
      12             : // MOOSE includes
      13             : #include "ActionWarehouse.h"
      14             : #include "Factory.h"
      15             : #include "FEProblem.h"
      16             : #include "MooseVariableFE.h"
      17             : #include "NonlinearSystemBase.h"
      18             : #include "Conversion.h"
      19             : #include "AddVariableAction.h"
      20             : #include "libmesh/string_to_enum.h"
      21             : 
      22             : registerMooseAction("MooseApp", SetupResidualDebugAction, "setup_residual_debug");
      23             : 
      24             : InputParameters
      25        1827 : SetupResidualDebugAction::validParams()
      26             : {
      27        1827 :   InputParameters params = Action::validParams();
      28        1827 :   params.addParam<std::vector<NonlinearVariableName>>(
      29             :       "show_var_residual", {}, "Variables for which residuals will be sent to the output file.");
      30        1827 :   params.addClassDescription(
      31             :       "Adds the necessary objects for computing the residuals for individual variables.");
      32        1827 :   return params;
      33           0 : }
      34             : 
      35        1623 : SetupResidualDebugAction::SetupResidualDebugAction(const InputParameters & parameters)
      36             :   : Action(parameters),
      37        1623 :     _show_var_residual(getParam<std::vector<NonlinearVariableName>>("show_var_residual"))
      38             : {
      39        1623 : }
      40             : 
      41             : void
      42        1611 : SetupResidualDebugAction::act()
      43             : {
      44        1611 :   if (_problem.get() == NULL)
      45           0 :     return;
      46             : 
      47             :   // debug variable residuals
      48        1651 :   for (const auto & var_name : _show_var_residual)
      49             :   {
      50             :     // Get the variable
      51          40 :     MooseVariableFEBase & var = _problem->getVariable(
      52             :         0, var_name, Moose::VarKindType::VAR_SOLVER, Moose::VarFieldType::VAR_FIELD_STANDARD);
      53             : 
      54             :     // Turn on variable residual debugging in the system of the variable
      55             :     // TODO: Add linear system support
      56          40 :     _problem->getNonlinearSystemBase(var.sys().number()).debuggingResiduals(true);
      57             : 
      58             :     // add aux-variable
      59          40 :     auto order = AddVariableAction::getNonlinearVariableOrders();
      60          40 :     auto family = AddVariableAction::getNonlinearVariableFamilies();
      61             : 
      62          40 :     auto fe_type = var.feType();
      63          40 :     order = Utility::enum_to_string<Order>(fe_type.order);
      64          40 :     family = Utility::enum_to_string(fe_type.family);
      65             : 
      66          40 :     InputParameters params = _factory.getValidParams("DebugResidualAux");
      67          40 :     const std::set<SubdomainID> & subdomains = var.activeSubdomains();
      68             : 
      69          40 :     std::stringstream aux_var_ss;
      70          40 :     aux_var_ss << "residual_" << var.name();
      71          40 :     std::string aux_var_name = aux_var_ss.str();
      72             : 
      73          40 :     auto var_params = _factory.getValidParams("MooseVariable");
      74          40 :     var_params.set<MooseEnum>("family") = family;
      75          40 :     var_params.set<MooseEnum>("order") = order;
      76             : 
      77          40 :     if (subdomains.empty())
      78          20 :       _problem->addAuxVariable("MooseVariable", aux_var_name, var_params);
      79             :     else
      80             :     {
      81          20 :       std::vector<SubdomainName> block_names;
      82          20 :       block_names.reserve(subdomains.size());
      83          40 :       for (const SubdomainID & id : subdomains)
      84          20 :         block_names.push_back(Moose::stringify(id));
      85          20 :       params.set<std::vector<SubdomainName>>("block") = block_names;
      86          20 :       var_params.set<std::vector<SubdomainName>>("block") = block_names;
      87             : 
      88          20 :       _problem->addAuxVariable("MooseVariable", aux_var_name, var_params);
      89          20 :     }
      90             : 
      91             :     // add aux-kernel
      92          40 :     std::stringstream kern_ss;
      93          40 :     kern_ss << "residual_" << var.name() << "_kernel";
      94          40 :     std::string kern_name = kern_ss.str();
      95             : 
      96          40 :     params.set<AuxVariableName>("variable") = aux_var_name;
      97          40 :     params.set<NonlinearVariableName>("debug_variable") = var.name();
      98         120 :     params.set<ExecFlagEnum>("execute_on") = {EXEC_LINEAR, EXEC_TIMESTEP_END};
      99          40 :     _problem->addAuxKernel("DebugResidualAux", kern_name, params);
     100          40 :   }
     101          40 : }

Generated by: LCOV version 1.14