Main Page   Class Hierarchy   Data Structures   File List   Data Fields   Globals  

rs_birthrate.C

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

Generated at Sun Feb 24 09:57:13 2002 for STARLAB by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001