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.
velocity_pressure_norm_square_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: Suneth Warnakulasuriya
11 //
12 
13 #if !defined(KRATOS_VELOCITY_PRESSURE_NORM_SQUARE_RESPONSE_FUNCTION_H_INCLUDED)
14 #define KRATOS_VELOCITY_PRESSURE_NORM_SQUARE_RESPONSE_FUNCTION_H_INCLUDED
15 
16 // System includes
17 #include <string>
18 
19 // External includes
20 
21 // Project includes
22 #include "containers/model.h"
24 #include "includes/define.h"
26 #include "includes/model_part.h"
33 
34 // Application includes
36 
37 namespace Kratos
38 {
41 
44 
46 
59 {
60 public:
63 
65 
67 
68  using IndexType = std::size_t;
69 
71 
75 
78  Parameters Settings,
79  Model& rModel)
80  : mrModel(rModel)
81  {
82  KRATOS_TRY;
83 
84  Parameters default_settings(R"(
85  {
86  "main_model_part_name": "PLEASE_SPECIFY_MODEL_PART_NAME",
87  "norm_model_part_name": "PLEASE_SPECIFY_MODEL_PART_NAME",
88  "velocity_norm_factor": 1.0,
89  "pressure_norm_factor": 1.0,
90  "entities" : ["elements"]
91  })");
92 
93  Settings.ValidateAndAssignDefaults(default_settings);
94 
95  mMainModelPartName = Settings["main_model_part_name"].GetString();
96  mNormModelPartName = Settings["norm_model_part_name"].GetString();
97  mVelocityNormFactor = Settings["velocity_norm_factor"].GetDouble();
98  mPressureNormFactor = Settings["pressure_norm_factor"].GetDouble();
99 
100  mIsElements = false;
101  mIsConditions = false;
102  const auto& entities = Settings["entities"].GetStringArray();
103  for (const auto& entity : entities) {
104  if (entity == "elements") {
105  mIsElements = true;
106  } else if (entity == "conditions") {
107  mIsConditions = true;
108  } else {
109  KRATOS_ERROR << "Unsupported entity type provided under "
110  "\"entities\". Supported entity types are:\n"
111  << "\t elements\n"
112  << "\t conditions\n.";
113  }
114  }
115 
116  KRATOS_CATCH("");
117  }
118 
121  {
122  }
123 
127 
128  void Initialize() override
129  {
130  KRATOS_TRY;
131 
132  auto& r_main_model_part = mrModel.GetModelPart(mMainModelPartName);
133  auto& r_norm_model_part = mrModel.GetModelPart(mNormModelPartName);
134 
135  if (mIsElements && !mIsConditions) {
136  const IndexType number_of_entities =
137  r_norm_model_part.GetCommunicator().GlobalNumberOfElements();
138 
139  if (number_of_entities == 0) {
140  KRATOS_WARNING("VelocityPressureNormSquareResponseFunction")
141  << "Only \"elements\" are chosen as entities "
142  "and no elements are found in "
143  << r_norm_model_part.Name() << " model part. Therefore trying to compute response function on conditions.\n";
144  mIsConditions = true;
145  mIsElements = false;
146  }
147  }
148 
149  VariableUtils().SetFlag(STRUCTURE, false, r_main_model_part.Elements());
150  VariableUtils().SetFlag(STRUCTURE, false, r_main_model_part.Conditions());
151 
152  IndexType total_entities = 0;
153  if (mIsElements) {
154  total_entities += r_norm_model_part.GetCommunicator().GlobalNumberOfElements();
155  VariableUtils().SetFlag(STRUCTURE, true, r_norm_model_part.Elements());
156  }
157 
158  if (mIsConditions) {
159  total_entities += r_norm_model_part.GetCommunicator().GlobalNumberOfConditions();
160  VariableUtils().SetFlag(STRUCTURE, true, r_norm_model_part.Conditions());
161  }
162 
163  KRATOS_ERROR_IF(total_entities == 0)
164  << "No entities were found in "
165  << r_norm_model_part.Name() << " to calculate response function value. Please check model part.\n";
166 
167  KRATOS_CATCH("");
168  }
169 
171  const Element& rAdjointElement,
172  const Matrix& rResidualGradient,
173  Vector& rResponseGradient,
174  const ProcessInfo& rProcessInfo) override
175  {
176  rResponseGradient.clear();
177  }
178 
180  const Condition& rAdjointCondition,
181  const Matrix& rResidualGradient,
182  Vector& rResponseGradient,
183  const ProcessInfo& rProcessInfo) override
184  {
185  rResponseGradient.clear();
186  }
187 
189  const Element& rAdjointElement,
190  const Matrix& rResidualGradient,
191  Vector& rResponseGradient,
192  const ProcessInfo& rProcessInfo) override
193  {
194  CalculateEntityFirstDerivatives<Element>(
195  rAdjointElement, rResidualGradient, rResponseGradient, rProcessInfo);
196  }
197 
199  const Condition& rAdjointCondition,
200  const Matrix& rResidualGradient,
201  Vector& rResponseGradient,
202  const ProcessInfo& rProcessInfo) override
203  {
204  CalculateEntityFirstDerivatives<Condition>(
205  rAdjointCondition, rResidualGradient, rResponseGradient, rProcessInfo);
206  }
207 
209  const Element& rAdjointElement,
210  const Matrix& rResidualGradient,
211  Vector& rResponseGradient,
212  const ProcessInfo& rProcessInfo) override
213  {
214  rResponseGradient.clear();
215  }
216 
218  const Condition& rAdjointCondition,
219  const Matrix& rResidualGradient,
220  Vector& rResponseGradient,
221  const ProcessInfo& rProcessInfo) override
222  {
223  rResponseGradient.clear();
224  }
225 
227  Element& rAdjointElement,
228  const Variable<array_1d<double, 3>>& rVariable,
229  const Matrix& rSensitivityMatrix,
230  Vector& rSensitivityGradient,
231  const ProcessInfo& rProcessInfo) override
232  {
233  CalculateEntitySensitivityDerivatives(rAdjointElement, rSensitivityGradient, rProcessInfo);
234  }
235 
237  Condition& rAdjointCondition,
238  const Variable<array_1d<double, 3>>& rVariable,
239  const Matrix& rSensitivityMatrix,
240  Vector& rSensitivityGradient,
241  const ProcessInfo& rProcessInfo) override
242  {
243  CalculateEntitySensitivityDerivatives(rAdjointCondition, rSensitivityGradient, rProcessInfo);
244  }
245 
246  double CalculateValue(ModelPart& rModelPart) override
247  {
248  KRATOS_TRY
249 
250  double norm_square = 0.0;
251  if (mIsElements) {
252  norm_square += block_for_each<SumReduction<double>>(
253  rModelPart.Elements(), Matrix(),
254  [&](ModelPart::ElementType& rElement, Matrix& rMatrix) -> double {
255  return CalculateEntityValue<Element>(rElement, rMatrix);
256  });
257  }
258 
259  if (mIsConditions) {
260  norm_square += block_for_each<SumReduction<double>>(
261  rModelPart.Conditions(), Matrix(),
262  [&](ModelPart::ConditionType& rCondition, Matrix& rMatrix) -> double {
263  return CalculateEntityValue<Condition>(rCondition, rMatrix);
264  });
265  }
266 
267  return rModelPart.GetCommunicator().GetDataCommunicator().SumAll(norm_square);
268 
269  KRATOS_CATCH("");
270  }
271 
273 
274 private:
277 
278  Model& mrModel;
279 
280  std::string mMainModelPartName;
281  std::string mNormModelPartName;
282 
283  double mVelocityNormFactor;
284  double mPressureNormFactor;
285  bool mIsElements;
286  bool mIsConditions;
287 
291 
292  template<class TEntityType>
293  double CalculateEntityValue(
294  const TEntityType& rEntity,
295  Matrix& rShapeFunctions) const
296  {
297  if (rEntity.Is(STRUCTURE)) {
298  const auto& r_geometry = rEntity.GetGeometry();
299 
300  this->CalculateGeometryData(r_geometry, rShapeFunctions);
301  const Vector& N = row(rShapeFunctions, 0);
302 
303  const double volume = r_geometry.DomainSize();
304 
305  double pressure;
308  r_geometry, N, std::tie(velocity, VELOCITY), std::tie(pressure, PRESSURE));
309 
310  return mVelocityNormFactor * std::pow(volume * norm_2(velocity), 2) +
311  mPressureNormFactor * std::pow(volume * pressure, 2);
312  } else {
313  return 0;
314  }
315  }
316 
317  template<class TEntityType>
318  void CalculateEntityFirstDerivatives(
319  const TEntityType& rEntity,
320  const Matrix& rResidualGradient,
321  Vector& rResponseGradient,
322  const ProcessInfo& rProcessInfo) const
323  {
324  KRATOS_TRY
325 
326  const auto& r_geometry = rEntity.GetGeometry();
327  const IndexType number_of_nodes = r_geometry.PointsNumber();
328  const IndexType domain_size = rProcessInfo[DOMAIN_SIZE];
329  const IndexType block_size = rResidualGradient.size2() / number_of_nodes;
330  const IndexType skip_size = block_size - domain_size - 1;
331 
332  if (rResponseGradient.size() != rResidualGradient.size2()) {
333  rResponseGradient.resize(rResidualGradient.size2());
334  }
335 
336  if (rEntity.Is(STRUCTURE)) {
337  Matrix shape_functions;
338  this->CalculateGeometryData(r_geometry, shape_functions);
339  const Vector& N = row(shape_functions, 0);
340 
341  array_1d<double, 3> velocity;
342  double pressure;
344  r_geometry, N, std::tie(velocity, VELOCITY), std::tie(pressure, PRESSURE));
345 
346  const double volume_2 = std::pow(r_geometry.DomainSize(), 2);
347 
348  const double coeff_1 = volume_2 * 2.0 * mVelocityNormFactor;
349  const double coeff_2 = volume_2 * 2.0 * mPressureNormFactor * pressure;
350 
351  IndexType local_index = 0;
352  for (IndexType c = 0; c < number_of_nodes; ++c) {
353  // adding velocity derivatives
354  for (IndexType k = 0; k < domain_size; ++k) {
355  rResponseGradient[local_index++] = coeff_1 * N[c] * velocity[k];
356  }
357 
358  // adding pressure derivatives
359  rResponseGradient[local_index] = coeff_2 * N[c];
360 
361  // skipping rest of the derivatives if they are used
362  local_index += skip_size + 1;
363  }
364  } else {
365  rResponseGradient.clear();
366  }
367 
368  KRATOS_CATCH("");
369  }
370 
371  template<class TEntityType>
372  void CalculateEntitySensitivityDerivatives(
373  const TEntityType& rEntity,
374  Vector& rResponseGradient,
375  const ProcessInfo& rProcessInfo) const
376  {
377  KRATOS_TRY
378 
379  const auto& r_geometry = rEntity.GetGeometry();
380  const IndexType number_of_nodes = r_geometry.PointsNumber();
381  const IndexType domain_size = rProcessInfo[DOMAIN_SIZE];
382  const IndexType local_size = domain_size * number_of_nodes;
383 
384  if (rResponseGradient.size() != local_size) {
385  rResponseGradient.resize(local_size);
386  }
387 
388  if (rEntity.Is(STRUCTURE)) {
389  Matrix shape_functions;
390  this->CalculateGeometryData(r_geometry, shape_functions);
391  const Vector& N = row(shape_functions, 0);
392 
393  const double volume = r_geometry.DomainSize();
394 
395  double pressure;
396  array_1d<double, 3> velocity;
398  r_geometry, N, std::tie(velocity, VELOCITY), std::tie(pressure, PRESSURE));
399 
400  const double lx = r_geometry[0].X() - r_geometry[1].X();
401  const double ly = r_geometry[0].Y() - r_geometry[1].Y();
402 
403  for (IndexType c = 0; c < number_of_nodes; ++c) {
404  for (IndexType k = 0; k < domain_size; ++k) {
405  const double lx_derivative =
406  (c == 0 && k == 0) * (1.0) + (c == 1 && k == 0) * (-1.0);
407  const double ly_derivative =
408  (c == 0 && k == 1) * (1.0) + (c == 1 && k == 1) * (-1.0);
409  const double volume_derivative =
410  (1.0 / volume) * (lx * lx_derivative + ly * ly_derivative);
411 
412  double value = 0.0;
413  value += mVelocityNormFactor * std::pow(norm_2(velocity), 2) *
414  2 * volume * volume_derivative;
415  value += mPressureNormFactor * std::pow(pressure, 2) * 2 *
416  volume * volume_derivative;
417 
418  rResponseGradient[c * domain_size + k] = value;
419  }
420  }
421  } else {
422  rResponseGradient.clear();
423  }
424 
425  KRATOS_CATCH("");
426  }
427 
428  void CalculateGeometryData(
429  const GeometryType& rGeometry,
430  Matrix& rNContainer) const
431  {
432  const auto r_integrations_method = GeometryData::IntegrationMethod::GI_GAUSS_1;
433  const unsigned int number_of_gauss_points =
434  rGeometry.IntegrationPointsNumber(r_integrations_method);
435 
436  const std::size_t number_of_nodes = rGeometry.PointsNumber();
437 
438  if (rNContainer.size1() != number_of_gauss_points ||
439  rNContainer.size2() != number_of_nodes) {
440  rNContainer.resize(number_of_gauss_points, number_of_nodes, false);
441  }
442  rNContainer = rGeometry.ShapeFunctionsValues(r_integrations_method);
443  }
444 
446 };
447 
449 
451 
452 } /* namespace Kratos.*/
453 
454 #endif /* KRATOS_VELOCITY_PRESSURE_NORM_SQUARE_RESPONSE_FUNCTION_H_INCLUDED defined */
A base class for adjoint response functions.
Definition: adjoint_response_function.h:39
SizeType GlobalNumberOfElements() const
Definition: communicator.cpp:106
virtual const DataCommunicator & GetDataCommunicator() const
Definition: communicator.cpp:340
Base class for all Conditions.
Definition: condition.h:59
Base class for all Elements.
Definition: element.h:60
Geometry< NodeType > GeometryType
definition of the geometry type with given NodeType
Definition: element.h:83
void clear()
Definition: amatrix_interface.h:284
This class aims to manage different model parts across multi-physics simulations.
Definition: model.h:60
ModelPart & GetModelPart(const std::string &rFullModelPartName)
This method returns a model part given a certain name.
Definition: model.cpp:107
This class aims to manage meshes for multi-physics simulations.
Definition: model_part.h:77
Element ElementType
Definition: model_part.h:120
Communicator & GetCommunicator()
Definition: model_part.h:1821
ConditionsContainerType & Conditions(IndexType ThisIndex=0)
Definition: model_part.h:1381
ElementsContainerType & Elements(IndexType ThisIndex=0)
Definition: model_part.h:1189
This class provides to Kratos a data structure for I/O based on the standard of JSON.
Definition: kratos_parameters.h:59
std::vector< std::string > GetStringArray() const
This method returns the array of strings in the current Parameter.
Definition: kratos_parameters.cpp:693
double GetDouble() const
This method returns the double contained in the current Parameter.
Definition: kratos_parameters.cpp:657
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
A response function for norm square calculation.
Definition: velocity_pressure_norm_square_response_function.h:59
void Initialize() override
Definition: velocity_pressure_norm_square_response_function.h:128
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: velocity_pressure_norm_square_response_function.h:226
double CalculateValue(ModelPart &rModelPart) override
Calculate the scalar valued response function.
Definition: velocity_pressure_norm_square_response_function.h:246
std::size_t IndexType
Definition: velocity_pressure_norm_square_response_function.h:68
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: velocity_pressure_norm_square_response_function.h:236
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: velocity_pressure_norm_square_response_function.h:208
typename ElementType::GeometryType GeometryType
Definition: velocity_pressure_norm_square_response_function.h:66
void CalculateGradient(const Condition &rAdjointCondition, const Matrix &rResidualGradient, Vector &rResponseGradient, const ProcessInfo &rProcessInfo) override
Calculate the local gradient w.r.t. primal solution.
Definition: velocity_pressure_norm_square_response_function.h:179
VelocityPressureNormSquareResponseFunction(Parameters Settings, Model &rModel)
Constructor.
Definition: velocity_pressure_norm_square_response_function.h:77
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: velocity_pressure_norm_square_response_function.h:217
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: velocity_pressure_norm_square_response_function.h:188
void CalculateGradient(const Element &rAdjointElement, const Matrix &rResidualGradient, Vector &rResponseGradient, const ProcessInfo &rProcessInfo) override
Calculate the local gradient w.r.t. primal solution.
Definition: velocity_pressure_norm_square_response_function.h:170
~VelocityPressureNormSquareResponseFunction() override
Destructor.
Definition: velocity_pressure_norm_square_response_function.h:120
KRATOS_CLASS_POINTER_DEFINITION(VelocityPressureNormSquareResponseFunction)
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: velocity_pressure_norm_square_response_function.h:198
#define KRATOS_CATCH(MoreInfo)
Definition: define.h:110
#define KRATOS_TRY
Definition: define.h:109
static void EvaluateInPoint(const GeometryType &rGeometry, const Vector &rShapeFunction, const int Step, const TRefVariableValuePairArgs &... rValueVariablePairs)
Evaluates given list of variable pairs at gauss point locations at step.
Definition: fluid_calculation_utilities.h:75
#define KRATOS_ERROR
Definition: exception.h:161
#define KRATOS_ERROR_IF(conditional)
Definition: exception.h:162
#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::MatrixRow< const TExpressionType > row(AMatrix::MatrixExpression< TExpressionType, TCategory > const &TheExpression, std::size_t RowIndex)
Definition: amatrix_interface.h:649
float velocity
Definition: PecletTest.py:54
int domain_size
Definition: face_heat.py:4
int local_size
Definition: generate_total_lagrangian_mixed_volumetric_strain_element.py:17
int block_size
Definition: generate_total_lagrangian_mixed_volumetric_strain_element.py:16
c
Definition: generate_weakly_compressible_navier_stokes_element.py:108
int k
Definition: quadrature.py:595
N
Definition: sensitivityMatrix.py:29
volume
Definition: sp_statistics.py:15
float pressure
Definition: test_pureconvectionsolver_benchmarking.py:101