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.
residual_based_adjoint_static_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:
11 //
12 
13 #pragma once
14 
15 // System includes
16 #include <vector>
17 #include <string>
18 
19 // External includes
20 
21 // Project includes
22 #include "includes/define.h"
24 #include "utilities/openmp_utils.h"
27 
28 namespace Kratos
29 {
32 
34 
44 template <class TSparseSpace, class TDenseSpace>
45 class ResidualBasedAdjointStaticScheme : public Scheme<TSparseSpace, TDenseSpace>
46 {
47 public:
50 
52 
54 
56 
58 
60 
62 
64 
68 
70  explicit ResidualBasedAdjointStaticScheme(AdjointResponseFunction::Pointer pResponseFunction)
71  : Scheme<TSparseSpace, TDenseSpace>()
72  {
73  mpResponseFunction = pResponseFunction;
74 
77  mLHS.resize(num_threads);
78  }
79 
82  {
83  }
84 
88 
92 
104  void SetResponseFunction(AdjointResponseFunction::Pointer pResponseFunction)
105  {
106  mpResponseFunction = pResponseFunction;
107  }
108 
109  void Initialize(ModelPart& rModelPart) override
110  {
111  KRATOS_TRY;
112 
113  block_for_each(rModelPart.Nodes(), [](Node& rNode){
114  for (auto& r_dof : rNode.GetDofs()) {
115  if (r_dof->IsFree()) {
116  r_dof->GetSolutionStepValue() = 0.0;
117  }
118  }
119  });
120 
121  BaseType::Initialize(rModelPart);
122 
123  KRATOS_CATCH("");
124  }
125 
126  void Update(ModelPart& rModelPart,
127  DofsArrayType& rDofSet,
128  SystemMatrixType& rA,
129  SystemVectorType& rDx,
130  SystemVectorType& rb) override
131  {
132  KRATOS_TRY;
133 
134  // Update degrees of freedom: adjoint variables associated to the
135  // residual of the physical problem.
136  this->mpDofUpdater->UpdateDofs(rDofSet, rDx);
137 
138  KRATOS_CATCH("");
139  }
140 
141  void CalculateSystemContributions(Element& rCurrentElement,
142  LocalSystemMatrixType& rLHS_Contribution,
143  LocalSystemVectorType& rRHSContribution,
144  Element::EquationIdVectorType& rEquationId,
145  const ProcessInfo& rCurrentProcessInfo) override
146  {
147  KRATOS_TRY;
148 
149  int thread_id = OpenMPUtils::ThisThread();
150 
151  const auto& r_const_elem_ref = rCurrentElement;
152 
153  rCurrentElement.CalculateLeftHandSide(rLHS_Contribution, rCurrentProcessInfo);
154 
155  if (rRHSContribution.size() != rLHS_Contribution.size1())
156  rRHSContribution.resize(rLHS_Contribution.size1(), false);
157 
158  mpResponseFunction->CalculateGradient(
159  rCurrentElement, rLHS_Contribution, rRHSContribution, rCurrentProcessInfo);
160 
161  noalias(rRHSContribution) = -rRHSContribution;
162 
163  // Calculate system contributions in residual form.
164  r_const_elem_ref.GetValuesVector(mAdjointValues[thread_id]);
165  noalias(rRHSContribution) -= prod(rLHS_Contribution, mAdjointValues[thread_id]);
166 
167  r_const_elem_ref.EquationIdVector(rEquationId, rCurrentProcessInfo);
168 
169  KRATOS_CATCH("");
170  }
171 
172  void CalculateLHSContribution(Element& rCurrentElement,
173  LocalSystemMatrixType& rLHS_Contribution,
174  Element::EquationIdVectorType& rEquationId,
175  const ProcessInfo& rCurrentProcessInfo) override
176  {
177  KRATOS_TRY;
178 
179  rCurrentElement.CalculateLeftHandSide(rLHS_Contribution, rCurrentProcessInfo);
180  rCurrentElement.EquationIdVector(rEquationId, rCurrentProcessInfo);
181 
182  KRATOS_CATCH("");
183  }
184 
186  Element& rCurrentElement,
187  LocalSystemVectorType& rRHSContribution,
188  Element::EquationIdVectorType& rEquationId,
189  const ProcessInfo& rCurrentProcessInfo) override
190  {
191  int thread_id = OpenMPUtils::ThisThread();
192 
193  const auto& r_const_elem_ref = rCurrentElement;
194  auto& lhs = mLHS[thread_id];
195 
196  rCurrentElement.CalculateLeftHandSide(lhs, rCurrentProcessInfo);
197 
198  if (rRHSContribution.size() != lhs.size1())
199  rRHSContribution.resize(lhs.size1(), false);
200 
201  mpResponseFunction->CalculateGradient(
202  rCurrentElement, lhs, rRHSContribution, rCurrentProcessInfo);
203 
204  noalias(rRHSContribution) = -rRHSContribution;
205 
206  // Calculate system contributions in residual form.
207  r_const_elem_ref.GetValuesVector(mAdjointValues[thread_id]);
208  noalias(rRHSContribution) -= prod(lhs, mAdjointValues[thread_id]);
209 
210  r_const_elem_ref.EquationIdVector(rEquationId, rCurrentProcessInfo);
211  }
212 
213  void CalculateSystemContributions(Condition& rCurrentCondition,
214  LocalSystemMatrixType& rLHS_Contribution,
215  LocalSystemVectorType& rRHSContribution,
216  Condition::EquationIdVectorType& rEquationId,
217  const ProcessInfo& rCurrentProcessInfo) override
218  {
219  KRATOS_TRY;
220 
221  int thread_id = OpenMPUtils::ThisThread();
222  const auto& r_const_cond_ref = rCurrentCondition;
223  rCurrentCondition.CalculateLeftHandSide(rLHS_Contribution, rCurrentProcessInfo);
224 
225  if (rRHSContribution.size() != rLHS_Contribution.size1())
226  rRHSContribution.resize(rLHS_Contribution.size1(), false);
227 
228  mpResponseFunction->CalculateGradient(
229  rCurrentCondition, rLHS_Contribution, rRHSContribution, rCurrentProcessInfo);
230 
231  noalias(rRHSContribution) = -rRHSContribution;
232 
233  // Calculate system contributions in residual form.
234  r_const_cond_ref.GetValuesVector(mAdjointValues[thread_id]);
235  noalias(rRHSContribution) -= prod(rLHS_Contribution, mAdjointValues[thread_id]);
236 
237  r_const_cond_ref.EquationIdVector(rEquationId, rCurrentProcessInfo);
238 
239  KRATOS_CATCH("");
240  }
241 
242  void CalculateLHSContribution(Condition& rCurrentCondition,
243  LocalSystemMatrixType& rLHS_Contribution,
244  Condition::EquationIdVectorType& rEquationId,
245  const ProcessInfo& rCurrentProcessInfo) override
246  {
247  KRATOS_TRY;
248 
249  rCurrentCondition.CalculateLeftHandSide(rLHS_Contribution, rCurrentProcessInfo);
250  rCurrentCondition.EquationIdVector(rEquationId, rCurrentProcessInfo);
251 
252  KRATOS_CATCH("");
253  }
254 
256  Condition& rCurrentCondition,
257  LocalSystemVectorType& rRHSContribution,
258  Condition::EquationIdVectorType& rEquationId,
259  const ProcessInfo& rCurrentProcessInfo) override
260  {
261  int thread_id = OpenMPUtils::ThisThread();
262 
263  const auto& r_const_elem_ref = rCurrentCondition;
264  auto& lhs = mLHS[thread_id];
265 
266  rCurrentCondition.CalculateLeftHandSide(lhs, rCurrentProcessInfo);
267 
268  if (rRHSContribution.size() != lhs.size1())
269  rRHSContribution.resize(lhs.size1(), false);
270 
271  mpResponseFunction->CalculateGradient(
272  rCurrentCondition, lhs, rRHSContribution, rCurrentProcessInfo);
273 
274  noalias(rRHSContribution) = -rRHSContribution;
275 
276  // Calculate system contributions in residual form.
277  r_const_elem_ref.GetValuesVector(mAdjointValues[thread_id]);
278  noalias(rRHSContribution) -= prod(lhs, mAdjointValues[thread_id]);
279 
280  r_const_elem_ref.EquationIdVector(rEquationId, rCurrentProcessInfo);
281  }
282 
283  void Clear() override
284  {
285  this->mpDofUpdater->Clear();
286  }
287 
291 
295 
299 
301 
302 protected:
305 
309 
310  AdjointResponseFunction::Pointer mpResponseFunction;
311  std::vector<LocalSystemVectorType> mAdjointValues;
312  std::vector<LocalSystemMatrixType> mLHS;
313 
317 
321 
325 
329 
333 
335 
336 private:
339 
343 
344  typename TSparseSpace::DofUpdaterPointerType mpDofUpdater =
345  TSparseSpace::CreateDofUpdater();
346 
350 
354 
358 
362 
366 
368 
369 }; /* Class ResidualBasedAdjointStaticScheme */
370 
372 
375 
377 
378 } /* namespace Kratos.*/
Base class for all Conditions.
Definition: condition.h:59
std::vector< std::size_t > EquationIdVectorType
Definition: condition.h:98
virtual void CalculateLeftHandSide(MatrixType &rLeftHandSideMatrix, const ProcessInfo &rCurrentProcessInfo)
Definition: condition.h:426
virtual void EquationIdVector(EquationIdVectorType &rResult, const ProcessInfo &rCurrentProcessInfo) const
Definition: condition.h:260
Base class for all Elements.
Definition: element.h:60
virtual void CalculateLeftHandSide(MatrixType &rLeftHandSideMatrix, const ProcessInfo &rCurrentProcessInfo)
Definition: element.h:423
virtual void EquationIdVector(EquationIdVectorType &rResult, const ProcessInfo &rCurrentProcessInfo) const
Definition: element.h:258
std::vector< std::size_t > EquationIdVectorType
Definition: element.h:98
This class aims to manage meshes for multi-physics simulations.
Definition: model_part.h:77
NodesContainerType & Nodes(IndexType ThisIndex=0)
Definition: model_part.h:507
This class defines the node.
Definition: node.h:65
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
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 static adjoint equations.
Definition: residual_based_adjoint_static_scheme.h:46
~ResidualBasedAdjointStaticScheme() override
Destructor.
Definition: residual_based_adjoint_static_scheme.h:81
void Update(ModelPart &rModelPart, DofsArrayType &rDofSet, SystemMatrixType &rA, SystemVectorType &rDx, SystemVectorType &rb) override
Performing the update of the solution.
Definition: residual_based_adjoint_static_scheme.h:126
void CalculateLHSContribution(Element &rCurrentElement, LocalSystemMatrixType &rLHS_Contribution, Element::EquationIdVectorType &rEquationId, const ProcessInfo &rCurrentProcessInfo) override
This function is designed to calculate just the LHS contribution.
Definition: residual_based_adjoint_static_scheme.h:172
ResidualBasedAdjointStaticScheme(AdjointResponseFunction::Pointer pResponseFunction)
Constructor.
Definition: residual_based_adjoint_static_scheme.h:70
std::vector< LocalSystemMatrixType > mLHS
Definition: residual_based_adjoint_static_scheme.h:312
void CalculateLHSContribution(Condition &rCurrentCondition, LocalSystemMatrixType &rLHS_Contribution, Condition::EquationIdVectorType &rEquationId, const ProcessInfo &rCurrentProcessInfo) override
Functions totally analogous to the precedent but applied to the "condition" objects.
Definition: residual_based_adjoint_static_scheme.h:242
void CalculateSystemContributions(Condition &rCurrentCondition, LocalSystemMatrixType &rLHS_Contribution, LocalSystemVectorType &rRHSContribution, Condition::EquationIdVectorType &rEquationId, const ProcessInfo &rCurrentProcessInfo) override
Functions totally analogous to the precedent but applied to the "condition" objects.
Definition: residual_based_adjoint_static_scheme.h:213
void CalculateSystemContributions(Element &rCurrentElement, LocalSystemMatrixType &rLHS_Contribution, LocalSystemVectorType &rRHSContribution, Element::EquationIdVectorType &rEquationId, const ProcessInfo &rCurrentProcessInfo) override
This function is designed to be called in the builder and solver to introduce the selected time integ...
Definition: residual_based_adjoint_static_scheme.h:141
BaseType::LocalSystemMatrixType LocalSystemMatrixType
Definition: residual_based_adjoint_static_scheme.h:61
Scheme< TSparseSpace, TDenseSpace > BaseType
Definition: residual_based_adjoint_static_scheme.h:53
void CalculateRHSContribution(Condition &rCurrentCondition, LocalSystemVectorType &rRHSContribution, Condition::EquationIdVectorType &rEquationId, const ProcessInfo &rCurrentProcessInfo) override
Functions totally analogous to the precedent but applied to the "condition" objects.
Definition: residual_based_adjoint_static_scheme.h:255
KRATOS_CLASS_POINTER_DEFINITION(ResidualBasedAdjointStaticScheme)
void CalculateRHSContribution(Element &rCurrentElement, LocalSystemVectorType &rRHSContribution, Element::EquationIdVectorType &rEquationId, const ProcessInfo &rCurrentProcessInfo) override
This function is designed to calculate just the RHS contribution.
Definition: residual_based_adjoint_static_scheme.h:185
BaseType::TSystemVectorType SystemVectorType
Definition: residual_based_adjoint_static_scheme.h:57
void SetResponseFunction(AdjointResponseFunction::Pointer pResponseFunction)
Set the Response Function.
Definition: residual_based_adjoint_static_scheme.h:104
std::vector< LocalSystemVectorType > mAdjointValues
Definition: residual_based_adjoint_static_scheme.h:311
void Clear() override
Liberate internal storage.
Definition: residual_based_adjoint_static_scheme.h:283
BaseType::DofsArrayType DofsArrayType
Definition: residual_based_adjoint_static_scheme.h:63
BaseType::TSystemMatrixType SystemMatrixType
Definition: residual_based_adjoint_static_scheme.h:55
BaseType::LocalSystemVectorType LocalSystemVectorType
Definition: residual_based_adjoint_static_scheme.h:59
void Initialize(ModelPart &rModelPart) override
This is the place to initialize the Scheme.
Definition: residual_based_adjoint_static_scheme.h:109
AdjointResponseFunction::Pointer mpResponseFunction
Definition: residual_based_adjoint_static_scheme.h:310
This class provides the implementation of the basic tasks that are needed by the solution strategy.
Definition: scheme.h:56
typename TSparseSpace::MatrixType TSystemMatrixType
Matrix type definition.
Definition: scheme.h:71
typename TSparseSpace::VectorType TSystemVectorType
Vector type definition.
Definition: scheme.h:74
typename TDenseSpace::VectorType LocalSystemVectorType
Local system vector type definition.
Definition: scheme.h:80
virtual void Initialize(ModelPart &rModelPart)
This is the place to initialize the Scheme.
Definition: scheme.h:168
typename TDenseSpace::MatrixType LocalSystemMatrixType
Local system matrix type definition.
Definition: scheme.h:77
#define KRATOS_CATCH(MoreInfo)
Definition: define.h:110
#define KRATOS_TRY
Definition: define.h:109
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
void block_for_each(TIterator itBegin, TIterator itEnd, TFunction &&rFunction)
Execute a functor on all items of a range in parallel.
Definition: parallel_utilities.h:299
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
lhs
Definition: generate_frictional_mortar_condition.py:297
def num_threads
Definition: script.py:75