-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSam_gran_compact_v1.8.cpp
1430 lines (1244 loc) · 56.1 KB
/
Sam_gran_compact_v1.8.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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/************ Wet Granular and Self-propelled particle simulator *****************/
/**********Developed at Dr. Mahesh V. Panchagnula and Dr. Srikanth Vedantam's group at IIT Madras**************/
/**********Fluid Mechanics, Department of Applied Mechanics, IIT Madras**************/
/**********Work started in C by Mr. Pavan Bonkinpillewar**************/
/* Pavan Bonkinpillewar, Mahesh V Panchagnula and Srikanth Vedantam
/* "Flow of Wet Granular Material in a lid Driven Cavity" */
/*Seventh M.I.T. Conference on Computational Fluid and Solid Mechanics, Massachusetts Institute of Technology, Cambridge, U.S.A*/
/**********Initial C++ framework by Bright Varghese**************/
/**********Final C++ code developed by Sam Mathew (sam<dot>cfd<dot>iitm<at>gmail<dot>com)**************/
/**********C++ learing from "C++ for C programmers" course offered by Prof. Ira Pohl on http://www.Coursera.org"**************/
/***********This code is covered under GNU Public licensing*************/
/*------------------------------------------------------------------------------
# License
#
# This is a free code: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This wet granular code is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
#
#------------------------------------------------------------------------------*/
/************************/
/* Combined code for
/* Belt driven cavity/enclosure problem (wet granular) {keywords: belt, }
/* and
/* Self-propelled particles {keywods: LVA, beta, ...}
/*********************************************************************
/**********************************************************************
/* Salient features of the code:
/* (a) There are two inline functions defined globally: (1) Random no. generator between 0 and 1 (white noise). (2) Gaussian random number generator with mean at 0 and 'sigma' as the standard deviation.
/* (b) There are three classes: (1) Vect2D, (2) Particle, (3) Simulate. Each of the classes have corresponding properties (dimensions, e.g., position, diameter, beta, etc.) and functions/methods.
/* (c) The main() function generates an object of the Simulate class. Bulk of the calculations then happen, when the function 'mainLoop' associated Simulate is called from main().
/* (d) A global vector of objects of the class Particle is generated after the Particle class definition. This vector of objects are then populated (generated), and manipulated within the different calls during the simulation.
/* (e) Numerical integration of the particles position and velocity is done using the Velocity-Verlet algorithm.
/* (f) Computational efficiency is achieved by neighbour list generation for each particle. The current formulation is a combination of cell-list and Verlet-list, which is updated only if the position of any of the particles moves more than the skin radius pre-defined in the first few lines before the fucntion definitions. In this way, there is no approximation as long as the cutoff radius is well defined.
---------------------
/* The different variables and constants defined at the beginning correspond to the numerics, material properties, radii, domain size, cut-off radius and skin-radius (for generating Verlet list), etc.
/************************/
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <fstream>
#include <numeric>
#include <algorithm>
#include <time.h>
using namespace std;
const float dt = 1e-4;
const int monitor = 500;
float t_final = 410.0;
float t_initial = 0.0;
float time_BELT_starts = 10.0;
// Update in case of self-propelled particles
float LVA = 0.0;
float time_SYS_starts = 0.0;
/**********************************************/
float Cv=1000;
const float SPRING=20000;
const float eta = 2;
const int n_ex = 800;
float exp_power;
float exf[n_ex];
const float PI=3.14159;
float gaussStdDevPercRAD = 0.05; // Uncomment this line to specify the standard deviation of the Gaussian w.r.t. mean radius (RAD)
const float SIGMA=213.34;
const float RHO = 100;
float GRAV=9.81;
float RAD = 0.0025;
float Secondary_RAD = 0.00125;
float SMALL_RAD = 0.001;
float BIG_RAD = 0.004;
const float Wall_RAD = 0.0025;
const float X0 = 0.0;
const float Y0 = 0.0;
const float WIDTH = 0.4;
const float HEIGHT = 0.4;
float r_cutoff = 5.0*(2.0*RAD);
float r_skin = 0.5*RAD;
//float V_surr = PI*pow(r_cutoff,2);
float Xmin = -6.0*Wall_RAD;
float Ymin = -6.0*Wall_RAD;
float Xmax = WIDTH + 6.0*Wall_RAD;
float Ymax = HEIGHT + 6.0*Wall_RAD;
float CONC = 0.00;
const int CORE_PARTICLES = 6084;
int bot_Core_small = 0;
int bot_Core_big = 3042;
int top_Core_small = 0;
int top_Core_big = 3042;
int m = ceil((Ymax - Ymin)/(r_cutoff+r_skin));
int n = ceil((Xmax - Xmin)/(r_cutoff+r_skin));
int ncells = m*n;
vector<vector<int> > nb_part(CORE_PARTICLES);
float BELT_ini_vel = 0.0;
float BELT_vel_x_bot = 0.5;
float BELT_vel_y_right = 0.0;
float BELT_vel_x_top = 0.0;
float BELT_vel_y_left = 0.0;
inline float rand01() {return static_cast<double> (rand())/RAND_MAX;}
inline float randGauss(float sigma)
{
float phi = rand01()*2*PI;
float Upsilon = rand01();
float Psi = -log(Upsilon);
float r = sigma*sqrt(2.0*Psi);
float x = r*cos(phi);
float y = r*sin(phi);
if (rand01() > 0.5)
return x;
else
return y;
}
/*// for a more accurate calculation of neighbouring particles' volume within the cutoff radius
float circle_int_area(float r1, float r2, float d)
{
float r = r2;
float R = r1;
if(R < r)
{
// swap
r = r1;
R = r2;
}
if (d < R - r)
return PI*r*r;
else
{
float a1 = r*r*acos((d*d + r*r - R*R)/(2*d*r));
float a2 = R*R*acos((d*d + R*R - r*r)/(2*d*R));
float a3 = 0.5*sqrt((-d+r+R)*(d+r-R)*(d-r+R)*(d+r+R));
return a1 + a2 - a3;
}
}*/
ofstream ofp_energy("energy.txt", ios::out);
ofstream ofp_monitor("monitor.txt", ios::out);
float wall_energy_input = 0.0;
float sp_pot_energy = 0.0;
class Vect2D
{
public:
float x, y;
Vect2D()
{
x = y = 0.0f;
}
Vect2D(float tx, float ty)
{
x = tx;
y = ty;
}
~Vect2D() {}
float distance_calc(Vect2D & d2)
{
Vect2D d;
d.x = d2.x - x;
d.y = d2.y - y;
return sqrt(d.x * d.x + d.y * d.y);
}
void add_vel(const Vect2D & vel_prev, const Vect2D & f, const Vect2D & f_prev, const float m,float dt = 0.0f)
{
if (dt == 0.0f)
{
x = vel_prev.getX() + f.getX();
y = vel_prev.getY() + f.getY();
}
else
{
x = vel_prev.getX() + ((f.getX()+f_prev.getX())/m)* dt*0.5;
y = vel_prev.getY() + ((f.getY()+f_prev.getY())/m)* dt*0.5;
}
}
void add_pos_vel(const Vect2D & v, const Vect2D & f, const float m, float dt = 0.0f)
{
if (dt == 0.0f)
{
x += v.getX();
y += v.getY();
}
else
{
x += v.getX() * dt + 0.5*(f.getX()/m)*dt*dt;
y += v.getY() * dt + 0.5*(f.getY()/m)*dt*dt;
}
}
void add_pos_vel(const Vect2D & v, const float m, float dt = 0.0f)
{
if (dt == 0.0f)
{
x += v.getX();
y += v.getY();
}
else
{
x += (v.getX()/m) * dt;
y += (v.getY()/m) * dt;
}
}
/* void addc(float f)
{
x += f;
y += f;
}
void subtract(const Vect2D & v)
{
x -= v.getX();
y -= v.getY();
}
void scale(float mul)
{
x *= mul;
y *= mul;
}
void normalize()
{
float ln = magnitude();
x /= ln;
y /= ln;
}*/
float magnitude() const {return sqrt( (x)*(x) + (y)*(y) );}
float getX() const {return x;}
float getY() const {return y;}
void setX(float v) {x = v;}
void setY(float v) {y = v;}
};
class Particle : public Vect2D
{
public:
Vect2D position, position_prev;
float displace;
Vect2D velocity, velocity_prev, vf;
Vect2D force, force_prev;
float radius,mass, beta;
int species;
unsigned short wall_tag;
unsigned short wall_layer;
float vf_0, v_surr_part, V_surr;
Vect2D local_order_param;
float mass_surr = 0, mass_bot = 0, mass_top = 0;
short unsigned num_surr = 0, num_bot = 0, num_top = 0;
Particle()
{
radius = RAD; //creates disks of radius=RAD.
position.x = rand01()*WIDTH;
position.y = rand01()*HEIGHT;
position_prev.x = 0.0;
position_prev.y = 0.0;
displace = 0.0;
mass = SIGMA*PI*radius*radius;
beta = 0.0;
velocity.x = 0.0; //initial velocity and force is zero.
velocity.y = 0.0;
velocity_prev.x = 0.0; //initial velocity and force is zero.
velocity_prev.y = 0.0;
force.x = 0.0;
force.y = 0.0;
force_prev.x = 0.0;
force_prev.y = 0.0;
v_surr_part = 0;
vf_0 = 0.0;
V_surr = PI*pow(5*(2*radius),2);
species = 0;
wall_tag = 0;
wall_layer = 0;
local_order_param.x = 0.0;
local_order_param.y = 0.0;
mass_surr = 0.0; num_surr = 0;
mass_bot = 0.0; num_bot = 0;
mass_top = 0.0; num_top = 0;
}
Particle(float u)
{
radius = RAD;
position.x = rand01()*WIDTH;
position.y = rand01()*u;
position_prev.x = 0.0;
position_prev.y = 0.0;
displace = 0.0;
mass = SIGMA*PI*radius*radius;
beta = 0.0;
velocity.x = 0.0;
velocity.y = 0.0;
velocity_prev.x = 0.0; //initial velocity and force is zero.
velocity_prev.y = 0.0;
force.x = 0.0;
force.y = 0.0;
force_prev.x = 0.0;
force_prev.y = 0.0;
v_surr_part = 0;
vf_0 = 0.0;
V_surr = PI*pow(5*(2*radius),2);
species = 0;
wall_tag = 0;
wall_layer = 0;
local_order_param.x = 0.0;
local_order_param.y = 0.0;
mass_surr = 0.0; num_surr = 0;
mass_bot = 0.0; num_bot = 0;
mass_top = 0.0; num_top = 0;
}
Particle(float u,float v)
{
radius = RAD;
position.x = u;
position.y = v;
position_prev.x = u;
position_prev.y = v;
displace = 0.0;
mass = SIGMA*PI*radius*radius;
beta = 0.0;
velocity.x = 0.0;
velocity.y = 0.0;
velocity_prev.x = 0.0; //initial velocity and force is zero.
velocity_prev.y = 0.0;
force.x = 0.0;
force.y = 0.0;
force_prev.x = 0.0;
force_prev.y = 0.0;
v_surr_part = 0;
vf_0 = 0.0;
V_surr = PI*pow(5*(2*radius),2);
species = 0;
wall_tag = 0;
wall_layer = 0;
local_order_param.x = 0.0;
local_order_param.y = 0.0;
mass_surr = 0.0; num_surr = 0;
mass_bot = 0.0; num_bot = 0;
mass_top = 0.0; num_top = 0;
}
Particle(float u,float v, float r)
{
radius = r;
position.x = u;
position.y = v;
position_prev.x = u;
position_prev.y = v;
displace = 0.0;
mass = SIGMA*PI*radius*radius;
beta = 0.0;
velocity.x = 0.0;
velocity.y = 0.0;
velocity_prev.x = 0.0; //initial velocity and force is zero.
velocity_prev.y = 0.0;
force.x = 0.0;
force.y = 0.0;
force_prev.x = 0.0;
force_prev.y = 0.0;
v_surr_part = 0;
vf_0 = 0.0;
V_surr = PI*pow(5*(2*radius),2);
species = 0;
wall_tag = 0;
wall_layer = 0;
local_order_param.x = 0.0;
local_order_param.y = 0.0;
mass_surr = 0.0; num_surr = 0;
mass_bot = 0.0; num_bot = 0;
mass_top = 0.0; num_top = 0;
}
Particle(float u,float v, float r, float vel_x, float vel_y)
{
radius = r;
position.x = u;
position.y = v;
position_prev.x = u;
position_prev.y = v;
displace = 0.0;
mass = SIGMA*PI*radius*radius;
beta = 0.0;
velocity.x = vel_x;
velocity.y = vel_y;
velocity_prev.x = 0.0; //initial velocity and force is zero.
velocity_prev.y = 0.0;
force.x = 0.0;
force.y = 0.0;
force_prev.x = 0.0;
force_prev.y = 0.0;
v_surr_part = 0;
vf_0 = 0.0;
V_surr = PI*pow(5*(2*radius),2);
species = 0;
wall_tag = 0;
wall_layer = 0;
local_order_param.x = 0.0;
local_order_param.y = 0.0;
mass_surr = 0.0; num_surr = 0;
mass_bot = 0.0; num_bot = 0;
mass_top = 0.0; num_top = 0;
}
~Particle() {}
void setPrevPosx(float x_prev) { position_prev.x = x_prev; }
void setPrevPosy(float y_prev) { position_prev.y = y_prev; }
void setDisplace(float d) { displace = d; }
void setVelx(float velx) { velocity.x = velx; }
void setVely(float vely) { velocity.y = vely; }
void addCx(float c) { position.x += c; }
void addCy(float c) { position.y += c; }
void setRadius(float r) { radius = r; }
void setMass(float m) { mass = m; }
void setBeta(float b) { beta = b; }
void setVsurr(float vsurr) { V_surr = vsurr; }
void setSpecies(int tag) { species = tag; }
void setWall(unsigned short tag, unsigned short layer)
{
wall_tag = tag;
wall_layer = layer;
}
void collideOther(Particle * other, bool VerletUpdate) //when the disk collides with other disks.
{
Vect2D f, dist;
dist.x = other->position.x - position.x;
dist.y = other->position.y - position.y;
float distance = position.distance_calc(other->position);
float minDist = (other->radius + radius);
int tmp_id;
float tmpexf, masstmp;
if(distance<0.2*minDist) distance=0.1*minDist;
// the present particle is in the sphere of influence of the 'other'
float h = 5*(2*radius);
if(distance < h)
{
tmp_id = 1+n_ex*(distance/h);
tmpexf = exf[tmp_id];
masstmp = other->mass*tmpexf;
v_surr_part += PI*pow(other->radius,2);
vf.x += masstmp * other->velocity.x;
vf.y += masstmp * other->velocity.y;
vf_0 += masstmp;
}
// the 'other' particle is in the sphere of influence of the present
h = 5*(2*other->radius);
if (distance < h)
{
tmp_id = 1+n_ex*(distance/h);
tmpexf = exf[tmp_id];
masstmp = mass*tmpexf;
other->v_surr_part += PI*pow(radius,2);
other->vf.x += masstmp * velocity.x;
other->vf.y += masstmp * velocity.y;
other->vf_0 += masstmp;
}
if (distance < minDist)
{
f.x = -(minDist/distance -1) * SPRING * dist.x;
f.y = -(minDist/distance -1) * SPRING * dist.y;
if (VerletUpdate)
sp_pot_energy += 0.5*SPRING*pow(minDist - distance,2);
}
force.x += f.x;
force.y += f.y;
other->force.x -= f.x;
other->force.y -= f.y;
}
void orderCalcOther(Particle * other)
{
Vect2D a, dist;
float angle;
dist.x = other->position.x - position.x;
dist.y = other->position.y - position.y;
float distance = position.distance_calc(other->position);
float minDist = (other->radius + radius);
if (distance < minDist*1.2)
{
angle = atan2(dist.y, dist.x);
local_order_param.x += cos(6*angle);
local_order_param.y += sin(6*angle);
angle = atan2(-1.0*dist.y, -1.0*dist.x);
other->local_order_param.x += cos(6*angle);
other->local_order_param.y += sin(6*angle);
num_surr +=1;
other->num_surr += 1;
mass_surr += other->mass;
other->mass_surr += mass;
if (other->species == 1)
{
num_bot += 1;
mass_bot += other->mass;
}
else
{
num_top += 1;
mass_top += other->mass;
}
if (species == 1)
{
other->num_bot += 1;
other->mass_bot += mass;
}
else
{
other->num_top += 1;
other->mass_top += mass;
}
}
}
void update_pos(const float dt)
{
position.add_pos_vel(velocity,force,mass,dt);
float part_x = position.getX();
float part_y = position.getY();
if ( part_x < X0) {position.setX(RAD*2.0);}
if ( part_x > X0+WIDTH) {position.setX(WIDTH - 2.0*RAD);}
if ( part_y < Y0) {position.setY(RAD*2.0);}
if ( part_y > Y0+HEIGHT) {position.setY(HEIGHT - 2.0*RAD);}
}
void update_wall_pos(const float dt)
{
position.add_pos_vel(velocity,1.0, dt);
float part_x = position.getX();
float part_y = position.getY();
if (wall_tag == 1 || wall_tag == 3) // Top or bottom wall
{
if (wall_layer == 1 || wall_layer == 3) // 1st or 3rd layer
{
if ( part_x > WIDTH + 2*Wall_RAD) {position.setX(part_x - (WIDTH + 4*Wall_RAD));}
if ( part_x < -2*Wall_RAD) {position.setX(part_x + (WIDTH + 4*Wall_RAD));}
}
else // 2nd layer
{
if ( part_x > WIDTH + Wall_RAD) {position.setX(part_x - (WIDTH + 2*Wall_RAD));}
if ( part_x < -Wall_RAD) {position.setX(part_x + (WIDTH + 2*Wall_RAD));}
}
}
if (wall_tag == 2 || wall_tag == 4) // Side walls (left or right)
{
if (wall_layer == 1 || wall_layer == 3) // 1st or 3rd layer
{
if ( part_y > HEIGHT + 2*Wall_RAD) {position.setY(part_y - (HEIGHT + 4*Wall_RAD));}
if ( part_y < -2*Wall_RAD) {position.setY(part_y + (HEIGHT + 2*Wall_RAD));}
}
else // 2nd layer
{
if ( part_y > HEIGHT + Wall_RAD) {position.setY(part_y - (HEIGHT + 2*Wall_RAD));}
if ( part_y < -Wall_RAD) {position.setY(part_y + (HEIGHT + 2*Wall_RAD));}
}
}
}
void pseudo_update_vel(const float dt)
{
velocity_prev.x = velocity.x;
velocity_prev.y = velocity.y;
velocity.add_pos_vel(force, mass, dt);
}
void update_vel(const float dt)
{
velocity.add_vel(velocity_prev, force, force_prev, mass, dt);
}
};
vector<Particle> disks;
class Simulate : public Particle
{
public:
int stop = clock();
Vect2D gravity;
vector<int> wall_limits;
float time = t_initial;
Vect2D displace_max;
float power_sum = 0;
Simulate()
{
gravity.x = 0.0;
gravity.y = -GRAV;
}
~Simulate() {}
void boundaryParticlesGenerate()
{
ofstream ofp_wall("wall_number.txt", ios::out);
ofstream ofp_wall_part_stat_1("wall_data_stat_1.txt", ios::out);
ofstream ofp_wall_part_stat_2("wall_data_stat_2.txt", ios::out);
ofstream ofp_wall_part_stat_3("wall_data_stat_3.txt", ios::out);
ofstream ofp_wall_part_stat_4("wall_data_stat_4.txt", ios::out);
wall_limits.push_back(disks.size());
// /***************** Bottom wall (belt) ************************/
for( float i = -Wall_RAD; i <= WIDTH + Wall_RAD ; i+= RAD * 2 ) // Layer 1
{
Particle disk = Particle(i, -Wall_RAD, Wall_RAD, BELT_ini_vel, 0.0); // Moving wall
disk.setWall(1,1); // bottom wall tag = 1
disks.push_back(disk);
if (fabs(BELT_vel_x_bot) == 0.0)
ofp_wall_part_stat_1 << disk.position.x << "\t" << disk.position.y << "\t"<< disk.radius << "\t"<< disk.velocity.x << "\t" << disk.velocity.y << endl;
}
for( float i = 0; i <= WIDTH ; i+= RAD * 2 ) // Layer 2
{
Particle disk = Particle(i, -Wall_RAD*(1+2*sin(PI/3.0)), Wall_RAD, BELT_ini_vel, 0.0); // Moving wall
disk.setWall(1,2); // bottom wall tag = 1
disks.push_back(disk);
if (fabs(BELT_vel_x_bot) == 0.0)
ofp_wall_part_stat_1 << disk.position.x << "\t" << disk.position.y << "\t"<< disk.radius << "\t"<< disk.velocity.x << "\t" << disk.velocity.y << endl;
}
for( float i = -Wall_RAD; i <= WIDTH + Wall_RAD ; i+= RAD * 2 ) // Layer 3
{
Particle disk = Particle(i, -Wall_RAD*(1+4*sin(PI/3.0)), Wall_RAD, BELT_ini_vel, 0.0); // Moving wall
disk.setWall(1,3); // bottom wall tag = 1
disks.push_back(disk);
if (fabs(BELT_vel_x_bot) == 0.0)
ofp_wall_part_stat_1 << disk.position.x << "\t" << disk.position.y << "\t"<< disk.radius << "\t"<< disk.velocity.x << "\t" << disk.velocity.y << endl;
}
wall_limits.push_back(disks.size());
unsigned short bot_wall = wall_limits.back() - wall_limits.front();
ofp_wall << bot_wall << endl;
ofp_wall_part_stat_1.close();
// /***************** Right wall ************************/
for( float i = HEIGHT + Wall_RAD; i >= -Wall_RAD ; i-= Wall_RAD * 2) // Layer 1
{
Particle disk = Particle(WIDTH + Wall_RAD, i, Wall_RAD);
disk.setWall(2,1); // right wall tag = 2
disks.push_back(disk);
if (fabs(BELT_vel_y_right) == 0.0)
ofp_wall_part_stat_2 << disk.position.x << "\t" << disk.position.y << "\t"<< disk.radius << "\t"<< disk.velocity.x << "\t" << disk.velocity.y << endl;
}
for( float i = HEIGHT; i >= 0.0 ; i-= Wall_RAD * 2) // Layer 2
{
Particle disk = Particle(WIDTH + Wall_RAD*(1+2*sin(PI/3.0)), i, Wall_RAD);
disk.setWall(2,2); // right wall tag = 2
disks.push_back(disk);
if (fabs(BELT_vel_y_right) == 0.0)
ofp_wall_part_stat_2 << disk.position.x << "\t" << disk.position.y << "\t"<< disk.radius << "\t"<< disk.velocity.x << "\t" << disk.velocity.y << endl;
}
for( float i = HEIGHT + Wall_RAD; i >= -Wall_RAD ; i-= Wall_RAD * 2) // Layer 3
{
Particle disk = Particle(WIDTH + Wall_RAD*(1+4*sin(PI/3.0)), i, Wall_RAD);
disk.setWall(2,3); // right wall tag = 2
disks.push_back(disk);
if (fabs(BELT_vel_y_right) == 0.0)
ofp_wall_part_stat_2 << disk.position.x << "\t" << disk.position.y << "\t"<< disk.radius << "\t"<< disk.velocity.x << "\t" << disk.velocity.y << endl;
}
wall_limits.push_back(disks.size());
unsigned short right_wall = wall_limits.back() - bot_wall;
ofp_wall << right_wall << endl;
ofp_wall_part_stat_2.close();
// /***************** Top wall ************************/
for( float i = WIDTH + Wall_RAD; i >= -Wall_RAD ; i-= Wall_RAD * 2) // Layer 1
{
Particle disk = Particle(i, HEIGHT + Wall_RAD, Wall_RAD);
disk.setWall(3,1); // top wall tag = 3
disks.push_back(disk);
if (fabs(BELT_vel_x_top) == 0.0)
ofp_wall_part_stat_3 << disk.position.x << "\t" << disk.position.y << "\t"<< disk.radius << "\t"<< disk.velocity.x << "\t" << disk.velocity.y << endl;
}
for( float i = WIDTH; i >= 0.0 ; i-= Wall_RAD * 2) // Layer 2
{
Particle disk = Particle(i, HEIGHT + Wall_RAD*(1+2*sin(PI/3.0)), Wall_RAD);
disk.setWall(3,2); // top wall tag = 3
disks.push_back(disk);
if (fabs(BELT_vel_x_top) == 0.0)
ofp_wall_part_stat_3 << disk.position.x << "\t" << disk.position.y << "\t"<< disk.radius << "\t"<< disk.velocity.x << "\t" << disk.velocity.y << endl;
}
for( float i = WIDTH + Wall_RAD; i >= -Wall_RAD ; i-= Wall_RAD * 2) // Layer 3
{
Particle disk = Particle(i, HEIGHT + Wall_RAD*(1+4*sin(PI/3.0)), Wall_RAD);
disk.setWall(3,3); // top wall tag = 3
disks.push_back(disk);
if (fabs(BELT_vel_x_top) == 0.0)
ofp_wall_part_stat_3 << disk.position.x << "\t" << disk.position.y << "\t"<< disk.radius << "\t"<< disk.velocity.x << "\t" << disk.velocity.y << endl;
}
wall_limits.push_back(disks.size());
unsigned short top_wall = wall_limits.back() - bot_wall - right_wall;
ofp_wall << top_wall << endl;
ofp_wall_part_stat_3.close();
// /***************** Left wall ************************/
for( float i = -Wall_RAD; i <= HEIGHT + Wall_RAD ; i+= Wall_RAD*2 ) // Layer 1
{
Particle disk = Particle(-Wall_RAD, i, Wall_RAD);
disk.setWall(4,1); // left wall tag = 4
disks.push_back(disk);
if (fabs(BELT_vel_y_left) == 0.0)
ofp_wall_part_stat_4 << disk.position.x << "\t" << disk.position.y << "\t"<< disk.radius << "\t"<< disk.velocity.x << "\t" << disk.velocity.y << endl;
}
for( float i = 0.0; i <= HEIGHT ; i+= Wall_RAD*2 ) // Layer 2
{
Particle disk = Particle(-Wall_RAD*(1+2*sin(PI/3.0)), i, Wall_RAD);
disk.setWall(4,2); // left wall tag = 4
disks.push_back(disk);
if (fabs(BELT_vel_y_left) == 0.0)
ofp_wall_part_stat_4 << disk.position.x << "\t" << disk.position.y << "\t"<< disk.radius << "\t"<< disk.velocity.x << "\t" << disk.velocity.y << endl;
}
for( float i = -Wall_RAD; i <= HEIGHT + Wall_RAD ; i+= Wall_RAD*2 ) // Layer 3
{
Particle disk = Particle(-Wall_RAD*(1+4*sin(PI/3.0)), i, Wall_RAD);
disk.setWall(4,3); // left wall tag = 4
disks.push_back(disk);
if (fabs(BELT_vel_y_left) == 0.0)
ofp_wall_part_stat_4 << disk.position.x << "\t" << disk.position.y << "\t"<< disk.radius << "\t"<< disk.velocity.x << "\t" << disk.velocity.y << endl;
}
wall_limits.push_back(disks.size());
unsigned short left_wall = wall_limits.back() - bot_wall - right_wall - top_wall;
ofp_wall << left_wall << endl;
ofp_wall.close();
ofp_wall_part_stat_4.close();
}
void boundaryParticlesRead()
{
int i = 0, wall_tot;
float x,y,r,velx,vely;
wall_limits.push_back(disks.size());
ifstream ifp_wall("wall_ini.txt", ios::in);
while(!ifp_wall.eof())
{
ifp_wall>>x>>y>>r>>velx>>vely;
Particle disk = Particle(x,y,r,velx,vely);
disks.push_back(disk);
i++;
}
disks.pop_back();
wall_tot = disks.size();
// Assigning wall tags in a single statement
//for (int j=0; j <disks.size(); j++)
// disks[j].setWall((j+1)%(disks.size()/4),1); // The read particles are assigned tags 1, 2, 3, 4
// The read particles are assigned tags 1, 2, 3, 4
// Assigning wall tags in 4 statements
for (int j=0; j <disks.size()/4; j++)
disks[j].setWall(1,1);
for (int j=disks.size()/4; j <disks.size()/2; j++)
disks[j].setWall(2,1);
for (int j=disks.size()/2; j <disks.size()*3/4; j++)
disks[j].setWall(3,1);
for (int j=disks.size()*3/4; j <disks.size(); j++)
disks[j].setWall(4,1);
wall_limits.push_back(disks.size()/4);
wall_limits.push_back(disks.size()/2);
wall_limits.push_back(disks.size()*3/4);
wall_limits.push_back(disks.size());
}
void interiorParticlesGenerate(int i)
{
// N_s = N* (x / (1-x*(r/R)^2))
int small_part = floor(CORE_PARTICLES*CONC/(1-CONC*(1-pow(Secondary_RAD/RAD,2))));
// N_b = N* ( (1-x) / ( 1 - x * (1 - (r/R)^2) ) )
int big_part = floor(CORE_PARTICLES*(1 - CONC)/(1-CONC*(1-pow(Secondary_RAD/RAD,2))));
ofstream ofp_int("bot_top_small_big_number.txt", ios::out);
bot_Core_small = floor(small_part/2);
bot_Core_big = floor(big_part/2);
top_Core_small = small_part - bot_Core_small;
top_Core_big = big_part - bot_Core_big;
for (int i = 0; i < bot_Core_small; i++)
{
Particle disk = Particle(HEIGHT/2.0);
disk.setSpecies(1);
disk.setRadius(Secondary_RAD);
disk.setMass(SIGMA*PI*pow(Secondary_RAD,2));
disk.setVsurr(PI*pow(5*2*Secondary_RAD,2));
disks.push_back(disk);
}
ofp_int << bot_Core_small << endl;
for (int i = bot_Core_small; i < bot_Core_small+bot_Core_big; i++)
{
Particle disk = Particle(HEIGHT/2.0);
disk.setSpecies(1);
disks.push_back(disk);
}
ofp_int << bot_Core_big << endl;
for (int i = bot_Core_small+bot_Core_big; i < bot_Core_small+bot_Core_big + top_Core_small; i++)
{
Particle disk = Particle(HEIGHT/2.0);
disk.addCy(HEIGHT/2.0);
disk.setSpecies(2);
disk.setRadius(Secondary_RAD);
disk.setMass(SIGMA*PI*pow(Secondary_RAD,2));
disk.setVsurr(PI*pow(5*2*Secondary_RAD,2));
disks.push_back(disk);
}
ofp_int << top_Core_small << endl;
for (int i = bot_Core_small+bot_Core_big + top_Core_small; i < bot_Core_small+bot_Core_big + top_Core_small + top_Core_big; i++)
{
Particle disk = Particle(HEIGHT/2.0);
disk.addCy(HEIGHT/2.0);
disk.setSpecies(2);
disks.push_back(disk);
}
ofp_int << top_Core_big << endl;
ofp_int.close();
if (i>1)
{
float norm_area = CORE_PARTICLES * PI*RAD*RAD, tot_area;
vector<float> r;
if (i == 2) // Uniform random
{
for (int i = 0; i< CORE_PARTICLES; i++)
r.push_back(rand01()*(BIG_RAD - SMALL_RAD) + SMALL_RAD);
}
if (i == 3)// Gaussian random
{
float norm_area = CORE_PARTICLES * PI*RAD*RAD, tot_area;
// Gaussian random
float min = 0.0, max = 0.0;
for (int i = 0; i< CORE_PARTICLES; i++)
{
r.push_back(randGauss(1.0));
if (r.back() < min) min = r.back();
if (r.back() > max) max = r.back();
}
float mean = accumulate(r.begin(), r.end(), 0.0)/r.size();
float std_dev = 0.0;
for_each (r.begin(), r.end(), [&](const float rad) { std_dev += (rad - mean) * (rad - mean); });
std_dev = sqrt(std_dev/(r.size() - 1));
for (int i = 0; i< CORE_PARTICLES; i++)
r[i] = (r[i] - mean) / (max - min); // Mean normalization and scaling done
mean = accumulate(r.begin(), r.end(), 0.0)/r.size();
std_dev = 0.0;
for_each (r.begin(), r.end(), [&](const float rad) { std_dev += (rad - mean) * (rad - mean); });
std_dev = sqrt(std_dev/(r.size() - 1));
for (int i = 0; i< CORE_PARTICLES; i++)
r[i] = r[i]*gaussStdDevPercRAD * RAD/std_dev + RAD;
}
tot_area = accumulate(r.begin(), r.end(), 0.0, [](float total, float rad){return total + PI*rad*rad;});
float max_RAD = RAD;
for (int i = 0; i< CORE_PARTICLES; i++)
{
r[i] *= sqrt(norm_area/tot_area);
disks[i+wall_limits.back()].setRadius(r[i]);
disks[i+wall_limits.back()].setMass(SIGMA*PI*r[i]*r[i]);
disks[i+wall_limits.back()].setVsurr(PI*pow(5*2*r[i],2));
if (r[i] > max_RAD)
max_RAD = r[i];
}
r_cutoff = 5.0*(2.0*max_RAD);
r_skin = 0.5*RAD;
}
}
void interiorParticlesRead()
{
int i = 0;
float x,y,r,velx,vely, lva;
ifstream ifp_int("interior_ini.txt", ios::in);
while(!ifp_int.eof())
{
ifp_int>>x>>y>>r>>velx>>vely>>lva;
Particle disk = Particle(x,y,r,velx,vely);
if (i < bot_Core_small + bot_Core_big)
disk.setSpecies(1);
else
disk.setSpecies(2);
disks.push_back(disk);
i++;
}
disks.pop_back();
}
void neighbour_list()
{
nb_part.resize(disks.size());
vector<vector<int> > cell_part(ncells);
// Un-comment the below 4 lines in case the domain is expanding
n = ceil((Xmax - Xmin)/(r_cutoff+r_skin)); // n has been updated
m = ceil((Ymax - Ymin)/(r_cutoff+r_skin)); // m has been updated
ncells = n*m; // ncells has been updated
vector<int> head(ncells);
vector<int> tail(disks.size());
int cell_ix, cell_iy, cell_n;
int l;
for (unsigned short j=0; j < ncells; j++) head[j] = -1;
// Head-tail construction
for (int i = 0 ; i < disks.size(); i++)
{
nb_part[i].clear();
cell_ix = floor((disks[i].position.x - Xmin)/(r_cutoff+r_skin));
cell_iy = floor((disks[i].position.y - Ymin)/(r_cutoff+r_skin));
cell_n = cell_ix + cell_iy * n;
tail[i] = head[cell_n];
head[cell_n] = i;
disks[i].setPrevPosx(disks[i].position.x);
disks[i].setPrevPosy(disks[i].position.y);
}
// particles in cell construction
for (int i=0; i < ncells; i++)
{
if (head[i] > -1)
{
cell_part[i].clear(); //cell_part.shrink_to_fit();
l=0;
cell_part[i].push_back(head[i]);
while (tail[cell_part[i][l]] > -1)
{
cell_part[i].push_back(tail[cell_part[i][l]]);
l++;
}
/*
cell_part[i] contains the ids of particles in the i-th cell.
The interior particles have higher ids and are more probable to be heads in a given cell.
*/
}
}