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

Generated by: LCOV version 1.14