LCOV - code coverage report
Current view: top level - src/mfem/utils - CoefficientManager.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: #31405 (292dce) with base fef103 Lines: 80 80 100.0 %
Date: 2025-09-04 07:52:05 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 MOOSE_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        1511 : CoefficientManager::declareScalar(const std::string & name, std::shared_ptr<mfem::Coefficient> coef)
      21             : {
      22        1511 :   this->_scalar_coeffs.addCoefficient(name, coef);
      23        1510 :   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         148 : CoefficientManager::declareScalarProperty(const std::string & name,
      34             :                                           const std::vector<std::string> & blocks,
      35             :                                           std::shared_ptr<mfem::Coefficient> coef)
      36             : {
      37         148 :   this->_scalar_coeffs.addPiecewiseBlocks(name, coef, blocks);
      38         148 :   return getScalarCoefficient(name);
      39             : }
      40             : 
      41             : mfem::Coefficient &
      42         136 : CoefficientManager::declareScalarProperty(const std::string & name,
      43             :                                           const std::vector<std::string> & blocks,
      44             :                                           const std::string & existing_or_literal)
      45             : {
      46         136 :   std::shared_ptr<mfem::Coefficient> coef = this->getScalarCoefficientPtr(existing_or_literal);
      47         135 :   if (std::dynamic_pointer_cast<mfem::PWCoefficient>(coef))
      48           1 :     mooseError("Properties must not be defined out of other properties or piecewise coefficients.");
      49         268 :   return this->declareScalarProperty(name, blocks, coef);
      50         135 : }
      51             : 
      52             : mfem::VectorCoefficient &
      53         516 : CoefficientManager::declareVector(const std::string & name,
      54             :                                   std::shared_ptr<mfem::VectorCoefficient> coef)
      55             : {
      56         516 :   this->_vector_coeffs.addCoefficient(name, coef);
      57         515 :   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          36 : CoefficientManager::declareVectorProperty(const std::string & name,
      68             :                                           const std::vector<std::string> & blocks,
      69             :                                           std::shared_ptr<mfem::VectorCoefficient> coef)
      70             : {
      71          36 :   this->_vector_coeffs.addPiecewiseBlocks(name, coef, blocks);
      72          36 :   return getVectorCoefficient(name);
      73             : }
      74             : 
      75             : mfem::VectorCoefficient &
      76          24 : 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          24 :       this->getVectorCoefficientPtr(existing_or_literal);
      82          23 :   if (std::dynamic_pointer_cast<mfem::PWVectorCoefficient>(coef))
      83           1 :     mooseError("Properties must not be defined out of other properties or piecewise coefficients.");
      84          44 :   return this->declareVectorProperty(name, blocks, coef);
      85          23 : }
      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        1401 : CoefficientManager::getScalarCoefficientPtr(const std::string & name)
     123             : {
     124        1401 :   if (this->_scalar_coeffs.hasCoefficient(name))
     125         595 :     return this->_scalar_coeffs.getCoefficientPtr(name);
     126             :   // If name not present, check if it can be parsed cleanly into a real number
     127         806 :   std::istringstream ss(MooseUtils::trim(name));
     128             :   mfem::real_t real_value;
     129         806 :   if (ss >> real_value && ss.eof())
     130             :   {
     131         801 :     this->declareScalar<mfem::ConstantCoefficient>(name, real_value);
     132         801 :     return this->_scalar_coeffs.getCoefficientPtr(name);
     133             :   }
     134          15 :   mooseError("Scalar coefficient with name '" + name + "' has not been declared.");
     135         806 : }
     136             : 
     137             : std::shared_ptr<mfem::VectorCoefficient>
     138         275 : CoefficientManager::getVectorCoefficientPtr(const std::string & name)
     139             : {
     140         275 :   if (this->_vector_coeffs.hasCoefficient(name))
     141         222 :     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          53 :   std::vector<mfem::real_t> vec_values;
     144         159 :   if (MooseUtils::tokenizeAndConvert(name, vec_values) && vec_values.size() > 0)
     145             :   {
     146          48 :     this->declareVector<mfem::VectorConstantCoefficient>(
     147          96 :         name, mfem::Vector(vec_values.data(), vec_values.size()));
     148          48 :     return this->_vector_coeffs.getCoefficientPtr(name);
     149             :   }
     150          15 :   mooseError("Vector coefficient with name '" + name + "' has not been declared.");
     151          53 : }
     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        1261 : CoefficientManager::getScalarCoefficient(const std::string & name)
     162             : {
     163        1261 :   return *this->getScalarCoefficientPtr(name);
     164             : }
     165             : 
     166             : mfem::VectorCoefficient &
     167         247 : CoefficientManager::getVectorCoefficient(const std::string & name)
     168             : {
     169         247 :   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         641 : CoefficientManager::setTime(const double time)
     201             : {
     202         641 :   this->_scalar_coeffs.setTime(time);
     203         641 :   this->_vector_coeffs.setTime(time);
     204         641 :   this->_matrix_coeffs.setTime(time);
     205         641 : }
     206             : }
     207             : 
     208             : #endif

Generated by: LCOV version 1.14