25 #include "include/arena_alloc.hh"
26 #include <boost/algorithm/string/replace.hpp>
36 template <
int spacedim,
class Value>
44 "String, array of strings, or matrix of strings with formulas for individual "
45 "entries of scalar, vector, or tensor value respectively.\n"
46 "For vector values, you can use just one string to enter homogeneous vector.\n"
47 "For square (($N\\times N$))-matrix values, you can use:\n\n"
48 " - array of strings of size (($N$)) to enter diagonal matrix\n"
49 " - array of strings of size (($\\frac12N(N+1)$)) to enter symmetric matrix (upper triangle, row by row)\n"
50 " - just one string to enter (spatially variable) multiple of the unit matrix.\n"
51 "Formula can contain variables ```x,y,z,t,d``` and usual operators and functions." )
55 "The vector used to project evaluation point onto the surface.")
57 "The name of region set considered as the surface. You have to set surface region if you "
58 "want to use formula variable ```d```.")
65 template <
int spacedim,
class Value>
67 Input::register_class< FieldFormula<spacedim, Value>,
unsigned int >(
"FieldFormula") +
72 template <
int spacedim,
class Value>
75 formula_matrix_(this->value_.n_rows(), this->value_.n_cols()),
76 first_time_set_(true), arena_alloc_(nullptr)
80 for(
unsigned int row=0; row < this->
value_.n_rows(); row++) {
84 for(
unsigned int i=0; i < this->
value_.n_rows()*this->
value_.n_cols(); i++) {
91 template <
int spacedim,
class Value>
93 this->init_unit_conversion_coefficient(rec, init_data);
101 template <
int spacedim,
class Value>
106 this->is_constant_in_space_ =
false;
111 bool any_parser_changed =
false;
112 std::string value_input_address = in_rec_.address_string();
113 has_depth_var_ =
false;
114 this->is_constant_in_space_ =
true;
117 std::string vars = string(
"x,y,z").substr(0, 2*spacedim-1);
118 std::vector<bool> time_dependent(this->value_.n_rows() * this->value_.n_cols(),
false);
120 for(
unsigned int row=0; row < this->value_.n_rows(); row++)
121 for(
unsigned int col=0; col < this->value_.n_cols(); col++) {
125 FunctionParser tmp_parser;
126 tmp_parser.AddConstant(
"Pi", 3.14159265358979323846);
127 tmp_parser.AddConstant(
"E", 2.71828182845904523536);
130 #pragma GCC diagnostic push
131 #pragma GCC diagnostic ignored "-Wunused-variable"
133 int err=tmp_parser.ParseAndDeduceVariables(formula_matrix_.at(row,col), var_list);
134 ASSERT(err != FunctionParser::FP_NO_ERROR)(tmp_parser.ErrorMsg()).error(
"ParseAndDeduceVariables error\n");
136 #pragma GCC diagnostic pop
138 for(std::string &var_name : var_list ) {
139 if (var_name == std::string(
"t") ) time_dependent[row*this->value_.n_rows()+col]=
true;
140 else if (var_name == std::string(
"d") ) {
141 this->is_constant_in_space_ =
false;
145 WarningOut().fmt(
"Unset surface region. Variable '{}' in the FieldFormula[{}][{}] == '{}' will be set to zero\n at the input address:\n {} \n",
146 var_name, row, col, formula_matrix_.at(row,col), value_input_address );
148 else if (var_name ==
"x" || var_name ==
"y" || var_name ==
"z") {
149 this->is_constant_in_space_ =
false;
153 WarningOut().fmt(
"Unknown variable '{}' in the FieldFormula[{}][{}] == '{}'\n at the input address:\n {} \n",
154 var_name, row, col, formula_matrix_.at(row,col), value_input_address );
158 parser_matrix_[row][col].AddConstant(
"Pi", 3.14159265358979323846);
159 parser_matrix_[row][col].AddConstant(
"E", 2.71828182845904523536);
160 if (time_dependent[row*this->value_.n_rows()+col]) {
161 parser_matrix_[row][col].AddConstant(
"t", time.
end());
166 vars += string(
",d");
169 for(
unsigned int row=0; row < this->value_.n_rows(); row++)
170 for(
unsigned int col=0; col < this->value_.n_cols(); col++) {
175 if (time_dependent[row*this->value_.n_rows()+col] || first_time_set_ ) {
176 parser_matrix_[row][col].Parse(formula_matrix_.at(row,col), vars);
178 if ( parser_matrix_[row][col].GetParseErrorType() != FunctionParser::FP_NO_ERROR ) {
179 THROW( ExcFParserError() << EI_FParserMsg(parser_matrix_[row][col].ErrorMsg()) << EI_Row(row)
180 << EI_Col(col) << EI_Formula(formula_matrix_.at(row,col)) );
183 parser_matrix_[row][col].Optimize();
184 any_parser_changed =
true;
190 first_time_set_ =
false;
192 return any_parser_changed;
196 template <
int spacedim,
class Value>
199 std::string surface_region;
200 if ( in_rec_.opt_val(
"surface_region", surface_region) ) {
201 surface_depth_ = std::make_shared<SurfaceDepth>(mesh, surface_region, in_rec_.val<std::string>(
"surface_direction"));
209 template <
int spacedim,
class Value>
213 auto p_depth = this->eval_depth_var(p);
214 for(
unsigned int row=0; row < this->value_.n_rows(); row++)
215 for(
unsigned int col=0; col < this->value_.n_cols(); col++) {
216 this->value_(row,col) = this->unit_conversion_coefficient_ * parser_matrix_[row][col].Eval(p_depth.memptr());
218 return this->r_value_;
225 template <
int spacedim,
class Value>
231 for(
unsigned int i=0; i< point_list.
size(); i++) {
232 Value envelope(value_list[i]);
233 ASSERT_EQ( envelope.n_rows(), this->value_.n_rows() )(i)(envelope.n_rows())(this->value_.n_rows())
234 .error(
"value_list['i'] has wrong number of rows\n");
235 auto p_depth = this->eval_depth_var(point_list.
vec<spacedim>(i));
237 for(
unsigned int row=0; row < this->value_.n_rows(); row++)
238 for(
unsigned int col=0; col < this->value_.n_cols(); col++) {
239 envelope(row,col) = this->unit_conversion_coefficient_ * parser_matrix_[row][col].Eval(p_depth.memptr());
245 template <
int spacedim,
class Value>
252 for (
unsigned int i=reg_chunk_begin; i<reg_chunk_end; ++i) {
255 for (
auto it : eval_field_data_) {
258 auto value_cache =
it.first->value_cache();
259 for (
unsigned int i=reg_chunk_begin; i<reg_chunk_end; ++i) {
260 if (
it.first->name() ==
"X") {
261 x_[i] = value_cache->template vec<3>(i)(0);
262 y_[i] = value_cache->template vec<3>(i)(1);
263 z_[i] = value_cache->template vec<3>(i)(2);
265 it.second[i] = value_cache->data_[i];
273 subset_vec.assign(subsets_ + subsets_begin, subsets_ + subsets_end);
275 for(
unsigned int row=0; row < this->value_.n_rows(); row++)
276 for(
unsigned int col=0; col < this->value_.n_cols(); col++) {
277 b_parser_[row*this->value_.n_cols()+col].set_subset(subset_vec);
278 b_parser_[row*this->value_.n_cols()+col].run();
279 for (
unsigned int i=reg_chunk_begin; i<reg_chunk_end; ++i) {
280 auto cache_val = data_cache.template mat<Value::NRows_, Value::NCols_>(i);
281 cache_val(row, col) = res_[i];
282 data_cache.
set(i) = cache_val;
288 template <
int spacedim,
class Value>
291 if (surface_depth_ && has_depth_var_) {
294 p_depth.subvec(0,spacedim-1) = p;
296 p_depth(spacedim) = surface_depth_->compute_distance(p);
297 }
catch (SurfaceDepth::ExcTooLargeSnapDistance &e) {
298 e << SurfaceDepth::EI_FieldTime(this->time_.end());
299 e << in_rec_.ei_address();
310 for (
auto i : shape) r *= i;
315 template <
int spacedim,
class Value>
317 required_fields_.clear();
320 for(
unsigned int row=0; row < this->value_.n_rows(); row++)
321 for(
unsigned int col=0; col < this->value_.n_cols(); col++) {
323 unsigned int i_p = row*this->value_.n_cols()+col;
325 std::string expr = formula_matrix_.at(row,col);
328 boost::replace_all(expr,
"^",
"**");
329 boost::replace_all(expr,
"max(",
"maximum(");
330 boost::replace_all(expr,
"min(",
"minimum(");
331 boost::replace_all(expr,
"Pi",
"pi");
332 boost::replace_all(expr,
"E",
"e");
334 std::string pref(
"if(");
335 auto res = std::mismatch(pref.begin(), pref.end(), expr.begin());
336 if ( (res.first == pref.end()) && (expr.back() ==
')') ) {
337 std::string subexpr = expr.substr(3, expr.size()-4);
338 std::string delimiter =
",";
339 std::string cond = subexpr.substr(0, subexpr.find(delimiter));
340 subexpr.erase(0, cond.size()+1);
341 std::string if_case = subexpr.substr(0, subexpr.find(delimiter));
342 std::string else_case = subexpr.substr(if_case.size()+1);
343 expr =
"(" + if_case +
" if " + cond +
" else " + else_case +
")";
347 b_parser_[i_p].parse( expr );
348 }
catch (std::exception
const& e) {
349 if (
typeid(e) ==
typeid(bparser::Exception))
350 THROW( ExcParserError() << EI_BParserMsg(e.what()) << EI_Formula(expr) << Input::EI_Address( in_rec_.address_string() ) );
353 auto free_symbols = b_parser_[i_p].free_symbols();
354 variables.insert(variables.end(), free_symbols.begin(), free_symbols.end());
357 std::sort( variables.begin(), variables.end() );
358 variables.erase( std::unique( variables.begin(), variables.end() ), variables.end() );
361 for (
auto var : variables) {
362 if (var ==
"x" || var ==
"y" || var ==
"z") {
363 required_fields_.push_back( field_set.
field(
"X") );
364 sum_shape_sizes_ += spacedim;
366 else if (var ==
"t") has_time_ =
true;
368 auto field_ptr = field_set.
field(var);
369 if (field_ptr !=
nullptr) required_fields_.push_back( field_ptr );
371 field_ptr = field_set.
user_field(var, this->time_);
372 if (field_ptr !=
nullptr) required_fields_.push_back( field_ptr );
373 else THROW( ExcUnknownField() << EI_Field(var) << Input::EI_Address( in_rec_.address_string() ) );
376 if (field_ptr->value_cache() ==
nullptr)
THROW( ExcNotDoubleField() << EI_Field(var) << Input::EI_Address( in_rec_.address_string() ) );
379 sum_shape_sizes_ +=
n_shape( field_ptr->shape_ );
386 return required_fields_;
390 template <
int spacedim,
class Value>
393 if (arena_alloc_!=
nullptr) {
396 eval_field_data_.clear();
401 uint n_vectors = sum_shape_sizes_ + 1;
403 res_ = arena_alloc_->create_array<
double>(vec_size);
404 for (
auto field : required_fields_) {
405 std::string field_name = field->name();
406 eval_field_data_[field] = arena_alloc_->create_array<
double>(
n_shape( field->shape_ ) * vec_size);
407 if (field_name ==
"X") {
408 x_ = eval_field_data_[field] + 0;
409 y_ = eval_field_data_[field] + vec_size;
410 z_ = eval_field_data_[field] + 2*vec_size;
413 subsets_ = arena_alloc_->create_array<
uint>(n_subsets);
415 for(
unsigned int row=0; row < this->value_.n_rows(); row++)
416 for(
unsigned int col=0; col < this->value_.n_cols(); col++) {
418 unsigned int i_p = row*this->value_.n_cols()+col;
420 b_parser_[i_p].set_constant(
"t", {}, {this->time_.end()});
422 for (
auto field : required_fields_) {
423 std::string field_name = field->name();
424 if (field_name ==
"X") {
425 b_parser_[i_p].set_variable(
"x", {}, x_);
426 b_parser_[i_p].set_variable(
"y", {}, y_);
427 b_parser_[i_p].set_variable(
"z", {}, z_);
429 b_parser_[i_p].set_variable(field_name, {}, eval_field_data_[field]);
431 b_parser_[i_p].set_variable(
"_result_", {}, res_);
432 b_parser_[i_p].compile();
434 for (
uint i=0; i<n_subsets; ++i)
439 template <
int spacedim,
class Value>