def calcRadialDiskDensity(): yng = young.loadAllYoungStars('./') r2d = yng.getArray('r2d') names = yng.getArray('name') sidx = r2d.argsort() r2d = r2d[sidx] names = [names[ss] for ss in sidx] halfway = len(r2d) / 2 names_inner = names[0:halfway] names_outer = names[halfway:] # copy over the *.mc.dat files for name in names_inner: if not os.path.exists('aorb_efit_all_inner/%s.mc.dat' % name): shutil.copy('aorb_efit_all/%s.mc.dat' % name, 'aorb_efit_all_inner/%s.mc.dat' % name) for name in names_outer: if not os.path.exists('aorb_efit_all_outer/%s.mc.dat' % name): shutil.copy('aorb_efit_all/%s.mc.dat' % name, 'aorb_efit_all_outer/%s.mc.dat' % name) #diski = aorb.Disk('./', 'aorb_efit_all_inner/') #diski.names = names_inner #diski.run(makeplot=True) disko = aorb.Disk('./', 'aorb_efit_all_outer/') disko.names = names_outer disko.run(makeplot=True)
def calcRadialDiskDensity3(): yng = young.loadAllYoungStars('./') r2d = yng.getArray('r2d') names = yng.getArray('name') sidx = r2d.argsort() r2d = r2d[sidx] names = [names[ss] for ss in sidx] cut1 = 3.5 cut2 = 7.0 names1 = [] names2 = [] names3 = [] for rr in range(len(r2d)): if (r2d[rr] <= cut1): names1.append(names[rr]) if ((r2d[rr] > cut1) & (r2d[rr] <= cut2)): names2.append(names[rr]) if (r2d[rr] > cut2): names3.append(names[rr]) # copy over the *.mc.dat files for name in names1: if not os.path.exists('aorb_efit_all_r1/%s.mc.dat' % name): shutil.copy('aorb_efit_all/%s.mc.dat' % name, 'aorb_efit_all_r1/%s.mc.dat' % name) for name in names2: if not os.path.exists('aorb_efit_all_r2/%s.mc.dat' % name): shutil.copy('aorb_efit_all/%s.mc.dat' % name, 'aorb_efit_all_r2/%s.mc.dat' % name) for name in names3: if not os.path.exists('aorb_efit_all_r3/%s.mc.dat' % name): shutil.copy('aorb_efit_all/%s.mc.dat' % name, 'aorb_efit_all_r3/%s.mc.dat' % name) disk1 = aorb.Disk('./', 'aorb_efit_all_r1/') disk1.names = names1 disk1.run(makeplot=True) disk2 = aorb.Disk('./', 'aorb_efit_all_r2/') disk2.names = names2 disk2.run(makeplot=True) disk3 = aorb.Disk('./', 'aorb_efit_all_r3/') disk3.names = names3 disk3.run(makeplot=True)
def generateData(root, outdir, N=10**7): """ Remember to calculate at least 2 times more stars than you want because many are dropped from our observing window. """ cc = objects.Constants() yng = young.loadAllYoungStars(root) names = yng.getArray('name') # Get out the distributions we will need for our simulation. # Each relevant parameter will be plotted as a function of # radius, divided into the following radial bins: # BIN #1: r = 0.8" - 2.5" # BIN #2: r = 2.5" - 3.5" # BIN #3: r = 3.5" - 7.0" # BIN #4: r = 7.0" - 13.5" binsIn = np.array([0.8, 2.5, 3.5, 7.0, 13.5]) colors = ['red', 'orange', 'green', 'blue'] legend = ['0.8"-2.5"', '2.5"-3.5"', '3.5"-7.0"', '7.0"-13.5"'] r2d = yng.getArray('r2d') py.figure(1) py.clf() py.hist(r2d, bins=binsIn, histtype='step', linewidth=2) py.xlabel('Projected Radius (arcsec)') py.ylabel('Number of Stars Observed') py.savefig(outdir + 'hist_r2d_obs.png') binCnt = len(binsIn) - 1 ridx = [] for bb in range(binCnt): idx = np.where((r2d >= binsIn[bb]) & (r2d < binsIn[bb + 1]))[0] ridx.append(idx) ########## # # For each star we will use error distributions from the # data itself. Set these up. # ########## # Positional Uncertainties in arcsec xerrDist, yerrDist = distPositionalError(yng, binsIn) # Proper motion uncertainties in arcsec/yr vxerrDist, vyerrDist = distProperMotionError(yng, binsIn, r2d, ridx, colors, legend, outdir) # Radial velocity uncertainties in km/s vzerrDist = distRadialVelocityError(yng, binsIn, r2d, ridx, colors, legend, outdir) # Acceleration uncertainties in arcsec/yr^2 axerrDist, ayerrDist = distAccelerationError(yng, binsIn, r2d, colors, legend, outdir) # We will need a random number distribution for each of these. # These are all 0-1, but we will convert them into indices into # the above distributions later on when we know the radius of each # of the simulated stars. xerrRand = scipy.rand(N) yerrRand = scipy.rand(N) vxerrRand = scipy.rand(N) vyerrRand = scipy.rand(N) vzerrRand = scipy.rand(N) axerrRand = scipy.rand(N) ayerrRand = scipy.rand(N) ########## # # Generate simulated stars' orbital parameters # ########## # Inclination and Omega (degrees) i, o = distNormalVector(N, outdir) # Angle to the ascending node (degrees) w = scipy.rand(N) * 360.0 # Semi-major axis (pc) and period (years) a, p = distSemimajorAxisPeriod(N, outdir) # Eccentricity e = scipy.stats.powerlaw.rvs(2, 0, size=N) # Lets correct the eccentricities just to prevent the # infinite loops. edx = np.where(e > 0.98)[0] e[edx] = 0.98 # Time of Periapse (yr) t0 = scipy.rand(N) * p ########## # # Determine x, y, vx, vy, vz, ax, ay # Assign errors accordingly. # Record star to file IF it falls within our projected # observing window. # ########## x = np.zeros(N, dtype=np.float32) y = np.zeros(N, dtype=np.float32) vx = np.zeros(N, dtype=np.float32) vy = np.zeros(N, dtype=np.float32) vz = np.zeros(N, dtype=np.float32) ax = np.zeros(N, dtype=np.float32) ay = np.zeros(N, dtype=np.float32) xerr = np.zeros(N, dtype=np.float32) yerr = np.zeros(N, dtype=np.float32) vxerr = np.zeros(N, dtype=np.float32) vyerr = np.zeros(N, dtype=np.float32) vzerr = np.zeros(N, dtype=np.float32) axerr = np.zeros(N, dtype=np.float32) ayerr = np.zeros(N, dtype=np.float32) skipped = np.zeros(N, dtype=np.int16) for nn in range(N): if (((nn % 10**4) == 0)): print 'Simulated Star %d: ' % nn, time.ctime(time.time()) orb = orbits.Orbit() orb.w = w[nn] orb.o = o[nn] orb.i = i[nn] orb.e = e[nn] orb.p = p[nn] orb.t0 = t0[nn] # Remember r (arcsec), v (mas/yr), a (mas/yr^2) (pos, vel, acc) = orb.kep2xyz(np.array([0.0]), mass=4.0e6, dist=8.0e3) pos = pos[0] # in arcsec vel = vel[0] / 10**3 # convert to arcsec/yr acc = acc[0] / 10**3 # convert to arcsec/yr^2 r2d = math.sqrt(pos[0]**2 + pos[1]**2) # Impose our observing limits if (r2d < binsIn[0]) or (r2d > binsIn[-1]): skipped[nn] = 1 continue tmp = np.where(binsIn > r2d)[0] errIdx = tmp[0] - 1 # Set noisey positions xerr[nn] = xerrDist[errIdx] yerr[nn] = yerrDist[errIdx] x[nn] = np.random.normal(pos[0], xerr[nn]) y[nn] = np.random.normal(pos[1], yerr[nn]) # Set noisy velocities errCnt = len(vxerrDist) vxerr[nn] = vxerrDist[errIdx][int(math.floor(vxerrRand[nn] * errCnt))] vyerr[nn] = vyerrDist[errIdx][int(math.floor(vyerrRand[nn] * errCnt))] vzerr[nn] = vzerrDist[errIdx][int(math.floor(vzerrRand[nn] * errCnt))] # Convert radial velocity into km/s vel[2] *= 8.0 * cc.cm_in_au / (1e5 * cc.sec_in_yr) vx[nn] = np.random.normal(vel[0], vxerr[nn]) vy[nn] = np.random.normal(vel[1], vyerr[nn]) vz[nn] = np.random.normal(vel[2], vzerr[nn]) # Set noisy accelerations ONLY for close in stars if (errIdx <= 1): # Close in stars have acceleration constraints axerr[nn] = axerrDist[errIdx][int( math.floor(axerrRand[nn] * errCnt))] ayerr[nn] = ayerrDist[errIdx][int( math.floor(ayerrRand[nn] * errCnt))] ax[nn] = np.random.normal(acc[0], axerr[nn]) ay[nn] = np.random.normal(acc[1], ayerr[nn]) else: axerr[nn] = 0.0 ayerr[nn] = 0.0 ax[nn] = 0.0 ay[nn] = 0.0 useIdx = np.where(skipped == 0)[0] finalTotal = len(useIdx) skipCount = N - finalTotal print 'Skipped %d, Final Total %d' % (skipCount, finalTotal) x = x[useIdx] y = y[useIdx] vx = vx[useIdx] vy = vy[useIdx] vz = vz[useIdx] ax = ax[useIdx] ay = ay[useIdx] xerr = xerr[useIdx] yerr = yerr[useIdx] vxerr = vxerr[useIdx] vyerr = vyerr[useIdx] vzerr = vzerr[useIdx] axerr = axerr[useIdx] ayerr = ayerr[useIdx] #print 'x = ', x[-1], xerr[-1] #print 'y = ', y[-1], yerr[-1] #print 'vx = ', vx[-1], vxerr[-1] #print 'vy = ', vy[-1], vyerr[-1] #print 'vz = ', vz[-1], vzerr[-1] #print 'ax = ', ax[-1], axerr[-1] #print 'ay = ', ay[-1], ayerr[-1] # Verify that we get the expected 2D distribution back out again checkSurfaceDensity(x, y, outdir) ########## # # Write to output file # ########## _out = open(outdir + '/isotropic_stars.dat', 'w') pickle.dump((x, xerr), _out) pickle.dump((y, yerr), _out) pickle.dump((vx, vxerr), _out) pickle.dump((vy, vyerr), _out) pickle.dump((vz, vzerr), _out) pickle.dump((ax, axerr), _out) pickle.dump((ay, ayerr), _out) _out.close()
def generateData(root, outdir, N=10**7): """ Remember to calculate at least 2 times more stars than you want because many are dropped from our observing window. """ cc = objects.Constants() yng = young.loadAllYoungStars(root) names = yng.getArray('name') # Get out the distributions we will need for our simulation. # Each relevant parameter will be plotted as a function of # radius, divided into the following radial bins: # BIN #1: r = 0.8" - 2.5" # BIN #2: r = 2.5" - 3.5" # BIN #3: r = 3.5" - 7.0" # BIN #4: r = 7.0" - 13.5" binsIn = np.array([0.8, 2.5, 3.5, 7.0, 13.5]) colors = ['red', 'orange', 'green', 'blue'] legend = ['0.8"-2.5"', '2.5"-3.5"', '3.5"-7.0"', '7.0"-13.5"'] r2d = yng.getArray('r2d') py.figure(1) py.clf() py.hist(r2d, bins=binsIn, histtype='step', linewidth=2) py.xlabel('Projected Radius (arcsec)') py.ylabel('Number of Stars Observed') py.savefig(outdir + 'hist_r2d_obs.png') binCnt = len(binsIn) - 1 ridx = [] for bb in range(binCnt): idx = np.where((r2d >= binsIn[bb]) & (r2d < binsIn[bb+1]))[0] ridx.append( idx ) ########## # # For each star we will use error distributions from the # data itself. Set these up. # ########## # Positional Uncertainties in arcsec xerrDist, yerrDist = distPositionalError(yng, binsIn) # Proper motion uncertainties in arcsec/yr vxerrDist, vyerrDist = distProperMotionError(yng, binsIn, r2d, ridx, colors, legend, outdir) # Radial velocity uncertainties in km/s vzerrDist = distRadialVelocityError(yng, binsIn, r2d, ridx, colors, legend, outdir) # Acceleration uncertainties in arcsec/yr^2 axerrDist, ayerrDist = distAccelerationError(yng, binsIn, r2d, colors, legend, outdir) # We will need a random number distribution for each of these. # These are all 0-1, but we will convert them into indices into # the above distributions later on when we know the radius of each # of the simulated stars. xerrRand = scipy.rand(N) yerrRand = scipy.rand(N) vxerrRand = scipy.rand(N) vyerrRand = scipy.rand(N) vzerrRand = scipy.rand(N) axerrRand = scipy.rand(N) ayerrRand = scipy.rand(N) ########## # # Generate simulated stars' orbital parameters # ########## # Inclination and Omega (degrees) i, o = distNormalVector(N, outdir) # Angle to the ascending node (degrees) w = scipy.rand(N) * 360.0 # Semi-major axis (pc) and period (years) a, p = distSemimajorAxisPeriod(N, outdir) # Eccentricity e = scipy.stats.powerlaw.rvs(2, 0, size=N) # Lets correct the eccentricities just to prevent the # infinite loops. edx = np.where(e > 0.98)[0] e[edx] = 0.98 # Time of Periapse (yr) t0 = scipy.rand(N) * p ########## # # Determine x, y, vx, vy, vz, ax, ay # Assign errors accordingly. # Record star to file IF it falls within our projected # observing window. # ########## x = np.zeros(N, dtype=np.float32) y = np.zeros(N, dtype=np.float32) vx = np.zeros(N, dtype=np.float32) vy = np.zeros(N, dtype=np.float32) vz = np.zeros(N, dtype=np.float32) ax = np.zeros(N, dtype=np.float32) ay = np.zeros(N, dtype=np.float32) xerr = np.zeros(N, dtype=np.float32) yerr = np.zeros(N, dtype=np.float32) vxerr = np.zeros(N, dtype=np.float32) vyerr = np.zeros(N, dtype=np.float32) vzerr = np.zeros(N, dtype=np.float32) axerr = np.zeros(N, dtype=np.float32) ayerr = np.zeros(N, dtype=np.float32) skipped = np.zeros(N, dtype=np.int16) for nn in range(N): if ( ((nn % 10**4) == 0)): print 'Simulated Star %d: ' % nn, time.ctime(time.time()) orb = orbits.Orbit() orb.w = w[nn] orb.o = o[nn] orb.i = i[nn] orb.e = e[nn] orb.p = p[nn] orb.t0 = t0[nn] # Remember r (arcsec), v (mas/yr), a (mas/yr^2) (pos, vel, acc) = orb.kep2xyz(np.array([0.0]), mass=4.0e6, dist=8.0e3) pos = pos[0] # in arcsec vel = vel[0] / 10**3 # convert to arcsec/yr acc = acc[0] / 10**3 # convert to arcsec/yr^2 r2d = math.sqrt(pos[0]**2 + pos[1]**2) # Impose our observing limits if (r2d < binsIn[0]) or (r2d > binsIn[-1]): skipped[nn] = 1 continue tmp = np.where(binsIn > r2d)[0] errIdx = tmp[0] - 1 # Set noisey positions xerr[nn] = xerrDist[errIdx] yerr[nn] = yerrDist[errIdx] x[nn] = np.random.normal(pos[0], xerr[nn]) y[nn] = np.random.normal(pos[1], yerr[nn]) # Set noisy velocities errCnt = len(vxerrDist) vxerr[nn] = vxerrDist[errIdx][ int(math.floor(vxerrRand[nn] * errCnt)) ] vyerr[nn] = vyerrDist[errIdx][ int(math.floor(vyerrRand[nn] * errCnt)) ] vzerr[nn] = vzerrDist[errIdx][ int(math.floor(vzerrRand[nn] * errCnt)) ] # Convert radial velocity into km/s vel[2] *= 8.0 * cc.cm_in_au / (1e5 * cc.sec_in_yr) vx[nn] = np.random.normal(vel[0], vxerr[nn]) vy[nn] = np.random.normal(vel[1], vyerr[nn]) vz[nn] = np.random.normal(vel[2], vzerr[nn]) # Set noisy accelerations ONLY for close in stars if (errIdx <= 1): # Close in stars have acceleration constraints axerr[nn] = axerrDist[errIdx][ int(math.floor(axerrRand[nn] * errCnt)) ] ayerr[nn] = ayerrDist[errIdx][ int(math.floor(ayerrRand[nn] * errCnt)) ] ax[nn] = np.random.normal(acc[0], axerr[nn]) ay[nn] = np.random.normal(acc[1], ayerr[nn]) else: axerr[nn] = 0.0 ayerr[nn] = 0.0 ax[nn] = 0.0 ay[nn] = 0.0 useIdx = np.where(skipped == 0)[0] finalTotal = len(useIdx) skipCount = N - finalTotal print 'Skipped %d, Final Total %d' % (skipCount, finalTotal) x = x[useIdx] y = y[useIdx] vx = vx[useIdx] vy = vy[useIdx] vz = vz[useIdx] ax = ax[useIdx] ay = ay[useIdx] xerr = xerr[useIdx] yerr = yerr[useIdx] vxerr = vxerr[useIdx] vyerr = vyerr[useIdx] vzerr = vzerr[useIdx] axerr = axerr[useIdx] ayerr = ayerr[useIdx] #print 'x = ', x[-1], xerr[-1] #print 'y = ', y[-1], yerr[-1] #print 'vx = ', vx[-1], vxerr[-1] #print 'vy = ', vy[-1], vyerr[-1] #print 'vz = ', vz[-1], vzerr[-1] #print 'ax = ', ax[-1], axerr[-1] #print 'ay = ', ay[-1], ayerr[-1] # Verify that we get the expected 2D distribution back out again checkSurfaceDensity(x, y, outdir) ########## # # Write to output file # ########## _out = open(outdir + '/isotropic_stars.dat', 'w') pickle.dump((x, xerr), _out) pickle.dump((y, yerr), _out) pickle.dump((vx, vxerr), _out) pickle.dump((vy, vyerr), _out) pickle.dump((vz, vzerr), _out) pickle.dump((ax, axerr), _out) pickle.dump((ay, ayerr), _out) _out.close()