def choosedt(dt,n,neq,T_cs,Y_CO,Y_C_free,Y_O_free,K_ra,K_rd,n_C_free,size,dust_Y): adap_flag = 0 # Change in Y_C_free from CO formation: delta_YC_CO = ab.dYCO_dt(Y_CO, Y_C_free, Y_O_free, n, K_ra, K_rd) * dt # Change in Y_C_free from carbon dust nucleation (and growth) dYdt_nucl = 0. dNdt_grow = np.zeros([par.arraylen]) sat = n_C_free/neq if sat > 1: J = (ph.cshape**3 * ph.vC**2 * ph.sigma/(18*np.pi**2*ph.mC))**(1./2.) * n_C_free**2 * \ np.exp(-4*ph.cshape**3*ph.vC**2*ph.sigma**3/27./(ph.kB*T_cs)**3/(np.log(sat)**2)) dmu = ph.kB*T_cs*np.log(sat) ncritical=np.maximum(8*ph.cshape**3*ph.vC**2*ph.sigma**3/27/dmu**3,2) dYdt_nucl = J*ncritical/n # Calculate the possible growth dNdt_grow = ph.cshape*(size*ph.vC)**(2./3.) * np.sqrt(ph.kB*T_cs/(2*np.pi*ph.mC)) * n_C_free delta_YC_grow = np.sum(dNdt_grow*dust_Y)*dt # dust_Y is a 1d array storing previous dYdt_nucl[i], read into function delta_YC_nucl = dYdt_nucl*dt delta_YC_grow = np.sum(dNdt_grow*dust_Y)*dt # Maybe this will be too stringent, but let's try # if the sum (not absolute value?) of all changes in Y_C_free is greater than # say, 30% of Y_C_free (+ a minimum value) or Y_O_free, for future abundances # then reduce dt: delta_YC_tot = delta_YC_CO + delta_YC_nucl + delta_YC_grow while ((np.absolute(delta_YC_tot) > par.percent*Y_C_free + par.Y_min) \ or np.absolute(delta_YC_tot) > par.percent*Y_O_free + par.Y_min): print 'timestep reduced ', Y_C_free, delta_YC_CO, delta_YC_nucl, delta_YC_grow adap_flag = 1 dt = dt/2. delta_YC_CO = ab.dYCO_dt(Y_CO, Y_C_free, Y_O_free, n, K_ra, K_rd) * dt delta_YC_nucl = dYdt_nucl*dt delta_YC_grow = np.sum(dNdt_grow*dust_Y)*dt delta_YC_tot = delta_YC_CO + delta_YC_nucl + delta_YC_grow return adap_flag, dt
ratesfile.write("%.5f %.5e %.5e %.5e \n"%(K_ra, K_th, K_nth, J_C)) thermofile.write("%.5f %.5f\n"%(T_cs, n)) #----------- TIME EVOLUTION ----------------------# i = 1 # running to tmax - dt because interpolate function doesn't like t values outside of t_array while t < par.tmax - par.dt_init: #---------------- First choose dt ------------------# dt = par.dt_init # -------------- Molecular Abundances ------------------ #adap_flag, dt = choosedt(dt,n,neq,T_cs,Y_CO,Y_C_free,Y_O_free,K_ra,K_rd,n_C_free,size,dust_Y) # Evolve Y_CO dYCOdt = ab.dYCO_dt(Y_CO, Y_C_free, Y_O_free, n, K_ra, K_rd) # ----------------------- DUST ----------------------- # The equilibrium number fraction of carbon at this T neq=6.9e13*np.exp(-84428.2/T_cs)/ph.kB/T_cs if neq==0: neq=par.floor # just set it to a very small number if it comes out as 0 if neq!=neq: neq=par.floor # or if it comes out to be a NaN # Carbon saturation sat_C = n_C_free/neq # Reset the rate of gaseous C depletion due to nucleation as 0 dYdt_nucl_C = 0 J_C = 0. ncritical = 0 # Compute the growth rate of previously formed grains [# of atoms / s] # depends on current size of each set of grains
ratesfile.write("%.5f %.5e %.5e \n" % (K_ra, K_th, K_nth)) thermofile.write("%.5f %.5f %.5f %.5f %.5f \n" % (T_cs, n, delta, R_cs, c_s)) #----------- TIME EVOLUTION ----------------------# while t < par.tmax: # What to do when Y_C_free goes negative?? For now, break #if Y_C_free < 0.: break #---------------- First choose dt ------------------# dt = par.dt_init Y_CO_equilibrium = ab.Y_CO_equil(par.Y_C_tot, par.Y_O_tot, n, K_ra, K_rd) #if t > par.t_integrate: if Y_CO_equilibrium > 1e-5: integrate_flag = 1 adaptive_dt_flag = 0 delta_YCO = ab.dYCO_dt(Y_CO, Y_C_free, Y_O_free, n, K_ra, K_rd) * dt # Reduce dt if delta_YCO is too large while (np.absolute(delta_YCO) > Y_C_free \ or np.absolute(delta_YCO) > Y_O_free): adaptive_dt_flag = 1 dt = dt / 2. delta_YCO = ab.dYCO_dt(Y_CO, Y_C_free, Y_O_free, n, K_ra, K_rd) * dt # Also reduce dt so we don't form more than some percent of CO #while ((Y_CO > par.CO_amount or Y_CO + delta_YCO > par.CO_amount) \ if ((Y_CO > 0) and ((Y_CO > par.CO_amount) or (Y_CO + delta_YCO > par.CO_amount))): while (np.absolute(delta_YCO) > par.percentCO * Y_CO): adaptive_dt_flag = 1 dt = dt / 2. delta_YCO = ab.dYCO_dt(Y_CO, Y_C_free, Y_O_free, n, K_ra,
thermofile.write("%.5f %.5f %.5f %.5f %.5f \n"%(T_cs, n, delta, R_cs, c_s)) #----------- TIME EVOLUTION ----------------------# while t < par.tmax: # What to do when Y_C_free goes negative?? For now, break #if Y_C_free < 0.: break #---------------- First choose dt ------------------# dt = par.dt_init Y_CO_equilibrium = ab.Y_CO_equil(par.Y_C_tot, par.Y_O_tot, n, K_ra, K_rd) #if t > par.t_integrate: if Y_CO_equilibrium > 1e-5: integrate_flag = 1 adaptive_dt_flag = 0 delta_YCO = ab.dYCO_dt(Y_CO, Y_C_free, Y_O_free, n, K_ra, K_rd) * dt # Reduce dt if delta_YCO is too large while (np.absolute(delta_YCO) > Y_C_free \ or np.absolute(delta_YCO) > Y_O_free): adaptive_dt_flag = 1 dt = dt/2. delta_YCO = ab.dYCO_dt(Y_CO, Y_C_free, Y_O_free, n, K_ra, K_rd) * dt # Also reduce dt so we don't form more than some percent of CO #while ((Y_CO > par.CO_amount or Y_CO + delta_YCO > par.CO_amount) \ if ((Y_CO > 0) and ((Y_CO > par.CO_amount) or (Y_CO + delta_YCO > par.CO_amount))): while (np.absolute(delta_YCO) > par.percentCO*Y_CO): adaptive_dt_flag = 1 dt = dt/2. delta_YCO = ab.dYCO_dt(Y_CO, Y_C_free, Y_O_free, n, K_ra, K_rd) * dt # Evolve Y_CO Y_CO = np.maximum(delta_YCO + Y_CO,0.)
"%.5e %.5e %.5e %.5e %.5e %.5e %.5e %.5e %.5e \n" % (K_CO_ra, K_CO_th, K_CO_nth, K_SiO_ra, K_SiO_th, K_SiO_nth, K_CO_H, J_Cg, J_Sig) ) thermofile.write("%.5f %.5f\n" % (T_cs, n)) # ----------- TIME EVOLUTION ----------------------# i = 1 # running to tmax - dt because interpolate function doesn't like t values outside of t_array while t < par.tmax - par.dt_init: # ---------------- First choose dt ------------------# dt = par.dt_init # -------------- Molecular Abundances ------------------ # Evolve Molecular reactions dYCOdt = ab.dYCO_dt(Y_CO, Y_C, Y_O, n, K_CO_ra, K_CO_rd) dYSiOdt = ab.dYSiO_dt(Y_SiO, Y_Si, Y_O, n, K_SiO_ra, K_SiO_rd) # dYHdt = ab.dYH_dt(Y_CO,Y_H,n,K_CO_H) # dYCdt = ab.dYC_dt(Y_CO,Y_H,n,K_CO_H) dYOHdt = ab.dYOH_dt(Y_CO, Y_H, n, K_CO_H) # ----------------------- DUST ----------------------- # The equilibrium number fraction of carbon at this T neq = 6.9e13 * np.exp(-84428.2 / T_cs) / ph.kB / T_cs if neq == 0: neq = par.floor # just set it to a very small number if it comes out as 0 if neq != neq: neq = par.floor # or if it comes out to be a NaN # Carbon saturation sat_Cg = n_C / neq