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.
sensitivity_builder_scheme.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 #if !defined(KRATOS_SENSITIVITY_BUILDER_SCHEME_H_INCLUDED)
14 #define KRATOS_SENSITIVITY_BUILDER_SCHEME_H_INCLUDED
15 
16 // System includes
17 
18 // External includes
19 
20 // Project includes
22 #include "includes/define.h"
23 #include "includes/model_part.h"
27 #include "utilities/openmp_utils.h"
29 
30 namespace Kratos
31 {
34 
53 {
54 public:
57 
59 
61 
63 
65 
69 
73  {
74  // Allocate auxiliary memory.
75  // This needs to be done in the constructor because, this scheme
76  // is used to calculate sensitivities w.r.t. element quantities
77  // and in there we don't usually pass the model part. Hence, we
78  // can not call SensitivityBuilderScheme::Initialize
79  // method.
80  const int number_of_threads = ParallelUtilities::GetNumThreads();
81  mSensitivityMatrices.resize(number_of_threads);
82  mAdjointVectors.resize(number_of_threads);
83  mPartialSensitivity.resize(number_of_threads);
84  }
85 
87  virtual ~SensitivityBuilderScheme() = default;
88 
92 
93  virtual int Check(
94  const ModelPart& rModelPart,
95  const ModelPart& rSensitivityModelPart) const
96  {
97  return 0;
98  }
99 
100  virtual void Initialize(
101  ModelPart& rModelPart,
102  ModelPart& rSensitivityModelPart,
103  AdjointResponseFunction& rResponseFunction)
104  {
105  const auto& r_communicator = rModelPart.GetCommunicator();
106  const auto& r_nodes = rModelPart.Nodes();
107  const int number_of_nodes = r_nodes.size();
108 
109  std::vector<int> indices;
110  indices.resize(number_of_nodes);
111  IndexPartition<int>(number_of_nodes).for_each([&](const int Index) {
112  indices[Index] = (r_nodes.begin() + Index)->Id();
113  });
114 
115  const auto& r_data_communicator = r_communicator.GetDataCommunicator();
117  r_nodes, indices, r_data_communicator);
118  }
119 
121  ModelPart& rModelPart,
122  ModelPart& rSensitivityModelPart,
123  AdjointResponseFunction& rResponseFunction)
124  {}
125 
126  virtual void FinalizeSolutionStep(
127  ModelPart& rModelPart,
128  ModelPart& rSensitivityModelPart,
129  AdjointResponseFunction& rResponseFunction)
130  {}
131 
132  virtual void Finalize(
133  ModelPart& rModelPart,
134  ModelPart& rSensitivityModelPart,
135  AdjointResponseFunction& rResponseFunction)
136  {}
137 
138  virtual void Update(
139  ModelPart& rModelPart,
140  ModelPart& rSensitivityModelPart,
141  AdjointResponseFunction& rResponseFunction)
142  {}
143 
144  virtual void Clear()
145  {
146  mSensitivityMatrices.clear();
147  mAdjointVectors.clear();
148  mPartialSensitivity.clear();
149  mGlobalPointerNodalMap.clear();
150  }
151 
169  virtual void CalculateSensitivity(
170  ElementType& rCurrentElement,
171  AdjointResponseFunction& rResponseFunction,
172  Vector& rSensitivity,
173  GlobalPointersVector<NodeType>& rGPSensitivityVector,
174  const Variable<double>& rVariable,
175  const ProcessInfo& rCurrentProcessInfo)
176  {
177  CalculateLocalSensitivityAndGlobalPointersVector(
178  rCurrentElement, rResponseFunction, rSensitivity,
179  rGPSensitivityVector, rVariable, rCurrentProcessInfo);
180  }
181 
199  virtual void CalculateSensitivity(
200  ElementType& rCurrentElement,
201  AdjointResponseFunction& rResponseFunction,
202  Vector& rSensitivity,
203  GlobalPointersVector<ConditionType>& rGPSensitivityVector,
204  const Variable<double>& rVariable,
205  const ProcessInfo& rCurrentProcessInfo)
206  {}
207 
225  virtual void CalculateSensitivity(
226  ElementType& rCurrentElement,
227  AdjointResponseFunction& rResponseFunction,
228  Vector& rSensitivity,
229  GlobalPointersVector<ElementType>& rGPSensitivityVector,
230  const Variable<double>& rVariable,
231  const ProcessInfo& rCurrentProcessInfo)
232  {
233  CalculateLocalSensitivityAndGlobalPointersVector(
234  rCurrentElement, rResponseFunction, rSensitivity,
235  rGPSensitivityVector, rVariable, rCurrentProcessInfo);
236  }
237 
255  virtual void CalculateSensitivity(
256  ConditionType& rCurrentCondition,
257  AdjointResponseFunction& rResponseFunction,
258  Vector& rSensitivity,
259  GlobalPointersVector<NodeType>& rGPSensitivityVector,
260  const Variable<double>& rVariable,
261  const ProcessInfo& rCurrentProcessInfo)
262  {
263  CalculateLocalSensitivityAndGlobalPointersVector(
264  rCurrentCondition, rResponseFunction, rSensitivity,
265  rGPSensitivityVector, rVariable, rCurrentProcessInfo);
266  }
267 
285  virtual void CalculateSensitivity(
286  ConditionType& rCurrentCondition,
287  AdjointResponseFunction& rResponseFunction,
288  Vector& rSensitivity,
289  GlobalPointersVector<ConditionType>& rGPSensitivityVector,
290  const Variable<double>& rVariable,
291  const ProcessInfo& rCurrentProcessInfo)
292  {
293  CalculateLocalSensitivityAndGlobalPointersVector(
294  rCurrentCondition, rResponseFunction, rSensitivity,
295  rGPSensitivityVector, rVariable, rCurrentProcessInfo);
296  }
297 
315  virtual void CalculateSensitivity(
316  ConditionType& rCurrentCondition,
317  AdjointResponseFunction& rResponseFunction,
318  Vector& rSensitivity,
319  GlobalPointersVector<ElementType>& rGPSensitivityVector,
320  const Variable<double>& rVariable,
321  const ProcessInfo& rCurrentProcessInfo)
322  {}
323 
341  virtual void CalculateSensitivity(
342  ElementType& rCurrentElement,
343  AdjointResponseFunction& rResponseFunction,
344  Vector& rSensitivity,
345  GlobalPointersVector<NodeType>& rGPSensitivityVector,
346  const Variable<array_1d<double, 3>>& rVariable,
347  const ProcessInfo& rCurrentProcessInfo)
348  {
349  CalculateLocalSensitivityAndGlobalPointersVector(
350  rCurrentElement, rResponseFunction, rSensitivity,
351  rGPSensitivityVector, rVariable, rCurrentProcessInfo);
352  }
353 
371  virtual void CalculateSensitivity(
372  ElementType& rCurrentElement,
373  AdjointResponseFunction& rResponseFunction,
374  Vector& rSensitivity,
375  GlobalPointersVector<ConditionType>& rGPSensitivityVector,
376  const Variable<array_1d<double, 3>>& rVariable,
377  const ProcessInfo& rCurrentProcessInfo)
378  {}
379 
397  virtual void CalculateSensitivity(
398  ElementType& rCurrentElement,
399  AdjointResponseFunction& rResponseFunction,
400  Vector& rSensitivity,
401  GlobalPointersVector<ElementType>& rGPSensitivityVector,
402  const Variable<array_1d<double, 3>>& rVariable,
403  const ProcessInfo& rCurrentProcessInfo)
404  {
405  CalculateLocalSensitivityAndGlobalPointersVector(
406  rCurrentElement, rResponseFunction, rSensitivity,
407  rGPSensitivityVector, rVariable, rCurrentProcessInfo);
408  }
409 
427  virtual void CalculateSensitivity(
428  ConditionType& rCurrentCondition,
429  AdjointResponseFunction& rResponseFunction,
430  Vector& rSensitivity,
431  GlobalPointersVector<NodeType>& rGPSensitivityVector,
432  const Variable<array_1d<double, 3>>& rVariable,
433  const ProcessInfo& rCurrentProcessInfo)
434  {
435  CalculateLocalSensitivityAndGlobalPointersVector(
436  rCurrentCondition, rResponseFunction, rSensitivity,
437  rGPSensitivityVector, rVariable, rCurrentProcessInfo);
438  }
439 
457  virtual void CalculateSensitivity(
458  ConditionType& rCurrentCondition,
459  AdjointResponseFunction& rResponseFunction,
460  Vector& rSensitivity,
461  GlobalPointersVector<ConditionType>& rGPSensitivityVector,
462  const Variable<array_1d<double, 3>>& rVariable,
463  const ProcessInfo& rCurrentProcessInfo)
464  {
465  CalculateLocalSensitivityAndGlobalPointersVector(
466  rCurrentCondition, rResponseFunction, rSensitivity,
467  rGPSensitivityVector, rVariable, rCurrentProcessInfo);
468  }
469 
487  virtual void CalculateSensitivity(
488  ConditionType& rCurrentCondition,
489  AdjointResponseFunction& rResponseFunction,
490  Vector& rSensitivity,
491  GlobalPointersVector<ElementType>& rGPSensitivityVector,
492  const Variable<array_1d<double, 3>>& rVariable,
493  const ProcessInfo& rCurrentProcessInfo)
494  {}
495 
499 
501  virtual std::string Info() const
502  {
503  return "SensitivityBuilderScheme";
504  }
505 
507  virtual void PrintInfo(std::ostream& rOStream) const
508  {
509  rOStream << Info();
510  }
511 
513  virtual void PrintData(std::ostream& rOStream) const
514  {
515  rOStream << Info();
516  }
517 
519 
520 protected:
523 
524  std::vector<Matrix> mSensitivityMatrices;
525  std::vector<Vector> mAdjointVectors;
526  std::vector<Vector> mPartialSensitivity;
527 
528  std::unordered_map<int, GlobalPointer<ModelPart::NodeType>> mGlobalPointerNodalMap;
529  const int mRank;
530 
532 private:
535 
536  template <typename TEntityType, typename TDerivativeEntityType, typename TDataType>
537  void CalculateLocalSensitivityAndGlobalPointersVector(
538  TEntityType& rEntity,
539  AdjointResponseFunction& rResponseFunction,
540  Vector& rSensitivityVector,
541  GlobalPointersVector<TDerivativeEntityType>& rGPSensitivityVector,
542  const Variable<TDataType>& rVariable,
543  const ProcessInfo& rProcessInfo)
544  {
545  KRATOS_TRY;
546 
547  this->CalculateLocalSensitivity(rEntity, rResponseFunction,
548  rSensitivityVector, rVariable, rProcessInfo);
549 
550  if (rGPSensitivityVector.size() != 1) {
551  rGPSensitivityVector.resize(1);
552  }
553 
554  rGPSensitivityVector(0) = GlobalPointer<TDerivativeEntityType>(&rEntity, mRank);
555 
556  KRATOS_CATCH("");
557  }
558 
559  template <typename TEntityType, typename TDataType>
560  void CalculateLocalSensitivityAndGlobalPointersVector(
561  TEntityType& rEntity,
562  AdjointResponseFunction& rResponseFunction,
563  Vector& rSensitivityVector,
564  GlobalPointersVector<NodeType>& rGPSensitivityVector,
565  const Variable<TDataType>& rVariable,
566  const ProcessInfo& rProcessInfo)
567  {
568  KRATOS_TRY;
569 
570  this->CalculateLocalSensitivity(rEntity, rResponseFunction,
571  rSensitivityVector, rVariable, rProcessInfo);
572 
573  auto& r_geometry = rEntity.GetGeometry();
574  if (rGPSensitivityVector.size() != r_geometry.PointsNumber()) {
575  rGPSensitivityVector.resize(r_geometry.PointsNumber());
576  }
577 
578  for (unsigned int i = 0; i < r_geometry.PointsNumber(); ++i) {
579  rGPSensitivityVector(i) = mGlobalPointerNodalMap[r_geometry[i].Id()];
580  }
581 
582  KRATOS_CATCH("");
583  }
584 
585  template <class TEntityType, class TDataType>
586  void CalculateLocalSensitivity(
587  TEntityType& rCurrentEntity,
588  AdjointResponseFunction& rResponseFunction,
589  Vector& rSensitivity,
590  const Variable<TDataType>& rVariable,
591  const ProcessInfo& rCurrentProcessInfo)
592  {
593  KRATOS_TRY;
594 
595  const auto k = OpenMPUtils::ThisThread();
596 
597  rCurrentEntity.CalculateSensitivityMatrix(rVariable, mSensitivityMatrices[k], rCurrentProcessInfo);
598  rCurrentEntity.GetValuesVector(mAdjointVectors[k]);
599 
601  << "mAdjointVectors.size(): " << mAdjointVectors[k].size()
602  << " incompatible with mSensitivityMatrices[k].size1(): "
603  << mSensitivityMatrices[k].size2() << ". Variable: " << rVariable << std::endl;
604 
605  rResponseFunction.CalculatePartialSensitivity(
606  rCurrentEntity, rVariable, mSensitivityMatrices[k], mPartialSensitivity[k], rCurrentProcessInfo);
607 
609  << "mPartialSensitivity.size(): " << mPartialSensitivity[k].size()
610  << " incompatible with mSensitivityMatrices.size1(): "
611  << mSensitivityMatrices[k].size1() << ". Variable: " << rVariable << std::endl;
612 
613  if (rSensitivity.size() != mSensitivityMatrices[k].size1()) {
614  rSensitivity.resize(mSensitivityMatrices[k].size1(), false);
615  }
616 
618 
619  KRATOS_CATCH("");
620  }
621 
623 
624 }; /* Class SensitivityBuilderScheme */
625 
627 
628 } /* namespace Kratos.*/
629 
630 #endif /* KRATOS_SENSITIVITY_BUILDER_SCHEME_H_INCLUDED defined */
A base class for adjoint response functions.
Definition: adjoint_response_function.h:39
Base class for all Conditions.
Definition: condition.h:59
Base class for all Elements.
Definition: element.h:60
This class is a wrapper for a pointer to a data that is located in a different rank.
Definition: global_pointer.h:44
static std::unordered_map< int, GlobalPointer< typename TContainerType::value_type > > RetrieveGlobalIndexedPointersMap(const TContainerType &rContainer, const std::vector< int > &rIdList, const DataCommunicator &rDataCommunicator)
Retrieves a map of global pointers corresponding to the given entity ids, where the global pointers p...
Definition: global_pointer_utilities.h:95
This class is a vector which stores global pointers.
Definition: global_pointers_vector.h:61
size_type size() const
Definition: global_pointers_vector.h:307
void resize(size_type new_dim) const
Definition: global_pointers_vector.h:366
This class is useful for index iteration over containers.
Definition: parallel_utilities.h:451
void for_each(TUnaryFunction &&f)
Definition: parallel_utilities.h:514
This class aims to manage meshes for multi-physics simulations.
Definition: model_part.h:77
Element ElementType
Definition: model_part.h:120
Communicator & GetCommunicator()
Definition: model_part.h:1821
Node NodeType
Definition: model_part.h:117
NodesContainerType & Nodes(IndexType ThisIndex=0)
Definition: model_part.h:507
Condition ConditionType
Definition: model_part.h:121
This class defines the node.
Definition: node.h:65
static int ThisThread()
Wrapper for omp_get_thread_num().
Definition: openmp_utils.h:108
Holder for general data related to MPI (or suitable serial equivalents for non-MPI runs).
Definition: parallel_environment.h:57
static int GetNumThreads()
Returns the current number of threads.
Definition: parallel_utilities.cpp:34
ProcessInfo holds the current value of different solution parameters.
Definition: process_info.h:59
Scheme used in the Sensitivity Builder.
Definition: sensitivity_builder_scheme.h:53
virtual void FinalizeSolutionStep(ModelPart &rModelPart, ModelPart &rSensitivityModelPart, AdjointResponseFunction &rResponseFunction)
Definition: sensitivity_builder_scheme.h:126
virtual void CalculateSensitivity(ConditionType &rCurrentCondition, AdjointResponseFunction &rResponseFunction, Vector &rSensitivity, GlobalPointersVector< ConditionType > &rGPSensitivityVector, const Variable< array_1d< double, 3 >> &rVariable, const ProcessInfo &rCurrentProcessInfo)
Calculates sensitivity from a given condition.
Definition: sensitivity_builder_scheme.h:457
std::vector< Vector > mPartialSensitivity
Definition: sensitivity_builder_scheme.h:526
virtual void Update(ModelPart &rModelPart, ModelPart &rSensitivityModelPart, AdjointResponseFunction &rResponseFunction)
Definition: sensitivity_builder_scheme.h:138
virtual void CalculateSensitivity(ElementType &rCurrentElement, AdjointResponseFunction &rResponseFunction, Vector &rSensitivity, GlobalPointersVector< ConditionType > &rGPSensitivityVector, const Variable< array_1d< double, 3 >> &rVariable, const ProcessInfo &rCurrentProcessInfo)
Calculates sensitivity from a given element.
Definition: sensitivity_builder_scheme.h:371
virtual void PrintData(std::ostream &rOStream) const
Print object's data.
Definition: sensitivity_builder_scheme.h:513
virtual std::string Info() const
Turn back information as a string.
Definition: sensitivity_builder_scheme.h:501
virtual void CalculateSensitivity(ConditionType &rCurrentCondition, AdjointResponseFunction &rResponseFunction, Vector &rSensitivity, GlobalPointersVector< ElementType > &rGPSensitivityVector, const Variable< array_1d< double, 3 >> &rVariable, const ProcessInfo &rCurrentProcessInfo)
Calculates sensitivity from a given condition.
Definition: sensitivity_builder_scheme.h:487
KRATOS_CLASS_POINTER_DEFINITION(SensitivityBuilderScheme)
virtual void CalculateSensitivity(ElementType &rCurrentElement, AdjointResponseFunction &rResponseFunction, Vector &rSensitivity, GlobalPointersVector< NodeType > &rGPSensitivityVector, const Variable< double > &rVariable, const ProcessInfo &rCurrentProcessInfo)
Calculates sensitivity from a given element.
Definition: sensitivity_builder_scheme.h:169
virtual void Finalize(ModelPart &rModelPart, ModelPart &rSensitivityModelPart, AdjointResponseFunction &rResponseFunction)
Definition: sensitivity_builder_scheme.h:132
virtual void CalculateSensitivity(ElementType &rCurrentElement, AdjointResponseFunction &rResponseFunction, Vector &rSensitivity, GlobalPointersVector< NodeType > &rGPSensitivityVector, const Variable< array_1d< double, 3 >> &rVariable, const ProcessInfo &rCurrentProcessInfo)
Calculates sensitivity from a given element.
Definition: sensitivity_builder_scheme.h:341
virtual void Clear()
Definition: sensitivity_builder_scheme.h:144
std::vector< Vector > mAdjointVectors
Definition: sensitivity_builder_scheme.h:525
virtual void CalculateSensitivity(ElementType &rCurrentElement, AdjointResponseFunction &rResponseFunction, Vector &rSensitivity, GlobalPointersVector< ElementType > &rGPSensitivityVector, const Variable< array_1d< double, 3 >> &rVariable, const ProcessInfo &rCurrentProcessInfo)
Calculates sensitivity from a given element.
Definition: sensitivity_builder_scheme.h:397
virtual void CalculateSensitivity(ConditionType &rCurrentCondition, AdjointResponseFunction &rResponseFunction, Vector &rSensitivity, GlobalPointersVector< NodeType > &rGPSensitivityVector, const Variable< array_1d< double, 3 >> &rVariable, const ProcessInfo &rCurrentProcessInfo)
Calculates sensitivity from a given condition.
Definition: sensitivity_builder_scheme.h:427
virtual ~SensitivityBuilderScheme()=default
Destructor.
virtual void CalculateSensitivity(ConditionType &rCurrentCondition, AdjointResponseFunction &rResponseFunction, Vector &rSensitivity, GlobalPointersVector< ElementType > &rGPSensitivityVector, const Variable< double > &rVariable, const ProcessInfo &rCurrentProcessInfo)
Calculates sensitivity from a given condition.
Definition: sensitivity_builder_scheme.h:315
virtual void CalculateSensitivity(ElementType &rCurrentElement, AdjointResponseFunction &rResponseFunction, Vector &rSensitivity, GlobalPointersVector< ConditionType > &rGPSensitivityVector, const Variable< double > &rVariable, const ProcessInfo &rCurrentProcessInfo)
Calculates sensitivity from a given element.
Definition: sensitivity_builder_scheme.h:199
const int mRank
Definition: sensitivity_builder_scheme.h:529
virtual void Initialize(ModelPart &rModelPart, ModelPart &rSensitivityModelPart, AdjointResponseFunction &rResponseFunction)
Definition: sensitivity_builder_scheme.h:100
virtual void InitializeSolutionStep(ModelPart &rModelPart, ModelPart &rSensitivityModelPart, AdjointResponseFunction &rResponseFunction)
Definition: sensitivity_builder_scheme.h:120
std::vector< Matrix > mSensitivityMatrices
Definition: sensitivity_builder_scheme.h:524
virtual void CalculateSensitivity(ConditionType &rCurrentCondition, AdjointResponseFunction &rResponseFunction, Vector &rSensitivity, GlobalPointersVector< ConditionType > &rGPSensitivityVector, const Variable< double > &rVariable, const ProcessInfo &rCurrentProcessInfo)
Calculates sensitivity from a given condition.
Definition: sensitivity_builder_scheme.h:285
virtual void CalculateSensitivity(ElementType &rCurrentElement, AdjointResponseFunction &rResponseFunction, Vector &rSensitivity, GlobalPointersVector< ElementType > &rGPSensitivityVector, const Variable< double > &rVariable, const ProcessInfo &rCurrentProcessInfo)
Calculates sensitivity from a given element.
Definition: sensitivity_builder_scheme.h:225
virtual void CalculateSensitivity(ConditionType &rCurrentCondition, AdjointResponseFunction &rResponseFunction, Vector &rSensitivity, GlobalPointersVector< NodeType > &rGPSensitivityVector, const Variable< double > &rVariable, const ProcessInfo &rCurrentProcessInfo)
Calculates sensitivity from a given condition.
Definition: sensitivity_builder_scheme.h:255
virtual int Check(const ModelPart &rModelPart, const ModelPart &rSensitivityModelPart) const
Definition: sensitivity_builder_scheme.h:93
std::unordered_map< int, GlobalPointer< ModelPart::NodeType > > mGlobalPointerNodalMap
Definition: sensitivity_builder_scheme.h:528
virtual void PrintInfo(std::ostream &rOStream) const
Print information about this object.
Definition: sensitivity_builder_scheme.h:507
SensitivityBuilderScheme()
Constructor.
Definition: sensitivity_builder_scheme.h:71
#define KRATOS_CATCH(MoreInfo)
Definition: define.h:110
#define KRATOS_TRY
Definition: define.h:109
#define KRATOS_ERROR_IF(conditional)
Definition: exception.h:162
DataCommunicator & GetDefaultDataCommunicator()
Definition: testing.cpp:24
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
Internals::Matrix< double, AMatrix::dynamic, 1 > Vector
Definition: amatrix_interface.h:472
AMatrix::MatrixProductExpression< TExpression1Type, TExpression2Type > prod(AMatrix::MatrixExpression< TExpression1Type, TCategory1 > const &First, AMatrix::MatrixExpression< TExpression2Type, TCategory2 > const &Second)
Definition: amatrix_interface.h:568
T & noalias(T &TheMatrix)
Definition: amatrix_interface.h:484
def Index()
Definition: hdf5_io_tools.py:38
int k
Definition: quadrature.py:595
integer i
Definition: TensorModule.f:17