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.
velocity_bossak_adjoint_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_VELOCITY_BOSSAK_ADJOINT_SCHEME_H_INCLUDED)
14 #define KRATOS_VELOCITY_BOSSAK_ADJOINT_SCHEME_H_INCLUDED
15 
16 // System includes
17 #include <vector>
18 #include <string>
19 #include <unordered_set>
20 #include <functional>
21 
22 // External includes
23 
24 // Project includes
25 #include "includes/define.h"
26 #include "includes/checks.h"
35 
36 // Application includes
38 
39 namespace Kratos
40 {
43 
44 template <class TSparseSpace, class TDenseSpace>
45 class VelocityBossakAdjointScheme : public ResidualBasedAdjointBossakScheme<TSparseSpace, TDenseSpace>
46 {
47 public:
50 
52 
54 
56 
58 
60 
61  using BossakConstants = typename BaseType::BossakConstants;
62 
66 
69  Parameters Settings,
70  AdjointResponseFunction::Pointer pResponseFunction,
71  const IndexType Dimension,
72  const IndexType BlockSize)
73  : BaseType(Settings, pResponseFunction),
74  mAdjointSlipUtilities(Dimension, BlockSize)
75  {
76  KRATOS_INFO(this->Info()) << this->Info() << " created [ Dimensionality = " << Dimension << ", BlockSize = " << BlockSize << " ].\n";
77  }
78 
80  ~VelocityBossakAdjointScheme() override = default;
81 
85 
86  void Initialize(ModelPart& rModelPart) override
87  {
88  KRATOS_TRY;
89 
90  BaseType::Initialize(rModelPart);
91 
92  // Allocate auxiliary memory.
94  mAuxiliaryMatrix.resize(num_threads);
95  mRotatedMatrix.resize(num_threads);
96 
97  KRATOS_CATCH("");
98  }
99 
103 
105  std::string Info() const override
106  {
107  return "VelocityBossakAdjointScheme";
108  }
109 
111 
112 protected:
115 
117  Element& rElement,
118  LocalSystemMatrixType& rLHS_Contribution,
119  LocalSystemVectorType& rRHS_Contribution,
120  const ProcessInfo& rCurrentProcessInfo) override
121  {
122  CalculateEntityGradientContributions(
123  rElement, rLHS_Contribution, rRHS_Contribution, rCurrentProcessInfo);
124  }
125 
127  Condition& rCondition,
128  LocalSystemMatrixType& rLHS_Contribution,
129  LocalSystemVectorType& rRHS_Contribution,
130  const ProcessInfo& rCurrentProcessInfo) override
131  {
132  CalculateEntityGradientContributions(
133  rCondition, rLHS_Contribution, rRHS_Contribution, rCurrentProcessInfo);
134  }
135 
137  Element& rElement,
138  LocalSystemMatrixType& rLHS_Contribution,
139  LocalSystemVectorType& rRHS_Contribution,
140  const ProcessInfo& rCurrentProcessInfo) override
141  {
142  CalculateEntityFirstDerivativeContributions(
143  rElement, rLHS_Contribution, rRHS_Contribution, rCurrentProcessInfo);
144  }
145 
147  Condition& rCondition,
148  LocalSystemMatrixType& rLHS_Contribution,
149  LocalSystemVectorType& rRHS_Contribution,
150  const ProcessInfo& rCurrentProcessInfo) override
151  {
152  CalculateEntityFirstDerivativeContributions(
153  rCondition, rLHS_Contribution, rRHS_Contribution, rCurrentProcessInfo);
154  }
155 
157  Element& rElement,
158  LocalSystemMatrixType& rLHS_Contribution,
159  LocalSystemVectorType& rRHS_Contribution,
160  const ProcessInfo& rCurrentProcessInfo) override
161  {
162  CalculateEntitySecondDerivativeContributions(
163  rElement, rLHS_Contribution, rRHS_Contribution, rCurrentProcessInfo);
164  }
165 
167  Condition& rCondition,
168  LocalSystemMatrixType& rLHS_Contribution,
169  LocalSystemVectorType& rRHS_Contribution,
170  const ProcessInfo& rCurrentProcessInfo) override
171  {
172  CalculateEntitySecondDerivativeContributions(
173  rCondition, rLHS_Contribution, rRHS_Contribution, rCurrentProcessInfo);
174  }
175 
177  Element& rElement,
178  LocalSystemVectorType& rAdjointTimeSchemeValues2,
179  LocalSystemVectorType& rAdjointTimeSchemeValues3,
180  AdjointResponseFunction& rAdjointResponseFunction,
181  const BossakConstants& rBossakConstants,
182  const ProcessInfo& rCurrentProcessInfo) override
183  {
184  CalculateEntityTimeSchemeContributions(rElement, rAdjointTimeSchemeValues2,
185  rAdjointTimeSchemeValues3,
186  rCurrentProcessInfo);
187  }
188 
190  Condition& rCondition,
191  LocalSystemVectorType& rAdjointTimeSchemeValues2,
192  LocalSystemVectorType& rAdjointTimeSchemeValues3,
193  AdjointResponseFunction& rAdjointResponseFunction,
194  const BossakConstants& rBossakConstants,
195  const ProcessInfo& rCurrentProcessInfo) override
196  {
197  CalculateEntityTimeSchemeContributions(rCondition, rAdjointTimeSchemeValues2,
198  rAdjointTimeSchemeValues3,
199  rCurrentProcessInfo);
200  }
201 
203  Element& rElement,
204  LocalSystemVectorType& rAdjointAuxiliaryValues,
205  AdjointResponseFunction& rAdjointResponseFunction,
206  const BossakConstants& rBossakConstants,
207  const ProcessInfo& rCurrentProcessInfo) override
208  {
209  CalculateEntityAuxiliaryVariableContributions(
210  rElement, rAdjointAuxiliaryValues, rCurrentProcessInfo);
211  }
212 
214  Condition& rCondition,
215  LocalSystemVectorType& rAdjointAuxiliaryValues,
216  AdjointResponseFunction& rAdjointResponseFunction,
217  const BossakConstants& rBossakConstants,
218  const ProcessInfo& rCurrentProcessInfo) override
219  {
220  CalculateEntityAuxiliaryVariableContributions(
221  rCondition, rAdjointAuxiliaryValues, rCurrentProcessInfo);
222  }
223 
224  void CheckAndResizeThreadStorage(unsigned SystemSize) override
225  {
226  const int k = OpenMPUtils::ThisThread();
227 
229 
230  if (mAuxiliaryMatrix[k].size1() != SystemSize ||
231  mAuxiliaryMatrix[k].size2() != SystemSize) {
232  mAuxiliaryMatrix[k].resize(SystemSize, SystemSize, false);
233  }
234 
235  if (mRotatedMatrix[k].size1() != SystemSize || mRotatedMatrix[k].size2() != SystemSize) {
236  mRotatedMatrix[k].resize(SystemSize, SystemSize, false);
237  }
238  }
239 
241 
242 private:
245 
246  std::vector<Matrix> mAuxiliaryMatrix;
247  std::vector<Matrix> mRotatedMatrix;
248 
249  const FluidAdjointSlipUtilities mAdjointSlipUtilities;
250 
254 
255  template<class TEntityType>
256  void CalculateEntityGradientContributions(
257  TEntityType& rCurrentEntity,
258  LocalSystemMatrixType& rLHS_Contribution,
259  LocalSystemVectorType& rRHS_Contribution,
260  const ProcessInfo& rCurrentProcessInfo)
261  {
262  const int k = OpenMPUtils::ThisThread();
263 
264  auto& aux_matrix = this->mAuxiliaryMatrix[k];
265 
266  rCurrentEntity.CalculateLeftHandSide(aux_matrix, rCurrentProcessInfo);
267  this->mpResponseFunction->CalculateGradient(
268  rCurrentEntity, aux_matrix, rRHS_Contribution, rCurrentProcessInfo);
269 
271  rLHS_Contribution, aux_matrix, rCurrentEntity.GetGeometry());
272 
273  noalias(rRHS_Contribution) = -rRHS_Contribution;
274  }
275 
276  template<class TEntityType>
277  void CalculateEntityFirstDerivativeContributions(
278  TEntityType& rCurrentEntity,
279  LocalSystemMatrixType& rLHS_Contribution,
280  LocalSystemVectorType& rRHS_Contribution,
281  const ProcessInfo& rCurrentProcessInfo)
282  {
283  const int k = OpenMPUtils::ThisThread();
284  auto& aux_matrix = this->mAuxiliaryMatrix[k];
285  auto& rotated_matrix = this->mRotatedMatrix[k];
286  auto& response_first_derivatives = this->mFirstDerivsResponseGradient[k];
287 
288  rCurrentEntity.CalculateFirstDerivativesLHS(aux_matrix, rCurrentProcessInfo);
289  this->mpResponseFunction->CalculateFirstDerivativesGradient(
290  rCurrentEntity, aux_matrix, response_first_derivatives, rCurrentProcessInfo);
291 
293  rotated_matrix, aux_matrix, rCurrentEntity.GetGeometry());
294 
295  noalias(rLHS_Contribution) += this->mBossak.C6 * rotated_matrix;
296  noalias(rRHS_Contribution) -= this->mBossak.C6 * response_first_derivatives;
297  }
298 
299  template<class TEntityType>
300  void CalculateEntitySecondDerivativeContributions(
301  TEntityType& rCurrentEntity,
302  LocalSystemMatrixType& rLHS_Contribution,
303  LocalSystemVectorType& rRHS_Contribution,
304  const ProcessInfo& rCurrentProcessInfo)
305  {
306  const int k = OpenMPUtils::ThisThread();
307  auto& aux_matrix = this->mAuxiliaryMatrix[k];
308  auto& rotated_matrix = this->mRotatedMatrix[k];
309  auto& response_second_derivatives = this->mSecondDerivsResponseGradient[k];
310 
311  rCurrentEntity.CalculateSecondDerivativesLHS(aux_matrix, rCurrentProcessInfo);
312  aux_matrix *= (1.0 - this->mBossak.Alpha);
313  this->mpResponseFunction->CalculateSecondDerivativesGradient(
314  rCurrentEntity, aux_matrix, response_second_derivatives, rCurrentProcessInfo);
315 
317  rotated_matrix, aux_matrix, rCurrentEntity.GetGeometry());
318 
319  noalias(rLHS_Contribution) += this->mBossak.C7 * rotated_matrix;
320  noalias(rRHS_Contribution) -= this->mBossak.C7 * response_second_derivatives;
321  }
322 
323  template<class TEntityType>
324  void CalculateEntityTimeSchemeContributions(
325  TEntityType& rCurrentEntity,
326  LocalSystemVectorType& rAdjointTimeSchemeValues2,
327  LocalSystemVectorType& rAdjointTimeSchemeValues3,
328  const ProcessInfo& rProcessInfo)
329  {
330  KRATOS_TRY
331 
332  const int k = OpenMPUtils::ThisThread();
333  auto& adjoint_values = this->mAdjointValuesVector[k];
334  auto& aux_matrix = this->mAuxiliaryMatrix[k];
335  auto& entity_first_derivatives = this->mFirstDerivsLHS[k];
336  auto& entity_second_derivatives = this->mSecondDerivsLHS[k];
337  auto& response_first_derivatives = this->mFirstDerivsResponseGradient[k];
338  auto& response_second_derivatives = this->mSecondDerivsResponseGradient[k];
339 
340  const auto& r_const_entity_ref = rCurrentEntity;
341  r_const_entity_ref.GetValuesVector(adjoint_values);
342  this->CheckAndResizeThreadStorage(adjoint_values.size());
343 
345  rCurrentEntity.CalculateFirstDerivativesLHS(aux_matrix, rProcessInfo);
346  this->mpResponseFunction->CalculateFirstDerivativesGradient(
347  rCurrentEntity, aux_matrix, response_first_derivatives, rProcessInfo);
348 
350  entity_first_derivatives, aux_matrix, rCurrentEntity.GetGeometry());
351 
352  rCurrentEntity.CalculateSecondDerivativesLHS(aux_matrix, rProcessInfo);
353  aux_matrix *= (1.0 - this->mBossak.Alpha);
354  this->mpResponseFunction->CalculateSecondDerivativesGradient(
355  rCurrentEntity, aux_matrix, response_second_derivatives, rProcessInfo);
356 
358  entity_second_derivatives, aux_matrix, rCurrentEntity.GetGeometry());
359 
360  if (rAdjointTimeSchemeValues2.size() != response_first_derivatives.size()) {
361  rAdjointTimeSchemeValues2.resize(response_first_derivatives.size(), false);
362  }
363 
364  noalias(rAdjointTimeSchemeValues2) =
365  -response_first_derivatives - prod(entity_first_derivatives, adjoint_values);
366 
367  if (rAdjointTimeSchemeValues3.size() != response_second_derivatives.size()) {
368  rAdjointTimeSchemeValues3.resize(response_second_derivatives.size(), false);
369  }
370 
371  noalias(rAdjointTimeSchemeValues3) =
372  -response_second_derivatives - prod(entity_second_derivatives, adjoint_values);
373 
374  KRATOS_CATCH("");
375  }
376 
377  template <class TEntityType>
378  void CalculateEntityAuxiliaryVariableContributions(
379  TEntityType& rCurrentEntity,
380  LocalSystemVectorType& rAdjointAuxiliaryValues,
381  const ProcessInfo& rProcessInfo)
382  {
383  KRATOS_TRY
384 
385  const int k = OpenMPUtils::ThisThread();
386  auto& adjoint_values = this->mAdjointValuesVector[k];
387  auto& aux_matrix = this->mAuxiliaryMatrix[k];
388  auto& entity_second_derivatives = this->mSecondDerivsLHS[k];
389  auto& response_second_derivatives = this->mSecondDerivsResponseGradient[k];
390 
391  const auto& r_const_entity_ref = rCurrentEntity;
392  r_const_entity_ref.GetValuesVector(adjoint_values);
393  this->CheckAndResizeThreadStorage(adjoint_values.size());
394 
395  rCurrentEntity.CalculateSecondDerivativesLHS(aux_matrix, rProcessInfo);
396  aux_matrix *= this->mBossak.Alpha;
397  this->mpResponseFunction->CalculateSecondDerivativesGradient(
398  rCurrentEntity, aux_matrix, response_second_derivatives, rProcessInfo);
399 
401  entity_second_derivatives, aux_matrix, rCurrentEntity.GetGeometry());
402 
403  if (rAdjointAuxiliaryValues.size() != entity_second_derivatives.size1()) {
404  rAdjointAuxiliaryValues.resize(entity_second_derivatives.size1(), false);
405  }
406  noalias(rAdjointAuxiliaryValues) =
407  prod(entity_second_derivatives, adjoint_values) + response_second_derivatives;
408 
409  KRATOS_CATCH("");
410  }
411 
413 
414 }; /* Class VelocityBossakAdjointScheme */
415 
417 
418 } /* namespace Kratos.*/
419 
420 #endif /* KRATOS_VELOCITY_BOSSAK_ADJOINT_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
Definition: fluid_adjoint_slip_utilities.h:39
void CalculateRotatedSlipConditionAppliedNonSlipVariableDerivatives(Matrix &rOutput, const Matrix &rResidualDerivatives, const GeometryType &rGeometry) const
Calculates rotated slip applied state derivatives.
Definition: fluid_adjoint_slip_utilities.cpp:102
void CalculateRotatedSlipConditionAppliedSlipVariableDerivatives(Matrix &rOutput, const Matrix &rResidualDerivatives, const GeometryType &rGeometry) const
Calculates rotated slip applied state derivatives.
Definition: fluid_adjoint_slip_utilities.cpp:58
This class aims to manage meshes for multi-physics simulations.
Definition: model_part.h:77
static int ThisThread()
Wrapper for omp_get_thread_num().
Definition: openmp_utils.h:108
static int GetNumThreads()
Returns the current number of threads.
Definition: parallel_utilities.cpp:34
This class provides to Kratos a data structure for I/O based on the standard of JSON.
Definition: kratos_parameters.h:59
A sorted associative container similar to an STL set, but uses a vector to store pointers to its data...
Definition: pointer_vector_set.h:72
ProcessInfo holds the current value of different solution parameters.
Definition: process_info.h:59
A scheme for dynamic adjoint equations, using Bossak time integration.
Definition: residual_based_adjoint_bossak_scheme.h:50
BossakConstants mBossak
Definition: residual_based_adjoint_bossak_scheme.h:396
virtual void CheckAndResizeThreadStorage(unsigned SystemSize)
Definition: residual_based_adjoint_bossak_scheme.h:595
BaseType::LocalSystemMatrixType LocalSystemMatrixType
Definition: residual_based_adjoint_bossak_scheme.h:65
std::vector< LocalSystemMatrixType > mSecondDerivsLHS
Definition: residual_based_adjoint_bossak_scheme.h:402
std::vector< LocalSystemMatrixType > mFirstDerivsLHS
Definition: residual_based_adjoint_bossak_scheme.h:400
AdjointResponseFunction::Pointer mpResponseFunction
Definition: residual_based_adjoint_bossak_scheme.h:394
BaseType::LocalSystemVectorType LocalSystemVectorType
Definition: residual_based_adjoint_bossak_scheme.h:63
BaseType::DofsArrayType DofsArrayType
Definition: residual_based_adjoint_bossak_scheme.h:67
std::vector< LocalSystemVectorType > mFirstDerivsResponseGradient
Definition: residual_based_adjoint_bossak_scheme.h:401
std::vector< LocalSystemVectorType > mSecondDerivsResponseGradient
Definition: residual_based_adjoint_bossak_scheme.h:403
void Initialize(ModelPart &rModelPart) override
This is the place to initialize the Scheme.
Definition: residual_based_adjoint_bossak_scheme.h:151
std::vector< LocalSystemVectorType > mAdjointValuesVector
Definition: residual_based_adjoint_bossak_scheme.h:404
This class provides the implementation of the basic tasks that are needed by the solution strategy.
Definition: scheme.h:56
Definition: velocity_bossak_adjoint_scheme.h:46
void CalculateFirstDerivativeContributions(Element &rElement, LocalSystemMatrixType &rLHS_Contribution, LocalSystemVectorType &rRHS_Contribution, const ProcessInfo &rCurrentProcessInfo) override
Definition: velocity_bossak_adjoint_scheme.h:136
void CalculateAuxiliaryVariableContributions(Element &rElement, LocalSystemVectorType &rAdjointAuxiliaryValues, AdjointResponseFunction &rAdjointResponseFunction, const BossakConstants &rBossakConstants, const ProcessInfo &rCurrentProcessInfo) override
Definition: velocity_bossak_adjoint_scheme.h:202
void CalculateGradientContributions(Element &rElement, LocalSystemMatrixType &rLHS_Contribution, LocalSystemVectorType &rRHS_Contribution, const ProcessInfo &rCurrentProcessInfo) override
Definition: velocity_bossak_adjoint_scheme.h:116
typename BaseType::LocalSystemMatrixType LocalSystemMatrixType
Definition: velocity_bossak_adjoint_scheme.h:57
void CalculateTimeSchemeContributions(Condition &rCondition, LocalSystemVectorType &rAdjointTimeSchemeValues2, LocalSystemVectorType &rAdjointTimeSchemeValues3, AdjointResponseFunction &rAdjointResponseFunction, const BossakConstants &rBossakConstants, const ProcessInfo &rCurrentProcessInfo) override
Definition: velocity_bossak_adjoint_scheme.h:189
VelocityBossakAdjointScheme(Parameters Settings, AdjointResponseFunction::Pointer pResponseFunction, const IndexType Dimension, const IndexType BlockSize)
Constructor.
Definition: velocity_bossak_adjoint_scheme.h:68
void Initialize(ModelPart &rModelPart) override
This is the place to initialize the Scheme.
Definition: velocity_bossak_adjoint_scheme.h:86
void CalculateAuxiliaryVariableContributions(Condition &rCondition, LocalSystemVectorType &rAdjointAuxiliaryValues, AdjointResponseFunction &rAdjointResponseFunction, const BossakConstants &rBossakConstants, const ProcessInfo &rCurrentProcessInfo) override
Definition: velocity_bossak_adjoint_scheme.h:213
void CalculateFirstDerivativeContributions(Condition &rCondition, LocalSystemMatrixType &rLHS_Contribution, LocalSystemVectorType &rRHS_Contribution, const ProcessInfo &rCurrentProcessInfo) override
Definition: velocity_bossak_adjoint_scheme.h:146
KRATOS_CLASS_POINTER_DEFINITION(VelocityBossakAdjointScheme)
std::string Info() const override
Turn back information as a string.
Definition: velocity_bossak_adjoint_scheme.h:105
void CalculateTimeSchemeContributions(Element &rElement, LocalSystemVectorType &rAdjointTimeSchemeValues2, LocalSystemVectorType &rAdjointTimeSchemeValues3, AdjointResponseFunction &rAdjointResponseFunction, const BossakConstants &rBossakConstants, const ProcessInfo &rCurrentProcessInfo) override
Definition: velocity_bossak_adjoint_scheme.h:176
typename BaseType::LocalSystemVectorType LocalSystemVectorType
Definition: velocity_bossak_adjoint_scheme.h:55
void CalculateSecondDerivativeContributions(Element &rElement, LocalSystemMatrixType &rLHS_Contribution, LocalSystemVectorType &rRHS_Contribution, const ProcessInfo &rCurrentProcessInfo) override
Definition: velocity_bossak_adjoint_scheme.h:156
void CalculateSecondDerivativeContributions(Condition &rCondition, LocalSystemMatrixType &rLHS_Contribution, LocalSystemVectorType &rRHS_Contribution, const ProcessInfo &rCurrentProcessInfo) override
Definition: velocity_bossak_adjoint_scheme.h:166
typename BaseType::BossakConstants BossakConstants
Definition: velocity_bossak_adjoint_scheme.h:61
void CalculateGradientContributions(Condition &rCondition, LocalSystemMatrixType &rLHS_Contribution, LocalSystemVectorType &rRHS_Contribution, const ProcessInfo &rCurrentProcessInfo) override
Definition: velocity_bossak_adjoint_scheme.h:126
~VelocityBossakAdjointScheme() override=default
Destructor.
void CheckAndResizeThreadStorage(unsigned SystemSize) override
Definition: velocity_bossak_adjoint_scheme.h:224
#define KRATOS_CATCH(MoreInfo)
Definition: define.h:110
#define KRATOS_TRY
Definition: define.h:109
std::size_t IndexType
The definition of the index type.
Definition: key_hash.h:35
#define KRATOS_INFO(label)
Definition: logger.h:250
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
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
int k
Definition: quadrature.py:595
def num_threads
Definition: script.py:75
double C6
Definition: residual_based_adjoint_bossak_scheme.h:390
double Alpha
Definition: residual_based_adjoint_bossak_scheme.h:381
double C7
Definition: residual_based_adjoint_bossak_scheme.h:391