TrioCFD 1.9.9_beta
TrioCFD documentation
Loading...
Searching...
No Matches
Polyedriser.cpp
1/****************************************************************************
2* Copyright (c) 2024, CEA
3* All rights reserved.
4*
5* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
9*
10* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
11* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
12* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
13*
14*****************************************************************************/
15
16#include <Polyedriser.h>
17#include <Domaine.h>
18#include <Scatter.h>
19#include <Polyedre.h>
20#include <vector>
21
22
23
24// Numbering convention
25// vertices faces 4(face z=1)
26// 6------7 *------*
27// /| /| /| 3 /|
28// 2------3 | *------* |
29// | | | | |0| |1|
30// | 4----|-5 | *----|-*
31// |/ |/ |/ 2 |/
32// 0------1 *------*
33// 5(face z=0)
34
35static int faces_sommets_poly[6][4] =
36{
37 { 0, 4, 6, 2 },
38 { 1, 5, 7, 3 },
39 { 0, 2, 3, 1 },
40 { 4, 6, 7, 5 },
41 { 2, 6, 7, 3 },
42 { 0, 4, 5, 1 }
43};
44
45Implemente_instanciable_32_64(Polyedriser_32_64,"Polyedriser",Interprete_geometrique_base_32_64<_T_>);
46// XD polyedriser interprete polyedriser INHERITS_BRACE cast hexahedra into polyhedra so that the indexing of the mesh
47// XD_CONT vertices is compatible with PolyMAC_HFV discretization. Must be used in PolyMAC_HFV discretization if a
48// XD_CONT hexahedral mesh has been produced with TRUST's internal mesh generator.
49// XD attr domain_name ref_domaine domain_name REQ Name of domain.
50
51template <typename _SIZE_>
53{
54 return os;
55}
56
57template <typename _SIZE_>
59{
60 return is;
61}
62
63
64// compute the angle between the vectors u and v (which are coplanar)
65static double computeAngleBetweenCoplanarVectors(std::vector<double> u, std::vector<double> v,
66 std::vector<double> normale)
67{
68 double dot = u[0]*v[0] + u[1]*v[1] + u[2]*v[2];
69 double det = u[0]*v[1]*normale[2]
70 + v[0]*normale[1]*u[2]
71 + normale[0]*u[1]*v[2]
72 - u[2]* v[1] *normale[0]
73 - v[2]* normale[1] *u[0]
74 - normale[2]* u[1] *v[0];
75
76 double angle = atan2(det, dot);
77 if(det == 0.0)
78 {
79 int dir = 0;
80 while(v[dir] == 0.0) dir++;
81 double quotient = u[dir] / v[dir];
82 if( quotient > 0)
83 angle = 0.0;
84 else if( quotient < 0 )
85 angle = M_PI;
86 }
87 else if(det < 0)
88 angle += 2*M_PI;
89
90 return angle;
91}
92
93// reordering vertices inside the faces in trigonometric order
94// (or anti-trigonometric, depending on the orientation of the normal)
95template <typename _SIZE_>
96static void reorder_vertices(Faces_32_64<_SIZE_>& faces, const DoubleTab_T<_SIZE_>& coords)
97{
98 using int_t = _SIZE_;
99 int nb_sommets = faces.les_sommets().dimension_int(1);
100 int_t nb_faces = faces.nb_faces();
101 for(int_t face = 0; face < nb_faces; face++)
102 {
103 // computing the center of gravity of the face
104 std::vector<double> center(3,0.0);
105 for(int s=0; s < nb_sommets; s++)
106 for(int dir=0; dir<3; dir++)
107 center[dir] += coords(faces.sommet(face,s),dir);
108 for(int dir=0; dir<3; dir++)
109 center[dir] /= nb_sommets;
110
111 // computing the normal to the face
112 int_t s0 = faces.sommet(face, 0),
113 s1 = faces.sommet(face, 1),
114 s2 = faces.sommet(face, 2);
115 std::vector<double> u =
116 {
117 coords(s1,0) - coords(s0,0),
118 coords(s1,1) - coords(s0,1),
119 coords(s1,2) - coords(s0,2),
120 };
121 std::vector<double> v =
122 {
123 coords(s2,0) - coords(s0,0),
124 coords(s2,1) - coords(s0,1),
125 coords(s2,2) - coords(s0,2),
126 };
127 std::vector<double> normale =
128 {
129 u[1]*v[2] - u[2]*v[1] ,
130 u[2]*v[0] - u[0]*v[2] ,
131 u[0]*v[1] - u[1]*v[0] ,
132 };
133 double normale_norm = sqrt( normale[0]*normale[0] + normale[1]*normale[1] + normale[2]*normale[2] );
134 for(int dir=0; dir<3; dir++) normale[dir] /= normale_norm;
135
136 //reference vector used to compute angles
137 std::vector<double> vec_ref = { coords(faces.sommet(face,0),0) - center[0],
138 coords(faces.sommet(face,0),1) - center[1],
139 coords(faces.sommet(face,0),2) - center[2]
140 };
141
142 //sorting the vertices in the face according to their angle formed with the reference vector
143 //in ascending order
144 for (int i = 0; i < nb_sommets-1; i++)
145 {
146 for (int j = 0; j < nb_sommets-i-1; j++)
147 {
148 std::vector<double> vec1 = {coords(faces.sommet(face,j),0)-center[0],
149 coords(faces.sommet(face,j),1)-center[1],
150 coords(faces.sommet(face,j),2)-center[2]
151 };
152 std::vector<double> vec2 = {coords(faces.sommet(face,j+1),0)-center[0],
153 coords(faces.sommet(face,j+1),1)-center[1],
154 coords(faces.sommet(face,j+1),2)-center[2]
155 };
156 double angle1 = computeAngleBetweenCoplanarVectors(vec1, vec_ref, normale);
157 double angle2 = computeAngleBetweenCoplanarVectors(vec2, vec_ref, normale);
158
159 if (angle1 > angle2)
160 {
161 int_t tmp = faces.sommet(face,j);
162 faces.sommet(face, j) = faces.sommet(face,j+1);
163 faces.sommet(face, j+1) = tmp;
164 }
165 }
166 }
167 }
168 return;
169}
170
171template <typename _SIZE_>
173{
174 if(domaine.type_elem()->que_suis_je() == "Hexaedre")
175 {
176 domaine.typer("Polyedre");
177 Polyedre_32_64<_SIZE_>& p = ref_cast(Polyedre_32_64<_SIZE_>, domaine.type_elem().valeur());
178
179 ArrOfInt_t Pi, Fi, N;
180 const IntTab_t& elements = domaine.les_elems();
181 int_t nb_elems = elements.dimension(0);
182 Pi.resize_array(nb_elems+1);
183 Fi.resize_array(nb_elems*6+1);
184 N.resize_array(nb_elems*24);
185
186 int_t face = 0;
187 int_t node = 0;
188 for (int_t e = 0; e < nb_elems; e++)
189 {
190 Pi[e] = face; // Index of polyhedra
191
192 for(int f=0; f<6; f++)
193 {
194 Fi[face] = face*4; // Index of faces:
195 for(int s=0; s<4; s++)
196 {
197 int som_loc = faces_sommets_poly[f][s];
198 N[node] = elements(e, som_loc);
199 node++;
200 }
201 face++;
202 }
203 }
204 Fi[nb_elems*6] = node;
205 Pi[nb_elems] = face;
206 p.affecte_connectivite_numero_global(N, Fi, Pi, domaine.les_elems());
207 }
208 else
209 {
210 Cerr << "We do not yet know how to Polyedriser_32_64 the "
211 << domaine.type_elem()->que_suis_je() <<"s"<<finl;
212 Cerr << "Try to use convertAllToPoly option of Lire_MED|Read_MED keyword if you read a MED file for your mesh." << finl;
214 }
215
216 const DoubleTab_t& coords = domaine.coord_sommets();
217
218 for (auto &itr : domaine.faces_bord())
219 {
220 Faces_t& les_faces = itr.faces();
221 les_faces.typer(Type_Face::polygone_3D);
222 reorder_vertices(les_faces, coords);
223 }
224
225 for (auto &itr : domaine.faces_raccord())
226 {
227 Faces_t& les_faces = itr->faces();
228 les_faces.typer(Type_Face::polygone_3D);
229 reorder_vertices(les_faces, coords);
230 }
231
232 for (auto &itr : domaine.bords_int())
233 {
234 Faces_t& les_faces = itr.faces();
235 les_faces.typer(Type_Face::polygone_3D);
236 reorder_vertices(les_faces, coords);
237 }
238
239 for (auto &itr : domaine.groupes_faces())
240 {
241 Faces_t& les_faces = itr.faces();
242 les_faces.typer(Type_Face::polygone_3D);
243 reorder_vertices(les_faces, coords);
244 }
245 return;
246}
247
248template <typename _SIZE_>
250{
252 {
253 Cerr << "Interpreter "<<this->que_suis_je()<<" cannot be applied for dimension " << Objet_U::dimension <<finl;
255 }
257 {
258 Cerr << "Interpreter "<<this->que_suis_je()<<" cannot be applied during a parallel calculation !" << finl;
259 Cerr << "The mesh can't be changed after it has been partitioned." << finl;
261 }
262
263 this->associer_domaine(is);
265 polyedriser(this->domaine());
267 return is;
268}
269
270
271
272template class Polyedriser_32_64<int>;
273#if INT_is_64_ == 2
274template class Polyedriser_32_64<trustIdType>;
275#endif
276
277
Class defining operators and methods for all reading operation in an input flow (file,...
Definition Entree.h:42
Class Faces Faces describes a set of faces by their type (point, segment, triangle or quadrangle),...
Definition Faces.h:50
void typer(const Motcle &)
Sets the type of the faces.
Definition Faces.cpp:382
int_t nb_faces() const
Definition Faces.h:66
const IntTab_t & les_sommets() const
Returns the array of vertices of all faces.
Definition Faces.h:74
int_t sommet(int_t, int) const
Returns the number of the j-th vertex of the i-th face.
Definition Faces.h:130
Class Interprete_geometrique_base.
friend class Entree
Definition Objet_U.h:71
static int dimension
Definition Objet_U.h:94
const Nom & que_suis_je() const
Returns the string identifying the class.
Definition Objet_U.cpp:104
virtual Entree & readOn(Entree &)
Reads an Objet_U from an input stream. Virtual method to override.
Definition Objet_U.cpp:289
virtual Sortie & printOn(Sortie &) const
Writes the object to an output stream. Virtual method to override.
Definition Objet_U.cpp:278
Class Polyedre: represents the Polyedre geometric element.
Definition Polyedre.h:29
void affecte_connectivite_numero_global(const ArrOfInt_t &Nodes, const ArrOfInt_t &FacesIndex, const ArrOfInt_t &PolyhedronIndex, IntTab_t &les_elems)
Definition Polyedre.cpp:350
Polyedriser Class intended to convert a hexahedron into a polyhedron.
Definition Polyedriser.h:26
Domaine_32_64< _SIZE_ > Domaine_t
Definition Polyedriser.h:36
void polyedriser(Domaine_t &) const
Faces_32_64< _SIZE_ > Faces_t
Definition Polyedriser.h:37
DoubleTab_T< _SIZE_ > DoubleTab_t
Definition Polyedriser.h:34
ArrOfInt_T< _SIZE_ > ArrOfInt_t
Definition Polyedriser.h:31
Entree & interpreter_(Entree &) override
IntTab_T< _SIZE_ > IntTab_t
Definition Polyedriser.h:33
int dimension_application() const
Definition Polyedriser.h:40
static bool is_parallel()
Definition Process.cpp:108
static void exit(int exit_code=-1)
Exit routine for TRUST within a Kokkos region.
Definition Process.cpp:466
static void init_sequential_domain(Domaine_32_64< _SIZE_ > &dom)
Create parallel descriptors for the vertex and element arrays of the domain (necessary because Scatte...
Definition Scatter.cpp:2739
static void uninit_sequential_domain(Domaine_32_64< _SIZE_ > &dom)
Method used by interpreters that modify the domain (sequential), destroys the descriptors of vertices...
Definition Scatter.cpp:2754
Base class for output streams.
Definition Sortie.h:52
void resize_array(_SIZE_ new_size, RESIZE_OPTIONS opt=RESIZE_OPTIONS::COPY_INIT)
int dimension_int(int d) const
Definition TRUSTTab.tpp:152
_SIZE_ dimension(int d) const
Definition TRUSTTab.tpp:133