OGS
MaterialPropertyLib::Function Class Referencefinal

Detailed Description

A function property defined by mathematical expression. For the evaluation of the expressions the exprtk library is used. In the expressions all variables defined in MaterialPropertyLib::Variable enum, t for time, x,y,z for the spatial position are supported, and curves from the <curves> section can be called using their names. A curve is a single argument function and can be used in an expression like curveA(sin(t)).

The evaluation is thread-safe for OpenMP by using per-thread storage.

Definition at line 34 of file Function.h.

#include <Function.h>

Inheritance diagram for MaterialPropertyLib::Function:
[legend]
Collaboration diagram for MaterialPropertyLib::Function:
[legend]

Classes

struct  Implementation

Public Member Functions

 Function (std::string name, std::vector< std::string > const &value_string_expressions, std::vector< std::pair< std::string, std::vector< std::string > > > const &dvalue_string_expressions, std::vector< D2ValueConfig > const &d2value_string_expressions, std::map< std::string, std::unique_ptr< MathLib::PiecewiseLinearInterpolation > > const &curves)
PropertyDataType value (VariableArray const &variable_array, ParameterLib::SpatialPosition const &pos, double const t, double const dt) const override
PropertyDataType dValue (VariableArray const &variable_array, Variable const variable, ParameterLib::SpatialPosition const &pos, double const t, double const dt) const override
PropertyDataType d2Value (VariableArray const &variable_array, Variable const variable1, Variable const variable2, ParameterLib::SpatialPosition const &pos, double const t, double const dt) const override
 Default implementation: 2nd derivative of any constant property is zero.
 ~Function ()
Public Member Functions inherited from MaterialPropertyLib::Property
virtual ~Property ()
virtual PropertyDataType initialValue (ParameterLib::SpatialPosition const &pos, double const t) const
virtual PropertyDataType value () const
virtual PropertyDataType value (VariableArray const &variable_array, VariableArray const &variable_array_prev, ParameterLib::SpatialPosition const &pos, double const t, double const dt) const
virtual PropertyDataType dValue (VariableArray const &variable_array, VariableArray const &variable_array_prev, Variable const variable, ParameterLib::SpatialPosition const &pos, double const t, double const dt) const
virtual void setProperties (std::vector< std::unique_ptr< Phase > > const &phases)
 Default implementation:
void setScale (std::variant< Medium *, Phase *, Component * > scale)
template<typename T>
initialValue (ParameterLib::SpatialPosition const &pos, double const t) const
template<typename T>
value () const
template<typename T>
value (VariableArray const &variable_array, VariableArray const &variable_array_prev, ParameterLib::SpatialPosition const &pos, double const t, double const dt) const
template<typename T>
value (VariableArray const &variable_array, ParameterLib::SpatialPosition const &pos, double const t, double const dt) const
template<typename T>
dValue (VariableArray const &variable_array, VariableArray const &variable_array_prev, Variable const variable, ParameterLib::SpatialPosition const &pos, double const t, double const dt) const
template<typename T>
dValue (VariableArray const &variable_array, Variable const variable, ParameterLib::SpatialPosition const &pos, double const t, double const dt) const
template<typename T>
d2Value (VariableArray const &variable_array, Variable const &variable1, Variable const &variable2, ParameterLib::SpatialPosition const &pos, double const t, double const dt) const

Private Member Functions

std::variant< Function::Implementation< 2 > *, Function::Implementation< 3 > * > getImplementationForDimensionOfVariableArray (VariableArray const &variable_array) const

Private Attributes

std::unique_ptr< Implementation< 2 > > impl2_
std::unique_ptr< Implementation< 3 > > impl3_
std::vector< Variablerequired_variables_enum_
 Variables used in the exprtk expressions.

Additional Inherited Members

Protected Attributes inherited from MaterialPropertyLib::Property
std::string name_
PropertyDataType value_
 The single value of a property.
PropertyDataType dvalue_
std::variant< Medium *, Phase *, Component * > scale_

Constructor & Destructor Documentation

◆ Function()

MaterialPropertyLib::Function::Function ( std::string name,
std::vector< std::string > const & value_string_expressions,
std::vector< std::pair< std::string, std::vector< std::string > > > const & dvalue_string_expressions,
std::vector< D2ValueConfig > const & d2value_string_expressions,
std::map< std::string, std::unique_ptr< MathLib::PiecewiseLinearInterpolation > > const & curves )

Definition at line 560 of file Function.cpp.

569{
570 name_ = std::move(name);
571
572 // Validate the d2value blocks once, before the per-thread/per-dimension
573 // Implementations are built: each unordered {v1,v2} pair may appear at
574 // most once, and each block must carry one expression per value component.
575 {
576 std::vector<VariablePair> seen_pairs;
577 for (auto const& [vname1, vname2, exprs] : d2value_string_expressions)
578 {
579 VariablePair const pair{convertStringToVariable(vname1),
581 if (ranges::contains(seen_pairs, pair))
582 {
583 OGS_FATAL(
584 "Function property '{}': duplicate d2value block for "
585 "variable pair '{}'/'{}'. Each unordered pair may "
586 "appear at most once.",
587 name_, vname1, vname2);
588 }
589 seen_pairs.push_back(pair);
590
591 if (exprs.size() != value_string_expressions.size())
592 {
593 OGS_FATAL(
594 "Function property '{}': the number of d2Value expressions "
595 "({:d}) for variables '{:s}'/'{:s}' does not match the "
596 "number of value expressions ({:d}).",
597 name_, exprs.size(), vname1, vname2,
598 value_string_expressions.size());
599 }
600 }
601 }
602
603 // Collect variables from all expressions (value, dvalue, and d2value) so
604 // the symbol table is complete even when a variable appears only in a
605 // derivative.
606 auto all_exprs = value_string_expressions;
607 for (auto const& [_, exprs] : dvalue_string_expressions)
608 {
609 all_exprs.insert(all_exprs.end(), exprs.begin(), exprs.end());
610 }
611 for (auto const& [_1, _2, exprs] : d2value_string_expressions)
612 {
613 all_exprs.insert(all_exprs.end(), exprs.begin(), exprs.end());
614 }
615 auto const expression_symbol_names =
617
619 expression_symbol_names |
620 ranges::views::filter(
621 [&curves](std::string const& s)
622 {
623 return !ParameterLib::isBuiltinSymbol(s) && !curves.contains(s);
624 }) |
625 ranges::views::transform([](std::string const& s)
626 { return convertStringToVariable(s); }) |
627 ranges::to<std::vector>;
628
629 int const num_threads = BaseLib::getNumberOfThreads();
630
631 impl2_ = std::make_unique<Implementation<2>>(
632 num_threads, expression_symbol_names, required_variables_enum_,
633 value_string_expressions, dvalue_string_expressions,
634 d2value_string_expressions, curves);
635 impl3_ = std::make_unique<Implementation<3>>(
636 num_threads, expression_symbol_names, required_variables_enum_,
637 value_string_expressions, dvalue_string_expressions,
638 d2value_string_expressions, curves);
639}
#define OGS_FATAL(...)
Definition Error.h:10
std::unique_ptr< Implementation< 3 > > impl3_
Definition Function.h:72
std::vector< Variable > required_variables_enum_
Variables used in the exprtk expressions.
Definition Function.h:79
std::unique_ptr< Implementation< 2 > > impl2_
Definition Function.h:71
int getNumberOfThreads()
Variable convertStringToVariable(std::string const &string)
bool isBuiltinSymbol(std::string_view const name)
std::vector< std::string > collectVariables(std::vector< std::string > const &expression_strings)

References ParameterLib::collectVariables(), MaterialPropertyLib::convertStringToVariable(), BaseLib::getNumberOfThreads(), impl2_, impl3_, ParameterLib::isBuiltinSymbol(), MaterialPropertyLib::name, MaterialPropertyLib::Property::name_, OGS_FATAL, and required_variables_enum_.

◆ ~Function()

MaterialPropertyLib::Function::~Function ( )
default

Member Function Documentation

◆ d2Value()

PropertyDataType MaterialPropertyLib::Function::d2Value ( VariableArray const & variable_array,
Variable const variable1,
Variable const variable2,
ParameterLib::SpatialPosition const & pos,
double const t,
double const dt ) const
overridevirtual

Default implementation: 2nd derivative of any constant property is zero.

This virtual method will compute the second derivative of a property with respect to the given variables pv1 and pv2.

Reimplemented from MaterialPropertyLib::Property.

Definition at line 723 of file Function.cpp.

728{
729 return std::visit(
730 [&](auto&& impl_ptr)
731 {
732 auto& thread_data = impl_ptr->per_thread_data[currentThreadId(
733 name_, impl_ptr->per_thread_data.size())];
734 auto const it = ranges::find(thread_data.d2value_expressions,
735 VariablePair{variable1, variable2},
737
738 if (it == thread_data.d2value_expressions.end())
739 {
740 OGS_FATAL(
741 "Requested second derivative with respect to variables "
742 "'{:s}'/'{:s}' not provided for Function-type property "
743 "'{:s}'.",
744 variable_enum_to_string[static_cast<int>(variable1)],
745 variable_enum_to_string[static_cast<int>(variable2)],
746 name_);
747 }
748
749 return thread_data.evaluate(impl_ptr->non_scalar_variables,
750 variable_array, pos, t,
751 it->expressions);
752 },
754}
std::variant< Function::Implementation< 2 > *, Function::Implementation< 3 > * > getImplementationForDimensionOfVariableArray(VariableArray const &variable_array) const
Definition Function.cpp:642
static int currentThreadId(std::string const &property_name, std::size_t const num_slots)
Definition Function.cpp:661
static const std::array< std::string, static_cast< int >(Variable::number_of_variables)> variable_enum_to_string

References MaterialPropertyLib::currentThreadId(), getImplementationForDimensionOfVariableArray(), MaterialPropertyLib::Property::name_, OGS_FATAL, MaterialPropertyLib::variable_enum_to_string, and MaterialPropertyLib::D2ValueExpression::variables.

◆ dValue()

PropertyDataType MaterialPropertyLib::Function::dValue ( VariableArray const & variable_array,
Variable const variable,
ParameterLib::SpatialPosition const & pos,
double const t,
double const dt ) const
overridevirtual

This virtual method will compute the property derivative value based on the variables that are passed as arguments with the default implementation using empty variables array for the previous time step.

The default implementation of this method only returns the property value derivative without altering it.

Reimplemented from MaterialPropertyLib::Property.

Definition at line 695 of file Function.cpp.

699{
700 return std::visit(
701 [&](auto&& impl_ptr)
702 {
703 auto& thread_data = impl_ptr->per_thread_data[currentThreadId(
704 name_, impl_ptr->per_thread_data.size())];
705 auto const it = ranges::find_if(thread_data.dvalue_expressions,
706 [&variable](auto const& v)
707 { return v.first == variable; });
708
709 if (it == end(thread_data.dvalue_expressions))
710 {
711 OGS_FATAL(
712 "Requested derivative with respect to the variable {:s} "
713 "not provided for Function-type property {:s}.",
714 variable_enum_to_string[static_cast<int>(variable)], name_);
715 }
716
717 return thread_data.evaluate(impl_ptr->non_scalar_variables,
718 variable_array, pos, t, it->second);
719 },
721}

References MaterialPropertyLib::currentThreadId(), getImplementationForDimensionOfVariableArray(), MaterialPropertyLib::Property::name_, OGS_FATAL, and MaterialPropertyLib::variable_enum_to_string.

◆ getImplementationForDimensionOfVariableArray()

std::variant< Function::Implementation< 2 > *, Function::Implementation< 3 > * > MaterialPropertyLib::Function::getImplementationForDimensionOfVariableArray ( VariableArray const & variable_array) const
private

Definition at line 642 of file Function.cpp.

644{
645 if (variable_array.is2D())
646 {
647 return impl2_.get();
648 }
649 if (variable_array.is3D())
650 {
651 return impl3_.get();
652 }
653
654 OGS_FATAL(
655 "Variable array has vectors for 2 and 3 dimensions simultaneously. "
656 "Mixed dimensions cannot be dealt within Function evaluation.");
657}

References impl2_, impl3_, MaterialPropertyLib::VariableArray::is2D(), MaterialPropertyLib::VariableArray::is3D(), and OGS_FATAL.

Referenced by d2Value(), dValue(), and value().

◆ value()

PropertyDataType MaterialPropertyLib::Function::value ( VariableArray const & variable_array,
ParameterLib::SpatialPosition const & pos,
double const t,
double const dt ) const
overridevirtual

This virtual method will compute the property value based on the variables that are passed as arguments with the default implementation using empty variables array for the previous time step.

Reimplemented from MaterialPropertyLib::Property.

Definition at line 679 of file Function.cpp.

682{
683 return std::visit(
684 [&](auto&& impl_ptr)
685 {
686 auto& thread_data = impl_ptr->per_thread_data[currentThreadId(
687 name_, impl_ptr->per_thread_data.size())];
688 return thread_data.evaluate(impl_ptr->non_scalar_variables,
689 variable_array, pos, t,
690 thread_data.value_expressions);
691 },
693}

References MaterialPropertyLib::currentThreadId(), getImplementationForDimensionOfVariableArray(), and MaterialPropertyLib::Property::name_.

Member Data Documentation

◆ impl2_

std::unique_ptr<Implementation<2> > MaterialPropertyLib::Function::impl2_
private

Definition at line 71 of file Function.h.

Referenced by Function(), and getImplementationForDimensionOfVariableArray().

◆ impl3_

std::unique_ptr<Implementation<3> > MaterialPropertyLib::Function::impl3_
private

Definition at line 72 of file Function.h.

Referenced by Function(), and getImplementationForDimensionOfVariableArray().

◆ required_variables_enum_

std::vector<Variable> MaterialPropertyLib::Function::required_variables_enum_
private

Variables used in the exprtk expressions.

Definition at line 79 of file Function.h.

Referenced by Function().


The documentation for this class was generated from the following files: