LCOV - code coverage report
Current view: top level - src/mfem/utils - CoefficientManager.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: #33380 (547b29) with base 8581c3 Lines: 88 88 100.0 %
Date: 2026-07-20 19:36:28 Functions: 25 25 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 "MooseStringUtils.h"
      13             : #include "CoefficientManager.h"
      14             : #include "MFEMQuadratureFunctionCoefficientBase.h"
      15             : #include <algorithm>
      16             : 
      17             : namespace Moose::MFEM
      18             : {
      19             : 
      20             : mfem::Coefficient &
      21       12325 : CoefficientManager::declareScalar(const std::string & name, std::shared_ptr<mfem::Coefficient> coef)
      22             : {
      23       12325 :   this->_scalar_coeffs.addCoefficient(name, coef);
      24       12323 :   return *coef;
      25             : }
      26             : 
      27             : mfem::Coefficient &
      28           8 : CoefficientManager::declareScalar(const std::string & name, const std::string & existing_or_literal)
      29             : {
      30           8 :   return this->declareScalar(name, this->getScalarCoefficientPtr(existing_or_literal));
      31             : }
      32             : 
      33             : mfem::Coefficient &
      34         401 : CoefficientManager::declareScalarProperty(const std::string & name,
      35             :                                           const std::vector<std::string> & blocks,
      36             :                                           std::shared_ptr<mfem::Coefficient> coef)
      37             : {
      38         401 :   this->_scalar_coeffs.addPiecewiseBlocks(name, coef, blocks);
      39         401 :   return getScalarCoefficient(name);
      40             : }
      41             : 
      42             : mfem::Coefficient &
      43         377 : CoefficientManager::declareScalarProperty(const std::string & name,
      44             :                                           const std::vector<std::string> & blocks,
      45             :                                           const std::string & existing_or_literal)
      46             : {
      47         377 :   std::shared_ptr<mfem::Coefficient> coef = this->getScalarCoefficientPtr(existing_or_literal);
      48         375 :   if (std::dynamic_pointer_cast<mfem::PWCoefficient>(coef))
      49           2 :     mooseError("Properties must not be defined out of other properties or piecewise coefficients.");
      50         746 :   return this->declareScalarProperty(name, blocks, coef);
      51         375 : }
      52             : 
      53             : mfem::VectorCoefficient &
      54        4910 : CoefficientManager::declareVector(const std::string & name,
      55             :                                   std::shared_ptr<mfem::VectorCoefficient> coef)
      56             : {
      57        4910 :   this->_vector_coeffs.addCoefficient(name, coef);
      58        4908 :   return *coef;
      59             : }
      60             : 
      61             : mfem::VectorCoefficient &
      62           8 : CoefficientManager::declareVector(const std::string & name, const std::string & existing_or_literal)
      63             : {
      64           8 :   return this->declareVector(name, this->getVectorCoefficientPtr(existing_or_literal));
      65             : }
      66             : 
      67             : mfem::VectorCoefficient &
      68          86 : CoefficientManager::declareVectorProperty(const std::string & name,
      69             :                                           const std::vector<std::string> & blocks,
      70             :                                           std::shared_ptr<mfem::VectorCoefficient> coef)
      71             : {
      72          86 :   this->_vector_coeffs.addPiecewiseBlocks(name, coef, blocks);
      73          86 :   return getVectorCoefficient(name);
      74             : }
      75             : 
      76             : mfem::VectorCoefficient &
      77          62 : CoefficientManager::declareVectorProperty(const std::string & name,
      78             :                                           const std::vector<std::string> & blocks,
      79             :                                           const std::string & existing_or_literal)
      80             : {
      81             :   std::shared_ptr<mfem::VectorCoefficient> coef =
      82          62 :       this->getVectorCoefficientPtr(existing_or_literal);
      83          60 :   if (std::dynamic_pointer_cast<mfem::PWVectorCoefficient>(coef))
      84           2 :     mooseError("Properties must not be defined out of other properties or piecewise coefficients.");
      85         116 :   return this->declareVectorProperty(name, blocks, coef);
      86          60 : }
      87             : 
      88             : mfem::MatrixCoefficient &
      89          32 : CoefficientManager::declareMatrix(const std::string & name,
      90             :                                   std::shared_ptr<mfem::MatrixCoefficient> coef)
      91             : {
      92          32 :   this->_matrix_coeffs.addCoefficient(name, coef);
      93          30 :   return *coef;
      94             : }
      95             : 
      96             : mfem::MatrixCoefficient &
      97           6 : CoefficientManager::declareMatrix(const std::string & name, const std::string & existing_coef)
      98             : {
      99           6 :   return this->declareMatrix(name, this->getMatrixCoefficientPtr(existing_coef));
     100             : }
     101             : 
     102             : mfem::MatrixCoefficient &
     103          30 : CoefficientManager::declareMatrixProperty(const std::string & name,
     104             :                                           const std::vector<std::string> & blocks,
     105             :                                           std::shared_ptr<mfem::MatrixCoefficient> coef)
     106             : {
     107          30 :   this->_matrix_coeffs.addPiecewiseBlocks(name, coef, blocks);
     108          30 :   return getMatrixCoefficient(name);
     109             : }
     110             : 
     111             : mfem::MatrixCoefficient &
     112           6 : CoefficientManager::declareMatrixProperty(const std::string & name,
     113             :                                           const std::vector<std::string> & blocks,
     114             :                                           const std::string & existing_coef)
     115             : {
     116           6 :   std::shared_ptr<mfem::MatrixCoefficient> coef = this->getMatrixCoefficientPtr(existing_coef);
     117           4 :   if (std::dynamic_pointer_cast<mfem::PWMatrixCoefficient>(coef))
     118           2 :     mooseError("Properties must not be defined out of other properties or piecewise coefficients.");
     119           4 :   return this->declareMatrixProperty(name, blocks, coef);
     120           4 : }
     121             : 
     122             : std::shared_ptr<mfem::Coefficient>
     123        6080 : CoefficientManager::getScalarCoefficientPtr(const std::string & name)
     124             : {
     125        6080 :   if (this->_scalar_coeffs.hasCoefficient(name))
     126        2963 :     return this->_scalar_coeffs.getCoefficientPtr(name);
     127             :   // If name not present, check if it can be parsed cleanly into a real number
     128        3117 :   std::istringstream ss(MooseUtils::trim(name));
     129             :   mfem::real_t real_value;
     130        3117 :   if (ss >> real_value && ss.eof())
     131             :   {
     132        3107 :     this->declareScalar<mfem::ConstantCoefficient>(name, real_value);
     133        3107 :     return this->_scalar_coeffs.getCoefficientPtr(name);
     134             :   }
     135          30 :   mooseError("Scalar coefficient with name '" + name + "' has not been declared.");
     136        3117 : }
     137             : 
     138             : std::shared_ptr<mfem::VectorCoefficient>
     139        5623 : CoefficientManager::getVectorCoefficientPtr(const std::string & name)
     140             : {
     141        5623 :   if (this->_vector_coeffs.hasCoefficient(name))
     142        5448 :     return this->_vector_coeffs.getCoefficientPtr(name);
     143             :   // If name not present, check if it can be parsed cleanly into a vector of real numbers
     144         175 :   std::vector<mfem::real_t> vec_values;
     145         525 :   if (MooseUtils::tokenizeAndConvert(name, vec_values) && vec_values.size() > 0)
     146             :   {
     147         165 :     this->declareVector<mfem::VectorConstantCoefficient>(
     148         330 :         name, mfem::Vector(vec_values.data(), vec_values.size()));
     149         165 :     return this->_vector_coeffs.getCoefficientPtr(name);
     150             :   }
     151          30 :   mooseError("Vector coefficient with name '" + name + "' has not been declared.");
     152         175 : }
     153             : 
     154             : std::shared_ptr<mfem::MatrixCoefficient>
     155          70 : CoefficientManager::getMatrixCoefficientPtr(const std::string & name)
     156             : {
     157          70 :   return this->_matrix_coeffs.getCoefficientPtr(name);
     158             :   // TODO: Work out how to parse literal matrices from input.
     159             : }
     160             : 
     161             : mfem::Coefficient &
     162        5695 : CoefficientManager::getScalarCoefficient(const std::string & name)
     163             : {
     164        5695 :   return *this->getScalarCoefficientPtr(name);
     165             : }
     166             : 
     167             : mfem::VectorCoefficient &
     168        5553 : CoefficientManager::getVectorCoefficient(const std::string & name)
     169             : {
     170        5553 :   return *this->getVectorCoefficientPtr(name);
     171             : }
     172             : 
     173             : mfem::MatrixCoefficient &
     174          58 : CoefficientManager::getMatrixCoefficient(const std::string & name)
     175             : {
     176          58 :   return *this->getMatrixCoefficientPtr(name);
     177             : }
     178             : 
     179             : bool
     180          80 : CoefficientManager::scalarPropertyIsDefined(const std::string & name,
     181             :                                             const std::string & block) const
     182             : {
     183          80 :   return this->_scalar_coeffs.propertyDefinedOnBlock(name, block);
     184             : }
     185             : 
     186             : bool
     187          80 : CoefficientManager::vectorPropertyIsDefined(const std::string & name,
     188             :                                             const std::string & block) const
     189             : {
     190          80 :   return this->_vector_coeffs.propertyDefinedOnBlock(name, block);
     191             : }
     192             : 
     193             : bool
     194          80 : CoefficientManager::matrixPropertyIsDefined(const std::string & name,
     195             :                                             const std::string & block) const
     196             : {
     197          80 :   return this->_matrix_coeffs.propertyDefinedOnBlock(name, block);
     198             : }
     199             : 
     200             : void
     201        1624 : CoefficientManager::setTime(const mfem::real_t time)
     202             : {
     203        1624 :   this->_scalar_coeffs.setTime(time);
     204        1624 :   this->_vector_coeffs.setTime(time);
     205        1624 :   this->_matrix_coeffs.setTime(time);
     206        1624 : }
     207             : 
     208             : void
     209        2462 : CoefficientManager::markSolutionChanged()
     210             : {
     211       29647 :   auto mark_solution_changed = [](auto & coef)
     212             :   {
     213       29647 :     if (auto * const qf_coef = dynamic_cast<MFEMQuadratureFunctionCoefficientBase *>(&coef))
     214         632 :       qf_coef->MarkSolutionChanged();
     215       29647 :   };
     216        2462 :   this->_scalar_coeffs.apply(mark_solution_changed);
     217        2462 :   this->_vector_coeffs.apply(mark_solution_changed);
     218        2462 : }
     219             : }
     220             : 
     221             : #endif

Generated by: LCOV version 1.14