triSurface.cpp
Go to the documentation of this file.
1 /*------------------------------- phasicFlow ---------------------------------
2  O C enter of
3  O O E ngineering and
4  O O M ultiscale modeling of
5  OOOOOOO F luid flow
6 ------------------------------------------------------------------------------
7  Copyright (C): www.cemf.ir
8  email: hamid.r.norouzi AT gmail.com
9 ------------------------------------------------------------------------------
10 Licence:
11  This file is part of phasicFlow code. It is a free software for simulating
12  granular and multiphase flows. You can redistribute it and/or modify it under
13  the terms of GNU General Public License v3 or any other later versions.
14 
15  phasicFlow is distributed to help others in their research in the field of
16  granular and multiphase flows, but WITHOUT ANY WARRANTY; without even the
17  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 
19 -----------------------------------------------------------------------------*/
20 
21 
22 #include "triSurface.hpp"
23 #include "error.hpp"
24 #include "iOstream.hpp"
25 #include "triSurfaceKernels.hpp"
26 
27 
29 (
30  const realx3x3 & tri,
31  realx3Vector& points,
32  int32x3Vector& vertices
33 )
34 {
35  int32x3 newTri;
36 
37  // first point
38  if ( int32 i = find(points, tri.x()) ; i > -1)
39  {
40  newTri.x() = i; // existing point
41  }
42  else
43  {
44  points.push_back(tri.x()); // new point
45  newTri.x() = static_cast<int32>(points.size()) - 1;
46  }
47 
48  // second point
49 
50  if ( int32 j = find(points, tri.y()) ; j > -1)
51  {
52  newTri.y() = j; // existing point
53  }
54  else
55  {
56  points.push_back(tri.y()); // new point
57  newTri.y() = static_cast<int32>(points.size()) - 1;
58  }
59 
60  // third point
61 
62  if ( int32 k = find(points, tri.z()) ; k > -1)
63  {
64  newTri.z() = k; // existing point
65  }
66  else
67  {
68  points.push_back(tri.z()); // new point
69  newTri.z() = static_cast<int32>(points.size()) - 1;
70  }
71 
72  // adds the new tirple vertices to the list
73  vertices.push_back( newTri );
74 
75  //output<< " points " <<points << endl;
76  //output<< " vertices "<<vertices <<endl;
77 
78  return static_cast<int32>(vertices.size()) -1;
79 }
80 
82 {
83 
84  int32 maxIdx = -1;
85 
86  if(vertices_.size()>0)
87  {
88  auto verDeviceVec = vertices_.deviceVectorAll();
89 
90  Kokkos::parallel_reduce(
91  "triSurface::calcMaxIndex",
92  vertices_.size(),
93  LAMBDA_HD(int32 i, int32& valueToUpdate){
94  valueToUpdate = max(valueToUpdate, max(verDeviceVec[i]));
95  },
96  Kokkos::Max<int32>( maxIdx )
97  );
98  }
99 
100  return maxIdx;
101 }
102 
104 {
105 
106  auto maxIdx = calcMaxIndex();
107  maxIndex_ = maxIdx;
108  if( maxIdx >= static_cast<int32>(points_.size())) return false;
109  return true;
110 }
111 
112 
113 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
115 :
116  points_("points"),
117  vertices_("vertices"),
118  area_("area"),
119  maxIndex_(-1)
120 {}
121 
123 (
124  const realx3Vector& points,
125  const int32x3Vector& vertices
126 )
127 :
128  points_("points", "points", points),
129  vertices_("vertices", "vertices", vertices),
130  area_("area", "area"),
131  maxIndex_(-1)
132 {
133  if( check() )
134  {
136  "the indices and number of points do not match. \n";
137  fatalExit;
138  }
139 
140  area_.reallocate(vertices_.capacity(), vertices_.size());
141 
142  pFlow::triSurfaceKernels::calculateArea(points_, vertices_, area_);
143 
144 }
145 
146 
148 (
149  const realx3x3Vector& triangles
150 )
151 :
152  points_("points"),
153  vertices_("vertices"),
154  area_("area")
155 {
156 
157  Vector<realx3> points;
158  Vector<int32x3> vertices;
159 
160  points.clear();
161  vertices.clear();
162 
163  for( const auto& tr: triangles)
164  {
165  if(auto i= addTriangle(tr, points, vertices); i < 0 )
166  {
168  "failed to insert a triangle into triSurface. \n";
169  fatalExit;
170  }
171  }
172 
173  points_.assign(points);
174 
175  vertices_.assign(vertices);
176 
177  area_.reallocate(vertices_.capacity(), vertices_.size());
178 
179  pFlow::triSurfaceKernels::calculateArea(points_, vertices_, area_);
180 
181  if( !check() )
182  {
184  "the indices and number of points do not match. \n";
185  fatalExit;
186  }
187 }
188 
189 
191 (
192  iIstream& is
193 )
194 {
195 
197 
198  is >> points_;
200 
201  is >> vertices_;
203 
204  if( !check() )
205  {
206  ioErrorInFile( is.name(), is.lineNumber() ) <<
207  "the indices and number of points do not match. \n";
208  return false;
209  }
210 
211  area_.reallocate(vertices_.capacity(), vertices_.size());
212 
213  pFlow::triSurfaceKernels::calculateArea(points_, vertices_, area_);
214 
215  return true;
216 }
217 
219 (
220  iOstream& os
221 )const
222 {
223 
224  os << points_;
225  os << vertices_;
226 
227  return os.check(FUNCTION_NAME);;
228 }
229 
230 
231 
232 
fatalExit
#define fatalExit
Definition: error.hpp:57
pFlow::find
int64 find(Vector< T, Allocator > &vec, const T &val)
Definition: VectorAlgorithm.hpp:69
pFlow::triSurface::calcMaxIndex
int32 calcMaxIndex() const
Definition: triSurface.cpp:81
FUNCTION_NAME
#define FUNCTION_NAME
Definition: pFlowMacros.hpp:29
pFlow::Vector::size
auto size() const
Definition: Vector.hpp:299
pFlow::triSurface::writeTriSurface
bool writeTriSurface(iOstream &os) const
Definition: triSurface.cpp:219
pFlow::triple::y
INLINE_FUNCTION_HD T & y()
Definition: triple.hpp:141
pFlow::IOstream::check
virtual bool check(const char *operation) const
Definition: IOstream.cpp:42
pFlow::triSurface::vertices_
int32x3Field_D vertices_
vectices indices of triangles
Definition: triSurface.hpp:114
pFlow::triSurface::check
bool check()
Definition: triSurface.cpp:103
pFlow::iIstream
Definition: iIstream.hpp:33
fatalErrorInFunction
#define fatalErrorInFunction
Definition: error.hpp:42
pFlow::int32
int int32
Definition: builtinTypes.hpp:53
triSurfaceKernels.hpp
pFlow::IOstream::fatalCheck
bool fatalCheck(const char *operation) const
Definition: IOstream.cpp:48
pFlow::triSurfaceKernels::calculateArea
INLINE_FUNCTION_H bool calculateArea(const realx3Field_D &points, const int32x3Field_D &vertices, realField_D &area)
Definition: triSurfaceKernels.hpp:32
pFlow::IOstream::name
virtual const word & name() const
Definition: IOstream.cpp:31
pFlow::triple::z
INLINE_FUNCTION_HD T & z()
Definition: triple.hpp:144
pFlow::max
T max(const Vector< T, Allocator > &v)
Definition: VectorMath.hpp:164
pFlow::Vector::clear
auto clear()
Definition: Vector.hpp:248
pFlow::triSurface::triSurface
triSurface()
Definition: triSurface.cpp:114
LAMBDA_HD
#define LAMBDA_HD
Definition: pFlowMacros.hpp:54
ioErrorInFile
#define ioErrorInFile(fileName, lineNumber)
Definition: error.hpp:49
pFlow::IOstream::lineNumber
int32 lineNumber() const
Definition: IOstream.hpp:187
iOstream.hpp
pFlow::triple::x
INLINE_FUNCTION_HD T & x()
Definition: triple.hpp:138
pFlow::VectorSingle::size
INLINE_FUNCTION_H size_t size() const
Definition: VectorSingle.hpp:360
pFlow::VectorSingle::deviceVectorAll
INLINE_FUNCTION_H viewType & deviceVectorAll()
Definition: VectorSingle.hpp:295
pFlow::triple
Definition: triple.hpp:37
pFlow::Vector< realx3 >
pFlow::iOstream
Definition: iOstream.hpp:53
pFlow::triSurface::readTriSurface
bool readTriSurface(iIstream &is)
Definition: triSurface.cpp:191
triSurface.hpp
pFlow::triSurface::addTriangle
int32 addTriangle(const realx3x3 &tri, realx3Vector &points, int32x3Vector &vertices)
Definition: triSurface.cpp:29
error.hpp