www.mooseframework.org
FunctionMaterialPropertyDescriptor.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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 
12 #include "Material.h"
13 #include "Kernel.h"
14 #include <algorithm>
15 
17  const std::string & expression, MooseObject * parent)
18  : _dependent_vars(), _derivative_vars(), _parent(parent)
19 {
20  auto define = expression.find_last_of(":=");
21 
22  // expression contains a ':='
23  if (define != std::string::npos)
24  {
25  // section before ':=' is the name used in the function expression
26  _fparser_name = expression.substr(0, define - 1);
27 
28  // parse right hand side
29  parseDerivative(expression.substr(define + 1));
30  }
31  else
32  {
33  // parse entire expression and use natural material property base name
34  // for D(x(t),t,t) this would simply be 'x'!
35  parseDerivative(expression);
37  }
38 
39  _value = nullptr;
40 }
41 
43 
46  : _fparser_name(rhs._fparser_name),
47  _base_name(rhs._base_name),
48  _dependent_vars(rhs._dependent_vars),
49  _derivative_vars(rhs._derivative_vars),
50  _value(nullptr),
51  _parent(rhs._parent)
52 {
53 }
54 
56  const FunctionMaterialPropertyDescriptor & rhs, MooseObject * parent)
57  : _fparser_name(rhs._fparser_name),
58  _base_name(rhs._base_name),
59  _dependent_vars(rhs._dependent_vars),
60  _derivative_vars(rhs._derivative_vars),
61  _value(nullptr),
62  _parent(parent)
63 {
64 }
65 
66 std::vector<FunctionMaterialPropertyDescriptor>
67 FunctionMaterialPropertyDescriptor::parseVector(const std::vector<std::string> & expression_list,
68  MooseObject * parent)
69 {
70  std::vector<FunctionMaterialPropertyDescriptor> fmpds;
71  for (auto & ex : expression_list)
72  fmpds.push_back(FunctionMaterialPropertyDescriptor(ex, parent));
73  return fmpds;
74 }
75 
76 void
78 {
79  _derivative_vars.push_back(var);
80  _value = nullptr;
81 }
82 
83 bool
84 FunctionMaterialPropertyDescriptor::dependsOn(const std::string & var) const
85 {
86  return std::find(_dependent_vars.begin(), _dependent_vars.end(), var) != _dependent_vars.end() ||
87  std::find(_derivative_vars.begin(), _derivative_vars.end(), var) != _derivative_vars.end();
88 }
89 
90 std::vector<VariableName>
92 {
93  std::set<VariableName> all;
94  all.insert(_dependent_vars.begin(), _dependent_vars.end());
95  all.insert(_derivative_vars.begin(), _derivative_vars.end());
96 
97  return std::vector<VariableName>(all.begin(), all.end());
98 }
99 
100 void
102 {
103  auto open = expression.find_first_of("[");
104  auto close = expression.find_last_of("]");
105 
106  if (open == std::string::npos && close == std::string::npos)
107  {
108  // no derivative requested
109  parseDependentVariables(expression);
110 
111  return;
112  }
113  else if (open != std::string::npos && close != std::string::npos &&
114  expression.substr(0, open) == "D")
115  {
116  // tokenize splits the arguments in d2h2:=D[h2(eta1,eta2),eta1] into 'h2(eta1' 'eta2)' 'eta1'
117  // DAMN!!
118  auto arguments = expression.substr(open + 1, close - open - 1);
119  auto close2 = arguments.find_last_of(")");
120 
121  if (close2 == std::string::npos)
122  {
123  // rest of argument list 0 is the function and 1,.. are the variable to take the derivative
124  // w.r.t.
125  MooseUtils::tokenize(arguments, _derivative_vars, 0, ",");
126 
127  // check for empty [] brackets
128  if (_derivative_vars.size() > 0)
129  {
130  // parse argument zero of D[] as the function material property
132 
133  // remove function from the _derivative_vars vector
134  _derivative_vars.erase(_derivative_vars.begin());
135 
136  return;
137  }
138  }
139  else
140  {
141  parseDependentVariables(arguments.substr(0, close2 + 1));
142  MooseUtils::tokenize(arguments.substr(close2 + 2), _derivative_vars, 0, ",");
143  return;
144  }
145  }
146 
147  mooseError("Malformed material_properties expression '", expression, "'");
148 }
149 
150 void
152 {
153  auto open = expression.find_first_of("(");
154  auto close = expression.find_last_of(")");
155 
156  if (open == std::string::npos && close == std::string::npos)
157  {
158  // material property name without arguments
159  _base_name = expression;
160  }
161  else if (open != std::string::npos && close != std::string::npos)
162  {
163  // take material property name before bracket
164  _base_name = expression.substr(0, open);
165 
166  // parse argument list
167  MooseUtils::tokenize(expression.substr(open + 1, close - open - 1), _dependent_vars, 0, ",");
168 
169  // cremove duplicates from dependent variable list
170  std::sort(_dependent_vars.begin(), _dependent_vars.end());
171  _dependent_vars.erase(std::unique(_dependent_vars.begin(), _dependent_vars.end()),
172  _dependent_vars.end());
173  }
174  else
175  mooseError("Malformed material_properties expression '", expression, "'");
176 }
177 
178 void
180 {
181  Moose::out << "MPD: " << _fparser_name << ' ' << _base_name << " deriv = [";
182  for (auto & dv : _derivative_vars)
183  Moose::out << dv << ' ';
184  Moose::out << "] dep = [";
185  for (auto & dv : _dependent_vars)
186  Moose::out << dv << ' ';
187  Moose::out << "] " << getPropertyName() << '\n';
188 }
189 
192 {
193  if (_value == nullptr)
194  {
195  DerivativeMaterialInterface<Material> * _material_parent =
197  DerivativeMaterialInterface<Kernel> * _kernel_parent =
199 
200  // get the material property reference
201  if (_material_parent)
202  _value =
203  &(_material_parent->getMaterialPropertyDerivative<Real>(_base_name, _derivative_vars));
204  else if (_kernel_parent)
206  else
207  mooseError("A FunctionMaterialPropertyDescriptor must be owned by either a Material or a "
208  "Kernel object.");
209  }
210 
211  return *_value;
212 }
bool dependsOn(const std::string &var) const
Check if a material property depends on a given variable.
void tokenize(const std::string &str, std::vector< T > &elements, unsigned int min_len=1, const std::string &delims="/")
This function will split the passed in string on a set of delimiters appending the substrings to the ...
Definition: MooseUtils.h:532
const MaterialProperty< Real > & value() const
get the property reference
const MaterialProperty< U > & getMaterialPropertyDerivative(const std::string &base, const std::vector< VariableName > &c)
Methods for retreiving derivative material properties.
const std::string getPropertyName() const
get the property name
Material properties get fully described using this structure, including their dependent variables and...
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:207
void addDerivative(const VariableName &var)
take another derivative
static std::vector< FunctionMaterialPropertyDescriptor > parseVector(const std::vector< std::string > &, MooseObject *)
construct a vector of FunctionMaterialPropertyDescriptors from a vector of strings ...
std::string _base_name
function material property base name
MooseObject * _parent
material object that owns this descriptor
std::vector< VariableName > getDependentVariables()
builds a list of dependent variables (exactly all variabled for which depends on returns true) ...
Every object that can be built by the factory should be derived from this class.
Definition: MooseObject.h:42
the_pc close()
std::string _fparser_name
name used in function expression
const MaterialProperty< Real > * _value
material property value (this is lazily updated and cached when read through value()) ...