# PhasicFlow: src/phasicFlow/triSurface/triSurface.cpp Source File
PhasicFlow is an open-source parallel DEM (discrete element method) package for simulating granular flow. It is developed in C++ and can be exectued on both GPU (like CUDA) and CPU.

triSurface.cpp
Go to the documentation of this file.
/*------------------------------- phasicFlow ---------------------------------
O C enter of
O O E ngineering and
O O M ultiscale modeling of
OOOOOOO F luid flow
------------------------------------------------------------------------------
Copyright (C): www.cemf.ir
email: hamid.r.norouzi AT gmail.com
------------------------------------------------------------------------------
Licence:
This file is part of phasicFlow code. It is a free software for simulating
granular and multiphase flows. You can redistribute it and/or modify it under
the terms of GNU General Public License v3 or any other later versions.
phasicFlow is distributed to help others in their research in the field of
granular and multiphase flows, but WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
-----------------------------------------------------------------------------*/
#include "triSurface.hpp"
#include "error.hpp"
#include "iOstream.hpp"
#include "Vectors.hpp"
#include "triSurfaceKernels.hpp"
namespace pFlow
{
bool convertToTriSurfaceComponents
(
uint32 basePointIndex,
span<realx3x3> tris,
realx3Vector& points,
uint32x3Vector& vertices,
realVector& area,
realx3Vector& normal
)
{
auto nt = tris.size();
points.clear();
points.reserve(nt);
vertices.clear();
vertices.resize(nt);
area.clear();
area.resize(nt);
normal.clear();
normal.resize(nt);
for(auto i=0; i<nt; i++)
{
uint32x3 newTri;
const auto& tri = tris[i];
if(!pFlow::triangle::valid(tri.comp1(),tri.comp2(), tri.comp3()))
{
fatalErrorInFunction<<
"Invalid triangle "<< tri << endl;
return false;
}
if(auto np = find(points, tri.comp1()); np<0 )
{
points.push_back(tri.comp1()); // new point
newTri.comp1() = static_cast< uint32 >( basePointIndex + points.size() - 1);
}
else
{
newTri.comp1() = basePointIndex+static_cast< uint32 >(np);
}
if(auto np = find(points, tri.comp2()); np<0 )
{
points.push_back(tri.comp2()); // new point
newTri.comp2() = static_cast< uint32 >( basePointIndex + points.size() - 1);
}
else
{
newTri.comp2() = basePointIndex+static_cast< uint32 >(np);
}
if(auto np = find(points, tri.comp3()); np<0 )
{
points.push_back(tri.comp3()); // new point
newTri.comp3() = static_cast< uint32 >( basePointIndex + points.size() - 1);
}
else
{
newTri.comp3() = basePointIndex+static_cast< uint32 >(np);
}
vertices[i] = newTri;
normal[i] = triangle::normal(tri.comp1(), tri.comp2(), tri.comp3());
area[i] = triangle::surface(tri.comp1(), tri.comp2(), tri.comp3());
}
return true;
}
}
bool pFlow::triSurface::calculateNormals()
{
return pFlow::triSurfaceKernels::calculateNormals(
points_,
vertices_,
normals_);
}
bool pFlow::triSurface::calculateArea()
{
return pFlow::triSurfaceKernels::calculateArea(
points_,
vertices_,
area_);
}
bool pFlow::triSurface::append
(
const realx3x3Field_H &triangles
)
{
uint32 basePointIndex = numPoints();
auto triData = triangles.getSpan();
realx3Vector points("points");
uint32x3Vector vertices("vertices");
realVector area("area");
realx3Vector normal("normal");
if( !convertToTriSurfaceComponents(
basePointIndex,
triData,
points,
vertices,
area,
normal))
{
fatalErrorInFunction<<
"Error in constructing triSuface from raw data "<<triangles.name()<<endl;
return false;
}
points_.append(points);
vertices_.append(vertices);
area_.append(area);
normals_.append(normal);
return true;
}
bool pFlow::triSurface::append(const realx3x3Vector &triangles)
{
uint32 basePointIndex = numPoints();
auto triData = triangles.getSpan();
realx3Vector points("points");
uint32x3Vector vertices("vertices");
realVector area("area");
realx3Vector normal("normal");
if( !convertToTriSurfaceComponents(
basePointIndex,
triData,
points,
vertices,
area,
normal))
{
fatalErrorInFunction<<
"Error in constructing triSuface from raw data "<<triangles.name()<<endl;
return false;
}
points_.append(points);
vertices_.append(vertices);
area_.append(area);
normals_.append(normal);
return true;
}
pFlow::triSurface::triSurface
(
const objectFile &obj,
repository *owner
)
:
IOobject
(
obj,
IOPattern::AllProcessorsSimilar,
owner
)
{
// this constructor is used by multiTrisurface and read operation is
// done in that class
}
pFlow::triSurface::triSurface
(
const objectFile &objf,
repository* owner,
const triSurface &surf
)
:
IOobject
(
objectFile
(
objf.name(),
objf.localPath(),
objectFile::READ_NEVER,
objf.wFlag()
),
IOPattern::AllProcessorsSimilar,
owner
),
points_(surf.points_),
vertices_(surf.vertices_),
area_(surf.area_),
normals_(surf.normals_)
{}
pFlow::triSurface::triSurface
(
const realx3x3Field_H &triangles,
repository *owner
)
:
IOobject
(
objectFile(
triangles.name(),
"",
IOobject::READ_NEVER,
IOobject::WRITE_ALWAYS),
IOPattern::AllProcessorsSimilar,
owner
)
{
if( !append(triangles) )
{
fatalExit;
}
}
bool pFlow::triSurface::read(iIstream &is, const IOPattern &iop)
{
points_.clear();
if(!points_.read(is, iop, true))
{
ioErrorInFile(is.name(), is.lineNumber())<<
" when reading field "<< points_.name()<<endl;
return false;
}
vertices_.clear();
if(!vertices_.read(is, iop, true))
{
ioErrorInFile(is.name(), is.lineNumber())<<
" when reading field "<< vertices_.name()<<endl;
return false;
}
normals_.reallocate(vertices_.capacity(), vertices_.size());
area_.reallocate(vertices_.capacity(), vertices_.size());
calculateArea();
calculateNormals();
return true;
}
bool pFlow::triSurface::write(iOstream &os, const IOPattern &iop) const
{
if( !points_.write(os, iop))
{
ioErrorInFile(os.name(), os.lineNumber())<<
"when writing field "<<points_.name()<<endl;
return false;
}
if( !vertices_.write(os,iop) )
{
ioErrorInFile(os.name(), os.lineNumber())<<
"when writing field "<<vertices_.name()<<endl;
return false;
}
return true;
}
pFlow::span
Definition: span.hpp:31
pFlow::triSurfaceKernels::calculateNormals
INLINE_FUNCTION_H bool calculateNormals(const realx3Field_D &points, const uint32x3Field_D &vertices, realx3Field_D &normals)
Definition: triSurfaceKernels.hpp:57
fatalExit
#define fatalExit
Fatal exit.
Definition: error.hpp:98
pFlow::triSurface::normals_
realx3Field_D normals_
normal vector of triangles
Definition: triSurface.hpp:119
pFlow::triangle::valid
INLINE_FUNCTION_HD bool valid(const realx3 &p1, const realx3 &p2, const realx3 &p3)
Definition: triangleFunctions.hpp:49
pFlow::find
int64 find(Vector< T, Allocator > &vec, const T &val)
Definition: VectorAlgorithm.hpp:69
pFlow::triple::comp3
INLINE_FUNCTION_HD T & comp3()
access component
Definition: triple.hpp:192
pFlow::Vector::reserve
void reserve(size_t cap)
Reserve capacity for vector Preserve the content.
Definition: Vector.hpp:284
pFlow::uint32
unsigned int uint32
Definition: builtinTypes.hpp:56
pFlow::span::size
INLINE_FUNCTION_HD uint32 size() const
Returns the number of elements in the span.
Definition: span.hpp:101
pFlow::Vector::getSpan
auto getSpan()
Definition: Vector.hpp:296
pFlow::triSurface::calculateNormals
bool calculateNormals()
Definition: triSurface.cpp:107
pFlow::triangle::normal
INLINE_FUNCTION_HD realx3 normal(const realx3 &p1, const realx3 &p2, const realx3 &p3)
Definition: triangleFunctions.hpp:38
pFlow::Vector::size
auto size() const
Size of the vector.
Definition: Vector.hpp:265
pFlow::objectFile::WRITE_ALWAYS
@ WRITE_ALWAYS
Definition: objectFile.hpp:43
Vectors.hpp
pFlow::triSurface::write
bool write(iOstream &os, const IOPattern &iop) const override
Definition: triSurface.cpp:282
pFlow::endl
iOstream & endl(iOstream &os)
Add newline and flush stream.
Definition: iOstream.hpp:341
pFlow::Field
Definition: Field.hpp:36
pFlow::Field::name
word name() const
Definition: Field.hpp:182
pFlow::triSurface::points_
realx3Field_D points_
points of triangles
Definition: triSurface.hpp:110
pFlow
Definition: demGeometry.hpp:27
pFlow::IOobject
Definition: IOobject.hpp:35
pFlow::iIstream
Interface class for any input stream
Definition: iIstream.hpp:37
fatalErrorInFunction
#define fatalErrorInFunction
Report a fatal error and function name and exit the application.
Definition: error.hpp:77
triSurfaceKernels.hpp
pFlow::VectorSingle::getSpan
INLINE_FUNCTION_H auto getSpan()
Definition: VectorSingle.cpp:569
pFlow::triSurface::vertices_
uint32x3Field_D vertices_
vectices indices of triangles
Definition: triSurface.hpp:113
pFlow::objectFile::name
virtual const word & name() const
Definition: objectFile.hpp:101
pFlow::IOPattern
Definition: IOPattern.hpp:32
pFlow::triple::comp1
INLINE_FUNCTION_HD T & comp1()
access component
Definition: triple.hpp:168
pFlow::objectFile
Definition: objectFile.hpp:30
pFlow::IOstream::name
virtual const word & name() const
Return the name of the stream.
Definition: IOstream.cpp:31
pFlow::objectFile::READ_NEVER
@ READ_NEVER
Definition: objectFile.hpp:37
pFlow::triSurfaceKernels::calculateArea
INLINE_FUNCTION_H bool calculateArea(const realx3Field_D &points, const uint32x3Field_D &vertices, realField_D &area)
Definition: triSurfaceKernels.hpp:32
pFlow::triSurface::read
bool read(iIstream &is, const IOPattern &iop) override
Definition: triSurface.cpp:257
pFlow::Vector::name
const word & name() const
Name of the vector.
Definition: Vector.hpp:259
ioErrorInFile
#define ioErrorInFile(fileName, lineNumber)
Report an error in file operation with supplied fileName and lineNumber.
Definition: error.hpp:87
pFlow::IOPattern::AllProcessorsSimilar
@ AllProcessorsSimilar
Definition: IOPattern.hpp:55
pFlow::objectFile::localPath
virtual const fileSystem & localPath() const
Definition: objectFile.hpp:107
pFlow::repository
Definition: repository.hpp:34
pFlow::IOstream::lineNumber
int32 lineNumber() const
Const access to the current stream line number.
Definition: IOstream.hpp:223
pFlow::triSurface::area_
realField_D area_
area of each triangle
Definition: triSurface.hpp:116
iOstream.hpp
pFlow::triSurface::triSurface
triSurface(const objectFile &obj, repository *owner)
Definition: triSurface.cpp:191
pFlow::triSurface
Definition: triSurface.hpp:103
pFlow::triple
A set of 3 variables that can be used for vector variables.
Definition: triple.hpp:36
pFlow::triSurface::calculateArea
bool calculateArea()
Definition: triSurface.cpp:115
pFlow::Vector< realx3 >
pFlow::convertToTriSurfaceComponents
bool convertToTriSurfaceComponents(uint32 basePointIndex, span< realx3x3 > tris, realx3Vector &points, uint32x3Vector &vertices, realVector &area, realx3Vector &normal)
Definition: triSurface.cpp:33
pFlow::iOstream
Interface class for any output stream.
Definition: iOstream.hpp:59
pFlow::objectFile::wFlag
writeFlag wFlag() const
Definition: objectFile.hpp:119
triSurface.hpp
pFlow::triSurface::append
bool append(const realx3x3Field_H &triangles)
Definition: triSurface.cpp:124
pFlow::triangle::surface
INLINE_FUNCTION_HD real surface(const realx3 &p1, const realx3 &p2, const realx3 &p3)
Definition: triangleFunctions.hpp:30
pFlow::triple::comp2
INLINE_FUNCTION_HD T & comp2()
access component
Definition: triple.hpp:180
error.hpp