def time_test_imf(massLimits, imfSlopes, totalMass):
    tstart = datetime.now()
    res = imf.sample_imf(massLimits, imfSlopes, totalMass, makeMultiples=False)
    m = res[0]
    tend = datetime.now()

    print 'N stars = {0:d}  Mass = {1:.0f} Msun'.format(len(m), np.sum(m))

    dt = tend - tstart
    print dt

    return m
Пример #2
0
def model_young_cluster(iso, makeMultiples=True):
    c = constants

    massLimits = np.array([0.1, 0.5, 150])
    imfSlopes = np.array([-1.3, -2.3])
    clusterMass = 1e4
    MFamp = 0.44
    MFindex = 0.51
    CSFamp = 0.50
    CSFindex = 0.45
    CSFmax = 3
    qMin = 0.01
    qIndex = -0.4

    # Sample a power-law IMF randomly
    results = imf.sample_imf(massLimits, imfSlopes, clusterMass,
                             makeMultiples=makeMultiples,
                             multiMFamp=MFamp, multiMFindex=MFindex,
                             multiCSFamp=CSFamp, multiCSFindex=CSFindex,
                             multiCSFmax=CSFmax,
                             multiQmin=qMin, multiQindex=qIndex)

    mass = results[0]
    isMultiple = results[1]
    compMasses = results[2]
    systemMasses = results[3]

    mag127m = np.zeros(len(mass), dtype=float)
    mag139m = np.zeros(len(mass), dtype=float)
    mag153m = np.zeros(len(mass), dtype=float)
    magJ = np.zeros(len(mass), dtype=float)
    magH = np.zeros(len(mass), dtype=float)
    magKp = np.zeros(len(mass), dtype=float)
    magLp = np.zeros(len(mass), dtype=float)
    temp = np.zeros(len(mass), dtype=float)
    isWR = np.zeros(len(mass), dtype=bool)

    def match_model_mass(theMass):
        dm = np.abs(iso.M - theMass)
        mdx = dm.argmin()

        # Model mass has to be within 2% of the desired mass
        if (dm[mdx] / theMass) > 0.1:
            return None
        else:
            return mdx

    def combine_mag(mag1, mdx_cc, iso_mag):
        f1 = 10**(-mag1 / 2.5)
        f2 = 10**(-iso_mag[mdx_cc] / 2.5)
        new_mag = -2.5 * np.log10(f1 + f2)
        return new_mag

    for ii in range(len(mass)):
        # Find the closest model mass (returns None, if nothing with dm = 0.1
        mdx = match_model_mass(mass[ii])
        if mdx == None:
            continue

        mag127m[ii] = iso.mag127m[mdx]
        mag139m[ii] = iso.mag139m[mdx]
        mag153m[ii] = iso.mag153m[mdx]
        magJ[ii] = iso.magJ[mdx]
        magH[ii] = iso.magH[mdx]
        magKp[ii] = iso.magKp[mdx]
        magLp[ii] = iso.magLp[mdx]
        temp[ii] = iso.T[mdx]
        isWR[ii] = iso.isWR[mdx]

        # Determine if this system is a binary.
        if isMultiple[ii]:
            n_stars = len(compMasses[ii])
            for cc in range(n_stars):
                mdx_cc = match_model_mass(compMasses[ii][cc])
                if mdx_cc != None:
                    mag127m[ii] = combine_mag(mag127m[ii], mdx_cc, iso.mag127m)
                    mag139m[ii] = combine_mag(mag139m[ii], mdx_cc, iso.mag139m)
                    mag153m[ii] = combine_mag(mag153m[ii], mdx_cc, iso.mag153m)
                    magJ[ii] = combine_mag(magJ[ii], mdx_cc, iso.magJ)
                    magH[ii] = combine_mag(magH[ii], mdx_cc, iso.magH)
                    magKp[ii] = combine_mag(magKp[ii], mdx_cc, iso.magKp)
                    magLp[ii] = combine_mag(magLp[ii], mdx_cc, iso.magLp)
                else:
                    print 'Rejected a companion %.2f' % compMasses[ii][cc]
        

    # Get rid of the bad ones
    idx = np.where(temp != 0)[0]
    cdx = np.where(temp == 0)[0]

    if len(cdx) > 0 and verbose:
        print 'Found %d stars out of mass range: Minimum bad mass = %.1f' % \
            (len(cdx), mass[cdx].min())

    mass = mass[idx]
    mag127m = mag127m[idx]
    mag139m = mag139m[idx]
    mag153m = mag153m[idx]
    magJ = magJ[idx]
    magH = magH[idx]
    magKp = magKp[idx]
    magLp = magLp[idx]
    temp = temp[idx]
    isWR = isWR[idx]
    isMultiple = isMultiple[idx]
    systemMasses = systemMasses[idx]
    if makeMultiples:
        compMasses = [compMasses[ii] for ii in idx]
    idx_noWR = np.where(isWR == False)[0]

    mag127m_noWR = mag127m[idx_noWR]
    mag139m_noWR = mag139m[idx_noWR]
    mag153m_noWR = mag153m[idx_noWR]
    magJ_noWR = magJ[idx_noWR]
    magH_noWR = magH[idx_noWR]
    magKp_noWR = magKp[idx_noWR]
    magLp_noWR = magLp[idx_noWR]
    num_WR = len(mag127m) - len(idx_noWR)

    cluster = dataUtil.DataHolder()
    cluster.mass = mass
    cluster.Teff = temp
    cluster.isWR = isWR
    cluster.mag127m = mag127m
    cluster.mag139m = mag139m
    cluster.mag153m = mag153m
    cluster.magJ = magJ
    cluster.magH = magH
    cluster.magKp = magKp
    cluster.magLp = magLp
    cluster.isMultiple = isMultiple
    cluster.compMasses = compMasses
    cluster.systemMasses = systemMasses

    cluster.idx_noWR = idx_noWR
    cluster.mag127m_noWR = mag127m_noWR
    cluster.mag139m_noWR = mag139m_noWR
    cluster.mag153m_noWR = mag153m_noWR
    cluster.magJ_noWR = magJ_noWR
    cluster.magH_noWR = magH_noWR
    cluster.magKp_noWR = magKp_noWR
    cluster.magLp_noWR = magLp_noWR
    cluster.num_WR = num_WR

    # Summary parameters
    cluster.massLimits = massLimits
    cluster.imfSlopes = imfSlopes
    cluster.sumIMFmass = clusterMass
    cluster.makeMultiples = makeMultiples
    cluster.MFamp = MFamp
    cluster.MFindex = MFindex
    cluster.CSFamp = CSFamp
    cluster.CSFindex = CSFindex
    cluster.CSFmax = CSFmax
    cluster.qMin = qMin
    cluster.qIndex = qIndex
            
    return cluster