77#include "Vector/vector_dist.hpp"
78#include "Plot/GoogleChart.hpp"
79#include "Plot/util.hpp"
88typedef float real_number;
92constexpr int velocity = 0;
93constexpr int force = 1;
94constexpr int energy = 2;
98template<
typename vector_dist_type,
typename NN_type>
99__global__
void calc_force_gpu(vector_dist_type vd, NN_type NN, real_number sigma12, real_number sigma6, real_number r_cut2)
101 auto p = GET_PARTICLE(vd);
107 vd.template getProp<force>(p)[0] = 0.0;
108 vd.template getProp<force>(p)[1] = 0.0;
109 vd.template getProp<force>(p)[2] = 0.0;
114 auto Np = NN.getNNIterator(NN.getCell(vd.getPos(p)));
123 if (q == p) {++Np;
continue;};
132 real_number rn = norm2(r);
138 Point<3,real_number> f = 24.0*(2.0 *sigma12 / (rn*rn*rn*rn*rn*rn*rn) - sigma6 / (rn*rn*rn*rn)) * r;
141 vd.template getProp<force>(p)[0] += f.get(0);
142 vd.template getProp<force>(p)[1] += f.get(1);
143 vd.template getProp<force>(p)[2] += f.get(2);
152template<
typename vector_dist_type>
153__global__
void update_velocity_position(vector_dist_type vd, real_number dt)
155 auto p = GET_PARTICLE(vd);
158 vd.template getProp<velocity>(p)[0] += 0.5*dt*vd.template getProp<force>(p)[0];
159 vd.template getProp<velocity>(p)[1] += 0.5*dt*vd.template getProp<force>(p)[1];
160 vd.template getProp<velocity>(p)[2] += 0.5*dt*vd.template getProp<force>(p)[2];
163 vd.getPos(p)[0] += vd.template getProp<velocity>(p)[0]*dt;
164 vd.getPos(p)[1] += vd.template getProp<velocity>(p)[1]*dt;
165 vd.getPos(p)[2] += vd.template getProp<velocity>(p)[2]*dt;
168template<
typename vector_dist_type>
169__global__
void update_velocity(vector_dist_type vd, real_number dt)
171 auto p = GET_PARTICLE(vd);
174 vd.template getProp<velocity>(p)[0] += 0.5*dt*vd.template getProp<force>(p)[0];
175 vd.template getProp<velocity>(p)[1] += 0.5*dt*vd.template getProp<force>(p)[1];
176 vd.template getProp<velocity>(p)[2] += 0.5*dt*vd.template getProp<force>(p)[2];
183template<
typename vector_dist_type,
typename NN_type>
184__global__
void particle_energy(vector_dist_type vd, NN_type NN, real_number sigma12, real_number sigma6, real_number
shift, real_number r_cut2)
186 auto p = GET_PARTICLE(vd);
192 auto Np = NN.getNNIterator(NN.getCell(vd.getPos(p)));
203 if (q == p) {++Np;
continue;};
209 real_number rn = norm2(xp - xq);
215 E += 2.0 * ( sigma12 / (rn*rn*rn*rn*rn*rn) - sigma6 / ( rn*rn*rn) ) -
shift;
222 vd.template getProp<energy>(p) = E + (vd.template getProp<velocity>(p)[0]*vd.template getProp<velocity>(p)[0] +
223 vd.template getProp<velocity>(p)[1]*vd.template getProp<velocity>(p)[1] +
224 vd.template getProp<velocity>(p)[2]*vd.template getProp<velocity>(p)[2]) / 2;
231template<
typename CellList>
void calc_forces(
vector_dist_gpu<3,real_number,
aggregate<real_number[3],real_number[3],real_number> > & vd,
CellList & NN, real_number sigma12, real_number sigma6, real_number r_cut2)
233 vd.updateCellList(NN);
236 auto it2 = vd.getDomainIteratorGPU();
238 CUDA_LAUNCH(calc_force_gpu,it2,vd.toKernel(),NN.toKernel(),sigma12,sigma6,r_cut2);
243template<
typename CellList> real_number calc_energy(
vector_dist_gpu<3,real_number,
aggregate<real_number[3],real_number[3],real_number> > & vd,
CellList & NN, real_number sigma12, real_number sigma6, real_number r_cut2)
245 real_number rc = r_cut2;
246 real_number
shift = 2.0 * ( sigma12 / (rc*rc*rc*rc*rc*rc) - sigma6 / ( rc*rc*rc) );
248 vd.updateCellList(NN);
250 auto it2 = vd.getDomainIteratorGPU();
252 CUDA_LAUNCH(particle_energy,it2,vd.toKernel(),NN.toKernel(),sigma12,sigma6,
shift,r_cut2);
257 return reduce_local<energy,_add_>(vd);
262int main(
int argc,
char* argv[])
264 openfpm_init(&argc,&argv);
266 real_number sigma = 0.01;
267 real_number r_cut = 3.0*sigma;
270 size_t sz[3] = {100,100,100};
276 size_t bc[3]={PERIODIC,PERIODIC,PERIODIC};
281 real_number dt = 0.00005;
282 real_number sigma12 = pow(sigma,12);
283 real_number sigma6 = pow(sigma,6);
291 auto it = vd.getGridIterator(sz);
303 vd.getLastPos()[0] = key.get(0) * it.getSpacing(0);
304 vd.getLastPos()[1] = key.get(1) * it.getSpacing(1);
305 vd.getLastPos()[2] = key.get(2) * it.getSpacing(2);
308 vd.template getLastProp<velocity>()[0] = 0.0;
309 vd.template getLastProp<velocity>()[1] = 0.0;
310 vd.template getLastProp<velocity>()[2] = 0.0;
312 vd.template getLastProp<force>()[0] = 0.0;
313 vd.template getLastProp<force>()[1] = 0.0;
314 vd.template getLastProp<force>()[2] = 0.0;
319 vd.hostToDevicePos();
320 vd.hostToDeviceProp<velocity,force>();
322 vd.map(RUN_ON_DEVICE);
323 vd.ghost_get<>(RUN_ON_DEVICE);
331 auto NN = vd.getCellListGPU(r_cut);
337 calc_forces(vd,NN,sigma12,sigma6,r_cut*r_cut);
338 unsigned long int f = 0;
341 for (
size_t i = 0; i < nstep ; i++)
344 auto it3 = vd.getDomainIteratorGPU();
346 CUDA_LAUNCH(update_velocity_position,it3,vd.toKernel(),dt);
351 vd.map(RUN_ON_DEVICE);
352 vd.template ghost_get<>(RUN_ON_DEVICE);
357 calc_forces(vd,NN,sigma12,sigma6,r_cut*r_cut);
361 auto it4 = vd.getDomainIteratorGPU();
363 CUDA_LAUNCH(update_velocity,it4,vd.toKernel(),dt);
370 vd.deviceToHostPos();
371 vd.deviceToHostProp<0,1,2>();
375 vd.write_frame(
"particles_",f);
380 vd.ghost_get<>(RUN_ON_DEVICE);
383 real_number energy = calc_energy(vd,NN,sigma12,sigma6,r_cut*r_cut);
384 auto & vcl = create_vcluster();
394 if (vcl.getProcessUnitID() == 0)
395 std::cout <<
"Energy: " << energy << std::endl;
404 std::cout <<
"Time: " << tsim.
getwct() << std::endl;
410 options.
title = std::string(
"Energy with time");
413 options.
yAxis = std::string(
"Energy");
416 options.
xAxis = std::string(
"iteration");
422 options.
width = 1280;
428 options.
more = GC_ZOOM;
438 cg.
write(
"gc_plot2_out.html");
445int main(
int argc,
char* argv[])
This class represent an N-dimensional box.
Class for FAST cell list implementation.
Small class to produce graph with Google chart in HTML.
void write(std::string file)
It write the graphs on file in html format using Google charts.
void AddLinesGraph(openfpm::vector< X > &x, openfpm::vector< Y > &y, const GCoptions &opt)
Add a simple lines graph.
This class implement the point shape in an N-dimensional space.
__device__ __host__ const T & get(unsigned int i) const
Get coordinate.
Implementation of 1-D std::vector like structure.
Class for cpu time benchmarking.
void stop()
Stop the timer.
void start()
Start the timer.
double getwct()
Return the elapsed real time.
size_t width
width of the graph in pixels
size_t heigh
height of the graph in pixels
std::string xAxis
X axis name.
size_t lineWidth
Width of the line.
std::string title
Title of the chart.
std::string yAxis
Y axis name.
aggregate of properties, from a list of object if create a struct that follow the OPENFPM native stru...