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.
mass_response_function_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: Baumgaertner Daniel, https://github.com/dbaumgaertner
10 // Geiser Armin, https://github.com/armingeiser
11 //
12 
13 #pragma once
14 
15 // ------------------------------------------------------------------------------
16 // System includes
17 // ------------------------------------------------------------------------------
18 #include <iostream>
19 #include <string>
20 
21 // ------------------------------------------------------------------------------
22 // External includes
23 // ------------------------------------------------------------------------------
24 
25 // ------------------------------------------------------------------------------
26 // Project includes
27 // ------------------------------------------------------------------------------
28 #include "includes/define.h"
30 #include "includes/model_part.h"
35 
36 // ==============================================================================
37 
38 namespace Kratos
39 {
40 
43 
47 
51 
55 
59 
61 
66 {
67 public:
70 
72 
75 
79 
82  : mrModelPart(model_part)
83  {
84  std::string gradient_mode = responseSettings["gradient_mode"].GetString();
85  if (gradient_mode.compare("finite_differencing") == 0)
86  {
87  double delta = responseSettings["step_size"].GetDouble();
88  mDelta = delta;
89  }
90  else
91  KRATOS_ERROR << "Specified gradient_mode '" << gradient_mode << "' not recognized. The only option is: finite_differencing" << std::endl;
92  }
93 
96  {
97  }
98 
102 
106 
107  // ==============================================================================
108  void Initialize()
109  {}
110 
111  // --------------------------------------------------------------------------
112  double CalculateValue()
113  {
114  KRATOS_TRY;
115 
116  // Variables
117  double total_mass = 0.0;
118  const std::size_t domain_size = mrModelPart.GetProcessInfo()[DOMAIN_SIZE];
119 
120  // Incremental computation of total mass
121  for (auto& elem_i : mrModelPart.Elements()){
122  const bool element_is_active = elem_i.IsDefined(ACTIVE) ? elem_i.Is(ACTIVE) : true;
123  if(element_is_active)
125  }
126 
127  return total_mass;
128 
129  KRATOS_CATCH("");
130  }
131 
132  // --------------------------------------------------------------------------
134  {
135  KRATOS_TRY;
136 
137  // Formula computed in general notation:
138  // \frac{dm_{total}}{dx}
139 
140  // First gradients are initialized
141  VariableUtils().SetHistoricalVariableToZero(SHAPE_SENSITIVITY, mrModelPart.Nodes());
142 
143  const std::size_t domain_size = mrModelPart.GetProcessInfo()[DOMAIN_SIZE];
144 
145  // Start process to identify element neighbors for every node
146  FindNodalNeighboursProcess neighorFinder(mrModelPart);
147  neighorFinder.Execute();
148 
149  for(auto& node_i : mrModelPart.Nodes())
150  {
151  // Get all neighbor elements of current node
152  GlobalPointersVector<Element >& ng_elem = node_i.GetValue(NEIGHBOUR_ELEMENTS);
153 
154  // Compute total mass of all neighbor elements before finite differencing
155  double mass_before_fd = 0.0;
156  for(std::size_t i = 0; i < ng_elem.size(); i++)
157  {
158  Element& ng_elem_i = ng_elem[i];
159  const bool element_is_active = ng_elem_i.IsDefined(ACTIVE) ? ng_elem_i.Is(ACTIVE) : true;
160 
161  // Compute mass according to element dimension
162  if(element_is_active)
164  }
165 
166  // Compute sensitivities using finite differencing in the three spatial direction
167  array_3d gradient(3, 0.0);
168 
169  // Apply pertubation in X-direction and recompute total mass of all neighbor elements
170  double mass_after_fd = 0.0;
171  node_i.X() += mDelta;
172  node_i.X0() += mDelta;
173  for(std::size_t i = 0; i < ng_elem.size(); i++)
174  {
175  Element& ng_elem_i = ng_elem[i];
176  const bool element_is_active = ng_elem_i.IsDefined(ACTIVE) ? ng_elem_i.Is(ACTIVE) : true;
177 
178  // Compute mass according to element dimension
179  if(element_is_active)
181  }
182  gradient[0] = (mass_after_fd - mass_before_fd) / mDelta;
183  node_i.X() -= mDelta;
184  node_i.X0() -= mDelta;
185 
186  // Apply pertubation in Y-direction and recompute total mass of all neighbor elements
187  mass_after_fd = 0.0;
188  node_i.Y() += mDelta;
189  node_i.Y0() += mDelta;
190  for(std::size_t i = 0; i < ng_elem.size(); i++)
191  {
192  Element& ng_elem_i = ng_elem[i];
193  const bool element_is_active = ng_elem_i.IsDefined(ACTIVE) ? ng_elem_i.Is(ACTIVE) : true;
194 
195  // Compute mass according to element dimension
196  if(element_is_active)
198  }
199  gradient[1] = (mass_after_fd - mass_before_fd) / mDelta;
200  node_i.Y() -= mDelta;
201  node_i.Y0() -= mDelta;
202 
203  // Apply pertubation in Z-direction and recompute total mass of all neighbor elements
204  mass_after_fd = 0.0;
205  node_i.Z() += mDelta;
206  node_i.Z0() += mDelta;
207  for(std::size_t i = 0; i < ng_elem.size(); i++)
208  {
209  Element& ng_elem_i = ng_elem[i];
210  const bool element_is_active = ng_elem_i.IsDefined(ACTIVE) ? ng_elem_i.Is(ACTIVE) : true;
211 
212  // Compute mass according to element dimension
213  if(element_is_active)
215  }
216  gradient[2] = (mass_after_fd - mass_before_fd) / mDelta;
217  node_i.Z() -= mDelta;
218  node_i.Z0() -= mDelta;
219 
220  // Compute sensitivity
221  noalias(node_i.FastGetSolutionStepValue(SHAPE_SENSITIVITY)) = gradient;
222 
223  }
224 
225  KRATOS_CATCH("");
226  }
227 
228  // ==============================================================================
229 
233 
237 
241 
243  std::string Info() const
244  {
245  return "MassResponseFunctionUtility";
246  }
247 
249  virtual void PrintInfo(std::ostream &rOStream) const
250  {
251  rOStream << "MassResponseFunctionUtility";
252  }
253 
255  virtual void PrintData(std::ostream &rOStream) const
256  {
257  }
258 
262 
264 
265 protected:
268 
272 
276 
280 
281  // ==============================================================================
282 
286 
290 
294 
296 
297 private:
300 
304 
305  ModelPart &mrModelPart;
306  double mDelta;
307 
311 
315 
319 
323 
327 
329  // MassResponseFunctionUtility& operator=(MassResponseFunctionUtility const& rOther);
330 
332  // MassResponseFunctionUtility(MassResponseFunctionUtility const& rOther);
333 
335 
336 }; // Class MassResponseFunctionUtility
337 
339 
342 
346 
348 
349 } // namespace Kratos.
Base class for all Elements.
Definition: element.h:60
This method allows to look for neighbours in a triangular or tetrahedral mesh.
Definition: find_nodal_neighbours_process.h:59
void Execute() override
This method esxecutes the neighbour search.
Definition: find_nodal_neighbours_process.cpp:49
bool IsDefined(Flags const &rOther) const
Definition: flags.h:279
bool Is(Flags const &rOther) const
Definition: flags.h:274
size_type size() const
Definition: global_pointers_vector.h:307
Short class definition.
Definition: mass_response_function_utility.h:66
std::string Info() const
Turn back information as a string.
Definition: mass_response_function_utility.h:243
void CalculateGradient()
Definition: mass_response_function_utility.h:133
void Initialize()
Definition: mass_response_function_utility.h:108
array_1d< double, 3 > array_3d
Definition: mass_response_function_utility.h:71
double CalculateValue()
Definition: mass_response_function_utility.h:112
virtual void PrintData(std::ostream &rOStream) const
Print object's data.
Definition: mass_response_function_utility.h:255
KRATOS_CLASS_POINTER_DEFINITION(MassResponseFunctionUtility)
Pointer definition of MassResponseFunctionUtility.
virtual ~MassResponseFunctionUtility()
Destructor.
Definition: mass_response_function_utility.h:95
MassResponseFunctionUtility(ModelPart &model_part, Parameters responseSettings)
Default constructor.
Definition: mass_response_function_utility.h:81
virtual void PrintInfo(std::ostream &rOStream) const
Print information about this object.
Definition: mass_response_function_utility.h:249
This class aims to manage meshes for multi-physics simulations.
Definition: model_part.h:77
ProcessInfo & GetProcessInfo()
Definition: model_part.h:1746
ElementsContainerType & Elements(IndexType ThisIndex=0)
Definition: model_part.h:1189
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
double GetDouble() const
This method returns the double contained in the current Parameter.
Definition: kratos_parameters.cpp:657
std::string GetString() const
This method returns the string contained in the current Parameter.
Definition: kratos_parameters.cpp:684
static double CalculateElementMass(Element &rElement, const std::size_t DomainSize)
Definition: total_structural_mass_process.cpp:34
This class implements a set of auxiliar, already parallelized, methods to perform some common tasks r...
Definition: variable_utils.h:63
void SetHistoricalVariableToZero(const Variable< TType > &rVariable, NodesContainerType &rNodes)
Sets the nodal value of any variable to zero.
Definition: variable_utils.h:757
#define KRATOS_CATCH(MoreInfo)
Definition: define.h:110
#define KRATOS_TRY
Definition: define.h:109
#define KRATOS_ERROR
Definition: exception.h:161
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
T & noalias(T &TheMatrix)
Definition: amatrix_interface.h:484
int domain_size
Definition: face_heat.py:4
model_part
Definition: face_heat.py:14
integer i
Definition: TensorModule.f:17
double precision, dimension(3, 3), public delta
Definition: TensorModule.f:16