def model(visibilities, params, parameters, plot=False):

    # Set the values of all of the parameters.

    p = {}
    for key in parameters:
        if parameters[key]["fixed"]:
            if parameters[key]["value"] in parameters.keys():
                if parameters[parameters[key]["value"]]["fixed"]:
                    value = parameters[parameters[key]["value"]]["value"]
                else:
                    value = params[parameters[key]["value"]]
            else:
                value = parameters[key]["value"]
        else:
            value = params[key]

        if key[0:3] == "log":
            p[key[3:]] = 10.**value
        else:
            p[key] = value

    # Make sure alpha and beta are defined.

    if p["disk_type"] == "exptaper":
        t_rdisk = p["T0"] * (p["R_disk"] / 1.)**-p["q"]
        p["h_0"] = ((k_b*(p["R_disk"]*AU)**3*t_rdisk) / (G*p["M_star"]*M_sun * \
                2.37*m_p))**0.5 / AU
    else:
        p["h_0"] = ((k_b * AU**3 * p["T0"]) / (G*p["M_star"]*M_sun * \
                2.37*m_p))**0.5 / AU
    p["beta"] = 0.5 * (3 - p["q"])
    p["alpha"] = p["gamma"] + p["beta"]

    # Set up the dust.

    dustopac = "pollack_new.hdf5"

    dust_gen = dust.DustGenerator(dust.__path__[0] + "/data/" + dustopac)

    ddust = dust_gen(p["a_max"] / 1e4, p["p"])
    edust = dust_gen(1.0e-4, 3.5)

    # Set up the gas.

    gases = []
    abundance = []

    index = 1
    while index > 0:
        if "gas_file" + str(index) in p:
            g = gas.Gas()
            g.set_properties_from_lambda(gas.__path__[0]+"/data/"+\
                    p["gas_file"+str(index)])

            gases.append(g)
            abundance.append(p["abundance" + str(index)])

            index += 1
        else:
            index = -1

    # Make sure we are in a temp directory to not overwrite anything.

    original_dir = os.environ["PWD"]
    os.mkdir("/tmp/temp_{1:s}_{0:d}".format(comm.Get_rank(), source))
    os.chdir("/tmp/temp_{1:s}_{0:d}".format(comm.Get_rank(), source))

    # Write the parameters to a text file so it is easy to keep track of them.

    f = open("params.txt", "w")
    for key in p:
        f.write("{0:s} = {1}\n".format(key, p[key]))
    f.close()

    # Set up the model.

    m = modeling.YSOModel()
    m.add_star(mass=p["M_star"],
               luminosity=p["L_star"],
               temperature=p["T_star"])

    if p["envelope_type"] == "ulrich":
        m.set_spherical_grid(p["R_in"], p["R_env"], 100, 51, 2, code="radmc3d")
    else:
        m.set_spherical_grid(p["R_in"], max(5*p["R_disk"],300), 100, 51, 2, \
                code="radmc3d")

    if p["disk_type"] == "exptaper":
        m.add_pringle_disk(mass=p["M_disk"], rmin=p["R_in"], rmax=p["R_disk"], \
                plrho=p["alpha"], h0=p["h_0"], plh=p["beta"], dust=ddust, \
                t0=p["T0"], plt=p["q"], gas=gases, abundance=abundance,\
                aturb=p["a_turb"])
    else:
        m.add_disk(mass=p["M_disk"], rmin=p["R_in"], rmax=p["R_disk"], \
                plrho=p["alpha"], h0=p["h_0"], plh=p["beta"], dust=ddust, \
                t0=p["T0"], plt=p["q"], gas=gases, abundance=abundance,\
                aturb=p["a_turb"])

    if p["envelope_type"] == "ulrich":
        m.add_ulrich_envelope(mass=p["M_env"], rmin=p["R_in"], rmax=p["R_env"],\
                cavpl=p["ksi"], cavrfact=p["f_cav"], dust=edust, \
                t0=p["T0_env"], tpl=p["q_env"], gas=gases, abundance=abundance,\
                aturb=p["a_turb_env"])
    else:
        pass

    m.grid.set_wavelength_grid(0.1, 1.0e5, 500, log=True)

    # Run the images/visibilities/SEDs.

    for j in range(len(visibilities["file"])):
        # Shift the wavelengths by the velocities.

        b = p["v_sys"] * 1.0e5 / c
        lam = c / visibilities["data"][j].freq / 1.0e-4
        wave = lam * numpy.sqrt((1. - b) / (1. + b))

        # Set the wavelengths for RADMC3D to use.

        m.set_camera_wavelength(wave)

        if p["docontsub"]:
            m.run_image(name=visibilities["lam"][j], nphot=1e5, \
                    npix=visibilities["npix"][j], lam=None, \
                    pixelsize=visibilities["pixelsize"][j], tgas_eq_tdust=True,\
                    scattering_mode_max=0, incl_dust=True, incl_lines=True, \
                    loadlambda=True, incl=p["i"], pa=p["pa"], dpc=p["dpc"], \
                    code="radmc3d", verbose=False, writeimage_unformatted=True,\
                    setthreads=ncpus)

            m.run_image(name="cont", nphot=1e5, \
                    npix=visibilities["npix"][j], lam=None, \
                    pixelsize=visibilities["pixelsize"][j], tgas_eq_tdust=True,\
                    scattering_mode_max=0, incl_dust=True, incl_lines=False, \
                    loadlambda=True, incl=p["i"], pa=p["pa"], dpc=p["dpc"], \
                    code="radmc3d", verbose=False, writeimage_unformatted=True,\
                    setthreads=ncpus)

            m.images[visibilities["lam"][j]].image -= m.images["cont"].image
        else:
            m.run_image(name=visibilities["lam"][j], nphot=1e5, \
                    npix=visibilities["npix"][j], lam=None, \
                    pixelsize=visibilities["pixelsize"][j], tgas_eq_tdust=True,\
                    scattering_mode_max=0, incl_dust=False, incl_lines=True, \
                    loadlambda=True, incl=p["i"], pa=p["pa"], dpc=p["dpc"], \
                    code="radmc3d", verbose=False, writeimage_unformatted=True,\
                    setthreads=ncpus)

        m.visibilities[visibilities["lam"][j]] = uv.interpolate_model(\
                visibilities["data"][j].u, visibilities["data"][j].v, \
                visibilities["data"][j].freq, \
                m.images[visibilities["lam"][j]], dRA=-p["x0"], dDec=-p["y0"], \
                nthreads=ncpus)

        if plot:
            lam = c / visibilities["image"][j].freq / 1.0e-4
            wave = lam * numpy.sqrt((1. - b) / (1. + b))

            m.set_camera_wavelength(wave)

            if p["docontsub"]:
                m.run_image(name=visibilities["lam"][j], nphot=1e5, \
                        npix=visibilities["image_npix"][j], lam=None, \
                        pixelsize=visibilities["image_pixelsize"][j], \
                        tgas_eq_tdust=True, scattering_mode_max=0, \
                        incl_dust=True, incl_lines=True, loadlambda=True, \
                        incl=p["i"], pa=-p["pa"], dpc=p["dpc"], code="radmc3d",\
                        verbose=False, setthreads=ncpus)

                m.run_image(name="cont", nphot=1e5, \
                        npix=visibilities["image_npix"][j], lam=None, \
                        pixelsize=visibilities["image_pixelsize"][j], \
                        tgas_eq_tdust=True, scattering_mode_max=0, \
                        incl_dust=True, incl_lines=False, loadlambda=True, \
                        incl=p["i"], pa=-p["pa"], dpc=p["dpc"], code="radmc3d",\
                        verbose=False, setthreads=ncpus)

                m.images[visibilities["lam"]
                         [j]].image -= m.images["cont"].image
            else:
                m.run_image(name=visibilities["lam"][j], nphot=1e5, \
                        npix=visibilities["image_npix"][j], lam=None, \
                        pixelsize=visibilities["image_pixelsize"][j], \
                        tgas_eq_tdust=True, scattering_mode_max=0, \
                        incl_dust=False, incl_lines=True, loadlambda=True, \
                        incl=p["i"], pa=-p["pa"], dpc=p["dpc"], code="radmc3d",\
                        verbose=False, setthreads=ncpus)

            x, y = numpy.meshgrid(numpy.linspace(-256,255,512), \
                    numpy.linspace(-256,255,512))

            beam = misc.gaussian2d(x, y, 0., 0., \
                    visibilities["image"][j].header["BMAJ"]/2.355/\
                    visibilities["image"][j].header["CDELT2"], \
                    visibilities["image"][j].header["BMIN"]/2.355/\
                    visibilities["image"][j].header["CDELT2"], \
                    (90-visibilities["image"][j].header["BPA"])*\
                    numpy.pi/180., 1.0)

            for ind in range(len(wave)):
                m.images[visibilities["lam"][j]].image[:,:,ind,0] = \
                        scipy.signal.fftconvolve(\
                        m.images[visibilities["lam"][j]].image[:,:,ind,0], \
                        beam, mode="same")

    os.system("rm params.txt")
    os.chdir(original_dir)
    os.system("rmdir /tmp/temp_{1:s}_{0:d}".format(comm.Get_rank(), source))

    return m
Exemple #2
0
plt.ylim(-12,12)

plt.xlabel('$\Delta$ R.A. ["]', fontsize=14)
plt.ylabel('$\Delta$ Dec. ["]', fontsize=14)

plt.gca().tick_params(labelsize=14)

plt.show()

# Do the Fourier transform with TrIFT

u = numpy.linspace(100.,1.5e7,10000)
v = numpy.repeat(0.001,10000)

t1 = time.time()
vis = uv.interpolate_model(u, v, numpy.array([2.3e11]), m.images["image"], \
        code="trift", dRA=0., dDec=0., nthreads=4)
t2 = time.time()
print(t2 - t1)

# Do a high resolution model to compare with.

m.run_visibilities(name="image", nphot=1e5, npix=1024, \
        pixelsize=8*25./1024, lam="1000", phi=0, incl=0, code="radmc3d", \
        dpc=100, verbose=False, nostar=True)

m.visibilities["image"] = uv.average(m.visibilities["image"], \
        gridsize=1000000, binsize=1000, radial=True)

# Finally, plot the visibilities.

plt.loglog(u/1e3, vis.amp*1000, "k-", \
        ax.yaxis.set_ticklabels([])

    ax.tick_params(labelsize=14)

plt.show()

# Do the Fourier transform with TrIFT

ruv = numpy.linspace(100.,1.5e7,10000)

pa = 0.
u = ruv * numpy.cos(pa)
v = ruv * numpy.sin(pa)

t1 = time.time()
vis = uv.interpolate_model(u, v, m.images["image"].freq, m.images["image"], \
        code="trift", dRA=0., dDec=0.)
t2 = time.time()
print(t2 - t1)

# Do a model with GALARIO to compare with.

npix = 1024
imsize = 25.

t1 = time.time()
m.run_image(name="galario", nphot=1e5, npix=npix, pixelsize=imsize/npix,\
        lam=None, tgas_eq_tdust=True, scattering_mode_max=0, incl_dust=False, \
        incl_lines=True, imolspec=1, iline=2, widthkms=10., linenlam=25, \
        pa=0-180, incl=45, code="radmc3d", dpc=100, verbose=False, nostar=True)
t2 = time.time()
print(t2 - t1)
m.run_image(name="image", nphot=1e5, npix=25, pixelsize=1.0, lam=None, \
        tgas_eq_tdust=True, scattering_mode_max=0, incl_dust=False, \
        incl_lines=True, imolspec=1, iline=2, widthkms=10., linenlam=25, \
        phi=0, incl=45, code="radmc3d", dpc=100, verbose=False, \
        unstructured=True, camera_nrrefine=100, camera_refine_criterion=1, \
        nostar=True)

# Do the Fourier transform with TrIFT

ruv = numpy.linspace(100., 1.5e7, 10000)

pa = 0.
u = ruv * numpy.cos(pa)
v = ruv * numpy.sin(pa)

vis = uv.interpolate_model(u, v, m.images["image"].freq, m.images["image"], \
        code="trift", dRA=0., dDec=0.)
t2 = time.time()

# Calculate the effective resolution.

triang = tri.Triangulation(m.images["image"].x, m.images["image"].y)
trift_res = numpy.inf
for triangle in triang.triangles:
    for i in range(3):
        length = numpy.sqrt((m.images["image"].x[triangle[i]] - \
                m.images["image"].x[triangle[i-1]])**2 + \
                (m.images["image"].y[triangle[i]] - \
                m.images["image"].y[triangle[i-1]])**2)
        if length < trift_res:
            trift_res = length
print(trift_res)