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.
expression_utils.h
Go to the documentation of this file.
1 // | / |
2 // ' / __| _` | __| _ \ __|
3 // . \ | ( | | ( |\__ `
4 // _|\_\_| \__,_|\__|\___/ ____/
5 // Multi-Physics
6 //
7 // License: BSD License
8 // license: OptimizationApplication/license.txt
9 //
10 // Main author: Suneth Warnakulasuriya
11 // Máté Kelemen
12 //
13 
14 #pragma once
15 
16 // System includes
17 #include <type_traits>
18 #include <iterator>
19 
20 // Project includes
21 #include "includes/define.h"
24 #include "expression/expression.h"
27 
28 namespace Kratos
29 {
30 
33 
34 class KRATOS_API(KRATOS_CORE) ExpressionUtils
35 {
36 public:
39 
40  using IndexType = std::size_t;
41 
45 
60  static Expression::ConstPointer Collapse(const Expression::ConstPointer& rpExpression);
61 
89  static Expression::ConstPointer Slice(
90  const Expression::ConstPointer& rpExpression,
91  std::size_t Offset,
92  std::size_t Stride);
93 
116  template <class TIterator>
118  const Expression::ConstPointer& rpExpression,
119  TIterator NewShapeBegin,
120  TIterator NewShapeEnd)
121  {
122  static_assert(std::is_same_v<typename std::iterator_traits<TIterator>::value_type,std::size_t>);
123  return UnaryReshapeExpression::Create(rpExpression, NewShapeBegin, NewShapeEnd);
124  }
125 
147  static Expression::ConstPointer Reshape(
148  const Expression::ConstPointer& rpExpression,
149  const std::vector<IndexType>& rNewShape);
150 
177  static Expression::ConstPointer Comb(const std::vector<Expression::ConstPointer>& rpExpressions);
178 
179 
207  template <class TIterator>
208  static Expression::ConstPointer Comb(TIterator ExpressionBegin, TIterator ExpressionEnd)
209  {
210  static_assert(std::is_same_v<typename std::iterator_traits<TIterator>::value_type,Expression::ConstPointer>
211  || std::is_same_v<typename std::iterator_traits<TIterator>::value_type,Expression::Pointer>);
212  return UnaryCombineExpression::Create(ExpressionBegin, ExpressionEnd);
213  }
214 
227  static Expression::ConstPointer Abs(const Expression::ConstPointer& rpExpression);
228 
242  const Expression::ConstPointer& rpExpression,
243  const double Power);
244 
267  const Expression::ConstPointer& rpExpression,
268  const Expression::ConstPointer& rpPowerpExpression);
269 
282  static Expression::ConstPointer Scale(
283  const Expression::ConstPointer& rpExpression,
284  const double Scale);
285 
307  static Expression::ConstPointer Scale(
308  const Expression::ConstPointer& rpExpression,
309  const Expression::ConstPointer& rpScaleExpression);
310 
328  static Expression::ConstPointer EntityMin(const Expression::ConstPointer& rpExpression);
329 
347  static Expression::ConstPointer EntityMax(const Expression::ConstPointer& rpExpression);
348 
366  static Expression::ConstPointer EntitySum(const Expression::ConstPointer& rpExpression);
367 
371 
388  static double Sum(
389  const Expression::ConstPointer& rpExpression,
390  const DataCommunicator& rDataCommunicator);
391 
408  static double NormInf(
409  const Expression::ConstPointer& rpExpression,
410  const DataCommunicator& rDataCommunicator);
411 
428  static double NormL2(
429  const Expression::ConstPointer& rpExpression,
430  const DataCommunicator& rDataCommunicator);
431 
449  static double NormP(
450  const Expression::ConstPointer& rpExpression,
451  const double P,
452  const DataCommunicator& rDataCommunicator);
453 
475  static double InnerProduct(
476  const Expression::ConstPointer& rpExpression1,
477  const Expression::ConstPointer& rpExpression2,
478  const DataCommunicator& rDataCommunicator);
479 
483 
484  #ifndef KRATOS_EXPRESSION_UTILS_CEXP_METHOD_1
485  #define KRATOS_EXPRESSION_UTILS_CEXP_METHOD_1(METHOD_NAME) \
486  \
492  template <class TContainerType> \
493  static ContainerExpression<TContainerType> METHOD_NAME( \
494  const ContainerExpression<TContainerType>& rContainerExpression) \
495  { \
496  auto copy = rContainerExpression; \
497  copy.SetExpression(METHOD_NAME(rContainerExpression.pGetExpression())); \
498  return copy; \
499  }
500  #endif
501 
502  #ifndef KRATOS_EXPRESSION_UTILS_CEXP_METHOD_2
503  #define KRATOS_EXPRESSION_UTILS_CEXP_METHOD_2(METHOD_NAME) \
504  \
510  template <class TContainerType> \
511  static double METHOD_NAME(const ContainerExpression<TContainerType>& rContainerExpression) \
512  { \
513  return METHOD_NAME( \
514  rContainerExpression.pGetExpression(), \
515  rContainerExpression.GetModelPart().GetCommunicator().GetDataCommunicator()); \
516  }
517  #endif
518 
519  #ifndef KRATOS_EXPRESSION_UTILS_CEXP_METHOD_3
520  #define KRATOS_EXPRESSION_UTILS_CEXP_METHOD_3(METHOD_NAME) \
521  \
527  template <class TContainerType> \
528  static ContainerExpression<TContainerType> METHOD_NAME( \
529  const ContainerExpression<TContainerType>& rContainerExpression, const double Value) \
530  { \
531  auto copy = rContainerExpression; \
532  copy.SetExpression(METHOD_NAME(rContainerExpression.pGetExpression(), Value)); \
533  return copy; \
534  } \
535  \
542  template <class TContainerType> \
543  static ContainerExpression<TContainerType> METHOD_NAME( \
544  const ContainerExpression<TContainerType>& rContainerExpression1, \
545  const ContainerExpression<TContainerType>& rContainerExpression2) \
546  { \
547  auto copy = rContainerExpression1; \
548  copy.SetExpression(METHOD_NAME(rContainerExpression1.pGetExpression(), \
549  rContainerExpression2.pGetExpression())); \
550  return copy; \
551  }
552  #endif
553 
564 
565  #undef KRATOS_EXPRESSION_UTILS_CEXP_METHOD_1
566  #undef KRATOS_EXPRESSION_UTILS_CEXP_METHOD_2
567  #undef KRATOS_EXPRESSION_UTILS_CEXP_METHOD_3
568 
578  template<class TContainerType>
580  const ContainerExpression<TContainerType>& rContainerExpression,
581  std::size_t Offset,
582  std::size_t Stride)
583  {
584  auto copy = rContainerExpression;
585  copy.SetExpression(Slice(rContainerExpression.pGetExpression(), Offset, Stride));
586  return copy;
587  }
588 
599  template <class TContainerType, class TIterator>
601  const ContainerExpression<TContainerType>& rContainerExpression,
602  TIterator NewShapeBegin,
603  TIterator NewShapeEnd)
604  {
605  auto copy = rContainerExpression;
606  copy.SetExpression(Reshape(rContainerExpression.pGetExpression(), NewShapeBegin, NewShapeEnd));
607  return copy;
608  }
609 
618  template<class TContainerType>
619  static ContainerExpression<TContainerType> Reshape(
620  const ContainerExpression<TContainerType>& rContainerExpression,
621  const std::vector<IndexType>& rNewShape)
622  {
623  return Reshape(rContainerExpression, rNewShape.begin(), rNewShape.end());
624  }
625 
633  template <class TContainerType>
634  static ContainerExpression<TContainerType> Comb(const std::vector<typename ContainerExpression<TContainerType>::Pointer>& rpContainerExpressions)
635  {
636  std::vector<Expression::ConstPointer> exps;
637  std::transform(rpContainerExpressions.begin(), rpContainerExpressions.end(), std::back_inserter(exps), [](const auto& V) { return V->pGetExpression(); });
638  KRATOS_ERROR_IF(rpContainerExpressions.empty())
639  << "Empty container list provided for combing.";
640  auto copy = *rpContainerExpressions.front();
641  copy.SetExpression(UnaryCombineExpression::Create(exps.begin(), exps.end()));
642  return copy;
643  }
644 
653  template <class TContainerType>
654  static double NormP(
655  const ContainerExpression<TContainerType>& rContainerExpression,
656  const double P)
657  {
658  return NormP(rContainerExpression.pGetExpression(), P, rContainerExpression.GetModelPart().GetCommunicator().GetDataCommunicator());
659  }
660 
669  template<class TContainerType>
670  static double InnerProduct(
671  const ContainerExpression<TContainerType>& rContainerExpression1,
672  const ContainerExpression<TContainerType>& rContainerExpression2)
673  {
674  return InnerProduct(rContainerExpression1.pGetExpression(), rContainerExpression2.pGetExpression(), rContainerExpression1.GetModelPart().GetCommunicator().GetDataCommunicator());
675  }
676 
678 };
679 
681 }
virtual const DataCommunicator & GetDataCommunicator() const
Definition: communicator.cpp:340
Container variable data holder.
Definition: container_expression.h:80
ModelPart & GetModelPart()
Get the Model Part used in the container data.
Definition: container_expression.cpp:209
Expression::ConstPointer pGetExpression() const
Get the expression pointer.
Definition: container_expression.cpp:185
void SetExpression(Expression::ConstPointer pExpression)
Set the Expression of the container data.
Definition: container_expression.cpp:158
Serial (do-nothing) version of a wrapper class for MPI communication.
Definition: data_communicator.h:318
Kratos::intrusive_ptr< Expression > Pointer
Definition: expression.h:44
Kratos::intrusive_ptr< const Expression > ConstPointer
Definition: expression.h:46
Definition: expression_utils.h:35
std::size_t IndexType
Definition: expression_utils.h:40
static Expression::ConstPointer Comb(TIterator ExpressionBegin, TIterator ExpressionEnd)
Append the components of a set of expressions to the current expression's components.
Definition: expression_utils.h:208
static Expression::ConstPointer Reshape(const Expression::ConstPointer &rpExpression, TIterator NewShapeBegin, TIterator NewShapeEnd)
Construct an expression with identical data but interpreted with a new item shape.
Definition: expression_utils.h:117
Communicator & GetCommunicator()
Definition: model_part.h:1821
static Expression::Pointer Create(TIteratorType Begin, TIteratorType End)
Definition: unary_combine_expression.h:84
static Expression::Pointer Create(Expression::ConstPointer pExpression, TIteratorType Begin, TIteratorType End)
Definition: unary_reshape_expression.h:64
#define KRATOS_EXPRESSION_UTILS_CEXP_METHOD_3(METHOD_NAME)
Definition: expression_utils.h:510
#define KRATOS_EXPRESSION_UTILS_CEXP_METHOD_2(METHOD_NAME)
Definition: expression_utils.h:498
#define KRATOS_EXPRESSION_UTILS_CEXP_METHOD_1(METHOD_NAME)
Definition: expression_utils.h:485
#define KRATOS_ERROR_IF(conditional)
Definition: exception.h:162
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
V
Definition: generate_droplet_dynamics.py:256
Definition: quadrature.h:29