1#include "PaticleUniformAnalysis.h"
5#include "ParticleSystem/Module/Kernel.h"
10 template<typename TDataType>
11 PaticleUniformAnalysis<TDataType>::PaticleUniformAnalysis()
14 mSummation = std::make_shared<SummationDensity<TDataType>>();
15 this->inSmoothingLength()->connect(mSummation->inSmoothingLength());
16 this->inSamplingDistance()->connect(mSummation->inSamplingDistance());
17 this->inPosition()->connect(mSummation->inPosition());
18 this->inNeighborIds()->connect(mSummation->inNeighborIds());
22 template<typename TDataType>
23 PaticleUniformAnalysis<TDataType>::~PaticleUniformAnalysis() {
25 m_SurfaceEnergy.clear();
26 m_DensityEnergy.clear();
27 m_TotalEnergy.clear();
34 template <typename Real, typename Coord>
35 __global__ void K_SufaceEnergy(
39 DArrayList<int> neighbors,
42 int pId = threadIdx.x + (blockIdx.x * blockDim.x);
43 if (pId >= posArr.size()) return;
45 SmoothKernel<Real> kern;
46 Coord pos_i = posArr[pId];
48 List<int>& list_i = neighbors[pId];
49 int nbSize = list_i.size();
52 for (int ne = 0; ne < nbSize; ne++)
55 Real r = (pos_i - posArr[j]).norm();
59 g += kern.gradient(r, smoothingLength, 1.0) * (pos_i - posArr[j]) * (1.0f / r);
60 w += kern.Weight(r, smoothingLength);
63 if (w < EPSILON) w = EPSILON;
67 Energy[pId] = g.dot(g);
72 template <typename Real>
73 __global__ void K_DensityEnergy(
79 int pId = threadIdx.x + (blockIdx.x * blockDim.x);
80 if (pId >= Energy.size()) return;
81 Real e = density[pId] < rho_0 ? rho_0 - density[pId] : 0.0f;
82 Energy[pId] = e * e / (rho_0 * rho_0);
84 //printf("%f\r\n", Energy[pId]);
88 template <typename Real>
89 __global__ void K_TotalEnergy(
90 DArray<Real> TotalEnergy,
91 DArray<Real> DensityEnergy,
92 DArray<Real> SurfaceEnergy,
97 int pId = threadIdx.x + (blockIdx.x * blockDim.x);
98 if (pId >= TotalEnergy.size()) return;
100 TotalEnergy[pId] = A_d* DensityEnergy[pId] + A_s * SurfaceEnergy[pId];
101 //if(TotalEnergy[pId] > 1.0)
102 //printf("%f, %f + %f \r\n", TotalEnergy[pId], DensityEnergy[pId], SurfaceEnergy[pId]);
108 template<typename TDataType>
109 void PaticleUniformAnalysis<TDataType>::constrain() {
113 this->initializeImpl();
118 int num = this->inPosition()->getData().size();
120 if (m_TotalEnergy.size() != num)
122 m_DensityEnergy.resize(num);
123 m_SurfaceEnergy.resize(num);
124 m_TotalEnergy.resize(num);
126 m_reduce = Reduction<float>::Create(num);
127 m_arithmetic = Arithmetic<float>::Create(num);
128 m_reduce_real = Reduction<float>::Create(num);
129 m_Density.resize(num);
133 mSummation->varRestDensity()->setValue(1000.0f);
134 mSummation->varKernelType()->setCurrentKey(0);
135 mSummation->update();
138 std::cout << "m_init_density: " << m_init_density << std::endl;
140 cuExecute(num, K_DensityEnergy,
142 mSummation->outDensity()->getData(),
147 cuExecute(num, K_SufaceEnergy,
149 mSummation->outDensity()->getData(),
150 this->inPosition()->getData(),
151 this->inNeighborIds()->getData(),
154 cuExecute(num, K_TotalEnergy,
162 Real total_energy = m_arithmetic->Dot(m_TotalEnergy, m_TotalEnergy);
163 Real total_Surface_Energy = m_arithmetic->Dot(m_SurfaceEnergy, m_SurfaceEnergy);
165 //std::cout << "*** PaticleUniformAnalysis :" << sqrt(total_energy)/num << std::endl;
166 std::cout << "*** total_energy: " << total_energy/num <<", Surface_Energy: "<< total_Surface_Energy/num<< std::endl;
167 if (counter % 1 == 0)
169 *m_output << total_energy / num << std::endl;
180 template<typename TDataType>
181 bool PaticleUniformAnalysis<TDataType>::initializeImpl() {
182 std::cout << "PaticleUniformAnalysis initializeImpl " << std::endl;
184 std::string filename = mOutpuPath + mOutputPrefix + std::string(".txt");
186 m_output.reset(new std::fstream(filename.c_str(), std::ios::out));
188 int num = this->inPosition()->getData().size();
190 if (m_TotalEnergy.size() != num)
192 m_DensityEnergy.resize(num);
193 m_SurfaceEnergy.resize(num);
194 m_TotalEnergy.resize(num);
196 m_reduce = Reduction<float>::Create(num);
197 m_arithmetic = Arithmetic<float>::Create(num);
198 m_reduce_real = Reduction<float>::Create(num);
199 m_Density.resize(num);
204 mSummation->varRestDensity()->setValue(1000.0f);
205 mSummation->varKernelType()->setCurrentKey(0);
206 mSummation->update();
208 m_init_density = m_reduce_real->maximum(
209 mSummation->outDensity()->getData().begin(),
210 mSummation->outDensity()->getData().size());
217 template<typename TDataType>
218 void PaticleUniformAnalysis<TDataType>::setNamePrefix(std::string prefix)
220 mOutputPrefix = prefix;
223 template<typename TDataType>
224 void PaticleUniformAnalysis<TDataType>::setOutputPath(std::string path)
231 DEFINE_CLASS(PaticleUniformAnalysis);