-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
6196 lines (5333 loc) · 188 KB
/
main.c
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
/***************************************************************************
* Copyright (C) 2009 by Andreas H.W. Kuepper *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
/***************************************************************************
* Compile using the command: cc -lm -o mcluster mcluster.c *
* or use the Makefile, i.e. type: make mcluster or make mcluster_sse *
***************************************************************************/
/***************************************************************************
* Main array with stellar parameters: *
* star[][0] = mass(t = epoch) *
* star[][1-3] = {x, y, z} *
* star[][4-6] = {vx, vy, vz} *
* star[][7] = mass(t = 0) *
* star[][8] = kstar, stellar type in (SSE/BSE) *
* star[][9] = epoch1, age within evolutionary phase (SSE/BSE) *
* star[][10] = ospin, spin of star (SSE/BSE) *
* star[][11] = rstar, radius of star (SSE/BSE) *
* star[][12] = lstar, luminosity (SSE/BSE) *
* star[][13] = epochstar, age of star (SSE/BSE) *
* star[][14] = Zstar, metallicity of star *
***************************************************************************/
/***************************************************************************
* Implemented 20th June 2018 by A. Leveque ([email protected]) *
* to include scaling in N-body units assuming spherical symmetry, new *
* eigenevolutoin for initial binary proprieties (D. Belloni et al., 2017, *
* MNRAS, 471, 2812) and multiple stellar population *
* *
* Multiple stellar population prosiger: generation of different stellar *
* populations separately; solving Jeans equation to obtain velocities for *
* dynamical equilibrium (acknowledge to J. Hong); COM corrections and *
* scaling in N-body units *
***************************************************************************/
/***************************************************************************
* Modified 28th October 2022 by A. Kamlah ([email protected]) *
* details will follow here ... *
* See also github: https://github.com/AlbrechtKamlah/mcluster *
***************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include<string.h>
#include<sys/stat.h>
#include<getopt.h>
#include<assert.h>
#include <stdbool.h>
#ifdef GPU
#include <cuda.h>
#include <cuda_runtime.h>
#endif
#include "main.h"
//use OpenMP if not specified otherwise
#ifndef NOOMP
#include<omp.h>
#endif
extern void input_();
int main (int argv, char **argc) {
/*******************
* Input variables *
*******************/
//Basic physical parameters
input_();
int i;
int N[10], profile[10], mfunc[10], pairing[10], adis[10], OBperiods[10], eigen[10];
double fbin[10], conc_pop[10], W0[10], S[10], D[10], a[10], Rmax[10], single_mass[10], mlow[10], mup[10], alpha_L3[10], beta_L3[10], mu_L3[10], msort[10], amin[10], amax[10], epoch[10], Z[10];
double fbin_reference[10];
for (i=0;i<10;i++) {
N[i] = mclusterarri_.npop[i];
profile[i] = mclusterarri_.initmodel[i];
mfunc[i] = mclusterarri_.imfg[i];
pairing[i] = mclusterarri_.pairing[i];
adis[i] = mclusterarri_.adis[i];
OBperiods[i] = 0;
eigen[i] = mclusterarri_.eigen[i];
fbin[i] = mclusterarrd_.fracb[i];
fbin_reference[i] = mclusterarrd_.fracb_reference[i];
W0[i] = mclusterarrd_.w0[i];
conc_pop[i] = mclusterarrd_.conc_pop[i];
S[i] = mclusterarrd_.Seg[i];
D[i] = mclusterarrd_.fractal[i];
single_mass[i] = mclusterarrd_.equalmass[i];
mlow[i] = mclusterarrd_.mlow[i];
mup[i] = mclusterarrd_.mup[i];
alpha_L3[i] = mclusterarrd_.alpha_L3[i];
beta_L3[i] = mclusterarrd_.beta_L3[i];
mu_L3[i] = mclusterarrd_.mu_L3[i];
msort[i] = 5.0;
amin[i] = (mclusterarrd_.amin[i]); //input in rsun, the code need in pc
amax[i] = (mclusterarrd_.amax[i]); //input in rsun, the code need in pc
epoch[i] = mclusterarrd_.epoch_pop[i];
Z[i] = mclusterarrd_.zini_pop[i];
}
double Mcl = 0.0; //Total mass of the cluster, only used when N is set to 0, necessary for usage of maximum stellar mass relation of Weidner & Kroupa (2006)
double Qtot = mclusterd_.qvir; //Initial virial ratio; =0.5 virial equilibrium, <0.5 collapsing, >0.5 expanding
double tcrit = 100.0; //Simulation time [N-body units (Myr in Nbody6 custom)]
int tf = mclusteri_.tf; //Tidal field: =0 no tidal field, =1 point-mass galaxy
double rbar = mclusterd_.rbar; //Tidal radius for point mass tidal field
double rh_mcl = mclusterd_.rh_mcl; //Half mass radius radius for the whole system
double rh_1pop = 0.0; //Half mass radius radius for the first population in Nbody units
//Mass function parameters
int p;
double **alpha;
alpha = (double **)calloc(10,sizeof(double *));
for (p=0;p<10;p++){
alpha[p] = (double *)calloc(MAX_AN,sizeof(double));
}
multiplearraytodouble(mclusterchar_.alphaimfchar, alpha);
double **mlim;
mlim = (double **)calloc(10, sizeof(double *));
for (p=0;p<10;p++){
mlim[p] = (double *)calloc(MAX_MN, sizeof(double));
if (mlim[p] == NULL) {
printf("\nMemory allocation failed!\n");
return 0;
}
// memset(mlim[p], 0, MAX_MN*sizeof(double));
}
multiplearraytodouble(mclusterchar_.mlimimfchar, mlim);
int weidner[10] = {0, 0}; //Usage of Weidner & Kroupa (2006) relation for most massive star; =0 off, =1 on
int mloss = 0; //Stellar evolution; 0 = off, 3 = Eggleton, Tout & Hurley [KZ19]
int remnant = 1; //Use random kick velocity and present-day escape velocity to determine retention of compact remnants & evolved binary components (only for SSE/BSE version); =0 off, =1 on
double FeH = -1.41; //Metallicity [Fe/H], only used when Z is set to 0
int prantzos = 0; //Usage of Prantzos (2007) relation for the life-times of stars. Set upper mass limit to Lifetime(mup) >= epoch
//#ifdef SSE
int BSE = mclusteri_.BSE; //Apply binary star evolution using BSE (Hurley, Tout & Pols 2002) =0 off, =1 on [use either eigenevolution or BSE; BSE recommended when using SSE]
//#endif
//Gas parameters (only used for Nbody6 input)
double extmass = 0.0; //external Plummer (gas) sphere mass [Msun]
double extrad = 0.0; //external Plummer (gas) sphere scale factor [pc]
double extdecay = 0.0; //decay time for gas expulsion (set 0 for no decay) [Myr]
double extstart = 0.0; //delay time for start of gas expulsion [Myr]
//Code parameters
unsigned int seed = mclusteri_.seedmc; //Number seed for random number generator; =0 for randomization by local time
double dtadj = 1.0; //DTADJ [N-body units (Myr in Nbody6 custom)], energy-check time step
double dtout = 1.0; //DELTAT [N-body units (Myr in Nbody6 custom)], output interval, must be multiple of DTADJ
double dtplot = 1.0; //DTPLOT [N-body units (Myr in Nbody6 custom)], output of HRdiagnostics, should be multiple of DTOUT, set to zero if output not desired
int gpu = 0; //Use of GPU, 0= off, 1= on
int regupdate = 0; //Update of regularization parameters during computation; 0 = off, 0 > on
int etaupdate = 0; //Update of ETAI & ETAR during computation; 0 = off, 0 > on
int esc = 2; //Removal of escapers; 0 = no removal, 1 = regular removal at 2*R_tide; 2 = removal and output in ESC
int units = 0; //Units of McLuster output; 0= Nbody-Units, 1= astrophysical units
int pot_energy_MOCCA = mclusteri_.potential_energy;
int outputfor = mclusteri_.outputf;
//McLuster internal parameters
int symmetry = 1; //Force spherical symmetry for fractal clusters; =0 off, =1 on (recommended)
int check = mclusteri_.check_en; //Make energy check at end of McLuster; =0 off, =1 on
double Zsun = 0.02; //Solar metallicity
int NMAX = 20000000; //Maximum number of stars & orbits allowed in McLuster
int NNBMAX_NBODY6 = 500; //Maximum number of neighbours allowed in NBODY6
double upper_IMF_limit = 150.0; //Maximum stellar mass allowed in McLuster [Msun]
int an[10] = {0,0,0,0,0,0,0,0,0,0}; //Counter for number of alpha slopes for mfunc = 2
int mn[10] = {0,0,0,0,0,0,0,0,0,0}; //Counter for number of mass limits for mfunc = 1, 2 & 4
//SSE internal parameters (see Hurley, Pols & Tout 2000)
value1_.neta = mclusteri2_.neta; //0.477Reimers mass-loss coefficent (neta*4x10^-13; 0.5 normally)
value1_.bwind = mclusteri2_.bwind; //0.0 Binary enhanced mass loss parameter (inactive for single)
value1_.hewind = mclusteri2_.hewind; //1.0Helium star mass loss factor (1.0 normally)
value1_.mxns = mclusteri2_.mxns; //Maximum NS mass (1.8, nsflag=0; 3.0, nsflag=1)
value1_.FctorCl = mclusteri2_.FctorCl; //Mass transfer 1.0
points_.pts1 = mclusteri2_.pts1; //Time-step parameter in evolution phase: MS (0.05)
points_.pts2 = mclusteri2_.pts2; //Time-step parameter in evolution phase: GB, CHeB, AGB, HeGB (0.01)
points_.pts3 = mclusteri2_.pts3; //Time-step parameter in evolution phase: HG, HeMS (0.02)
value4_.disp = mclusteri2_.disp; //Kick velocities 265 is a new value
// 190 is a old
value4_.bhflag = mclusteri1_.bhflag; //bhflag > 0 allows velocity kick at BH formation
value6_.bhspin = mclusteri1_.bhspin; //bhspin -- 0: Fuller model, 1: Geneva model, 2: MESA model
flags1_.psflag = mclusteri1_.psflag; //P(I)SNe PP(I)SNe schemes. 2 is a new value; 0 is a old
flags1_.ecflag = mclusteri1_.ecflag; //ECSNe 1 is a new value
// 0 is a old value
flags2_.mdflag = mclusteri1_.mdflag; //Wind mass loss prescriptions 3 is a new prescription....
// 1 old Hurley value...
flags3_.kmech = mclusteri1_.kmech; //Kick mechanism
fback_.fbfac = 1.0; //Fallback parameter
fback_.fbtot = 0.0; //Fallback parameter
fback_.ecs = 0.0; //ECS flag
fback_.mco = 0.0; //Compact mass
//BSE internal parameters (see Hurley, Pols & Tout 2002)
flags_.ceflag = mclusteri1_.ceflag; //ceflag > 0 activates spin-energy correction in common-envelope (0) #defunct#
flags_.tflag = mclusteri1_.tflag; //tflag > 0 activates tidal circularisation (1)
flags_.ifflag = mclusteri1_.ifflag; //ifflag > 0 uses WD IFMR of HPE, 1995, MNRAS, 272, 800 (0)
flags_.nsflag = mclusteri1_.nsflag; //nsflag > 0 takes NS/BH mass from Belczynski et al. 2002, ApJ, 572, 407 (1)
flags_.wdflag = mclusteri1_.wdflag; //wdflag > 0 uses modified-Mestel cooling for WDs (0)
value5_.beta = mclusteri2_.beta; //beta is wind velocity factor: proportional to vwind**2 (1/8)
value5_.xi = mclusteri2_.xi; //xi is the wind accretion efficiency factor (1.0)
value5_.acc2 = mclusteri2_.acc2; //acc2 is the Bondi-Hoyle wind accretion factor (3/2)
value5_.epsnov = mclusteri2_.epsnov; //epsnov is the fraction of accreted matter retained in nova eruption (0.001)
value5_.eddfac = mclusteri2_.eddfac; //eddfac is Eddington limit factor for mass transfer (1.0)
value5_.gamma = mclusteri2_.gamma; //gamma is the angular momentum factor for mass lost during Roche (-1.0)
value2_.alpha1 = mclusteri2_.alpha1; //alpha1 is the common-envelope efficiency parameter (1.0)
value2_.lambda = mclusteri2_.lambda; //lambda is the binding energy factor for common envelope evolution (0.5)
/*********
* Start *
*********/
printf("\n-----START---- \n");
#ifdef NOOMP
clock_t t1, t2;
t1 = clock(); //start stop-watch
#else
double t1, t2;
#pragma omp parallel
{
if (omp_get_thread_num() == 0)
if (omp_get_num_threads() > 1) printf("\n\nUsing OpenMP with %d threads\n", omp_get_num_threads());
t1 = omp_get_wtime(); //start stop-watch
}
#endif
if(seed)
{
srand48(seed); //initialize random number generator by seed
}
else
{
seed = (unsigned) time(NULL); //initialize random number generator by local time
seed %= 100000;
srand48(seed);
}
printf("\n\nRandom seed = %i\n\n\n", seed);
if (seed) value3_.idum = seed; //idum is the random number seed used in the kick routine.
else value3_.idum = 10000000.0*drand48();
int j, k;
double M[10]; //Total mass [M_sun]
double mmean[10]; //Mean stellar mass [M_sun]
int NNBMAX; //Maximum neighbour number (Nbody6 only)
double RS0; //Initial radius of neighbour sphere [pc], Nbody6 only
double rtide; //Tidal radius [pc]
double omega; //Angular velocity of cluster around the galaxy
double rvir; //Virial radius [pc]
// double rvir[10]; //Virial radius [pc]
double cmr[7]; //For CoM correction
// double rking[10], rplummer[10]; //King-, Plummer radius
// double rtidkg[10];
double MMAX; //most massive star
double tscale; //time-scale factor
double vscale[10];
double ekin = 0.0; //kinetic energy
double epot = 0.0; //potential energy
double sigma = 0.0; //velocity dispersion
int bin; //KZ(22) parameter (binaries yes/no)
int sse; //(evolved stellar population yes/no)
double submass[MAX_AN], subcount[MAX_AN], norm[MAX_AN], N_tmp, M_tmp; //mass function parameters for mfunc = 2
double Mtotal = 0.0;
/***********************
* Generate star array *
***********************/
// number of stellar population
int numberofpop = 0;
int nbin[10];
for(i=0; i<10; i++){
if(N[i]) numberofpop++;
}
if (!areArrayEqual(fbin,fbin_reference)){
printf(" INFO Determining number of stars for each population from the reference binary fraction\n");
determine_new_number_of_stars_from_reference_model(N,fbin,fbin_reference,mfunc,mlow,mup,numberofpop);
}
// number of stars and binaries for each stellar population
int Ntot = 0, Nbintot = 0;
int Nsub = 0, Nbinsub = 0;
for (i=0;i<numberofpop;i++) {
nbin[i] = N[i]*fbin[i];
N[i] = N[i] + nbin[i];
}
for (i=0;i<numberofpop;i++){
Ntot += N[i];
Nbintot += nbin[i];
}
// double rplum_t;
// if(numberofpop == 1){
// rplum_t = rplum[0];
// } else {
// rplum_t = rplum[numberofpop-1];
// }
int columns = 15;
double **star;
star = (double **)calloc(NMAX,sizeof(double *));
for (j=0;j<NMAX;j++){
star[j] = (double *)calloc(columns,sizeof(double));
if (star[j] == NULL) {
printf("\nMemory allocation failed!\n");
return 0;
}
}
double **cmb;
cmb = (double **)calloc(Nbintot,sizeof(double *));
for (j=0;j<Nbintot;j++){
cmb[j] = (double *)calloc(6,sizeof(double));
if (cmb[j] == NULL) {
printf("\nMemory allocation failed!\n");
return 0;
}
}
//eccentricities and semi-major axis storing arrays
double *eccbinaries;
eccbinaries = (double *)calloc(Nbintot,sizeof(double));
double *abinaries;
abinaries = (double *)calloc(Nbintot,sizeof(double));
double **mbin; //component mass & stellar evol parameter array
mbin = (double **)calloc(Nbintot, sizeof(double *));
for (j=0;j<Nbintot;j++){
mbin[j] = (double *)calloc(22, sizeof(double));
if (mbin[j] == NULL) {
printf("\nMemory allocation failed!\n");
return 0;
}
}
/*******************
* Generate masses *
*******************/
printf("\n\n-----GENERATE MASSES----- \n");
//START CICLE FOR GENERATE MASS FOR MULTIPLE POPULATIONS OR FOR SINGLE POPULATION
for (i=0;i<numberofpop;i++){
printf("\nCreating mass for stellar population number %i\n",i+1);
// if (profile[i] == 4) rplum[i] = a[i]; //set half-mass radius temporarily to scale radius for computation of escape velocity
/*******************************************
* Evaluate Z from [Fe/H] if Z is set to 0 *
*******************************************/
if (!Z[i]) {
Z[i] = pow(10.0, 0.977*FeH)*Zsun; //Bertelli, Bressan, Chiosi, Fagotto, Nasi, 1994, A&AS, 106, 275
printf("\nUsing Bertelli et al. (1994) relation to convert FeH = %.3f into Z = %.3f\n", FeH, Z[i]);
}
//used to change the correct star array; star_array will be constructed as: Nbinaries_1_gen, Nsingle_1_gen, Nbinaries_2_gen, Nsingle_2_gen, ..
if (i != 0){
Nsub += N[i-1];
Nbinsub += nbin[i-1];
}
/**********************************
* Calculate maximum stellar mass *
**********************************/
// if (mfunc[i] == 3) weidner[i] = 1; //always use Weidner relation when using optimal sampling!
// if (!N[i] && weidner[i] && mfunc[i]) {
// mup[i] = upper_IMF_limit;
// printf("\nUsing maximum stellar mass-cluster mass relation for upper stellar mass limit\n");
// printf("\n(Weidner & Kroupa 2006, Pflamm-Altenburg & Kroupa 2007)\n");
//analytic fit to the observational data from Pflamm-Altenburg & Kroupa (2007), implementation by M. Kruckow
// MMAX = pow(10,2.56*log10(Mcl)*pow(pow(3.82,9.17)+pow(log10(Mcl),9.17),-1/9.17)-0.38);
/*
//three-part power-law fit to the observational data
if (Mcl < 1000.0) {
MMAX = (log10(Mcl)*0.540563175) - 0.14120167;
MMAX = pow(10.0,MMAX);
} else if (Mcl < 3300.0) {
MMAX = (log10(Mcl)*0.19186051) + 0.9058611;
MMAX = pow(10.0,MMAX);
} else {
MMAX = (log10(Mcl)*0.360268003) + 0.313342031;
MMAX = pow(10.0,MMAX);
}*/
// } else {
MMAX = mup[i];
// }
if (mfunc[i] && epoch[i] && prantzos) {
printf("\nUsing Prantzos (2007) relation to reduce upper mass limit to Lifetime(mup) > epoch\n");
while (Lifetime(MMAX) < sqrt(pow(epoch[i],2))) {
MMAX -= 0.01;
}
}
//for now, all Z and epochs are the same, i.e. a single stellar population
if (mfunc[i] == 1) {
printf(" Maximum stellar mass set to: %.2f\n", MMAX);
generate_m1(&N[i], star, mlow[i], mup[i], &M[i], &mmean[i], MMAX, Mcl, epoch[i], Z[i], rh_mcl, remnant, Nsub);
} else if (mfunc[i] == 2) {
if (mn[i]) {
for (j = mn[i]+1; j < MAX_MN; j++) mlim[i][j] = 0.0;
} else {
for (j=0; j<MAX_MN; j++) {
if (mlim[i][j]) mn[i]++;
}
}
if (an[i]) {
for (j = an[i]+1; j < MAX_AN; j++) alpha[i][j] = 0.0;
} else {
for (j=0; j<MAX_AN; j++) {
if (alpha[i][j]) an[i]++;
}
}
if (an[i] >= mn[i]) an[i] = mn[i] - 1;
mn[i] = an[i] + 1;
if (!mn[i]){
printf("\nError: at least one mass limit has to be specified\n");
return 1;
} else if (mn[i] == 1) {
single_mass[i] = mlim[i][0];
printf("\nSetting stellar masses to %g solar mass\n",single_mass[i]);
// if (!N[i]) N[i] = Mcl/single_mass[i];
for (j=0;j<N[i];j++) star[j+Nsub][0] = 1.0/N[i];
mmean[i] = single_mass[i];
M[i] = N[i]*mmean[i];
printf("\nM = %g\n", M[i]);
} else {
printf("\nMaximum stellar mass set to: %.2f\n",MMAX);
norm[an[i]-1] = 1.; //normalization factor of integral
N_tmp = subcount[an[i]-1] = subint(mlim[i][an[i]-1], mlim[i][an[i]], alpha[i][an[i]-1] + 1.); //integrated number of stars in interval [mlim[an-1]:mlim[an]]
M_tmp = submass[an[i]-1] = subint(mlim[i][an[i]-1], mlim[i][an[i]], alpha[i][an[i]-1] + 2.); //integrated mass of stars in interval [mlim[an-1]:mlim[an]]
for (j = an[i] - 2; j >= 0; j--) {
norm[j] = norm[j+1] * pow(mlim[i][j+1], alpha[i][j+1] - alpha[i][j]);
subcount[j] = norm[j] * subint(mlim[i][j], mlim[i][j+1], alpha[i][j] + 1.);
N_tmp += subcount[j];
submass[j] = norm[j] * subint(mlim[i][j], mlim[i][j+1], alpha[i][j] + 2.);
M_tmp += submass[j];
}
generate_m2(an[i], mlim[i], alpha[i], Mcl, M_tmp, subcount, &N[i], &mmean[i], &M[i], star, MMAX, epoch[i], Z[i], abs(rh_mcl), remnant, Nsub);
}
// } else if (mfunc[i] == 3) {
// printf(" Maximum stellar mass set to: %.2f\n",MMAX);
// generate_m3(&N[i], star, mlow[i], mup[i], &M[i], &mmean[i], MMAX, Mcl, Nsub);
// randomize(star, N[i], Nsub);
} else if (mfunc[i] == 3) {
printf(" Maximum stellar mass set to: %.2f\n", MMAX);
printf(" Using L3 IMF (Maschberger 2012)\n");
generate_m4(&N[i], star, alpha_L3[i], beta_L3[i], mu_L3[i], mlow[i], mup[i], &M[i], &mmean[i], MMAX, Mcl, epoch[i], Z[i], abs(rh_mcl), remnant, Nsub);
} else if (mfunc[i] == 0) {
printf(" Setting stellar masses to %.1f solar mass\n", single_mass[i]);
// if (!N[i]) N[i] = Mcl/single_mass[i];
for (j=0;j<N[i];j++) {
star[j+Nsub][0] = single_mass[i];
star[j+Nsub][7] = single_mass[i];
star[j+Nsub][8] = 0;
star[j+Nsub][9] = 0.0;
star[j+Nsub][10] = 0.0;
star[j+Nsub][11] = 0.0;
star[j+Nsub][12] = 0.0;
star[j+Nsub][13] = 0.0;
star[j+Nsub][14] = 0.0;
}
mmean[i] = single_mass[i];
M[i] = N[i]*mmean[i];
printf("\nM = %g\n", M[i]);
mloss = 0;
}
Mtotal += M[i];
}//END CICLE FOR GENERATE MASS FOR MULTIPLE POPULATIONS OR FOR SINGLE POPULATION
double ***rho_dens;
rho_dens = (double ***)calloc(numberofpop, sizeof(double **));
for (i=0;i<numberofpop;i++){
rho_dens[i] = (double **)calloc(N[i], sizeof(double *));
for (j=0;j<N[i];j++){
rho_dens[i][j] = (double *)calloc(2, sizeof(double));
if (rho_dens[i][j] == NULL) {
printf("\nMemory allocation failed!\n");
return 0;
}
}
}
for(j=0;j<numberofpop;j++) printf("\n Mass %i generation: \t%.3f\n", j+1, M[j]);
printf("\n Total mass %.3f\n",Mtotal);
if(rbar < 0.0){
double galactocentric_distance = abs(rbar);
printf("\n Tidal radius determined from the galactocentric distance\n");
printf(" Input galactocentric distance: %.2f kpc\n",galactocentric_distance);
rbar = 0.31 * pow(galactocentric_distance,2.0/3.0) * pow(Mtotal,1.0/3.0);
printf(" Initial tidal radius: %.2f pc\n",rbar);
}
/*************************************
* Generate positions and velocities *
*************************************/
printf("\n\n-----GENERATE POSITIONS & VELOCITIES----- \n");
//calculate half-mass radius according to Marks & Kroupa 2012 if Rh is set to -2
// if ((rplum[i] == -1) && (M[i])) {
// rplum[i] = 0.1*pow(M[i],0.13); //Marks & Kroupa (2012), implementation by M. Kruckow
// printf("\nUsing Marks & Kroupa (2012) relation to derive half-mass radius from cluster mass: %g (pc)\n", Rh[i]);
// }
//evaluate approximate tidal radius assuming circular orbit
if (tf == 1) {
printf(" Point mass tidal potential\n");
// rtide = rbar;
} //else if (!tf) {
// rtide = 1.0E6;
// }
rtide = rbar;
printf(" Approximate tidal radius: %g (pc)\n", rtide);
//START CICLE FOR GENERATE POSITION FOR MULTIPLE POPULATIONS OR FOR SINGLE POPULATION
for (i=0;i<numberofpop;i++){
printf("\nGenerating positions and velocities for stellar population number %i\n",i+1);
//used to change the correct star array; star_array will be constructed as: Nbinaries_1_gen, Nsingle_1_gen, Nbinaries_2_gen, Nsingle_2_gen, ..
if (i == 0){
Nsub = 0;
Nbinsub = 0;
} else {
Nsub = Nsub + N[i-1]+nbin[i-1];
Nbinsub += nbin[i-1];
}
//change pairing, adis and OBperiods for eigenevolution
if( (adis[i] == 3) && (eigen[i] == 0) ) {
OBperiods[i] = 1;
}
//standard distribution for m<5MSun; Sana et al (2012) for m>5MSun
if(adis[i] == 6) {
OBperiods[i] = 1;
pairing[i] = 3;
msort[i] = mlow[i]; // to have equal mass distribution also for m<5MSun
eigen[i] = 0; // no eigenevolution has to be possible
}
if(eigen[i] == 1){
pairing[i] = 1;
adis[i] = 2;
}
if(eigen[i] == 2){
pairing[i] = 3;
adis[i] = 3;
OBperiods[i] = 1;
}
//set all stars to the same metallicity and age for now
double epochstar, zstar;
epochstar = epoch[i]; //age compared to the oldest stars in the cluster [Myr]
zstar = Z[i];
for (j=0;j<N[i];j++) {
star[j+Nsub][13] = epochstar;
star[j+Nsub][14] = zstar;
}
//Pair binary masses and convert to centre-of-mass particles
int Nstars;
Nstars = N[i];
if (nbin[i]) {
printf("\nPreparing binary components.\n");
//Specify component pairing
if (pairing[i]) {
if (pairing[i] == 1 && adis[i] != 6) printf(" Applying ordered pairing for stars with masses > %.1f Msun.\n",msort[i]);
else if (pairing[i] == 2 && adis[i] != 6) printf(" Applying random pairing for stars with masses > %.1f Msun.\n",msort[i]);
else if (pairing[i] == 3 && adis[i] != 6) printf(" Applying uniform mass ratio distribution for stars with masses > %.1f Msun.\n",msort[i]);
if (adis[i] == 6) printf(" Applying uniform mass ratio distribution for all stars.\n");
order(star, N[i], M[i], msort[i], pairing[i], Nsub, fbin[i]);
} else {
randomize(star, N[i], Nsub);
}
N[i] -= nbin[i];
//printf("\nNumber of pop %i Nsub %i\n", i+1,Nsub);
for (j=0;j<nbin[i];j++) {
mbin[j+Nbinsub][0] = star[2*j+Nsub][0]+star[2*j+Nsub+1][0]; //system mass
mbin[j+Nbinsub][1] = star[2*j+Nsub][0]; //primary mass
mbin[j+Nbinsub][2] = star[2*j+Nsub+1][0]; //secondary mass
mbin[j+Nbinsub][3] = star[2*j+Nsub][7]; //primary m0
mbin[j+Nbinsub][4] = star[2*j+Nsub][8]; //primary kw
mbin[j+Nbinsub][5] = star[2*j+Nsub][9]; //primary epoch1
mbin[j+Nbinsub][6] = star[2*j+Nsub][10]; //primary spin
mbin[j+Nbinsub][7] = star[2*j+Nsub][11]; //primary r
mbin[j+Nbinsub][8] = star[2*j+Nsub][12]; //primary lum
mbin[j+Nbinsub][9] = star[2*j+Nsub+1][7]; //secondary m0
mbin[j+Nbinsub][10] = star[2*j+Nsub+1][8]; //secondary kw
mbin[j+Nbinsub][11] = star[2*j+Nsub+1][9]; //secondary epoch1
mbin[j+Nbinsub][12] = star[2*j+Nsub+1][10]; //secondary spin
mbin[j+Nbinsub][13] = star[2*j+Nsub+1][11]; //secondary r
mbin[j+Nbinsub][14] = star[2*j+Nsub+1][12]; //secondary lum
mbin[j+Nbinsub][15] = 1000+(j+Nsub); //identifier
mbin[j+Nbinsub][16] = star[2*j+Nsub][13]; //primary epochstar
mbin[j+Nbinsub][17] = star[2*j+Nsub+1][13]; //secondary epochstar
mbin[j+Nbinsub][18] = star[2*j+Nsub][14]; //primary zstar
mbin[j+Nbinsub][19] = star[2*j+Nsub+1][14]; //secondary zstar
star[2*j+Nsub][0] += star[2*j+Nsub+1][0]; //system mass
star[2*j+Nsub+1][0] = 0.0;
star[2*j+Nsub][7] = 1000+(j+Nsub); //identifier
star[2*j+Nsub+1][7] = 0.0; //identifier
star[2*j+Nsub][12] += star[2*j+Nsub+1][12]; //system luminosity
star[2*j+Nsub+1][12] = 0.0;
}
order(star, Nstars, M[i], 0.0, 0, Nsub, fbin[i]);
randomize(star, N[i], Nsub);
}
//prepare mass segregation
double mlowest = MMAX; //search lowest mass star
double mhighest = 0; //search highest mass star
double mmeancom = 0.0;
for (j=0;j<N[i];j++) {
star[j+Nsub][0] /= M[i]; //scale masses to Nbody units
mmeancom += star[j+Nsub][0];
if (star[j+Nsub][0] < mlowest) mlowest = star[j+Nsub][0];
if (star[j+Nsub][0] > mhighest) mhighest = star[j+Nsub][0];
}
mmeancom /= N[i];
int Nseg = ceil(N[i]*mmeancom/mlowest); //number of necessary pos & vel pairs for Baumgardt et al. (2008) mass segregation routine
int Nunseg = N[i];
double *Mcum;
Mcum = (double *)calloc(N[i], sizeof(double));
int k;
if ((S[i]) && !(profile[i] == 3)) {//sort masses when mass segregation parameter > 0
printf("\nApplying mass segregation with S = %f\n",S[i]);
segregate(star, N[i], S[i], Nsub);
Mcum[0] = star[Nsub][0];
for (j=1;j<N[i];j++) {//calculate cumulative mass function Mcum
Mcum[j] = Mcum[j-1] + star[j+Nsub][0];
}
N[i] = Nseg;
}
double **rho_dens_single_pop;
rho_dens_single_pop = (double **)calloc(N[i], sizeof(double *));
for (j=0;j<N[i];j++){
rho_dens_single_pop[j] = (double *)calloc(2, sizeof(double));
if (rho_dens_single_pop[j] == NULL) {
printf("\nMemory allocation failed!\n");
return 0;
}
}
double cc;
if(i==0) cc=1.0;
else cc = conc_pop[i-1];
//generate scaled pos & vel, postpone scaling for Plummer and King in case of mass segregation
double rhking[10], rvirking[10], rking[10];
if (profile[i] == 2) {
printf("\nGenerating King model with parameters: N = %i\t W0 = %g\t D = %.2f\n",N[i], W0[i], D[i]);
generate_king(N[i], W0[i], star, &rvirking[i], &rhking[i], &rking[i], D[i], symmetry, Nsub, rho_dens_single_pop, cc, M[i], Mtotal);
} else if (profile[i] == 3) {
N[i] = Nunseg;
printf("\nGenerating segregated Subr model with parameters: N = %i\t S = %g\t\n",N[i], S[i]);
rvir = determine_rvir(rh_mcl, Mtotal, M[0], tf, rtide,cc);
//rvir[i] = Rh[i]/0.76857063065978; //(value provided by L. Subr)
generate_subr(N[i], S[i], star, rtide, rvir, Nsub);
// printf ("\nrvir = %.5f\t rtide = %.5f (pc)\n", rvir, rtide);
// } else if (profile[i] == 4) {
// if (gamma[i][1] == 0.0 && gamma[i][2] == 2.0) printf("\nGenerating EFF model with parameters: N = %i\t a = %.1f\t gamma = %.3f\t Rmax = %.1f\t D = %.2f\n",N[i], a[i], gamma[i][0], Rmax[i], D[i]);
// else printf("\nGenerating Nuker model with parameters: N = %i\t a = %.1f\t gamma (outer) = %.3f\t gamma (inner) = %.3f\t transition = %.3f\t Rmax = %.1f\t D = %.2f\n",N[i], a[i], gamma[i][0],gamma[i][1],gamma[i][2], Rmax[i], D[i]);
// double p[6];
// p[1] = 1.0; //rho0, will be scaled according to Rmax and Mtot
// p[2] = a[i];//scale radius
// p[3] = gamma[i][0];//outer power-law slope (>0.5)
// p[4] = gamma[i][1];//inner power-law slope (0.0 for EFF template)
// p[5] = gamma[i][2];//transition parameter (2.0 for EFF template)
// generate_profile(N[i], star, Rmax[i], M[i], p, &rplum_t[i], D[i], symmetry, Nsub);
// printf("\nRh = %.1f pc\n", Rh[i]);
} else if (profile[i] == 0) {
printf("\nGenerating fractal distribution with parameters: N = %i\t D = %.2f\n", N[i], D[i]);
if(D[i] > 0.0) {
fractalize(D[i], N[i], star, 0, symmetry, Nsub);
} else{
fractalize_spherical(-D[i], N[i], star, 0, symmetry, Nsub);
}
rvir = determine_rvir(rh_mcl, Mtotal, M[0], tf, rtide,cc);
} else if (profile[i] == 1) {
printf(" Generating Plummer model with parameters: N = %i\t D = %.2f\n", N[i], D[i]);
rvir = determine_rvir(rh_mcl, Mtotal, M[0], tf, rtide,cc);
generate_plummer(N[i], star, rtide, rvir, D[i], symmetry, Qtot, Nsub, rho_dens[i], cc, M[i], Mtotal);
}
//Apply Baumgardt et al. (2008) mass segregation
if (!(profile[i] == 3) && (S[i])) {
double **m_temp;
m_temp = (double **)calloc(Nseg, sizeof(double *));
for (j=0;j<Nseg;j++){
m_temp[j] = (double *)calloc(9, sizeof(double));
if (m_temp[j] == NULL) {
printf("\nMemory allocation failed!\n");
return 0;
}
}
for (k=0;k<Nseg;k++) {//temporarily store the masses & stellar evol parameters
m_temp[k][0] = star[k+Nsub][0];
m_temp[k][1] = star[k+Nsub][7];
m_temp[k][2] = star[k+Nsub][8];
m_temp[k][3] = star[k+Nsub][9];
m_temp[k][4] = star[k+Nsub][10];
m_temp[k][5] = star[k+Nsub][11];
m_temp[k][6] = star[k+Nsub][12];
m_temp[k][7] = star[k+Nsub][13];
m_temp[k][8] = star[k+Nsub][14];
}
printf("\nOrdering orbits by energy.\n");
energy_order(star, N[i], Nstars, Nsub, rho_dens_single_pop);
int nlow, nhigh, nrandom;
for (k=0;k<Nunseg;k++) {
nhigh = Nseg*Mcum[k];
if (k) {
nlow = Nseg*Mcum[k-1];
} else {
nlow = 0;
}
nrandom = (nhigh-nlow)*drand48()+nlow;
star[k+Nsub][0] = m_temp[k][0];
star[k+Nsub][1] = star[nrandom][1];
star[k+Nsub][2] = star[nrandom][2];
star[k+Nsub][3] = star[nrandom][3];
star[k+Nsub][4] = star[nrandom][4];
star[k+Nsub][5] = star[nrandom][5];
star[k+Nsub][6] = star[nrandom][6];
star[k+Nsub][7] = m_temp[k][1];
star[k+Nsub][8] = m_temp[k][2];
star[k+Nsub][9] = m_temp[k][3];
star[k+Nsub][10] = m_temp[k][4];
star[k+Nsub][11] = m_temp[k][5];
star[k+Nsub][12] = m_temp[k][6];
star[k+Nsub][13] = m_temp[k][7];
star[k+Nsub][14] = m_temp[k][8];
rho_dens[i][k][0] = rho_dens_single_pop[nrandom][0];
rho_dens[i][k][1] = rho_dens_single_pop[nrandom][1];
}
// reorder the masses
for (k=Nunseg;k<Nseg;k++) {
star[k+Nsub][0] = m_temp[k][0];
star[k+Nsub][7] = m_temp[k][1];
star[k+Nsub][8] = m_temp[k][2];
star[k+Nsub][9] = m_temp[k][3];
star[k+Nsub][10] = m_temp[k][4];
star[k+Nsub][11] = m_temp[k][5];
star[k+Nsub][12] = m_temp[k][6];
star[k+Nsub][13] = m_temp[k][7];
star[k+Nsub][14] = m_temp[k][8];
// remove position and velocities for orbits in other stellar population - mass segregation prosiger creates number of orbits > number of population, thus the positions and velocities for "outer" population could be
// not null
for (j=1;j<7;j++) star[k+Nsub][j] = 0.0;
}
for (j=0;j<Nunseg;j++) free (m_temp[j]);
free(m_temp);
for (j=0;j<N[i];j++) free (rho_dens_single_pop[j]);
free(rho_dens_single_pop);
N[i] = Nunseg;
} else {
printf("No mass segregation\n");
for (j=0;j<N[i];j++) {
rho_dens[i][j][0] = rho_dens_single_pop[j][0];
rho_dens[i][j][1] = rho_dens_single_pop[j][1];
}
for (j=0;j<N[i];j++) free (rho_dens_single_pop[j]);
free(rho_dens_single_pop);
}
// tscale = sqrt(rvir[i]*rvir[i]*rvir[i]/(G*M[i]));
// vscale[i] = rvir[i]/tscale;
// for (j=0; j<N[i]; j++) { //distance scaled to the relative ratio with the first generation scaling factor
// for (k=1;k<4;k++)
// star[j+Nsub][k] *= rvir[i]/rvir[0];
// }
// for (j=0; j<N[i]; j++) {
// for (k=4;k<7;k++)
// star[j+Nsub][k] *= vscale[i]/vscale[0];
// }
if (i>0) {
printf(" Scaling %i population by %f\n",i+1,conc_pop[i-1]);
for (j=0; j<N[i]; j++) { //distance scaled to the relative ratio with the first generation scaling factor
for (k=1;k<4;k++)
star[j+Nsub][k] *= conc_pop[i-1];
}
}
for (j=0;j<N[i];j++) {
star[j+Nsub][0] *= M[i];
}
free(Mcum);
} //END CICLE FOR GENERATE POSITION FOR MULTIPLE POPULATIONS
// Generate binaries properties
if (seed) srand48(seed);
FILE *TABLEkick;
TABLEkick = fopen("vkick.dat","w");
fprintf(TABLEkick,"# Initial Mass M0(M*) \t Mass M(M*) \t stellar type KW \t VS[0](km/s) \t VS[1](km/s) \t VS[2](km/s) \t resulting VKICK(km/s) \t Spin(1/year) \t Epoch \t metallicity Z(Z/Z*) \t POP_id (placeholder) \n");
FILE *TABLEbinaries;
TABLEbinaries = fopen("binaries.dat","w");
fprintf(TABLEbinaries,"# tphys(Myr) \t Initial Mass0 M1(M*) \t Initial Mass0 M2(M*) \t Mass M1(M*) \t Mass M2(M*) \t Stellar radius R1(R*) \t Stellar radius R2(R*) \t stellar type KW1 \t stellar type KW2 \t eccentricity e \t semi-major axis a(AU) \t Period P(day) \t core mass Mc1(M*) \t core mass Mc2(M*) \t core radius Rc1(R*) \t core radius Rc2(R*) \t envelope mass Me1(M*) \t envelope mass Me2(M*) \t envelope radius Re1(R*) \t envelope radius Re2(R*) \t luminosity L1(L*) \t luminosity L2(L*) \t VS1[0](km/s) \t VS1[1](km/s) \t VS1[2](km/s) \t resulting VKICK1(km/s) \t VS2[0](km/s) \t VS2[1](km/s) \t VS2[2](km/s) \t resulting VKICK2(km/s) \t Spin1(1/year) \t Spin2(1/year) \t Epoch \t metallicity Z(Z/Z*) \t \n");
FILE *TABLEsingles;
TABLEsingles = fopen("singles.dat","w");
fprintf(TABLEsingles,"# tphys(Myr) \t Initial Mass M0(M*) \t Mass M(M*) \t Stellar radius R(R*) \t stellar type KW \t core mass Mc(M*) \t core radius Rc(R*) \t envelope mass Me(M*) \t envelope radius Re(R*) \t luminosity L(L*) \t effective temperature Teff(K) \t VS[0](km/s) \t VS[1](km/s) \t VS[2](km/s) \t resulting VKICK(km/s) \t Spin(1/year) \t Epoch \t metallicity Z(Z/Z*) \t POP_id (placeholder) \n");
printf("\n\n-----GENERATE BINARIES PROPERITES----- \n");
for (i=0;i<numberofpop;i++){
//used to change the correct star array; star_array will be constructed as: Nbinaries_1_gen, Nsingle_1_gen, Nbinaries_2_gen, Nsingle_2_gen, ..
if (i == 0){
Nsub = 0;
Nbinsub = 0;
} else {
Nsub += N[i-1];
Nbinsub += nbin[i-1];
}
if (!nbin[i]) {
printf("\nNo primordial binaries for population number %i!\n",i+1);
} else {
printf("\nCreating %i primordial binary systems, fraction: %6.2f percent for population number %i.\n", nbin[i], 1.0*nbin[i]/N[i]*100.0,i+1);
//change pairing, adis and OBperiods for eigenevolution
if( (adis[i] == 3) && (eigen[i] == 0) ) {
OBperiods[i] = 1;
}
if(adis[i] == 6) {
OBperiods[i] = 1;
msort[i] = 5.0; // restore to 5MSun to have Sana et al (2012) period distribution
eigen[i] = 0;
}
if(eigen[i] == 1){
pairing[i] = 1;
adis[i] = 2;
}
if(eigen[i] == 2){
pairing[i] = 3;
adis[i] = 3;
OBperiods[i] = 1;
}
// verify amin and amax for binaries
// if(amin[i] > 0.0){
// amin[i] *= RSUN2PC_MC;
// } else {
// double mleast = MMAX; //search lowest mass star
// for (j=0;j<Ntot;j++) {
// if (star[j][0] < mleast && star[j][0] != 0.0) mleast = star[j][0];
// }
// double radleast;
// standalone_rzamsf(mleast,&radleast); // Tout Radius relation Tout et al. 1996 MNRAS 281 257
// amin[i] *= -(200.0*radleast)*RSUN2PC_MC; // POP-III minimum semi-major axis Kamlah 20.07.2021
// }
if(amax[i] > 0.0){
amax[i] *= RSUN2PC_MC;
} else {
amax[i] *= -2.5*abs(rh_mcl)/Ntot;
}
get_binaries(nbin[i], mbin, Mtotal, pairing[i], N[i], adis[i], amin[i], amax[i], rh_mcl, Ntot, eigen[i], BSE, epoch[i], Z[i], remnant, OBperiods[i], msort[i], Nsub, Nbinsub,eccbinaries, abinaries,TABLEkick,TABLEbinaries);
if (eigen[i] || (BSE && epoch[i])) {
N[i] += nbin[i];
double **mbin_index; //sort mbin by identifier
mbin_index = (double **)calloc(nbin[i], sizeof(double *));
for (j=0;j<nbin[i];j++){
mbin_index[j] = (double *)calloc(2, sizeof(double));
if (mbin_index[j] == NULL) {
printf("\nMemory allocation failed!\n");
return 0;
}
}
for (j=0;j<nbin[i];j++) {
mbin_index[j][0] = mbin[j+Nbinsub][15];
mbin_index[j][1] = j+Nbinsub;
}
shellsort(mbin_index,nbin[i],2);
double **star_index;//sort star by identifier
star_index = (double **)calloc(N[i], sizeof(double *));
for (j=0;j<N[i];j++){
star_index[j] = (double *)calloc(2, sizeof(double));
if (star_index[j] == NULL) {
printf("\nMemory allocation failed!\n");
return 0;
}
}
for (j=0;j<N[i];j++) {
star_index[j][0] = star[j+Nsub+Nbinsub][7];
star_index[j][1] = j+Nsub+Nbinsub;
}
shellsort(star_index,N[i],2);
for (j=0;j<nbin[i];j++) {
if (mbin[(int) mbin_index[j][1]][15] == star[(int) star_index[j][1]][7]) {
star[(int) star_index[j][1]][0] = mbin[(int) mbin_index[j][1]][1] + mbin[(int) mbin_index[j][1]][2];
}
}
for (j=0;j<nbin[i];j++) free (mbin_index[j]);
free(mbin_index);
for (j=0;j<N[i];j++) free (star_index[j]);
free(star_index);
N[i] -= nbin[i];
}
}
}
for (i=0;i<numberofpop;i++){
//used to change the correct star array
if (i == 0){
Nsub = 0;
Nbinsub = 0;
} else {
Nsub += N[i-1];
Nbinsub += nbin[i-1];
}
if(BSE && epoch[i]){
printf("SSE/BSE has been activated\n");
evolve_stars(N[i], star, Mtotal, epoch[i], Z[i], Nsub, nbin[i], TABLEkick,TABLEsingles);
}
}
fclose(TABLEkick);
fclose(TABLEsingles);
double massafter=0.0;
for (j=0;j<Ntot;j++) massafter += star[j][0];
if(Mtotal != massafter){
printf("Mass changed due to eigenevolution and/or stellar evolution from M = %.2f to M = %.2f\n",Mtotal,massafter);
}
// Change mass after eigenevolution and/or stellar evolution
Mtotal = massafter;
if (rh_mcl < 0.0){
double massafter_first_population=0.0;