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.
finite_difference_utility.h
Go to the documentation of this file.
1 // KRATOS ___| | | |
2 // \___ \ __| __| | | __| __| | | __| _` | |
3 // | | | | | ( | | | | ( | |
4 // _____/ \__|_| \__,_|\___|\__|\__,_|_| \__,_|_| MECHANICS
5 //
6 // License: BSD License
7 // license: StructuralMechanicsApplication/license.txt
8 //
9 // Main authors: Martin Fusseder, https://github.com/MFusseder
10 //
11 
12 #pragma once
13 
14 // System includes
15 
16 // External includes
17 
18 // Project includes
19 #include "includes/define.h"
20 #include "includes/element.h"
21 #include "includes/condition.h"
23 
24 namespace Kratos
25 {
26 
34 class KRATOS_API(STRUCTURAL_MECHANICS_APPLICATION) FiniteDifferenceUtility
35 {
36 public:
37 
39  typedef std::size_t IndexType;
40  typedef std::size_t SizeType;
41 
42  static void CalculateRightHandSideDerivative(Element& rElement,
43  const Vector& rRHS,
44  const Variable<double>& rDesignVariable,
45  const double& rPertubationSize,
46  Matrix& rOutput,
47  const ProcessInfo& rCurrentProcessInfo);
48 
49  template <typename TElementType>
50  static void CalculateRightHandSideDerivative(TElementType& rElement,
51  const Vector& rRHS,
52  const array_1d_component_type& rDesignVariable,
53  Node& rNode,
54  const double& rPertubationSize,
55  Vector& rOutput,
56  const ProcessInfo& rCurrentProcessInfo)
57  {
58  KRATOS_TRY;
59 
60  if( rDesignVariable == SHAPE_SENSITIVITY_X || rDesignVariable == SHAPE_SENSITIVITY_Y || rDesignVariable == SHAPE_SENSITIVITY_Z )
61  {
62  const IndexType coord_dir =
63  FiniteDifferenceUtility::GetCoordinateDirection(rDesignVariable);
64 
65  // define working variables
66  Vector RHS_perturbed;
67 
68  if (rOutput.size() != rRHS.size())
69  rOutput.resize(rRHS.size(), false);
70 
71  // perturb the design variable
72  rNode.GetInitialPosition()[coord_dir] += rPertubationSize;
73  rNode.Coordinates()[coord_dir] += rPertubationSize;
74 
75  // compute LHS after perturbation
76  rElement.CalculateRightHandSide(RHS_perturbed, rCurrentProcessInfo);
77 
78  // compute derivative of RHS w.r.t. design variable with finite differences
79  noalias(rOutput) = (RHS_perturbed - rRHS) / rPertubationSize;
80 
81  // unperturb the design variable
82  rNode.GetInitialPosition()[coord_dir] -= rPertubationSize;
83  rNode.Coordinates()[coord_dir] -= rPertubationSize;
84  }
85  else
86  {
87  KRATOS_WARNING("FiniteDifferenceUtility") << "Unsupported nodal design variable: " << rDesignVariable << std::endl;
88  if ( (rOutput.size() != 0) )
89  rOutput.resize(0,false);
90  }
91 
92  KRATOS_CATCH("");
93  }
94 
95  static void CalculateLeftHandSideDerivative(Element& rElement,
96  const Matrix& rLHS,
97  const array_1d_component_type& rDesignVariable,
98  Node& rNode,
99  const double& rPertubationSize,
100  Matrix& rOutput,
101  const ProcessInfo& rCurrentProcessInfo);
102 
103  static void CalculateMassMatrixDerivative(Element& rElement,
104  const Matrix& rMassMatrix,
105  const array_1d_component_type& rDesignVariable,
106  Node& rNode,
107  const double& rPertubationSize,
108  Matrix& rOutput,
109  const ProcessInfo& rCurrentProcessInfo);
110 
111 private:
112 
113  static std::size_t GetCoordinateDirection(const array_1d_component_type& rDesignVariable);
114 
115 }; // class FiniteDifferenceUtility.
116 
117 
118 
119 } // namespace Kratos.
120 
121 
Base class for all Elements.
Definition: element.h:60
FiniteDifferenceUtility.
Definition: finite_difference_utility.h:35
static void CalculateRightHandSideDerivative(TElementType &rElement, const Vector &rRHS, const array_1d_component_type &rDesignVariable, Node &rNode, const double &rPertubationSize, Vector &rOutput, const ProcessInfo &rCurrentProcessInfo)
Definition: finite_difference_utility.h:50
std::size_t SizeType
Definition: finite_difference_utility.h:40
Variable< double > array_1d_component_type
Definition: finite_difference_utility.h:38
std::size_t IndexType
Definition: finite_difference_utility.h:39
void resize(std::size_t NewSize1, std::size_t NewSize2, bool preserve=0)
Definition: amatrix_interface.h:224
This class defines the node.
Definition: node.h:65
const PointType & GetInitialPosition() const
Definition: node.h:559
CoordinatesArrayType const & Coordinates() const
Definition: point.h:215
ProcessInfo holds the current value of different solution parameters.
Definition: process_info.h:59
#define KRATOS_CATCH(MoreInfo)
Definition: define.h:110
#define KRATOS_TRY
Definition: define.h:109
#define KRATOS_WARNING(label)
Definition: logger.h:265
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
T & noalias(T &TheMatrix)
Definition: amatrix_interface.h:484