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 1574 : CoefficientManager::declareScalar(const std::string & name, std::shared_ptr<mfem::Coefficient> coef)
21 : {
22 1574 : this->_scalar_coeffs.addCoefficient(name, coef);
23 1572 : return *coef;
24 : }
25 :
26 : mfem::Coefficient &
27 8 : CoefficientManager::declareScalar(const std::string & name, const std::string & existing_or_literal)
28 : {
29 8 : return this->declareScalar(name, this->getScalarCoefficientPtr(existing_or_literal));
30 : }
31 :
32 : mfem::Coefficient &
33 173 : CoefficientManager::declareScalarProperty(const std::string & name,
34 : const std::vector<std::string> & blocks,
35 : std::shared_ptr<mfem::Coefficient> coef)
36 : {
37 173 : this->_scalar_coeffs.addPiecewiseBlocks(name, coef, blocks);
38 173 : return getScalarCoefficient(name);
39 : }
40 :
41 : mfem::Coefficient &
42 149 : CoefficientManager::declareScalarProperty(const std::string & name,
43 : const std::vector<std::string> & blocks,
44 : const std::string & existing_or_literal)
45 : {
46 149 : std::shared_ptr<mfem::Coefficient> coef = this->getScalarCoefficientPtr(existing_or_literal);
47 147 : if (std::dynamic_pointer_cast<mfem::PWCoefficient>(coef))
48 2 : mooseError("Properties must not be defined out of other properties or piecewise coefficients.");
49 290 : return this->declareScalarProperty(name, blocks, coef);
50 147 : }
51 :
52 : mfem::VectorCoefficient &
53 575 : CoefficientManager::declareVector(const std::string & name,
54 : std::shared_ptr<mfem::VectorCoefficient> coef)
55 : {
56 575 : this->_vector_coeffs.addCoefficient(name, coef);
57 573 : return *coef;
58 : }
59 :
60 : mfem::VectorCoefficient &
61 8 : CoefficientManager::declareVector(const std::string & name, const std::string & existing_or_literal)
62 : {
63 8 : return this->declareVector(name, this->getVectorCoefficientPtr(existing_or_literal));
64 : }
65 :
66 : mfem::VectorCoefficient &
67 62 : CoefficientManager::declareVectorProperty(const std::string & name,
68 : const std::vector<std::string> & blocks,
69 : std::shared_ptr<mfem::VectorCoefficient> coef)
70 : {
71 62 : this->_vector_coeffs.addPiecewiseBlocks(name, coef, blocks);
72 62 : return getVectorCoefficient(name);
73 : }
74 :
75 : mfem::VectorCoefficient &
76 38 : 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 38 : this->getVectorCoefficientPtr(existing_or_literal);
82 36 : if (std::dynamic_pointer_cast<mfem::PWVectorCoefficient>(coef))
83 2 : mooseError("Properties must not be defined out of other properties or piecewise coefficients.");
84 68 : return this->declareVectorProperty(name, blocks, coef);
85 36 : }
86 :
87 : mfem::MatrixCoefficient &
88 32 : CoefficientManager::declareMatrix(const std::string & name,
89 : std::shared_ptr<mfem::MatrixCoefficient> coef)
90 : {
91 32 : this->_matrix_coeffs.addCoefficient(name, coef);
92 30 : return *coef;
93 : }
94 :
95 : mfem::MatrixCoefficient &
96 6 : CoefficientManager::declareMatrix(const std::string & name, const std::string & existing_coef)
97 : {
98 6 : return this->declareMatrix(name, this->getMatrixCoefficientPtr(existing_coef));
99 : }
100 :
101 : mfem::MatrixCoefficient &
102 30 : CoefficientManager::declareMatrixProperty(const std::string & name,
103 : const std::vector<std::string> & blocks,
104 : std::shared_ptr<mfem::MatrixCoefficient> coef)
105 : {
106 30 : this->_matrix_coeffs.addPiecewiseBlocks(name, coef, blocks);
107 30 : return getMatrixCoefficient(name);
108 : }
109 :
110 : mfem::MatrixCoefficient &
111 6 : CoefficientManager::declareMatrixProperty(const std::string & name,
112 : const std::vector<std::string> & blocks,
113 : const std::string & existing_coef)
114 : {
115 6 : std::shared_ptr<mfem::MatrixCoefficient> coef = this->getMatrixCoefficientPtr(existing_coef);
116 4 : if (std::dynamic_pointer_cast<mfem::PWMatrixCoefficient>(coef))
117 2 : mooseError("Properties must not be defined out of other properties or piecewise coefficients.");
118 4 : return this->declareMatrixProperty(name, blocks, coef);
119 4 : }
120 :
121 : std::shared_ptr<mfem::Coefficient>
122 1490 : CoefficientManager::getScalarCoefficientPtr(const std::string & name)
123 : {
124 1490 : if (this->_scalar_coeffs.hasCoefficient(name))
125 660 : return this->_scalar_coeffs.getCoefficientPtr(name);
126 : // If name not present, check if it can be parsed cleanly into a real number
127 830 : std::istringstream ss(MooseUtils::trim(name));
128 : mfem::real_t real_value;
129 830 : if (ss >> real_value && ss.eof())
130 : {
131 820 : this->declareScalar<mfem::ConstantCoefficient>(name, real_value);
132 820 : return this->_scalar_coeffs.getCoefficientPtr(name);
133 : }
134 30 : mooseError("Scalar coefficient with name '" + name + "' has not been declared.");
135 830 : }
136 :
137 : std::shared_ptr<mfem::VectorCoefficient>
138 368 : CoefficientManager::getVectorCoefficientPtr(const std::string & name)
139 : {
140 368 : if (this->_vector_coeffs.hasCoefficient(name))
141 293 : 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 75 : std::vector<mfem::real_t> vec_values;
144 225 : if (MooseUtils::tokenizeAndConvert(name, vec_values) && vec_values.size() > 0)
145 : {
146 65 : this->declareVector<mfem::VectorConstantCoefficient>(
147 130 : name, mfem::Vector(vec_values.data(), vec_values.size()));
148 65 : return this->_vector_coeffs.getCoefficientPtr(name);
149 : }
150 30 : mooseError("Vector coefficient with name '" + name + "' has not been declared.");
151 75 : }
152 :
153 : std::shared_ptr<mfem::MatrixCoefficient>
154 70 : CoefficientManager::getMatrixCoefficientPtr(const std::string & name)
155 : {
156 70 : return this->_matrix_coeffs.getCoefficientPtr(name);
157 : // TODO: Work out how to parse literal matrices from input.
158 : }
159 :
160 : mfem::Coefficient &
161 1333 : CoefficientManager::getScalarCoefficient(const std::string & name)
162 : {
163 1333 : return *this->getScalarCoefficientPtr(name);
164 : }
165 :
166 : mfem::VectorCoefficient &
167 322 : CoefficientManager::getVectorCoefficient(const std::string & name)
168 : {
169 322 : return *this->getVectorCoefficientPtr(name);
170 : }
171 :
172 : mfem::MatrixCoefficient &
173 58 : CoefficientManager::getMatrixCoefficient(const std::string & name)
174 : {
175 58 : return *this->getMatrixCoefficientPtr(name);
176 : }
177 :
178 : bool
179 80 : CoefficientManager::scalarPropertyIsDefined(const std::string & name,
180 : const std::string & block) const
181 : {
182 80 : return this->_scalar_coeffs.propertyDefinedOnBlock(name, block);
183 : }
184 :
185 : bool
186 80 : CoefficientManager::vectorPropertyIsDefined(const std::string & name,
187 : const std::string & block) const
188 : {
189 80 : return this->_vector_coeffs.propertyDefinedOnBlock(name, block);
190 : }
191 :
192 : bool
193 80 : CoefficientManager::matrixPropertyIsDefined(const std::string & name,
194 : const std::string & block) const
195 : {
196 80 : return this->_matrix_coeffs.propertyDefinedOnBlock(name, block);
197 : }
198 :
199 : void
200 641 : CoefficientManager::setTime(const mfem::real_t 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
|