PeriDyno 1.0.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
AdaptiveBoundary.cu
Go to the documentation of this file.
1#include "AdaptiveBoundary.h"
2#include "Algorithm/CudaRand.h"
3//#include "Log.h"
4//#include "Node.h"
5
6//#include "Module/AdaptiveBoundaryConstraint.h"
7
8//#include "Topology/DistanceField3D.h"
9//#include "Topology/TriangleSet.h"
10
11namespace dyno
12{
13 IMPLEMENT_TCLASS(AdaptiveBoundary, TDataType)
14
15 template<typename TDataType>
16 AdaptiveBoundary<TDataType>::AdaptiveBoundary()
17 : Node()
18 {
19 this->varNormalFriction()->setValue(0.95);
20 this->varTangentialFriction()->setValue(0.0);
21 }
22
23 template<typename TDataType>
24 AdaptiveBoundary<TDataType>::~AdaptiveBoundary()
25 {
26 }
27
28 template<typename Real, typename Coord>
29 __global__ void K_ConstrainSDF(
30 DArray<Coord> posArr,
31 DArray<Coord> velArr,
32 DArray<Real> posSDF,
33 DArray<Coord> posNormal,
34 Real normalFriction,
35 Real tangentialFriction,
36 Real dt)
37 {
38 int pId = threadIdx.x + (blockIdx.x * blockDim.x);
39 if (pId >= posArr.size()) return;
40
41 Coord pos = posArr[pId];
42 Coord vec = velArr[pId];
43
44 Real dist = posSDF[pId];
45 Coord normal = posNormal[pId];
46 // constrain particle
47 if (dist <= 0)
48 {
49 Real olddist = -dist;
50 RandNumber rGen(pId);
51 dist = 0.0001f*rGen.Generate();
52 // reflect position
53 pos -= (olddist + dist)*normal;
54 // reflect velocity
55 Real vlength = vec.norm();
56 Real vec_n = vec.dot(normal);
57 Coord vec_normal = vec_n * normal;
58 Coord vec_tan = vec - vec_normal;
59 if (vec_n > 0) vec_normal = -vec_normal;
60 vec_normal *= (1.0f - normalFriction);
61 vec = vec_normal + vec_tan * (1.0f - tangentialFriction);
62 //vec *= pow(Real(M_E), -dt*tangentialFriction);
63 //vec *= (1.0f - tangentialFriction);
64 }
65
66 posArr[pId] = pos;
67 velArr[pId] = vec;
68 }
69
70 template<typename TDataType>
71 void AdaptiveBoundary<TDataType>::updateStates()
72 {
73 Real dt = this->stateTimeStep()->getData();
74
75 auto aSDF = this->getBoundarys();
76
77 auto pSys = this->getParticleSystems();
78
79 DArray<Coord> pos_normal;
80 DArray<Real> pos_sdf;
81 for (int i = 0; i < pSys.size(); i++)
82 {
83 DArray<Coord>& position = pSys[i]->statePosition()->getData();
84 DArray<Coord>& velocity = pSys[i]->stateVelocity()->getData();
85 for (int j = 0; j < aSDF.size(); j++)
86 {
87 aSDF[j]->stateSDFTopology()->constDataPtr()->getSignDistance(position, pos_sdf, pos_normal);
88 cuExecute(position.size(),
89 K_ConstrainSDF,
90 position,
91 velocity,
92 pos_sdf,
93 pos_normal,
94 this->varNormalFriction()->getData(),
95 this->varTangentialFriction()->getData(),
96 dt);
97 }
98 }
99
100 auto triSys = this->getTriangularSystems();
101 for (int i = 0; i < triSys.size(); i++)
102 {
103 DArray<Coord>& position = triSys[i]->statePosition()->getData();
104 DArray<Coord>& velocity = triSys[i]->stateVelocity()->getData();
105
106 for (int j = 0; j < aSDF.size(); j++)
107 {
108 aSDF[j]->stateSDFTopology()->constDataPtr()->getSignDistance(position, pos_sdf, pos_normal);
109 cuExecute(position.size(),
110 K_ConstrainSDF,
111 position,
112 velocity,
113 pos_sdf,
114 pos_normal,
115 this->varNormalFriction()->getData(),
116 this->varTangentialFriction()->getData(),
117 dt);
118 }
119 }
120
121 pos_normal.clear();
122 pos_sdf.clear();
123 }
124
125 DEFINE_CLASS(AdaptiveBoundary);
126}