00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "SeBa_hist.h"
00028
00029 #ifdef TOOLBOX
00030
00031 real **mkrarray(int rows, int cols) {
00032 real ** ret = new real *[rows];
00033 for (int i=0; i<cols; i++)
00034 ret[i] = new real[cols];
00035
00036 for (int i=0; i<rows; i++)
00037 for (int j=0; j<cols; j++)
00038 ret[i][j] = 0;
00039
00040 return ret;
00041 }
00042
00043 void print_binary_matrix(real **population) {
00044
00045 real T_row = 0;
00046 cerr << " ";
00047 for (int j=0; j<no_of_stellar_type-1; j++) {
00048 cerr << " " << type_short_string((stellar_type)j);
00049 if(j==(int)Main_Sequence ||
00050 j==(int)Super_Giant ||
00051 j==(int)Thorn_Zytkow)
00052 cerr << " ";
00053 }
00054 cerr << endl;
00055
00056 for (int k = 0; k < no_of_stellar_type-1; k++) {
00057 T_row = 0;
00058 for (int i = 0; i < no_of_stellar_type-1; i++)
00059 T_row += population[i][k];
00060
00061 if(T_row>0) {
00062 cerr << " " << type_short_string((stellar_type)k);
00063
00064 for (int i = 0; i < no_of_stellar_type-1; i++) {
00065 cerr << " " << population[i][k];
00066 if(i==(int)Main_Sequence ||
00067 i==(int)Super_Giant ||
00068 i==(int)Thorn_Zytkow)
00069 cerr << " ";
00070 }
00071 cerr << endl;
00072 }
00073 }
00074 }
00075
00076 void print_binary_array(real **population) {
00077
00078 real N_row = 0;
00079
00080 int n_all = no_of_star_type_summ*no_of_star_type_summ;
00081 real *pri_sec = new real[n_all];
00082 for (int j=0; j<n_all; j++)
00083 pri_sec[j] = 0;
00084
00085 int js, ks, ts;
00086 for (int j=0; j<no_of_stellar_type-1; j++)
00087 for (int k = 0; k < no_of_stellar_type-1; k++) {
00088 js = (int)summarize_stellar_type((stellar_type)j);
00089 ks = (int)summarize_stellar_type((stellar_type)k);
00090 if(js>ks) {
00091 ts = js;
00092 js=ks;
00093 ks = ts;
00094 }
00095 pri_sec[js + no_of_star_type_summ*ks] += population[j][k];
00096 }
00097
00098 for (js=0; js<no_of_star_type_summ-1; js++)
00099 for (ks = 0; ks < no_of_star_type_summ-1; ks++) {
00100 if(pri_sec[js + no_of_star_type_summ*ks]>0)
00101 cerr << " (" << type_short_string((stellar_type_summary)js)
00102 << ", " << type_short_string((stellar_type_summary)ks) <<")\t"
00103 << pri_sec[js + no_of_star_type_summ*ks] << endl;
00104 }
00105
00106 delete []pri_sec;
00107 }
00108
00109 local real integrate_birthrate(real t_start, real t_end,
00110 real tau, real cnst) {
00111
00112 real integrand;
00113 if (tau==0)
00114 integrand = cnst*(t_end - t_start);
00115 else
00116 integrand = tau * cnst * (exp(-t_start/tau) - exp(-t_end/tau));
00117
00118 return integrand;
00119
00120 }
00121
00122 local real contribution_to_population(real current_time,
00123 real t_start, real t_end,
00124 real tau, real cnst) {
00125
00126 real t_int_start = max(0., current_time - t_end);
00127 real t_int_end = max(0., current_time - t_start);
00128
00129 return integrate_birthrate(t_int_start, t_int_end, tau, cnst);
00130
00131 }
00132
00133 local int extract_population(SeBa_hist *hi, real snap_time,
00134 real tau, real int_cnst,
00135 real normalize) {
00136
00137 int binaries_read = 0;
00138 real total_number_of_binaries = 0;
00139
00140 real** detached_population;
00141 real** contact_population;
00142 real* single_population;
00143 real N_detached=0, N_contact=0, N_single=0;
00144 binary_type tpe_bin;
00145 stellar_type prim_type, sec_type, ts;
00146 real prim_mass, sec_mass;
00147
00148 detached_population = mkrarray((int)no_of_stellar_type,
00149 (int)no_of_stellar_type);
00150 contact_population = mkrarray((int)no_of_stellar_type,
00151 (int)no_of_stellar_type);
00152 single_population = new real[(int)no_of_stellar_type];
00153 for(int i=0; i<(int)no_of_stellar_type; i++)
00154 single_population[i]=0;
00155
00156 int mergers = 0;
00157 SeBa_hist* next_hi = get_history(hi, cin);
00158 do {
00159
00160 binaries_read++;
00161
00162 if (hi->get_last()->get_binary_type() == Merged)
00163 mergers++;
00164
00165
00166
00167 real tstart, tend, birthweight;
00168
00169
00170 if (hi->get_last()->get_time() < snap_time) {
00171 cerr << "ERROR: snap_time not reached" << endl;
00172 exit(-1);
00173 }
00174
00175 for_all_SeBa_hist(SeBa_hist, hi, hj) {
00176
00177
00178
00179
00180
00181
00182
00183 tstart = hj->get_time();
00184 if(tstart>snap_time) {
00185 break;
00186 }
00187 else {
00188 tend = hj->get_future()->get_time();
00189 if (tend>snap_time)
00190 tend = snap_time;
00191 }
00192
00193 birthweight = contribution_to_population(snap_time, tstart,
00194 tend, tau, int_cnst);
00195 if(hj) {
00196 tpe_bin = hj->get_binary_type();
00197 prim_type = hj->get_primary_type();
00198 prim_mass = hj->get_parameter(primary_mass);
00199 sec_type = hj->get_secondary_type();
00200 sec_mass = hj->get_parameter(secondary_mass);
00201
00202 if(prim_type>=0 && sec_type>=0) {
00203 if(tpe_bin==Detached) {
00204 N_detached += birthweight;
00205 detached_population[(int)prim_type][(int)sec_type]
00206 += birthweight;
00207 total_number_of_binaries += birthweight;
00208
00209 }
00210 else if(tpe_bin==Semi_Detached ||
00211 tpe_bin==Contact) {
00212 N_contact += birthweight;
00213 contact_population[(int)prim_type][(int)sec_type] += birthweight;
00214 total_number_of_binaries += birthweight;
00215 }
00216 else {
00217 N_single += birthweight;
00218 single_population[(int)prim_type] += birthweight;
00219 total_number_of_binaries += birthweight;
00220
00221 if(tpe_bin==Disrupted) {
00222 N_single += birthweight;
00223 single_population[(int)sec_type] += birthweight;
00224 total_number_of_binaries += birthweight;
00225 }
00226 }
00227 }
00228 }
00229 }
00230
00231 delete hi;
00232
00233 hi = next_hi;
00234 next_hi = get_history(hi, cin);
00235 }
00236 while (next_hi);
00237
00238
00239 PRL(total_number_of_binaries);
00240 if(normalize>0) {
00241 int p = cerr.precision(LOW_PRECISION);
00242 cerr << "Normalized to "<<normalize<<endl;
00243 total_number_of_binaries /= normalize;
00244
00245 for (int j = 0; j<no_of_stellar_type-1; j++) {
00246
00247 single_population[j] /= total_number_of_binaries;
00248
00249 for (int i = 0; i < no_of_stellar_type-1; i++) {
00250 detached_population[i][j] /= total_number_of_binaries;
00251 contact_population[i][j] /= total_number_of_binaries;
00252 }
00253 }
00254 cerr.precision(p);
00255 }
00256
00257 if (N_detached>0) {
00258 cerr << " Detached population"<<endl;
00259 print_binary_matrix(detached_population);
00260 cerr << "\n Summary:" << endl;
00261 print_binary_array(detached_population);
00262 }
00263 else
00264 cerr << " ---No detached binaries---" <<endl;
00265
00266 cerr << endl;
00267
00268 if (N_contact>0) {
00269 cerr << " Semi-detached/contact population"<<endl;
00270 print_binary_matrix(contact_population);
00271 cerr << "\n Summary:" << endl;
00272 print_binary_array(contact_population);
00273 }
00274 else
00275 cerr << " ---No contact Binaries---" <<endl;
00276
00277 cerr << endl;
00278
00279 cerr << " Single population"<<endl;
00280 cerr << " ";
00281 for (int j=0; j<no_of_stellar_type-1; j++) {
00282 cerr << " " << type_short_string((stellar_type)j);
00283 if(j==(int)Main_Sequence ||
00284 j==(int)Super_Giant ||
00285 j==(int)Thorn_Zytkow)
00286 cerr << " ";
00287 }
00288
00289 cerr << endl;
00290
00291 cerr << endl << " ";
00292 for (int j = 0; j<no_of_stellar_type-1; j++) {
00293 cerr << " " << single_population[j];
00294 if(j==(int)Main_Sequence ||
00295 j==(int)Super_Giant ||
00296 j==(int)Thorn_Zytkow)
00297 cerr << " ";
00298 }
00299 cerr << endl;
00300
00301 return binaries_read;
00302
00303 }
00304
00305
00306
00307
00308 main(int argc, char ** argv) {
00309
00310 real normalize = -1;
00311 real snap_time = 0;
00312 real tau = 0;
00313 real cnst = 1;
00314
00315 char *comment;
00316 check_help();
00317
00318 extern char *poptarg;
00319 int c;
00320 char* param_string = "N:t:x:c:";
00321
00322 while ((c = pgetopt(argc, argv, param_string)) != -1)
00323 switch(c)
00324 {
00325 case 't': snap_time = atof(poptarg);
00326 break;
00327 case 'x': tau = atof(poptarg);
00328 break;
00329 case 'N': normalize = atof(poptarg);
00330 break;
00331 case 'c': cnst = atof(poptarg);
00332 break;
00333 case '?': params_to_usage(cerr, argv[0], param_string);
00334 get_help();
00335 exit(1);
00336 }
00337
00338
00339 SeBa_hist* hi = new SeBa_hist;
00340 if (!hi->read_SeBa_hist(cin))
00341 exit(-1);
00342
00343 cerr << "Time = " << snap_time << " [Myr]" << endl << endl;
00344
00345
00346 int binaries_read = extract_population(hi, snap_time,
00347 tau, cnst, normalize);
00348
00349 cerr << "Total number of binaries read: " << binaries_read << endl;
00350 }
00351
00352 #endif // endof: TOOLBOX
00353
00354
00355