Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Xiangyu/revise sycl diffusion #748

Closed
wants to merge 34 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
7d46099
naming
Xiangyu-Hu Feb 5, 2025
8e977ef
not finished
Xiangyu-Hu Feb 5, 2025
8586772
to check locally
Xiangyu-Hu Feb 5, 2025
a0e6edc
2 and 3d mesh vtu and vtp use shared code
Xiangyu-Hu Feb 5, 2025
ec8b7c1
include io_vtk.hpp
Xiangyu-Hu Feb 6, 2025
282ec5e
add surface indication from shape
Xiangyu-Hu Feb 6, 2025
e2086b9
add 3d dambreak ck case
Xiangyu-Hu Feb 7, 2025
6112b22
to add sycl version
Xiangyu-Hu Feb 7, 2025
91afdd7
case runs but need to check locally
Xiangyu-Hu Feb 7, 2025
cdbc5b1
drafting 3d stfb case
Xiangyu-Hu Feb 7, 2025
363dfa8
to add sycl version
Xiangyu-Hu Feb 7, 2025
8192c03
to test locally
Xiangyu-Hu Feb 7, 2025
869c901
to test cpu sycl
Xiangyu-Hu Feb 7, 2025
becf394
no mixing shared and device memory in single expression.
Xiangyu-Hu Feb 7, 2025
51341f0
I see strange behavior on Nvidia GPU (first run, ok, second run crash…
Xiangyu-Hu Feb 8, 2025
86988cf
the strange issue seems due to simbody library
Xiangyu-Hu Feb 8, 2025
bf42ae6
Merge branch 'master' into xiangyu/revise_FM_mesh
Xiangyu-Hu Feb 9, 2025
e748576
small change
Xiangyu-Hu Feb 9, 2025
09f32fe
update the regression test date for 3d dambreak_sycl
Xiangyu-Hu Feb 9, 2025
0e204ab
revise triangle mesh shape
Xiangyu-Hu Feb 9, 2025
8e722f3
to test locally
Xiangyu-Hu Feb 9, 2025
d49b984
add vtk out for triangle mesh observer
Xiangyu-Hu Feb 9, 2025
03ce1e0
to test locally
Xiangyu-Hu Feb 10, 2025
47d1908
a bug in compuitng the number of faces
Xiangyu-Hu Feb 10, 2025
b7f4faa
rename InteractArgs to DynamicsArgs for generalization in complex dyn…
Xiangyu-Hu Feb 11, 2025
cd345d3
add to sycl case
Xiangyu-Hu Feb 11, 2025
7ae7f23
all compiled
Xiangyu-Hu Feb 11, 2025
f82d281
use discrete variable for position in base particles
Xiangyu-Hu Feb 11, 2025
fe1fa5c
Update base_local_dynamics.h
Xiangyu-Hu Feb 12, 2025
a2cb9f7
revise 0d regression test case
Xiangyu-Hu Feb 12, 2025
23627e6
1. move three members in base particles into protected, 2. revamp the…
Xiangyu-Hu Feb 12, 2025
eccccb1
reverse back diffusion dynamics
Xiangyu-Hu Feb 12, 2025
30baf2d
ready to add contact diffusion
Xiangyu-Hu Feb 12, 2025
cbd4cda
one bug in register variable from geometric data
Xiangyu-Hu Feb 13, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 0 additions & 105 deletions src/for_3D_build/bodies/mesh_helper_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -403,111 +403,6 @@ void MeshFileHelpers::minimumDistance(StdVec<Real> &all_data_of_distance_between
}
}

void MeshFileHelpers::vtuFileHeader(std::ofstream &out_file)
{
out_file << "<VTKFile type=\"UnstructuredGrid\" version=\"1.0\" byte_order=\"LittleEndian\" header_type=\"UInt64\">\n";
out_file << "<UnstructuredGrid>\n";
out_file << "<FieldData>\n";
out_file << "<DataArray type=\"Int32\" Name=\"ispatch\" NumberOfTuples=\"1\" format=\"ascii\" RangeMin=\"0\" RangeMax=\"0\">\n";
out_file << "0\n";
out_file << "</DataArray>\n";
out_file << "</FieldData>\n";
}

void MeshFileHelpers::vtuFileNodeCoordinates(std::ofstream &out_file, StdLargeVec<Vecd> &node_coordinates_,
StdLargeVec<StdVec<size_t>> &elements_nodes_connection_, SPHBody &bounds_, Real &rangemax)
{
// Write point data
out_file << "<Piece NumberOfPoints=\"" << node_coordinates_.size() << "\" NumberOfCells=\"" << elements_nodes_connection_.size() << "\">\n";
out_file << "<PointData>\n";
out_file << "</PointData>\n";
out_file << "<CellData>\n";
out_file << "</CellData>\n";
out_file << "<Points>\n";
BoundingBox bounds = bounds_.getSPHSystemBounds();
Real first_max = bounds.first_.cwiseAbs().maxCoeff();
Real second_max = bounds.second_.cwiseAbs().maxCoeff();
rangemax = 1.03075 * (std::max(first_max, second_max));
out_file << "<DataArray type=\"Float64\" Name=\"Points\" NumberOfComponents=\"3\" format=\"ascii\" RangeMin=\"0\" RangeMax=\"" << rangemax << "\">\n";

size_t total_nodes = node_coordinates_.size();
for (size_t node = 0; node != total_nodes; ++node)
{
out_file << node_coordinates_[node][0] << " " << node_coordinates_[node][1] << " " << node_coordinates_[node][2] << " \n";
}
}

void MeshFileHelpers::vtuFileInformationKey(std::ofstream &out_file, Real &rangemax)
{
out_file << "<InformationKey name=\"L2_NORM_RANGE\" location=\"vtkDataArray\" length=\"2\">\n";
out_file << "<Value index=\"0\">\n";
out_file << "0\n";
out_file << "</Value>\n";
out_file << "<Value index=\"1\">\n";
out_file << rangemax << " \n";
out_file << "</Value>\n";
out_file << "</InformationKey>\n";
out_file << "<InformationKey name=\"L2_NORM_FINITE_RANGE\" location=\"vtkDataArray\" length=\"2\">\n";
out_file << "<Value index=\"0\">\n";
out_file << "0\n";
out_file << "</Value>\n";
out_file << "<Value index=\"1\">\n";
out_file << rangemax << " \n";
out_file << "</Value>\n";
out_file << "</InformationKey>\n";
out_file << "</DataArray>\n";
out_file << "</Points>\n";
}

void MeshFileHelpers::vtuFileCellConnectivity(std::ofstream &out_file, StdLargeVec<StdVec<size_t>> &elements_nodes_connection_, StdLargeVec<Vecd> &node_coordinates_)
{
out_file << "<Cells>\n";
// Write Cell data
out_file << "<DataArray type=\"Int64\" Name=\"connectivity\" format=\"ascii\" RangeMin=\"0\" RangeMax=\"" << node_coordinates_.size() - 1 << "\">\n";

for (const auto &cell : elements_nodes_connection_)
{
for (const auto &vertex : cell)
{
out_file << vertex << " ";
}
out_file << "\n";
}

out_file << "</DataArray>\n";
}

void MeshFileHelpers::vtuFileOffsets(std::ofstream &out_file, StdLargeVec<StdVec<size_t>> &elements_nodes_connection_)
{
out_file << "<DataArray type=\"Int64\" Name=\"offsets\" format=\"ascii\" RangeMin=\"4\" RangeMax=\"" << 4 * elements_nodes_connection_.size() << "\">\n";

size_t offset = 0;
for (const auto &face : elements_nodes_connection_)
{
offset += face.size();
out_file << offset << " ";
}
out_file << "\n</DataArray>\n";
}

void MeshFileHelpers::vtuFileTypeOfCell(std::ofstream &out_file, StdLargeVec<StdVec<size_t>> &elements_nodes_connection_)
{
size_t type = 10;
// Specifies type of cell 10 = tetrahedral
out_file << "<DataArray type=\"UInt8\" Name=\"types\" format=\"ascii\" RangeMin=\"10\" RangeMax=\"10\">\n";
for (const auto &types : elements_nodes_connection_)
{
for (size_t i = 0; i < types.size(); ++i)
{
out_file << type << " ";
}
out_file << "\n";
}
// Write face attribute data
out_file << "</DataArray>\n";
out_file << "</Cells>\n";
}

void MeshFileHelpers::numberOfNodesFluent(std::ifstream &mesh_file, size_t &number_of_points, std::string &text_line)
{

Expand Down
145 changes: 51 additions & 94 deletions src/for_3D_build/geometries/triangle_mesh_shape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,43 @@
namespace SPH
{
//=================================================================================================//
SimTK::ContactGeometry::TriangleMesh *TriangleMeshShape::generateTriangleMesh(const SimTK::PolygonalMesh &poly_mesh)
TriangleMeshShape::TriangleMeshShape(const std::string &shape_name)
: Shape(shape_name), triangle_mesh_(nullptr) {}
//=================================================================================================//
void TriangleMeshShape::initializeFromPolygonalMesh(const SimTK::PolygonalMesh &poly_mesh)
{
SimTK::ContactGeometry::TriangleMesh *triangle_mesh;
triangle_mesh = triangle_mesh_ptr_keeper_.createPtr<SimTK::ContactGeometry::TriangleMesh>(poly_mesh);
if (!SimTK::ContactGeometry::TriangleMesh::isInstance(*triangle_mesh))
triangle_mesh_ = triangle_mesh_ptr_keeper_.createPtr<TriangleMesh>(poly_mesh);
if (!TriangleMesh::isInstance(*triangle_mesh_))
{
std::cout << "\n Error the triangle mesh is not valid" << std::endl;
std::cout << __FILE__ << ':' << __LINE__ << std::endl;
throw;
}
std::cout << "num of faces:" << triangle_mesh->getNumFaces() << std::endl;
std::cout << "TriangleMesh:" << name_ << std::endl;
std::cout << "num of vertices:" << triangle_mesh_->getNumVertices() << std::endl;
std::cout << "num of faces:" << triangle_mesh_->getNumFaces() << std::endl;

std::vector<std::array<Real, 3>> vertices;
vertices.reserve(triangle_mesh_->getNumVertices());
for (int i = 0; i < triangle_mesh_->getNumVertices(); i++)
{
const auto &p = triangle_mesh_->getVertexPosition(i);
vertices.push_back({Real(p[0]), Real(p[1]), Real(p[2])});
}

return triangle_mesh;
std::vector<std::array<int, 3>> faces;
faces.reserve(triangle_mesh_->getNumFaces());
for (int i = 0; i < triangle_mesh_->getNumFaces(); i++)
{
auto f1 = triangle_mesh_->getFaceVertex(i, 0);
auto f2 = triangle_mesh_->getFaceVertex(i, 1);
auto f3 = triangle_mesh_->getFaceVertex(i, 2);
faces.push_back({f1, f2, f3});
}
triangle_mesh_distance_.construct(vertices, faces);
}
//=================================================================================================//
SimTK::ContactGeometry::TriangleMesh *TriangleMeshShape::getTriangleMesh()
TriangleMesh *TriangleMeshShape::getTriangleMesh()
{
if (triangle_mesh_ == nullptr)
{
Expand All @@ -31,51 +52,14 @@ SimTK::ContactGeometry::TriangleMesh *TriangleMeshShape::getTriangleMesh()
//=================================================================================================//
bool TriangleMeshShape::checkContain(const Vec3d &probe_point, bool BOUNDARY_INCLUDED)
{
SimTKVec2 uv_coordinate;
bool inside = false; // note that direct prediction is not reliable sometime.
int face_id;

SimTKVec3 closest_pnt = triangle_mesh_->findNearestPoint(EigenToSimTK(probe_point), inside, face_id, uv_coordinate);
Vec3d from_face_to_pnt = probe_point - SimTKToEigen(closest_pnt);
Real distance_to_pnt = from_face_to_pnt.norm();
Vec3d direction_to_pnt = from_face_to_pnt / (distance_to_pnt + TinyReal);
Vec3d face_normal = SimTKToEigen(triangle_mesh_->getFaceNormal(face_id));
Real cosine_angle = face_normal.dot(direction_to_pnt);

int ite = 0;
while (fabs(cosine_angle) < Eps)
{
Vec3d jittered = probe_point; // jittering
for (int l = 0; l != probe_point.size(); ++l)
jittered[l] = probe_point[l] + rand_uniform(-0.5, 0.5) * (SqrtEps + distance_to_pnt * 0.1);
Vec3d from_face_to_jittered = jittered - SimTKToEigen(closest_pnt);
Vec3d direction_to_jittered = from_face_to_jittered / (from_face_to_jittered.norm() + TinyReal);
cosine_angle = face_normal.dot(direction_to_jittered);

ite++;
if (ite > 100)
{
std::cout << "\n Error: TriangleMeshShape::checkContain not able to achieve! " << std::endl;
std::cout << __FILE__ << ':' << __LINE__ << std::endl;
exit(1);
}
}
Real distance = triangle_mesh_distance_.signed_distance(probe_point).distance;

return cosine_angle < 0.0 ? true : false;
return distance < 0.0 ? true : false;
}
//=================================================================================================//
Vecd TriangleMeshShape::findClosestPoint(const Vecd &probe_point)
{
bool inside = false;
int face_id;
SimTKVec2 norm;
SimTKVec3 closest_pnt = triangle_mesh_->findNearestPoint(SimTKVec3(probe_point[0], probe_point[1], probe_point[2]), inside, face_id, norm);
if (face_id < 0 && face_id > triangle_mesh_->getNumFaces())
{
std::cout << "\n Error the nearest point is not valid" << std::endl;
std::cout << __FILE__ << ':' << __LINE__ << std::endl;
throw;
}
auto closest_pnt = triangle_mesh_distance_.signed_distance(probe_point).nearest_point;
return Vecd(closest_pnt[0], closest_pnt[1], closest_pnt[2]);
}
//=================================================================================================//
Expand All @@ -101,30 +85,35 @@ TriangleMeshShapeBrick::TriangleMeshShapeBrick(Vecd halfsize, int resolution, Ve
const std::string &shape_name)
: TriangleMeshShape(shape_name)
{
SimTK::PolygonalMesh polymesh = SimTK::PolygonalMesh::createBrickMesh(SimTKVec3(halfsize[0], halfsize[1], halfsize[2]), resolution);
triangle_mesh_ = generateTriangleMesh(polymesh.transformMesh(SimTKVec3(translation[0], translation[1], translation[2])));
SimTK::PolygonalMesh poly_mesh = SimTK::PolygonalMesh::createBrickMesh(
SimTKVec3(halfsize[0], halfsize[1], halfsize[2]), resolution);
initializeFromPolygonalMesh(
poly_mesh.transformMesh(SimTKVec3(translation[0], translation[1], translation[2])));
}
//=================================================================================================//
TriangleMeshShapeBrick::TriangleMeshShapeBrick(const TriangleMeshShapeBrick::ShapeParameters &shape_parameters,
const std::string &shape_name)
TriangleMeshShapeBrick::TriangleMeshShapeBrick(
const TriangleMeshShapeBrick::ShapeParameters &shape_parameters, const std::string &shape_name)
: TriangleMeshShapeBrick(shape_parameters.halfsize_, shape_parameters.resolution_,
shape_parameters.translation_, shape_name) {}
//=================================================================================================//
TriangleMeshShapeSphere::TriangleMeshShapeSphere(Real radius, int resolution, Vec3d translation,
const std::string &shape_name)
: TriangleMeshShape(shape_name)
{
SimTK::PolygonalMesh polymesh = SimTK::PolygonalMesh::createSphereMesh(radius, resolution);
triangle_mesh_ = generateTriangleMesh(polymesh.transformMesh(SimTKVec3(translation[0], translation[1], translation[2])));
SimTK::PolygonalMesh poly_mesh = SimTK::PolygonalMesh::createSphereMesh(radius, resolution);
initializeFromPolygonalMesh(
poly_mesh.transformMesh(SimTKVec3(translation[0], translation[1], translation[2])));
}
//=================================================================================================//
TriangleMeshShapeCylinder::TriangleMeshShapeCylinder(SimTK::UnitVec3 axis, Real radius, Real halflength, int resolution,
Vecd translation, const std::string &shape_name)
TriangleMeshShapeCylinder::TriangleMeshShapeCylinder(
SimTK::UnitVec3 axis, Real radius, Real halflength, int resolution,
Vecd translation, const std::string &shape_name)
: TriangleMeshShape(shape_name)
{
SimTK::PolygonalMesh polymesh =
SimTK::PolygonalMesh poly_mesh =
SimTK::PolygonalMesh::createCylinderMesh(axis, radius, halflength, resolution);
triangle_mesh_ = generateTriangleMesh(polymesh.transformMesh(SimTKVec3(translation[0], translation[1], translation[2])));
initializeFromPolygonalMesh(
poly_mesh.transformMesh(SimTKVec3(translation[0], translation[1], translation[2])));
}
//=================================================================================================//
TriangleMeshShapeSTL::TriangleMeshShapeSTL(const std::string &filepathname, Vec3d translation,
Expand All @@ -137,43 +126,11 @@ TriangleMeshShapeSTL::TriangleMeshShapeSTL(const std::string &filepathname, Vec3
std::cout << __FILE__ << ':' << __LINE__ << std::endl;
throw;
}
SimTK::PolygonalMesh polymesh;
polymesh.loadStlFile(filepathname);
polymesh.scaleMesh(scale_factor);
polymesh.transformMesh(SimTKVec3(translation[0], translation[1], translation[2]));
triangle_mesh_ = generateTriangleMesh(polymesh);

std::vector<std::array<Real, 3>> vertices;
vertices.reserve(polymesh.getNumVertices());
for (int i = 0; i < polymesh.getNumVertices(); i++)
{
const auto &p = polymesh.getVertexPosition(i);
vertices.push_back({Real(p[0]), Real(p[1]), Real(p[2])});
}

std::vector<std::array<int, 3>> faces;
faces.reserve(polymesh.getNumFaces());
for (int i = 0; i < polymesh.getNumFaces(); i++)
{
auto f1 = polymesh.getFaceVertex(i, 0);
auto f2 = polymesh.getFaceVertex(i, 1);
auto f3 = polymesh.getFaceVertex(i, 2);
faces.push_back({f1, f2, f3});
}
triangle_mesh_distance_.construct(vertices, faces);
}
//=================================================================================================//
bool TriangleMeshShapeSTL::checkContain(const Vec3d &probe_point, bool BOUNDARY_INCLUDED)
{
Real distance = triangle_mesh_distance_.signed_distance(probe_point).distance;

return distance < 0.0 ? true : false;
}
//=================================================================================================//
Vecd TriangleMeshShapeSTL::findClosestPoint(const Vecd &probe_point)
{
auto closest_pnt = triangle_mesh_distance_.signed_distance(probe_point).nearest_point;
return Vecd(closest_pnt[0], closest_pnt[1], closest_pnt[2]);
SimTK::PolygonalMesh poly_mesh;
poly_mesh.loadStlFile(filepathname);
poly_mesh.scaleMesh(scale_factor);
poly_mesh.transformMesh(SimTKVec3(translation[0], translation[1], translation[2]));
initializeFromPolygonalMesh(poly_mesh);
}
//=================================================================================================//
} // namespace SPH
Loading
Loading