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.
simple_steady_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_SIMPLE_STEADY_ADJOINT_SCHEME_H_INCLUDED)
14 #define KRATOS_SIMPLE_STEADY_ADJOINT_SCHEME_H_INCLUDED
15 
16 // System includes
17 #include <string>
18 
19 // External includes
20 
21 // Project includes
22 #include "includes/define.h"
25 
26 // Application includes
29 
30 namespace Kratos
31 {
34 
36 
47 template <class TSparseSpace, class TDenseSpace>
48 class SimpleSteadyAdjointScheme : public ResidualBasedAdjointStaticScheme<TSparseSpace, TDenseSpace>
49 {
50 public:
53 
54  KRATOS_CLASS_POINTER_DEFINITION(SimpleSteadyAdjointScheme);
55 
56  using BaseType = ResidualBasedAdjointStaticScheme<TSparseSpace, TDenseSpace>;
57 
58  using LocalSystemVectorType = typename BaseType::LocalSystemVectorType;
59 
60  using LocalSystemMatrixType = typename BaseType::LocalSystemMatrixType;
61 
65 
67  explicit SimpleSteadyAdjointScheme(
68  AdjointResponseFunction::Pointer pResponseFunction,
69  const IndexType Dimension,
70  const IndexType BlockSize)
71  : BaseType(pResponseFunction),
72  mAdjointSlipUtilities(Dimension, BlockSize)
73  {
74  // Allocate auxiliary memory.
75  const int number_of_threads = ParallelUtilities::GetNumThreads();
76  mAuxMatrices.resize(number_of_threads);
77 
78  KRATOS_INFO(this->Info()) << this->Info() << " created [ Dimensionality = " << Dimension << ", BlockSize = " << BlockSize << " ].\n";
79  }
80 
82  ~SimpleSteadyAdjointScheme() override = default;
83 
87 
88  void CalculateSystemContributions(
89  Element& rCurrentElement,
90  LocalSystemMatrixType& rLHS_Contribution,
91  LocalSystemVectorType& rRHS_Contribution,
92  Element::EquationIdVectorType& rEquationId,
93  const ProcessInfo& rCurrentProcessInfo) override
94  {
95  CalculateEntitySystemContributions(rCurrentElement, rLHS_Contribution, rRHS_Contribution,
96  rEquationId, rCurrentProcessInfo);
97  }
98 
99  void CalculateLHSContribution(
100  Element& rCurrentElement,
101  LocalSystemMatrixType& rLHS_Contribution,
102  Element::EquationIdVectorType& rEquationId,
103  const ProcessInfo& rCurrentProcessInfo) override
104  {
105  const int thread_id = OpenMPUtils::ThisThread();
106  Matrix& aux_matrix = mAuxMatrices[thread_id];
107  CalculateEntityLHSContribution(rCurrentElement, aux_matrix, rLHS_Contribution,
108  rEquationId, rCurrentProcessInfo);
109  }
110 
111  void CalculateSystemContributions(
112  Condition& rCurrentCondition,
113  LocalSystemMatrixType& rLHS_Contribution,
114  LocalSystemVectorType& rRHS_Contribution,
115  Condition::EquationIdVectorType& rEquationId,
116  const ProcessInfo& rCurrentProcessInfo) override
117  {
118  CalculateEntitySystemContributions(rCurrentCondition, rLHS_Contribution, rRHS_Contribution,
119  rEquationId, rCurrentProcessInfo);
120  }
121 
122  void CalculateLHSContribution(
123  Condition& rCurrentCondition,
124  LocalSystemMatrixType& rLHS_Contribution,
125  Condition::EquationIdVectorType& rEquationId,
126  const ProcessInfo& rCurrentProcessInfo) override
127  {
128  const int thread_id = OpenMPUtils::ThisThread();
129  Matrix& aux_matrix = mAuxMatrices[thread_id];
130  CalculateEntityLHSContribution(rCurrentCondition, aux_matrix, rLHS_Contribution,
131  rEquationId, rCurrentProcessInfo);
132 
133  }
134 
138 
140  std::string Info() const override
141  {
142  return "SimpleSteadyAdjointScheme";
143  }
144 
146 
147 private:
150 
151  std::vector<Matrix> mAuxMatrices;
152 
153  const FluidAdjointSlipUtilities mAdjointSlipUtilities;
154 
158 
159  template<class TEntityType>
160  void CalculateEntitySystemContributions(
161  TEntityType& rEntity,
162  LocalSystemMatrixType& rLHS_Contribution,
163  LocalSystemVectorType& rRHS_Contribution,
164  typename TEntityType::EquationIdVectorType& rEquationId,
165  const ProcessInfo& rCurrentProcessInfo)
166  {
167  KRATOS_TRY;
168 
169  const int thread_id = OpenMPUtils::ThisThread();
170  Matrix& residual_derivatives = mAuxMatrices[thread_id];
171 
172  CalculateEntityLHSContribution<TEntityType>(
173  rEntity, residual_derivatives, rLHS_Contribution, rEquationId, rCurrentProcessInfo);
174 
175  if (rRHS_Contribution.size() != rLHS_Contribution.size1())
176  rRHS_Contribution.resize(rLHS_Contribution.size1(), false);
177 
178  this->mpResponseFunction->CalculateFirstDerivativesGradient(
179  rEntity, residual_derivatives, rRHS_Contribution, rCurrentProcessInfo);
180 
181  noalias(rRHS_Contribution) = -rRHS_Contribution;
182 
183  // Calculate system contributions in residual form.
184  if (rLHS_Contribution.size1() != 0) {
185  auto& adjoint_values = this->mAdjointValues[thread_id];
186  rEntity.GetValuesVector(adjoint_values);
187  noalias(rRHS_Contribution) -= prod(rLHS_Contribution, adjoint_values);
188  }
189 
190  KRATOS_CATCH("");
191  }
192 
193  template<class TEntityType>
194  void CalculateEntityLHSContribution(
195  TEntityType& rEntity,
196  Matrix& rEntityResidualFirstDerivatives,
197  Matrix& rEntityRotatedResidualFirstDerivatives,
198  Condition::EquationIdVectorType& rEquationId,
199  const ProcessInfo& rCurrentProcessInfo)
200  {
201  KRATOS_TRY
202 
203  rEntity.CalculateFirstDerivativesLHS(rEntityResidualFirstDerivatives, rCurrentProcessInfo);
204  rEntity.EquationIdVector(rEquationId, rCurrentProcessInfo);
205 
206  mAdjointSlipUtilities.CalculateRotatedSlipConditionAppliedSlipVariableDerivatives(
207  rEntityRotatedResidualFirstDerivatives, rEntityResidualFirstDerivatives, rEntity.GetGeometry());
208 
209  KRATOS_CATCH("");
210  }
211 
213 
214 }; /* Class SimpleSteadyAdjointScheme */
215 
217 
218 } /* namespace Kratos.*/
219 
220 #endif /* KRATOS_SIMPLE_STEADY_ADJOINT_SCHEME_H_INCLUDED defined */
std::string Info() const override
Turn back information as a string.
Definition: periodic_interface_process.hpp:93
std::vector< std::size_t > EquationIdVectorType
Definition: condition.h:98
std::vector< std::size_t > EquationIdVectorType
Definition: element.h:98
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
#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
Internals::Matrix< double, AMatrix::dynamic, AMatrix::dynamic > Matrix
Definition: amatrix_interface.h:470
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
#define KRATOS_CLASS_POINTER_DEFINITION(a)
Definition: smart_pointers.h:69