ParsedFEMFunction provides support for FParser-based parsed functions in FEMSystem.
More...
#include <parsed_fem_function.h>
|
| ParsedFEMFunction (const System &sys, const std::string &expression, const std::vector< std::string > *additional_vars=nullptr, const std::vector< Output > *initial_vals=nullptr) |
| Constructor. More...
|
|
ParsedFEMFunction & | operator= (const ParsedFEMFunction &)=delete |
| This class contains a const reference so it can't be copy or move-assigned. More...
|
|
ParsedFEMFunction & | operator= (ParsedFEMFunction &&)=delete |
|
| ParsedFEMFunction (const ParsedFEMFunction &)=default |
| The remaining 5 special functions can be safely defaulted. More...
|
|
| ParsedFEMFunction (ParsedFEMFunction &&)=default |
|
virtual | ~ParsedFEMFunction ()=default |
|
void | reparse (const std::string &expression) |
| Re-parse with new expression. More...
|
|
virtual void | init_context (const FEMContext &c) override |
| Prepares a context object for use. More...
|
|
virtual std::unique_ptr< FEMFunctionBase< Output > > | clone () const override |
|
virtual Output | operator() (const FEMContext &c, const Point &p, const Real time=0.) override |
|
void | operator() (const FEMContext &c, const Point &p, const Real time, DenseVector< Output > &output) override |
| Evaluation function for time-dependent vector-valued functions. More...
|
|
virtual Output | component (const FEMContext &c, unsigned int i, const Point &p, Real time=0.) override |
|
const std::string & | expression () |
|
Output | get_inline_value (const std::string &inline_var_name) const |
|
void | set_inline_value (const std::string &inline_var_name, Output newval) |
| Changes the value of an inline variable. More...
|
|
void | operator() (const FEMContext &, const Point &p, DenseVector< Output > &output) |
| Evaluation function for time-independent vector-valued functions. More...
|
|
|
void | partial_reparse (const std::string &expression) |
|
std::size_t | find_name (const std::string &varname, const std::string &expr) const |
|
void | eval_args (const FEMContext &c, const Point &p, const Real time) |
|
Output | eval (FunctionParserBase< Output > &parser, const std::string &libmesh_dbg_var(function_name), unsigned int libmesh_dbg_var(component_idx)) const |
|
Output | eval (char &libmesh_dbg_var(parser), const std::string &libmesh_dbg_var(function_name), unsigned int libmesh_dbg_var(component_idx)) const |
|
template<typename Output = Number>
class libMesh::ParsedFEMFunction< Output >
ParsedFEMFunction provides support for FParser-based parsed functions in FEMSystem.
All overridden virtual functions are documented in fem_function_base.h.
- Author
- Roy Stogner
- Date
- 2014
Support for using parsed functions in FEMSystem.
Definition at line 57 of file parsed_fem_function.h.
◆ ParsedFEMFunction() [1/3]
template<typename Output>
Constructor.
Definition at line 202 of file parsed_fem_function.h.
215 #ifdef LIBMESH_ENABLE_SECOND_DERIVATIVES
217 #endif // LIBMESH_ENABLE_SECOND_DERIVATIVES
218 _additional_vars (additional_vars ? *additional_vars : std::vector<std::string>()),
219 _initial_vals (initial_vals ? *initial_vals : std::vector<Output>())
◆ ParsedFEMFunction() [2/3]
template<typename Output = Number>
The remaining 5 special functions can be safely defaulted.
- Note
- The underlying FunctionParserBase class has a copy constructor, so this class should be default-constructible. And, although FunctionParserBase's move constructor is deleted, this class should still be move-constructible because FunctionParserBase only appears in a vector.
◆ ParsedFEMFunction() [3/3]
template<typename Output = Number>
◆ ~ParsedFEMFunction()
template<typename Output = Number>
◆ clone()
template<typename Output >
- Returns
- A new copy of the function.
The new copy should be as "deep" as necessary to allow independent destruction and simultaneous evaluations of the copies in different threads.
Implements libMesh::FEMFunctionBase< Output >.
Definition at line 389 of file parsed_fem_function.h.
391 return libmesh_make_unique<ParsedFEMFunction>
Referenced by main().
◆ component()
template<typename Output >
- Returns
- The vector component
i
at coordinate p
and time time
.
- Note
- Subclasses aren't required to override this, since the default implementation is based on the full vector evaluation, which is often correct.
-
Subclasses are recommended to override this, since the default implementation is based on a vector evaluation, which is usually unnecessarily inefficient.
Reimplemented from libMesh::FEMFunctionBase< Output >.
Definition at line 431 of file parsed_fem_function.h.
438 libmesh_assert_less (i,
parsers.size());
◆ eval() [1/2]
template<typename Output = Number>
Output libMesh::ParsedFEMFunction< Output >::eval |
( |
char & |
libmesh_dbg_varparser, |
|
|
const std::string & |
libmesh_dbg_varfunction_name, |
|
|
unsigned int |
libmesh_dbg_varcomponent_idx |
|
) |
| const |
|
inlineprotected |
◆ eval() [2/2]
template<typename Output>
Output libMesh::ParsedFEMFunction< Output >::eval |
( |
FunctionParserBase< Output > & |
parser, |
|
|
const std::string & |
libmesh_dbg_varfunction_name, |
|
|
unsigned int |
libmesh_dbg_varcomponent_idx |
|
) |
| const |
|
inlineprotected |
Definition at line 803 of file parsed_fem_function.h.
808 Output result = parser.Eval(
_spacetime.data());
809 int error_code = parser.EvalError();
812 libMesh::err <<
"ERROR: FunctionParser is unable to evaluate component "
814 <<
" of expression '"
816 <<
"' with arguments:\n";
822 std::string error_message =
"Reason: ";
827 error_message +=
"Division by zero";
830 error_message +=
"Square Root error (negative value)";
833 error_message +=
"Log error (negative value)";
836 error_message +=
"Trigonometric error (asin or acos of illegal value)";
839 error_message +=
"Maximum recursion level reached";
842 error_message +=
"Unknown";
845 libmesh_error_msg(error_message);
◆ eval_args()
template<typename Output >
Definition at line 674 of file parsed_fem_function.h.
687 unsigned int request_index = 0;
688 for (
unsigned int v=0; v !=
_n_vars; ++v)
693 c.point_value(v, p,
_spacetime[LIBMESH_DIM+1+request_index]);
698 for (
unsigned int v=0; v !=
_n_vars; ++v)
711 c.point_gradient(v, p, g);
713 for (
unsigned int d=0; d != LIBMESH_DIM; ++d)
718 _spacetime[LIBMESH_DIM+1+request_index] = g(d);
723 #ifdef LIBMESH_ENABLE_SECOND_DERIVATIVES
725 for (
unsigned int v=0; v !=
_n_vars; ++v)
744 c.point_hessian(v, p, h);
746 for (
unsigned int d1=0; d1 != LIBMESH_DIM; ++d1)
747 for (
unsigned int d2=0; d2 != LIBMESH_DIM; ++d2)
752 _spacetime[LIBMESH_DIM+1+request_index] = h(d1,d2);
756 #endif // LIBMESH_ENABLE_SECOND_DERIVATIVES
761 c.get_side_fe(0, side_fe);
763 const std::vector<Point> & normals = side_fe->get_normals();
765 const std::vector<Point> & xyz = side_fe->get_xyz();
767 libmesh_assert_equal_to(normals.size(), xyz.size());
771 bool at_quadrature_point =
false;
777 const Point & n = normals[qp];
778 for (
unsigned int d=0; d != LIBMESH_DIM; ++d)
780 _spacetime[LIBMESH_DIM+1+request_index] = n(d);
784 at_quadrature_point =
true;
◆ expression()
template<typename Output = Number>
◆ find_name()
template<typename Output >
Definition at line 648 of file parsed_fem_function.h.
651 const std::size_t namesize = varname.size();
652 std::size_t varname_i = expr.find(varname);
654 while ((varname_i != std::string::npos) &&
656 (std::isalnum(expr[varname_i-1]) ||
657 (expr[varname_i-1] ==
'_'))) ||
658 ((varname_i+namesize < expr.size()) &&
659 (std::isalnum(expr[varname_i+namesize]) ||
660 (expr[varname_i+namesize] ==
'_')))))
662 varname_i = expr.find(varname, varname_i+1);
◆ get_inline_value()
template<typename Output >
- Returns
- The value of an inline variable.
- Note
- Will only be correct if the inline variable value is independent of input variables, if the inline variable is not redefined within any subexpression, and if the inline variable takes the same value within any subexpressions where it appears.
Definition at line 445 of file parsed_fem_function.h.
450 bool found_var_name =
false;
452 Output old_var_value(0.);
456 const std::size_t varname_i =
457 find_name(inline_var_name, subexpression);
458 if (varname_i == std::string::npos)
461 const std::size_t assignment_i =
462 subexpression.find(
":", varname_i+1);
464 libmesh_assert_not_equal_to(assignment_i, std::string::npos);
466 libmesh_assert_equal_to(subexpression[assignment_i+1],
'=');
467 for (std::size_t i = varname_i+1; i != assignment_i; ++i)
468 libmesh_assert_equal_to(subexpression[i],
' ');
470 std::size_t end_assignment_i =
471 subexpression.find(
";", assignment_i+1);
473 libmesh_assert_not_equal_to(end_assignment_i, std::string::npos);
475 std::string new_subexpression =
476 subexpression.substr(0, end_assignment_i+1) +
479 #ifdef LIBMESH_HAVE_FPARSER
482 FunctionParserBase<Output> fp;
483 fp.AddConstant(
"NaN", std::numeric_limits<Real>::quiet_NaN());
484 fp.AddConstant(
"pi", std::acos(
Real(-1)));
485 fp.AddConstant(
"e", std::exp(
Real(1)));
486 if (fp.Parse(new_subexpression,
variables) != -1)
488 (
"ERROR: FunctionParser is unable to parse modified expression: "
489 << new_subexpression <<
'\n' << fp.ErrorMsg());
491 Output new_var_value = this->
eval(fp, new_subexpression, 0);
493 return new_var_value;
497 libmesh_assert_equal_to(old_var_value, new_var_value);
501 old_var_value = new_var_value;
502 found_var_name =
true;
507 libmesh_error_msg(
"ERROR: This functionality requires fparser!");
512 return old_var_value;
Referenced by libMesh::ParsedFEMFunctionParameter< T >::get(), ParsedFEMFunctionTest::testInlineGetter(), and ParsedFEMFunctionTest::testInlineSetter().
◆ init_context()
template<typename Output >
Prepares a context object for use.
Most problems will want to reimplement this for efficiency, in order to call FE::get_*() as their particular function requires.
Reimplemented from libMesh::FEMFunctionBase< Output >.
Definition at line 357 of file parsed_fem_function.h.
359 for (
unsigned int v=0; v !=
_n_vars; ++v)
362 c.get_element_fe(v, elem_fe);
367 #ifdef LIBMESH_ENABLE_SECOND_DERIVATIVES
369 elem_fe->get_d2phi();
370 #endif // LIBMESH_ENABLE_SECOND_DERIVATIVES
376 c.get_side_fe(0, side_fe);
378 side_fe->get_normals();
◆ operator()() [1/3]
template<typename Output >
Evaluation function for time-independent vector-valued functions.
Sets output values in the passed-in output
DenseVector.
Definition at line 144 of file fem_function_base.h.
◆ operator()() [2/3]
template<typename Output>
◆ operator()() [3/3]
template<typename Output >
◆ operator=() [1/2]
template<typename Output = Number>
This class contains a const reference so it can't be copy or move-assigned.
◆ operator=() [2/2]
template<typename Output = Number>
◆ partial_reparse()
template<typename Output >
Definition at line 584 of file parsed_fem_function.h.
590 size_t nextstart = 0,
end = 0;
592 while (
end != std::string::npos)
608 end = std::string::npos;
614 std::string::npos :
end - nextstart));
618 libmesh_error_msg(
"ERROR: FunctionParser is unable to parse empty expression.\n");
621 #ifdef LIBMESH_HAVE_FPARSER
624 FunctionParserBase<Output> fp;
625 fp.AddConstant(
"NaN", std::numeric_limits<Real>::quiet_NaN());
626 fp.AddConstant(
"pi", std::acos(
Real(-1)));
627 fp.AddConstant(
"e", std::exp(
Real(1)));
630 (
"ERROR: FunctionParser is unable to parse expression: "
635 libmesh_error_msg(
"ERROR: This functionality requires fparser!");
640 nextstart = (
end == std::string::npos) ?
641 std::string::npos :
end + 1;
◆ reparse()
template<typename Output >
Re-parse with new expression.
Definition at line 228 of file parsed_fem_function.h.
239 for (
unsigned int v=0; v !=
_n_vars; ++v)
246 if (varname_i == std::string::npos)
255 for (
unsigned int v=0; v !=
_n_vars; ++v)
259 for (
unsigned int d=0; d != LIBMESH_DIM; ++d)
261 std::string gradname = std::string(
"grad_") +
262 "xyz"[d] +
'_' + varname;
267 if (gradname_i == std::string::npos)
277 #ifdef LIBMESH_ENABLE_SECOND_DERIVATIVES
278 for (
unsigned int v=0; v !=
_n_vars; ++v)
282 for (
unsigned int d1=0; d1 != LIBMESH_DIM; ++d1)
283 for (
unsigned int d2=0; d2 != LIBMESH_DIM; ++d2)
285 std::string hessname = std::string(
"hess_") +
286 "xyz"[d1] +
"xyz"[d2] +
'_' + varname;
291 if (hessname_i == std::string::npos)
301 #endif // LIBMESH_ENABLE_SECOND_DERIVATIVES
310 if (nx_i != std::string::npos ||
311 ny_i != std::string::npos ||
312 nz_i != std::string::npos)
◆ set_inline_value()
template<typename Output>
Changes the value of an inline variable.
- Note
- Forever after, the variable value will take the given constant, independent of input variables, in every subexpression where it is already defined.
-
Currently only works if the inline variable is not redefined within any one subexpression.
Definition at line 518 of file parsed_fem_function.h.
524 bool found_var_name =
false;
529 const std::size_t varname_i =
530 find_name(inline_var_name, subexpression);
531 if (varname_i == std::string::npos)
535 found_var_name =
true;
538 const std::size_t assignment_i =
539 subexpression.find(
":", varname_i+1);
541 libmesh_assert_not_equal_to(assignment_i, std::string::npos);
543 libmesh_assert_equal_to(subexpression[assignment_i+1],
'=');
544 for (std::size_t i = varname_i+1; i != assignment_i; ++i)
545 libmesh_assert_equal_to(subexpression[i],
' ');
547 std::size_t end_assignment_i =
548 subexpression.find(
";", assignment_i+1);
550 libmesh_assert_not_equal_to(end_assignment_i, std::string::npos);
552 std::ostringstream new_subexpression;
553 new_subexpression << subexpression.substr(0, assignment_i+2)
554 << std::setprecision(std::numeric_limits<Output>::digits10+2)
555 #ifdef LIBMESH_USE_COMPLEX_NUMBERS
556 <<
'(' << newval.real() <<
'+'
557 << newval.imag() <<
'i' <<
')'
561 << subexpression.substr(end_assignment_i,
568 std::string new_expression;
572 new_expression +=
'{';
573 new_expression += subexpression;
574 new_expression +=
'}';
Referenced by libMesh::ParsedFEMFunctionParameter< T >::set(), and ParsedFEMFunctionTest::testInlineSetter().
◆ _additional_vars
template<typename Output = Number>
◆ _expression
template<typename Output = Number>
◆ _initial_vals
template<typename Output = Number>
◆ _n_requested_grad_components
template<typename Output = Number>
◆ _n_requested_hess_components
template<typename Output = Number>
◆ _n_requested_vars
template<typename Output = Number>
◆ _n_vars
template<typename Output = Number>
◆ _need_var
template<typename Output = Number>
◆ _need_var_grad
template<typename Output = Number>
◆ _need_var_hess
template<typename Output = Number>
◆ _requested_normals
template<typename Output = Number>
◆ _spacetime
template<typename Output = Number>
◆ _subexpressions
template<typename Output = Number>
◆ _sys
template<typename Output = Number>
◆ parsers [1/2]
template<typename Output = Number>
◆ parsers [2/2]
template<typename Output = Number>
◆ variables
template<typename Output = Number>
The documentation for this class was generated from the following file: