00001
00002
00003
00004
00005
00006 #include "dyn.h"
00007 #include "star/stdfunc.h"
00008
00009 #define Rsun_pc 2.255e-8 // R_sun/parsec = 6.960e+10/3.086e+18
00010
00011 #ifdef TOOLBOX
00012
00013 local real encounter_rate(int im, real ni, real mi, real ri, real vi,
00014 real rci,
00015 int jm, real nj, real mj, real rj, real vj,
00016 real rcj)
00017 {
00018
00019 real to_volume = 4*PI/3.;
00020
00021
00022
00023
00024 real n_rhoi = 1./(to_volume * pow(rci, 3));
00025 real n_rhoj = 1./(to_volume * pow(rcj, 3));
00026 real r_tot = ri + rj;
00027 real m_tot = mi + mj;
00028 real v_rel = sqrt(pow(vi, 2) + pow(vj, 2));
00029 real rcore = min(rci, rcj);
00030
00031 real geometric = 6.31e-15 * v_rel * pow(r_tot, 2);
00032 real grav_foc = 3.61e-9 * m_tot * r_tot / v_rel;
00033
00034 real rate = 0;
00035
00036 if (im==jm) {
00037
00038 if (ni>1)
00039 rate = 0.5 * ((ni-1) * n_rhoi)
00040 * (nj * n_rhoj)
00041 * pow(rcore, 3)
00042 * (grav_foc + geometric);
00043 }
00044 else
00045 rate = (ni * n_rhoi)
00046 * (nj * n_rhoj)
00047 * pow(rcore, 3)
00048 * (grav_foc + geometric);
00049
00050 #if 0
00051 if(rate>0)
00052 cerr << jm <<" "<< im <<" "
00053 << mj <<" "<< mi <<" "
00054 << nj <<" "<< ni <<" "
00055 << rate << endl;
00056 #endif
00057
00058 return rate;
00059 }
00060
00061
00062
00063
00064
00065 main(int argc, char **argv)
00066 {
00067 int nzones = 10000;
00068 int nstep = 1;
00069 real mmin = 0.1;
00070 real mmax = 100;
00071 real x_imf= 2.35;
00072 real rcore= 0.1;
00073 real vsun = 10;
00074 real dt = 1;
00075 int n = 4;
00076
00077 extern char *poptarg;
00078 int c;
00079 char* param_string = "N:n:M:m:T:x:r:v:";
00080
00081 while ((c = pgetopt(argc, argv, param_string)) != -1)
00082 switch(c) {
00083
00084 case 'n': nzones = atoi(poptarg);
00085 break;
00086 case 'N': nstep = atoi(poptarg);
00087 break;
00088 case 'M': mmax = atof(poptarg);
00089 break;
00090 case 'm': mmin = atof(poptarg);
00091 break;
00092 case 'T': dt = atof(poptarg);
00093 break;
00094 case 'x': x_imf = atof(poptarg);
00095 break;
00096 case 'r': rcore = atof(poptarg);
00097 break;
00098 case 'v': vsun = atof(poptarg);
00099 break;
00100 case '?': params_to_usage(cerr, argv[0], param_string);
00101 exit(1);
00102 }
00103
00104
00105
00106 real *imf = new real[n*nzones+1];
00107 real *cmf = new real[n*nzones+1];
00108 real *fmf = new real[n*nzones+1];
00109 real *mass = new real[n*nzones+1];
00110 real *radi = new real[n*nzones+1];
00111 real *vdsp = new real[n*nzones+1];
00112 real *loss = new real[n*nzones+1];
00113 real *gain = new real[n*nzones+1];
00114
00115 real dmass = (mmax-mmin)/(1.*nzones);
00116
00117 for(int im=1; im<n*nzones; im++) {
00118 mass[im] = mmin + dmass * im;
00119
00120 radi[im] = 1./pow(mass[im], 1.7);
00121 vdsp[im] = vsun/sqrt(mass[im]);
00122 imf[im] = 0;
00123 cmf[im] = 0;
00124 fmf[im] = 0;
00125 loss[im] = 0;
00126 gain[im] = 0;
00127 }
00128
00129 for(int im=1; im<nzones; im++)
00130 imf[im] = 1./pow(mass[im], x_imf);
00131 for(int im=1; im<nzones; im++)
00132 imf[im] = imf[im]/imf[nzones-1];
00133
00134
00135 for(int im=1; im<nzones; im++) {
00136
00137 cmf[im] = imf[im];
00138 }
00139
00140 real rate, total_rate = 0;
00141 real ncoll, nloss=0;
00142 int km;
00143 real total_loss, total_gain, nstar_loss;
00144 total_loss=total_gain=nstar_loss=0;
00145
00146 for(int is=0; is<nstep; is++) {
00147 for (int im=1; im<n*nzones; im++) {
00148 for (int jm=im; jm<n*nzones; jm++)
00149 if(cmf[im]>0 && cmf[jm]>0) {
00150
00151 rate = encounter_rate(im, cmf[im], mass[im], radi[im],
00152 vdsp[im], rcore,
00153 jm, cmf[jm], mass[jm], radi[jm],
00154 vdsp[jm], rcore);
00155 total_rate += rate;
00156
00157 ncoll = max(0., min(rate * dt,
00158 min(cmf[im]-loss[im],
00159 cmf[jm]-loss[jm])));
00160
00161 km = min(n*nzones, im + jm);
00162 loss[im] += ncoll;
00163 loss[jm] += ncoll;
00164 gain[km] += ncoll;
00165
00166
00167
00168 }
00169 }
00170
00171 cerr << " Total encounter rate = "
00172 << total_rate << " [per Myr]" << endl;
00173
00174 for (int im=1; im<n*nzones; im++) {
00175 nloss += gain[im] - loss[im];
00176 total_loss += loss[im] * mass[im];
00177 total_gain += gain[im] * mass[im];
00178 fmf[im] = cmf[im] - loss[im] + gain[im];
00179 }
00180 for (int im=1; im<=n*nzones; im++) {
00181 loss[im]=gain[im]=0;
00182 cmf[im] = fmf[im];
00183 }
00184 }
00185
00186
00187 real imf_tot = 0;
00188 for (int im=1; im<n*nzones; im++)
00189 imf_tot += imf[im];
00190 real fmtot=0;
00191 real imtot=0;
00192 for (int im=1; im<n*nzones; im++) {
00193 imf[im] = imf[im]/imf_tot;
00194 fmf[im] = fmf[im]/imf_tot;
00195 imtot += imf[im]*mass[im];
00196 fmtot += fmf[im]*mass[im];
00197 }
00198
00199 cerr << "Old and new mass function: "<<endl;
00200 for (km=1; km<n*nzones; km++) {
00201 if(imf[km]>0 || fmf[km]>0)
00202 cerr << km <<" "<< mass[km] <<" "
00203 << imf[km] << " " << fmf[km] << endl;
00204 }
00205
00206 cerr << " N star lost = " << nloss<< endl;
00207 cerr << "Total mass goes from: "<< imtot << " to " << fmtot <<endl;
00208
00209 }
00210
00211 #endif