https://mooseframework.inl.gov
CoefficientManager.C
Go to the documentation of this file.
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 CoefficientManager::declareScalar(const std::string & name, std::shared_ptr<mfem::Coefficient> coef)
21 {
22  this->_scalar_coeffs.addCoefficient(name, coef);
23  return *coef;
24 }
25 
26 mfem::Coefficient &
27 CoefficientManager::declareScalar(const std::string & name, const std::string & existing_or_literal)
28 {
29  return this->declareScalar(name, this->getScalarCoefficientPtr(existing_or_literal));
30 }
31 
32 mfem::Coefficient &
34  const std::vector<std::string> & blocks,
35  std::shared_ptr<mfem::Coefficient> coef)
36 {
37  this->_scalar_coeffs.addPiecewiseBlocks(name, coef, blocks);
38  return getScalarCoefficient(name);
39 }
40 
41 mfem::Coefficient &
43  const std::vector<std::string> & blocks,
44  const std::string & existing_or_literal)
45 {
46  std::shared_ptr<mfem::Coefficient> coef = this->getScalarCoefficientPtr(existing_or_literal);
47  if (std::dynamic_pointer_cast<mfem::PWCoefficient>(coef))
48  mooseError("Properties must not be defined out of other properties or piecewise coefficients.");
49  return this->declareScalarProperty(name, blocks, coef);
50 }
51 
52 mfem::VectorCoefficient &
53 CoefficientManager::declareVector(const std::string & name,
54  std::shared_ptr<mfem::VectorCoefficient> coef)
55 {
56  this->_vector_coeffs.addCoefficient(name, coef);
57  return *coef;
58 }
59 
60 mfem::VectorCoefficient &
61 CoefficientManager::declareVector(const std::string & name, const std::string & existing_or_literal)
62 {
63  return this->declareVector(name, this->getVectorCoefficientPtr(existing_or_literal));
64 }
65 
66 mfem::VectorCoefficient &
68  const std::vector<std::string> & blocks,
69  std::shared_ptr<mfem::VectorCoefficient> coef)
70 {
71  this->_vector_coeffs.addPiecewiseBlocks(name, coef, blocks);
72  return getVectorCoefficient(name);
73 }
74 
75 mfem::VectorCoefficient &
77  const std::vector<std::string> & blocks,
78  const std::string & existing_or_literal)
79 {
80  std::shared_ptr<mfem::VectorCoefficient> coef =
81  this->getVectorCoefficientPtr(existing_or_literal);
82  if (std::dynamic_pointer_cast<mfem::PWVectorCoefficient>(coef))
83  mooseError("Properties must not be defined out of other properties or piecewise coefficients.");
84  return this->declareVectorProperty(name, blocks, coef);
85 }
86 
87 mfem::MatrixCoefficient &
88 CoefficientManager::declareMatrix(const std::string & name,
89  std::shared_ptr<mfem::MatrixCoefficient> coef)
90 {
91  this->_matrix_coeffs.addCoefficient(name, coef);
92  return *coef;
93 }
94 
95 mfem::MatrixCoefficient &
96 CoefficientManager::declareMatrix(const std::string & name, const std::string & existing_coef)
97 {
98  return this->declareMatrix(name, this->getMatrixCoefficientPtr(existing_coef));
99 }
100 
101 mfem::MatrixCoefficient &
103  const std::vector<std::string> & blocks,
104  std::shared_ptr<mfem::MatrixCoefficient> coef)
105 {
106  this->_matrix_coeffs.addPiecewiseBlocks(name, coef, blocks);
107  return getMatrixCoefficient(name);
108 }
109 
110 mfem::MatrixCoefficient &
112  const std::vector<std::string> & blocks,
113  const std::string & existing_coef)
114 {
115  std::shared_ptr<mfem::MatrixCoefficient> coef = this->getMatrixCoefficientPtr(existing_coef);
116  if (std::dynamic_pointer_cast<mfem::PWMatrixCoefficient>(coef))
117  mooseError("Properties must not be defined out of other properties or piecewise coefficients.");
118  return this->declareMatrixProperty(name, blocks, coef);
119 }
120 
121 std::shared_ptr<mfem::Coefficient>
123 {
124  if (this->_scalar_coeffs.hasCoefficient(name))
125  return this->_scalar_coeffs.getCoefficientPtr(name);
126  // If name not present, check if it can be parsed cleanly into a real number
127  std::istringstream ss(MooseUtils::trim(name));
128  mfem::real_t real_value;
129  if (ss >> real_value && ss.eof())
130  {
131  this->declareScalar<mfem::ConstantCoefficient>(name, real_value);
132  return this->_scalar_coeffs.getCoefficientPtr(name);
133  }
134  mooseError("Scalar coefficient with name '" + name + "' has not been declared.");
135 }
136 
137 std::shared_ptr<mfem::VectorCoefficient>
139 {
140  if (this->_vector_coeffs.hasCoefficient(name))
141  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  std::vector<mfem::real_t> vec_values;
144  if (MooseUtils::tokenizeAndConvert(name, vec_values) && vec_values.size() > 0)
145  {
146  this->declareVector<mfem::VectorConstantCoefficient>(
147  name, mfem::Vector(vec_values.data(), vec_values.size()));
148  return this->_vector_coeffs.getCoefficientPtr(name);
149  }
150  mooseError("Vector coefficient with name '" + name + "' has not been declared.");
151 }
152 
153 std::shared_ptr<mfem::MatrixCoefficient>
155 {
156  return this->_matrix_coeffs.getCoefficientPtr(name);
157  // TODO: Work out how to parse literal matrices from input.
158 }
159 
160 mfem::Coefficient &
162 {
163  return *this->getScalarCoefficientPtr(name);
164 }
165 
166 mfem::VectorCoefficient &
168 {
169  return *this->getVectorCoefficientPtr(name);
170 }
171 
172 mfem::MatrixCoefficient &
174 {
175  return *this->getMatrixCoefficientPtr(name);
176 }
177 
178 bool
180  const std::string & block) const
181 {
182  return this->_scalar_coeffs.propertyDefinedOnBlock(name, block);
183 }
184 
185 bool
187  const std::string & block) const
188 {
189  return this->_vector_coeffs.propertyDefinedOnBlock(name, block);
190 }
191 
192 bool
194  const std::string & block) const
195 {
196  return this->_matrix_coeffs.propertyDefinedOnBlock(name, block);
197 }
198 
199 void
200 CoefficientManager::setTime(const double time)
201 {
202  this->_scalar_coeffs.setTime(time);
203  this->_vector_coeffs.setTime(time);
204  this->_matrix_coeffs.setTime(time);
205 }
206 }
207 
208 #endif
std::string name(const ElemQuality q)
bool propertyDefinedOnBlock(const std::string &name, const std::string &block) const
mfem::Coefficient & declareScalarProperty(const std::string &name, const std::vector< std::string > &blocks, const std::string &existing_or_literal)
Use an existing scalar coefficient for a property on some blocks of the mesh.
char ** blocks
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302
mfem::VectorCoefficient & getVectorCoefficient(const std::string &name)
Return a vector coefficient with the given name or, if that doesn&#39;t exists, try interpreting the name...
bool tokenizeAndConvert(const std::string &str, std::vector< T > &tokenized_vector, const std::string &delimiter=" \\\)
tokenizeAndConvert splits a string using delimiter and then converts to type T.
mfem::VectorCoefficient & declareVectorProperty(const std::string &name, const std::vector< std::string > &blocks, const std::string &existing_or_literal)
Use an existing vector coefficient for a property on some blocks of the mesh.
bool scalarPropertyIsDefined(const std::string &name, const std::string &block) const
mfem::MatrixCoefficient & declareMatrixProperty(const std::string &name, const std::vector< std::string > &blocks, const std::string &existing_coef)
Use an existing matrix coefficient for a property on some blocks of the mesh.
std::shared_ptr< mfem::VectorCoefficient > getVectorCoefficientPtr(const std::string &name)
void setTime(const double time)
std::shared_ptr< mfem::MatrixCoefficient > getMatrixCoefficientPtr(const std::string &name)
mfem::MatrixCoefficient & declareMatrix(const std::string &name, const std::string &existing_coef)
Declare an alias to an existing matrix coefficient.
mfem::MatrixCoefficient & getMatrixCoefficient(const std::string &name)
Return scalar coefficient with the given name.
mfem::Coefficient & declareScalar(const std::string &name, const std::string &existing_or_literal)
Declare an alias to an existing scalar coefficient or, if it does not exist, try interpreting the nam...
std::shared_ptr< mfem::Coefficient > getScalarCoefficientPtr(const std::string &name)
void addCoefficient(const std::string &name, std::shared_ptr< T > coeff)
Add a named global coefficient.
mfem::VectorCoefficient & declareVector(const std::string &name, const std::string &existing_or_literal)
Declare an alias to an existing vector coefficientor or, if it does not exist, try interpreting the n...
std::string trim(const std::string &str, const std::string &white_space=" \\\)
Standard scripting language trim function.
bool vectorPropertyIsDefined(const std::string &name, const std::string &block) const
bool matrixPropertyIsDefined(const std::string &name, const std::string &block) const
mfem::Coefficient & getScalarCoefficient(const std::string &name)
Return a scalar coefficient with the given name or, if that doesn&#39;t exists, try interpreting the name...
void setTime(const double time)
std::shared_ptr< T > getCoefficientPtr(const std::string &name)
void addPiecewiseBlocks(const std::string &name, std::shared_ptr< T > coeff, const std::vector< std::string > &blocks)
Add piecewise material property.
bool hasCoefficient(const std::string &name) const