PeriDyno 1.0.0
Loading...
Searching...
No Matches
GhostFluid.cu
Go to the documentation of this file.
1#include "GhostFluid.h"
2
3#include "Module/ProjectionBasedFluidModel.h"
4
5namespace dyno
6{
7 IMPLEMENT_TCLASS(GhostFluid, TDataType)
8
9 template<typename TDataType>
10 GhostFluid<TDataType>::GhostFluid()
11 : ParticleFluid<TDataType>()
12 {
13 auto model = std::make_shared<ProjectionBasedFluidModel<DataType3f>>();
14 model->varSmoothingLength()->setValue(0.01);
15
16 this->stateTimeStep()->connect(model->inTimeStep());
17 this->statePositionMerged()->connect(model->inPosition());
18 this->stateVelocityMerged()->connect(model->inVelocity());
19 this->stateNormalMerged()->connect(model->inNormal());
20 this->stateAttributeMerged()->connect(model->inAttribute());
21 this->animationPipeline()->pushModule(model);
22
23 this->setDt(0.001f);
24 }
25
26 template<typename TDataType>
27 void GhostFluid<TDataType>::resetStates()
28 {
29 ParticleFluid<TDataType>::resetStates();
30
31 constructMergedArrays();
32 }
33
34 template<typename TDataType>
35 void GhostFluid<TDataType>::preUpdateStates()
36 {
37 ParticleFluid<TDataType>::preUpdateStates();
38
39 //To ensure updates on fluid particles by other nodes can be mapped onto the merged arrays
40 constructMergedArrays();
41 }
42
43 template<typename TDataType>
44 void GhostFluid<TDataType>::postUpdateStates()
45 {
46 auto& pos = this->statePosition()->getData();
47 auto& vel = this->stateVelocity()->getData();
48
49 auto& posMerged = this->statePositionMerged()->constData();
50 auto& velMerged = this->stateVelocityMerged()->constData();
51
52 pos.assign(posMerged, pos.size());
53 vel.assign(velMerged, vel.size());
54
55 ParticleFluid<TDataType>::postUpdateStates();
56 }
57
58 __global__ void SetupFluidAttributes(
59 DArray<Attribute> allAttributes,
60 int num)
61 {
62 int pId = threadIdx.x + (blockIdx.x * blockDim.x);
63 if (pId >= num) return;
64
65 allAttributes[pId].setDynamic();
66 allAttributes[pId].setFluid();
67 }
68
69 __global__ void SetupBoundaryAttributes(
70 DArray<Attribute> allAttributes,
71 DArray<Attribute> boundaryAttributes,
72 int offset)
73 {
74 int pId = threadIdx.x + (blockIdx.x * blockDim.x);
75 if (pId >= boundaryAttributes.size()) return;
76
77 allAttributes[offset + pId].setFixed();
78 allAttributes[offset + pId].setRigid();
79 }
80
81 template<typename TDataType>
82 void GhostFluid<TDataType>::constructMergedArrays()
83 {
84 auto& pos = this->statePosition()->constData();
85 auto& vel = this->stateVelocity()->constData();
86
87 auto boundaryParticles = this->getBoundaryParticles();
88
89 int totalNumber = 0;
90 uint numOfGhostParticles = boundaryParticles != nullptr ? boundaryParticles->statePosition()->size() : 0;
91 uint numOfFluidParticles = pos.size();
92
93 totalNumber += (numOfFluidParticles + numOfGhostParticles);
94
95 if (totalNumber <= 0)
96 return;
97
98 //Initialize state fields for merged data
99 if (totalNumber != this->statePositionMerged()->size()) {
100 this->statePositionMerged()->resize(totalNumber);
101 this->stateVelocityMerged()->resize(totalNumber);
102 this->stateAttributeMerged()->resize(totalNumber);
103 this->stateNormalMerged()->resize(totalNumber);
104 }
105
106 auto& posMerged = this->statePositionMerged()->getData();
107 auto& velMerged = this->stateVelocityMerged()->getData();
108
109 int offset = 0;
110 posMerged.assign(pos, pos.size(), 0, 0);
111 velMerged.assign(vel, vel.size(), 0, 0);
112
113 offset += pos.size();
114
115 auto& normMerged = this->stateNormalMerged()->getData();
116 normMerged.reset();
117
118 if (boundaryParticles != nullptr)
119 {
120 auto& bPos = boundaryParticles->statePosition()->constData();
121 auto& bVel = boundaryParticles->stateVelocity()->constData();
122 auto& bNor = boundaryParticles->stateNormal()->constData();
123 posMerged.assign(bPos, bPos.size(), offset, 0);
124 velMerged.assign(bVel, bVel.size(), offset, 0);
125 normMerged.assign(bNor, bNor.size(), offset, 0);
126 }
127
128 //Initialize the attribute field
129 auto& attMerged = this->stateAttributeMerged()->getData();
130 if (numOfFluidParticles != 0)
131 {
132 cuExecute(offset,
133 SetupFluidAttributes,
134 attMerged,
135 offset);
136 }
137
138 if (boundaryParticles != nullptr)
139 {
140 auto& bAtt = boundaryParticles->stateAttribute()->getData();
141 cuExecute(bAtt.size(),
142 SetupBoundaryAttributes,
143 attMerged,
144 bAtt,
145 offset);
146 }
147 }
148
149 DEFINE_CLASS(GhostFluid);
150}