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 : #include "ParsedMaterialBase.h"
11 : #include "ParsedAux.h"
12 : #include "MooseObject.h"
13 :
14 : InputParameters
15 62475 : ParsedMaterialBase::validParams()
16 : {
17 62475 : InputParameters params = emptyInputParameters();
18 249900 : params.addCoupledVar("args", "Vector of variables used in the parsed function");
19 374850 : params.deprecateCoupledVar("args", "coupled_variables", "02/07/2024");
20 :
21 : // Constants and their values
22 187425 : params.addParam<std::vector<std::string>>(
23 : "constant_names",
24 124950 : std::vector<std::string>(),
25 : "Vector of constants used in the parsed function (use this for kB etc.)");
26 187425 : params.addParam<std::vector<std::string>>(
27 : "constant_expressions",
28 124950 : std::vector<std::string>(),
29 : "Vector of values for the constants in constant_names (can be an FParser expression)");
30 :
31 : // Variables with applied tolerances and their tolerance values
32 187425 : params.addParam<std::vector<std::string>>("tol_names",
33 124950 : std::vector<std::string>(),
34 : "Vector of variable names to be protected from "
35 : "being 0 or 1 within a tolerance (needed for log(c) "
36 : "and log(1-c) terms)");
37 187425 : params.addParam<std::vector<Real>>("tol_values",
38 124950 : std::vector<Real>(),
39 : "Vector of tolerance values for the variables in tol_names");
40 :
41 : // Functors and their symbols
42 249900 : params.addParam<std::vector<MooseFunctorName>>(
43 : "functor_names", {}, "Functors to use in the parsed expression");
44 249900 : params.addParam<std::vector<std::string>>(
45 : "functor_symbols",
46 : {},
47 : "Symbolic name to use for each functor in 'functor_names' in the parsed expression. If not "
48 : "provided, then the actual functor names will be used in the parsed expression.");
49 :
50 : // Material properties
51 187425 : params.addParam<std::vector<std::string>>(
52 : "material_property_names",
53 124950 : std::vector<std::string>(),
54 : "Vector of material properties used in the parsed function");
55 :
56 : // Postprocessors
57 187425 : params.addParam<std::vector<PostprocessorName>>(
58 : "postprocessor_names",
59 124950 : std::vector<PostprocessorName>(),
60 : "Vector of postprocessor names used in the parsed function");
61 :
62 : // Function expression
63 499800 : params.addDeprecatedCustomTypeParam<std::string>(
64 : "function",
65 : "FunctionExpression",
66 : "Parsed function (see FParser) expression for the parsed material",
67 : "'function' is deprecated, use 'expression' instead");
68 : // TODO Make required once deprecation is handled, see #19119
69 312375 : params.addCustomTypeParam<std::string>(
70 : "expression",
71 : "FunctionExpression",
72 : "Parsed function (see FParser) expression for the parsed material");
73 :
74 62475 : return params;
75 0 : }
76 :
77 4149 : ParsedMaterialBase::ParsedMaterialBase(const InputParameters & parameters, const MooseObject * obj)
78 4149 : : _derived_object(obj),
79 12447 : _function_param(parameters.isParamValid("function") ? "function" : "expression"),
80 4149 : _function(parameters.get<std::string>(_function_param))
81 : {
82 : // get constant vectors
83 4149 : _constant_names = parameters.get<std::vector<std::string>>("constant_names");
84 4149 : _constant_expressions = parameters.get<std::vector<std::string>>("constant_expressions");
85 :
86 : // get tolerance vectors
87 4149 : _tol_names = parameters.get<std::vector<std::string>>("tol_names");
88 4149 : _tol_values = parameters.get<std::vector<Real>>("tol_values");
89 :
90 : // get functor vectors
91 4149 : _functor_names = parameters.get<std::vector<MooseFunctorName>>("functor_names");
92 4149 : _functor_symbols = parameters.get<std::vector<std::string>>("functor_symbols");
93 :
94 : // validate all vector names (constants, tolerances, and functors)
95 4149 : validateVectorNames();
96 4149 : }
97 :
98 : void
99 5679 : ParsedMaterialBase::validateVectorNames(const std::set<std::string> & reserved_names)
100 : {
101 : // helper method to raise an paramError
102 0 : auto raiseErr = [this](std::string param_name, std::string msg)
103 : {
104 0 : if (_derived_object != nullptr)
105 0 : _derived_object->paramError(param_name, msg);
106 : else
107 0 : mooseException(msg);
108 5679 : };
109 :
110 11442 : auto hasDuplicates = [](const std::vector<std::string> & values)
111 : {
112 11442 : std::set<std::string> s(values.begin(), values.end());
113 22884 : return values.size() != s.size();
114 11442 : };
115 :
116 : // helper function to check if the name given is one of the constants
117 84 : auto isKnownConstantName = [this](const std::string & name)
118 : {
119 : return (
120 84 : _constant_names.size() &&
121 84 : (std::find(_constant_names.begin(), _constant_names.end(), name) != _constant_names.end()));
122 5679 : };
123 :
124 : // helper function to check if the name given is one of the reserved_names
125 84 : auto isReservedName = [reserved_names](const std::string & name)
126 5763 : { return reserved_names.find(name) != reserved_names.end(); };
127 :
128 : // check constants
129 5679 : if (hasDuplicates(_constant_names))
130 0 : raiseErr("constant_names", "In the constants duplicate names are not permitted.");
131 5679 : for (const auto & name : _constant_names)
132 : {
133 0 : if (isReservedName(name))
134 0 : raiseErr("constant_names", "In constants, the name '" + name + "' is not permitted.");
135 : }
136 :
137 : // check tolerance vectors
138 5679 : if (hasDuplicates(_tol_names))
139 0 : raiseErr("tol_names", "In the tolerances duplicate names are not permitted.");
140 :
141 : // check functor vectors
142 5679 : if (_functor_symbols.empty())
143 : {
144 5595 : if (!_functor_names.empty())
145 0 : raiseErr("functor_names", "functor_symbols must be the same length as functor_names.");
146 : }
147 : else
148 : {
149 84 : if (_functor_symbols.size() != _functor_names.size())
150 0 : raiseErr("functor_names", "functor_symbols must be the same length as functor_names.");
151 84 : std::vector<std::string> names;
152 84 : if (_functor_symbols.empty())
153 0 : std::copy(_functor_names.begin(), _functor_names.end(), std::back_inserter(names));
154 : else
155 84 : std::copy(_functor_symbols.begin(), _functor_symbols.end(), std::back_inserter(names));
156 84 : if (hasDuplicates(names))
157 0 : raiseErr(_functor_symbols.empty() ? "functor_names" : "functor_symbols",
158 : "In functors, duplicate names are not permitted.");
159 168 : for (const auto & name : names)
160 : {
161 84 : if (isKnownConstantName(name))
162 0 : raiseErr(_functor_symbols.empty() ? "functor_names" : "functor_symbols",
163 0 : "In functors, the name '" + name + "' is already in use as a constant.");
164 84 : if (isReservedName(name))
165 0 : raiseErr(_functor_symbols.empty() ? "functor_names" : "functor_symbols",
166 0 : "In functors, the name '" + name + "' is not permitted.");
167 : }
168 84 : }
169 5679 : }
|