10 #if !defined(KRATOS_TETRAHEDRA_RECONNECT_H_INCLUDED )
11 #define KRATOS_TETRAHEDRA_RECONNECT_H_INCLUDED
29 #include "u_qualityMetrics.h"
32 #include "u_TetraFunctions.h"
33 #include "u_ShowMetrics.h"
34 #include "u_ParallelFunctions.h"
35 #include "u_MeshLoaders.h"
36 #include "u_elementCluster.h"
37 #include "u_ProcessTime.h"
38 #include "u_TetGenInterface.h"
95 std::cout <<
"Creating mesh" <<
"\n";
96 m =
new TVolumeMesh();
124 if (
debugMode) std::cout <<
"Reading nodes"<<
"\n";
127 for (ModelPart::NodesContainerType::iterator i_node = r_model_part.
NodesBegin() ; i_node != r_model_part.
NodesEnd() ; i_node++)
131 for (ModelPart::NodesContainerType::iterator it=r_model_part.
NodesBegin(); it!=r_model_part.
NodesEnd(); it++)
137 auto v =
new TVertex(fPos);
138 v->setID( it->Id() );
142 if (
debugMode) std::cout <<
"Reading elements"<<
"\n";
144 for (ModelPart::ElementsContainerType::iterator el_it=r_model_part.
ElementsBegin(); el_it!=r_model_part.
ElementsEnd(); el_it++)
147 if (geom.
size() != 4)
149 std::cout <<
"Invalid element size" << el_it->
Id();
152 TVertex *v0 =
m->findVertexById(geom[0].Id());
155 std::cout <<
"Invalid element reference" << el_it->Id();
158 TVertex *v1 =
m->findVertexById(geom[1].Id());
161 std::cout <<
"Invalid element reference" << el_it->Id();
164 TVertex *v2 =
m->findVertexById(geom[2].Id());
167 std::cout <<
"Invalid element reference" << el_it->Id();
170 TVertex *v3 =
m->findVertexById(geom[3].Id());
173 std::cout <<
"Invalid element reference" << el_it->Id();
177 auto t =
new TTetra(
nullptr, v0,v1,v2,v3);
181 if (
debugMode) std::cout <<
" Number of vertexes read :"<<
m->vertexes->Count() <<
"\n";
182 if (
debugMode) std::cout <<
" Number of elements read :"<<
m->elements->Count() <<
"\n";
183 m->updateIndexes(GENERATE_SURFACE | KEEP_ORIG_IDS);
184 if (
debugMode) std::cout <<
" Number of faces read :"<<
m->fFaces->Count() <<
"\n";
195 if (
debugMode) std::cout <<
"-------------Generating for Kratos----------------" <<
"\n";
196 m->vertexes->Sort(sortByID);
197 Element::Pointer pReferenceElement = *(mrModelPart.Elements().begin()).base();
202 for (
int i=0;
i<
m->vertexes->Count();
i++)
204 if (
m->vertexes->structure[
i]->elementsList->Count() == 0)
205 m->vertexes->elementAt(
i)->flag = 1;
207 m->vertexes->elementAt(
i)->flag = 0;
209 if (
debugMode) std::cout <<
" Nodes : Mark elements to remove : " <<
"\n";
210 bool shownMessage =
false;
219 for (ModelPart::NodesContainerType::iterator i_node = mrModelPart.Nodes().begin() ; i_node != mrModelPart.Nodes().end() ; i_node++)
221 TVertex*
v =
m->findVertexById(i_node->Id());
225 std::cout <<
"Error at id "<<i_node->Id() <<
"\n";
229 i_node->Set(TO_ERASE ,
v->flag == 1);
231 if (
debugMode) std::cout <<
" Generating Elements for Kratos " <<
"\n";
233 mrModelPart.Elements().clear();
235 Properties::Pointer properties = mrModelPart.GetMesh().pGetProperties(1);
236 (mrModelPart.Elements()).reserve(
m->elements->Count());
237 bool messageShown =
false;
239 for (
int i=0;
i<
m->elements->Count() ;
i++)
242 TTetra*
t = (TTetra*)(
m->elements->elementAt(
i));
243 Node::Pointer v0 = mrModelPart.pGetNode(
t->vertexes[0]->getID());
244 Node::Pointer v1 = mrModelPart.pGetNode(
t->vertexes[1]->getID());
245 Node::Pointer v2 = mrModelPart.pGetNode(
t->vertexes[2]->getID());
246 Node::Pointer v3 = mrModelPart.pGetNode(
t->vertexes[3]->getID());
247 if ((v0.get() ==
nullptr) || (v1.get()==
nullptr) || (v2.get() ==
nullptr) || (v3.get() ==
nullptr))
251 std::cout <<
"Invalid vertex access " <<
t->getID() <<
"\n";
259 Element::Pointer p_element = pReferenceElement->Create(
i+1, geom, properties);
260 (mrModelPart.Elements()).push_back(p_element);
263 if (
debugMode) std::cout <<
"Generation OK " <<
"\n";
264 mrModelPart.Elements().Sort();
266 if (removeFreeVertexes )
268 std::cout <<
"Removing free vertexes " <<
"\n";
271 if (
debugMode) std::cout <<
"-----------------Generation Finished OK-------------------" <<
"\n";
280 auto qt =
new TetQuality(
m) ;
283 int numNegElements = qt->nonPositive;
285 return numNegElements == 0;
293 for (
int i=0 ;
i<100 ;
i++)
295 m->elementsToRemove->Add(
m->elements->elementAt(
i));
305 std::cout <<
"Updating nodes"<<
"\n";
307 for (ModelPart::NodesContainerType::iterator it=r_model_part.
NodesBegin(); it!=r_model_part.
NodesEnd(); it++)
316 TVertex *
v =
m->findVertexById(
id);
344 auto qt =
new TetQuality(
m) ;
346 int numNegElements = qt->nonPositive;
348 return numNegElements == 0;
353 unsigned int n_points_before_refinement = rModelPart.
Nodes().size();
356 if (
m->vertexes->Count()>(
int)n_points_before_refinement)
362 const int max_results = 10000;
368 std::cout <<
"...Adding Nodes :"<<
m->vertexes->Count() - n_points_before_refinement<<
"\n";
370 for (
int i = n_points_before_refinement;
i<
m->vertexes->Count();
i++)
374 TVertex *
v =
m->vertexes->elementAt(
i);
375 double x= (float)(
v->fPos.x);
376 double y= (float)(
v->fPos.y);
377 double z= (float)(
v->fPos.z);
387 for (Node::DofsContainerType::iterator iii = reference_dofs.begin(); iii != reference_dofs.end(); iii++)
392 (p_new_dof)->FreeDof();
396 auto result_begin = results.begin();
397 Element::Pointer pelement;
399 bool is_found = element_finder.FindPointOnMesh(pnode->Coordinates(),
N, pelement, result_begin, max_results);
401 if (is_found ==
true)
416 for (
int el = 0;
el<
m->elements->Count();
el++)
425 TTetra *
t = (TTetra*)(
m->elements->elementAt(
el));
427 ModelPart::NodesContainerType::iterator it1 = (rNodes).find(
t->vertexes[0]->getID());
428 ModelPart::NodesContainerType::iterator it2 = (rNodes).find(
t->vertexes[1]->getID());
429 ModelPart::NodesContainerType::iterator it3 = (rNodes).find(
t->vertexes[2]->getID());
430 ModelPart::NodesContainerType::iterator it4 = (rNodes).find(
t->vertexes[3]->getID());
432 if ( it1 == rModelPart.
Nodes().end() )
433 KRATOS_THROW_ERROR(std::logic_error,
"trying to use an inexisting node with id ",it1->Id());
434 if ( it2 == rModelPart.
Nodes().end() )
435 KRATOS_THROW_ERROR(std::logic_error,
"trying to use an inexisting node with id ",it2->Id());
436 if ( it3 == rModelPart.
Nodes().end() )
437 KRATOS_THROW_ERROR(std::logic_error,
"trying to use an inexisting node with id ",it3->Id());
438 if ( it4 == rModelPart.
Nodes().end() )
439 KRATOS_THROW_ERROR(std::logic_error,
"trying to use an inexisting node with id ",it4->Id());
441 Node::Pointer pn1 = *it1.base();
442 Node::Pointer pn2 = *it2.base();
443 Node::Pointer pn3 = *it3.base();
444 Node::Pointer pn4 = *it4.base();
446 t->vertexes[0]->expectedSize = (pn1)->FastGetSolutionStepValue(NODAL_H);
447 t->vertexes[1]->expectedSize = (pn2)->FastGetSolutionStepValue(NODAL_H);
448 t->vertexes[2]->expectedSize = (pn3)->FastGetSolutionStepValue(NODAL_H);
449 t->vertexes[3]->expectedSize = (pn4)->FastGetSolutionStepValue(NODAL_H);
465 unsigned int step_data_size,
468 unsigned int buffer_size = pnode->GetBufferSize();
475 double* step_data = (pnode)->SolutionStepData().Data(
step);
478 double* node0_data = geom[0].SolutionStepData().Data(
step);
479 double* node1_data = geom[1].SolutionStepData().Data(
step);
480 double* node2_data = geom[2].SolutionStepData().Data(
step);
481 double* node3_data = geom[3].SolutionStepData().Data(
step);
484 for (
unsigned int j= 0;
j<step_data_size;
j++)
487 step_data[
j] =
N[0]*node0_data[
j] +
N[1]*node1_data[
j] +
N[2]*node2_data[
j] +
N[3]*node3_data[
j];
492 pnode->FastGetSolutionStepValue(IS_BOUNDARY)=0.0;
496 bool processByNode,
bool processByFace,
bool processByEdge,
497 bool saveToFile,
bool removeFreeVertexes ,
498 bool evaluateInParallel ,
bool reinsertNodes ,
bool debugMode,
int minAngle)
501 m->vertexes->Sort(sortByID);
507 TMeshLoader* ml2 =
new TVMWLoader();
508 std::string s(
"out_MeshFromKratos.vwm");
516 std::cout <<
"...Start Optimization..." <<
"\n";
517 auto ml2 =
new TVMWLoader();
519 s =
"../out_MeshFromKratos" + intToString(simIter)+
".vwm";
521 ml2->save( s.data(),
m);
527 if (
debugMode) std::cout <<
"...Start Optimization..." <<
"\n";
535 OpenMPUtils::SetNumThreads(omp_get_max_threads());
543 std::cout <<
"Number of active threads"<< OpenMPUtils::GetNumThreads() <<
"\n";
544 std::cout <<
"Debug mode is Active" <<
"\n";
551 std::cout <<
"...Trying to allocate memory\n";
553 std::cout <<
"...Allocate memory OK\n";
555 for (
int iter = 0 ; iter< iterations ; iter ++)
561 if (evaluateInParallel )
563 if (
debugMode) std::cout <<
"...Parallel optimizing by Node. Iteration : "<< iter <<
"\n";
564 ParallelEvaluateClusterByNode((TVolumeMesh*)(
m),vrelaxQuality,minAngle);
568 if (
debugMode) std::cout <<
"...Optimizing by Node. Iteration : "<< iter <<
"\n";
569 evaluateClusterByNode( (TVolumeMesh*)(
m),minAngle,vrelaxQuality);
572 m->updateIndexes(GENERATE_SURFACE | KEEP_ORIG_IDS);
577 if (evaluateInParallel )
579 if (
debugMode) std::cout <<
"...Parallel optimizing by Face. Iteration : "<< iter <<
"\n";
580 ParallelEvaluateClusterByFace((TVolumeMesh*)(
m),vrelaxQuality,minAngle);
581 if (
debugMode) std::cout <<
"...End. Iteration : "<< iter <<
"\n";
585 if (
debugMode) std::cout <<
"...Optimizing by Face. Iteration : "<< iter <<
"\n";
586 evaluateClusterByFace(
m,minAngle,vrelaxQuality);
587 if (
debugMode) std::cout <<
"...End. Iteration : "<< iter <<
"\n";
590 m->updateIndexes(GENERATE_SURFACE | KEEP_ORIG_IDS);
596 if (evaluateInParallel )
598 if (
debugMode) std::cout <<
"...Parallel optimizing by Edge. Iteration : "<< iter <<
"\n";
599 ParallelEvaluateClusterByEdge((TVolumeMesh*)(
m),vrelaxQuality,minAngle);
600 if (
debugMode) std::cout <<
"...End. Iteration : "<< iter <<
"\n";
604 if (
debugMode) std::cout <<
"...Optimizing by Edge. Iteration : "<< iter <<
"\n";
605 evaluateClusterByEdge( (TVolumeMesh*)(
m),minAngle,vrelaxQuality);
608 m->updateIndexes(GENERATE_SURFACE | KEEP_ORIG_IDS);
613 std :: cout<<
"Number of faces:" <<
m->fFaces->Count() <<
"\n";
615 m->updateIndexes(GENERATE_SURFACE | KEEP_ORIG_IDS);
617 std :: cout<<
"Number of faces:" <<
m->fFaces->Count() <<
"\n";
623 TMeshLoader* ml2 =
new TVMWLoader();
625 s =
"out_MeshFromKratos" + intToString(iter)+
".vwm";
634 TMeshLoader* ml2 =
new TVMWLoader();
635 std::string s(
"out_MeshC.vwm");
642 if (
debugMode) std::cout <<
"...Trying to reinsert nodes..." <<
"\n";
654 std::cout <<
"...Interpolating and adding new Nodes..." <<
"\n";
664 int vToR =
m->vertexesToRemove->Count();
665 int ri = vertexTetraReInsertion(
m ,
m->vertexesToRemove);
666 m->updateIndexes(GENERATE_SURFACE | KEEP_ORIG_IDS);
667 std :: cout<<
" Reinsert vertexes " << ri <<
" of " << vToR <<
"\n";
670 std :: cout<<
"........................................"<<
"\n";
676 if (
m ==
nullptr ) return ;
677 if (
debugMode) std::cout <<
"...Output to Kratos Format" <<
"\n";
682 std::cout <<
"...Trying to release memory\n";
684 std::cout <<
"...Release memory OK\n";
704 virtual std::string
Info()
const
706 std::stringstream buffer;
707 buffer <<
"TetrahedraReconnectUtility" ;
714 rOStream <<
"TetrahedraReconnectUtility";
836 rOStream << std::endl;
This class is designed to allow the fast location of MANY points on the top of a 3D mesh.
Definition: binbased_fast_point_locator.h:68
void UpdateSearchDatabase()
Function to construct or update the search database.
Definition: binbased_fast_point_locator.h:139
ConfigureType::ResultContainerType ResultContainerType
Definition: binbased_fast_point_locator.h:81
Dof represents a degree of freedom (DoF).
Definition: dof.h:86
This process removes the entities from a model part with the flag TO_ERASE.
Definition: entity_erase_process.h:70
Geometry base class.
Definition: geometry.h:71
SizeType size() const
Definition: geometry.h:518
IndexType const & Id() const
Id of this Geometry.
Definition: geometry.h:964
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
NodeType::Pointer CreateNewNode(int Id, double x, double y, double z, VariablesList::Pointer pNewVariablesList, IndexType ThisIndex=0)
Definition: model_part.cpp:270
SizeType GetNodalSolutionStepDataSize()
Definition: model_part.h:571
NodeIterator NodesBegin(IndexType ThisIndex=0)
Definition: model_part.h:487
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
NodeIterator NodesEnd(IndexType ThisIndex=0)
Definition: model_part.h:497
NodesContainerType & Nodes(IndexType ThisIndex=0)
Definition: model_part.h:507
std::vector< std::unique_ptr< Dof< double > >> DofsContainerType
The DoF container type definition.
Definition: node.h:92
A four node tetrahedra geometry with linear shape functions.
Definition: tetrahedra_3d_4.h:59
Short class definition.
Definition: tetrahedra_reconnect_utility.h:73
bool debugMode
Definition: tetrahedra_reconnect_utility.h:79
void updateNodesPositions(ModelPart &r_model_part)
function updateNodesPositions Update only nodes positions without regenerating the structure
Definition: tetrahedra_reconnect_utility.h:303
int maxNumThreads
Definition: tetrahedra_reconnect_utility.h:77
void innerConvertToKratos(ModelPart &mrModelPart, TVolumeMesh *m, bool removeFreeVertexes)
function innerConvertToKratos This function converts back to Kratos format
Definition: tetrahedra_reconnect_utility.h:193
void setMaxNumThreads(int mxTh)
function OptimizeQuality
Definition: tetrahedra_reconnect_utility.h:332
TVolumeMesh * m
Definition: tetrahedra_reconnect_utility.h:87
KRATOS_CLASS_POINTER_DEFINITION(TetrahedraReconnectUtility)
Pointer definition of TetrahedraReconnectUtility.
virtual std::string Info() const
Turn back information as a string.
Definition: tetrahedra_reconnect_utility.h:704
void OptimizeQuality(ModelPart &r_model_part, int simIter, int iterations, bool processByNode, bool processByFace, bool processByEdge, bool saveToFile, bool removeFreeVertexes, bool evaluateInParallel, bool reinsertNodes, bool debugMode, int minAngle)
Definition: tetrahedra_reconnect_utility.h:495
void setBlockSize(int bs)
Definition: tetrahedra_reconnect_utility.h:337
void setVertexExpectedSize(ModelPart &rModelPart, TVolumeMesh *m)
Definition: tetrahedra_reconnect_utility.h:414
virtual ~TetrahedraReconnectUtility()=default
Destructor.
virtual void PrintInfo(std::ostream &rOStream) const
Print information about this object.
Definition: tetrahedra_reconnect_utility.h:712
void FinalizeOptimization(bool removeFreeVertexes)
function FinalizeOptimization Destroy the structure
Definition: tetrahedra_reconnect_utility.h:674
void TestRemovingElements()
Definition: tetrahedra_reconnect_utility.h:291
TetrahedraReconnectUtility(ModelPart &r_model_part)
Default constructor.
Definition: tetrahedra_reconnect_utility.h:92
bool isaValidMesh()
Definition: tetrahedra_reconnect_utility.h:342
int blockSize
Definition: tetrahedra_reconnect_utility.h:78
ModelPart & refMP
Definition: tetrahedra_reconnect_utility.h:89
bool EvaluateQuality()
Definition: tetrahedra_reconnect_utility.h:278
void Interpolate(Geometry< Node > &geom, const array_1d< double, 4 > &N, unsigned int step_data_size, Node::Pointer pnode)
Definition: tetrahedra_reconnect_utility.h:464
void InterpolateAndAddNewNodes(ModelPart &rModelPart, TVolumeMesh *m, BinBasedFastPointLocator< 3 > &element_finder)
Definition: tetrahedra_reconnect_utility.h:351
void tryToReinsertNodes()
function tryToReinsertNodes Reinsert removed nodes into the structure
Definition: tetrahedra_reconnect_utility.h:662
virtual void PrintData(std::ostream &rOStream) const
Print object's data.
Definition: tetrahedra_reconnect_utility.h:718
void innerConvertFromKratos(ModelPart &r_model_part, TVolumeMesh *m)
function innerConvertFromKratos This function converts from Kratos format, to the inner structure
Definition: tetrahedra_reconnect_utility.h:122
#define KRATOS_THROW_ERROR(ExceptionType, ErrorMessage, MoreInfo)
Definition: define.h:77
z
Definition: GenerateWind.py:163
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
std::ostream & operator<<(std::ostream &rOStream, const LinearMasterSlaveConstraint &rThis)
output stream function
Definition: linear_master_slave_constraint.h:432
int step
Definition: face_heat.py:88
y
Other simbols definition.
Definition: generate_axisymmetric_navier_stokes_element.py:54
v
Definition: generate_convection_diffusion_explicit_element.py:114
int t
Definition: ode_solve.py:392
int j
Definition: quadrature.py:648
el
Definition: read_stl.py:25
N
Definition: sensitivityMatrix.py:29
x
Definition: sensitivityMatrix.py:49
integer i
Definition: TensorModule.f:17