www.mooseframework.org
FunctionMaterialPropertyDescriptor.C
Go to the documentation of this file.
2 #include "DerivativeMaterialInterface.h"
3 #include "Material.h"
4 #include "Kernel.h"
5 #include <algorithm>
6 
8  const std::string & expression, MooseObject * parent)
9  : _dependent_vars(), _derivative_vars(), _parent(parent)
10 {
11  size_t define = expression.find_last_of(":=");
12 
13  // expression contains a ':='
14  if (define != std::string::npos)
15  {
16  // section before ':=' is the name used in the function expression
17  _fparser_name = expression.substr(0, define - 1);
18 
19  // parse right hand side
20  parseDerivative(expression.substr(define + 1));
21  }
22  else
23  {
24  // parse entire expression and use natural material property base name
25  // for D(x(t),t,t) this would simply be 'x'!
26  parseDerivative(expression);
28  }
29 
30  _value = NULL;
31 }
32 
34 
41  _value(NULL),
42  _parent(rhs._parent)
43 {
44 }
45 
46 std::vector<FunctionMaterialPropertyDescriptor>
47 FunctionMaterialPropertyDescriptor::parseVector(const std::vector<std::string> & expression_list,
48  MooseObject * parent)
49 {
50  std::vector<FunctionMaterialPropertyDescriptor> fmpds;
51  for (std::vector<std::string>::const_iterator i = expression_list.begin();
52  i != expression_list.end();
53  ++i)
54  fmpds.push_back(FunctionMaterialPropertyDescriptor(*i, parent));
55  return fmpds;
56 }
57 
58 void
60 {
61  _derivative_vars.push_back(var);
62  _value = NULL;
63 }
64 
65 bool
66 FunctionMaterialPropertyDescriptor::dependsOn(const std::string & var) const
67 {
68  return std::find(_dependent_vars.begin(), _dependent_vars.end(), var) != _dependent_vars.end() ||
69  std::find(_derivative_vars.begin(), _derivative_vars.end(), var) != _derivative_vars.end();
70 }
71 
72 std::vector<VariableName>
74 {
75  std::set<VariableName> all;
76  all.insert(_dependent_vars.begin(), _dependent_vars.end());
77  all.insert(_derivative_vars.begin(), _derivative_vars.end());
78 
79  return std::vector<VariableName>(all.begin(), all.end());
80 }
81 
82 void
84 {
85  size_t open = expression.find_first_of("[");
86  size_t close = expression.find_last_of("]");
87 
88  if (open == std::string::npos && close == std::string::npos)
89  {
90  // no derivative requested
91  parseDependentVariables(expression);
92 
93  return;
94  }
95  else if (open != std::string::npos && close != std::string::npos &&
96  expression.substr(0, open) == "D")
97  {
98  // tokenize splits the arguments in d2h2:=D[h2(eta1,eta2),eta1] into 'h2(eta1' 'eta2)' 'eta1'
99  // DAMN!!
100  std::string arguments = expression.substr(open + 1, close - open - 1);
101  size_t close2 = arguments.find_last_of(")");
102 
103  if (close2 == std::string::npos)
104  {
105  // rest of argument list 0 is the function and 1,.. are the variable to take the derivative
106  // w.r.t.
107  MooseUtils::tokenize(arguments, _derivative_vars, 0, ",");
108 
109  // check for empty [] brackets
110  if (_derivative_vars.size() > 0)
111  {
112  // parse argument zero of D[] as the function material property
114 
115  // remove function from the _derivative_vars vector
116  _derivative_vars.erase(_derivative_vars.begin());
117 
118  return;
119  }
120  }
121  else
122  {
123  parseDependentVariables(arguments.substr(0, close2 + 1));
124  MooseUtils::tokenize(arguments.substr(close2 + 2), _derivative_vars, 0, ",");
125  return;
126  }
127  }
128 
129  mooseError("Malformed material_properties expression '", expression, "'");
130 }
131 
132 void
134 {
135  size_t open = expression.find_first_of("(");
136  size_t close = expression.find_last_of(")");
137 
138  if (open == std::string::npos && close == std::string::npos)
139  {
140  // material property name without arguments
141  _base_name = expression;
142  }
143  else if (open != std::string::npos && close != std::string::npos)
144  {
145  // take material property name before bracket
146  _base_name = expression.substr(0, open);
147 
148  // parse argument list
149  MooseUtils::tokenize(expression.substr(open + 1, close - open - 1), _dependent_vars, 0, ",");
150 
151  // cremove duplicates from dependent variable list
152  std::sort(_dependent_vars.begin(), _dependent_vars.end());
153  _dependent_vars.erase(std::unique(_dependent_vars.begin(), _dependent_vars.end()),
154  _dependent_vars.end());
155  }
156  else
157  mooseError("Malformed material_properties expression '", expression, "'");
158 }
159 
160 void
162 {
163  Moose::out << "MPD: " << _fparser_name << ' ' << _base_name << " deriv = [";
164  for (unsigned int i = 0; i < _derivative_vars.size(); ++i)
165  Moose::out << _derivative_vars[i] << ' ';
166  Moose::out << "] dep = [";
167  for (unsigned int i = 0; i < _dependent_vars.size(); ++i)
168  Moose::out << _dependent_vars[i] << ' ';
169  Moose::out << "] " << getPropertyName() << '\n';
170 }
171 
172 const MaterialProperty<Real> &
174 {
175  if (_value == NULL)
176  {
177  DerivativeMaterialInterface<Material> * _material_parent =
178  dynamic_cast<DerivativeMaterialInterface<Material> *>(_parent);
179  DerivativeMaterialInterface<Kernel> * _kernel_parent =
180  dynamic_cast<DerivativeMaterialInterface<Kernel> *>(_parent);
181 
182  // get the material property reference
183  if (_material_parent)
184  _value =
185  &(_material_parent->getMaterialPropertyDerivative<Real>(_base_name, _derivative_vars));
186  else if (_kernel_parent)
187  _value = &(_kernel_parent->getMaterialPropertyDerivative<Real>(_base_name, _derivative_vars));
188  else
189  mooseError("A FunctionMaterialPropertyDescriptor must be owned by either a Material or a "
190  "Kernel object.");
191  }
192 
193  return *_value;
194 }
Material properties get fully described using this structure, including their dependent variables and...
const std::string getPropertyName() const
get the property name
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) ...
std::string _fparser_name
name used in function expression
bool dependsOn(const std::string &var) const
Check if a material property depends on a given variable.
const MaterialProperty< Real > & value() const
get the property reference
const MaterialProperty< Real > * _value
material property value (this is lazily updated and cached when read through value()) ...