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.
drag_response_function.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 #if !defined(KRATOS_DRAG_RESPONSE_FUNCTION_H_INCLUDED)
14 #define KRATOS_DRAG_RESPONSE_FUNCTION_H_INCLUDED
15 
16 // System includes
17 #include <string>
18 
19 // External includes
20 
21 // Project includes
22 #include "includes/define.h"
27 
28 namespace Kratos
29 {
32 
35 
37 
46 template <unsigned int TDim>
48 {
49 public:
52 
54 
58 
60  DragResponseFunction(Parameters Settings, ModelPart& rModelPart)
61  : mrModelPart(rModelPart)
62  {
63  KRATOS_TRY;
64 
65  Parameters default_settings(R"(
66  {
67  "structure_model_part_name": "PLEASE_SPECIFY_STRUCTURE_MODEL_PART",
68  "drag_direction": [1.0, 0.0, 0.0]
69  })");
70 
71  Settings.ValidateAndAssignDefaults(default_settings);
72 
73  mStructureModelPartName = Settings["structure_model_part_name"].GetString();
74 
75  if (Settings["drag_direction"].IsArray() == false ||
76  Settings["drag_direction"].size() != 3)
77  {
78  KRATOS_ERROR << "Invalid \"drag_direction\"." << std::endl;
79  }
80 
81  for (unsigned int d = 0; d < TDim; ++d)
82  mDragDirection[d] = Settings["drag_direction"][d].GetDouble();
83 
84  if (std::abs(norm_2(mDragDirection) - 1.0) > 1e-3)
85  {
86  const double magnitude = norm_2(mDragDirection);
87  if (magnitude == 0.0)
88  KRATOS_ERROR << "\"drag_direction\" is zero." << std::endl;
89 
90  KRATOS_WARNING("DragResponseFunction")
91  << "Non unit magnitude in \"drag_direction\"." << std::endl;
92  KRATOS_WARNING("DragResponseFunction") << "Normalizing ..." << std::endl;
93 
94  for (unsigned int d = 0; d < TDim; ++d)
95  mDragDirection[d] /= magnitude;
96  }
97 
98  KRATOS_CATCH("");
99  }
100 
103  {
104  }
105 
109 
113 
114  void Initialize() override
115  {
116  KRATOS_TRY;
117 
118  Check();
119 
120  VariableUtils().SetFlag(STRUCTURE, false, mrModelPart.Nodes());
122  STRUCTURE, true,
123  mrModelPart.GetSubModelPart(mStructureModelPartName).Nodes());
124 
125  KRATOS_CATCH("");
126  }
127 
128  void CalculateGradient(const Element& rAdjointElement,
129  const Matrix& rResidualGradient,
130  Vector& rResponseGradient,
131  const ProcessInfo& rProcessInfo) override
132  {
133  CalculateDragContribution(
134  rResidualGradient, rAdjointElement.GetGeometry().Points(), rResponseGradient);
135  }
136 
138  const Condition& rAdjointCondition,
139  const Matrix& rResidualGradient,
140  Vector& rResponseGradient,
141  const ProcessInfo& rProcessInfo) override
142  {
143  rResponseGradient.clear();
144  }
145 
146  void CalculateFirstDerivativesGradient(const Element& rAdjointElement,
147  const Matrix& rResidualGradient,
148  Vector& rResponseGradient,
149  const ProcessInfo& rProcessInfo) override
150  {
151  CalculateDragContribution(
152  rResidualGradient, rAdjointElement.GetGeometry().Points(), rResponseGradient);
153  }
154 
155  void CalculateFirstDerivativesGradient(const Condition& rAdjointCondition,
156  const Matrix& rResidualGradient,
157  Vector& rResponseGradient,
158  const ProcessInfo& rProcessInfo) override
159  {
160  rResponseGradient.clear();
161  }
162 
163  void CalculateSecondDerivativesGradient(const Element& rAdjointElement,
164  const Matrix& rResidualGradient,
165  Vector& rResponseGradient,
166  const ProcessInfo& rProcessInfo) override
167  {
168  CalculateDragContribution(
169  rResidualGradient, rAdjointElement.GetGeometry().Points(), rResponseGradient);
170  }
171 
172  void CalculateSecondDerivativesGradient(const Condition& rAdjointCondition,
173  const Matrix& rResidualGradient,
174  Vector& rResponseGradient,
175  const ProcessInfo& rProcessInfo) override
176  {
177  rResponseGradient.clear();
178  }
179 
180  void CalculatePartialSensitivity(Element& rAdjointElement,
181  const Variable<array_1d<double, 3>>& rVariable,
182  const Matrix& rSensitivityMatrix,
183  Vector& rSensitivityGradient,
184  const ProcessInfo& rProcessInfo) override
185  {
186  KRATOS_TRY;
187 
188  CalculateDragContribution(
189  rSensitivityMatrix, rAdjointElement.GetGeometry().Points(), rSensitivityGradient);
190 
191  KRATOS_CATCH("");
192  }
193 
194  void CalculatePartialSensitivity(Condition& rAdjointCondition,
195  const Variable<array_1d<double, 3>>& rVariable,
196  const Matrix& rSensitivityMatrix,
197  Vector& rSensitivityGradient,
198  const ProcessInfo& rProcessInfo) override
199  {
200  if (rSensitivityGradient.size() != rSensitivityMatrix.size1())
201  rSensitivityGradient.resize(rSensitivityMatrix.size1(), false);
202 
203  rSensitivityGradient.clear();
204  }
205 
206  double CalculateValue(ModelPart& rModelPart) override
207  {
208  KRATOS_TRY;
209  KRATOS_ERROR << "DragResponseFunction::CalculateValue(ModelPart& "
210  "rModelPart) is not implemented!!!\n";
211  KRATOS_CATCH("");
212  }
213 
215 
216 protected:
219 
223 
227 
229 
230 private:
233 
234  ModelPart& mrModelPart;
235  std::string mStructureModelPartName;
236  array_1d<double, TDim> mDragDirection;
237 
241 
245 
246  void Check()
247  {
248  KRATOS_TRY;
249 
250  if (mrModelPart.HasSubModelPart(mStructureModelPartName) == false)
251  KRATOS_ERROR << "No sub model part \"" << mStructureModelPartName
252  << "\"" << std::endl;
253 
254  KRATOS_CATCH("");
255  }
256 
257  void CalculateDragContribution(const Matrix& rDerivativesOfResidual,
258  const Element::NodesArrayType& rNodes,
259  Vector& rDerivativesOfDrag) const
260  {
261  constexpr std::size_t max_size = 50;
262  BoundedVector<double, max_size> drag_flag_vector(rDerivativesOfResidual.size2());
263  drag_flag_vector.clear();
264 
265  const unsigned num_nodes = rNodes.size();
266  const unsigned local_size = rDerivativesOfResidual.size2() / num_nodes;
267 
268  for (unsigned i_node = 0; i_node < num_nodes; ++i_node)
269  {
270  if (rNodes[i_node].Is(STRUCTURE))
271  {
272  for (unsigned d = 0; d < TDim; ++d)
273  drag_flag_vector[i_node * local_size + d] = mDragDirection[d];
274  }
275  }
276 
277  if (rDerivativesOfDrag.size() != rDerivativesOfResidual.size1())
278  rDerivativesOfDrag.resize(rDerivativesOfResidual.size1(), false);
279 
280  noalias(rDerivativesOfDrag) = prod(rDerivativesOfResidual, drag_flag_vector);
281  }
282 
284 };
285 
287 
289 
290 } /* namespace Kratos.*/
291 
292 #endif /* KRATOS_DRAG_RESPONSE_FUNCTION_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
A response function for drag.
Definition: drag_response_function.h:48
void CalculateFirstDerivativesGradient(const Condition &rAdjointCondition, const Matrix &rResidualGradient, Vector &rResponseGradient, const ProcessInfo &rProcessInfo) override
Calculate the local gradient w.r.t. first derivatives of primal solution.
Definition: drag_response_function.h:155
void CalculatePartialSensitivity(Condition &rAdjointCondition, const Variable< array_1d< double, 3 >> &rVariable, const Matrix &rSensitivityMatrix, Vector &rSensitivityGradient, const ProcessInfo &rProcessInfo) override
Calculate the partial sensitivity w.r.t. design variable.
Definition: drag_response_function.h:194
double CalculateValue(ModelPart &rModelPart) override
Calculate the scalar valued response function.
Definition: drag_response_function.h:206
void CalculateGradient(const Condition &rAdjointCondition, const Matrix &rResidualGradient, Vector &rResponseGradient, const ProcessInfo &rProcessInfo) override
Calculate the local gradient w.r.t. primal solution.
Definition: drag_response_function.h:137
~DragResponseFunction() override
Destructor.
Definition: drag_response_function.h:102
void Initialize() override
Definition: drag_response_function.h:114
void CalculateFirstDerivativesGradient(const Element &rAdjointElement, const Matrix &rResidualGradient, Vector &rResponseGradient, const ProcessInfo &rProcessInfo) override
Calculate the local gradient w.r.t. first derivatives of primal solution.
Definition: drag_response_function.h:146
void CalculateSecondDerivativesGradient(const Element &rAdjointElement, const Matrix &rResidualGradient, Vector &rResponseGradient, const ProcessInfo &rProcessInfo) override
Calculate the local gradient w.r.t. second derivatives of primal solution.
Definition: drag_response_function.h:163
void CalculateSecondDerivativesGradient(const Condition &rAdjointCondition, const Matrix &rResidualGradient, Vector &rResponseGradient, const ProcessInfo &rProcessInfo) override
Calculate the local gradient w.r.t. second derivatives of primal solution.
Definition: drag_response_function.h:172
void CalculateGradient(const Element &rAdjointElement, const Matrix &rResidualGradient, Vector &rResponseGradient, const ProcessInfo &rProcessInfo) override
Calculate the local gradient w.r.t. primal solution.
Definition: drag_response_function.h:128
KRATOS_CLASS_POINTER_DEFINITION(DragResponseFunction)
void CalculatePartialSensitivity(Element &rAdjointElement, const Variable< array_1d< double, 3 >> &rVariable, const Matrix &rSensitivityMatrix, Vector &rSensitivityGradient, const ProcessInfo &rProcessInfo) override
Calculate the partial sensitivity w.r.t. design variable.
Definition: drag_response_function.h:180
DragResponseFunction(Parameters Settings, ModelPart &rModelPart)
Constructor.
Definition: drag_response_function.h:60
Base class for all Elements.
Definition: element.h:60
Geometry< NodeType >::PointsArrayType NodesArrayType
definition of nodes container type, redefined from GeometryType
Definition: element.h:86
GeometryType & GetGeometry()
Returns the reference of the geometry.
Definition: geometrical_object.h:158
const PointsArrayType & Points() const
Definition: geometry.h:1768
void resize(std::size_t NewSize1, std::size_t NewSize2, bool preserve=0)
Definition: amatrix_interface.h:224
void clear()
Definition: amatrix_interface.h:284
This class aims to manage meshes for multi-physics simulations.
Definition: model_part.h:77
ModelPart & GetSubModelPart(std::string const &SubModelPartName)
Definition: model_part.cpp:2029
NodesContainerType & Nodes(IndexType ThisIndex=0)
Definition: model_part.h:507
This class provides to Kratos a data structure for I/O based on the standard of JSON.
Definition: kratos_parameters.h:59
void ValidateAndAssignDefaults(const Parameters &rDefaultParameters)
This function is designed to verify that the parameters under testing match the form prescribed by th...
Definition: kratos_parameters.cpp:1306
std::string GetString() const
This method returns the string contained in the current Parameter.
Definition: kratos_parameters.cpp:684
ProcessInfo holds the current value of different solution parameters.
Definition: process_info.h:59
Variable class contains all information needed to store and retrive data from a data container.
Definition: variable.h:63
This class implements a set of auxiliar, already parallelized, methods to perform some common tasks r...
Definition: variable_utils.h:63
void SetFlag(const Flags &rFlag, const bool FlagValue, TContainerType &rContainer)
Sets a flag according to a given status over a given container.
Definition: variable_utils.h:900
#define KRATOS_CATCH(MoreInfo)
Definition: define.h:110
#define KRATOS_TRY
Definition: define.h:109
#define KRATOS_ERROR
Definition: exception.h:161
#define KRATOS_WARNING(label)
Definition: logger.h:265
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
TExpressionType::data_type norm_2(AMatrix::MatrixExpression< TExpressionType, TCategory > const &TheExpression)
Definition: amatrix_interface.h:625
Internals::Matrix< double, AMatrix::dynamic, 1 > Vector
Definition: amatrix_interface.h:472
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
int local_size
Definition: generate_total_lagrangian_mixed_volumetric_strain_element.py:17
int d
Definition: ode_solve.py:397
e
Definition: run_cpp_mpi_tests.py:31