zh2 = 2.8
mu_h2 = yt.YTArray(zh2 * u.Da.to(u.g), 'g')

# Problem setup: pure density field
sz = 16
max_rad = 10000 * u.au
rbreak = 1000 * u.au
zz, yy, xx = np.indices([sz, sz, sz]) * max_rad / (sz / 2.)
mid = max_rad * (sz - 1.) / sz
rr = ((zz - mid)**2 + (yy - mid)**2 + (xx - mid)**2)**0.5
max_velo = 2 * u.km / u.s
velo = max_velo - u.Quantity([mid - zz, mid - yy, mid - xx
                              ]) / rr.max() * max_velo

# now rr has units
dens = broken_powerlaw(rr, rbreak=rbreak, n0=5e8 * u.cm**-3, power=-1.5)

data = {
    'density': ((dens * u.Da * zh2).to(u.g / u.cm**3), "g/cm**3"),
    'z_velocity': (velo[0].to(u.km / u.s).value, 'km/s'),
    'y_velocity': (velo[1].to(u.km / u.s).value, 'km/s'),
    'x_velocity': (velo[2].to(u.km / u.s).value, 'km/s'),
}
bbox = np.array([[-max_rad.value, max_rad.value]] * 3)
ds = yt.load_uniform_grid(data,
                          dens.shape,
                          length_unit="au",
                          bbox=bbox,
                          nprocs=64)

dust_to_gas = 0.01
zh2 = 2.8
mu_h2 = yt.YTArray(zh2 * u.Da.to(u.g), 'g')


# Problem setup: pure density field
sz = 16
max_rad = 10000*u.au
rbreak = 1000*u.au
zz,yy,xx = np.indices([sz,sz,sz]) * max_rad / (sz/2.)
mid = max_rad * (sz-1.)/sz
rr = ((zz-mid)**2 + (yy-mid)**2 + (xx-mid)**2)**0.5
max_velo = 2*u.km/u.s
velo = max_velo - u.Quantity([mid-zz, mid-yy, mid-xx]) / rr.max() * max_velo

# now rr has units
dens = broken_powerlaw(rr, rbreak=rbreak, n0=5e8*u.cm**-3, power=-1.5)

data = {'density': ((dens*u.Da*zh2).to(u.g/u.cm**3), "g/cm**3"),
        'z_velocity': (velo[0].to(u.km/u.s).value, 'km/s'),
        'y_velocity': (velo[1].to(u.km/u.s).value, 'km/s'),
        'x_velocity': (velo[2].to(u.km/u.s).value, 'km/s'),
       }
bbox = np.array([[-max_rad.value,max_rad.value]]*3)
ds = yt.load_uniform_grid(data, dens.shape, length_unit="au", bbox=bbox, nprocs=64)

dust_to_gas = 0.01
def _DustDensity(field, data):
    return dust_to_gas * data['density']
ds.add_field(("gas", "dust_density"), function=_DustDensity, units="g/cm**3")

def _NumberDensityCH3OH(field, data):
def core_model_dust(outname, x_co=1.0e-4, x_h2co=1.0e-9, x_ch3oh=1e-9, zh2=2.8,
                    sz=16, max_rad=10000*u.au, rbreak=1000*u.au,
                    radius_cm=1*u.au.to(u.cm), mass_g=1*u.M_sun.to(u.g),
                    n0=5e8*u.cm**-3,
                    power=-1.5,
                    recompute_dusttemperature=True,
                    luminosity=2e4*u.L_sun,):

    if not os.path.exists('dustkappa_mrn5.inp'):
        get_dust_opacity()


    mu_h2 = yt.YTArray(zh2 * u.Da.to(u.g), 'g')


    # Problem setup: pure density field
    zz,yy,xx = np.indices([sz,sz,sz])
    rr = ((zz-(sz-1)/2.)**2 + (yy-(sz-1)/2.)**2 + (xx-(sz-1)/2.)**2)**0.5
    max_velo = 1*u.km/u.s
    velo = max_velo - np.array([(sz-1)/2.-zz, (sz-1/2.)-yy, (sz-1/2.)-xx]) / rr.max() * max_velo

    # now rr has units
    rr = rr * max_rad / (sz/2.)
    dens = broken_powerlaw(rr, rbreak=rbreak, n0=n0, power=power)

    data = {('gas','density'): ((dens*u.Da*zh2).to(u.g/u.cm**3), "g/cm**3"),
            ('gas','z_velocity'): (velo[0].to(u.km/u.s).value, 'km/s'),
            ('gas','y_velocity'): (velo[1].to(u.km/u.s).value, 'km/s'),
            ('gas','x_velocity'): (velo[2].to(u.km/u.s).value, 'km/s'),
           }
    bbox = np.array([[-max_rad.value,max_rad.value]]*3)
    ds = yt.load_uniform_grid(data, dens.shape, length_unit="au", bbox=bbox, nprocs=64)

    dust_to_gas = 0.01
    def _DustDensity(field, data):
        return dust_to_gas * data['density']
    ds.add_field(("gas", "dust_density"), function=_DustDensity, units="g/cm**3")

    def _NumberDensityH2(field, data):
        return (1./mu_h2)*data['density'] # data['density']#
    ds.add_field(("gas", "number_density_H2"), function=_NumberDensityH2, units="cm**-3")

    def _GasTemperature(field, data):
        return yt.YTArray(np.ones_like(data['density'])*100., 'K')
    ds.add_field(("gas", "temperature"), function=_GasTemperature, units='K')

    def _DustTemperature(field, data):
        return yt.YTArray(np.ones_like(data['density'])*100., 'K')
    ds.add_field(("gas", "dust_temperature"), function=_DustTemperature, units='K')

    writer = RadMC3DWriter(ds)

    writer.write_amr_grid()
    dens_fn = "numberdens_h2.inp" # not h2_numberdens.inp?
    writer.write_line_file(("gas", "number_density_H2"), dens_fn)
    writer.write_dust_file(("gas", "temperature"), "gas_temperature.inp")
    writer.write_dust_file(("gas", "dust_density"), "dust_density.inp")
    #writer.write_dust_file(("gas", "dust_temperature"), "dust_temperature.inp")

    writer.write_line_file([('gas','x_velocity'), ('gas','y_velocity'),
                            ('gas','z_velocity')], "gas_velocity.inp")

    # central star
    position_cm = [0.0, 0.0, 0.0]
    temperature_K = 1000.0
    temperature_K = ((luminosity /
                      (4 * np.pi * (radius_cm*u.cm)**2 * constants.sigma_sb))**0.25
                    ).to(u.K).value
    star = RadMC3DSource(radius_cm, mass_g, position_cm, temperature_K)

    sources_list = [star]
    wavelengths_micron = np.logspace(-1.0, 4.0, 1000)

    writer.write_source_files(sources_list, wavelengths_micron)


    get_dust_opacity()

    params=dict(istar_sphere=0, itempdecoup=0, lines_mode=3, nphot=1000000,
                nphot_scat=30000, nphot_spec=100000, rto_style=3,
                scattering_mode=0, scattering_mode_max=0, tgas_eq_tdust=1,)

    params_string = """
    istar_sphere = {istar_sphere}
    itempdecoup = {itempdecoup}
    lines_mode = {lines_mode}
    nphot = {nphot}
    nphot_scat = {nphot_scat}
    nphot_spec = {nphot_spec}
    rto_style = {rto_style}
    scattering_mode = {scattering_mode}
    scattering_mode_max = {scattering_mode_max}
    tgas_eq_tdust = {tgas_eq_tdust}
    """

    with open('wavelength_micron.inp', 'w') as fh:
        fh.write("{0}\n".format(len(wavelengths_micron)))
        for nu in wavelengths_micron:
            fh.write("{0}\n".format(nu))

    # with open('stars.inp', 'w') as fh:
    #     fh.write("2\n")
    #     nstars = 1
    #     nlam = nfrq
    #     fh.write("{0} {1}\n".format(nstars,nlam))
    #     rstar = (1*u.au).to(u.cm).value
    #     mstar = 15*u.M_sun.to(u.g)
    #     x,y,z = 0,0,0
    #     fh.write("{0} {1} {2} {3} {4}\n".format(rstar, mstar, x, y, z))
    #     for nu in wavelengths_micron:
    #         fh.write("{0}\n".format(nu))
    #     temperature = 1000 * u.K
    #     for nu in wavelengths_micron:
    #         fh.write("{0}\n".format(-temperature.to(u.K).value))


    with open('radmc3d.inp','w') as f:
        params['lines_mode'] = 1 # 3 = sobolev (LVG)
        f.write(params_string.format(**params))

    if recompute_dusttemperature:
        # compute the dust temperature
        assert os.system('radmc3d mctherm') == 0

        dust_temperature = read_dust_temperature('dust_temperature.bdat', sz=sz)
        shutil.copy('dust_temperature.bdat','dust_temperature_{0}.bdat'.format(outname))
    else:
        try:
            shutil.copy('dust_temperature_{0}.bdat'.format(outname),'dust_temperature.bdat',)
        except Exception as ex:
            print(ex)
        dust_temperature = read_dust_temperature('dust_temperature.bdat', sz=sz)

    if os.path.exists('lines.inp'):
        os.remove('lines.inp')
    assert os.system('radmc3d image npix 50 incl 0 sizeau 10000 noscat pointau 0.0  0.0  0.0 fluxcons lambda 1323 dpc 5400') == 0
    im = radmc3dPy.image.readImage('image.out')
    im.writeFits('dustim1323um_{0}.fits'.format(outname), fitsheadkeys={}, dpc=5400,
                 coord='19h23m43.963s +14d30m34.56s', overwrite=True)

    return dust_temperature
Example #4
0
def core_model_dust(
    outname,
    x_co=1.0e-4,
    x_h2co=1.0e-9,
    x_ch3oh=1e-9,
    zh2=2.8,
    sz=16,
    max_rad=10000 * u.au,
    rbreak=1000 * u.au,
    radius_cm=1 * u.au.to(u.cm),
    mass_g=1 * u.M_sun.to(u.g),
    n0=5e8 * u.cm**-3,
    power=-1.5,
    recompute_dusttemperature=True,
    luminosity=2e4 * u.L_sun,
):

    if not os.path.exists('dustkappa_mrn5.inp'):
        get_dust_opacity()

    mu_h2 = yt.YTArray(zh2 * u.Da.to(u.g), 'g')

    # Problem setup: pure density field
    zz, yy, xx = np.indices([sz, sz, sz])
    rr = ((zz - (sz - 1) / 2.)**2 + (yy - (sz - 1) / 2.)**2 +
          (xx - (sz - 1) / 2.)**2)**0.5
    max_velo = 1 * u.km / u.s
    velo = max_velo - np.array([(sz - 1) / 2. - zz, (sz - 1 / 2.) - yy,
                                (sz - 1 / 2.) - xx]) / rr.max() * max_velo

    # now rr has units
    rr = rr * max_rad / (sz / 2.)
    dens = broken_powerlaw(rr, rbreak=rbreak, n0=n0, power=power)

    data = {
        ('gas', 'density'): ((dens * u.Da * zh2).to(u.g / u.cm**3), "g/cm**3"),
        ('gas', 'z_velocity'): (velo[0].to(u.km / u.s).value, 'km/s'),
        ('gas', 'y_velocity'): (velo[1].to(u.km / u.s).value, 'km/s'),
        ('gas', 'x_velocity'): (velo[2].to(u.km / u.s).value, 'km/s'),
    }
    bbox = np.array([[-max_rad.value, max_rad.value]] * 3)
    ds = yt.load_uniform_grid(data,
                              dens.shape,
                              length_unit="au",
                              bbox=bbox,
                              nprocs=64)

    dust_to_gas = 0.01

    def _DustDensity(field, data):
        return dust_to_gas * data['density']

    ds.add_field(("gas", "dust_density"),
                 function=_DustDensity,
                 units="g/cm**3")

    def _NumberDensityH2(field, data):
        return (1. / mu_h2) * data['density']  # data['density']#

    ds.add_field(("gas", "number_density_H2"),
                 function=_NumberDensityH2,
                 units="cm**-3")

    def _GasTemperature(field, data):
        return yt.YTArray(np.ones_like(data['density']) * 100., 'K')

    ds.add_field(("gas", "temperature"), function=_GasTemperature, units='K')

    def _DustTemperature(field, data):
        return yt.YTArray(np.ones_like(data['density']) * 100., 'K')

    ds.add_field(("gas", "dust_temperature"),
                 function=_DustTemperature,
                 units='K')

    writer = RadMC3DWriter(ds)

    writer.write_amr_grid()
    dens_fn = "numberdens_h2.inp"  # not h2_numberdens.inp?
    writer.write_line_file(("gas", "number_density_H2"), dens_fn)
    writer.write_dust_file(("gas", "temperature"), "gas_temperature.inp")
    writer.write_dust_file(("gas", "dust_density"), "dust_density.inp")
    #writer.write_dust_file(("gas", "dust_temperature"), "dust_temperature.inp")

    writer.write_line_file([('gas', 'x_velocity'), ('gas', 'y_velocity'),
                            ('gas', 'z_velocity')], "gas_velocity.inp")

    # central star
    position_cm = [0.0, 0.0, 0.0]
    temperature_K = 1000.0
    temperature_K = ((luminosity /
                      (4 * np.pi *
                       (radius_cm * u.cm)**2 * constants.sigma_sb))**0.25).to(
                           u.K).value
    star = RadMC3DSource(radius_cm, mass_g, position_cm, temperature_K)

    sources_list = [star]
    wavelengths_micron = np.logspace(-1.0, 4.0, 1000)

    writer.write_source_files(sources_list, wavelengths_micron)

    get_dust_opacity()

    params = dict(
        istar_sphere=0,
        itempdecoup=0,
        lines_mode=3,
        nphot=10000,
        nphot_scat=3000,
        nphot_spec=10000,
        rto_style=3,
        scattering_mode=0,
        scattering_mode_max=0,
        tgas_eq_tdust=1,
    )

    params_string = """
    istar_sphere = {istar_sphere}
    itempdecoup = {itempdecoup}
    lines_mode = {lines_mode}
    nphot = {nphot}
    nphot_scat = {nphot_scat}
    nphot_spec = {nphot_spec}
    rto_style = {rto_style}
    scattering_mode = {scattering_mode}
    scattering_mode_max = {scattering_mode_max}
    tgas_eq_tdust = {tgas_eq_tdust}
    """

    with open('wavelength_micron.inp', 'w') as fh:
        fh.write("{0}\n".format(len(wavelengths_micron)))
        for nu in wavelengths_micron:
            fh.write("{0}\n".format(nu))

    # with open('stars.inp', 'w') as fh:
    #     fh.write("2\n")
    #     nstars = 1
    #     nlam = nfrq
    #     fh.write("{0} {1}\n".format(nstars,nlam))
    #     rstar = (1*u.au).to(u.cm).value
    #     mstar = 15*u.M_sun.to(u.g)
    #     x,y,z = 0,0,0
    #     fh.write("{0} {1} {2} {3} {4}\n".format(rstar, mstar, x, y, z))
    #     for nu in wavelengths_micron:
    #         fh.write("{0}\n".format(nu))
    #     temperature = 1000 * u.K
    #     for nu in wavelengths_micron:
    #         fh.write("{0}\n".format(-temperature.to(u.K).value))

    with open('radmc3d.inp', 'w') as f:
        params['lines_mode'] = 1  # 3 = sobolev (LVG)
        f.write(params_string.format(**params))

    if recompute_dusttemperature:
        # compute the dust temperature
        assert os.system('radmc3d mctherm') == 0

        dust_temperature = read_dust_temperature('dust_temperature.bdat',
                                                 sz=sz)
        shutil.move('dust_temperature.bdat',
                    'dust_temperature_{0}.bdat'.format(outname))
    else:
        try:
            shutil.copy(
                'dust_temperature_{0}.bdat'.format(outname),
                'dust_temperature.bdat',
            )
            success = True
        except Exception as ex:
            success = False
            print(ex)
        dust_temperature = read_dust_temperature('dust_temperature.bdat',
                                                 sz=sz)
        if success:
            os.remove('dust_temperature.bdat')

    if os.path.exists('lines.inp'):
        os.remove('lines.inp')
    assert os.system(
        'radmc3d image npix 50 incl 0 sizeau 10000 noscat pointau 0.0  0.0  0.0 fluxcons lambda 1323 dpc 5400'
    ) == 0
    im = radmc3dPy.image.readImage('image.out')
    im.writeFits('dustim1323um_{0}.fits'.format(outname),
                 fitsheadkeys={},
                 dpc=5400,
                 coord='19h23m43.963s +14d30m34.56s',
                 overwrite=True)

    return dust_temperature