PeriDyno 1.0.0
Loading...
Searching...
No Matches
NormalForce.cu
Go to the documentation of this file.
1#include "NormalForce.h"
2
3namespace dyno
4{
5
6 template<typename TDataType>
7 NormalForce<TDataType>::NormalForce()
8 :ConstraintModule()
9 {
10 this->outNormalForce()->allocate();
11 }
12
13
14 template<typename TDataType>
15 NormalForce<TDataType>::~NormalForce()
16 {
17 mNormalForceFlag.clear();
18 }
19
20 template<typename Real, typename Coord>
21 __global__ void NormalForce_UpdateVelocity(
22 DArray<Coord> forces,
23 DArray<bool> flags,
24 DArray<Coord> Velocities,
25 DArray<Coord> positions,
26 DArray<Coord> normals,
27 Real dt,
28 Real scale
29 )
30 {
31 int pId = threadIdx.x + (blockIdx.x * blockDim.x);
32 if (pId >= forces.size()) return;
33
34 if (!flags[pId])
35 {
36 forces[pId] = Coord(0.0f);
37 Velocities[pId] = Coord(0.0f);
38 return;
39
40 }
41
42 forces[pId] = normals[pId] * scale;
43
44 Velocities[pId] += dt * (forces[pId]);
45 }
46
47
48 template<typename Coord>
49 Real __device__ PointToEdgeDistance(Coord O, Coord A, Coord B)
50 {
51 Coord AB = B - A;
52 Coord AO = O - A;
53 Coord AP = AB.dot(AO) * AB;
54 Coord P = A + AP;
55 return (O - P).norm();
56 }
57
58
59 template<typename Coord>
60 Real __device__ PointToMeshDistance(Coord O, Coord A, Coord B, Coord C, Coord normal)
61 {
62 Coord AB = B - A;
63 Coord AC = C - A;
64 Coord AO = O - A;
65 return AO.dot(normal) / normal.norm();
66
67 }
68
69
70 template<typename Coord>
71 __global__ void NormalForce_EmptyEdge(
72 DArray<bool> NormalForceFlag,
73 DArray<Coord> positions,
74 DArrayList<int> triangle_neighbors,
75 DArray<int> particleMeshID,
76 DArray<Coord> vertices,
77 DArray<TopologyModule::Triangle> triangles,
78 DArray<Coord> noramls,
79 DArray<TopologyModule::Edg2Tri> Edg2Tri,
80 DArray<TopologyModule::Tri2Edg> Tri2Edg
81 ) {
82 int pId = threadIdx.x + (blockIdx.x * blockDim.x);
83 if (pId >= positions.size()) return;
84
85 NormalForceFlag[pId] = true;
86
87 List<int>& nbrTriIds_i = triangle_neighbors[pId];
88 int nbrSize = nbrTriIds_i.size();
89
90 Real min_d = 0.0f;
91 int min_triID = -1;
92
93 int triangleID = particleMeshID[pId];
94 int& A_id = triangles[triangleID][0];
95 int& B_id = triangles[triangleID][1];
96 int& C_id = triangles[triangleID][2];
97
98 Real d_ab = PointToEdgeDistance(positions[pId], vertices[A_id], vertices[B_id]);
99 Real d_bc = PointToEdgeDistance(positions[pId], vertices[B_id], vertices[C_id]);
100 Real d_ac = PointToEdgeDistance(positions[pId], vertices[A_id], vertices[C_id]);
101
102
103 Real distance = PointToMeshDistance(positions[pId],
104 vertices[triangles[triangleID][0]],
105 vertices[triangles[triangleID][1]],
106 vertices[triangles[triangleID][2]],
107 noramls[pId]
108 );
109
110 if (distance > 0) {
111 NormalForceFlag[pId] = false;
112 }
113 }
114
115 template<typename TDataType>
116 void NormalForce<TDataType>::constrain()
117 {
118 //std::cout << "Normal Force!" << std::endl;
119 int num = this->inPosition()->size();
120
121 if (this->outNormalForce()->isEmpty())
122 this->outNormalForce()->allocate();
123
124 if (this->outNormalForce()->size() != num)
125 {
126 this->outNormalForce()->resize(num);
127 }
128
129 if (mNormalForceFlag.size() != num)
130 {
131 mNormalForceFlag.resize(num);
132 }
133
134 auto ts = this->inTriangleSet()->constDataPtr();
135 auto& vertices = ts->getPoints();
136 auto& triangles = ts->getTriangles();
137 auto& edge2tri = ts->getEdge2Triangle();
138 auto& tri2edge = ts->getTriangle2Edge();
139
140 cuExecute(num, NormalForce_EmptyEdge,
141 mNormalForceFlag,
142 this->inPosition()->getData(),
143 this->inTriangleNeighborIds()->getData(),
144 this->inParticleMeshID()->getData(),
145 vertices,
146 triangles,
147 this->inParticleNormal()->getData(),
148 edge2tri,
149 tri2edge
150 );
151
152 cuExecute(num, NormalForce_UpdateVelocity,
153 this->outNormalForce()->getData(),
154 mNormalForceFlag,
155 this->inVelocity()->getData(),
156 this->inPosition()->getData(),
157 this->inParticleNormal()->getData(),
158 this->inTimeStep()->getValue(),
159 this->varStrength()->getValue()
160 );
161
162 }
163
164 DEFINE_CLASS(NormalForce);
165}