LCOV - code coverage report
Current view: top level - src/actions - SetAdaptivityOptionsAction.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: #32971 (54bef8) with base c6cf66 Lines: 72 76 94.7 %
Date: 2026-05-29 20:35:17 Functions: 4 4 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 "SetAdaptivityOptionsAction.h"
      11             : #include "FEProblem.h"
      12             : #include "RelationshipManager.h"
      13             : 
      14             : #include "libmesh/fe.h"
      15             : 
      16             : registerMooseAction("MooseApp", SetAdaptivityOptionsAction, "set_adaptivity_options");
      17             : registerMooseAction("MooseApp", SetAdaptivityOptionsAction, "add_geometric_rm");
      18             : registerMooseAction("MooseApp", SetAdaptivityOptionsAction, "add_algebraic_rm");
      19             : 
      20             : namespace Moose
      21             : {
      22             : InputParameters
      23        2345 : commonAdaptivityParams()
      24             : {
      25        2345 :   InputParameters params = Action::validParams();
      26        7035 :   params.addParam<unsigned int>(
      27        4690 :       "steps", 0, "The number of adaptive steps to use when doing a Steady simulation.");
      28       11725 :   params.addRangeCheckedParam<unsigned int>(
      29        4690 :       "interval", 1, "interval>0", "The number of time steps betweeen each adaptivity phase");
      30        7035 :   params.addParam<unsigned int>(
      31             :       "max_h_level",
      32        4690 :       0,
      33             :       "Maximum number of times a single element can be refined. If 0 then infinite.");
      34        7035 :   params.addParam<Real>("start_time",
      35        4690 :                         -std::numeric_limits<Real>::max(),
      36             :                         "The time that adaptivity will be active after.");
      37        7035 :   params.addParam<Real>("stop_time",
      38        4690 :                         std::numeric_limits<Real>::max(),
      39             :                         "The time after which adaptivity will no longer be active.");
      40        7035 :   params.addParam<unsigned int>(
      41             :       "cycles_per_step",
      42        4690 :       1,
      43             :       "The number of adaptive steps to use when on each timestep during a Transient simulation.");
      44        7035 :   params.addParam<bool>(
      45        4690 :       "recompute_markers_during_cycles", false, "Recompute markers during adaptivity cycles");
      46        9380 :   MooseEnum adaptivity("h=0 p=1 hp=2", "h");
      47        7035 :   params.addParam<MooseEnum>(
      48             :       "adaptivity_type", adaptivity, "Select between h, p or hp mesh adaptivity");
      49        4690 :   return params;
      50        2345 : }
      51             : }
      52             : 
      53             : InputParameters
      54        1875 : SetAdaptivityOptionsAction::validParams()
      55             : {
      56        1875 :   InputParameters params = Moose::commonAdaptivityParams();
      57        3750 :   params.addClassDescription("Action for defining adaptivity parameters.");
      58        7500 :   params.addParam<MarkerName>("marker",
      59             :                               "The name of the Marker to use to actually adapt the mesh.");
      60        5625 :   params.addParam<unsigned int>(
      61        3750 :       "initial_steps", 0, "The number of adaptive steps to do based on the initial condition.");
      62        7500 :   params.addParam<MarkerName>(
      63             :       "initial_marker",
      64             :       "The name of the Marker to use to adapt the mesh during initial refinement.");
      65        5625 :   params.addParamNamesToGroup("initial_steps initial_marker", "Initial Adaptivity");
      66        1875 :   return params;
      67           0 : }
      68             : 
      69        1831 : SetAdaptivityOptionsAction::SetAdaptivityOptionsAction(const InputParameters & params)
      70        1831 :   : Action(params)
      71             : {
      72        1831 : }
      73             : 
      74             : void
      75        5469 : SetAdaptivityOptionsAction::act()
      76             : {
      77             :   // Here we are going to mostly mimic the default ghosting in libmesh
      78             :   // By default libmesh adds:
      79             :   // 1) GhostPointNeighbors on the mesh
      80             :   // 2) DefaultCoupling with 1 layer as an algebraic ghosting functor on the dof_map, which also
      81             :   //    gets added to the mesh at the time a new System is added
      82             :   // 3) DefaultCoupling with 0 layers as a coupling functor on the dof_map, which also gets added to
      83             :   //    the mesh at the time a new System is added
      84             :   //
      85             :   // What we will do differently is:
      86             :   // - The 3rd ghosting functor adds nothing so we will not add it at all
      87             : 
      88        5469 :   if (_current_task == "add_algebraic_rm")
      89             :   {
      90        5457 :     auto rm_params = _factory.getValidParams("ElementSideNeighborLayers");
      91             : 
      92        1819 :     rm_params.set<std::string>("for_whom") = "Adaptivity";
      93        5457 :     rm_params.set<MooseMesh *>("mesh") = _mesh.get();
      94        1819 :     rm_params.set<Moose::RelationshipManagerType>("rm_type") =
      95             :         Moose::RelationshipManagerType::ALGEBRAIC;
      96             : 
      97        1819 :     if (rm_params.areAllRequiredParamsValid())
      98             :     {
      99        1819 :       auto rm_obj = _factory.create<RelationshipManager>(
     100        5457 :           "ElementSideNeighborLayers", "adaptivity_algebraic_ghosting", rm_params);
     101             : 
     102             :       // Delete the resources created on behalf of the RM if it ends up not being added to the
     103             :       // App.
     104        1819 :       if (!_app.addRelationshipManager(rm_obj))
     105         773 :         _factory.releaseSharedObjects(*rm_obj);
     106        1819 :     }
     107             :     else
     108           0 :       mooseError("Invalid initialization of ElementSideNeighborLayers");
     109        1819 :   }
     110             : 
     111        3650 :   else if (_current_task == "add_geometric_rm")
     112             :   {
     113        5475 :     auto rm_params = _factory.getValidParams("ElementPointNeighborLayers");
     114             : 
     115        1825 :     rm_params.set<std::string>("for_whom") = "Adaptivity";
     116        5475 :     rm_params.set<MooseMesh *>("mesh") = _mesh.get();
     117        1825 :     rm_params.set<Moose::RelationshipManagerType>("rm_type") =
     118             :         Moose::RelationshipManagerType::GEOMETRIC;
     119             : 
     120        1825 :     if (rm_params.areAllRequiredParamsValid())
     121             :     {
     122        1825 :       auto rm_obj = _factory.create<RelationshipManager>(
     123        5475 :           "ElementPointNeighborLayers", "adaptivity_geometric_ghosting", rm_params);
     124             : 
     125             :       // Delete the resources created on behalf of the RM if it ends up not being added to the
     126             :       // App.
     127        1825 :       if (!_app.addRelationshipManager(rm_obj))
     128           0 :         _factory.releaseSharedObjects(*rm_obj);
     129        1825 :     }
     130             :     else
     131           0 :       mooseError("Invalid initialization of ElementPointNeighborLayers");
     132        1825 :   }
     133             : 
     134        1825 :   else if (_current_task == "set_adaptivity_options")
     135             :   {
     136        1825 :     Adaptivity & adapt = _problem->adaptivity();
     137             : 
     138        5475 :     if (isParamValid("marker"))
     139        3426 :       adapt.setMarkerVariableName(getParam<MarkerName>("marker"));
     140        5475 :     if (isParamValid("initial_marker"))
     141        2010 :       adapt.setInitialMarkerVariableName(getParam<MarkerName>("initial_marker"));
     142             : 
     143        3650 :     adapt.setCyclesPerStep(getParam<unsigned int>("cycles_per_step"));
     144             : 
     145        3650 :     adapt.setMaxHLevel(getParam<unsigned int>("max_h_level"));
     146             : 
     147        1825 :     adapt.init(getParam<unsigned int>("steps"),
     148        5475 :                getParam<unsigned int>("initial_steps"),
     149        5475 :                getParam<MooseEnum>("adaptivity_type").getEnum<AdaptivityType>());
     150        1825 :     adapt.setUseNewSystem();
     151             : 
     152        7300 :     adapt.setTimeActive(getParam<Real>("start_time"), getParam<Real>("stop_time"));
     153        3650 :     adapt.setInterval(getParam<unsigned int>("interval"));
     154             : 
     155        5475 :     adapt.setRecomputeMarkersFlag(getParam<bool>("recompute_markers_during_cycles"));
     156             :   }
     157        5469 : }

Generated by: LCOV version 1.14