https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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
17namespace Moose::MFEM
18{
19
20mfem::Coefficient &
21CoefficientManager::declareScalar(const std::string & name, std::shared_ptr<mfem::Coefficient> coef)
22{
23 this->_scalar_coeffs.addCoefficient(name, coef);
24 return *coef;
25}
26
27mfem::Coefficient &
28CoefficientManager::declareScalar(const std::string & name, const std::string & existing_or_literal)
29{
30 return this->declareScalar(name, this->getScalarCoefficientPtr(existing_or_literal));
31}
32
33mfem::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
42mfem::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
53mfem::VectorCoefficient &
54CoefficientManager::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
61mfem::VectorCoefficient &
62CoefficientManager::declareVector(const std::string & name, const std::string & existing_or_literal)
63{
64 return this->declareVector(name, this->getVectorCoefficientPtr(existing_or_literal));
65}
66
67mfem::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
76mfem::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
88mfem::MatrixCoefficient &
89CoefficientManager::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
96mfem::MatrixCoefficient &
97CoefficientManager::declareMatrix(const std::string & name, const std::string & existing_coef)
98{
99 return this->declareMatrix(name, this->getMatrixCoefficientPtr(existing_coef));
100}
101
102mfem::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
111mfem::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
122std::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
138std::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
154std::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
161mfem::Coefficient &
163{
164 return *this->getScalarCoefficientPtr(name);
165}
166
167mfem::VectorCoefficient &
169{
170 return *this->getVectorCoefficientPtr(name);
171}
172
173mfem::MatrixCoefficient &
175{
176 return *this->getMatrixCoefficientPtr(name);
177}
178
179bool
181 const std::string & block) const
182{
183 return this->_scalar_coeffs.propertyDefinedOnBlock(name, block);
184}
185
186bool
188 const std::string & block) const
189{
190 return this->_vector_coeffs.propertyDefinedOnBlock(name, block);
191}
192
193bool
195 const std::string & block) const
196{
197 return this->_matrix_coeffs.propertyDefinedOnBlock(name, block);
198}
199
200void
201CoefficientManager::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
208void
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
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
char ** blocks
Shared lazy-update state for quadrature function coefficients.
std::shared_ptr< mfem::Coefficient > getScalarCoefficientPtr(const std::string &name)
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
std::shared_ptr< mfem::VectorCoefficient > getVectorCoefficientPtr(const std::string &name)
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.
void setTime(const mfem::real_t time)
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...
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.
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::VectorCoefficient & getVectorCoefficient(const std::string &name)
Return a vector coefficient with the given name or, if that doesn't exists, try interpreting the name...
bool vectorPropertyIsDefined(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't exists, try interpreting the name...
void markSolutionChanged()
Notify quadrature function coefficients that solution variables have changed, marking the stored valu...
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::shared_ptr< mfem::MatrixCoefficient > getMatrixCoefficientPtr(const std::string &name)
bool matrixPropertyIsDefined(const std::string &name, const std::string &block) const
bool propertyDefinedOnBlock(const std::string &name, const std::string &block) const
void addCoefficient(const std::string &name, std::shared_ptr< T > coeff)
Add a named global coefficient.
bool hasCoefficient(const std::string &name) const
void addPiecewiseBlocks(const std::string &name, std::shared_ptr< T > coeff, const std::vector< std::string > &blocks)
Add piecewise material property.
std::shared_ptr< T > getCoefficientPtr(const std::string &name)
void setTime(const mfem::real_t time)
void apply(F &&func)
Apply a function to every coefficient created by this map.
bool tokenizeAndConvert(const std::string &str, std::vector< T > &tokenized_vector, const std::string &delimiter=" \t\n\v\f\r")
tokenizeAndConvert splits a string using delimiter and then converts to type T.
std::string trim(const std::string &str, const std::string &white_space=" \t\n\v\f\r")
Standard scripting language trim function.
Utilities for converting between vector(s) of libMesh Points and MFEM Vector(s).