3#include "Module/ProjectionBasedFluidModel.h"
7 IMPLEMENT_TCLASS(GhostFluid, TDataType)
9 template<typename TDataType>
10 GhostFluid<TDataType>::GhostFluid()
11 : ParticleFluid<TDataType>()
13 auto model = std::make_shared<ProjectionBasedFluidModel<DataType3f>>();
14 model->varSmoothingLength()->setValue(0.01);
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);
26 template<typename TDataType>
27 void GhostFluid<TDataType>::resetStates()
29 ParticleFluid<TDataType>::resetStates();
31 constructMergedArrays();
34 template<typename TDataType>
35 void GhostFluid<TDataType>::preUpdateStates()
37 ParticleFluid<TDataType>::preUpdateStates();
39 //To ensure updates on fluid particles by other nodes can be mapped onto the merged arrays
40 constructMergedArrays();
43 template<typename TDataType>
44 void GhostFluid<TDataType>::postUpdateStates()
46 auto& pos = this->statePosition()->getData();
47 auto& vel = this->stateVelocity()->getData();
49 auto& posMerged = this->statePositionMerged()->constData();
50 auto& velMerged = this->stateVelocityMerged()->constData();
52 pos.assign(posMerged, pos.size());
53 vel.assign(velMerged, vel.size());
55 ParticleFluid<TDataType>::postUpdateStates();
58 __global__ void SetupFluidAttributes(
59 DArray<Attribute> allAttributes,
62 int pId = threadIdx.x + (blockIdx.x * blockDim.x);
63 if (pId >= num) return;
65 allAttributes[pId].setDynamic();
66 allAttributes[pId].setFluid();
69 __global__ void SetupBoundaryAttributes(
70 DArray<Attribute> allAttributes,
71 DArray<Attribute> boundaryAttributes,
74 int pId = threadIdx.x + (blockIdx.x * blockDim.x);
75 if (pId >= boundaryAttributes.size()) return;
77 allAttributes[offset + pId].setFixed();
78 allAttributes[offset + pId].setRigid();
81 template<typename TDataType>
82 void GhostFluid<TDataType>::constructMergedArrays()
84 auto& pos = this->statePosition()->constData();
85 auto& vel = this->stateVelocity()->constData();
87 auto boundaryParticles = this->getBoundaryParticles();
90 uint numOfGhostParticles = boundaryParticles != nullptr ? boundaryParticles->statePosition()->size() : 0;
91 uint numOfFluidParticles = pos.size();
93 totalNumber += (numOfFluidParticles + numOfGhostParticles);
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);
106 auto& posMerged = this->statePositionMerged()->getData();
107 auto& velMerged = this->stateVelocityMerged()->getData();
110 posMerged.assign(pos, pos.size(), 0, 0);
111 velMerged.assign(vel, vel.size(), 0, 0);
113 offset += pos.size();
115 auto& normMerged = this->stateNormalMerged()->getData();
118 if (boundaryParticles != nullptr)
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);
128 //Initialize the attribute field
129 auto& attMerged = this->stateAttributeMerged()->getData();
130 if (numOfFluidParticles != 0)
133 SetupFluidAttributes,
138 if (boundaryParticles != nullptr)
140 auto& bAtt = boundaryParticles->stateAttribute()->getData();
141 cuExecute(bAtt.size(),
142 SetupBoundaryAttributes,
149 DEFINE_CLASS(GhostFluid);