-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSPP_code.cpp
More file actions
2289 lines (2066 loc) · 86.6 KB
/
SPP_code.cpp
File metadata and controls
2289 lines (2066 loc) · 86.6 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
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
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <fstream>
#include <numeric>
#include <algorithm>
#include <ctime>
#include <random>
#include <unordered_map>
using namespace std;
const float dt = 1e-4;
const int monitor = 5000;
const int predation_monitor = 10000;
float t_final = 2000.0;
float t_initial = 0.0;
//float satisfy_timesteps = 0.0;
//float time_BELT_starts = 0.0;
// Update in case of self-propelled particles
float time_SYS_starts = 0.0;
float time_HUNT_starts = 2500.0;
int hunt_timelimit = 0; // In terms of iteration count
int satisfy_timelimit = 0; // In terms of iteration count
int refocus_timelimit = 0; // In terms of iteration count
/**********************************************/
float Cv = 0.0;
const float SPRING = 20000;
const float SPRING_WALL = 5000;
const float eta = 2;
const int n_ex = 800;
float exp_power;
float exf[n_ex];
//float gaussStdDevPercRAD = 0.05; // Uncomment this line to specify the standard deviation of the Gaussian w.r.t. mean radius (RAD)
const float SIGMA = 2.9256;
const float RHO = 1000; //1.22;
const float GRAV = 0; //9.81;
float RAD = 0.0136;
float Pred_RAD = RAD*4.0;
//float Secondary_RAD = 0.00125;
//float SMALL_RAD = 0.001;
//float BIG_RAD = 0.01;
//const float Wall_RAD = 0.0025;
const float X0 = 0.0;
const float Y0 = 0.0;
const float WIDTH = 2.5;
//const float HEIGHT = 0.4;
const float HEIGHT = WIDTH;
//const float xCentre = WIDTH/2.0;
//const float yCentre = HEIGHT/2.0;
// Zones for prey
float h_align = 5.0; // r_align = h_align*d
float h_separate = 0.5; // r_separate = h_separate*d
float h_attract = 20.0; // r_attract = h_attract*d
float h_escape = 20.0; // r_escape = h_escape*d
// Zones for predator
float h_detect = 5.0; // r_detect = h_detect*D
float r_kill = RAD + Pred_RAD;
float r_skin = 0.5*RAD;
float r_cutoff = h_attract*2.0*RAD;
float V_surr = M_PI*pow(r_cutoff,2);
float Xmin = X0; //-2.0*Wall_RAD;
float Ymin = Y0; //-2.0*Wall_RAD;
float Xmax = WIDTH; // + 2.0*Wall_RAD;
float Ymax = HEIGHT; // + 2.0*Wall_RAD;
//float CONC = 0.00;
const int CORE_PARTICLES = 508;
//int bot_Core_small = 0;
//int bot_Core_big = 0;
//int top_Core_small = 0;
//int top_Core_big = 0;
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.0;
//float BELT_vel_y_right = 0.0;
//float BELT_vel_x_top = 0.0;
//float BELT_vel_y_left = 0.0;
vector<vector<int>> hunt_list;
vector<vector<int>> predating_list;
unordered_map<int,char> escape_list; // unordered map conisting of prey IDs with escape activated
float beta_hunt = 0.08;
float beta_escape = 15.00;
float beta_repulse = 0.25;
float fric_factor = 0.1;
float noise = 0.0;
vector<float> noise_val(CORE_PARTICLES);
float blind_angle = 0.0;
float F_atr_max = 1e-6; // Maximum attraction force for danios = 1.2e-3
float wall_avoid_dist = 8*RAD;
float pred_avoid_dist = 0.05*(WIDTH+HEIGHT)/2;
float rot_angle_avoid = M_PI/4;
float vel_limit = 6.0*2.0*RAD;
float vmag_init = 6.0*2.0*RAD;
vector<float> vel_init(CORE_PARTICLES);
//int obs_p_id = 1 + rand() % CORE_PARTICLES;
//ofstream ofp_obs("obs_data.txt", ios::out);
//int satisfy_list[CORE_PARTICLES];
inline float rand01() {return static_cast<double> (rand())/RAND_MAX;}
inline double rand02(double range)
{
double c = 1 + (rand()/(RAND_MAX+1.0))*(1+range);
return c;
}
void gaussian_white(float mag) // returns Gaussian white random number between min and max
{
int seed = clock();
mt19937 gen(seed); //mersenne twister
for (unsigned short i = 0; i < noise_val.size(); i++)
{
normal_distribution <float> dist(0, sqrt(mag*mag/12.0));
noise_val[i] = dist(gen);
}
}
void uniform_random(float lower_lim, float upper_lim) // returns uniform random number between the limits
{
int seed = clock();
mt19937 gen(seed); //mersenne twister
for (unsigned short i = 0; i < vel_init.size(); i++)
{
uniform_real_distribution <float> dist(lower_lim, upper_lim);
vel_init[i] = dist(gen);
}
}
int signum(auto x)
{
return (x > 0) ? 1 : ((x < 0) ? -1 : 0);
}
/*inline float randGauss(float sigma)
{
float phi = rand01()*2*M_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 M_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 vect
{
public:
float x, y;
vect() //default constructor
{
x = y = 0.0f;
}
vect(float tx, float ty) //passing x and y position values at time t
{
x = tx;
y = ty;
}
~vect() {} //destructor
//Verlet-velocity algorithm start
vect distance_calc(vect & d_other) //address of d_other as argument
{
/* Uncomment below code for confined domain
* vect d;
* d.x = d_other.x - x;
* d.y = d_other.y - y;
* return sqrt(d.x * d.x + d.y * d.y);
*/
/**** Added distance calculation algorithm for periodic domain. (Updated on 08/08/20) ****/
vect d1;
float hx = WIDTH/2.0, hy = HEIGHT/2.0;
d1.x = d_other.x - x;
d1.y = d_other.y - y;
if (d1.x > hx) d1.x -= WIDTH;
else if (d1.x < -hx) d1.x += WIDTH;
if (d1.y > hy) d1.y -= HEIGHT;
else if (d1.y < -hy) d1.y += HEIGHT;
//if (*dz>hL) *dz-=L;
//else if (*dz<-hL) *dz+=L;
return d1;
// Returning the vector difference between the particle positions
}
float dotProduct(vect & other) // Returns the dot product between two vectors
{
return (x*other.x + y*other.y);
}
void add_pos_vel(const vect & v, const vect & 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 vect & f, const float m, float dt = 0.0f)
{
if (dt == 0.0f)
{
x += f.getX();
y += f.getY();
}
else
{
x += 0.5*(f.getX()/m) * dt;
y += 0.5*(f.getY()/m) * dt;
}
}
//Verlet-velocity algorithm end
/* void addc(float f)
{
x += f;
y += f;
}
void subtract(const vect & v)
{
x -= v.getX();
y -= v.getY();
}
void scale(float mul)
{
x *= mul;
y *= mul;
}
void normalize()
{
float ln = magnitude();
x /= ln;
y /= ln; // the present particle is in the sphere of influence of the 'other'
}*/
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;}
void rotate_vect(bool dirn) // true for CCW and false for CW
{
float x_tmp = x;
if (dirn)
{
x = x * cos(rot_angle_avoid) - y * sin(rot_angle_avoid);
y = x_tmp * sin(rot_angle_avoid) + y * cos(rot_angle_avoid);
}
else
{
x = x * cos(-rot_angle_avoid) - y * sin(-rot_angle_avoid);
y = x_tmp * sin(-rot_angle_avoid) + y * cos(-rot_angle_avoid);
}
}
};
class particle : public vect
{
public:
vect position, position_prev;
float displace;
vect velocity, vf;//, velocity_prev;
vect 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;
// vect local_order_param;
// float mass_surr = 0, mass_bot = 0, mass_top = 0;
// short unsigned num_surr = 0, num_bot = 0, num_top = 0;
int id, wall_tag; //, wall_layer, kind;
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*M_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 = M_PI*pow(h_attract*2.0*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;
kind = 0;
*/
id = 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*M_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 = M_PI*pow(h_attract*2.0*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;
kind = 0;
*/
id = 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*M_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 = M_PI*pow(h_attract*2.0*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;
kind = 0;
*/
id = 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*M_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 = M_PI*pow(h_attract*2.0*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;
kind = 0;
*/
id = 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*M_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 = M_PI*pow(h_attract*2.0*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;
kind = 0;
*/
id = 0;
}
particle(float u,float v, float r, float vel_x, float vel_y, int part_type)
{
radius = r;
position.x = u;
position.y = v;
position_prev.x = u;
position_prev.y = v;
displace = 0.0;
mass = SIGMA*M_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 = M_PI*pow(h_attract*2.0*radius,2);
// species = 0;
wall_tag = part_type;
/* 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;
kind = part_type;
*/
id = 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;
}
bool vision_check(particle * other)
{
// Calculating the angle between the velocity and the pos diff vectors using dot product
// acos returns in range of [0,pi]
vect pos_diff;
float vmag, dot_prod, angle_diff;
pos_diff = position.distance_calc(other->position); // \vec_{pos2} - \vec_{pos1}
dot_prod = pos_diff.dotProduct(velocity);
vmag = velocity.magnitude();
if (vmag == 0.0) vmag = 1.0;
angle_diff = acos(dot_prod/(pos_diff.magnitude() * vmag));
if (angle_diff < M_PI - blind_angle) return true;
else return false;
}
float angle_diff_calc(particle * other)
{
// Calculating the angle between the velocity and the pos diff vectors using dot product
// acos returns in range of [0,pi]
vect pos_diff;
float vmag, dot_prod, angle_diff;
pos_diff = position.distance_calc(other->position); // \vec_{pos2} - \vec_{pos1}
dot_prod = pos_diff.dotProduct(velocity);
vmag = velocity.magnitude();
if (vmag == 0.0) vmag = 1.0;
angle_diff = acos(dot_prod/(pos_diff.magnitude() * vmag));
return angle_diff;
}
void weight_other(particle * other) // alignment
{
float tmpexf, masstmp, r_align, r_sep;
int tmp_id;
float distance = position.distance_calc(other->position).magnitude();
// the 'other' particle is in the sphere of influence of the present
r_align = h_align*2.0*radius;
r_sep = h_separate*2.0*(radius + other->radius);
if (distance > r_sep && distance < r_align && this->vision_check(other))
{
tmp_id = 1+n_ex*(distance/r_align);
tmpexf = exf[tmp_id];
masstmp = other->mass*tmpexf;
v_surr_part += M_PI*pow(other->radius,2);
vf.x += masstmp * other->velocity.x; //total x-weighted velocity calculated
vf.y += masstmp * other->velocity.y; //total y-weighted velocity calculated
vf_0 += masstmp; //total weights calculated
}
// the present particle is in the sphere of influence of the 'other'
r_align = h_align*2.0*other->radius;
if (distance > r_sep && distance < r_align && other->vision_check(this))
{
tmp_id = 1+n_ex*(distance/r_align);
tmpexf = exf[tmp_id];
masstmp = mass*tmpexf;
other->v_surr_part += M_PI*pow(radius,2);
other->vf.x += masstmp * velocity.x;
other->vf.y += masstmp * velocity.y;
other->vf_0 += masstmp;
}
}
void separate_other(particle * other, bool VerletUpdate = false) // repulsion.
{
//other is a pointer to an object
vect f, dist; //dist and f both have x and y members
dist = position.distance_calc(other->position);
float distance = dist.magnitude(); //pythagoras distance
float r_sep = h_separate * 2.0*(other->radius + radius);
// Short range repulsion force function
if (distance < r_sep)
{
if ((wall_tag > 0 && other->wall_tag <= 0) || (wall_tag <= 0 && other->wall_tag > 0))
//if ((kind != -1 && other->kind == -1) || (kind == -1 && other->kind != -1))
{
f.x = -(r_sep/distance -1) * SPRING_WALL * dist.x; //Spring force in case either is a wall particle
f.y = -(r_sep/distance -1) * SPRING_WALL * dist.y;
// if (VerletUpdate)
// sp_pot_energy += 0.5*SPRING_WALL*pow(minDist - distance,2);
}
else if (wall_tag <= 0 && other->wall_tag <= 0)
{
f.x = -(r_sep/distance -1) * SPRING * dist.x;
f.y = -(r_sep/distance -1) * SPRING * dist.y;
// if (VerletUpdate)
// sp_pot_energy += 0.5*SPRING*pow(minDist - distance,2); //0.5kx^2
}
}
//Force transfer
force.x += f.x;
force.y += f.y;
other->force.x -= f.x; //Twice acting
other->force.y -= f.y;
}
/* void orderCalcOther(particle * other) //Calculating the Order parameter, not required
{
vect 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).magnitude();
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();
// Uncomment below lines for a confined domain
/* if ( part_x < X0) {position.setX(RAD*2.0);} //if particle moves to left of origin
if ( part_x > X0+WIDTH) {position.setX(WIDTH - 2.0*RAD);} //if particle moves to right of width
if ( part_y < Y0) {position.setY(RAD*2.0);}
if ( part_y > Y0+HEIGHT) {position.setY(HEIGHT - 2.0*RAD);}
*/
/**** Below code is for periodic domain. Added on 08/08/20. ****/
if ( part_x < X0) {position.setX(WIDTH + part_x);} //if particle moves to left of origin
if ( part_x > X0+WIDTH) {position.setX(part_x - WIDTH);} //if particle moves to right of width
if ( part_y < Y0) {position.setY(HEIGHT + part_y);}
if ( part_y > Y0+HEIGHT) {position.setY(part_y - HEIGHT);}
/**** Update for periodic domain ends here. ****/
// Uncomment below for circular domain
/* float theta = atan2(part_y - yCentre, part_x - xCentre);
float r = sqrt(pow((yCentre - part_y),2) + pow((xCentre - part_x),2));
if ( r > WIDTH/2.0) //if particle moves outside domain
{
position.setX((WIDTH/2 - 2.0*RAD)*cos(theta) + xCentre);
position.setY((WIDTH/2 - 2.0*RAD)*sin(theta) + yCentre);
}
*/
}
/* 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 update_vel(const float dt) //Updates velocity by 1/2 timestep each time it is called
{
velocity.add_pos_vel(force, mass, dt);
// Comment below if there is no limit on velocity
float theta = atan2(velocity.y, velocity.x);
if (velocity.magnitude() > vel_limit)
{
velocity.x = vel_limit * cos(theta);
velocity.y = vel_limit * sin(theta);
}
}
};
vector<particle> disks; //empty vector array of particle type
class simulate : public particle
{
public:
int stop = clock();
vect gravity;
vector<int> wall_limits;
float time = t_initial;
// vect 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(M_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(M_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(M_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(M_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(M_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(M_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(M_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(M_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;
// int type;
wall_limits.push_back(disks.size());
ifstream ifp_wall("wall_ini.txt", ios::in);
while(!ifp_wall.eof()) //until end-of-file, transfer the position and velocity data stored in the file to particle function
{
ifp_wall>>x>>y>>r>>velx>>vely; // >>type;
particle disk = particle(x,y,r,velx,vely); //,type);
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());
cout << "\n ***** Wall particles' initial data imported.\n" << endl;
}
/*
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++)
{