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.
mesh_error_calculation_utilities.hpp
Go to the documentation of this file.
1 //
2 // Project Name: KratosDelaunayMeshingApplication $
3 // Created by: $Author: JMCarbonell $
4 // Last modified by: $Co-Author: $
5 // Date: $Date: April 2018 $
6 // Revision: $Revision: 0.0 $
7 //
8 //
9 
10 #if !defined(KRATOS_MESH_ERROR_CALCULATION_UTILITIES_H_INCLUDED)
11 #define KRATOS_MESH_ERROR_CALCULATION_UTILITIES_H_INCLUDED
12 
13 // System includes
14 
15 // External includes
16 #include "utilities/timer.h"
17 
18 // Project includes
19 #include "includes/variables.h"
20 #include "includes/model_part.h"
22 
24 
25 namespace Kratos
26 {
29 
33 
37 
41 
45 
47 
50  {
51  public:
57 
64 
67 
70 
74 
78 
79  //**************************************************************************
80  //**************************************************************************
81 
82  void NodalErrorCalculation(ModelPart &rModelPart, std::vector<double> &rNodalError, std::vector<int> &rIds, const Variable<double> &rVariable)
83  {
85 
86  std::vector<double> ElementalError;
87  std::vector<int> elems_ids;
88 
89  ElementalErrorCalculation(rModelPart, ElementalError, elems_ids, rVariable);
90 
91  if (!rNodalError.size())
92  rNodalError.resize(rModelPart.NumberOfNodes() + 1);
93 
94  std::fill(rNodalError.begin(), rNodalError.end(), 100);
95 
96  if (!rIds.size())
97  rIds.resize(MesherUtilities::GetMaxNodeId(rModelPart) + 1);
98 
99  std::fill(rIds.begin(), rIds.end(), 0);
100 
101  double NodalMeanError = 0;
102 
103  int id = 1;
104  for (auto &i_node : rModelPart.Nodes())
105  {
106  if (i_node.IsNot(NEW_ENTITY))
107  { // && i_node.IsNot(STRUCTURE)){
108 
109  ElementWeakPtrVectorType &nElements = i_node.GetValue(NEIGHBOUR_ELEMENTS);
110 
111  NodalMeanError = 0;
112  for (auto &i_nelem : nElements)
113  {
114  NodalMeanError += ElementalError[elems_ids[i_nelem.Id()]];
115  }
116 
117  rIds[i_node.Id()] = id;
118  rNodalError[id] = NodalMeanError / double(nElements.size());
119  }
120  else
121  {
122 
123  rIds[i_node.Id()] = id;
124  rNodalError[id] = 0;
125  }
126 
127  id++;
128  }
129 
130  KRATOS_CATCH("")
131  }
132 
133  //**************************************************************************
134  //**************************************************************************
135 
136  void ElementalErrorCalculation(ModelPart &rModelPart, std::vector<double> &rElementalError, std::vector<int> &rIds, const Variable<double> &rVariable)
137  {
138  KRATOS_TRY
139 
140  const ProcessInfo &CurrentProcessInfo = rModelPart.GetProcessInfo();
141 
142  std::vector<double> ElementVariable(MesherUtilities::GetMaxElementId(rModelPart) + 1);
143  std::fill(ElementVariable.begin(), ElementVariable.end(), 0);
144 
145  std::vector<int> elems_ids;
146  elems_ids.resize(MesherUtilities::GetMaxElementId(rModelPart) + 1); //mesh 0
147  std::fill(elems_ids.begin(), elems_ids.end(), 0);
148 
149  double VariableMax = std::numeric_limits<double>::min();
150  double VariableMin = std::numeric_limits<double>::max();
151 
152  std::vector<double> Value(1);
153 
154  unsigned int id = 1;
155  for (ModelPart::ElementsContainerType::const_iterator ie = rModelPart.ElementsBegin(); ie != rModelPart.ElementsEnd(); ++ie)
156  {
157  (ie)->CalculateOnIntegrationPoints(rVariable, Value, CurrentProcessInfo);
158 
159  elems_ids[ie->Id()] = id;
160  ElementVariable[id] = Value[0];
161 
162  if (ElementVariable[id] > VariableMax)
163  VariableMax = ElementVariable[id];
164 
165  if (ElementVariable[id] < VariableMin)
166  VariableMin = ElementVariable[id];
167 
168  // if( ElementVariable[id] == 0 )
169  // std::cout<<" ELEMENT ["<<ie->Id()<<"] : "<<ElementVariable[id]<<std::endl;
170 
171  id++;
172  }
173 
174  std::vector<double> NodalError(rModelPart.NumberOfNodes() + 1);
175  std::fill(NodalError.begin(), NodalError.end(), 0);
176 
177  std::vector<int> nodes_ids(MesherUtilities::GetMaxNodeId(rModelPart) + 1); //mesh 0
178  std::fill(nodes_ids.begin(), nodes_ids.end(), 0);
179 
180  double PatchSize = 0;
181  double PatchError = 0;
182 
183  double Size = 0;
184  double Error = 0;
185 
186  id = 1;
187  for (auto &i_node : rModelPart.Nodes())
188  {
189 
190  if (i_node.IsNot(NEW_ENTITY))
191  { // && i_node->IsNot(STRUCTURE)){
192 
193  PatchSize = 0;
194  PatchError = 0;
195 
196  ElementWeakPtrVectorType &nElements = i_node.GetValue(NEIGHBOUR_ELEMENTS);
197 
198  for (auto &i_nelem : nElements)
199  {
200 
201  Geometry<Node> &rGeometry = i_nelem.GetGeometry();
202 
203  Size = rGeometry.DomainSize(); //Area(); or Volume();
204  Error = ElementVariable[elems_ids[i_nelem.Id()]] * Size;
205 
206  PatchSize += Size;
207  PatchError += Error;
208  }
209 
210  if (PatchSize != 0)
211  {
212  nodes_ids[i_node.Id()] = id;
213  NodalError[id] = PatchError / PatchSize;
214  //std::cout<<" Node ["<<i_node->Id()<<"] : ( NodalError: "<<NodalError[id]<<", PatchError: "<<" PatchSize:"<<PatchSize<<std::endl;
215  }
216  else
217  {
218  std::cout << " WARNING : Size surrounding node: " << i_node.Id() << " is null " << std::endl;
219  }
220  }
221  else
222  {
223  nodes_ids[i_node.Id()] = id;
224  NodalError[id] = 0;
225  }
226 
227  id++;
228  }
229 
230  rElementalError.resize(MesherUtilities::GetMaxElementId(rModelPart) + 1);
231  std::fill(rElementalError.begin(), rElementalError.end(), 100);
232 
233  rIds.resize(MesherUtilities::GetMaxElementId(rModelPart) + 1); //mesh 0
234  std::fill(rIds.begin(), rIds.end(), 0);
235 
236  double VariableVariation = VariableMax - VariableMin;
237 
238  if (VariableVariation == 0)
239  {
240  VariableVariation = 1;
241  std::cout << " WARNING: " << rVariable << " min-max errors are the same ( MinVar= " << VariableMin << ", MaxVar= " << VariableMax << " )" << std::endl;
242  }
243  else
244  {
245 
246  if (GetEchoLevel() > 0)
247  std::cout << " Variable errors ( MinVar= " << VariableMin << ", MaxVar= " << VariableMax << " )" << std::endl;
248 
249  id = 1;
250  for (auto &i_elem : rModelPart.Elements())
251  {
252  PointsArrayType &vertices = i_elem.GetGeometry().Points();
253 
254  PatchError = 0;
255 
256  unsigned int NumberOfVertices = vertices.size();
257  for (unsigned int i = 0; i < NumberOfVertices; ++i)
258  {
259  PatchError += NodalError[nodes_ids[vertices[i].Id()]];
260  }
261 
262  if (NumberOfVertices != 0)
263  {
264  PatchError /= double(NumberOfVertices);
265  }
266  else
267  {
268  std::cout << " ERROR ME: Number of Vertices of the Element: " << i_elem.Id() << " is null " << std::endl;
269  }
270 
271  rIds[i_elem.Id()] = id;
272  rElementalError[id] = fabs((PatchError - ElementVariable[id]) / VariableVariation) * 100;
273 
274  id++;
275  }
276  }
277 
278  KRATOS_CATCH("")
279  }
280 
284  virtual void SetEchoLevel(int Level)
285  {
286  mEchoLevel = Level;
287  }
288 
290  {
291  return mEchoLevel;
292  }
293 
307 
308  protected:
330 
331  private:
337 
338  int mEchoLevel;
339 
356 
357  }; // Class MeshErrorCalculationUtilities
358 
365 
367 
368 } // namespace Kratos.
369 
370 #endif // KRATOS_MESH_ERROR_CALCULATION_UTILITIES_H_INCLUDED defined
Geometry base class.
Definition: geometry.h:71
PointerVector< TPointType > PointsArrayType
Definition: geometry.h:118
virtual double DomainSize() const
This method calculate and return length, area or volume of this geometry depending to it's dimension.
Definition: geometry.h:1371
This class is a vector which stores global pointers.
Definition: global_pointers_vector.h:61
size_type size() const
Definition: global_pointers_vector.h:307
Short class definition.
Definition: mesh_error_calculation_utilities.hpp:50
ModelPart::NodesContainerType NodesContainerType
Definition: mesh_error_calculation_utilities.hpp:55
void NodalErrorCalculation(ModelPart &rModelPart, std::vector< double > &rNodalError, std::vector< int > &rIds, const Variable< double > &rVariable)
Definition: mesh_error_calculation_utilities.hpp:82
int GetEchoLevel()
Definition: mesh_error_calculation_utilities.hpp:289
void ElementalErrorCalculation(ModelPart &rModelPart, std::vector< double > &rElementalError, std::vector< int > &rIds, const Variable< double > &rVariable)
Definition: mesh_error_calculation_utilities.hpp:136
virtual void SetEchoLevel(int Level)
Definition: mesh_error_calculation_utilities.hpp:284
~MeshErrorCalculationUtilities()
Destructor.
Definition: mesh_error_calculation_utilities.hpp:69
MeshErrorCalculationUtilities()
Default constructor.
Definition: mesh_error_calculation_utilities.hpp:66
ModelPart::ElementsContainerType ElementsContainerType
Definition: mesh_error_calculation_utilities.hpp:54
ModelPart::MeshType::GeometryType::PointsArrayType PointsArrayType
Definition: mesh_error_calculation_utilities.hpp:56
GlobalPointersVector< Element > ElementWeakPtrVectorType
Definition: mesh_error_calculation_utilities.hpp:59
GlobalPointersVector< Condition > ConditionWeakPtrVectorType
Definition: mesh_error_calculation_utilities.hpp:60
GlobalPointersVector< Node > NodeWeakPtrVectorType
Definition: mesh_error_calculation_utilities.hpp:58
static unsigned int GetMaxNodeId(ModelPart &rModelPart)
Definition: mesher_utilities.hpp:1408
static unsigned int GetMaxElementId(ModelPart &rModelPart)
Definition: mesher_utilities.hpp:1481
This class aims to manage meshes for multi-physics simulations.
Definition: model_part.h:77
ElementIterator ElementsBegin(IndexType ThisIndex=0)
Definition: model_part.h:1169
MeshType::ElementsContainerType ElementsContainerType
Element container. A vector set of Elements with their Id's as key.
Definition: model_part.h:168
ProcessInfo & GetProcessInfo()
Definition: model_part.h:1746
ElementsContainerType & Elements(IndexType ThisIndex=0)
Definition: model_part.h:1189
MeshType::NodesContainerType NodesContainerType
Nodes container. Which is a vector set of nodes with their Id's as key.
Definition: model_part.h:128
ElementIterator ElementsEnd(IndexType ThisIndex=0)
Definition: model_part.h:1179
SizeType NumberOfNodes(IndexType ThisIndex=0) const
Definition: model_part.h:341
NodesContainerType & Nodes(IndexType ThisIndex=0)
Definition: model_part.h:507
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
static double max(double a, double b)
Definition: GeometryFunctions.h:79
static double min(double a, double b)
Definition: GeometryFunctions.h:71
pybind11::list CalculateOnIntegrationPoints(TObject &dummy, const Variable< TDataType > &rVariable, const ProcessInfo &rProcessInfo)
Definition: add_mesh_to_python.cpp:142
TSpaceType::IndexType Size(TSpaceType &dummy, typename TSpaceType::VectorType const &rV)
Definition: add_strategies_to_python.cpp:111
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
TABLE_NUMBER_ANGULAR_VELOCITY TABLE_NUMBER_MOMENT I33 BEAM_INERTIA_ROT_UNIT_LENGHT_Y KRATOS_DEFINE_APPLICATION_VARIABLE(DEM_APPLICATION, double, BEAM_INERTIA_ROT_UNIT_LENGHT_Z) typedef std double
Definition: DEM_application_variables.h:182
integer i
Definition: TensorModule.f:17