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.
generate_new_nodes_mesher_process.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_GENERATE_NEW_NODES_MESHER_PROCESS_H_INCLUDED )
11 #define KRATOS_GENERATE_NEW_NODES_MESHER_PROCESS_H_INCLUDED
12 
13 
14 // External includes
15 
16 // System includes
17 
18 // Project includes
21 
22 #include "includes/model_part.h"
26 
28 //Data:
29 //StepData: CONTACT_FORCE, DISPLACEMENT
30 //Flags: (checked)
31 // (set)
32 // (modified)
33 // (reset)
34 //(set):=(set in this process)
35 
36 namespace Kratos
37 {
38 
41 
43 
48  : public MesherProcess
49 {
50 public:
53 
56 
61 
65 
68  MesherUtilities::MeshingParameters& rRemeshingParameters,
69  int EchoLevel)
70  : mrModelPart(rModelPart),
71  mrRemesh(rRemeshingParameters)
72  {
73  mEchoLevel = EchoLevel;
74  }
75 
76 
79 
80 
84 
86  void operator()()
87  {
88  Execute();
89  }
90 
91 
95 
97  void Execute() override
98  {
100 
101  if( mEchoLevel > 0 )
102  std::cout<<" [ GENERATE NEW NODES: "<<std::endl;
103 
104  if( mrModelPart.Name() != mrRemesh.SubModelPartName )
105  std::cout<<" ModelPart Supplied do not corresponds to the Meshing Domain: ("<<mrModelPart.Name()<<" != "<<mrRemesh.SubModelPartName<<")"<<std::endl;
106 
107  //Find out where the new nodes belong to:
108 
109  //creating an auxiliary list for the new nodes
110  std::vector<NodeType::Pointer > list_of_new_nodes;
111 
112  this->GenerateNewNodes(mrModelPart, list_of_new_nodes);
113 
114  if( mEchoLevel > 0 )
115  std::cout <<" [ GENERATED NODES: ( added: " << mrRemesh.Info->InsertedNodes <<" ) ]"<<std::endl;
116 
117 
118  //project variables to new nodes from mesh elements
119  if( list_of_new_nodes.size() > 0)
120  this->ProjectVariablesToNewNodes( mrModelPart, list_of_new_nodes );
121 
122  //set node variables
123  for(std::vector<NodeType::Pointer>::iterator it = list_of_new_nodes.begin(); it!=list_of_new_nodes.end(); ++it)
124  {
125  this->SetNewNodeVariables(mrModelPart,(*it));
126  }
127 
128 
129  this->SetNodesToModelPart(mrModelPart, list_of_new_nodes);
130 
131  if( mEchoLevel > 0 )
132  std::cout<<" GENERATE NEW NODES ]; "<<std::endl;
133 
134 
135  KRATOS_CATCH(" ")
136  }
137 
138 
142 
143 
147 
148 
152 
154  std::string Info() const override
155  {
156  return "GenerateNewNodesMesherProcess";
157  }
158 
160  void PrintInfo(std::ostream& rOStream) const override
161  {
162  rOStream << "GenerateNewNodesMesherProcess";
163  }
164 
166  void PrintData(std::ostream& rOStream) const override
167  {
168  }
169 
170 
174 
176 
177 
178 private:
181 
185  ModelPart& mrModelPart;
186 
188 
189  MesherUtilities mMesherUtilities;
190 
191  int mEchoLevel;
192 
196 
197 
201 
202 
203  //*******************************************************************************************
204  //*******************************************************************************************
205 
206  void GenerateNewNodes(ModelPart& rModelPart, std::vector<NodeType::Pointer>& list_of_nodes)
207  {
208  KRATOS_TRY
209 
210  NodeType::Pointer pNode;
211 
212  //center
213  double xc = 0;
214  double yc = 0;
215  double zc = 0;
216 
217  //assign data to dofs
218  NodeType::DofsContainerType& ReferenceDofs = rModelPart.Nodes().front().GetDofs();
219 
220  VariablesList& rVariablesList = rModelPart.GetNodalSolutionStepVariablesList();
221 
222  unsigned int id = MesherUtilities::GetMaxNodeId(rModelPart) + 1;
223 
224  ModelPart::ElementsContainerType::iterator element_begin = rModelPart.ElementsBegin();
225  const unsigned int dimension = element_begin->GetGeometry().WorkingSpaceDimension();
226 
227  double* OutPointList = mrRemesh.OutMesh.GetPointList();
228 
229  int& InNumberOfPoints = mrRemesh.InMesh.GetNumberOfPoints();
230  int& OutNumberOfPoints = mrRemesh.OutMesh.GetNumberOfPoints();
231 
232 
233  if (OutNumberOfPoints > InNumberOfPoints)
234  {
235  for(int i = InNumberOfPoints; i<OutNumberOfPoints; ++i)
236  {
237  int base = i*dimension;
238 
239  xc = OutPointList[base];
240  yc = OutPointList[base+1];
241  zc = 0;
242  if(dimension==3)
243  zc=OutPointList[base+2];
244 
245  //create a new node
246  pNode = Kratos::make_intrusive<Node>( id, xc, yc, zc );
247 
248  //set new id
249  if(mrRemesh.InputInitializedFlag){
250  mrRemesh.NodalPreIds.push_back( id );
251  pNode->SetId(i+1);
252  if( id > mrRemesh.NodeMaxId )
253  mrRemesh.NodeMaxId = id;
254  }
255 
256  //giving model part variables list to the node
257  pNode->SetSolutionStepVariablesList(&rVariablesList);
258 
259  //set buffer size
260  pNode->SetBufferSize(rModelPart.GetBufferSize());
261 
262  //generating the dofs
263  for(Node::DofsContainerType::iterator i_dof = ReferenceDofs.begin(); i_dof != ReferenceDofs.end(); ++i_dof)
264  {
265  NodeType::DofType& rDof = **i_dof;
266  NodeType::DofType::Pointer pNewDof = pNode->pAddDof( rDof );
267 
268  (pNewDof)->FreeDof();
269  }
270 
271  list_of_nodes.push_back(pNode);
272 
273  id++;
274 
275  }
276 
277  }
278 
279 
280  //Inserted nodes
281  mrRemesh.Info->InsertedNodes = OutNumberOfPoints-InNumberOfPoints;
282 
283 
284  KRATOS_CATCH( "" )
285  }
286 
287  //**************************************************************************
288  //**************************************************************************
289 
290  void ProjectVariablesToNewNodes(ModelPart& rModelPart, std::vector<NodeType::Pointer >& list_of_new_nodes)
291  {
292 
293  KRATOS_TRY
294 
295  //defintions for spatial search
296  typedef Node PointType;
297  typedef Node::Pointer PointPointerType;
298  typedef std::vector<PointPointerType> PointVector;
299  typedef PointVector::iterator PointIterator;
300  //typedef std::vector<double> DistanceVector;
301  typedef std::vector<double>::iterator DistanceIterator;
302 
303  typedef Bucket<3, PointType, PointVector, PointPointerType, PointIterator, DistanceIterator > BucketType;
304  typedef Tree< KDTreePartition<BucketType> > KdtreeType; //Kdtree
305  //defintions for spatial search
306 
307  unsigned int bucket_size = 20;
308  KdtreeType NodesTree(list_of_new_nodes.begin(),list_of_new_nodes.end(),bucket_size);
309 
310 
311  //Find out where the new nodes belong to:
312  std::vector<double> ShapeFunctionsN;
313  std::vector<VariablesListDataValueContainer> VariablesListVector(list_of_new_nodes.size());
314 
315  VariablesList& rVariablesList = rModelPart.GetNodalSolutionStepVariablesList();
316 
317  //find the center and "radius" of the element
318  double radius = 0;
319  Node center(0,0.0,0.0,0.0);
320 
321  unsigned int MaximumNumberOfPointsInRadius = list_of_new_nodes.size();
322  std::vector<Node::Pointer> PointsInRadius (MaximumNumberOfPointsInRadius);
323  std::vector<double> PointsInRadiusDistances (MaximumNumberOfPointsInRadius);
324 
325  //geometry
326  ModelPart::ElementsContainerType::iterator element_begin = rModelPart.ElementsBegin();
327  const unsigned int nds = element_begin->GetGeometry().size();
328 
329  std::vector<std::vector<double> > ElementPointCoordinates(nds);
330  std::vector<double> PointCoordinates(3);
331  std::fill( PointCoordinates.begin(), PointCoordinates.end(), 0.0 );
332  std::fill( ElementPointCoordinates.begin(), ElementPointCoordinates.end(), PointCoordinates );
333 
334  for(ModelPart::ElementsContainerType::const_iterator ie = rModelPart.ElementsBegin();
335  ie != rModelPart.ElementsEnd(); ++ie)
336  {
337 
338  //coordinates
339  for(unsigned int i=0; i<ie->GetGeometry().size(); ++i)
340  {
341  PointCoordinates[0] = ie->GetGeometry()[i].X();
342  PointCoordinates[1] = ie->GetGeometry()[i].Y();
343  PointCoordinates[2] = ie->GetGeometry()[i].Z();
344 
345  ElementPointCoordinates[i] = PointCoordinates;
346  }
347 
348  std::fill( PointCoordinates.begin(), PointCoordinates.end(), 0.0 );
349  MeshDataTransferUtilities DataTransferUtilities;
350  DataTransferUtilities.CalculateCenterAndSearchRadius( ElementPointCoordinates, PointCoordinates, radius );
351 
352  //find all of the new nodes within the radius
353  center.X() = PointCoordinates[0];
354  center.Y() = PointCoordinates[1];
355  center.Z() = PointCoordinates[2];
356 
357  double Radius = radius * 1.01;
358  int NumberOfPointsInRadius = NodesTree.SearchInRadius (center, Radius, PointsInRadius.begin(), PointsInRadiusDistances.begin(), MaximumNumberOfPointsInRadius);
359 
360 
361  //check if inside and eventually interpolate
362  for(std::vector<Node::Pointer>::iterator it_found = PointsInRadius.begin(); it_found != (PointsInRadius.begin() + NumberOfPointsInRadius) ; ++it_found)
363  {
364 
365  PointCoordinates[0] = (*it_found)->X();
366  PointCoordinates[1] = (*it_found)->Y();
367  PointCoordinates[2] = (*it_found)->Z();
368 
369  bool is_inside = MesherUtilities::CalculatePosition( ElementPointCoordinates, PointCoordinates, ShapeFunctionsN );
370 
371  if(is_inside == true)
372  {
373  double alpha = 1; //1 to interpolate, 0 to leave the original data
374  DataTransferUtilities.Interpolate( ie->GetGeometry(), ShapeFunctionsN, rVariablesList, *(it_found), alpha );
375  }
376  }
377  }
378 
379  KRATOS_CATCH( "" )
380 
381  }
382 
383 
384  //*******************************************************************************************
385  //*******************************************************************************************
386 
387  virtual void SetNewNodeVariables(ModelPart& rModelPart, NodeType::Pointer& pNode)
388  {
389  KRATOS_TRY
390 
391  //set model part
392  pNode->SetValue(MODEL_PART_NAME,rModelPart.Name());
393 
394  //set original position
395  const array_1d<double,3>& Displacement = pNode->FastGetSolutionStepValue(DISPLACEMENT);
396  pNode->X0() = pNode->X() - Displacement[0];
397  pNode->Y0() = pNode->Y() - Displacement[1];
398  pNode->Z0() = pNode->Z() - Displacement[2];
399 
400  //set contact force
401  if ( pNode->SolutionStepsDataHas(CONTACT_FORCE) )
402  pNode->FastGetSolutionStepValue(CONTACT_FORCE).clear();
403 
404 
405  KRATOS_CATCH( "" )
406  }
407 
408 
409  //*******************************************************************************************
410  //*******************************************************************************************
411 
412  void SetNodesToModelPart(ModelPart& rModelPart, std::vector<NodeType::Pointer>& list_of_nodes)
413  {
414  KRATOS_TRY
415 
416  if(list_of_nodes.size()){
417 
418  //add new conditions: ( SOLID body model part )
419  for(std::vector<NodeType::Pointer>::iterator i_node = list_of_nodes.begin(); i_node!= list_of_nodes.end(); ++i_node)
420  {
421  rModelPart.Nodes().push_back(*(i_node));
422  }
423 
424  }
425 
426  KRATOS_CATCH( "" )
427  }
431 
432 
436 
437 
441 
442 
445 
446 
448 
449 
451  //Process(Process const& rOther);
452 
453 
455 
456 }; // Class Process
457 
459 
462 
463 
467 
468 
470 inline std::istream& operator >> (std::istream& rIStream,
472 
474 inline std::ostream& operator << (std::ostream& rOStream,
475  const GenerateNewNodesMesherProcess& rThis)
476 {
477  rThis.PrintInfo(rOStream);
478  rOStream << std::endl;
479  rThis.PrintData(rOStream);
480 
481  return rOStream;
482 }
484 
485 
486 } // namespace Kratos.
487 
488 #endif // KRATOS_GENERATE_NEW_NODES_MESHER_PROCESS_H_INCLUDED defined
Base class for all Conditions.
Definition: condition.h:59
Dof represents a degree of freedom (DoF).
Definition: dof.h:86
Dof * Pointer
Pointer definition of Dof.
Definition: dof.h:93
Refine Mesh Elements Process 2D and 3D.
Definition: generate_new_nodes_mesher_process.hpp:49
void operator()()
This operator is provided to call the process as a function and simply calls the Execute method.
Definition: generate_new_nodes_mesher_process.hpp:86
void Execute() override
Execute method is used to execute the Process algorithms.
Definition: generate_new_nodes_mesher_process.hpp:97
virtual ~GenerateNewNodesMesherProcess()
Destructor.
Definition: generate_new_nodes_mesher_process.hpp:78
ConditionType::GeometryType GeometryType
Definition: generate_new_nodes_mesher_process.hpp:60
KRATOS_CLASS_POINTER_DEFINITION(GenerateNewNodesMesherProcess)
Pointer definition of Process.
GenerateNewNodesMesherProcess(ModelPart &rModelPart, MesherUtilities::MeshingParameters &rRemeshingParameters, int EchoLevel)
Default constructor.
Definition: generate_new_nodes_mesher_process.hpp:67
void PrintData(std::ostream &rOStream) const override
Print object's data.s.
Definition: generate_new_nodes_mesher_process.hpp:166
ModelPart::NodeType NodeType
Definition: generate_new_nodes_mesher_process.hpp:57
std::string Info() const override
Turn back information as a string.
Definition: generate_new_nodes_mesher_process.hpp:154
ModelPart::PropertiesType PropertiesType
Definition: generate_new_nodes_mesher_process.hpp:59
ModelPart::ConditionType ConditionType
Definition: generate_new_nodes_mesher_process.hpp:58
void PrintInfo(std::ostream &rOStream) const override
Print information about this object.
Definition: generate_new_nodes_mesher_process.hpp:160
Geometry base class.
Definition: geometry.h:71
The base class for processes passed to the solution scheme.
Definition: mesher_process.hpp:37
Short class definition.
Definition: mesher_utilities.hpp:49
static unsigned int GetMaxNodeId(ModelPart &rModelPart)
Definition: mesher_utilities.hpp:1408
static bool CalculatePosition(const std::vector< std::vector< double >> &rPointCoordinates, const std::vector< double > &rCenter, std::vector< double > &rShapeFunctionsN)
Definition: mesher_utilities.hpp:1272
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
std::string & Name()
Definition: model_part.h:1811
IndexType GetBufferSize() const
This method gets the suffer size of the model part database.
Definition: model_part.h:1876
NodesContainerType & Nodes(IndexType ThisIndex=0)
Definition: model_part.h:507
VariablesList & GetNodalSolutionStepVariablesList()
Definition: model_part.h:549
This class defines the node.
Definition: node.h:65
std::vector< std::unique_ptr< Dof< double > >> DofsContainerType
The DoF container type definition.
Definition: node.h:92
Properties encapsulates data shared by different Elements or Conditions. It can store any type of dat...
Definition: properties.h:69
Holds a list of variables and their position in VariablesListDataValueContainer.
Definition: variables_list.h:50
#define KRATOS_CATCH(MoreInfo)
Definition: define.h:110
#define KRATOS_TRY
Definition: define.h:109
static int EchoLevel
Definition: co_sim_EMPIRE_API.h:42
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
std::istream & operator>>(std::istream &rIStream, LinearMasterSlaveConstraint &rThis)
input stream function
DistanceVector::iterator DistanceIterator
Definition: internal_variables_interpolation_process.h:56
std::vector< PointTypePointer > PointVector
Definition: internal_variables_interpolation_process.h:53
PointVector::iterator PointIterator
Definition: internal_variables_interpolation_process.h:54
std::ostream & operator<<(std::ostream &rOStream, const LinearMasterSlaveConstraint &rThis)
output stream function
Definition: linear_master_slave_constraint.h:432
Point PointType
Geometric definitions.
Definition: mortar_classes.h:36
alpha
Definition: generate_convection_diffusion_explicit_element.py:113
int dimension
Definition: isotropic_damage_automatic_differentiation.py:123
float radius
Definition: mesh_to_mdpa_converter.py:18
float xc
Definition: rotating_cone.py:77
float yc
Definition: rotating_cone.py:78
integer i
Definition: TensorModule.f:17
int & GetNumberOfPoints()
Definition: mesher_utilities.hpp:182
double * GetPointList()
Definition: mesher_utilities.hpp:177
Definition: mesher_utilities.hpp:631
MeshingInfoParameters::Pointer Info
Definition: mesher_utilities.hpp:681
MeshContainer InMesh
Definition: mesher_utilities.hpp:674
MeshContainer OutMesh
Definition: mesher_utilities.hpp:675
bool InputInitializedFlag
Definition: mesher_utilities.hpp:661
std::string SubModelPartName
Definition: mesher_utilities.hpp:642
std::vector< int > NodalPreIds
Definition: mesher_utilities.hpp:668
unsigned int NodeMaxId
Definition: mesher_utilities.hpp:666