Ejemplo n.º 1
0
def manage_GC_data(GCname, fname_GClist, pathGCs):
    """
    PURPOSE:
        Return the GC data of the target GC.
        Also the parameters of the background globular clusters / subhalos
        IF GCname appears in fname_GClist, it will be omitted from fname_GClist
    ARGUMENTS:
        GCname: the target GC of interest
        fname_GClist: text file containing the list of the clusters we want to act on the target GC
        pathGCs: relative path to directory containing background halos
    RETURNS:
        GCParams, bgGCNames, bgGCParams
            bg implies background (which is either a different GC or subhalo)
    """
    names = np.loadtxt(fname_GClist, dtype=str)
    GCParams = inputGC.Plummer(GCname)
    fnames = os.listdir(pathGCs)
    my_fnames = [os.path.splitext(x)[0] for x in fnames]
    bgGCParams = []
    bgGCNames = []
    for name in names:
        if name != GCname:
            retunedVal = inputGC.Plummer(name)
            if type(retunedVal) == list:
                if "orbit" + name in my_fnames:
                    bgGCParams.append(retunedVal)
                    bgGCNames.append("orbit" + name)
                else:
                    print(name, "is not found in ", pathGCs, "skipping")
            else:
                print("skipping", name)
    bgGCParams = np.array(bgGCParams).T
    return GCParams, bgGCParams, bgGCNames
Ejemplo n.º 2
0
import time
import inputGC
from astropy.io import fits
from astropy.table import Table

"""
Assign parameters: core radius, total mass, fraction of mass inside cutoff radius, total number of particles
"""

f = open('GClistred', 'r')
for line in f :
    GCname = line.strip()
    print(GCname)

    G=1.
    mtot,rcore=inputGC.Plummer(GCname)
    cutoff_massfraction=0.990 # fraction of mass inside the cutoff radius
    ntot=100000 # number of particles to be generated

    mass=[cutoff_massfraction*mtot/ntot]*ntot # mass of a single particle
    cutoff_mass=cutoff_massfraction*mtot
    c=cutoff_massfraction**(2/3)
    cutoff_radius=np.sqrt(rcore*rcore*(c/(1-c)))
    print("cutoff radius "+str(cutoff_radius))

    """
    Define functions
    """
    def plummer(mtot,rcore,r):
        plummer=3*mtot/(4*np.pi*rcore**3)
        plummer=plummer/(1+(r/rcore)**2)**(2.5)
Ejemplo n.º 3
0
        #then place the GC in the Galaxy, assigning to its baricentre positions and velocities as T Gyr ago
        x0 = x0 + xGCini
        y0 = y0 + yGCini
        z0 = z0 + zGCini
        vx0 = vx0 + vxGCini
        vy0 = vy0 + vyGCini
        vz0 = vz0 + vzGCini

        #now integrate the N-body GC forward in time
        start = time.time()

        backward = 'NO'
        N = len(x0)

        GCparam = inputGC.Plummer(GCname)
        MWparam = inputMW.MW(modelshort)

        integrate.deallocate()
        integrate.orbits(x0, y0, z0, vx0, vy0, vz0, GCparam, MWparam, model,
                         modelshort, GCname, backward, pathorbits, deltat,
                         Nstep, isave, N)

        xf = integrate.xp
        yf = integrate.yp
        zf = integrate.zp
        vxf = integrate.vxp
        vyf = integrate.vyp
        vzf = integrate.vzp
        tesc = integrate.tesc
        phiMW = integrate.phimw
Ejemplo n.º 4
0
def execute(number, GCname, gc_frame, Nstep, deltat, Nsample, model,
            modelshort):
    """ 
    ARGUMENTS:
        number:     Iterator for random gaussian intial conditions (do we do this??)
        GCname:     "Pal5"
        gc_frame:   galactic coordinate system  
        Nstep:      ?? number of total time steps
        deltat:     size of each time step
        Nsample:    used for selecting the random initial conditions
        model:      
        modelshort: 
    """

    ierror = 0
    if (number > 0):
        ierror = 1

    print('=================')
    if (ierror == 0):
        print('SIMULATION WITHOUT ERRORS')
    if (ierror == 1):
        print('SIMULATION WITH ERRORS # ' + str(number))

    pathic = '/initcond'
    path = '../outputDATA/' + model + pathic
    os.makedirs(path, exist_ok=True)
    if (ierror == 1):
        pathic = '/initcond/err/' + GCname + '/' + str(number)
        path = '../outputDATA/' + model + pathic
        os.makedirs(path, exist_ok=True)

    xGC, yGC, zGC, vxGC, vyGC, vzGC = inputGC.GCcoord(GCname, gc_frame, ierror,
                                                      path)

    print("current x,y,z,vx,vy,vz")
    print(xGC, yGC, zGC)
    print(vxGC, vyGC, vzGC)
    """
    INTEGRATE THE ORBITS OF THE GC BARYCENTRE BACKWARD IN TIME, TO GET THE "INITIAL CONDITIONS"
    """

    backward = 'YES'
    N = 1

    pathorbits = '/backwardorbits'
    path = '../outputDATA/' + model + pathorbits
    os.makedirs(path, exist_ok=True)
    if (ierror == 1):
        pathorbits = '/backwardorbits/err/' + GCname + '/' + str(number)
        path = '../outputDATA/' + model + pathorbits
        os.makedirs(path, exist_ok=True)

    GCparam = [0., 0.]
    MWparam = inputMW.MW(modelshort)
    integrate.orbits(xGC, yGC, zGC, vxGC, vyGC, vzGC, GCparam, MWparam, model,
                     modelshort, GCname, backward, pathorbits, deltat, Nstep,
                     N)

    xGCini = integrate.xp
    yGCini = integrate.yp
    zGCini = integrate.zp
    vxGCini = integrate.vxp
    vyGCini = integrate.vyp
    vzGCini = integrate.vzp

    print("initial x,y,z,vx,vy,vz")
    print(xGCini, yGCini, zGCini)
    print(vxGCini, vyGCini, vzGCini)
    """                                                                                                        
    READ INITIAL CONDITIONS OF THE N-BODY GC
    """
    x0, y0, z0, vx0, vy0, vz0 = inputGC.Nbody(GCname)

    if (ierror == 1
        ):  # use a sbusample of the N-body GC for integrating with errors
        GCpart = np.arange(0, len(x0))
        idx = np.random.choice(GCpart, Nsample)
        x0 = x0[idx]
        y0 = y0[idx]
        z0 = z0[idx]
        vx0 = vx0[idx]
        vy0 = vy0[idx]
        vz0 = vz0[idx]

    # tutte le particelle, non del centro del amasso
    x0 = x0 + xGCini
    y0 = y0 + yGCini
    z0 = z0 + zGCini
    vx0 = vx0 + vxGCini
    vy0 = vy0 + vyGCini
    vz0 = vz0 + vzGCini

    # now integrate the N-body GC forward in time
    start = time.time()

    backward = 'NO'
    N = len(x0)

    GCparam = inputGC.Plummer(GCname)
    MWparam = inputMW.MW(modelshort)

    integrate.deallocate()
    integrate.orbits(x0, y0, z0, vx0, vy0, vz0, GCparam, MWparam, model,
                     modelshort, GCname, backward, pathorbits, deltat, Nstep,
                     N)

    xf = integrate.xp
    yf = integrate.yp
    zf = integrate.zp
    vxf = integrate.vxp
    vyf = integrate.vyp
    vzf = integrate.vzp
    tesc = integrate.tesc
    phiMW = integrate.phimw
    phiGC = integrate.phigc

    end = time.time()
    print("elapsed time " + str(end - start))

    c1 = coord.SkyCoord(x=xf * u.kpc,
                        y=yf * u.kpc,
                        z=zf * u.kpc,
                        v_x=vxf * 10 * u.km / u.s,
                        v_y=vyf * 10 * u.km / u.s,
                        v_z=vzf * 10 * u.km / u.s,
                        frame=gc_frame)
    gc2 = c1.transform_to(coord.ICRS)

    ra = gc2.ra.value
    dec = gc2.dec.value
    D = gc2.distance.value
    pm_ra_cosdec = gc2.pm_ra_cosdec.value
    pm_dec = gc2.pm_dec.value
    RV = gc2.radial_velocity.value

    gc3 = gc2.transform_to('galactic')
    ll = gc3.l.value
    l = ll
    l[ll > 180] = l[ll > 180] - 360.
    b = gc3.b.value
    pm_l_cosb = gc3.pm_l_cosb.value
    pm_b = gc3.pm_b.value
    """                                                                                                        
    SAVE OUTPUT FILE
    """

    pathorbits = '/streams/'
    if (ierror == 0):
        path = '../outputDATA/' + model + pathorbits
        os.makedirs(path, exist_ok=True)
        hf = h5py.File(path + '/' + GCname + '.h5', 'w')
    if (ierror == 1):
        path = '../outputDATA/' + model + pathorbits + \
            '/err/' + GCname + '/' + str(number)
        os.makedirs(path, exist_ok=True)
        hf = h5py.File(path + '/' + GCname + '.h5', 'w')
    EQ = hf.create_group('equatorial')
    EQ.create_dataset('RA', data=ra)
    EQ.create_dataset('DEC', data=dec)
    EQ.create_dataset('D', data=D)
    EQ.create_dataset('PMRA_COSDEC', data=pm_ra_cosdec)
    EQ.create_dataset('PMDEC', data=pm_dec)
    EQ.create_dataset('RV', data=RV)
    GAL = hf.create_group('galactic')
    GAL.create_dataset('LONG', data=l)
    GAL.create_dataset('LAT', data=b)
    GAL.create_dataset('PML_COSB', data=pm_l_cosb)
    GAL.create_dataset('PMB', data=pm_b)
    GALCEN = hf.create_group('galactocentric')
    GALCEN.create_dataset('X', data=xf)
    GALCEN.create_dataset('Y', data=yf)
    GALCEN.create_dataset('Z', data=zf)
    GALCEN.create_dataset('VX', data=vxf)
    GALCEN.create_dataset('VY', data=vyf)
    GALCEN.create_dataset('VZ', data=vzf)
    ENERGY = hf.create_group('energy')
    ENERGY.create_dataset('Tescape', data=tesc)
    ENERGY.create_dataset('phiMW', data=phiMW)
    ENERGY.create_dataset('phiGC', data=phiGC)

    hf.close()

    integrate.deallocate()

    print("END")
Ejemplo n.º 5
0
def main(GCname):
    """ GOAL is to verify that we get sensical streams before and after... """

    """



    SET UP THE PARAMETERS



    """
    ### PARAMETERS TO CHANGE ####
    # ASSIGN MILKY WAY MODEL
    model = 'PII'
    modelshort = "GCGC"  # add this as another piece of information to add
    # PATH TO OBTAIN THE BACKWARD ORBITS of the GC CENTRE DE MASSE
    pathorbits = "/backwardorbitsGCGC/"
    backward = 'NO'
    pathstreams = "/streamsGCGC/"
    # LIST OF THE GCs. Current GC is omitted,
    fname_GClist = "GClist.txt"
    N = 1
    # ARE WE MAKING THE BIG TABLE WITH POTENTIAL PER TIME ?
    writeGCGC = 0
    # ASSIGN TOTAL NUMBER OF STEP FOR THE ORBIT INTEGRATION AND INTEGRATION TIME-STEP
    Nstep = 50000
    deltat = 0.001
    # ASSIGN PARAMETERS FOR MONTE-CARLO SIMULATIONS ON ERRORS
    Nsample = 10000
    n_trials = 1
    # ARE WE SAVING THE TIME STAMPS FOR THE MOVIE?
    isave = 100     # set to zero to not save the bin files !
    # PATH TO GC ORBITS
    path = '../outputDATA/' + model + pathorbits
    ierror = 0
    # INFO TO GET SUBHALOS
    pathSH = "../outputData/" + model + "/backwardorbitsOG/"
    # GALACTIC REFERENCE FRAME
    gc_frame = inputMW.ref_frame()
    # GET GALACTIC PARAMETERS
    MWparam = inputMW.MW(model)
    # INFORMATION ON GCs of DARKMATTER HALOS
    # must be initialized at zero for for orbits
    nhalos = 0
    nameSH = np.empty(0, dtype='c').T
    bgGCParams = np.empty(0)

    """


    OPEN THE INITIAL CONDITIONS FROM THE DATA FILES
        get the initial conditions of the target GC
        get initial confitions of the particles in the Target GC
        get the path information of the other GC/subhalos if they are being used


    """
    xGC, yGC, zGC, vxGC, vyGC, vzGC = inputGC.GCcoord(GCname, gc_frame, ierror)
    # GET THE INITAL COORDINATE FROM THE LAST LINE OF THE FILE
    with open(path + "orbit" + GCname + ".dat", 'r') as f:
        for line in f:
            pass
        last_line = line
    num = last_line.split()
    xGCini, yGCini, zGCini, vxGCini, vyGCini, vzGCini = \
        float(num[1]), float(num[2]), float(num[3]), \
        float(num[4]), float(num[5]), float(num[6])

    print("initial x,y,z,vx,vy,vz")
    print(xGCini, yGCini, zGCini)
    print(vxGCini, vyGCini, vzGCini)
    # GET THE INITIAL POSITIONS OF THE PARTICLES
    x0, y0, z0, vx0, vy0, vz0 = inputGC.Nbody(GCname)
    # PUT IN REFERENCE FRAME OF THE GALAXY
    x0 = x0 + xGCini
    y0 = y0 + yGCini
    z0 = z0 + zGCini
    vx0 = vx0 + vxGCini
    vy0 = vy0 + vyGCini
    vz0 = vz0 + vzGCini
    backward = "NO"
    N = len(x0)
    # Get the PARAMETERS OF THE SUBHALOS
    if not os.path.isfile(fname_GClist):
        GCparam = inputGC.Plummer(GCname)
        print(fname_GClist, "is empty. Using", nhalos, "dark matter subhalos")
    else:
        GCparam, bgGCParams, bgGCNames = manage_GC_data(
            GCname, fname_GClist, pathSH)
        nhalos = len(bgGCNames)
        res = max(bgGCNames, key=len)
        len_str = len(res)
        # this adds as many blank spaces to the right as necessary to have all elements in stringsSH as long as the longest one
        bgGCNames = [bgGCNames[i].ljust(len_str) for i in range(0, nhalos)]
        nameSH = np.array(bgGCNames, dtype='c').T
        print("USING", nhalos, "DARK MATTER HALOS")

    # MAKE FOLDER FOR SAVING THE MOVE
    if isave != 0:
        outdir = "../outputDATA4movies/" + model + "/" + modelshort + "/" + GCname
        if os.path.isdir(outdir):
            dircontents = os.listdir(outdir)
            if len(dircontents) > 0:
                print("")
                print("WARNING ! You might be over writing the movies for the film! ")
                print("The", outdir, "contains")
                print(dircontents)
                print("")
        else:
            os.makedirs(outdir)

    """



    CALCULATE THE ORBITS




    """
    start_time = time.time()
    print("starting integrale")
    integrate.orbits(x0, y0, z0, vx0, vy0, vz0, GCparam, MWparam, model, modelshort,
                     GCname, backward, pathorbits, deltat,
                     Nstep, pathSH, nameSH, bgGCParams, writeGCGC, isave)

    end_time = time.time()
    c_time = end_time - start_time
    minutes = np.floor(c_time / 60)
    seconds = c_time % 60
    print("COMPUTATION TIME: {:.0f} min {:.0f} s".format(minutes, seconds))

    """



        WRITE OUT THE STREAM FINAL COORDINATES




    """

    # get the final coordaintes of the particles
    xf = integrate.xp
    yf = integrate.yp
    zf = integrate.zp
    vxf = integrate.vxp
    vyf = integrate.vyp
    vzf = integrate.vzp
    tesc = integrate.tesc
    phiMW = integrate.phimw
    phiGC = integrate.phigc
    # GET OTHER COORDINATES
    c1 = coord.SkyCoord(x=xf * u.kpc, y=yf * u.kpc, z=zf * u.kpc, v_x=vxf * 10 *
                        u.km / u.s, v_y=vyf * 10 * u.km / u.s, v_z=vzf * 10 * u.km / u.s, frame=gc_frame)
    gc2 = c1.transform_to(coord.ICRS)
    # prepare RA and DEC
    ra = gc2.ra.value
    dec = gc2.dec.value
    D = gc2.distance.value
    pm_ra_cosdec = gc2.pm_ra_cosdec.value
    pm_dec = gc2.pm_dec.value
    RV = gc2.radial_velocity.value
    # prepare galactic longitude and latitude
    gc3 = gc2.transform_to('galactic')
    ll = gc3.l.value
    l = ll
    l[ll > 180] = l[ll > 180] - 360.
    b = gc3.b.value
    pm_l_cosb = gc3.pm_l_cosb.value
    pm_b = gc3.pm_b.value
    # SAVE OUTPUT DATA
    if (ierror == 0):
        path = '../outputData/' + model + pathstreams
        os.makedirs(path, exist_ok=True)
        hf = h5py.File(path + "/" + GCname + '.h5', 'w')
    # parameters
    EQ = hf.create_group('equatorial')
    EQ.create_dataset('RA', data=ra)
    EQ.create_dataset('DEC', data=dec)
    EQ.create_dataset('D', data=D)
    EQ.create_dataset('PMRA_COSDEC', data=pm_ra_cosdec)
    EQ.create_dataset('PMDEC', data=pm_dec)
    EQ.create_dataset('RV', data=RV)
    GAL = hf.create_group('galactic')
    GAL.create_dataset('LONG', data=l)
    GAL.create_dataset('LAT', data=b)
    GAL.create_dataset('PML_COSB', data=pm_l_cosb)
    GAL.create_dataset('PMB', data=pm_b)
    GALCEN = hf.create_group('galactocentric')
    GALCEN.create_dataset('X', data=xf)
    GALCEN.create_dataset('Y', data=yf)
    GALCEN.create_dataset('Z', data=zf)
    GALCEN.create_dataset('VX', data=vxf)
    GALCEN.create_dataset('VY', data=vyf)
    GALCEN.create_dataset('VZ', data=vzf)
    ENERGY = hf.create_group('energy')
    ENERGY.create_dataset('Tescape', data=tesc)
    ENERGY.create_dataset('phiMW', data=phiMW)
    ENERGY.create_dataset('phiGC', data=phiGC)
    # finish
    hf.close()
    integrate.deallocate()
    print("END")
Ejemplo n.º 6
0
def main(GCname):
    """ Integrate backward in time """
    """ 
    
    
    SET THE PARAMETERS AND PATH INFORMATION 
    
    
    """
    model = 'PII'
    modelshort = model
    # SET OUTPUT PATH OF WHERE TO SAVE THE BACKWARDORBITS
    pathorbits = "/backwardorbitsGCGC/"
    backward = 'YES'
    # GET THE POTENTIAL OF THE INTERACTIONS OF EACH GCGC
    writeGCGC = "YES"
    # LIST OF THE GCs. Current GC is omitted,
    fname_GClist = "GClist.txt"
    N = 1
    # ASSIGN TOTAL NUMBER OF STEP FOR THE ORBIT INTEGRATION AND INTEGRATION TIME-STEP
    Nstep = 50000
    deltat = 0.001
    # ASSIGN PARAMETERS FOR MONTE-CARLO SIMULATIONS ON ERRORS
    Nsample = 10000
    n_trials = 1
    # PATH TO GC ORBITS
    path = '../outputDATA/' + model + pathorbits
    ierror = 0
    # INFO TO GET SUBHALOS
    pathSH = "../outputData/" + model + "/backwardorbitsOG/"
    # GALACTIC REFERENCE FRAME
    gc_frame = inputMW.ref_frame()
    # GET GALACTIC PARAMETERS
    MWparam = inputMW.MW(modelshort)
    # GET THE GC initial conditions
    xGC, yGC, zGC, vxGC, vyGC, vzGC = inputGC.GCcoord(GCname, gc_frame, ierror)

    # the parameters of each
    if not os.path.isfile(fname_GClist):
        GCparam = inputGC.Plummer(GCname)
        nhalos = 0
        len_str = 2
        nameSH = np.empty(0, dtype='c').T
        bgGCParams = np.empty(0)
        print(fname_GClist, "is empty. Using", nhalos, "dark matter subhalos")
    else:
        GCparam, bgGCParams, bgGCNames = manage_GC_data(
            GCname, fname_GClist, pathSH)
        nhalos = len(bgGCNames)
        res = max(bgGCNames, key=len)
        len_str = len(res)
        # this adds as many blank spaces to the right as necessary to have all elements in stringsSH as long as the longest one
        bgGCNames = [bgGCNames[i].ljust(len_str) for i in range(0, nhalos)]
        nameSH = np.array(bgGCNames, dtype='c').T

    os.makedirs(path, exist_ok=True)

    integrate.orbits(xGC, yGC, zGC, vxGC, vyGC, vzGC, GCparam, MWparam, model,
                     modelshort, GCname, backward, pathorbits, deltat,
                     int(Nstep), pathSH, nameSH, bgGCParams, writeGCGC)

    # orbits(x,y,z,vx,vy,vz,GCparam,MWparam,
    # model,modelshort,GCname,backward,pathorbits,deltat,
    # Nstep,N,Nhalos,len_str,pathSH,nameSH)
    # No need to #DEFINE N don't need to be passed from python,
    # though they need to be declared in fortran
    #  But since the arrays are being passed from python, we don't need this variable that declares size
    # same thing with NHalos, since it is impiled with nameSH
    # no need to pass len_str either

    # I don't think we need this code below ?
    xGCini = integrate.xp
    yGCini = integrate.yp
    zGCini = integrate.zp
    vxGCini = integrate.vxp
    vyGCini = integrate.vyp
    vzGCini = integrate.vzp