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.
optimization_utilities.h
Go to the documentation of this file.
1 // ==============================================================================
2 // KratosShapeOptimizationApplication
3 //
4 // License: BSD License
5 // license: ShapeOptimizationApplication/license.txt
6 //
7 // Main authors: Baumgaertner Daniel, https://github.com/dbaumgaertner
8 //
9 // ==============================================================================
10 
11 #ifndef OPTIMIZATION_UTILITIES_H
12 #define OPTIMIZATION_UTILITIES_H
13 
14 // ------------------------------------------------------------------------------
15 // System includes
16 // ------------------------------------------------------------------------------
17 #include <iostream>
18 #include <string>
19 
20 // ------------------------------------------------------------------------------
21 // Project includes
22 // ------------------------------------------------------------------------------
23 #include "includes/define.h"
24 #include "includes/model_part.h"
25 #include "spaces/ublas_space.h"
27 
28 // ==============================================================================
29 
30 namespace Kratos
31 {
32 
35 
39 
40 
44 
48 
52 
54 
58 class KRATOS_API(SHAPE_OPTIMIZATION_APPLICATION) OptimizationUtilities
59 {
60 public:
63 
66 
69 
73 
77 
78 
82 
83  // ==============================================================================
84  // General optimization operations
85  // ==============================================================================
86  static void ComputeControlPointUpdate(ModelPart& rModelPart, const double StepSize, const bool Normalize);
87 
88  // --------------------------------------------------------------------------
89  static void AddFirstVariableToSecondVariable( ModelPart& rModelPart, const Variable<array_3d> &rFirstVariable, const Variable<array_3d> &rSecondVariable );
90 
91  // --------------------------------------------------------------------------
92  static double ComputeL2NormOfNodalVariable( ModelPart& rModelPart, const Variable<array_3d> &rVariable);
93 
94  // --------------------------------------------------------------------------
95  static double ComputeL2NormOfNodalVariable(ModelPart& rModelPart, const Variable<double> &rVariable);
96 
97  // --------------------------------------------------------------------------
98  static double ComputeMaxNormOfNodalVariable(ModelPart& rModelPart, const Variable<array_3d> &rVariable);
99 
100  // --------------------------------------------------------------------------
101  static double ComputeMaxNormOfNodalVariable(ModelPart& rModelPart, const Variable<double> &rVariable);
102 
103  // ==============================================================================
104  // For running unconstrained descent methods
105  // ==============================================================================
106  static void ComputeSearchDirectionSteepestDescent(ModelPart& rModelPart);
107 
108  // ==============================================================================
109  // For running penalized projection method
110  // ==============================================================================
111  static void ComputeProjectedSearchDirection(ModelPart& rModelPart);
112 
113  // --------------------------------------------------------------------------
114  static double CorrectProjectedSearchDirection(ModelPart& rModelPart, const double PrevConstraintValue, const double ConstraintValue, const double CorrectionScaling, const bool IsAdaptive );
115 
116  // --------------------------------------------------------------------------
117  static double ComputeCorrectionFactor(ModelPart& rModelPart, const double PrevConstraintValue, const double ConstraintValue, double& CorrectionScaling, const bool IsAdaptive);
118 
122  static void AssembleVector( ModelPart& rModelPart,
123  Vector& rVector,
124  const Variable<double> &rVariable);
125 
129  static void AssembleVector( ModelPart& rModelPart,
130  Vector& rVector,
131  const Variable<array_3d> &rVariable);
132 
136  static void AssignVectorToVariable(ModelPart& rModelPart,
137  const Vector& rVector,
138  const Variable<double> &rVariable);
139 
143  static void AssignVectorToVariable(ModelPart& rModelPart,
144  const Vector& rVector,
145  const Variable<array_3d> &rVariable);
146 
151  static void AssembleMatrix(ModelPart& rModelPart,
152  Matrix& rMatrix,
153  const std::vector<Variable<array_3d>*>& rVariables);
154 
155 
162  static void CalculateProjectedSearchDirectionAndCorrection(
163  Vector& rObjectiveGradient,
164  Matrix& rConstraintGradients,
165  Vector& rConstraintValues,
167  Vector& rProjectedSearchDirection,
168  Vector& rRestoration);
169 
170  // ==============================================================================
171  // For running relaxed gradient projection
172  // ==============================================================================
173 
177  static void AssembleBufferMatrix( Matrix& rMatrix,
178  const std::vector<double>& rVariables)
179  {
180  size_t VectorSize = rVariables.size();
181  rMatrix = ZeroMatrix(VectorSize, VectorSize);
182  IndexPartition(VectorSize).for_each([&](const int i) {rMatrix(i,i) = rVariables[i];} );
183  }
184 
191  Vector& rObjectiveGradient,
192  Matrix& rConstraintGradients,
193  Matrix& rRelaxationCoefficients,
194  Vector& rCorrectionCoefficients,
196  Vector& rProjectedSearchDirection,
197  Vector& rCorrection
198  )
199  {
200  // local variable naming according to https://msulaiman.org/onewebmedia/GradProj_2.pdf
201  Vector& nabla_f = rObjectiveGradient;
202  Matrix& N = rConstraintGradients;
203  Vector& s = rProjectedSearchDirection;
204  Vector& c = rCorrection;
205  Matrix& omega_r = rRelaxationCoefficients;
206  Vector& omega_c = rCorrectionCoefficients;
207 
208 
209  Matrix NTN = prod(trans(N), N);
210  Matrix I = IdentityMatrix(N.size2());
211  Matrix NTN_inv(NTN.size1(), NTN.size2());
212 
213  rSolver.Solve(NTN, NTN_inv, I); // solve with identity to get the inverse
214 
215 
216  s = - (nabla_f - prod(N, Vector(prod(omega_r, Vector(prod(NTN_inv, Vector(prod(trans(N), nabla_f))))))));
217 
218  c = - prod(N, omega_c);
219 
220  }
221 
222  // ==============================================================================
223 
227 
228 
232 
233 
237 
238 
242 
243 
245 
246 protected:
249 
250 
254 
255 
259 
260 
264 
265 
269 
270 
274 
275 
279 
280 
282 
283 private:
286 
287 
291 
292  // ==============================================================================
293  // Initialized by class constructor
294  // ==============================================================================
295 
299 
300 
304 
305 
309 
310 
314 
315 
319 
321 // OptimizationUtilities& operator=(OptimizationUtilities const& rOther);
322 
324 // OptimizationUtilities(OptimizationUtilities const& rOther);
325 
326 
328 
329 }; // Class OptimizationUtilities
330 
332 
335 
336 
340 
342 
343 
344 } // namespace Kratos.
345 
346 #endif // OPTIMIZATION_UTILITIES_H
This class is useful for index iteration over containers.
Definition: parallel_utilities.h:451
void for_each(TUnaryFunction &&f)
Definition: parallel_utilities.h:514
Base class for all the linear solvers in Kratos.
Definition: linear_solver.h:65
virtual bool Solve(SparseMatrixType &rA, VectorType &rX, VectorType &rB)
Definition: linear_solver.h:186
This class aims to manage meshes for multi-physics simulations.
Definition: model_part.h:77
Short class definition.
Definition: optimization_utilities.h:59
KRATOS_CLASS_POINTER_DEFINITION(OptimizationUtilities)
Pointer definition of OptimizationUtilities.
static void AssembleBufferMatrix(Matrix &rMatrix, const std::vector< double > &rVariables)
Definition: optimization_utilities.h:177
static void CalculateRelaxedProjectedSearchDirectionAndCorrection(Vector &rObjectiveGradient, Matrix &rConstraintGradients, Matrix &rRelaxationCoefficients, Vector &rCorrectionCoefficients, LinearSolver< DenseSpace, DenseSpace > &rSolver, Vector &rProjectedSearchDirection, Vector &rCorrection)
Definition: optimization_utilities.h:190
UblasSpace< double, Matrix, Vector > DenseSpace
Definition: optimization_utilities.h:65
array_1d< double, 3 > array_3d
Definition: optimization_utilities.h:64
A class template for handling data types, matrices, and vectors in a Ublas space.
Definition: ublas_space.h:121
Variable class contains all information needed to store and retrive data from a data container.
Definition: variable.h:63
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
AMatrix::IdentityMatrix< double > IdentityMatrix
Definition: amatrix_interface.h:564
Internals::Matrix< double, AMatrix::dynamic, 1 > Vector
Definition: amatrix_interface.h:472
KratosZeroMatrix< double > ZeroMatrix
Definition: amatrix_interface.h:559
AMatrix::MatrixProductExpression< TExpression1Type, TExpression2Type > prod(AMatrix::MatrixExpression< TExpression1Type, TCategory1 > const &First, AMatrix::MatrixExpression< TExpression2Type, TCategory2 > const &Second)
Definition: amatrix_interface.h:568
AMatrix::TransposeMatrix< const T > trans(const T &TheMatrix)
Definition: amatrix_interface.h:486
def Normalize(v)
Definition: embedded.py:28
c
Definition: generate_weakly_compressible_navier_stokes_element.py:108
N
Definition: sensitivityMatrix.py:29
integer i
Definition: TensorModule.f:17