-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAshwinBowlesSystem.cpp
More file actions
360 lines (320 loc) · 11.9 KB
/
Copy pathAshwinBowlesSystem.cpp
File metadata and controls
360 lines (320 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
* AshwinBowlesSystem.cpp
*
* Created on: Sep 15, 2015
* Author: becker
*/
#include "AshwinBowlesSystem.h"
#include <vector>
#include <iostream>
#include "Paramters.h"
#include <fstream>
#include <vector>
#include <cmath>
#include "vectormath.h"
//#define OPENCL_PROGRAM_NAME "asbkernels.cl"
AshwinBowlesSystem::AshwinBowlesSystem() {
asbTime=0;
particles=nullptr;
}
AshwinBowlesSystem::AshwinBowlesSystem(const Parameters ¶meters,
const HostParameters & hostparameters) {
setup(parameters, hostparameters);
asbTime=0;
}
AshwinBowlesSystem::~AshwinBowlesSystem() {
if(particles !=nullptr) delete particles;
//delete walls;
}
void AshwinBowlesSystem::setup(const Parameters& parameters,
const HostParameters & hostParameters) {
this->parameter = parameters;
//Create the openCl Stuff
initializeOpenCL(hostParameters);
//initialize particel System
particles = new ParticleSystem(context, queue, parameters,hostParameters);
//compile the opencl program
cl::Program program = loadCLSource(OPENCL_PROGRAM_NAME, context);
globalp = cl::NDRange(parameter.numberOfParticles*parameter.number_of_systems);
localp = cl::NullRange; //todo optmiale wgsize finden
//globalAcc=cl::NDRange((int) parameter.numberOfParticles / 2);
try {
program.build(devices);
} catch (cl::Error &error) {
std::cerr << error.what() << "(" << error.err() << ")" << std::endl;
std::cerr << "Build Log:\t "
<< program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(devices[0])
<< std::endl;
exit(-1);
}
std::cout << "Build Log:\t "
<< program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(devices[0])
<< std::endl;
/// initalize paramter Buffer
parameterBuffer = cl::Buffer(context,
CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, sizeof(parameter),
&this->parameter);
timeBuffer = cl::Buffer(context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,
sizeof(asbTime), &asbTime);
/// extract kernels;
try {
verletStep1Kernel = cl::Kernel(program, "verletStep1");
updateOffsetsKernel = cl::Kernel(program, "updateOffset");
calculateAccelarationKernel1 = cl::Kernel(program,
"calculateAccelarationOnestep");
} catch (cl::Error &error) {
std::cerr << error.what() << "(" << error.err() << ")" << std::endl;
exit(EXIT_FAILURE);
}
///set Kernel arguments
try {
verletStep1Kernel.setArg(0, particles->getPositionBuffer());
verletStep1Kernel.setArg(1, particles->getVelocityBuffer());
verletStep1Kernel.setArg(2, particles->getAccelerationBuffer());
// verletStep1Kernel.setArg(3,particles->getOldAccelerationBuffer());
verletStep1Kernel.setArg(3, parameterBuffer);
calculateAccelarationKernel1.setArg(0, particles->getOffsetBuffer());
calculateAccelarationKernel1.setArg(1, particles->getPositionBuffer());
calculateAccelarationKernel1.setArg(2, particles->getVelocityBuffer());
calculateAccelarationKernel1.setArg(3,
particles->getAccelerationBuffer());
calculateAccelarationKernel1.setArg(4, timeBuffer);
calculateAccelarationKernel1.setArg(5, parameterBuffer);
calculateAccelarationKernel1.setArg(6, (cl_int) 0);
updateOffsetsKernel.setArg(0, particles->getOffsetBuffer());
updateOffsetsKernel.setArg(1, particles->getPositionBuffer());
} catch (cl::Error &error) {
std::cerr << error.what() << "(" << error.err() << ")" << std::endl;
exit(EXIT_FAILURE);
}
}
void AshwinBowlesSystem::initializeOpenCL(const HostParameters &hostParameters) {
//Create the openCl Stuff
try {
//query plattforms
cl::Platform::get(&plattforms);
cout << "Available OpenCL Platforms: \n";
for (int i = 0; i < plattforms.size(); ++i) {
std::cout << i << ".)" << std::endl;
std::cout << "\t" << plattforms[i].getInfo<CL_PLATFORM_VENDOR>()
<< std::endl;
std::cout << "\t" << plattforms[i].getInfo<CL_PLATFORM_NAME>()
<< std::endl;
}
cout << "Platform " << hostParameters.targetPlatform <<" was chosen" << std::endl;
///todo platform selection via inputfile
//query gpu-devices
plattforms[hostParameters.targetPlatform].getDevices(CL_DEVICE_TYPE_ALL, &devices);
/// todo decive sxelcetion via inpuit file
std::cout << "Availible Devices:" << std::endl;
for(int i=0;i<devices.size();++i) {
std::cout << "device " << i << ": " << devices[i].getInfo<CL_DEVICE_NAME>() << std::endl;
}
cout << "\tDevice " << hostParameters.targetDevice << " was chosen." << endl;
// create a context
context = cl::Context(devices);
// create a command queue
queue = cl::CommandQueue(context, devices[hostParameters.targetDevice]);
} catch (cl::Error &error) {
std::cerr << error.what() << "(" << error.err() << ")" << std::endl;
exit(EXIT_FAILURE);
}
}
void AshwinBowlesSystem::enqueueTimeStep() {
try{
cl::NDRange globaldouble(particles->size()*2);
queue.enqueueNDRangeKernel(verletStep1Kernel,cl::NullRange,globalp,localp);
queue.enqueueNDRangeKernel(calculateAccelarationKernel1,cl::NullRange,globalp,localp);
// queue.enqueueNDRangeKernel(verletStep2Kernel,cl::NullRange,globalp,localp);
} catch (cl::Error &error) {
std::cerr << error.what() << "(" << error.err() << ")" << std::endl;
exit(EXIT_FAILURE);
}
}
bool AshwinBowlesSystem::isJammed(
int exitedBonds[], ///< \return number of exited bonds
std::vector<bool> &stateVector ///< \return the state vector
) {
stateVector.clear();
bool isJammed=true;
const vector<cl_float2> & pos = particles->getPositions();
stateVector.resize(pos.size());
for (int sysnum = 0; sysnum < particles->number_of_systems(); ++sysnum) {
exitedBonds[sysnum]=0;
for (int i = 1 + sysnum * particles->size();
i < (particles->size() + sysnum * particles->size()); ++i) {
const double &y1 = pos[i].s[1];
const double &y2 = pos[i - 1].s[1];
if (parameter.upperWall - y1 > parameter.radius * (1 + 0.01)
&& y1 - parameter.lowerWall
> parameter.radius * (1 + 0.01)) {
isJammed = false;
cout << "Particle " << i << " of system "
<< sysnum << " has no contact to the wall" << endl;
break;
}
if (fabs(y2 - y1) > 0.05 * parameter.radius) {
stateVector[i - 1] = false;
} else {
stateVector[i - 1] = true;
exitedBonds[sysnum]++;
if (i > 1) {
if (stateVector[i - 2]) {
isJammed = false;
cout << "Particle " << i << " of system "
<< sysnum << " do not fullfill the jamming condition." << endl;
break;
}
}
}
}
}
return isJammed;
}
void AshwinBowlesSystem::saveState(const string &filename,const int &timeStep,const int &number) {
ofstream saveFile(filename);
saveFile << timeStep << "\t" << number << endl;
saveFile.precision(20);
saveFile << *particles;
saveFile.close();
}
void AshwinBowlesSystem::getEnergy(double &Ekin, double &Epot) {
//calculate kinetic Energy
Ekin = 0;
Epot = 0;
for (int i = 0; i < particles->size()*particles->number_of_systems(); ++i) {
double velsq = (particles->getVelocities()[i].s[0])*(particles->getVelocities()[i].s[0])+
(particles->getVelocities()[i].s[1])*(particles->getVelocities()[i].s[1]);
Ekin += velsq * 0.5 * particles->mass();
double posx1 = particles->getPosition(i).s[0];
double posy1 = particles->getPosition(i).s[1];
if (i < particles->size() - 1) {
double posx2 = particles->getPosition(i + 1).s[0];
double posy2 = particles->getPosition(i + 1).s[1];
double overlapp = 2 * particles->radius()
- sqrt(
(posx1 - posx2) * (posx1 - posx2)
+ (posy1 - posy2) * (posy1 - posy2));
if (overlapp > 0) {
Epot += parameter.springConstant * overlapp * overlapp*0.5;
}
}
double wallOverlapp=particles->radius()-fabs(posy1-parameter.upperWall);
Epot+= wallOverlapp >0 ? wallOverlapp*wallOverlapp*parameter.springConstant*0.5 : 0;
wallOverlapp=particles->radius()-fabs(posy1-parameter.lowerWall);
Epot+= wallOverlapp >0 ? wallOverlapp*wallOverlapp*parameter.springConstant*0.5 : 0;
if(i==0) {
wallOverlapp=particles->radius()-fabs(posx1-parameter.leftWall-parameter.leftWallofset);
Epot+= wallOverlapp >0 ? wallOverlapp*wallOverlapp*parameter.springConstant*0.5 : 0;
}
if(i==particles->size()-1) {
wallOverlapp=particles->radius()-fabs(posx1-parameter.rightWall-parameter.rightWallOffset);
Epot+= wallOverlapp >0 ? wallOverlapp*wallOverlapp*parameter.springConstant*0.5 : 0;
}
}
}
cl::Program AshwinBowlesSystem::loadCLSource(const char *filename, const cl::Context &context) {
try {
std::ifstream sourceFile(filename); //open file
std::string sourceCode((std::istreambuf_iterator<char>(sourceFile)),
std::istreambuf_iterator<char>()); //copy file to c_string
cl::Program::Sources source(1,
std::make_pair(sourceCode.c_str(), sourceCode.length()));
cl::Program program = cl::Program(context, source);
return program;
} catch (std::ifstream::failure &e) {
std::cerr << "Exception opening/reading/closing file\n";
exit(EXIT_FAILURE);
}
}
void AshwinBowlesSystem::cpuTimestep() {
vector<cl_int> &posOffset = particles->getOffset();
vector<cl_float2> &position = particles->getPositions();
vector<cl_float2> &velocity = particles->getVelocities();
vector<cl_float2> &acceleration = particles->getAccelerations();
Parameters *paras = &(this->parameter);
for (int id = 0; id < position.size(); ++id) {
velocity[id] += 0.5f * acceleration[id] * paras->timestep;
position[id] += velocity[id] * paras->timestep;
velocity[id] += 0.5f * acceleration[id] * paras->timestep;
}
for (int id = 0; id < position.size(); ++id) {
int sysid = id % paras->numberOfParticles;
cl_float2 acc;
acc.s[0]=0;
acc.s[1]=0;
//caclulate accelaration due to the nearest right neighbor
//if ((id + 1) < paras->numberOfParticles) {
acc += ((sysid + 1) < paras->numberOfParticles) ?
calcAcceleration(&posOffset[id], &posOffset[id + 1],
&position[id], &position[id + 1], &velocity[id],
&velocity[id + 1], paras) : (cl_float2){0,0};
//}
//calculate acceleration due to the nearest left neighbor
//if ((id) > 0) {
acc += ((sysid) > 0) ?
calcAcceleration(&posOffset[id], &posOffset[id - 1],
&position[id], &position[id - 1], &velocity[id],
&velocity[id - 1], paras) : (cl_float2){0,0};
//}
//
//horizontalWalls
//oben
{
float overlapp = paras->radius - (paras->upperWall - position[id].s[1]);
float force = overlapp>0 ?
fmax( paras->springConstant * overlapp
+ paras->damping * velocity[id].s[1], 0) : 0;
acc.s[1] -= force * paras->inverseMass;
}
// //unten
{
float overlapp = paras->radius + (paras->lowerWall - position[id].s[1]);
float force = overlapp >0 ? fmax(
paras->springConstant * overlapp
- paras->damping * velocity[id].s[1], 0) : 0;
acc.s[1] += force * paras->inverseMass;
}
//calculate wall forces
if (sysid == 0 || sysid == (paras->numberOfParticles - 1)) {
int i = (sysid == 0 ? 0 : 1);
int wallOffset = (
sysid == 0 ? paras->leftWallofset : paras->rightWallOffset);
float verticalWall = (id == 0 ? paras->leftWall : paras->rightWall);
float overlapp = (float) (posOffset[id] - wallOffset)
+ (position[id].s[0] - verticalWall);
if (fabs(overlapp) < paras->radius) {
overlapp += (i == 0 ? -paras->radius : paras->radius);
overlapp *= -1;
//printf("%i %i %f \n",id,i,overlapp);
float force = (
i == 0 ?
fmax(
paras->springConstant * overlapp
- paras->damping * velocity[id].s[0],
-0) :
fmin(
paras->springConstant * overlapp
- paras->damping * velocity[id].s[0],
0));
acc.s[0] += force * paras->inverseMass;
}
}
if (sysid == 0) {
acc.s[0] += paras->stampAcceleration;
}
acc-=paras->viskosity*velocity[id]*paras->inverseMass;
velocity[id] += 0.5f * paras->timestep * (acc-acceleration[id]);
acceleration[id] = acc;
if(id==0) asbTime +=paras->timestep;
}
}
void AshwinBowlesSystem::updateOffsetCpu() {
vector<cl_int> &posOffsets = particles->getOffset();
vector<cl_float2> &positions = particles->getPositions();
for (int id = 0; id < posOffsets.size(); ++id) {
int truncPos = (int) positions[id].s[0];
posOffsets[id] += truncPos;
positions[id].s[0] -= truncPos;
}
}