LCOV - code coverage report
Current view: top level - src/mfem/variables - MFEMVariable.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: #33390 (250e9c) with base 846a5c Lines: 59 62 95.2 %
Date: 2026-07-31 18:15:22 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             : #ifdef MOOSE_MFEM_ENABLED
      11             : 
      12             : #include "MFEMVariable.h"
      13             : #include "MFEMProblem.h"
      14             : #include "MFEMFESpace.h"
      15             : #include "MFEMFESpaceHierarchy.h"
      16             : #include "MooseVariableBase.h"
      17             : #include "MFEMVectorMagnitudeCoefficient.h"
      18             : 
      19             : registerMooseObject("MooseApp", MFEMVariable);
      20             : 
      21             : InputParameters
      22        9607 : MFEMVariable::validParams()
      23             : {
      24        9607 :   InputParameters params = MFEMObject::validParams();
      25             :   // Create user-facing 'boundary' input for restricting inheriting object to boundaries.
      26       38428 :   params.addParam<MFEMFESpaceName>("fespace",
      27             :                                    "The finite element space this variable is defined on.");
      28       28821 :   params.addParam<std::string>(
      29             :       "fespace_hierarchy",
      30             :       "Name of a MFEMFESpaceHierarchy the variable lives on its finest level. "
      31             :       "Mutually exclusive with 'fespace'.");
      32             :   // Require moose variable parameters (not used!)
      33        9607 :   params += MooseVariableBase::validParams();
      34       19214 :   params.addClassDescription(
      35             :       "Class for adding MFEM variables to the problem (`mfem::ParGridFunction`s).");
      36       19214 :   params.registerBase("MooseVariableBase");
      37       19214 :   params.registerSystemAttributeName("MooseVariableBase");
      38       28821 :   params.addParam<VariableName>(
      39             :       "time_derivative",
      40             :       "Optional name to assign to the time derivative of the variable in transient problems.");
      41        9607 :   return params;
      42           0 : }
      43             : 
      44        3902 : MFEMVariable::MFEMVariable(const InputParameters & parameters)
      45             :   : MFEMObject(parameters),
      46        3914 :     _time_derivative_name(
      47       11706 :         isParamValid("time_derivative")
      48        3902 :             ? getParam<VariableName>("time_derivative")
      49             :             : VariableName(
      50        3890 :                   getMFEMProblem().getProblemData().time_derivative_map.createTimeDerivativeName(
      51        7792 :                       name())))
      52             : {
      53        7804 :   const bool has_fespace = isParamSetByUser("fespace");
      54        7804 :   const bool has_hierarchy = isParamSetByUser("fespace_hierarchy");
      55             : 
      56        3902 :   if (has_fespace && has_hierarchy)
      57           0 :     paramError("fespace_hierarchy", "Cannot specify both 'fespace' and 'fespace_hierarchy'.");
      58        3902 :   if (!has_fespace && !has_hierarchy)
      59           0 :     paramError("fespace", "Either 'fespace' or 'fespace_hierarchy' must be provided.");
      60             : 
      61        3902 :   if (has_fespace)
      62             :   {
      63        7782 :     const auto & fespace = getMFEMProblem().getMFEMObject<MFEMFESpace>(
      64       11673 :         "MFEMFESpace", getParam<MFEMFESpaceName>("fespace"));
      65        3891 :     _par_fespace = fespace.getFESpace();
      66        3891 :     _is_scalar = fespace.isScalar();
      67             :   }
      68             :   else
      69             :   {
      70          22 :     const auto & hierarchy_name = getParam<std::string>("fespace_hierarchy");
      71          22 :     const auto & hierarchy = getMFEMProblem().getMFEMObject<MFEMFESpaceHierarchy>(
      72             :         "MFEMFESpaceHierarchy", hierarchy_name);
      73          11 :     _par_fespace = getMFEMProblem().getProblemData().fespaces.GetShared(hierarchy_name);
      74          11 :     _is_scalar = hierarchy.isScalar();
      75             :   }
      76             : 
      77        3902 :   _gridfunction = buildGridFunction();
      78        3902 :   *_gridfunction = 0.0;
      79        3902 : }
      80             : 
      81             : const std::shared_ptr<mfem::ParGridFunction>
      82        3902 : MFEMVariable::buildGridFunction()
      83             : {
      84        3902 :   return std::make_shared<mfem::ParGridFunction>(_par_fespace.get());
      85             : }
      86             : 
      87             : void
      88        3902 : MFEMVariable::declareCoefficients()
      89             : {
      90        3902 :   const int cont_type = _par_fespace->FEColl()->GetContType();
      91        3902 :   if (_is_scalar)
      92             :   {
      93        4688 :     getMFEMProblem().getCoefficients().declareScalar<mfem::GridFunctionCoefficient>(
      94        4688 :         name(), getGridFunction().get());
      95             :     // If gradient is well-defined on this variable, create auxiliary coefficient
      96        2344 :     if (cont_type == mfem::FiniteElementCollection::CONTINUOUS)
      97             :     {
      98        3500 :       getMFEMProblem().getCoefficients().declareVector<mfem::GradientGridFunctionCoefficient>(
      99        3500 :           name() + "_grad", getGridFunction().get());
     100        5250 :       getMFEMProblem().getCoefficients().declareScalar<MFEMVectorMagnitudeCoefficient>(
     101        3500 :           name() + "_grad_mag",
     102        3500 :           getMFEMProblem().getCoefficients().getVectorCoefficient(name() + "_grad"));
     103             :     }
     104             :   }
     105             :   else
     106             :   {
     107        3116 :     getMFEMProblem().getCoefficients().declareVector<mfem::VectorGridFunctionCoefficient>(
     108        3116 :         name(), getGridFunction().get());
     109        4674 :     getMFEMProblem().getCoefficients().declareScalar<MFEMVectorMagnitudeCoefficient>(
     110        4674 :         name() + "_mag", getMFEMProblem().getCoefficients().getVectorCoefficient(name()));
     111             :     // If curl is well-defined on this variable, create auxiliary coefficient
     112        1558 :     if (cont_type == mfem::FiniteElementCollection::TANGENTIAL ||
     113             :         cont_type == mfem::FiniteElementCollection::CONTINUOUS)
     114             :     {
     115        2108 :       getMFEMProblem().getCoefficients().declareVector<mfem::CurlGridFunctionCoefficient>(
     116        2108 :           name() + "_curl", getGridFunction().get());
     117        3162 :       getMFEMProblem().getCoefficients().declareScalar<MFEMVectorMagnitudeCoefficient>(
     118        2108 :           name() + "_curl_mag",
     119        2108 :           getMFEMProblem().getCoefficients().getVectorCoefficient(name() + "_curl"));
     120             :     }
     121             :     // If divergence is well-defined on this variable, create auxiliary coefficient
     122        1558 :     if (cont_type == mfem::FiniteElementCollection::NORMAL ||
     123             :         cont_type == mfem::FiniteElementCollection::CONTINUOUS)
     124        1032 :       getMFEMProblem().getCoefficients().declareScalar<mfem::DivergenceGridFunctionCoefficient>(
     125        1032 :           name() + "_div", getGridFunction().get());
     126             :   }
     127        3902 : }
     128             : 
     129             : #endif

Generated by: LCOV version 1.14