-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulation.cpp
More file actions
873 lines (740 loc) · 42.9 KB
/
Copy pathSimulation.cpp
File metadata and controls
873 lines (740 loc) · 42.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
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
#include "Simulation.h"
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <iomanip>
#include <limits>
#include <random>
#include <fstream>
#include <vector>
#include <algorithm> // std::unique, std::sort
#include <sys/stat.h>
#include <string>
#include <stdlib.h>
#include "Array.h"
#include "Cell.h"
#include "Cells.h"
#include "Phages.h"
#include "Compute.h"
#include "InputOutput.h"
#include "Constants.h"
#include "tools.h"
#include "UniformGrid.h"
#include "Nutrients.h"
#include "Neighbours.h"
#include "Forces.h"
#include "ClockIt.h"
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif
using namespace std;
//void SimSingleThread(int N_cells, Cell* old_cells, Cell* new_cells, int** NeighbourList, \
// int maxNeighbours, UniformGrid& Grid, OutputFiles Files, bool append, \
// DoubleArray2D& Height, DoubleArray3D& Density, DoubleArray3D& Density1, \
// DoubleArray3D& Density2, DoubleArray2D& WallDensity, \
// DoubleArray2D& WallDensity1, DoubleArray2D& WallDensity2, \
// EnvArray3D& Environment, EnvArray3D& oldEnvironment, \
// AgaArray3D** FieldAgar, AgaArray3D** oldFieldAgar, AgaArray2D** FieldWall, \
// AgaArray2D** oldFieldWall, CoordArray2D& Normal,const std::string DirName, \
// const int caseTest);
int GetProcessorCount()
{
#ifdef _WIN32
SYSTEM_INFO info;
GetSystemInfo(&info);
return info.dwNumberOfProcessors;
#else
return sysconf(_SC_NPROCESSORS_ONLN);
#endif
}
void RunSimulation(int N_cells, Cell* old_cells, Cell* new_cells, int** NeighbourList, \
int maxNeighbours, UniformGrid& Grid, OutputFiles Files, bool append, \
DoubleArray2D& Height, DoubleArray3D& Density, DoubleArray3D& Density1, \
DoubleArray3D& Density2, DoubleArray2D& WallDensity, \
DoubleArray2D& WallDensity1, DoubleArray2D& WallDensity2, \
EnvArray3D& Environment, EnvArray3D& oldEnvironment, \
AgaArray3D** FieldAgar, AgaArray3D** oldFieldAgar, AgaArray2D** FieldWall, \
AgaArray2D** oldFieldWall, CoordArray2D& Normal, const std::string DirName, \
const int caseTest)
{
SimSingleThread(N_cells, old_cells, new_cells, NeighbourList, maxNeighbours, \
Grid, Files, append, Height, Density, Density1, Density2, \
WallDensity, WallDensity1, WallDensity2, Environment, oldEnvironment, \
FieldAgar, oldFieldAgar, FieldWall, oldFieldWall, Normal, DirName, \
caseTest);
}
////////////////////////////////////////////////////////////////////////////////
////////////////// Main function body //////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
void SimSingleThread(int N_cells, Cell* old_cells, Cell* new_cells, int** NeighbourList, \
int maxNeighbours, UniformGrid& Grid, OutputFiles Files, bool append, \
DoubleArray2D& Height, DoubleArray3D& Density, DoubleArray3D& Density1, \
DoubleArray3D& Density2, DoubleArray2D& WallDensity, \
DoubleArray2D& WallDensity1, DoubleArray2D& WallDensity2, \
EnvArray3D& Environment, EnvArray3D& oldEnvironment, \
AgaArray3D** FieldAgar, AgaArray3D** oldFieldAgar, AgaArray2D** FieldWall, \
AgaArray2D** oldFieldWall, CoordArray2D& Normal, const std::string DirName,
const int caseTest)
{
// initialize time (generations)
double dt = initial_dt; // time step
double t = t0; // current time
int minx, maxx, miny, maxy, maxz;
// File to write the number of cells and phage vs time
stringstream filename;
ofstream solFile;
solFile.setf(ios::scientific, ios::floatfield);
filename << DirName << "/Cell_time.dat";
solFile.open(filename.str(), ios::out);
// Files to store phage locations
stringstream filePhage;
ofstream outFile_Phage;
//stringstream PhageFolder= DirName << "/Phage";
//mkdir(PhageFolder.str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); // Done only once
int burst_count = 0;
int infected = 0;
int removed = 0;
int N_phages = 0;
double pressureCell = 0.0;
// create place to store the props for phages
std::vector<double> lx,ly,lz;
std::vector<bool> mask;
// **********************Initialize*******************************
// counter to determine when to output data
double NextOutTime = OutputTime;
double NextUpdateTime = UpdateTime;
double HeightDensityNextUpdateTime = UpdateTime;
double HeightDensityUpdateTime = UpdateTime;
bool OutFlag = true;
bool UpdateFlag = true;
bool HeightDensityUpdateFlag = true;
bool phage_case = false; // Only for phage infection
bool phage_source = true; // only to seed phages
bool debug = true;
bool ThreeDcase = false;
// filter for doing some smoothing and averaging of fields
DoubleArray2D Filter(FilterLen,FilterLen);
double value = 1.0/(double)(FilterLen*FilterLen);
for (int ii = 0; ii<FilterLen; ii++)
{
for (int jj = 0; jj<FilterLen; jj++)
{
Filter.Set(ii,jj,value);
}
}
int FilterDim = FilterLen/2;
DoubleArray3D Filter3D(FilterLen,FilterLen,FilterLen);
double norm = 0;
for (int ii=0; ii<FilterLen; ii++)
{
for (int jj=0; jj<FilterLen; jj++)
{
for (int kk=0; kk<FilterLen; kk++)
{
value = exp(-(ii-FilterLen/2)*(ii-FilterLen/2)-(jj-FilterLen/2)*(jj-FilterLen/2)-(kk-FilterLen/2)*(kk-FilterLen/2));
norm = norm + value;
}
}
}
for (int ii=0; ii<FilterLen; ii++)
{
for (int jj=0; jj<FilterLen; jj++)
{
for (int kk=0; kk<FilterLen; kk++)
{
value = exp(-(ii-FilterLen/2)*(ii-FilterLen/2)-(jj-FilterLen/2)*(jj-FilterLen/2)-(kk-FilterLen/2)*(kk-FilterLen/2));
Filter3D.Set(ii,jj,kk,value/norm);
}
}
}
// *******************Calculate important fields******************
DoubleArray3D RoughDensity(Density.Size().x, Density.Size().y, Density.Size().z);
DoubleArray3D RoughDensity1(Density1.Size().x, Density1.Size().y, Density1.Size().z);
DoubleArray3D RoughDensity2(Density2.Size().x, Density2.Size().y, Density2.Size().z);
DoubleArray3D insideColonyDen(Density.Size().x, Density.Size().y, Density.Size().z);
DoubleArray2D RoughWallDensity(WallDensity.Size().x, WallDensity.Size().y);
DoubleArray2D RoughWallDensity1(WallDensity1.Size().x, WallDensity1.Size().y);
DoubleArray2D RoughWallDensity2(WallDensity2.Size().x, WallDensity2.Size().y);
DoubleArray3D RoughDensityShiftP(Density.Size().x, Density.Size().y, Density.Size().z);
DoubleArray3D RoughDensity1ShiftP(Density1.Size().x, Density1.Size().y, Density1.Size().z);
DoubleArray3D RoughDensity2ShiftP(Density2.Size().x, Density2.Size().y, Density2.Size().z);
DoubleArray2D RoughWallDensityShiftP(WallDensity.Size().x, WallDensity.Size().y);
DoubleArray2D RoughWallDensity1ShiftP(WallDensity1.Size().x, WallDensity1.Size().y);
DoubleArray2D RoughWallDensity2ShiftP(WallDensity2.Size().x, WallDensity2.Size().y);
DoubleArray3D DensityShiftP(Density.Size().x, Density.Size().y, Density.Size().z);
DoubleArray3D Density1ShiftP(Density1.Size().x, Density1.Size().y, Density1.Size().z);
DoubleArray3D Density2ShiftP(Density2.Size().x, Density2.Size().y, Density2.Size().z);
DoubleArray2D WallDensityShiftP(WallDensity.Size().x, WallDensity.Size().y);
DoubleArray2D WallDensity1ShiftP(WallDensity1.Size().x, WallDensity1.Size().y);
DoubleArray2D WallDensity2ShiftP(WallDensity2.Size().x, WallDensity2.Size().y);
DoubleArray2D RoughHeight(Height.Size().x, Height.Size().y);
RoughHeight.Initialize(cellRadius);
RoughDensity.Initialize(0.0);
RoughDensity1.Initialize(0.0);
RoughDensity2.Initialize(0.0);
insideColonyDen.Initialize(0.0);
RoughWallDensity.Initialize(0.0);
RoughWallDensity1.Initialize(0.0);
RoughWallDensity2.Initialize(0.0);
RoughDensityShiftP.Initialize(0.0);
RoughDensity1ShiftP.Initialize(0.0);
RoughDensity2ShiftP.Initialize(0.0);
RoughWallDensityShiftP.Initialize(0.0);
RoughWallDensity1ShiftP.Initialize(0.0);
RoughWallDensity2ShiftP.Initialize(0.0);
DensityShiftP.Initialize(0.0);
Density1ShiftP.Initialize(0.0);
Density2ShiftP.Initialize(0.0);
WallDensityShiftP.Initialize(0.0);
WallDensity1ShiftP.Initialize(0.0);
WallDensity2ShiftP.Initialize(0.0);
// Make rough wall
DoubleArray2D Wall(BoxX,BoxY);
for (int ii = 0; ii < BoxX; ii++)
{
for (int jj = 0; jj < BoxY; jj++)
{
Wall.Set(ii,jj,drand48()*wall_rough);
}
}
std::cout << "Created wall" << std::endl;
int* IDlist = new int[maxNeighbours];
int IDlen = 0;
// cell stress tensor
Tensor stressTensor;
// net force
DoubleCoord Fnet;
// dividing cells
int* dividingCells = new int[maxCells]; // list of cells that need to divide at the end of a timestep
// calculate fields, output to file
CreateOutputFiles(0, Files, append);
GetDensity(RoughDensity, RoughDensity1, RoughDensity2, insideColonyDen, \
Height, Grid, old_cells, minx, maxx, miny, maxy, maxz);
ShiftDensity(RoughDensity, RoughDensity1, RoughDensity2, RoughWallDensity, \
RoughWallDensity1, RoughWallDensity2,RoughDensityShiftP, \
RoughDensity1ShiftP, RoughDensity2ShiftP,RoughWallDensityShiftP, \
RoughWallDensity1ShiftP, RoughWallDensity2ShiftP,BoxX, BoxY, BoxZ);
RoughDensityShiftP.Output(Files.roughDensity,3);
RoughDensity1ShiftP.Output(Files.roughDensity1,3);
RoughDensity2ShiftP.Output(Files.roughDensity2,3);
BoxAverage(RoughDensity, Density, RoughWallDensity, WallDensity, Filter3D, \
int((Filter3D.m_Size.x)/2), minx, maxx, miny, maxy, maxz,BoxX,BoxY,BoxZ);
BoxAverage(RoughDensity1, Density1,RoughWallDensity1, WallDensity1, Filter3D, \
int((Filter3D.m_Size.x)/2), minx, maxx, miny, maxy, maxz,BoxX,BoxY,BoxZ);
BoxAverage(RoughDensity2, Density2, RoughWallDensity2, WallDensity2,Filter3D, \
int((Filter3D.m_Size.x)/2), minx, maxx, miny, maxy, maxz,BoxX,BoxY,BoxZ);
BoxAverage(RoughDensityShiftP, DensityShiftP, RoughWallDensityShiftP, WallDensityShiftP, \
Filter3D, int((Filter3D.m_Size.x)/2), minx, maxx, miny, maxy, maxz,BoxX,BoxY,BoxZ);
BoxAverage(RoughDensity1ShiftP, Density1ShiftP,RoughWallDensity1ShiftP, WallDensity1ShiftP, \
Filter3D, int((Filter3D.m_Size.x)/2), minx, maxx, miny, maxy, maxz,BoxX,BoxY,BoxZ);
BoxAverage(RoughDensity2ShiftP, Density2ShiftP, RoughWallDensity2ShiftP, WallDensity2ShiftP, \
Filter3D, int((Filter3D.m_Size.x)/2), minx, maxx, miny, maxy, maxz,BoxX,BoxY,BoxZ);
DensityShiftP.Output(Files.density,3);
Density1ShiftP.Output(Files.density1,3);
Density2ShiftP.Output(Files.density2,3);
WallDensityShiftP.Output(Files.walldensity);
WallDensity1ShiftP.Output(Files.walldensity1);
WallDensity2ShiftP.Output(Files.walldensity2);
Height_Average(Height, RoughHeight, minx, maxx, miny, maxy);
RoughHeight.Output(Files.roughheight);
Smooth(RoughHeight, Height, Filter, FilterDim, minx, maxx, miny, maxy);
Height.Output(Files.height);
// find surface normal
GetSurfaceNormal(Normal, Height, minx, maxx, miny, maxy);
Normal.Output(Files.normal);
int Nconv = 0;
Nconv = UpdateEnvArray(&Environment, &oldEnvironment, FieldAgar, oldFieldAgar, \
FieldWall, oldFieldWall, DensityShiftP, Density1ShiftP, \
Density2ShiftP, WallDensityShiftP, WallDensity1ShiftP, \
WallDensity2ShiftP, minx, maxx, miny, maxy, maxz, Height, \
insideColonyDen);
//ShiftGrowthrate(&Environment, &FieldWall,BoxX, BoxY, BoxZ);
Environment.Output(Files.env,1);
for (int level=0;level<maxLevels;level++)
{
FieldAgar[level]->Append(Files.aga,1);
FieldWall[level]->Append(Files.wal);
}
// output cell data
if (OutFlag)
{
pressureCell = 0.0;
for (int cellID=0;cellID<N_cells;cellID++)
{
IntCoord XYAddress = Grid.GetXY( Grid.GetAddress(average(old_cells[cellID].Position)) );
DoubleCoord T(0,0,0);
F_surf_tension(old_cells[cellID], Grid, XYAddress, Height, Normal, Fnet, T);
Output(Files.cells, cellID, t, old_cells[cellID], Fnet);
// update the pressure for the colony
double cell_press = getPressure(Fnet);
pressureCell = pressureCell + cell_press;
}
}
fflush(Files.cells);
OutFlag = false;
CloseOutputFiles(Files);
// **************************** Loop through time and evolve colony ****************************
ClockIt ti0, ti1, ti2, ti3, ti4, ti00, tiph;
double SNutr, Smovgro, Swrite, Sdivide, Supdateout, st, Srho, sph;
SNutr=0; Smovgro=0; Swrite=0; Sdivide=0; Supdateout=0; st=0; Srho=0; sph=0;
int koutput=0;
rewind(Files.lineage);
int max_simulSteps = (t_max - t) / dt;
// Phage motion update coeffs
double Dloc = Dph * 3600 * 1.0e+12;
double Phageupdate = 2.0 * sqrt(Dloc * dt) / (3.0*L_divide*1.0e-04);
if(ThreeDcase)
{
Phageupdate = 2.0 * sqrt(Dloc * dt) / (1.0e+03*3.0*L_divide*1.0e-04);
}
//std::cout << "Diff " << Dph << " Cte: " << Phageupdate << std::endl;
// Phage adsorption coeffs
double Tdiv = 0.5 * pow(L_divide*1.0e-06,2.0) / (3600.0*Dph); //Time to move L_div in hours
double tolAds = 0.75 * L_divide + L_divide / (60*Tdiv*Adsorption_rate);
int jump = 50;//min(int(latent_period / dt) , 50);
//std::cout << "Tolerance ads.: " << tolAds << std::endl;
//exit(0);
//std::cout << t << " " << t0 << " " << initial_dt << " " << dt << std::endl;
//exit(0);
for(int iter = 0; iter < max_simulSteps; iter++)
{
// Compute the real time
t = t + dt;
if(N_cells > maxCells-1)
{
std::cout << "Simulation ended. Max. number of cells reached" << std::endl;
break;
}
// update fields only when UpdateFlag is true
ti0.start();
if (UpdateFlag and ThreeDcase)
{
UpdateFlag = false;
// find density
GetDensity(RoughDensity, RoughDensity1, RoughDensity2, insideColonyDen, \
Height, Grid, old_cells, minx, maxx, miny, maxy, maxz);
ShiftDensity(RoughDensity, RoughDensity1, RoughDensity2, RoughWallDensity, \
RoughWallDensity1, RoughWallDensity2,RoughDensityShiftP, \
RoughDensity1ShiftP, RoughDensity2ShiftP,RoughWallDensityShiftP, \
RoughWallDensity1ShiftP, RoughWallDensity2ShiftP,BoxX, BoxY, BoxZ);
BoxAverage(RoughDensity, Density, RoughWallDensity, WallDensity, Filter3D, \
int((Filter3D.m_Size.x)/2), minx, maxx, miny, maxy, maxz,BoxX,BoxY,BoxZ);
BoxAverage(RoughDensity1, Density1,RoughWallDensity1, WallDensity1, Filter3D, \
int((Filter3D.m_Size.x)/2), minx, maxx, miny, maxy, maxz,BoxX,BoxY,BoxZ);
BoxAverage(RoughDensity2, Density2, RoughWallDensity2, WallDensity2,Filter3D, \
int((Filter3D.m_Size.x)/2), minx, maxx, miny, maxy, maxz,BoxX,BoxY,BoxZ);
BoxAverage(RoughDensityShiftP, DensityShiftP, RoughWallDensityShiftP, \
WallDensityShiftP, Filter3D, int((Filter3D.m_Size.x)/2), \
minx, maxx, miny, maxy, maxz,BoxX,BoxY,BoxZ);
BoxAverage(RoughDensity1ShiftP, Density1ShiftP,RoughWallDensity1ShiftP, \
WallDensity1ShiftP, Filter3D, int((Filter3D.m_Size.x)/2), \
minx, maxx, miny, maxy, maxz,BoxX,BoxY,BoxZ);
BoxAverage(RoughDensity2ShiftP, Density2ShiftP, RoughWallDensity2ShiftP, \
WallDensity2ShiftP,Filter3D, int((Filter3D.m_Size.x)/2), \
minx, maxx, miny, maxy, maxz,BoxX,BoxY,BoxZ);
Height_Average(Height, RoughHeight, minx, maxx, miny, maxy);
// RoughHeight.Output(Files.height);
Smooth(RoughHeight, Height, Filter, FilterDim, minx, maxx, miny, maxy);
// Height.Output(Files.height);
// find surface normal
GetSurfaceNormal(Normal, Height, minx, maxx, miny, maxy);
// find nutrient concentrations
Nconv = UpdateEnvArray(&Environment, &oldEnvironment, FieldAgar, oldFieldAgar, \
FieldWall, oldFieldWall, DensityShiftP, Density1ShiftP, \
Density2ShiftP, WallDensityShiftP, WallDensity1ShiftP, \
WallDensity2ShiftP, minx, maxx, miny, maxy, maxz, Height, \
insideColonyDen);
// ShiftGrowthrate(&Environment, &FieldWall,BoxX, BoxY, BoxZ);
if (Nconv>=maxIter)
{
std::cout << "Environment has not converged" << std::endl;
}
}
SNutr+=ti0.stop()/1000.0;
ti00.start();
if (HeightDensityUpdateFlag and ThreeDcase)
{
HeightDensityUpdateFlag = false;
// find height
GetHeight(RoughDensity, Height, Grid, old_cells, minx, maxx, miny, maxy, maxz);
Height_Average(Height, RoughHeight, minx, maxx, miny, maxy);
Smooth(RoughHeight, Height, Filter, FilterDim, minx, maxx, miny, maxy);
// find surface normal
GetSurfaceNormal(Normal, Height, minx, maxx, miny, maxy);
}
Srho+=ti00.stop()/1000.0;
ti1.start();
if (OutFlag)
{
CreateOutputFiles(koutput++, Files, append);
pressureCell = 0.0;
for (int cellID=0;cellID<N_cells;cellID++)
{
// output cell data
mean_stress(old_cells[cellID], old_cells, NeighbourList[cellID], \
caseTest,Grid, Wall, Height, Normal, stressTensor, Fnet);
Output(Files.cells, cellID, t, old_cells[cellID], stressTensor);
// update the pressure for the colony
double cell_press = getPressure(Fnet);
pressureCell = pressureCell + cell_press;
}
}
// For debug
//std::cout << "Before move and grow" << std::endl << std::endl;
// Update neighbour lists
getNeighbours(old_cells, N_cells, Grid, NeighbourList, maxNeighbours);
// calculate forces and move cells
int numDivide = 0; // number of cells dividing
int cellID;
#pragma omp parallel for default(shared) private(cellID) schedule(static)
for(cellID=0;cellID<N_cells;cellID++)
{
GrowCell(old_cells[cellID], cellID, dt, dividingCells, numDivide, Environment, FieldWall, Grid, ThreeDcase);
//std::cout << "Growth done good" << std::endl;
MoveCell(cellID, Grid, old_cells, new_cells, NeighbourList[cellID], dt, Height, Normal, Wall, caseTest);
//std::cout << "Move done good" << std::endl;
}
// fflush(Files.cells);
// For debug
//std::cout << "After move and grow" << std::endl << std::endl;
Smovgro+=ti1.stop()/1000.0;
ti3.start();
// For debug
//std::cout << "Before divide" << std::endl << std::endl;
// Division (must be done after integration step because new neighbours are created)
for(int cellCount = 0; cellCount < numDivide; cellCount++)
{
// ID of cell that is undergoing division
const int cellID = dividingCells[cellCount];
DivideCell(cellID, N_cells, new_cells, Grid, NeighbourList[cellID], Wall, Height, Normal, t);
fprintf(Files.lineage, "%d %d\n", cellID, N_cells);
N_cells++;
// make list of all neighbours, new cell, and old cell
IDlen = NeighbourList[cellID][0];
memcpy(IDlist, NeighbourList[cellID], (IDlen+1)*sizeof(int)); // copy neighbours
IDlist[0] = cellID;
IDlist[IDlen+1] = N_cells-1;
IDlen+=2;
// update the neighbours of all of these cells
getNeighbours(new_cells, Grid, NeighbourList, maxNeighbours, IDlist, IDlen);
if(N_cells > maxCells-1)
{
std::cout << "Simulation ended. Max. number of cells reached" << std::endl;
break;
}
}
// For debug
//std::cout << "After divide" << std::endl << std::endl;
Sdivide+=ti3.stop()/1000.0;
////////////////////////////////////////////////////////////////////////////////
/////////////////////// Traditional tests //////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
if(caseTest == 1 and phage_source)
{
std::cout << "Make a phage-source-free case" << std::endl;
phage_source = false;
phage_case = false;
}
////////////////////////////////////////////////////////////////////////////////
//////////////// START Phage dynamics equations ////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Start the phage infection
tiph.start();
if(phage_source and t >= colony_time)
{
std::cout << std::endl << "Inserting the sources of phage" << std::endl;
phage_source = false;
phage_case = true;
int InfectSize = int(Infect_frac * colony_size / 100.0);
// Prepare and fill in the lists of cells to infect
int* toPhageList = new int[InfectSize];
if(caseTest == 2)
{
std::cout << "Make the PacMan Case" << std::endl;
InfectSize = getPacmanCell(N_cells, new_cells, InfectSize, toPhageList);
}
else if(caseTest == 3)
{
std::cout << "Make the Cells in border" << std::endl;
InfectSize = getBorderCell(N_cells, new_cells, InfectSize, toPhageList);
}
else if(caseTest == 11)
{
std::cout << "Make infection random on top Case" << std::endl;
for(int k=0; k < InfectSize; k++)
{
SourcePhage(N_cells-k-1, new_cells,dt);
}
InfectSize = 0;
}
else if(caseTest == 12)
{
std::cout << "Inserting the sources of phage by Uniform placement" << std::endl;
double* pos_x = new double[InfectSize];
double* pos_y = new double[InfectSize];
double* pos_z = new double[InfectSize];
double radii = getColonyRadii(N_cells, new_cells);
double height = getColonyHeight(N_cells, new_cells);
for(int k=0; k<InfectSize; k++)
{
double PI = 4.0*atan(1.0);
double r = drand48() * 0.01 * radii;
double h = (1.0 - 0.01*drand48()) * height;
double theta = (2.0*PI)*drand48();
double psi = (1.0*PI)*drand48();
double cx = r*sin(psi)*cos(theta);
double cy = r*sin(psi)*sin(theta);
double cz = h*cos(psi);
lx.push_back(cx);
ly.push_back(cy);
lz.push_back(cz);
mask.push_back(true);
}
N_phages = mask.size();
delete[] pos_x;
delete[] pos_y;
delete[] pos_z;
InfectSize = 0;
}
else if(caseTest == 13)
{
std::cout << "Make the Cells in Center" << std::endl;
InfectSize = getCenterCell(N_cells, new_cells, InfectSize, toPhageList);
}
else
{
std::cout << "Phage case not implemented choose [2-13]" << std::endl;
std::cout << "caseTest :: 2 - PacMan" << std::endl;
std::cout << "caseTest :: 3 - Border Cells" << std::endl;
std::cout << "caseTest :: 11 - Cells random top" << std::endl;
std::cout << "caseTest :: 12 - Phage random uniform" << std::endl;
std::cout << "caseTest :: 13 - Center Cells" << std::endl;
InfectSize = 0;
exit(0);
}
for(int k=0; k < InfectSize; k++)
{
int loc = toPhageList[k];
SourcePhage(loc, new_cells,dt);
}
delete[] toPhageList;
std::cout << "Done Inserting the sources of phage" << std::endl << std::endl;
}
// Exit the simulation if extinction
if(N_cells <= 2 and koutput > 10 and phage_source == false)
{
std::cout << "Simulation ended. Colony extincted" << std::endl;
std::cout << N_cells << std::endl;
break;
}
// For debug
//std::cout << "Before phage part" << std::endl << std::endl;
if(phage_source == false and phage_case and iter % jump == 0)
{
// Get the number of infected cells and update their loads
infected = UpdateInfection(N_cells, new_cells, dt * jump);
// Get the IDs of infected cells ready to burst
std::vector<int> deadList;
for(int k=0; k<N_cells; k++)
{
if(new_cells[k].PhageCell >= latent_period and new_cells[k].PhageCell < 10.0)
{
deadList.push_back(k);
}
}
//Sort cells in the deadList and remove duplicates
std::sort(deadList.begin(), deadList.end());
deadList.erase(std::unique(deadList.begin(), deadList.end()), deadList.end());
// Proceed to expand phage population and remove bursted cells
for(int infec_cellID=0; infec_cellID<deadList.size(); infec_cellID++)
{
// Get index on cell list
int loc_tgt = deadList[infec_cellID];
// Store the location of cell to burst
DoubleCoord ct = getCellcenter(loc_tgt, new_cells);
// Get Abortive status of cell
bool abortive = new_cells[loc_tgt].Abortive;
// Get Resistant status of cell
bool resistant = new_cells[loc_tgt].Resistant;
// Knowing the location of bursted cell release random phage there
if(resistant == false and abortive==false and new_cells[loc_tgt].PhageCell < 10.0)
{
for(int k=0; k<ProlePhage; k++)
{
// Place phages in new random places
double PI = 4.0*atan(1.0);
double r = drand48()*0.1;
double theta = (2.0*PI)*drand48();
double psi = (1.0*PI)*drand48();
lx.push_back(ct.x + r*sin(psi)*cos(theta));
ly.push_back(ct.y + r*sin(psi)*sin(theta));
lz.push_back(ct.z + 0.0*r*cos(psi));
mask.push_back(true);
}
N_phages = N_phages + ProlePhage;
}
if(resistant == false)
{
// Remove the loc_tgt cell from all the Neighbors
for(int k=1; k< NeighbourList[loc_tgt][0]; k++)
{
int pos = NeighbourList[loc_tgt][k];
removeKeyNeighbours(NeighbourList[pos], NeighbourList[pos][0], loc_tgt);
}
// Now we proceed to burst the cell
BurstCell(loc_tgt, N_cells-1, new_cells, Grid);
burst_count++;
N_cells--;
}
// For debug
//std::cout << "Killed stuff ID:: " << infec_cellID + 1 << std::endl;
}
deadList.clear();
// Update NeighbourList array
//getNeighbours(new_cells, N_cells, Grid, NeighbourList, maxNeighbours);
//Now we infect by proximity to phage location
int ph;
#pragma omp parallel for default(shared) private(ph) schedule(static)
for(ph=0; ph<mask.size(); ph++)
{
#pragma omp critical
{
if(mask[ph])
{
DoubleCoord pos = DoubleCoord(lx[ph],ly[ph],lz[ph]);
int count = GetBoxNumber(Grid, pos);
if(count > 1)
{
int* CellinBox = new int[count];
GetBoxCell(Grid, pos, CellinBox);
for(int k=0; k<count; k++)
{
int IDcell = CellinBox[k];
bool infect = infectCell(pos, IDcell, new_cells, tolAds);
bool resistant = new_cells[IDcell].Resistant;
if(infect and resistant==false)
{
new_cells[IDcell].PhageCell = dt;
new_cells[IDcell].Shrink = true;
mask[ph] = false;
removed++;
break;
}
else if(infect and resistant==true)
{
new_cells[IDcell].PhageCell = 0.0;
new_cells[IDcell].Shrink = true;
}
}
delete[] CellinBox;
}
// Now we move only the free phage
if(mask[ph])
{
double PI = 4.0*atan(1.0);
double r = drand48();
double theta = (2.0*PI)*drand48();
double psi = (1.0*PI)*drand48();
lx[ph] = lx[ph] + r*sin(psi)*cos(theta) * Phageupdate * jump;
ly[ph] = ly[ph] + r*sin(psi)*sin(theta) * Phageupdate * jump;
lz[ph] = lz[ph]; //+ r*cos(psi) * Phageupdate * (BoxZ+BoxZAgar) * jump / (BoxX);
}
}
}
}
}
// For debug
//std::cout << "After phage part" << std::endl << std::endl;
sph+=tiph.stop()/1000;
////////////////////////////////////////////////////////////////////////////////
//////////////// END Phage dynamics equations //////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
ti2.start();
// output fields to file
if (OutFlag)
{
OutFlag = false;
// save restart file with cell information
SaveCells(Files.restart, new_cells, N_cells, t, dt);
if(mask.size() > 0)
{
outFile_Phage.setf(ios::scientific, ios::floatfield);
filePhage.str("");
filePhage << DirName << "/Phage/phage_" << int(koutput) << ".dat";
outFile_Phage.open(filePhage.str(), ios::out);
for(int k=0; k<mask.size(); k++)
{
if(mask[k])
{
outFile_Phage << std::setprecision(4) << t << " " \
<< k << " " \
<< lx[k] << " " \
<< ly[k] << " " \
<< lz[k] << std::endl;
}
}
outFile_Phage.close();
}
if(debug)
{
std::cout << std::setprecision(2) << fixed << "t = " << 100.0 * t/t_max << " %;\t" << N_cells\
<< " living cells, " << infected << " infected cells, " \
<< burst_count << " killed cells, " << N_phages - removed << " phages. Sim. Time = " \
<< int(st/60.0/60.0) << ":" << int(st/60.0)%60 << ":" << int(st)%60 \
<< "; File written: " << koutput \
<< ". Time cost = " << SNutr << " - " << Srho << " - " << Smovgro \
<< " - " << Swrite << " - " << Sdivide << " - " << Supdateout << " - " << sph << std::endl;
}
else
{
std::cout << std::setprecision(2) << fixed << "t = " << 100.0 * t/t_max << " %;\t" << N_cells \
<< " living cells, " << infected << " infected cells, " \
<< burst_count << " killed cells, " << N_phages - removed << " phages. Sim. Time = " \
<< int(st/60.0/60.0) << ":" << int(st/60.0)%60 << ":" << int(st)%60 \
<< "; File written: " << koutput << std::endl;
}
// Update NeighbourList array
//getNeighbours(new_cells, N_cells, Grid, NeighbourList, maxNeighbours);
// Evaluate geometry features
double radii_val = getColonyRadii(N_cells, new_cells);
double height_val = getColonyHeight(N_cells, new_cells);
solFile << std::setprecision(4) << t << " " \
<< N_cells << " " << burst_count << " "\
<< infected << " " << pressureCell << " "\
<< N_phages - removed << " "\
<< radii_val << " " << height_val << std::endl;
st += SNutr+Smovgro+Swrite+Sdivide+Supdateout+Srho+sph;
SNutr=0; Smovgro=0; Swrite=0; Sdivide=0; Supdateout=0; Srho=0; sph=0;
fflush(stdout);
CloseOutputFiles(Files);
}
Swrite+=ti2.stop()/1000.0;
ti4.start();
// switch positions of old and new cells
{
Cell* temp = new_cells;
new_cells = old_cells;
old_cells = temp;
}
////////////////////////////////////////////////////////////////////////////////
////////////////// determine if we're writing output next time /////////////////
////////////////////////////////////////////////////////////////////////////////
OutFlag = (NextOutTime<=0);
NextOutTime = (OutFlag ? OutputTime : NextOutTime) - dt;
// determine if we're updating fields
UpdateFlag = (NextUpdateTime<=0);
NextUpdateTime = (UpdateFlag ? UpdateTime: NextUpdateTime) - dt;
HeightDensityUpdateFlag = (HeightDensityNextUpdateTime<=0);
HeightDensityNextUpdateTime = (HeightDensityUpdateFlag? HeightDensityUpdateTime: HeightDensityNextUpdateTime) - dt;
Supdateout+=ti4.stop()/1000.0;
}
fflush(Files.lineage);
solFile.close();
lx.clear();
ly.clear();
lz.clear();
mask.clear();
std::cout << "Done" << std::endl;
}