-
Notifications
You must be signed in to change notification settings - Fork 0
/
base_code.nb
2664 lines (2640 loc) · 128 KB
/
base_code.nb
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
(* Content-type: application/vnd.wolfram.mathematica *)
(*** Wolfram Notebook File ***)
(* http://www.wolfram.com/nb *)
(* CreatedBy='Mathematica 10.1' *)
(*CacheID: 234*)
(* Internal cache information:
NotebookFileLineBreakTest
NotebookFileLineBreakTest
NotebookDataPosition[ 158, 7]
NotebookDataLength[ 131110, 2655]
NotebookOptionsPosition[ 130545, 2634]
NotebookOutlinePosition[ 130911, 2650]
CellTagsIndexPosition[ 130868, 2647]
WindowFrame->Normal*)
(* Beginning of Notebook Content *)
Notebook[{
Cell[BoxData[
RowBox[{"(*",
RowBox[{
"Definitions", " ", "of", " ", "main", " ", "simulation", " ",
"functions"}], "*)"}]], "Input",
CellChangeTimes->{{3.634308216732935*^9, 3.634308266315771*^9}, {
3.644243092315852*^9, 3.644243122262565*^9}, {3.6442494980672407`*^9,
3.6442495094388905`*^9}, {3.6582596556061783`*^9, 3.658259660968831*^9},
3.659357519606468*^9}],
Cell[BoxData[
RowBox[{
RowBox[{"(*",
RowBox[{
"Solve", " ", "coupled", " ", "ODEs", " ", "governing", " ", "fitness",
" ", "class", " ", "abundances"}], "*)"}], "\[IndentingNewLine]",
RowBox[{
RowBox[{"SolveDiffEqs", "[",
RowBox[{
"r0_", ",", "\[Mu]0_", ",", "s_", ",", "K_", ",", "U_", ",",
"genotypeabundances_", ",", "numgenotypes_", ",", "mostfitclass_", ",",
"timeuntilnextenvchange_"}], "]"}], ":=",
RowBox[{"Module", "[",
RowBox[{
RowBox[{"{",
RowBox[{"C", ",", "diffeqarray", ",", "X", ",", "system", ",", "sol"}],
"}"}], ",", "\[IndentingNewLine]",
RowBox[{
RowBox[{"If", "[",
RowBox[{
RowBox[{"numgenotypes", "\[Equal]", "1"}], ",", "\[IndentingNewLine]",
RowBox[{
RowBox[{"C", "=",
RowBox[{"genotypeabundances", "[",
RowBox[{"[", "1", "]"}], "]"}]}], ";", "\[IndentingNewLine]",
RowBox[{"system", "=",
RowBox[{"{",
RowBox[{
RowBox[{
RowBox[{
SuperscriptBox["n1", "\[Prime]",
MultilineFunction->None], "[", "t", "]"}], "\[Equal]",
RowBox[{
RowBox[{"n1", "[", "t", "]"}], " ",
RowBox[{"(",
RowBox[{"r0", "-",
RowBox[{"mostfitclass", " ", "s"}], "-", "\[Mu]0", "-",
FractionBox[
RowBox[{"r0", " ",
RowBox[{"n1", "[", "t", "]"}]}], "K"]}], ")"}]}]}], ",",
RowBox[{
RowBox[{"n1", "[", "0", "]"}], "\[Equal]", "C"}]}], "}"}]}], ";",
"\[IndentingNewLine]",
RowBox[{"sol", "=",
RowBox[{"{",
RowBox[{"{",
RowBox[{
RowBox[{"n1", "[", "t", "]"}], "\[Rule]",
FractionBox[
RowBox[{"C", " ",
SuperscriptBox["\[ExponentialE]",
RowBox[{"t", " ",
RowBox[{"(",
RowBox[{"r0", "-", " ",
RowBox[{"mostfitclass", " ", "s"}], "-", "\[Mu]0"}],
")"}]}]], " ", "K", " ",
RowBox[{"(",
RowBox[{"r0", "-",
RowBox[{"mostfitclass", " ", "s"}], "-", "\[Mu]0"}], ")"}]}],
RowBox[{
RowBox[{"C", " ",
RowBox[{"(",
RowBox[{
RowBox[{"-", "1"}], "+",
SuperscriptBox["\[ExponentialE]",
RowBox[{"t", " ",
RowBox[{"(",
RowBox[{"r0", "-",
RowBox[{"mostfitclass", " ", "s"}], "-", "\[Mu]0"}],
")"}]}]]}], ")"}], " ", "r0"}], "+",
RowBox[{"K", " ",
RowBox[{"(",
RowBox[{"r0", "-",
RowBox[{"mostfitclass", " ", "s"}], "-", "\[Mu]0"}],
")"}]}]}]]}], "}"}], "}"}]}], ";"}], "\[IndentingNewLine]",
",", "\[IndentingNewLine]",
RowBox[{
RowBox[{"diffeqarray", "=",
RowBox[{"Array", "[",
RowBox[{
RowBox[{"0", " ", "&"}], ",",
RowBox[{"{",
RowBox[{"numgenotypes", ",", "numgenotypes"}], "}"}]}], "]"}]}],
";",
RowBox[{"(*",
RowBox[{
RowBox[{"first", " ", "index", " ", "is", " ", "which", " ",
RowBox[{"genotype", "'"}], "s", " ", "diffeq", " ", "is", " ",
"solved"}], ",", " ",
RowBox[{
"second", " ", "is", " ", "what", " ", "influences", " ", "it"}]}],
"*)"}], "\[IndentingNewLine]",
RowBox[{"Do", "[", "\[IndentingNewLine]",
RowBox[{
RowBox[{
RowBox[{
RowBox[{
RowBox[{"diffeqarray", "[",
RowBox[{"[", "i", "]"}], "]"}], "[",
RowBox[{"[", "i", "]"}], "]"}], "=",
RowBox[{"r0", "-", "\[Mu]0", "-",
RowBox[{"s", " ",
RowBox[{"(",
RowBox[{"mostfitclass", "-", "1", "+", "i"}], ")"}]}]}]}],
";"}], "\[IndentingNewLine]", ",",
RowBox[{"{",
RowBox[{"i", ",", "numgenotypes"}], "}"}]}], "]"}], ";",
"\[IndentingNewLine]",
RowBox[{"Do", "[", "\[IndentingNewLine]",
RowBox[{
RowBox[{
RowBox[{
RowBox[{
RowBox[{"diffeqarray", "[",
RowBox[{"[", "i", "]"}], "]"}], "[",
RowBox[{"[", "j", "]"}], "]"}], "-=", " ",
RowBox[{
RowBox[{"ToExpression", "[",
RowBox[{"\"\<n\>\"", "<>",
RowBox[{"ToString", "[", "i", "]"}], "<>", "\"\<[t]\>\""}],
"]"}], " ",
RowBox[{"r0", "/", "K"}]}]}], ";"}], "\[IndentingNewLine]", ",",
RowBox[{"{",
RowBox[{"i", ",", "numgenotypes"}], "}"}], ",",
RowBox[{"{",
RowBox[{"j", ",", "numgenotypes"}], "}"}]}], "]"}], ";",
"\[IndentingNewLine]",
RowBox[{
RowBox[{"X", "[", "t_", "]"}], "=",
RowBox[{
RowBox[{
RowBox[{"ToExpression", "[",
RowBox[{"\"\<n\>\"", "<>",
RowBox[{"ToString", "[", "#", "]"}], "<>", "\"\<[t]\>\""}],
"]"}], "&"}], "/@",
RowBox[{"Range", "[", "numgenotypes", "]"}]}]}], ";",
"\[IndentingNewLine]",
RowBox[{"system", " ", "=", " ",
RowBox[{"Join", "[", "\[IndentingNewLine]",
RowBox[{
RowBox[{"MapThread", "[",
RowBox[{
RowBox[{
RowBox[{"#1", "\[Equal]", "#2"}], "&"}], ",",
RowBox[{"{",
RowBox[{
RowBox[{"D", "[",
RowBox[{
RowBox[{"X", "[", "t", "]"}], ",", "t"}], "]"}], ",",
RowBox[{"diffeqarray", ".",
RowBox[{"X", "[", "t", "]"}]}]}], "}"}]}], "]"}], ",",
RowBox[{"(*",
RowBox[{"see", " ",
RowBox[{"tutorial", "/", "DSolveSystemsOfLinearODEs"}]}],
"*)"}], "\[IndentingNewLine]",
RowBox[{"MapThread", "[",
RowBox[{
RowBox[{
RowBox[{"#1", "\[Equal]", "#2"}], "&"}], ",",
RowBox[{"{",
RowBox[{
RowBox[{
RowBox[{"X", "[", "t", "]"}], "/.",
RowBox[{"t", "\[Rule]", "0"}]}], ",", "genotypeabundances"}],
"}"}]}], "]"}]}], "\[IndentingNewLine]", "]"}]}], ";",
"\[IndentingNewLine]",
RowBox[{"sol", "=",
RowBox[{"NDSolve", "[",
RowBox[{"system", ",", " ",
RowBox[{
RowBox[{
RowBox[{"ToExpression", "[",
RowBox[{"\"\<n\>\"", "<>",
RowBox[{"ToString", "[", "#", "]"}], "<>", "\"\<[t]\>\""}],
"]"}], "&"}], "/@",
RowBox[{"Range", "[", "numgenotypes", "]"}]}], ",",
RowBox[{"{",
RowBox[{"t", ",", "0", ",", "timeuntilnextenvchange"}], "}"}]}],
"]"}]}], ";"}]}], "\[IndentingNewLine]", "]"}], ";",
"\[IndentingNewLine]",
RowBox[{"{",
RowBox[{"system", ",", "sol"}], "}"}]}]}], "\[IndentingNewLine]",
"]"}]}]}]], "Input",
CellChangeTimes->{{3.5246095996360774`*^9, 3.5246096152142024`*^9}, {
3.5246096742142024`*^9, 3.5246096817454524`*^9}, {3.5246097203079524`*^9,
3.5246098370892024`*^9}, {3.5246098739954524`*^9,
3.5246098857298274`*^9}, {3.5246694253125*^9, 3.5246695780625*^9}, {
3.532781382234375*^9, 3.5327813964375*^9}, {3.5327816995*^9,
3.532781703578125*^9}, {3.532781758859375*^9, 3.532781777734375*^9}, {
3.532781815984375*^9, 3.532781822234375*^9}, 3.532781852671875*^9, {
3.532782004328125*^9, 3.532782011609375*^9}, {3.532782077125*^9,
3.53278213703125*^9}, {3.53280043028125*^9, 3.53280043253125*^9}, {
3.54055894603125*^9, 3.540558952234375*^9}, 3.634308098931197*^9, {
3.6343081464889174`*^9, 3.6343081682421618`*^9}, 3.634308207076383*^9, {
3.6350721864634438`*^9, 3.6350721926787996`*^9}, {3.635072230243948*^9,
3.6350722319760475`*^9}, {3.6350723981945543`*^9,
3.6350724024547977`*^9}, {3.6350749450482264`*^9,
3.6350749494454775`*^9}, {3.635088057513216*^9, 3.6350880650366464`*^9}, {
3.63508810811111*^9, 3.635088108144112*^9}, {3.635088154473762*^9,
3.635088161327154*^9}, {3.6350882419897676`*^9, 3.6350882422657833`*^9}, {
3.6350883702851057`*^9, 3.6350883805666933`*^9}, {3.635088420375971*^9,
3.635088430236535*^9}, {3.635088619067335*^9, 3.6350886208484373`*^9}, {
3.6350887926442633`*^9, 3.635088829631379*^9}, {3.6350888666934986`*^9,
3.6350888745369473`*^9}, {3.63508897249255*^9, 3.635088978679904*^9}, {
3.635089100249857*^9, 3.635089107623279*^9}, {3.6350895727038803`*^9,
3.635089574601989*^9}, {3.6350896237037973`*^9, 3.6350896458150616`*^9}, {
3.6350897617756944`*^9, 3.6350897713252406`*^9}, {3.635089806946278*^9,
3.6350898072762966`*^9}, {3.6350912848708105`*^9, 3.635091302290807*^9}, {
3.635091462826989*^9, 3.6350914801289787`*^9}, {3.635091518212157*^9,
3.635091519947256*^9}, {3.635091555459287*^9, 3.6350915585804653`*^9}, {
3.635091603903058*^9, 3.6350916043950863`*^9}, {3.635091654172933*^9,
3.635091654497952*^9}, {3.635091726287058*^9, 3.6350917311793375`*^9}, {
3.635091770747601*^9, 3.6350917709276114`*^9}, {3.6350918019843874`*^9,
3.6350918177422886`*^9}, {3.635091866470076*^9, 3.6350918666270847`*^9}, {
3.6350919587593546`*^9, 3.635091959260383*^9}, {3.6350919946664085`*^9,
3.6350920319475408`*^9}, {3.6350921008854837`*^9,
3.6350921150652947`*^9}, {3.63509214976328*^9, 3.635092155912631*^9}, {
3.6350927502506256`*^9, 3.6350927586791077`*^9}, {3.6350960628247147`*^9,
3.6350960671569624`*^9}, {3.6350963036804905`*^9,
3.6350963064086466`*^9}, {3.635097599755622*^9, 3.6350976088691435`*^9},
3.635097730018072*^9, {3.635098053401569*^9, 3.6350980551456685`*^9}, {
3.6350987165454984`*^9, 3.6350987175955586`*^9}, {3.6350993340778193`*^9,
3.635099334414839*^9}, {3.6351068928331556`*^9, 3.6351069544826813`*^9}, {
3.6351070788217936`*^9, 3.6351071155858965`*^9}, {3.6351139051419787`*^9,
3.6351139132154408`*^9}, {3.63511415780143*^9, 3.6351141645028133`*^9}, {
3.6351780701998835`*^9, 3.6351780823025756`*^9}, {3.6351781598000083`*^9,
3.6351781671164265`*^9}, {3.6351782484770803`*^9,
3.6351782775937457`*^9}, {3.635178645961815*^9, 3.635178647421899*^9},
3.635178679810751*^9, {3.6351788698796225`*^9, 3.635178870217642*^9}, {
3.6351790230863857`*^9, 3.635179032044898*^9}, {3.6351796345453587`*^9,
3.635179647595105*^9}, {3.635179899381507*^9, 3.6351798995155144`*^9}, {
3.6351801784034657`*^9, 3.635180187081962*^9}, {3.6351805977224493`*^9,
3.63518059790546*^9}, {3.6351807152291703`*^9, 3.6351807197734303`*^9}, {
3.6351814153522153`*^9, 3.635181417868359*^9}, {3.6351814671371775`*^9,
3.635181467627205*^9}, {3.635182014836504*^9, 3.6351820179766836`*^9}, {
3.635182098155269*^9, 3.6351821122650766`*^9}, {3.635182257046357*^9,
3.6351822770064993`*^9}, {3.6351823118884945`*^9, 3.635182311957498*^9}, {
3.635182875431727*^9, 3.6351828820441055`*^9}, {3.635183023439193*^9,
3.635183024002225*^9}, {3.635183126045061*^9, 3.63518313318147*^9}, {
3.6351839130160737`*^9, 3.635183916712285*^9}, {3.6351840002730646`*^9,
3.63518400054708*^9}, {3.635184093174378*^9, 3.6351841009808245`*^9}, {
3.6351841885148315`*^9, 3.6351841987514167`*^9}, {3.6351843549543505`*^9,
3.6351843550853586`*^9}, {3.6351844335168447`*^9,
3.6351844408212624`*^9}, {3.635184722586378*^9, 3.635184722722386*^9}, {
3.6351852885447493`*^9, 3.635185288611753*^9}, {3.635185643210035*^9,
3.63518565221455*^9}, {3.6351857259157653`*^9, 3.635185730004999*^9}, {
3.6351858502088747`*^9, 3.6351858674808626`*^9}, {3.635185974587989*^9,
3.6351859762810855`*^9}, {3.635186410075897*^9, 3.6351864531063585`*^9}, {
3.6351864865712724`*^9, 3.6351864945237274`*^9}, {3.635186585380924*^9,
3.635186585612937*^9}, 3.6351869045331783`*^9, {3.6351869759272623`*^9,
3.6351869817155933`*^9}, {3.635187066708454*^9, 3.6351871194064684`*^9}, {
3.6351872042213197`*^9, 3.6351872075155077`*^9}, {3.6351873305065427`*^9,
3.635187387246788*^9}, {3.6351874474142294`*^9, 3.6351874493003373`*^9}, {
3.635187520466408*^9, 3.635187522147504*^9}, {3.63518759341658*^9,
3.6351875955157003`*^9}, {3.635187652371952*^9, 3.6351876697729473`*^9}, {
3.63518776229924*^9, 3.6351877661964626`*^9}, {3.635187825123833*^9,
3.635187829134063*^9}, {3.6351878713704786`*^9, 3.635187884932254*^9}, {
3.6355197858663244`*^9, 3.635519786159341*^9}, {3.6442494997403364`*^9,
3.6442495002353644`*^9}, {3.658259670995079*^9, 3.658259712382492*^9},
3.659357519606566*^9, 3.664586129316842*^9, {3.674225383942503*^9,
3.674225420330544*^9}, {3.674225845881777*^9, 3.674225846522703*^9}, {
3.6742273320250053`*^9, 3.6742273369872417`*^9}, {3.6742285261702223`*^9,
3.6742285282660723`*^9}, {3.674229311445553*^9, 3.674229314513915*^9}, {
3.6743014926296053`*^9, 3.6743014940427427`*^9}, {3.674301567525618*^9,
3.67430156985935*^9}, {3.678125066904793*^9, 3.6781251008978*^9}}],
Cell[BoxData[
RowBox[{
RowBox[{"(*",
RowBox[{
"Calculate", " ", "time", " ", "to", " ", "next", " ", "mutation"}],
"*)"}], "\[IndentingNewLine]",
RowBox[{
RowBox[{"NextMutation", "[",
RowBox[{
"U_", ",", "bestclassbirths_", ",", "starttime_", ",",
"timeuntilnextenvchange_"}], "]"}], ":=",
RowBox[{"Module", "[",
RowBox[{
RowBox[{"{",
RowBox[{
"numreplications", ",", "F", ",", "guessedtime1", ",", "guessedtime2",
",", "guessedtimetemp", ",", "F1"}], "}"}], ",", "\[IndentingNewLine]",
RowBox[{
RowBox[{"numreplications", "=",
RowBox[{"RandomReal", "[",
RowBox[{"ExponentialDistribution", "[", "1", "]"}], "]"}]}], ";",
"\[IndentingNewLine]",
RowBox[{"F", "=",
RowBox[{"NDSolveValue", "[",
RowBox[{
RowBox[{"{",
RowBox[{
RowBox[{
RowBox[{
RowBox[{"y", "'"}], "[", "t", "]"}], "\[Equal]",
RowBox[{"U", " ",
RowBox[{"bestclassbirths", "[", "t", "]"}]}]}], ",",
RowBox[{
RowBox[{"y", "[", "starttime", "]"}], "\[Equal]", "0"}]}], "}"}],
",", "y", ",",
RowBox[{"{",
RowBox[{"t", ",", "starttime", ",", "timeuntilnextenvchange"}],
"}"}]}], "]"}]}], ";", "\[IndentingNewLine]",
RowBox[{"(*",
RowBox[{
RowBox[{
"Sometimes", " ", "when", " ", "pop", " ", "is", " ", "declining"}],
",", " ",
RowBox[{
RowBox[{"no", " ",
RowBox[{"F", "[", "t", "]"}], " ", "will", " ", "ever", " ", "be",
" ", "high", " ", "enough"}], ";", " ",
RowBox[{"check", " ", "for", " ", "this"}]}]}], "*)"}],
"\[IndentingNewLine]",
RowBox[{"If", "[",
RowBox[{
RowBox[{
RowBox[{"F", "[", "timeuntilnextenvchange", "]"}], "<",
"numreplications"}], ",", "\[IndentingNewLine]",
RowBox[{
RowBox[{"If", "[",
RowBox[{
RowBox[{"(", "veryverbose", ")"}], ",",
RowBox[{
RowBox[{"Print", "[",
RowBox[{
"\"\<Next mutation not estimated before \>\"", ",",
"timeuntilnextenvchange", ",", "\"\<, starting at \>\"", ",",
"starttime"}], "]"}], ";"}]}], "]"}], ";",
"\[IndentingNewLine]",
RowBox[{"timeuntilnextenvchange", "+", "1"}]}],
"\[IndentingNewLine]", ",", "\[IndentingNewLine]",
RowBox[{"mutationtime", "/.", "\[IndentingNewLine]",
RowBox[{"FindRoot", "[",
RowBox[{
RowBox[{
RowBox[{"F", "[", "mutationtime", "]"}], "\[Equal]",
"numreplications"}], " ", ",",
RowBox[{"{",
RowBox[{
"mutationtime", ",", "starttime", ",", "timeuntilnextenvchange"}],
"}"}], ",",
RowBox[{"Method", "->", "\"\<Brent\>\""}]}], "]"}]}]}],
"\[IndentingNewLine]", "]"}]}]}], "\[IndentingNewLine]",
"]"}]}]}]], "Input",
CellChangeTimes->{{3.5245162531772647`*^9, 3.5245162791774263`*^9}, {
3.524516461231696*^9, 3.524516596603982*^9}, {3.524516718200782*^9,
3.5245167221388865`*^9}, {3.524581465890625*^9, 3.5245815205625*^9}, {
3.524581849953125*^9, 3.524581854046875*^9}, {3.524582020890625*^9,
3.524582022078125*^9}, {3.52458205925*^9, 3.524582120515625*^9}, {
3.5245821954375*^9, 3.524582202453125*^9}, {3.5245917529375*^9,
3.5245917945625*^9}, {3.5245958254798274`*^9, 3.5245958386204524`*^9}, {
3.5245958893860774`*^9, 3.5245958918704524`*^9}, {3.5245959331517024`*^9,
3.5245959343704524`*^9}, {3.5245959703235774`*^9,
3.5245959704798274`*^9}, {3.5245960120267024`*^9,
3.5245960311985774`*^9}, {3.5245961793079524`*^9,
3.5245962117298274`*^9}, {3.5245962509017024`*^9,
3.5245962543860774`*^9}, {3.5245975038079524`*^9,
3.5245975731048274`*^9}, {3.5245976043079524`*^9,
3.5245976090892024`*^9}, {3.5245976413548274`*^9,
3.5245977088235774`*^9}, {3.5245978109485774`*^9,
3.5245978148235774`*^9}, {3.5245980265735774`*^9, 3.5245981870267024`*^9},
3.5245982261360774`*^9, {3.5245983439954524`*^9, 3.5245983503860774`*^9}, {
3.5245989262610774`*^9, 3.5245989287454524`*^9}, {3.5245989587610774`*^9,
3.5245989654485774`*^9}, {3.5245990726048274`*^9,
3.5245990842610774`*^9}, {3.5246090809954524`*^9, 3.5246090839173274`*^9},
3.5246092036985774`*^9, 3.5246095483704524`*^9, {3.531245672890625*^9,
3.53124575459375*^9}, {3.531251271828125*^9, 3.531251320265625*^9}, {
3.531251429453125*^9, 3.53125145871875*^9}, {3.531251547359375*^9,
3.53125156865625*^9}, {3.531251625984375*^9, 3.53125162965625*^9}, {
3.53125168759375*^9, 3.531251690203125*^9}, {3.53125807340625*^9,
3.531258076453125*^9}, {3.5317739185*^9, 3.53177392434375*^9}, {
3.532800526890625*^9, 3.532800581046875*^9}, {3.532876951609375*^9,
3.532876993109375*^9}, {3.532877029890625*^9, 3.532877164046875*^9}, {
3.532877282796875*^9, 3.53287736334375*^9}, {3.5328774665*^9,
3.532877472046875*^9}, {3.5328805380625*^9, 3.532880559484375*^9}, {
3.532880662421875*^9, 3.5328808483125*^9}, {3.532881154625*^9,
3.532881178515625*^9}, {3.53288147971875*^9, 3.532881564203125*^9}, {
3.532881596796875*^9, 3.532881625875*^9}, {3.532881682921875*^9,
3.532881715625*^9}, 3.53288174765625*^9, {3.532881911875*^9,
3.532881921625*^9}, 3.532881994609375*^9, {3.532882249453125*^9,
3.532882267328125*^9}, {3.532882856671875*^9, 3.532882863109375*^9}, {
3.532882952171875*^9, 3.532882971921875*^9}, {3.53288361634375*^9,
3.53288364665625*^9}, {3.532883881875*^9, 3.53288401009375*^9}, {
3.532884101421875*^9, 3.5328841066875*^9}, {3.53288448621875*^9,
3.532884523140625*^9}, {3.532884670296875*^9, 3.53288471578125*^9}, {
3.532892522828125*^9, 3.53289253709375*^9}, {3.5328926011875*^9,
3.532892620453125*^9}, {3.53289547359375*^9, 3.532895474703125*^9}, {
3.5401240068125*^9, 3.540124006828125*^9}, {3.5402191719375*^9,
3.540219209375*^9}, {3.540220495890625*^9, 3.54022050034375*^9}, {
3.540222595140625*^9, 3.540222604859375*^9}, {3.540222749265625*^9,
3.54022280071875*^9}, {3.5402358691875*^9, 3.54023593678125*^9}, {
3.540235993328125*^9, 3.540236106390625*^9}, {3.5402361474375*^9,
3.5402361534375*^9}, {3.540236199015625*^9, 3.54023626475*^9}, {
3.540236296140625*^9, 3.5402363035625*^9}, {3.540558217609375*^9,
3.540558237390625*^9}, {3.540561439890625*^9, 3.540561449765625*^9}, {
3.540757297828125*^9, 3.54075731534375*^9}, {3.540817443734375*^9,
3.5408174646875*^9}, {3.54081754178125*^9, 3.540817586234375*^9}, {
3.540817645515625*^9, 3.54081769834375*^9}, {3.540817840515625*^9,
3.540817853359375*^9}, {3.5408178850625*^9, 3.540817887359375*^9}, {
3.54081794865625*^9, 3.5408179661875*^9}, {3.5408181983125*^9,
3.540818222734375*^9}, {3.5408184654375*^9, 3.54081852646875*^9}, {
3.54081856075*^9, 3.5408185745*^9}, {3.54081863021875*^9,
3.5408186415*^9}, {3.540818672*^9, 3.54081869365625*^9}, {
3.540818755734375*^9, 3.54081876578125*^9}, {3.540819334390625*^9,
3.5408193608125*^9}, {3.54082188490625*^9, 3.5408219038125*^9}, {
3.54082193965625*^9, 3.54082203109375*^9}, {3.540827880015625*^9,
3.54082794971875*^9}, {3.54082800278125*^9, 3.5408281025625*^9}, {
3.540828144453125*^9, 3.5408284086875*^9}, {3.540828451421875*^9,
3.5408284609375*^9}, {3.54082851675*^9, 3.5408285175625*^9}, {
3.54083390565625*^9, 3.540833971625*^9}, 3.634308098931197*^9, {
3.6343081464889174`*^9, 3.634308168243162*^9}, {3.634854951968799*^9,
3.6348549541389236`*^9}, {3.634855861405816*^9, 3.6348558935796566`*^9}, {
3.634856481647292*^9, 3.634856505094633*^9}, {3.6349193502145834`*^9,
3.6349193507076116`*^9}, {3.6349194434009132`*^9, 3.634919446364083*^9},
3.6349194871854177`*^9, {3.634919570118161*^9, 3.634919581230797*^9}, {
3.634919657045133*^9, 3.634919664821578*^9}, {3.63492124904219*^9,
3.634921252110366*^9}, {3.634944914224763*^9, 3.634944923005265*^9}, {
3.6349449792534823`*^9, 3.6349449860568714`*^9}, {3.635005432255288*^9,
3.6350054360895076`*^9}, {3.6350099865777807`*^9,
3.6350099899669743`*^9}, {3.635010376476082*^9, 3.6350103792412395`*^9}, {
3.6351055064038563`*^9, 3.6351055067288747`*^9}, {3.6351322289630413`*^9,
3.6351322753636956`*^9}, {3.6351738654633865`*^9, 3.635173874583908*^9}, {
3.6351776658717575`*^9, 3.635177665939761*^9}, {3.641413098315462*^9,
3.6414131016966553`*^9}, 3.6421659273032765`*^9, {3.6582601620679817`*^9,
3.6582601964001017`*^9}, {3.658260235173861*^9, 3.6582602354366837`*^9}, {
3.65826027812988*^9, 3.658260300030651*^9}, {3.658494059539296*^9,
3.658494061185502*^9}, 3.659357519606637*^9}],
Cell[BoxData[
RowBox[{
RowBox[{"(*",
RowBox[{"Fixation", " ", "probability"}], "*)"}], "\[IndentingNewLine]",
RowBox[{
RowBox[{
RowBox[{"CalcPfix", "[",
RowBox[{
"r0_", ",", "\[Mu]0_", ",", "s_", ",", "K_", ",", "mutationtime_", ",",
"popsize_", ",", "i_", ",", "q_"}], "]"}], ":=",
RowBox[{"Module", "[",
RowBox[{
RowBox[{"{", "}"}], ",", "\[IndentingNewLine]",
RowBox[{"(*",
RowBox[{
"Analytical", " ", "fixation", " ", "probability", " ", "assuming",
" ", "constant", " ", "N", " ", "and", " ", "no", " ", "env", " ",
"change"}], "*)"}], "\[IndentingNewLine]",
RowBox[{"If", "[",
RowBox[{
RowBox[{
RowBox[{"r0",
RowBox[{"(",
RowBox[{"1", "-",
FractionBox["popsize", "K"]}], ")"}]}], ">",
RowBox[{
RowBox[{"i", " ", "s"}], "+", "\[Mu]0"}]}], "\[IndentingNewLine]",
",",
RowBox[{"-",
FractionBox[
RowBox[{
RowBox[{"popsize", " ", "r0"}], "+",
RowBox[{"K", " ",
RowBox[{"(",
RowBox[{
RowBox[{"-", "r0"}], "+",
RowBox[{"i", " ", "s"}], "+", "\[Mu]0"}], ")"}]}]}],
RowBox[{
RowBox[{"(",
RowBox[{"K", "-", "popsize"}], ")"}], " ", "r0"}]]}],
"\[IndentingNewLine]", ",", "0"}], "\[IndentingNewLine]", "]"}]}],
"\[IndentingNewLine]", "]"}]}], "\[IndentingNewLine]",
"\[IndentingNewLine]",
RowBox[{"(*",
RowBox[{
"Analytical", " ", "fixation", " ", "probability", " ", "using", " ",
"averaged", " ", "N"}], "*)"}], "\[IndentingNewLine]",
RowBox[{
RowBox[{"CalcPfix2", "[",
RowBox[{
"r0_", ",", "\[Mu]0_", ",", "s_", ",", "K_", ",", "mutationtime_", ",",
"timeuntilnextenvchange_", ",", "totalpopsize_", ",", "i_", ",", "q_"}],
"]"}], ":=",
RowBox[{"Module", "[",
RowBox[{
RowBox[{"{",
RowBox[{"F", ",", "popsize"}], "}"}], ",", "\[IndentingNewLine]",
RowBox[{
RowBox[{"popsize", "=",
RowBox[{"Mean", "[",
RowBox[{"Map", "[",
RowBox[{"totalpopsize", ",",
RowBox[{"Subdivide", "[",
RowBox[{"mutationtime", ",",
RowBox[{"Min", "[",
RowBox[{"timeuntilnextenvchange", ",",
RowBox[{"mutationtime", "+",
RowBox[{"q", " ", "s"}]}]}], "]"}], ",", "100"}], "]"}]}],
"]"}], "]"}]}], ";", "\[IndentingNewLine]", "\[IndentingNewLine]",
RowBox[{"If", "[",
RowBox[{
RowBox[{
RowBox[{"r0",
RowBox[{"(",
RowBox[{"1", "-",
FractionBox["popsize", "K"]}], ")"}]}], ">",
RowBox[{
RowBox[{"i", " ", "s"}], "+", "\[Mu]0"}]}], ",",
"\[IndentingNewLine]",
RowBox[{"-",
FractionBox[
RowBox[{
RowBox[{"popsize", " ", "r0"}], "+",
RowBox[{"K", " ",
RowBox[{"(",
RowBox[{
RowBox[{"-", "r0"}], "+",
RowBox[{"i", " ", "s"}], "+", "\[Mu]0"}], ")"}]}]}],
RowBox[{
RowBox[{"(",
RowBox[{"K", "-", "popsize"}], ")"}], " ", "r0"}]]}],
"\[IndentingNewLine]", ",", "0"}], "\[IndentingNewLine]", "]"}]}]}],
"\[IndentingNewLine]", "]"}]}], "\n", "\[IndentingNewLine]",
RowBox[{"(*",
RowBox[{
"Full", " ", "numerical", " ", "integration", " ", "over", " ",
"remainder", " ", "of", " ", "current", " ", "interval", " ", "followed",
" ", "by", " ", "timebetweenenvchanges", " ", "after", " ",
RowBox[{"change", "."}]}], "*)"}], "\[IndentingNewLine]",
RowBox[{
RowBox[{"CalcPfix3", "[",
RowBox[{
"r0_", ",", "\[Mu]0_", ",", "s_", ",", "K_", ",", "mutationtime_", ",",
"timeuntilnextenvchange_", ",", "totalpopsize_", ",", "i_", ",",
"totalpopsizeafter_", ",", "timebetweenenvchanges_"}], "]"}], ":=",
RowBox[{"Module", "[",
RowBox[{
RowBox[{"{",
RowBox[{"F", ",", "G", ",", "popsize", ",", "popsizeatchange"}], "}"}],
",", "\[IndentingNewLine]", "\[IndentingNewLine]",
RowBox[{
RowBox[{"F", "=",
RowBox[{"NDSolveValue", "[",
RowBox[{
RowBox[{"{",
RowBox[{
RowBox[{
RowBox[{
RowBox[{"y", "'"}], "[", "\[Tau]", "]"}], "\[Equal]",
RowBox[{
RowBox[{"r0", " ",
RowBox[{"(",
RowBox[{"1", "-",
RowBox[{
RowBox[{"totalpopsize", "[", "\[Tau]", "]"}], "/", "K"}]}],
")"}]}], "-", "\[Mu]0", "-",
RowBox[{"s", " ", "i"}]}]}], ",",
RowBox[{
RowBox[{"y", "[", "mutationtime", "]"}], "\[Equal]", "0"}]}],
"}"}], ",", "y", ",",
RowBox[{"{",
RowBox[{
"\[Tau]", ",", "mutationtime", ",", "timeuntilnextenvchange"}],
"}"}]}], "]"}]}], ";", "\[IndentingNewLine]",
RowBox[{"G", "=",
RowBox[{"NDSolveValue", "[",
RowBox[{
RowBox[{"{",
RowBox[{
RowBox[{
RowBox[{
RowBox[{"y", "'"}], "[", "\[Tau]", "]"}], "\[Equal]",
RowBox[{
RowBox[{"r0", " ",
RowBox[{"(",
RowBox[{"1", "-",
RowBox[{
RowBox[{"totalpopsizeafter", "[", "\[Tau]", "]"}], "/",
"K"}]}], ")"}]}], "-", "\[Mu]0", "-",
RowBox[{
RowBox[{"(",
RowBox[{"i", "+", "1"}], ")"}], "s"}]}]}], " ", ",",
RowBox[{
RowBox[{"y", "[", "0", "]"}], "\[Equal]",
RowBox[{"F", "[", "timeuntilnextenvchange", "]"}]}]}], "}"}],
",", "y", ",",
RowBox[{"{",
RowBox[{"\[Tau]", ",", "0", ",", "timebetweenenvchanges"}],
"}"}]}], "]"}]}], ";", "\[IndentingNewLine]",
"\[IndentingNewLine]",
RowBox[{"2", "/",
RowBox[{"(",
RowBox[{"1", "+",
RowBox[{"NIntegrate", "[",
RowBox[{
RowBox[{
RowBox[{"(",
RowBox[{
RowBox[{"r0", " ",
RowBox[{"(",
RowBox[{"1", "-",
RowBox[{
RowBox[{"totalpopsize", "[", "t", "]"}], "/", "K"}]}],
")"}]}], "+", "\[Mu]0", "+",
RowBox[{"s", " ", "i"}]}], ")"}], " ",
RowBox[{"Exp", "[",
RowBox[{"-",
RowBox[{"F", "[", "t", "]"}]}], "]"}]}], ",",
RowBox[{"{",
RowBox[{
"t", ",", "mutationtime", ",", "timeuntilnextenvchange"}],
"}"}]}], "]"}], "+",
RowBox[{"NIntegrate", "[",
RowBox[{
RowBox[{
RowBox[{"(",
RowBox[{
RowBox[{"r0", " ",
RowBox[{"(",
RowBox[{"1", "-",
RowBox[{
RowBox[{"totalpopsizeafter", "[", "t", "]"}], "/", "K"}]}],
")"}]}], "+", "\[Mu]0", "+",
RowBox[{"s", " ",
RowBox[{"(",
RowBox[{"i", "+", "1"}], ")"}]}]}], ")"}], " ",
RowBox[{"Exp", "[",
RowBox[{"-",
RowBox[{"G", "[", "t", "]"}]}], "]"}]}], ",",
RowBox[{"{",
RowBox[{"t", ",", "0", ",", "timebetweenenvchanges"}], "}"}]}],
"]"}]}], ")"}]}]}]}], "\[IndentingNewLine]", "]"}]}]}]}]], "Input",\
CellChangeTimes->{{3.6433963029212875`*^9, 3.643396303964347*^9},
3.6433963928574314`*^9, {3.643397011067791*^9, 3.643397052272148*^9},
3.658260202795792*^9, {3.658260321698737*^9, 3.658260342359684*^9},
3.6593575196067047`*^9, {3.6593799781824713`*^9, 3.659380013303062*^9}, {
3.659380401109899*^9, 3.659380407149775*^9}, {3.659380549594687*^9,
3.659380556055256*^9}, {3.659380662185565*^9, 3.659380668625708*^9},
3.659381590630258*^9, {3.659381621878663*^9, 3.6593816228458138`*^9}, {
3.659381798703782*^9, 3.659381801122719*^9}, {3.659381840654212*^9,
3.659381844336317*^9}, 3.659385043596918*^9, {3.659385278672112*^9,
3.659385281336136*^9}, {3.659385659646274*^9, 3.6593856616693983`*^9},
3.6593858156878347`*^9, {3.6593864994487677`*^9, 3.659386499591949*^9}, {
3.659388202220945*^9, 3.659388205871407*^9}, {3.659388317409442*^9,
3.659388327729501*^9}, {3.659389432390753*^9, 3.659389456855391*^9}, {
3.659391202381497*^9, 3.6593912065253897`*^9}}],
Cell[BoxData[
RowBox[{
RowBox[{"(*",
RowBox[{
"Initial", " ", "populaion", " ", "size", " ", "for", " ", "deterministic",
" ", "growth", " ", "after", " ", "establishment"}], "*)"}],
"\[IndentingNewLine]",
RowBox[{
RowBox[{"CalcEstablishmentTime", "[", "pfix_", "]"}], ":=",
"\[IndentingNewLine]",
RowBox[{"Module", "[",
RowBox[{
RowBox[{"{",
RowBox[{"ran", ",", "v0"}], "}"}], ",", "\[IndentingNewLine]",
RowBox[{
RowBox[{"ran", "=",
RowBox[{"RandomReal", "[", "]"}]}], ";", "\[IndentingNewLine]",
RowBox[{"v0", "=",
RowBox[{"-",
FractionBox[
RowBox[{"Log", "[",
RowBox[{"1", "-", "ran"}], "]"}], "pfix"]}]}], ";",
"\[IndentingNewLine]",
RowBox[{"{",
RowBox[{"ran", ",", "v0"}], "}"}]}]}], "\[IndentingNewLine]",
"]"}]}]}]], "Input",
CellChangeTimes->{{3.5336539685*^9, 3.533654073125*^9}, {3.5336541335*^9,
3.533654140640625*^9}, {3.53365420871875*^9, 3.533654226328125*^9}, {
3.533654367078125*^9, 3.533654449703125*^9}, {3.533655005*^9,
3.533655005328125*^9}, 3.533662273015625*^9, 3.634308098940198*^9,
3.634308168243162*^9, {3.6583324690410547`*^9, 3.658332494569017*^9},
3.659357519606781*^9}],
Cell[BoxData[
RowBox[{
RowBox[{"PruneUnfitClasses", "[",
RowBox[{"numgenotypes_", ",", "genotypeabundances_"}], "]"}], ":=",
RowBox[{"Module", "[",
RowBox[{
RowBox[{"{",
RowBox[{"i", ",", "newnumgenotypes", ",", "newgenotypeabundances"}],
"}"}], ",", "\[IndentingNewLine]",
RowBox[{
RowBox[{"i", "=", "numgenotypes"}], ";", "\[IndentingNewLine]",
RowBox[{"newnumgenotypes", "=", "numgenotypes"}], ";",
"\[IndentingNewLine]",
RowBox[{"While", "[",
RowBox[{
RowBox[{"i", ">", "0"}], ",", "\[IndentingNewLine]",
RowBox[{"If", "[",
RowBox[{
RowBox[{
RowBox[{"genotypeabundances", "[",
RowBox[{"[", "i", "]"}], "]"}], "<", "1"}], ",",
"\[IndentingNewLine]",
RowBox[{
RowBox[{"i", "=",
RowBox[{"i", "-", "1"}]}], ";", "\[IndentingNewLine]",
RowBox[{"If", "[",
RowBox[{
RowBox[{"i", "\[Equal]", "0"}], ",", "\[IndentingNewLine]",
RowBox[{
RowBox[{"If", "[",
RowBox[{
RowBox[{"(", "verbose", ")"}], ",",
RowBox[{
RowBox[{
"Print", "[", "\"\<WARNING: population extinction\>\"", "]"}],
";"}]}], "]"}], ";", "\[IndentingNewLine]",
RowBox[{"newgenotypeabundances", "=",
RowBox[{"{", "}"}]}], ";", "\[IndentingNewLine]",
RowBox[{"newnumgenotypes", "=", "0"}], ";"}]}],
"\[IndentingNewLine]", "]"}], ";"}], "\[IndentingNewLine]", ",",
"\[IndentingNewLine]",
RowBox[{
RowBox[{"If", "[",
RowBox[{
RowBox[{"i", "<", "numgenotypes"}], ",", "\[IndentingNewLine]",
RowBox[{
RowBox[{"If", "[",
RowBox[{
RowBox[{"(", "veryverbose", ")"}], ",",
RowBox[{
RowBox[{"Print", "[",
RowBox[{"\"\<Drop \>\"", ",",
RowBox[{"numgenotypes", "-", "i"}], ",",
"\"\< unfit genotype class(es) from \>\"", ",",
"genotypeabundances"}], "]"}], ";"}]}], "]"}], ";"}]}],
"]"}], ";", "\[IndentingNewLine]",
RowBox[{"newgenotypeabundances", "=",
RowBox[{"Drop", "[",
RowBox[{"genotypeabundances", ",",
RowBox[{"i", "-", "numgenotypes"}]}], "]"}]}], ";",
"\[IndentingNewLine]",
RowBox[{"newnumgenotypes", "=", "i"}], ";", "\[IndentingNewLine]",
RowBox[{"i", "=", "0"}], ";"}]}], "\[IndentingNewLine]", "]"}]}],
"\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]",
RowBox[{"{",
RowBox[{"newnumgenotypes", ",", "newgenotypeabundances"}], "}"}]}]}],
"\[IndentingNewLine]", "]"}]}]], "Input",
CellChangeTimes->{{3.5327987325625*^9, 3.53279878440625*^9}, {
3.53279883225*^9, 3.532799218265625*^9}, 3.532799318890625*^9,
3.532799493234375*^9, {3.532799665796875*^9, 3.5327997643125*^9}, {
3.532799820078125*^9, 3.532799853203125*^9}, {3.53279992921875*^9,
3.532799980140625*^9}, {3.53289683884375*^9, 3.53289684015625*^9}, {
3.54012533690625*^9, 3.5401253518125*^9}, {3.540739342140625*^9,
3.54073934253125*^9}, 3.6343080989481983`*^9, 3.634308168244162*^9, {
3.659297242196486*^9, 3.659297243827331*^9}, 3.659357519606844*^9}],
Cell[BoxData[
RowBox[{
RowBox[{"RunSimulationCheckEachMutation2", "[",
RowBox[{
"r0_", ",", "\[Mu]0_", ",", "s_", ",", "K_", ",", "U_", ",", "maxtime_",
",", "timebetweenenvchanges_", ",", "initmostfitclass_"}], "]"}], ":=",
RowBox[{"Module", "[",
RowBox[{
RowBox[{"{",
RowBox[{
"genotypeabundances", ",", "globaltime", ",", "numgenotypes", ",",
"system", ",", "sol", ",", "solafter", ",", "totalpopsize", ",",
"totalpopsizeafter", ",", "bestclasssize", ",", "bestclassbirths", ",",
"mutationtime", ",", "pfix", ",", "v0", ",", "establishtimeran", ",",
"fixation", ",", "fixationran", ",", "timeuntilnextenvchange", ",",
"lastenvwaittime", ",", "oldtotalpopsize", ",", "q", ",", "extinct",
",", "mostfitclass", ",", "history", ",", "esttime", ",", "qest"}],
"}"}], ",", "\[IndentingNewLine]", " ",
RowBox[{
RowBox[{"ivals", "=",
RowBox[{"{", "}"}]}], ";",
RowBox[{"qvals", "=",
RowBox[{"{", "}"}]}], ";", "\[IndentingNewLine]",
RowBox[{"(*",
RowBox[{
"Weird", " ", "context", " ", "conflicts", " ", "were", " ", "occuring",
" ", "when", " ", "using", " ", "Parallel", " ", "unless", " ",
"these", " ", "symbols", " ", "have", " ", "already", " ", "been", " ",
RowBox[{"used", ".", " ", "Ideally"}], " ", "should", " ", "not", " ",
"be", " ",
RowBox[{"here", "!"}]}], "*)"}], "\[IndentingNewLine]",
RowBox[{"{",
RowBox[{
"n1", ",", "n2", ",", "n3", ",", "n4", ",", "n5", ",", "n6", ",", "n7",
",", "n8", ",", "n9", ",", "n10", ",", "n11", ",", "n12", ",", "n13",
",", "n14", ",", "n15", ",", "n16", ",", "n17", ",", "n18", ",", "n19",
",", "n20", ",", "n21", ",", "n22", ",", "n23", ",", "n24", ",",
"n25", ",", "n26", ",", "n27", ",", "n28", ",", "n29", ",", "n30", ",",
"n31", ",", "n32", ",", "n33", ",", "n34", ",", "n35", ",", "n36",
",", "n37", ",", "n38", ",", "n39", ",", "n40", ",", "n41", ",", "n42",
",", "n43", ",", "n44", ",", "n45", ",", "n46", ",", "n47", ",",
"n48", ",", "n49", ",", "n50", ",", "n51", ",", "n52", ",", "n53", ",",
"n54", ",", "n55", ",", "n56", ",", "n57", ",", "n58", ",", "n59",
",", "n60", ",", "n61", ",", "n62", ",", "n63", ",", "n64", ",", "n65",
",", "n66", ",", "n67", ",", "n68", ",", "n69", ",", "n70", ",",
"n71", ",", "n72", ",", "n73", ",", "n74", ",", "n75", ",", "n76", ",",
"n77", ",", "n78", ",", "n79", ",", "n80", ",", "n81", ",", "n82",
",", "n83", ",", "n84", ",", "n85", ",", "n86", ",", "n87", ",", "n88",
",", "n89", ",", "n90", ",", "n91", ",", "n92", ",", "n93", ",",
"n94", ",", "n95", ",", "n96", ",", "n97", ",", "n98", ",", "n99", ",",
"n100"}], "}"}], ";", "\[IndentingNewLine]",
RowBox[{"mostfitclass", "=", "initmostfitclass"}], ";",
"\[IndentingNewLine]", "\[IndentingNewLine]",
RowBox[{"(*",
RowBox[{
RowBox[{"If", "[",
RowBox[{
RowBox[{
RowBox[{"(", " ",
RowBox[{
RowBox[{"K",
RowBox[{
RowBox[{"(",
RowBox[{"r", "-", "\[Mu]", "-",
RowBox[{"i", " ", "s"}]}], ")"}], "/", "r"}], " ",
RowBox[{"(",
RowBox[{"\[Mu]", "+",
RowBox[{"i", " ", "s"}]}], ")"}], " ", "U", " ", "i", " ",
RowBox[{"Log", "[", " ",
RowBox[{"K",
RowBox[{
RowBox[{"(",
RowBox[{"r", "-", "\[Mu]", "-",
RowBox[{"i", " ", "s"}]}], ")"}], "/", "r"}], " ", "s"}],
"]"}]}], "/.",
RowBox[{"i", "\[Rule]", " ",
RowBox[{
RowBox[{"(",
RowBox[{"r0", "-", "\[Mu]0"}], ")"}], "/",
RowBox[{"(",
RowBox[{"2", "s"}], ")"}]}]}]}], ")"}], ">", "1"}], ",",
"\[IndentingNewLine]",
RowBox[{"(*",
RowBox[{"multiple", " ", "mutations", " ", "regime"}], "*)"}],
"\[IndentingNewLine]",
RowBox[{
RowBox[{"esttime", "=",
RowBox[{
RowBox[{
RowBox[{"(",
RowBox[{
RowBox[{"(",
RowBox[{
RowBox[{"2",
RowBox[{"Log", "[", " ",
RowBox[{
RowBox[{"K", "/", "r"}],
RowBox[{"(",
RowBox[{"r", "-",
RowBox[{"i", " ", "s"}], "-", "\[Mu]"}], ")"}], " ",
"s"}], "]"}]}], "-",
RowBox[{"Log", "[",
RowBox[{"s", "/",
RowBox[{"(",
RowBox[{"U", " ", "i"}], ")"}]}], "]"}]}], ")"}],
RowBox[{"s", "/",
RowBox[{
RowBox[{"(",
RowBox[{"Log", "[",
RowBox[{"s", "/",
RowBox[{"(",
RowBox[{"U", " ", "i"}], ")"}]}], "]"}], ")"}], "^",
"2"}]}]}], ")"}], "^",
RowBox[{"-", "1"}]}], " ", "/.",
RowBox[{"i", "\[Rule]", " ",
RowBox[{
RowBox[{"(",
RowBox[{"r0", "-", "\[Mu]0"}], ")"}], "/",
RowBox[{"(",
RowBox[{"2", "s"}], ")"}]}]}]}]}], ";", "\[IndentingNewLine]",
RowBox[{"qest", "=",
RowBox[{"Floor", "[",
RowBox[{
RowBox[{"2", " ",
RowBox[{
RowBox[{"Log", "[", " ",
RowBox[{"K",
RowBox[{"(",
RowBox[{"r", "-", "\[Mu]", "-",
RowBox[{"i", " ", "s"}]}], ")"}], " ",
RowBox[{"s", "/", "r"}]}], "]"}], "/",
RowBox[{"Log", "[",
RowBox[{"s", "/",
RowBox[{"(",
RowBox[{"U", " ", "i"}], ")"}]}], "]"}]}]}], "/.",
RowBox[{"i", "\[Rule]", " ",
RowBox[{
RowBox[{"(",
RowBox[{"r0", "-", "\[Mu]0"}], ")"}], "/",
RowBox[{"(",
RowBox[{"2", "s"}], ")"}]}]}]}], "]"}]}], ";",
"\[IndentingNewLine]",
RowBox[{"genotypeabundances", "=",
RowBox[{"Table", "[",
RowBox[{
RowBox[{"Exp", "[",
RowBox[{
RowBox[{"-", "esttime"}], "*",
RowBox[{"Abs", "[",
RowBox[{"i", " ", "s"}], "]"}]}], "]"}], ",",
RowBox[{"{",
RowBox[{"i", ",",
RowBox[{"-", "qest"}], ",", "qest"}], "}"}]}], "]"}]}], ";",
"\[IndentingNewLine]",
RowBox[{"genotypeabundances", "=",
RowBox[{
FractionBox[
RowBox[{"K", " ",
RowBox[{"(",
RowBox[{"r0", "-", "\[Mu]0", "-",
RowBox[{
RowBox[{"(",
RowBox[{"mostfitclass", "+", "qest"}], ")"}], " ", "s"}]}],
")"}]}], "r0"],
RowBox[{"genotypeabundances", "/",
RowBox[{"Total", "[", "genotypeabundances", "]"}]}]}]}], ";",
"\[IndentingNewLine]",
RowBox[{"numgenotypes", "=",
RowBox[{"Length", "[", "genotypeabundances", "]"}]}], ";"}],
"\[IndentingNewLine]", ",", "\[IndentingNewLine]",
RowBox[{"(*",
RowBox[{"successional", " ", "regime"}], "*)"}],
"\[IndentingNewLine]",
RowBox[{
RowBox[{"genotypeabundances", "=",
RowBox[{"{",
FractionBox[
RowBox[{"K", " ",
RowBox[{"(",
RowBox[{"r0", "-", "\[Mu]0", "-",
RowBox[{"mostfitclass", " ", "s"}]}], ")"}]}], "r0"], "}"}]}],
";", "\[IndentingNewLine]",
RowBox[{"numgenotypes", "=", "1"}], ";"}]}], "\[IndentingNewLine]",
"]"}], ";"}], "*)"}], "\[IndentingNewLine]",
RowBox[{"genotypeabundances", "=",
RowBox[{"{",
RowBox[{
RowBox[{
FractionBox[
RowBox[{"K", " ",
RowBox[{"(",
RowBox[{"r0", "-", "\[Mu]0", "-",
RowBox[{"mostfitclass", " ", "s"}]}], ")"}]}], "r0"], "/", "4"}],
",",
RowBox[{
FractionBox[
RowBox[{"K", " ",
RowBox[{"(",
RowBox[{"r0", "-", "\[Mu]0", "-",
RowBox[{"mostfitclass", " ", "s"}]}], ")"}]}], "r0"], "/", "2"}],
",",
RowBox[{
FractionBox[
RowBox[{"K", " ",
RowBox[{"(",
RowBox[{"r0", "-", "\[Mu]0", "-",
RowBox[{"mostfitclass", " ", "s"}]}], ")"}]}], "r0"], "/",
"4"}]}], "}"}]}], ";", "\[IndentingNewLine]",
RowBox[{"numgenotypes", "=", "3"}], ";", "\[IndentingNewLine]",
"\[IndentingNewLine]",
RowBox[{"globaltime", "=", "0"}], ";", "\[IndentingNewLine]",
RowBox[{"timeuntilnextenvchange", "=",