KratosMultiphysics
KRATOS Multiphysics (Kratos) is a framework for building parallel, multi-disciplinary simulation software, aiming at modularity, extensibility, and high performance. Kratos is written in C++, and counts with an extensive Python interface.
unary_combine_expression.h
Go to the documentation of this file.
1 // | / |
2 // ' / __| _` | __| _ \ __|
3 // . \ | ( | | ( |\__ `
4 // _|\_\_| \__,_|\__|\___/ ____/
5 // Multi-Physics
6 //
7 // License: BSD License
8 // Kratos default license: kratos/license.txt
9 //
10 // Main authors: Suneth Warnakulasuriya
11 //
12 
13 #pragma once
14 
15 // System includes
16 #include <string>
17 #include <vector>
18 
19 // Project includes
20 #include "expression/expression.h"
21 
22 namespace Kratos {
23 
26 
37 public:
40 
41  using IndexType = std::size_t;
42 
46 
47  template<class TIteratorType>
49  TIteratorType Begin,
50  TIteratorType End)
51  : Expression(Begin != End ? (*Begin)->NumberOfEntities() : 0),
52  mSourceExpressions(Begin, End)
53 
54  {
55  mStrides.resize(mSourceExpressions.size());
56 
57  // every expression should have same number of entities
58  IndexType local_stride = 0;
59  for (IndexType i = 0; i < mSourceExpressions.size(); ++i) {
60  const auto& p_expression = mSourceExpressions[i];
61 
62  KRATOS_ERROR_IF_NOT(p_expression->NumberOfEntities() == this->NumberOfEntities())
63  << "Expression number of entities mismatch. [ required number of entities = "
64  << NumberOfEntities() << ", found number of entities = "
65  << p_expression->NumberOfEntities() << " ].\n"
66  << "Expressions:\n"
67  << "Reference = " << *mSourceExpressions[0] << "\n"
68  << "Current = " << p_expression << "\n";
69 
70  local_stride += p_expression->GetItemComponentCount();
71  mStrides[i] = local_stride;
72  }
73 
74  // Corner case: empty expression range provided
76  << "No expressions were given.\n";
77  }
78 
82 
83  template<class TIteratorType>
85  TIteratorType Begin,
86  TIteratorType End)
87  {
88  return Kratos::make_intrusive<UnaryCombineExpression>(Begin, End);
89  }
90 
91  double Evaluate(
92  const IndexType EntityIndex,
93  const IndexType EntityDataBeginIndex,
94  const IndexType ComponentIndex) const override
95  {
96  IndexType current_starting = 0;
97 
98  for (IndexType i = 0; i < mSourceExpressions.size(); ++i) {
99  const IndexType current_stride = mStrides[i];
100  if (ComponentIndex < current_stride) {
101  return mSourceExpressions[i]->Evaluate(EntityIndex, EntityIndex * (current_stride - current_starting), ComponentIndex - current_starting);
102  }
103  current_starting = current_stride;
104  }
105 
106  KRATOS_ERROR << "Component index is greater than the current expressions stride.\n";
107 
108  return 0.0;
109  }
110 
111  const std::vector<IndexType> GetItemShape() const override
112  {
113  return mStrides.size() == 1 && mStrides.back() == 1 ? std::vector<IndexType> {} : std::vector<IndexType> {mStrides.back()};
114  }
115 
116  IndexType GetMaxDepth() const override
117  {
118  IndexType max_depth = 0;
119  for (const auto& p_expression : mSourceExpressions) {
120  max_depth = std::max(max_depth, p_expression->GetMaxDepth());
121  }
122  return max_depth + 1;
123  }
124 
125  std::string Info() const override
126  {
127  std::stringstream msg;
128 
129  msg << "CombinedExpression: ";
130 
131  for (const auto& p_expression : mSourceExpressions) {
132  msg << "\n\t" << *p_expression;
133  }
134 
135  return msg.str();
136  }
137 
139 protected:
142 
143  const std::vector<Expression::ConstPointer> mSourceExpressions;
144 
145  std::vector<IndexType> mStrides;
146 
148 };
149 
150 } // namespace Kratos
Base class or all the expression types.
Definition: expression.h:38
IndexType GetItemComponentCount() const
Get the Local Size of the expression.
Definition: expression.cpp:104
IndexType NumberOfEntities() const
Get the maximum number of entities allowed for this expression.
Definition: expression.h:181
Kratos::intrusive_ptr< Expression > Pointer
Definition: expression.h:44
std::size_t IndexType
Definition: expression.h:48
Use to create an expression which combines given input expressions list in the order given.
Definition: unary_combine_expression.h:36
const std::vector< IndexType > GetItemShape() const override
Get the Shape of the expression.
Definition: unary_combine_expression.h:111
UnaryCombineExpression(TIteratorType Begin, TIteratorType End)
Definition: unary_combine_expression.h:48
static Expression::Pointer Create(TIteratorType Begin, TIteratorType End)
Definition: unary_combine_expression.h:84
std::string Info() const override
Definition: unary_combine_expression.h:125
IndexType GetMaxDepth() const override
Get the Max Depth of the lazy expression tree.
Definition: unary_combine_expression.h:116
std::vector< IndexType > mStrides
Definition: unary_combine_expression.h:145
const std::vector< Expression::ConstPointer > mSourceExpressions
Definition: unary_combine_expression.h:143
double Evaluate(const IndexType EntityIndex, const IndexType EntityDataBeginIndex, const IndexType ComponentIndex) const override
Evalute the expression for the given entity data start index and component index and returns the valu...
Definition: unary_combine_expression.h:91
#define KRATOS_ERROR
Definition: exception.h:161
#define KRATOS_ERROR_IF_NOT(conditional)
Definition: exception.h:163
#define KRATOS_ERROR_IF(conditional)
Definition: exception.h:162
static double max(double a, double b)
Definition: GeometryFunctions.h:79
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
integer i
Definition: TensorModule.f:17