-
Notifications
You must be signed in to change notification settings - Fork 1
/
mendel.f90
1407 lines (1169 loc) · 47.5 KB
/
mendel.f90
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
program mendel
use init
use inputs
use genome
use profiler
use polygenic
use random_pkg
use selection_module
include 'common.h'
! START_MPI
include 'mpif.h'
! END_MPI
! Data structures to maintain genetic information
real*8, allocatable, target, dimension(:,:,:) :: linkage_block_fitness
integer, allocatable, target, dimension(:,:,:) :: dmutn, nmutn, fmutn
integer, allocatable, target, dimension(:,:,:,:) :: lb_mutn_count
real, allocatable, dimension(:) :: initial_allele_effects
real*8, allocatable, dimension(:) :: fitness, pheno_fitness
real*8, allocatable, dimension(:) :: work_fitness, sorted_score
logical, allocatable, dimension(:) :: available
integer, allocatable, dimension(:) :: global_run_status
integer, allocatable, dimension(:) :: pop_size_array
integer*4 :: chmod
integer :: i, j, k, lb, m, nmax, gen, run_status
integer :: npath, max_size, this_size, gen_0, shutdown_gen
integer :: other_run_status, bottleneck_modulo, ibuff
! START_MPI
integer :: status2(MPI_Status_size,2), requests(2)
! END_MPI
integer :: total_offspring, offspring_count, empty, red, green, blue
integer :: ica_count(3), cumulative_offspring, mutn_indx
integer :: pop_size_allocation, current_global_pop_size
integer :: global_pop, mean_pop, delta_pop, delta, nie, mm, id
integer :: pop_size_winner, pop_size_loser, num_migrate
integer :: id_winner, id_loser
integer :: num_dmutns, num_fmutns, encode_mutn, string(40)
integer :: OLDGROUP,NEWGROUP,ranks(1),num_tribes_at_start
integer :: num_demes, other, active_demes(100)
integer :: fission_count, tribe_state, pop_ceiling
integer, parameter :: ZOMBIE = 0, LIVE = 1
real*8 accum(50), reproductive_advantage_factor
real selection_coefficient, aoki, migration_rate, x
real tribal_score, random_effects, genetic_effects, social_effects
real fraction_elimination, fraction_selected_away
real random, num_offspring, fav_mutn_per_gen, d
real real_pop_size, old_pop_size, growth_rate, gr1, gr2
real tin_migration, tout_migration, tin_gen, tout_gen, tin_run
real tin_offspring, tout_offspring, tgen, par_tgen
real tin_diagnostics, tout_diagnostics, tin_selection, tout_selection
real total_time_offspring, time_offspring, time_selection
real par_time_offspring, par_time_selection, tsub
real reproductive_rate_input
logical found, print_flag, am_parallel, file_exists, winner, create_fav_mutn
character*3 myid_str
character(len=128) :: arg, filename
integer, dimension(5) :: prescribed_pop_growth
! prescribed growth array starts with generation 2
prescribed_pop_growth = (/16,128,1024,8192,10000/)
call second(tin_run)
! Get command-line arguments
do i = 1, iargc()
call getarg(i, arg)
if (arg == "-h") then
print *, "usage: mendel {-c | -f} [filename]"
print *, "-c creates a new mendel.in file"
print *, "-f uses the specified filename"
print *, " e.g. mendel -f /home/bob/mendel.in"
stop
elseif (arg == "-d") then
call set_default_parameters
print *, "Using default parameters"
exit
elseif (arg == "-f") then
call getarg(i+1, filename)
print *, "filename is: ", filename
exit
elseif (arg == "-c") then
print *, "create input file"
open (5, file='./mendel.in.new')
call set_default_parameters
call write_parameters(5)
close(5)
print *, "Parameter file written: mendel.in.new"
stop
else ! web interface
filename = './mendel.in'
! print *, "ERROR: argument not supported. Try mendel -h", arg
! stop
end if
end do
if ( arg /= "-d" ) then
! Default filename
if ( iargc() == 0 ) then
filename = './mendel.in'
end if
print *, "Using filename: ", filename
! Open file containing run parameters
inquire(file=filename, exist=file_exists)
if ( file_exists ) then
open (5, file=filename, status='old')
else
print *, 'ERROR: could not find ',filename,len(filename)
stop
end if
! Read input parameters from input file mendel.in.
call read_parameters(5)
close(5)
end if
! Perform certain initializations including opening files.
call initialize(myid_str)
run_status = 0
if(is_parallel) then
! Since we may turn off is_parallel in case a tribe dies,
! remember the original state.
am_parallel = .true.
num_tribes_at_start = num_tribes
! for fission_tribes
num_demes = 1
active_demes = 0 ! iniitialize all values to zero
active_demes(1) = 1 ! number of currently active demes
active_demes(2) = 0 ! MPI id of first active deme (zero-based)
fission_count = 0 ! keep track of how many times fission happens
if (fission_tribes .and. fission_type /= 1) then
if (myid == 0) then
tribe_state = LIVE
live_pop_size = pop_size
else
tribe_state = ZOMBIE
live_pop_size = 0
endif
else
live_pop_size = pop_size
tribe_state = LIVE
endif
! compute global population size
!START_MPI
call mpi_isum(live_pop_size, global_pop_size, 1)
call mpi_mybcasti(global_pop_size, 1)
!END_MPI
allocate( global_run_status(num_tribes) )
allocate( pop_size_array(num_tribes) )
else
am_parallel = .false.
endif
reproductive_rate_input = reproductive_rate
if(is_parallel .and. tribal_competition) then
global_run_status = 0
run_status = 0
other_run_status = 0
pop_size_array = pop_size
pop_size_allocation = global_pop_size
write(*,*) 'Allocating tribe ', myid, ' with max pop_size of:',&
pop_size_allocation
elseif (pop_growth_model==2 .or. pop_growth_model==4) then
pop_size_allocation = 1.2*carrying_capacity*reproductive_rate
elseif (pop_growth_model == 3) then
bottleneck_yes = .false.
gr1 = pop_growth_rate
gr2 = pop_growth_rate2
reproductive_rate = gr1
if (is_parallel .and. fission_tribes) then
pop_size_allocation = 1.2*carrying_capacity*reproductive_rate*num_tribes
else
pop_size_allocation = 1.2*carrying_capacity*reproductive_rate
endif
else
pop_size_allocation = pop_size
endif
if(tribal_competition) then
fraction_selected_away = 1. - 1./reproductive_rate
tribal_fitness_factor = 1.d0
max_size = int(0.55*12.*pop_size_allocation &
*(1. - fraction_random_death))
nmax = 12.*(1. - fraction_random_death) + 0.999
! Limit the minimum value of heritability to be 10**-20.
group_heritability = max(1.e-20, group_heritability)
elseif (pop_growth_model == 3) then
max_size = int(pop_size_allocation*(1. - fraction_random_death))
nmax = 2.*reproductive_rate*(1. - fraction_random_death) + 0.999
else
max_size = int(1.1*reproductive_rate*pop_size_allocation &
*(1. - fraction_random_death))
nmax = 2.*reproductive_rate*(1. - fraction_random_death) + 0.999
end if
! Allocate memory for large arrays.
allocate( dmutn(max_del_mutn_per_indiv/2,2,max_size), &
nmutn(max_neu_mutn_per_indiv/2,2,max_size), &
fmutn(max_fav_mutn_per_indiv/2,2,max_size), &
lb_mutn_count(num_linkage_subunits,2,3,max_size),&
linkage_block_fitness(num_linkage_subunits,2,max_size), &
initial_allele_effects(num_linkage_subunits), &
pheno_fitness(max_size), fitness(max_size), &
work_fitness(max_size), sorted_score(max_size), &
available(pop_size_allocation) )
allocate( gp(max_size) )
if(polygenic_beneficials) then
allocate( pmutn(max_polys) )
poly_gen_first_instance = -1
num_polys_cumulative = 0
endif
!call init_genome(max_size,dmutn,nmutn,fmutn,linkage_block_fitness,lb_mutn_count)
x = 1E6 ! Megabyte
print *, '-------------------------------------'
print *, 'MEMORY USAGE INFORMATION (MBYTES):'
print *, 'genotype pointer:', sizeof(gp)/x
print *, 'dmutn size:' , sizeof(dmutn)/x
print *, 'fmutn size:' , sizeof(fmutn)/x
print *, 'nmutn size:' , sizeof(nmutn)/x
print *, 'lb_mutn_count size:',sizeof(lb_mutn_count)/x
print *, 'linkage_block_fitness size:',sizeof(linkage_block_fitness)/x
print *, '-------------------------------------'
print *
print *, 'Initializing data arrays... please wait....'
! If this is a restart case, read the restart dump file and
! set the current dump number to the restart dump number.
! Otherwise, set it to zero. The variable gen_0 is the initial
! generation number, retrieved from the restart dump in a restart
! case and zero otherwise.
if(restart_case) then
call read_restart_dump(dmutn,nmutn,fmutn,lb_mutn_count, &
linkage_block_fitness, &
initial_allele_effects, &
gen_0,max_size,myid_str)
dump_number = restart_dump_number
else
gen_0 = 0
dump_number = 0
end if
! If the bottleneck flag, bottleneck_yes, is false, set the value
! of bottleneck_generation beyond the generation range for this run.
if(.not.bottleneck_yes .and. pop_growth_model /= 3) &
bottleneck_generation = 1 + gen_0 + num_generations
! Initialize the population size to be equal to the parameter
! pop_size unless the parameter bottleneck_generation has the
! value zero. In the latter case, initialize the population size
! to bottleneck_pop_size.
if(abs(bottleneck_generation) > 0) then
current_pop_size = pop_size
else
current_pop_size = bottleneck_pop_size
end if
! Setup cyclic bottlenecking
if(bottleneck_yes.and.bottleneck_generation < 0) then
if(num_bottleneck_generations >= abs(bottleneck_generation)) &
then
write(*,*) 'ERROR: num_bottleneck_generations ', &
'>= cyclic bottleneck_generations'
stop
end if
bottleneck_modulo = abs(bottleneck_generation)
bottleneck_generation = abs(bottleneck_generation)
cyclic_bottlenecking = .true.
end if
! If not a restart case, initialize entire population to have no
! initial mutations.
! Initialize the linkage block fitness such that all individuals
! in the population have identical haplotypes. If initial
! contrasting alleles are to be included, generate them here.
if(.not.restart_case) then
dmutn = num_linkage_subunits*lb_modulo + 1
nmutn = num_linkage_subunits*lb_modulo + 1
fmutn = num_linkage_subunits*lb_modulo + 1
dmutn(1,:,:) = 0
fmutn(1,:,:) = 0
! With polygenic beneficials each linkage block represents a single
! nucleotide. Correspondingly, we do not accumulate mutations as
! in the traditional sense, but rather we maintain a single mutation
! for each linkage block. Therefore, number of mutations in each
! haplotype is constant, equal to the number of linkage subunits.
! We also use the array fmutn to store whether the string in nmutn
! matches the target or not and, if so, identify that instance with
! a unique positive identification number in fmutn(2,:,:).
if (polygenic_beneficials) then
nmutn(1,:,:) = num_linkage_subunits
fmutn(1,:,:) = 1
fmutn(2,:,:) = 0
write(*,'(/A,$)') 'POLYGENIC STRING INITIALIZATION: '
do lb=1,num_linkage_subunits
nmutn(1+lb,:,:) = nucl_to_int(polygenic_init(lb:lb))
write(*,'(a,$)') int_to_nucl(nucl_to_int(polygenic_init(lb:lb)))
enddo
write(*,*)
! if(recombination_model==full_sexual) recombination_model = suppressed
else
nmutn(1,:,:) = 0
lb_mutn_count = 0
endif
linkage_block_fitness = 1.d0
if(num_contrasting_alleles > 0) &
call gen_initial_contrasting_alleles(dmutn, fmutn, &
linkage_block_fitness, initial_allele_effects, max_size)
end if
! Read in a file containing a specific set of mutations.
if(upload_mutations) then
call read_mutn_file(dmutn,nmutn,fmutn,lb_mutn_count, &
linkage_block_fitness,max_size)
end if
! Generate num_initial_fav_mutn random initial favorable mutations.
do k=1,num_initial_fav_mutn
call favorable_mutn(fmutn,lb_mutn_count,linkage_block_fitness)
end do
post_sel_fitness = 1.d0
ica_count = 0
call second(tout)
sec(1) = sec(1) + tout - tin
! Step population through num_generations generations.
do gen=gen_0+1,gen_0+num_generations
!call print_genotype(1)
!call print_genotype(1,10)
msg_num = 1
call second(tin_gen)
! If the generation number lies within the bottleneck interval,
! set the current population size to bottleneck_pop_size.
if(cyclic_bottlenecking.and. &
(mod(gen,bottleneck_modulo)==0 &
.and.gen>gen_0+1+bottleneck_modulo)) then
bottleneck_generation = bottleneck_generation + bottleneck_modulo
end if
if(bottleneck_yes .and. gen >= bottleneck_generation .and. &
gen < bottleneck_generation + num_bottleneck_generations) &
then
current_pop_size = bottleneck_pop_size
do i=6,9,3
write(i,'(/"BOTTLENECK down to ", &
i6," individual(s) at generation = ",i6)') &
bottleneck_pop_size, gen
end do
end if
fertility_factor = 1. - fraction_random_death
! For competing tribes compute tribal fertility factor.
if(is_parallel .and. tribal_competition .and. &
gen > gen_0+1) then
reproductive_advantage_factor = tribal_fitness_factor-1.d0
! Compute the tribal fertility factor. The number is used
! to control the size of the various tribes.
fertility_factor = (1.d0 - fraction_random_death) &
*(1.d0 + tc_scaling_factor &
*reproductive_advantage_factor)
!selection_coefficient = max(1.d0 - fertility_factor,0.d0)
selection_coefficient = 1.d0 - fertility_factor
if(mod(gen,10)==0) then
write(*,'("myid =",i2," fertility_factor =",f7.4," selection_coefficient =",f7.4)') &
myid, fertility_factor, selection_coefficient
end if
endif
! Move individuals between tribes/processors.
!START_MPI
if (is_parallel.and.mod(gen,migration_generations)==0.and. &
num_indiv_exchanged > 0) then
call second(tin_migration)
if(mod(gen,10)==0 .and. myid==0) then
write(6,'(/"migrating ",i4," individual(s) every", &
i4," generation(s) between",i4," tribes")') &
num_indiv_exchanged, migration_generations, &
num_tribes
if(tribal_competition) then
write(*,*) 'competing pop sizes:', pop_size_array
endif
end if
call migration(dmutn,nmutn,fmutn,linkage_block_fitness, &
lb_mutn_count,gen,ierr,msg_num)
call second(tout_migration)
sec(4) = sec(4) + tout_migration - tin_migration
end if
!END_MPI
call second(tin_offspring)
num_offspring = 2.d0*reproductive_rate*fertility_factor
if(mod(gen - gen_0, 10) == 1 .or. gen - gen_0 <= 10) then
cumulative_offspring = 0
new_mutn_count = 0
end if
! Re-initialize random number generator using PID xor Time.
if(reseed_rng) call init_random_seed()
! Randomly mate one half of the population with members
! from the other half.
time_offspring = 0
call mating(dmutn,nmutn,fmutn,lb_mutn_count,linkage_block_fitness, &
num_offspring,available,pop_size_allocation,nmax,offspring_count, &
total_offspring,tsub,gen)
! create a beneficial mutation for each individual which received
! the polygenic target during this generation
if(polygenic_beneficials) then
! For waiting time experiments we map the mutations to nucleotides
! (e.g. the mutation number 732853 represents a mutation such as A->C)
! therefore, we need to maintain one mutation per linkage block
! in the neutral mutation array.
! Loop through all individuals in the population and count the number
! of matches with the target nucleotide string.
num_polys_this_gen = 0
do id=1,current_pop_size
do j=1,2 ! haplotype
if(fmutn(2,j,id) > 0) then
num_polys_this_gen = num_polys_this_gen + 1
endif
enddo
enddo
percent_pop_poly = real(num_polys_this_gen)/real(current_pop_size)/2.*100.
if(recombination_model == clonal) percent_pop_poly = 2.*percent_pop_poly
if (percent_pop_poly >= 99) then
polygenic_fixed = .true.
poly_stop_gen = gen
print *
print *, 'POLYGENICS: SHUTTING DOWN BECAUSE 99% OF POPULATION HAS ALLELE'
call diagnostics_history_plot(dmutn, nmutn, fmutn, lb_mutn_count, &
ica_count, gen, .true., current_global_pop_size)
goto 20 ! shutdown
endif
endif
time_offspring = time_offspring + tsub
! Because the Poisson random number generator does not yield
! the specified mean number of new mutations to sufficient
! accuracy, to improve accuracy make an adjustment to the value
! fed to the generator.
cumulative_offspring = cumulative_offspring + offspring_count
if((mod(gen - gen_0, 10) == 0 .or. gen - gen_0 < 10) .and. &
mutn_rate >= 1.) then
d = real(new_mutn_count)/real(cumulative_offspring)
poisson_mean = poisson_mean + 0.3*(mutn_rate - d)
end if
if(is_parallel .and. tribal_competition) then
! Modify the tribal population size such that selection
! intensity depends only on the default fertility.
real_pop_size = offspring_count/(reproductive_rate &
*(1. - fraction_random_death))
if(fitness_dependent_fertility) real_pop_size = &
real_pop_size/sqrt(min(1.d0, post_sel_fitness))
current_pop_size = int(real_pop_size)
if(real_pop_size - current_pop_size > randomnum(1)) &
current_pop_size = current_pop_size + 1
migration_rate = num_indiv_exchanged/real(current_pop_size)
! Following is the k value, what Aoki calls "group selection intensity"
! ref: Aoki, Kenichi, "A condition for group selection to prevail over
! counteracting selection" by Kenichi Aoki, Evolution 36(4), 1982,
! pp. 832-842.
aoki = 2*selection_coefficient*current_pop_size*migration_rate
if(myid.eq.0) write(*,'(a,i5,x,a,f7.4,x,a,f7.4,x,a,i7)') &
'gen:', gen, 'group_selection_intensity: ', aoki, &
'migration_rate:', migration_rate, &
'deme_size:', current_pop_size
! Modify the tribal population size to keep the global
! population size nearly constant.
!START_MPI
call mpi_mybarrier()
call mpi_isum(current_pop_size,global_pop,1)
call mpi_mybcasti(global_pop,1)
!END_MPI
mean_pop = global_pop_size/num_tribes
delta_pop = global_pop_size-global_pop
delta = 2*delta_pop/num_tribes
if(delta_pop > 0 .and. current_pop_size > mean_pop) then
k = min(delta, int(delta*randomnum(1) + 0.5))
current_pop_size = current_pop_size + k
elseif(delta_pop<0 .and. current_pop_size<mean_pop) then
k = max(delta, int(delta*randomnum(1) - 0.5))
current_pop_size = current_pop_size + k
end if
! START_MPI
call mpi_gather(current_pop_size,1,mpi_integer, &
pop_size_array,1,mpi_integer,0,mycomm,ierr)
! END_MPI
end if
! If there is tribal competition and all tribes but one go
! extinct, let the remaining tribe grow to the maximum size.
if(tribal_competition .and. .not.is_parallel) then
k = real(global_pop_size - current_pop_size)*0.1 + 0.9
current_pop_size = current_pop_size + k
end if
current_pop_size = min(current_pop_size, offspring_count)
!print *, '***',current_pop_size, offspring_count, pop_size
call second(tout_offspring)
sec(5) = sec(5) + tout_offspring - tin_offspring
! Impose selection based on fitness to reduce the population
! size to a value not to exceed the parameter pop_size.
call second(tin_selection)
call selection(dmutn, nmutn, fmutn, lb_mutn_count, &
linkage_block_fitness, fitness, pheno_fitness, &
work_fitness, sorted_score, initial_allele_effects, &
max_size, total_offspring, gen, lb_modulo, current_pop_size)
!call selection2(fitness, pheno_fitness, &
! work_fitness, sorted_score, initial_allele_effects, &
! max_size, total_offspring, gen, lb_modulo, current_pop_size)
call second(tout_selection)
time_selection = tout_selection - tin_selection
sec(6) = sec(6) + time_selection
! START_MPI
if(tribal_competition) then
call compute_tribal_fitness(dmutn, fmutn, pop_size_array, &
current_global_pop_size,gen)
end if
! END_MPI
! If the population size or the mean fitness has collapsed,
! print message and shutdown all processors.
if(post_sel_fitness < extinction_threshold) then
! If one tribe goes extinct, set trigger for shutdown
if(is_parallel) then
! Must shutdown parallel before calling diagnostics_history_plot
! otherwise will hang waiting for communications
if(num_tribes == 2) is_parallel = .false.
run_status = -1
else
do i=6,9,3
write(i,'(/"** SHUTDOWN DUE TO EXTINCTION **"/)')
end do
goto 20
end if
call diagnostics_history_plot(dmutn, nmutn, fmutn, &
lb_mutn_count, ica_count, gen, .true., &
current_global_pop_size)
do i=6,9,3
write(i,*)
write(i,*)'*** SHUTDOWN TRIBE',myid+1, &
' DUE TO EXTINCTION AT GEN:',gen
end do
end if
! Since clonal cells such as bacteria can multiply from a single cell
! we need to provide some means to allow a population size of one
! to recover from a bottleneck. This is done by increasing it to
! a value of 2 just before the mutational check, which will cause
! it to rebound correctly. If it is left as 1, it will never rebound
! correctly, but stay at a fixed value of 1 for the remainder of the
! simulation.
if(bottleneck_yes .and. bottleneck_pop_size == 1 .and. &
recombination_model == clonal .and. current_pop_size == 1) then
current_pop_size = 2
end if
! Mutational meltdown scenario
if((is_parallel .and. current_pop_size < &
extinction_threshold*pop_size) .or. &
current_pop_size <= 1) then
! If one tribe melts down, set trigger for shutdown
if(is_parallel) then
do i=6,9,3
write(i,*)
write(i,*)'*** SHUTDOWN TRIBE',myid+1, &
'DUE TO MUTATIONAL MELTDOWN AT GEN:',gen
write(i,*) myid+1,'Population size:', current_pop_size
end do
run_status = -(myid + 1)
else
do i=6,9,3
write(i,'(/"** SHUTDOWN DUE TO MUTATIONAL MELTDOWN **")')
write(i,*) 'Population size:', current_pop_size
end do
goto 20
end if
end if
! In case one tribe is set to run less generations than the other
!if(is_parallel .and. .not.homogenous_tribes .and. &
! gen==gen_0+num_generations) then
! do i=6,9,3
! write(i,*) 'TRIBE',myid+1,'IS SHUTTING DOWN. GEN:',gen
! end do
! run_status = -(myid + 1)
!end if
! START_MPI
! For the limiting case of two tribes, we must turn off the parallel
! switch if one of the tribes goes extinct. So, every generation
! communicate the status of each tribe to the other.
if(tribal_competition) then
if(num_tribes == 2) then
if(myid == 1) then
call mpi_send_int(run_status,0,msg_num,ierr)
msg_num = msg_num + 1
call mpi_recv_int(other_run_status,0,msg_num,ierr)
else
call mpi_recv_int(other_run_status,1,msg_num,ierr)
msg_num = msg_num + 1
call mpi_send_int(run_status,1,msg_num,ierr)
end if
msg_num = msg_num + 1
if(fission_tribes .and. fission_type==1) then
! Simple tribal fission - if one tribe goes extinct, split the
! surviving tribe into two and send half of its population
! to the tribe that went extinct
if (run_status < 0 .or. other_run_status < 0 ) then
if(myid==0) then
write(6,*) myid,'competing pop sizes:', pop_size_array
pop_size_winner = maxval(pop_size_array)
pop_size_loser = minval(pop_size_array)
id_winner = maxloc(pop_size_array,1) - 1
call mpi_send_int(pop_size_winner,1-myid,msg_num,ierr)
call mpi_send_int(pop_size_loser,1-myid,msg_num,ierr)
call mpi_send_int(id_winner,1-myid,msg_num,ierr)
else
call mpi_recv_int(pop_size_winner,1-myid,msg_num,ierr)
call mpi_recv_int(pop_size_loser,1-myid,msg_num,ierr)
call mpi_recv_int(id_winner,1-myid,msg_num,ierr)
end if
winner = .false.
if(run_status < 0) then ! dying tribe
run_status = 0 ! resurrect tribe - reset the status as ok
is_parallel = .true.
call mpi_recv_int(current_pop_size,1-myid,msg_num,ierr)
call mpi_recv_int(num_migrate,1-myid,msg_num,ierr)
else ! winning tribe... send half of its pop size to dying tribe
winner = .true.
write(6,*)'<font color=red>*** FISSION TRIBE ***</font>', myid
other_run_status = 0
num_migrate = (pop_size_winner - pop_size_loser)/2
current_pop_size = (pop_size_winner + pop_size_loser)/2
! if odd, round down half the time and round up half the time
if(mod(current_pop_size,2)==1 .and. randomnum(1).gt.0.5) then
num_migrate = num_migrate+1
current_pop_size = current_pop_size + 1
end if
call mpi_send_int(current_pop_size,1-myid,msg_num,ierr)
call mpi_send_int(num_migrate,1-myid,msg_num,ierr)
write(6,*) 'migrating half the tribe from',myid+1,' to ',2-myid
end if
! clear buffers of receiving tribes
if (.not.winner) then
dmutn = 0
fmutn = 0
nmutn = 0
lb_mutn_count = 0
linkage_block_fitness = 0
endif
! now migrate half the population
do k = 1, num_migrate
i = pop_size_winner-num_migrate + k
j = pop_size_loser + k
!if(myid.eq.0) write(*,*) 'migrating: ',i, 'to:',j
id_loser = 1 - id_winner
! here the myid == winner returns a logical indicating if this
! is sender or receiver
call migrate_individual(1-myid, i, j, dmutn, fmutn, nmutn, &
lb_mutn_count, linkage_block_fitness, myid == id_winner)
end do
end if
else ! .not.fission_tribes
! This is the alternative treatment to fission
! turn off parallel and let the one run to completion
if(other_run_status < 0) then
num_tribes = num_tribes - 1
is_parallel = .false.
end if
if(run_status < 0) goto 30
end if ! fission_tribes
else if (num_tribes > 2) then
! receive status from every process in group
! to check if one process has died
call mpi_allreduce(run_status,global_run_status,1, &
mpi_integer,mpi_sum,mycomm,ierr)
if(sum(global_run_status) < 0) then
ranks(1) = -sum(global_run_status)
call mpi_comm_group(mycomm,oldgroup,ierr)
call mpi_group_excl(oldgroup,1,ranks,newgroup,ierr)
call mpi_comm_create(mycomm,newgroup,mycomm,ierr)
if(run_status < 0) goto 30
call mpi_comm_size(mycomm,num_tribes,ierr)
end if
end if
end if
! END_MPI
! Write diagnostic information to output files.
current_pop_size = max(1, current_pop_size)
call second(tin_diagnostics)
if(mod(gen, 10) == 0 .or. (.not.bottleneck_yes .and. &
current_pop_size <= pop_size/20) .or. gen <= 10) then
print_flag = .true.
else
print_flag = .false.
end if
if(num_contrasting_alleles > 0) &
call diagnostics_contrasting_alleles(dmutn, nmutn, fmutn, &
work_fitness,initial_allele_effects, ica_count, max_size, .false.)
call diagnostics_history_plot(dmutn,nmutn,fmutn,lb_mutn_count, &
ica_count,gen,print_flag,current_global_pop_size)
if(gen <= 3 .or. mod(gen, 20) == 0) then
if (verbosity == 2) then
! Output fitness of each individual in population.
rewind(16)
write(16,'("# individual",2x,"fitness")')
do i=1,current_pop_size
write(16,*) i, fitness(i)
end do
call flush(16)
endif
end if
if(fitness_distrib_type == 1 .and. &
mod(gen, diagnostic_gens) == 0 .and. verbosity > 0) then
if(tracking_threshold /= 1.0) &
call diagnostics_mutn_bins_plot(dmutn, fmutn, accum, gen)
call diagnostics_near_neutrals_plot(dmutn, fmutn, &
linkage_block_fitness, lb_mutn_count, gen)
call diagnostics_selection(sorted_score,pheno_fitness, &
total_offspring,gen)
end if
call second(tout_diagnostics)
sec(7) = sec(7) + tout_diagnostics - tin_diagnostics
call second(tin_diagnostics)
! shutdown if fixation reached
if(polygenic_beneficials.and.polygenic_fixed.and.poly_stop_gen==gen) then
goto 20
endif
if (polygenic_beneficials) then
if (percent_pop_poly >= 99) then
print*,'POLYGENICS: SHUTTING DOWN BECAUSE 99% OF POPULATION HAS ALLELE'
goto 20 ! shutdown
endif
endif
if (plot_allele_gens > 0) then
if(mod(gen, plot_allele_gens)==0 .and. gen /= num_generations) &
call diagnostics_polymorphisms_plot(dmutn, nmutn, fmutn, &
work_fitness, max_size, gen)
endif
call second(tout_diagnostics)
sec(8) = sec(8) + tout_diagnostics - tin_diagnostics
call second(tout_gen)
tgen = tout_gen - tin_gen
if (verbosity == 2) then
write(22,'(i12,f17.7,2f19.7)') gen, tgen, time_offspring, &
time_selection
call flush(22)
endif
!START_MPI
if(is_parallel) then
call mpi_ravg(tgen,par_tgen,1)
call mpi_ravg(time_offspring,par_time_offspring,3)
call mpi_ravg(time_selection,par_time_selection,1)
if (myid==0) then
if (verbosity == 2) then
write(23,'(i12,f17.7,2f19.7)') gen, par_tgen, &
par_time_offspring, par_time_selection
end if
if (myid==0.and.(mod(gen,10)==0.or.gen<4)) then
write(*,'("iteration time: ",i6," milliseconds")') &
int(1000.*tgen)
end if
call flush(23)
end if
end if
!END_MPI
! Monitor state file for shutdown flag.
if (run_status >= 0) then
npath = index(data_file_path,' ') - 1
open(10, file=data_file_path(1:npath)//case_id//'.st8', &
status='unknown')
read(10,*) run_status
close(10)
! Premature shutdown
shutdown_gen = gen
if (run_status == 1) then
write(6,*) 'STATE: WRITING RESTART FILE & EXITING RUN'
write_dump = .true.
restart_dump_number = 8
dump_number = restart_dump_number
goto 20
end if
end if
! Write PPM data.
! do i=1,pop_size
! if (fitness(i) > 1) then
! red = 255
! green = 0
! blue = 0
! else
! red = int(fitness(i)*255)
! if (red < 0) red = 0
! green = red
! blue = red
! end if
! write(15,'(i4,$)') red,green,blue
! end do
! write(15,*)
! For dynamic population sizes compute new pop_size
if(pop_growth_model > 0) then
if(pop_growth_model == 1) then ! exponential growth
pop_size = ceiling(pop_growth_rate*pop_size)
current_pop_size = pop_size
else if (pop_growth_model == 2) then ! carrying capacity model
pop_size = ceiling(pop_size*(1. + pop_growth_rate* &
(1. - pop_size/carrying_capacity)))
current_pop_size = pop_size
else if (pop_growth_model == 3) then ! Founder effects
if (fission_tribes .and. gen < fission_threshold) then
pop_ceiling = carrying_capacity*num_tribes
else
pop_ceiling = carrying_capacity
endif
if (gen < bottleneck_generation .and. pop_size < pop_ceiling) then
pop_size = min(ceiling(gr1*pop_size), pop_ceiling)
reproductive_rate = gr1
else if (gen == bottleneck_generation) then
pop_size = bottleneck_pop_size
else if (gen > bottleneck_generation .and. pop_size < pop_ceiling) then
if (special_feature_code == 1500 .and. gen == 15) gr2 = 1.5
pop_size = min(ceiling(gr2*pop_size), pop_ceiling)
reproductive_rate = gr2
else
pop_size = pop_ceiling
reproductive_rate = reproductive_rate_input
end if
current_pop_size = pop_size
! START_MPI
! fission doubling
if (fission_tribes .and. fission_type == 2) then
if (myid == 0) &
print '(a,i5,x,a,i5,x,a,i5,x,a,i5)', 'gen:', gen, 'pop_size/tribe:', current_pop_size, &
'#demes:', num_demes, 'total_pop_size:', current_pop_size*num_demes
if (gen > bottleneck_generation .and. &
pop_size > fission_threshold .and. &
num_demes < num_tribes .and. gen < 50) then
if (myid == 0) print *, ">>> FISSION DOUBLING EVENT <<<"
! now migrate half the population take individuals between
! current_pop_size/2 and current_pop_size and use them to create
! a new population
if (myid < num_demes*2) tribe_state = LIVE
current_pop_size = pop_size/2
if (tribe_state == LIVE) then
other = mod(myid + num_demes, 2*num_demes)
! clear buffers of receiving tribes
if (myid > other) then
dmutn = 0
fmutn = 0
nmutn = 0
lb_mutn_count = 0
linkage_block_fitness = 0
endif
do k = 1, current_pop_size
i = k + current_pop_size
j = k
! we define sender if myid < other, and receiver otherwise
call migrate_individual(other, i, j, dmutn, fmutn, nmutn, &
lb_mutn_count, linkage_block_fitness, myid < other)
end do
end if
fission_count = fission_count + 1
pop_size = current_pop_size
num_demes = num_demes*2
active_demes(1) = num_demes
do i = 1, num_demes
active_demes(i) = i