OGS
MaterialPropertyLib::PerThreadData Struct Reference

Detailed Description

Symbol table storage and compiled expressions for one OpenMP thread. Each thread receives its own instance so that concurrent evaluations do not race on the exprtk symbol table or the VariableArray scratch space. The symbol table has to point to some fixed addresses. Therefore, it cannot directly use a user-provided VariableArray during evaluation. Instead, a user-provided VariableArray will be copied to this object before expression evaluation.

Definition at line 160 of file Function.cpp.

Collaboration diagram for MaterialPropertyLib::PerThreadData:
[legend]

Public Member Functions

template<int D>
 PerThreadData (std::integral_constant< int, D >, std::vector< std::string > const &expression_symbol_names, bool spatial_position_is_required, std::vector< std::string > const &used_curve_names, std::map< std::string, std::unique_ptr< MathLib::PiecewiseLinearInterpolation > > const &curves, std::vector< Variable > const &variables_enum, std::vector< Variable > *non_scalar_out, 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)
 PerThreadData (PerThreadData &&)
PerThreadDataoperator= (PerThreadData &&other) noexcept=delete
 PerThreadData (PerThreadData const &)=delete
PerThreadDataoperator= (PerThreadData const &)=delete
void updateNonScalarVariables (std::vector< Variable > const &non_scalar_variables, VariableArray const &new_variable_array)
PropertyDataType evaluate (std::vector< Variable > const &non_scalar_variables, VariableArray const &new_variable_array, ParameterLib::SpatialPosition const &pos, double t, std::vector< exprtk::expression< double > > const &expressions)
void buildCopyOps (std::vector< Variable > const &variables_enum, std::vector< Variable > *non_scalar_out)
void compileExpressions (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)

Public Attributes

std::map< std::string, ParameterLib::CurveWrappercurve_wrappers
 Curve wrappers owned by this thread; must outlive the symbol table.
VariableArray variable_array
exprtk::symbol_table< double > symbol_table
std::vector< exprtk::expression< double > > value_expressions
std::vector< std::pair< Variable, std::vector< exprtk::expression< double > > > > dvalue_expressions
std::vector< D2ValueExpressiond2value_expressions
ParameterLib::SymbolTableCache symbol_table_cache
 Cached pointers to symbol table variables (t, x, y, z).
std::vector< ScalarCopyOpscalar_copy_ops
 Scalar copy operations into this thread's symbol table.

Constructor & Destructor Documentation

◆ PerThreadData() [1/3]

template<int D>
MaterialPropertyLib::PerThreadData::PerThreadData ( std::integral_constant< int, D > ,
std::vector< std::string > const & expression_symbol_names,
bool spatial_position_is_required,
std::vector< std::string > const & used_curve_names,
std::map< std::string, std::unique_ptr< MathLib::PiecewiseLinearInterpolation > > const & curves,
std::vector< Variable > const & variables_enum,
std::vector< Variable > * non_scalar_out,
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 )
inline

Definition at line 163 of file Function.cpp.

178 expression_symbol_names, spatial_position_is_required,
179 used_curve_names, curves, variable_array, curve_wrappers)),
180 symbol_table_cache(symbol_table, spatial_position_is_required)
181 {
182 buildCopyOps(variables_enum, non_scalar_out);
183 compileExpressions(value_string_expressions, dvalue_string_expressions,
184 d2value_string_expressions);
185 }
static exprtk::symbol_table< double > createSymbolTable(std::vector< std::string > const &expression_symbol_names, bool const spatial_position_is_required, std::vector< std::string > const &used_curve_names, std::map< std::string, std::unique_ptr< MathLib::PiecewiseLinearInterpolation > > const &curves, VariableArray &variable_array, std::map< std::string, ParameterLib::CurveWrapper > &curve_wrappers)
Definition Function.cpp:66
std::map< std::string, ParameterLib::CurveWrapper > curve_wrappers
Curve wrappers owned by this thread; must outlive the symbol table.
Definition Function.cpp:198
void buildCopyOps(std::vector< Variable > const &variables_enum, std::vector< Variable > *non_scalar_out)
Definition Function.cpp:281
ParameterLib::SymbolTableCache symbol_table_cache
Cached pointers to symbol table variables (t, x, y, z).
Definition Function.cpp:251
void compileExpressions(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)
Definition Function.cpp:314
exprtk::symbol_table< double > symbol_table
Definition Function.cpp:208

References buildCopyOps(), compileExpressions(), MaterialPropertyLib::createSymbolTable(), curve_wrappers, symbol_table, symbol_table_cache, and variable_array.

Referenced by PerThreadData(), PerThreadData(), operator=(), and operator=().

◆ PerThreadData() [2/3]

MaterialPropertyLib::PerThreadData::PerThreadData ( PerThreadData && )
inline

Definition at line 187 of file Function.cpp.

188 {
189 OGS_FATAL(
190 "MPL's Function property: The internal PerThreadData is not "
191 "move-constructible.");
192 }
#define OGS_FATAL(...)
Definition Error.h:10

References PerThreadData(), and OGS_FATAL.

◆ PerThreadData() [3/3]

MaterialPropertyLib::PerThreadData::PerThreadData ( PerThreadData const & )
delete

References PerThreadData().

Member Function Documentation

◆ buildCopyOps()

void MaterialPropertyLib::PerThreadData::buildCopyOps ( std::vector< Variable > const & variables_enum,
std::vector< Variable > * non_scalar_out )
inline

Build scalar_copy_ops for this thread.

Parameters
variables_enumAll variables to visit.
non_scalar_outIf non-null, non-scalar variables are appended here (pass only for thread 0).

Definition at line 281 of file Function.cpp.

283 {
284 for (auto const variable : variables_enum)
285 {
286 auto const add_non_scalar = [&]()
287 {
288 if (non_scalar_out)
289 {
290 non_scalar_out->push_back(variable);
291 }
292 };
293
294 variable_array.visitVariable(
295 BaseLib::Overloaded{[&](VariableArray::Scalar* dst)
296 {
297 scalar_copy_ops.emplace_back(
298 variable_array, dst, variable);
299 },
301 { add_non_scalar(); },
303 { add_non_scalar(); }},
304 variable);
305 }
306 }
std::variant< std::monostate, Eigen::Vector< double, 5 >, Eigen::Vector< double, 9 > > DeformationGradient
std::variant< std::monostate, Eigen::Vector< double, 4 >, Eigen::Vector< double, 6 > > KelvinVector
std::vector< ScalarCopyOp > scalar_copy_ops
Scalar copy operations into this thread's symbol table.
Definition Function.cpp:254

References scalar_copy_ops, and variable_array.

Referenced by PerThreadData().

◆ compileExpressions()

void MaterialPropertyLib::PerThreadData::compileExpressions ( 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 )
inline

Compile value, dValue, and d2Value expressions for this thread.

Parameters
value_string_expressionsRaw value expression strings.
dvalue_string_expressionsVariable name paired with its derivative expression strings.
d2value_string_expressionsTwo variable names and second- derivative expression strings.

Definition at line 314 of file Function.cpp.

319 {
321 symbol_table, value_string_expressions);
322
323 for (auto const& [variable_name, string_expressions] :
324 dvalue_string_expressions)
325 {
326 if (string_expressions.size() != value_string_expressions.size())
327 {
328 OGS_FATAL(
329 "The number of dValue expressions ({:d}) for variable "
330 "'{:s}' does not match the number of value expressions "
331 "({:d}).",
332 string_expressions.size(), variable_name,
333 value_string_expressions.size());
334 }
335 dvalue_expressions.emplace_back(
336 convertStringToVariable(variable_name),
338 string_expressions));
339 }
340
341 // Component-count consistency is validated once in the Function
342 // constructor; here only the per-thread compilation remains.
343 for (auto const& [variable_name1, variable_name2, string_expressions] :
344 d2value_string_expressions)
345 {
346 d2value_expressions.emplace_back(
347 convertStringToVariable(variable_name1),
348 convertStringToVariable(variable_name2),
350 string_expressions));
351 }
352 }
Variable convertStringToVariable(std::string const &string)
std::vector< exprtk::expression< T > > compileExpressions(exprtk::symbol_table< T > &symbol_table, std::vector< std::string > const &string_expressions)
Definition ExprtkUtils.h:74
std::vector< std::pair< Variable, std::vector< exprtk::expression< double > > > > dvalue_expressions
Definition Function.cpp:233
std::vector< exprtk::expression< double > > value_expressions
Definition Function.cpp:219
std::vector< D2ValueExpression > d2value_expressions
Definition Function.cpp:248

References ParameterLib::compileExpressions(), MaterialPropertyLib::convertStringToVariable(), d2value_expressions, dvalue_expressions, OGS_FATAL, symbol_table, and value_expressions.

Referenced by PerThreadData().

◆ evaluate()

PropertyDataType MaterialPropertyLib::PerThreadData::evaluate ( std::vector< Variable > const & non_scalar_variables,
VariableArray const & new_variable_array,
ParameterLib::SpatialPosition const & pos,
double t,
std::vector< exprtk::expression< double > > const & expressions )

Evaluate expressions against the given variable array and position, returning the result as a PropertyDataType.

Parameters
non_scalar_variablesNon-scalar variables from Implementation (shared read-only).
new_variable_arrayInput variable array for this evaluation.
posSpatial position (coordinates required when spatial variables x, y, z are used).
tCurrent time.
expressionsCompiled exprtk expressions to evaluate.

Definition at line 519 of file Function.cpp.

524{
525 // Fast path: direct offset arithmetic.
526 for (auto const& op : scalar_copy_ops)
527 {
528 op.apply(new_variable_array);
529 }
530 // Fallback for KelvinVector / DeformationGradient variables.
531 updateNonScalarVariables(non_scalar_variables, new_variable_array);
532
533 // Set symbol table variables using cached pointers.
534 symbol_table_cache.setTimeAndPosition(t, pos);
535
536 // Dispatch on expression count: template specialisations fix N at compile
537 // time, enabling stack allocation in evaluateToArray and branch-free
538 // conversion in fromArray.
539 auto const n = expressions.size();
540 switch (n)
541 {
542 case 1:
543 return fromArray(evaluateToArray<1>(expressions));
544 case 2:
545 return fromArray(evaluateToArray<2>(expressions));
546 case 3:
547 return fromArray(evaluateToArray<3>(expressions));
548 case 4:
549 return fromArray(evaluateToArray<4>(expressions));
550 case 6:
551 return fromArray(evaluateToArray<6>(expressions));
552 case 9:
553 return fromArray(evaluateToArray<9>(expressions));
554 default:
555 OGS_FATAL(
556 "Cannot convert a vector of size {} to a PropertyDataType", n);
557 }
558}
static std::array< double, N > evaluateToArray(std::vector< exprtk::expression< double > > const &expressions)
Definition Function.cpp:508
PropertyDataType fromArray(std::array< double, N > const &values)
void updateNonScalarVariables(std::vector< Variable > const &non_scalar_variables, VariableArray const &new_variable_array)
Definition Function.cpp:417

References MaterialPropertyLib::evaluateToArray(), MaterialPropertyLib::fromArray(), OGS_FATAL, scalar_copy_ops, symbol_table_cache, and updateNonScalarVariables().

◆ operator=() [1/2]

PerThreadData & MaterialPropertyLib::PerThreadData::operator= ( PerThreadData && other)
deletenoexcept

References PerThreadData().

◆ operator=() [2/2]

PerThreadData & MaterialPropertyLib::PerThreadData::operator= ( PerThreadData const & )
delete

References PerThreadData().

◆ updateNonScalarVariables()

void MaterialPropertyLib::PerThreadData::updateNonScalarVariables ( std::vector< Variable > const & non_scalar_variables,
VariableArray const & new_variable_array )

Update non-scalar (KelvinVector / DeformationGradient) variables in this thread's variable_array from new_variable_array.

Definition at line 417 of file Function.cpp.

420{
421 for (auto const& variable : non_scalar_variables)
422 {
423 auto assign_kelvin_vector = [&variable, &new_variable_array](
425 {
426 auto assign_value = [&destination = *address,
427 &variable]<typename S>(S const& source)
428 {
429 if constexpr (std::is_same_v<S, std::monostate>)
430 {
431 OGS_FATAL(
432 "Function property: Kelvin vector variable '{:s}' is "
433 "not initialized.",
434 variable_enum_to_string[static_cast<int>(variable)]);
435 }
436 else
437 {
438 if (std::holds_alternative<S>(destination))
439 {
440 std::get<S>(destination) = MathLib::KelvinVector::
442 }
443 else
444 {
445 OGS_FATAL(
446 "Function property: Mismatch of Kelvin vector "
447 "sizes for variable {:s}.",
448 variable_enum_to_string[static_cast<int>(
449 variable)]);
450 }
451 }
452 };
453 std::visit(assign_value,
454 *std::get<VariableArray::KelvinVector const*>(
455 new_variable_array.address_of(variable)));
456 };
457 auto assign_deformation_gradient =
458 [&variable,
459 &new_variable_array](VariableArray::DeformationGradient* address)
460 {
461 auto assign_value = [&destination = *address,
462 &variable]<typename S>(S const& source)
463 {
464 if constexpr (std::is_same_v<S, std::monostate>)
465 {
466 OGS_FATAL(
467 "Function property: Vectorized tensor variable '{:s}' "
468 "is not initialized.",
469 variable_enum_to_string[static_cast<int>(variable)]);
470 }
471 else
472 {
473 if (std::holds_alternative<S>(destination))
474 {
475 std::get<S>(destination) = source;
476 }
477 else
478 {
479 OGS_FATAL(
480 "Function property: Mismatch of vectorized tensor "
481 "sizes for variable {:s}.",
482 variable_enum_to_string[static_cast<int>(
483 variable)]);
484 }
485 }
486 };
487 std::visit(assign_value,
488 *std::get<VariableArray::DeformationGradient const*>(
489 new_variable_array.address_of(variable)));
490 };
491
492 variable_array.visitVariable(
493 BaseLib::Overloaded{
495 {
496 OGS_FATAL(
497 "Function property: updateNonScalarVariables called "
498 "with a scalar variable.");
499 },
500 assign_kelvin_vector, assign_deformation_gradient},
501 variable);
502 }
503}
static const std::array< std::string, static_cast< int >(Variable::number_of_variables)> variable_enum_to_string
Eigen::Matrix< double, 4, 1 > kelvinVectorToSymmetricTensor(Eigen::Matrix< double, 4, 1, Eigen::ColMajor, 4, 1 > const &v)

References MaterialPropertyLib::VariableArray::address_of(), MathLib::KelvinVector::kelvinVectorToSymmetricTensor(), OGS_FATAL, variable_array, and MaterialPropertyLib::variable_enum_to_string.

Referenced by evaluate().

Member Data Documentation

◆ curve_wrappers

std::map<std::string, ParameterLib::CurveWrapper> MaterialPropertyLib::PerThreadData::curve_wrappers

Curve wrappers owned by this thread; must outlive the symbol table.

Definition at line 198 of file Function.cpp.

Referenced by PerThreadData().

◆ d2value_expressions

std::vector<D2ValueExpression> MaterialPropertyLib::PerThreadData::d2value_expressions

Second-derivative expressions w.r.t. an unordered pair of variables. Each entry stores the VariablePair - where (v1, v2) and (v2, v1) are treated as identical - together with multi-component expressions corresponding one-to-one with the value expressions. Specified in the project file as:

<d2value>
<variable_name>temperature</variable_name>
<variable_name>phase_pressure</variable_name>
<expression>d2f1/dT dp</expression>
<expression>d2f2/dT dp</expression>
</d2value>

Definition at line 248 of file Function.cpp.

Referenced by compileExpressions().

◆ dvalue_expressions

std::vector<std::pair<Variable, std::vector<exprtk::expression<double> > > > MaterialPropertyLib::PerThreadData::dvalue_expressions

Derivative expressions w.r.t. a differentiation variable. Outer vector is an unordered set of (Variable, expressions) pairs. Each pair's second element holds multi-component expressions, corresponding one-to-one with the input XML entries, e.g.:

<dvalue>
<variable>T</variable>
<expression>df1/dT</expression>
<expression>df2/dT</expression>
</dvalue>

Definition at line 233 of file Function.cpp.

Referenced by compileExpressions().

◆ scalar_copy_ops

std::vector<ScalarCopyOp> MaterialPropertyLib::PerThreadData::scalar_copy_ops

Scalar copy operations into this thread's symbol table.

Definition at line 254 of file Function.cpp.

Referenced by buildCopyOps(), and evaluate().

◆ symbol_table

exprtk::symbol_table<double> MaterialPropertyLib::PerThreadData::symbol_table

Symbol table holding variable storage. Must be destroyed after expressions (exprtk requirement) and after curve_wrappers and variable_array.

Definition at line 208 of file Function.cpp.

Referenced by PerThreadData(), and compileExpressions().

◆ symbol_table_cache

ParameterLib::SymbolTableCache MaterialPropertyLib::PerThreadData::symbol_table_cache

Cached pointers to symbol table variables (t, x, y, z).

Definition at line 251 of file Function.cpp.

Referenced by PerThreadData(), and evaluate().

◆ value_expressions

std::vector<exprtk::expression<double> > MaterialPropertyLib::PerThreadData::value_expressions

Value expressions evaluating the property value. Multi-component expressions corresponding one-to-one with the input XML entries, e.g.:

<value>
<expression>f1</expression>
<expression>f2</expression>
</value>

Definition at line 219 of file Function.cpp.

Referenced by compileExpressions().

◆ variable_array

VariableArray MaterialPropertyLib::PerThreadData::variable_array

Storage for vectorial variables (KelvinVector / DeformationGradient). Must be declared before symbol_table so it is initialized first (symbol_table stores pointers into variable_array).

Definition at line 203 of file Function.cpp.

Referenced by PerThreadData(), buildCopyOps(), and updateNonScalarVariables().


The documentation for this struct was generated from the following file: