-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparticle.cpp
524 lines (455 loc) · 11.1 KB
/
particle.cpp
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
/*
* particle.cpp
*
* Created on: Nov 18, 2015
* Author: reza
*/
#include "particle.h"
int particleSystem::particleSize(){
return _particle.size();
}
void particleSystem::particleResize(int n){
_particle.resize(n);
}
particle particleSystem::getParticle(int n){
return _particle[n];
}
void particleSystem::setParticle(int n, particle p){
_particle[n] = p;
}
Vec3d particleSystem::getPosInit(int n){
return _particle[n].r0;
}
void particleSystem::setPosInit(int n, Vec3d r0){
_particle[n].r0 = r0;
}
Vec3d particleSystem::getDisp(int n){
return _particle[n].u;
}
void particleSystem::setDisp(int n, Vec3d u){
_particle[n].u = u;
}
vector<double> particleSystem::getGradDisp(int n){
return _particle[n].gu;
}
void particleSystem::setGradDisp(int n, vector<double> gu){
_particle[n].gu = gu;
}
vector<double> particleSystem::getEpsilon(int n){
return _particle[n].epsilon;
}
void particleSystem::setEpsilon(int n, vector<double> epsilon){
_particle[n].epsilon = epsilon;
}
vector<double> particleSystem::getSigma(int n){
return _particle[n].sigma;
}
void particleSystem::setSigma(int n, vector<double> sigma){
_particle[n].sigma = sigma;
}
Vec3d particleSystem::getCurrPos(int n){
return (_particle[n].r0 + _particle[n].u);
}
Vec3d particleSystem::getVel(int n){
return _particle[n].v;
}
void particleSystem::setVel(int n, Vec3d v){
_particle[n].v = v;
}
Vec3d particleSystem::getVelLeap(int n){
return _particle[n].vleap;
}
void particleSystem::setVelLeap(int n, Vec3d vl){
_particle[n].vleap = vl;
}
Vec3d particleSystem::getAcc(int n){
return _particle[n].a;
}
void particleSystem::setAcc(int n, Vec3d a){
_particle[n].a = a;
}
double particleSystem::getMass(int n){
return _particle[n].m;
}
void particleSystem::setMass(int n, double m){
_particle[n].m = m;
}
double particleSystem::getDensity(int n){
return _particle[n].rho;
}
void particleSystem::setDensity(int n, double rho){
_particle[n].rho = rho;
}
void particleSystem::addDensity(int n, double rho){
_particle[n].rho += rho;
}
double particleSystem::getVolume(int n){
return _particle[n].V;
}
void particleSystem::setVolume(int n, double V){
_particle[n].V = V;
}
double particleSystem::getYoung(int n){
return _particle[n].E;
}
void particleSystem::setYoung(int n, double E){
_particle[n].E = E;
}
double particleSystem::getPoisson(int n){
return _particle[n].nu;
}
void particleSystem::setPoisson(int n, double nu){
_particle[n].nu = nu;
}
double particleSystem::getStrainEnergy(int n){
return _particle[n].U;
}
void particleSystem::setStrainEnergy(int n, double U){
_particle[n].U = U;
}
int particleSystem::getType(int n){
return _particle[n].type;
}
void particleSystem::setType(int n, int t){
_particle[n].type = t;
}
/*
//Initialization for roller
sph::sph(){
//Initializing parameters
double initRad = 0.05; //Particle "radius"
double r1 = 0.3; //Inner radius of roller
double r2 = 1.; //Outer radius of roller
g = -9.8;
t = 0.;
tMax = 1.;
dt = 0.005;
h = 4.*initRad;
hRcp = 1./h;
dumpSkip = 0.01/dt;
fileCounter = 0;
loopCounter = 0;
timeStart = clock();
//Initializing particles system
double initFlag = 1;
double radAnnulus = r1;
int annulusCounter = 0;
while(initFlag){
int nInit = _pSys.particleSize();
int n = (0.5*M_PI*radAnnulus/initRad);
n *= 2;
double dTeta = 2.*M_PI/n;
_pSys.particleResize(nInit+n);
for(int i=0; i<n; i++){
particle pTemp;
pTemp.r0 = Vec3d(radAnnulus*cos(i*dTeta), radAnnulus*sin(i*dTeta), 0.);
pTemp.u = Vec3d(0., 0., 0.);
pTemp.v = Vec3d(0., 0., 0.);
pTemp.vleap = Vec3d(0., 0., 0.);
pTemp.a = Vec3d(0., 0., 0.);
pTemp.m = 0.001;
pTemp.E = 100.;
pTemp.nu = 0.4;
if(annulusCounter == 0)
pTemp.type = 1;
else if(i==0)
pTemp.type = 2;
else
pTemp.type = 0;
_pSys.setParticle(nInit+i, pTemp);
}
radAnnulus += 2.*(3./M_PI)*initRad;
annulusCounter++;
if(radAnnulus>r2)
initFlag = 0;
}
int n = _pSys.particleSize();
FILE *fout;
fout=fopen("structure.dat", "w+");
for(int i=0; i<n; i++){
Vec3d posTemp = _pSys.getPosInit(i);
fprintf(fout, "%f\t%f\n", posTemp[0], posTemp[1]);
}
fclose(fout);
dumpData();
calcDensity();
}
*/
sph::sph(){
//Initializing parameters
double initRad = 0.05; //Particle "radius"
int nx = 20;
int ny = 5;
g = -9.8;
t = 0.;
tMax = 1.;
dt = 0.0001;
h = 4.*initRad;
hRcp = 1./h;
dumpSkip = 0.01/dt;
fileCounter = 0;
loopCounter = 0;
timeStart = clock();
//Initializing particles system
_pSys.particleResize(nx*ny);
for(int i=0; i<ny; i++){
for(int j=0; j<nx; j++){
particle pTemp;
pTemp.r0 = Vec3d((j*2.+1.)*initRad, (i*2.+1.)*initRad, 0.);
pTemp.u = Vec3d(0., 0., 0.);
pTemp.v = Vec3d(0., 0., 0.);
pTemp.vleap = Vec3d(0., 0., 0.);
pTemp.a = Vec3d(0., 0., 0.);
pTemp.m = 0.01;
pTemp.E = 100.;
pTemp.nu = 0.49;
if(i == 0)
pTemp.type = 1;
else
pTemp.type = 0;
_pSys.setParticle(i*nx+j, pTemp);
}
}
int n = _pSys.particleSize();
FILE *fout;
fout=fopen("structure.dat", "w+");
for(int i=0; i<n; i++){
Vec3d posTemp = _pSys.getPosInit(i);
fprintf(fout, "%f\t%f\n", posTemp[0], posTemp[1]);
}
fclose(fout);
dumpData();
calcDensity();
}
double sph::getT(){
return t;
}
double sph::getTMax(){
return tMax;
}
int sph::getDumpSkip(){
return dumpSkip;
}
int sph::getLoopCounter(){
return loopCounter;
}
void sph::calcDensity(){
int NNodes = _pSys.particleSize();
for(int i=0; i<NNodes; i++){
double rho0 = _pSys.getMass(i)*kernel(0., hRcp);
_pSys.setDensity(i, rho0);
}
for(int i=0; i<NNodes; i++){
Vec3d ri = _pSys.getPosInit(i);
double mi = _pSys.getMass(i);
double rhoi = _pSys.getDensity(i);
for(int j=i+1; j<NNodes; j++){
Vec3d rj = _pSys.getPosInit(j);
double mj = _pSys.getMass(j);
double dij = vector_length(ri-rj);
double Wij = kernel(dij, hRcp);
rhoi += mj*Wij;
_pSys.addDensity(j, mi*Wij);
}
_pSys.setDensity(i, rhoi);
_pSys.setVolume(i, mi/rhoi);
}
}
void sph::calcGradDisp(){
int NNodes = _pSys.particleSize();
for(int i=0; i<NNodes; i++){
vector<double> U(9, 0.);
_pSys.setGradDisp(i, U);
}
for(int i=0; i<NNodes; i++){
Vec3d ri = _pSys.getPosInit(i);
Vec3d ui = _pSys.getDisp(i);
vector<double> gui(_pSys.getGradDisp(i));
double voli = _pSys.getVolume(i);
for(int j=i+1; j<NNodes; j++){
Vec3d rj = _pSys.getPosInit(j);
Vec3d uj = _pSys.getDisp(j);
vector<double> guj(_pSys.getGradDisp(j));
double volj = _pSys.getVolume(j);
Vec3d Rij = ri-rj;
Vec3d uji = uj-ui;
Vec3d gWij = gradKernel(Rij, hRcp);
for(int k=0; k<3; k++){
for(int l=0; l<3; l++){
double gukl = uji[k]*gWij[l];
gui[3*k+l] += volj*gukl;
guj[3*k+l] += voli*gukl;
}
}
_pSys.setGradDisp(j, guj);
}
_pSys.setGradDisp(i, gui);
}
}
void sph::calcStrain(){
int NNodes = _pSys.particleSize();
for(int i=0; i<NNodes; i++){
vector<double> gui(_pSys.getGradDisp(i));
double Ei = _pSys.getYoung(i);
double nui = _pSys.getPoisson(i);
double voli = _pSys.getVolume(i);
//Make gradient of displacement into Jacobian
for(int j=0; j<3; j++){
gui[3*j+j] += 1.;
}
//Calculate strain
vector<double> epsilon(9,0.);
for(int j=0; j<3; j++){
for(int k=0; k<3; k++){
for(int l=0; l<3; l++){
epsilon[3*j+k] += gui[3*l+j]*gui[3*l+k];
}
if(j==k)
epsilon[3*j+k] -= 1.;
epsilon[3*j+k] *= 0.5;
}
}
_pSys.setEpsilon(i, epsilon);
//Calculate stress
double CConst = Ei/((1.+nui)*(1.-2.*nui));
vector<double> sigma(9);
sigma[0] = CConst*((1.-nui)*epsilon[0] + nui*(epsilon[4]+epsilon[8]));
sigma[4] = CConst*((1.-nui)*epsilon[4] + nui*(epsilon[0]+epsilon[8]));
sigma[8] = CConst*((1.-nui)*epsilon[8] + nui*(epsilon[0]+epsilon[4]));
for(int j=1; j<8; j++)
if(j!=4)
sigma[j] = CConst*(0.5-nui)*epsilon[j];
_pSys.setSigma(i, sigma);
//Calculate strain energy
double Ui = 0.;
for(int j=0; j<9; j++)
Ui += sigma[j]*epsilon[j];
Ui *= 0.5*voli;
_pSys.setStrainEnergy(i, Ui);
}
}
void sph::calcForce(){
int NNodes = _pSys.particleSize();
for(int i=0; i<NNodes; i++){
_pSys.setAcc(i, Vec3d(0.,g,0.));
}
for(int i=0; i<NNodes; i++){
Vec3d ri = _pSys.getPosInit(i);
Vec3d ai = _pSys.getAcc(i);
vector<double> gui(_pSys.getGradDisp(i));
vector<double> sigmai(_pSys.getSigma(i));
vector<double> epsiloni(_pSys.getEpsilon(i));
double mi = _pSys.getMass(i);
double mircp = 1./mi;
double voli = _pSys.getVolume(i);
for(int j=i+1; j<NNodes; j++){
Vec3d rj = _pSys.getPosInit(j);
Vec3d aj = _pSys.getAcc(j);
vector<double> guj(_pSys.getGradDisp(j));
vector<double> sigmaj(_pSys.getSigma(j));
vector<double> epsilonj(_pSys.getEpsilon(j));
double mj = _pSys.getMass(j);
double mjrcp = 1./mj;
double volj = _pSys.getVolume(j);
Vec3d Rij = ri-rj;
Vec3d dij = -voli*volj*gradKernel(Rij, hRcp);
//Gradient U add with I
vector<double> guit(gui), gujt(guj);
for(int k=0; k<3; k++){
guit[3*k+k] += 1.;
gujt[3*k+k] += 1.;
}
//Multiplication of (gradU+I)*sigma
vector<double> Mi(9,0.), Mj(9,0.);
for(int k=0; k<3; k++){
for(int l=0; l<3; l++){
for(int m=0; m<3; m++){
Mi[3*k+l] += guit[3*k+m]*sigmai[3*m+l];
Mj[3*k+l] += gujt[3*k+m]*sigmaj[3*m+l];
}
}
}
//Calculate acceleration
vector<double> aai(3,0.);
vector<double> aaj(3,0.);
for(int k=0; k<3; k++){
aai[k] = 0.;
aaj[k] = 0.;
for(int l=0; l<3; l++){
aai[k] -= Mj[3*k+l]*dij[l];
aaj[k] += Mi[3*k+l]*dij[l];
}
aai[k] *= mircp;
aaj[k] *= mjrcp;
}
ai += Vec3d(aai[0], aai[1], aai[2]);
aj += Vec3d(aaj[0], aaj[1], aaj[2]);
_pSys.setAcc(j, aj);
}
_pSys.setAcc(i, ai);
}
}
void sph::timeIntegration(){
int NNodes = _pSys.particleSize();
if(t == 0.){
// cout << "Initializing for leap velocity..." << endl;
for(int i=0; i<NNodes; i++){
_pSys.setVelLeap(i, _pSys.getVel(i)-0.5*_pSys.getVel(i)*dt);
}
}
else{
for(int i=0; i<NNodes; i++){
int ti = _pSys.getType(i);
if(ti != 1){
Vec3d ai = _pSys.getAcc(i);
Vec3d vli = _pSys.getVelLeap(i);
Vec3d ui = _pSys.getDisp(i);
vli += ai*dt;
ui += vli*dt;
_pSys.setVelLeap(i, vli);
_pSys.setDisp(i, ui);
_pSys.setVel(i, vli+0.5*ai*dt);
}
// //For rolling movement
// else{
// Vec3d ri = _pSys.getCurrPos(i);
// Vec3d ui = _pSys.getDisp(i);
// Vec3d wi(0., 0., 1.*sin(2.*M_PI*t));
//// Vec3d wi(0., 0., 2.*(1.-exp(-2.*M_PI*t)));
//
// Vec3d vlli = _pSys.getVelLeap(i);
// Vec3d vli = cross_product(wi, ri);
// Vec3d ai = (vli - vlli) / dt;
// ui += vli*dt;
//
// _pSys.setVelLeap(i, vli);
// _pSys.setVel(i, vli+0.5*ai*dt);
// _pSys.setDisp(i, ui);
// }
//For hung particle
else{
_pSys.setVelLeap(i, Vec3d(0.,0.,0.));
_pSys.setDisp(i, Vec3d(0.,0.,0.));
_pSys.setVel(i, Vec3d(0.,0.,0.));
}
}
}
t += dt;
loopCounter++;
}
void sph::dumpData(){
int NNodes = _pSys.particleSize();
FILE*fout;
char fileName[30];
sprintf(fileName, "dat/dat%05i.dat", fileCounter);
fout = fopen(fileName, "w+");
for(int i=0; i<NNodes; i++){
Vec3d ri = _pSys.getCurrPos(i);
fprintf(fout, "%f\t%f\n", ri[0], ri[1]);
}
fclose(fout);
fileCounter++;
loopCounter = 0;
cout << "TSimulation = " << t << "\tTRun = " <<
double(clock()-timeStart)/double(CLOCKS_PER_SEC) << " s." << endl;
}