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 MOOSE_MFEM_ENABLED
11 
12 #include "MooseStringUtils.h"
13 #include "CoefficientManager.h"
15 #include <algorithm>
16 
17 namespace Moose::MFEM
18 {
19 
20 mfem::Coefficient &
21 CoefficientManager::declareScalar(const std::string & name, std::shared_ptr<mfem::Coefficient> coef)
22 {
23  this->_scalar_coeffs.addCoefficient(name, coef);
24  return *coef;
25 }
26 
27 mfem::Coefficient &
28 CoefficientManager::declareScalar(const std::string & name, const std::string & existing_or_literal)
29 {
30  return this->declareScalar(name, this->getScalarCoefficientPtr(existing_or_literal));
31 }
32 
33 mfem::Coefficient &
35  const std::vector<std::string> & blocks,
36  std::shared_ptr<mfem::Coefficient> coef)
37 {
38  this->_scalar_coeffs.addPiecewiseBlocks(name, coef, blocks);
39  return getScalarCoefficient(name);
40 }
41 
42 mfem::Coefficient &
44  const std::vector<std::string> & blocks,
45  const std::string & existing_or_literal)
46 {
47  std::shared_ptr<mfem::Coefficient> coef = this->getScalarCoefficientPtr(existing_or_literal);
48  if (std::dynamic_pointer_cast<mfem::PWCoefficient>(coef))
49  mooseError("Properties must not be defined out of other properties or piecewise coefficients.");
50  return this->declareScalarProperty(name, blocks, coef);
51 }
52 
53 mfem::VectorCoefficient &
54 CoefficientManager::declareVector(const std::string & name,
55  std::shared_ptr<mfem::VectorCoefficient> coef)
56 {
57  this->_vector_coeffs.addCoefficient(name, coef);
58  return *coef;
59 }
60 
61 mfem::VectorCoefficient &
62 CoefficientManager::declareVector(const std::string & name, const std::string & existing_or_literal)
63 {
64  return this->declareVector(name, this->getVectorCoefficientPtr(existing_or_literal));
65 }
66 
67 mfem::VectorCoefficient &
69  const std::vector<std::string> & blocks,
70  std::shared_ptr<mfem::VectorCoefficient> coef)
71 {
72  this->_vector_coeffs.addPiecewiseBlocks(name, coef, blocks);
73  return getVectorCoefficient(name);
74 }
75 
76 mfem::VectorCoefficient &
78  const std::vector<std::string> & blocks,
79  const std::string & existing_or_literal)
80 {
81  std::shared_ptr<mfem::VectorCoefficient> coef =
82  this->getVectorCoefficientPtr(existing_or_literal);
83  if (std::dynamic_pointer_cast<mfem::PWVectorCoefficient>(coef))
84  mooseError("Properties must not be defined out of other properties or piecewise coefficients.");
85  return this->declareVectorProperty(name, blocks, coef);
86 }
87 
88 mfem::MatrixCoefficient &
89 CoefficientManager::declareMatrix(const std::string & name,
90  std::shared_ptr<mfem::MatrixCoefficient> coef)
91 {
92  this->_matrix_coeffs.addCoefficient(name, coef);
93  return *coef;
94 }
95 
96 mfem::MatrixCoefficient &
97 CoefficientManager::declareMatrix(const std::string & name, const std::string & existing_coef)
98 {
99  return this->declareMatrix(name, this->getMatrixCoefficientPtr(existing_coef));
100 }
101 
102 mfem::MatrixCoefficient &
104  const std::vector<std::string> & blocks,
105  std::shared_ptr<mfem::MatrixCoefficient> coef)
106 {
107  this->_matrix_coeffs.addPiecewiseBlocks(name, coef, blocks);
108  return getMatrixCoefficient(name);
109 }
110 
111 mfem::MatrixCoefficient &
113  const std::vector<std::string> & blocks,
114  const std::string & existing_coef)
115 {
116  std::shared_ptr<mfem::MatrixCoefficient> coef = this->getMatrixCoefficientPtr(existing_coef);
117  if (std::dynamic_pointer_cast<mfem::PWMatrixCoefficient>(coef))
118  mooseError("Properties must not be defined out of other properties or piecewise coefficients.");
119  return this->declareMatrixProperty(name, blocks, coef);
120 }
121 
122 std::shared_ptr<mfem::Coefficient>
124 {
125  if (this->_scalar_coeffs.hasCoefficient(name))
126  return this->_scalar_coeffs.getCoefficientPtr(name);
127  // If name not present, check if it can be parsed cleanly into a real number
128  std::istringstream ss(MooseUtils::trim(name));
129  mfem::real_t real_value;
130  if (ss >> real_value && ss.eof())
131  {
132  this->declareScalar<mfem::ConstantCoefficient>(name, real_value);
133  return this->_scalar_coeffs.getCoefficientPtr(name);
134  }
135  mooseError("Scalar coefficient with name '" + name + "' has not been declared.");
136 }
137 
138 std::shared_ptr<mfem::VectorCoefficient>
140 {
141  if (this->_vector_coeffs.hasCoefficient(name))
142  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  std::vector<mfem::real_t> vec_values;
145  if (MooseUtils::tokenizeAndConvert(name, vec_values) && vec_values.size() > 0)
146  {
147  this->declareVector<mfem::VectorConstantCoefficient>(
148  name, mfem::Vector(vec_values.data(), vec_values.size()));
149  return this->_vector_coeffs.getCoefficientPtr(name);
150  }
151  mooseError("Vector coefficient with name '" + name + "' has not been declared.");
152 }
153 
154 std::shared_ptr<mfem::MatrixCoefficient>
156 {
157  return this->_matrix_coeffs.getCoefficientPtr(name);
158  // TODO: Work out how to parse literal matrices from input.
159 }
160 
161 mfem::Coefficient &
163 {
164  return *this->getScalarCoefficientPtr(name);
165 }
166 
167 mfem::VectorCoefficient &
169 {
170  return *this->getVectorCoefficientPtr(name);
171 }
172 
173 mfem::MatrixCoefficient &
175 {
176  return *this->getMatrixCoefficientPtr(name);
177 }
178 
179 bool
181  const std::string & block) const
182 {
183  return this->_scalar_coeffs.propertyDefinedOnBlock(name, block);
184 }
185 
186 bool
188  const std::string & block) const
189 {
190  return this->_vector_coeffs.propertyDefinedOnBlock(name, block);
191 }
192 
193 bool
195  const std::string & block) const
196 {
197  return this->_matrix_coeffs.propertyDefinedOnBlock(name, block);
198 }
199 
200 void
201 CoefficientManager::setTime(const mfem::real_t time)
202 {
203  this->_scalar_coeffs.setTime(time);
204  this->_vector_coeffs.setTime(time);
205  this->_matrix_coeffs.setTime(time);
206 }
207 
208 void
210 {
211  auto mark_solution_changed = [](auto & coef)
212  {
213  if (auto * const qf_coef = dynamic_cast<MFEMQuadratureFunctionCoefficientBase *>(&coef))
214  qf_coef->MarkSolutionChanged();
215  };
216  this->_scalar_coeffs.apply(mark_solution_changed);
217  this->_vector_coeffs.apply(mark_solution_changed);
218 }
219 }
220 
221 #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:311
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)
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.
void apply(F &&func)
Apply a function to every coefficient created by this map.
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
void setTime(const mfem::real_t time)
void setTime(const mfem::real_t time)
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 markSolutionChanged()
Notify quadrature function coefficients that solution variables have changed, marking the stored valu...
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.
Utilities for converting between vector(s) of libMesh Points and MFEM Vector(s).
bool hasCoefficient(const std::string &name) const