Exemplo n.º 1
0
def ljet(nmax):
    # makes RT and theta-T diagrams for jet power

    r1 = 100. ; r2 = 1000. 
    theta1 = 0. ; theta2 = np.pi/4.
    
    re.rg("gdump")
    gdet=re.gdet ; gcov=re.gcov ; _dx2=re._dx2 ; _dx3=re._dx3

    fout_RTN = open("ljet_RTN.dat", "w")
    fout_RTS = open("ljet_RTS.dat", "w")
    fout_thT = open("ljet_thT.dat", "w")

    for k in arange(nmax):
        fname=re.dumpname(k)
        print("reading "+str(fname))
        re.rd(fname)
        faraday() # let's trust SashaTch
        Tcalcud()
        erN = - (gdet * Tud[1,0] * _dx2 * _dx3 * (re.h > theta1) * (re.h < theta2)).sum(-1).sum(-1) # theta-averaged energy flux 
        erS = - (gdet * Tud[1,0] * _dx2 * _dx3 * (re.h < (np.pi-theta1)) * (re.h < (np.pi - theta2))).sum(-1).sum(-1) # 
        eth = - (gdet * Tud[1,0] * _dx2 * _dx3 * (re.r > r1) * (re.r < r2)).sum(-1).sum(0) # r-averaged energy flux 
        for kr in arange(re.nx): 
            fout_RTN.write(str(re.t)+" "+str(re.r[kr,0,0])+" "+str(erN[kr])+"\n")
            fout_RTS.write(str(re.t)+" "+str(re.r[kr,0,0])+" "+str(erS[kr])+"\n")
        for kth in arange(re.ny):
            fout_thT.write(str(re.t)+" "+str(re.h[0,kth,0])+" "+str(eth[kth])+"\n")
        fout_RTN.flush() ; fout_RTS.flush() ; fout_thT.flush()
    fout_RTN.close() ; fout_RTS.close() ; fout_thT.close()
Exemplo n.º 2
0
def tworho(n1, n2):
    file1 = re.dumpname(n1)
    file2 = re.dumpname(n2)
    re.rg("gdump.back")
    re.rd(file1)
    r1 = re.r
    h1 = re.h
    ph1 = re.ph
    rho1 = re.rho
    re.rg("gdump")
    re.rd(file2)
    r2 = re.r
    h2 = re.h
    ph2 = re.ph
    rho2 = re.rho

    print("dimensions: " + str(np.shape(rho1)) + ", " + str(np.shape(rho2)))

    x1 = np.squeeze((r1 * sin(h1))[:, :, 0])
    y1 = np.squeeze((r1 * cos(h1))[:, :, 0])
    x2 = np.squeeze((r2 * sin(h2))[:, :, 0])
    y2 = np.squeeze((r2 * cos(h2))[:, :, 0])

    levs = np.linspace(0., rho1.max(), 10)
    xmax = 50.
    plt.clf()
    plt.contourf(x2, y2, (np.squeeze(rho2[:, :, 0])), levels=levs)
    plt.contour(x2, y2, np.squeeze(rho2[:, :, 0]), levels=levs, colors='k')
    plt.contour(x1, y1, np.squeeze(rho1[:, :, 0]), levels=levs, colors='w')
    plt.xlim(0., xmax)
    plt.ylim(-xmax / 4., xmax / 2.)
    plt.savefig("rhotest.png")
    plt.close()
Exemplo n.º 3
0
def mkmov(startn=0, endn=-1, ln=10, whichi=0, whichn=1, **kwargs):
    which = kwargs.pop("which", "mkfrm8panel")
    dosavefig = kwargs.pop("dosavefig", 1)
    print("Doing %s movie" % which)
    re.rg("gdump")
    #compute the total magnetic flux at t = 0
    re.rd("dump000")
    aphi = re.psicalc()
    aphimax = aphi.max()
    #construct file list
    flist1 = np.sort(glob.glob(os.path.join("dumps/", "dump[0-9][0-9][0-9]")))
    flist2 = np.sort(
        glob.glob(os.path.join("dumps/", "dump[0-9][0-9][0-9][0-9]")))
    flist1.sort()
    flist2.sort()
    flist = np.concatenate((flist1, flist2))
    if len(flist) == 0:
        flist1 = np.sort(
            glob.glob(os.path.join("dumps/", "dump[0-9][0-9][0-9]_0000")))
        flist2 = np.sort(
            glob.glob(os.path.join("dumps/", "dump[0-9][0-9][0-9][0-9]_0000")))
        flist1.sort()
        flist2.sort()
        flist = np.concatenate((flist1, flist2))
    firsttime = 1
    dpi = 135
    for fldname in flist:
        #find the index of the file
        fldindex = np.int(fldname.split("_")[0].split("p")[-1])
        if fldindex < startn:
            continue
        if endn >= 0 and fldindex >= endn:
            break
        if fldindex % whichn != whichi:
            #do every whichn'th snapshot starting with whichi'th snapshot
            continue
        if dosavefig:
            fname = "%s%04d.png" % (which, fldindex)
            if os.path.isfile(fname):
                print("File %s exists, skipping..." % fname)
                continue
        #print( "Reading " + fldname + " ..." )
        rd("dump%03d" % fldindex)
        if which == "mkfrmsimple":
            if firsttime:
                firsttime = 0
                fig = plt.figure(figsize=(12, 8))
                plt.clf()
            mkfrmsimple(fig=fig, aphimax=aphimax)
        else:
            print("Unknown movie type: %s" % which)
            return
        print(fldindex)
        plt.draw()
        if dosavefig:
            plt.savefig(fname, dpi=dpi)
Exemplo n.º 4
0
def dumpinfo(prefix):
    
    re.rg("gdump")
    re.rd(prefix)
    fout=open("dumps/"+prefix+"_dinfo.dat", "w")

    fout.write(str(re.nx)+" "+str(re.ny)+" "+str(re.nz)+"\n")
    print("nr x ntheta x nphi = "+str(re.nx)+" "+str(re.ny)+" "+str(re.nz))
    
    fout.write(str(re.a)+"\n")
    print("Kerr parameter a = "+str(re.a))

    fout.write(str(re.t)+"\n")
    print("time "+str(re.t))
    fout.close()
Exemplo n.º 5
0
def origin_plot(dumpn, xmax=30.):
    filename = re.dumpname(dumpn)
    re.rg("gdump")
    re.rd(filename)
    r = re.r
    h = re.h
    ph = re.ph
    rho = re.rho
    origin_r = re.origin_r
    origin_th = re.origin_th
    nxx = 10
    rlevs = (r.max() / 1.)**(np.arange(nxx) / np.double(nxx))
    thlevs = np.pi * np.arange(nxx) / np.double(nxx)
    x = np.squeeze((r * sin(h))[:, :, 0])
    y = np.squeeze((r * cos(h))[:, :, 0])

    print("R0_max = " + str(origin_r[rho > 1e-6].max()))

    nd = np.size(np.shape(np.squeeze(re.r)))
    if (nd > 1):
        plt.clf()
        plt.contourf(x, y, np.log10(np.squeeze(
            origin_r[:, :, 0])))  # , levels=np.log10(rlevs))
        plt.colorbar()
        plt.contour(x, y, np.squeeze(rho[:, :, 0]), colors='k')
        plt.contour(x, y, np.squeeze(r[:, :, 0]), levels=rlevs, colors='w')
        plt.contour(x, y, np.squeeze(h[:, :, 0]), levels=thlevs, colors='w')
        #    plt.contour(x, y, np.squeeze(origin_th[:,:,0]), levels=thlevs, colors='k')
        plt.xlim(0., xmax)
        plt.ylim(-xmax / 4., xmax / 2.)
        plt.title(r"t = " + str(re.t) + " $GM/c^3$")
        plt.savefig("dumps/" + filename + "_ori.png")
        plt.close()
        plt.clf()
        plt.plot(np.squeeze(r), np.squeeze(r), 'r')
        plt.plot(r[rho > 0.1], origin_r[rho > 0.1] * 25., '.k')
        plt.xlim(0., xmax)
        plt.ylim(0., xmax)
        plt.savefig("oritest.png")
    else:
        plt.clf()
        plt.plot(x, np.log10(np.squeeze(origin_r[:, :, 0])), 'k-')
        plt.plot(x, x, 'r:')
        plt.xscale('log')
        plt.yscale('log')
        plt.savefig("dumps/" + filename + "_ori.png")
    plt.close()
Exemplo n.º 6
0
def test1d(n1, n2):
    re.rg("gdump.back")

    ndump = np.arange(n2 - n1) + n1

    plt.clf()
    for kdump in ndump:
        re.rd(re.dumpname(kdump))
        #     plt.plot(np.squeeze(re.r), np.squeeze(re.origin_r), label=str(kdump))
        plt.plot(np.squeeze(re.r), np.squeeze(re.rho), label=str(kdump))
        #   plt.yscale('log')
    plt.legend()
    plt.savefig("rhotest.png")
    plt.clf()
    for kdump in ndump:
        re.rd(re.dumpname(kdump))
        plt.plot(np.squeeze(re.r), np.squeeze(re.origin_r), label=str(kdump))
        # plt.plot(np.squeeze(re.r), np.squeeze(re.rho), label=str(kdump))
        #   plt.yscale('log')
    plt.legend()
    plt.savefig("oritest.png")
Exemplo n.º 7
0
def defaultrun():

    dire=''
    rref=10.
 #   os.chdir(dire)
#    dumpinfo()
    re.rg(dire+"gdump")
    run=unique(re.r)
    nr=run[where(run<rref)].argmax() # where the radius is closest to rref (from inside)

    fout=open(dire+"dumps_mevol.dat", "w")

    flist=get_sorted_file_list()
    nlist=size(flist)
    print(str(nlist)+" files")
    print(flist)
    for k in arange(nlist):
        dumpinfo("../"+flist[k])
        framerip("../"+flist[k], alifactor=1)
        maccre, mwind, laccre, lwind = mint(rref)
        fromabove("../"+flist[k], alifactor=3)
        print("merger_remote defaultrun: reducing "+str(flist[k]))
        fout.write(str(re.t)+" "+str(maccre)+" "+str(mwind)+" "+str(laccre)+" "+str(lwind)+"\n")
    fout.close()
Exemplo n.º 8
0
def glevol(nmax, rref):
#    global rho, uu
    re.rg("gdump")
    gdet=re.gdet ; gcov=re.gcov ; _dx2=re._dx2 ; _dx3=re._dx3
    # rref is reference radius
    run=unique(re.r)
    nr=run[where(run<rref)].argmax()

#    maccre=zeros(nmax, dtype=double)

    fmdot=open("merge_mevol"+str(rref)+".dat", "w")

    for k in arange(nmax):
        fname=re.dumpname(k)
        print("reading "+str(fname))
        re.rd(fname)
        print("rho is "+str(shape(re.rho)))
        #        Tcalcud()
        # accretion rate at rref
        rhom=(re.rho).mean(axis=2) ;  uum=(re.uu).mean(axis=3)
        rhom_south(re.rho*cos(re.h)).mean(axis=2)
        rhom_east(re.rho*sin(re.h)).mean(axis=2)
        # do we need to multiply this by drdx??
        #        uum[0]=(uu[0]*drdx[0,0]).mean(axis=3) ;    uum[1]=(uu[1]*drdx[1,1]).mean(axis=3)
        #        uum[2]=(uu[2]*drdx[2,2]).mean(axis=3) ;    uum[2]=(uu[2]*drdx[2,2]).mean(axis=3)
        gm=sqrt(old_div(gdet,gcov[1,1]))[:,0,:]
        maccre=-trapz((rhom*uum[1]*(uum[1]<0.)*gm)[nr,:], x=re.h[nr,:,0])*_dx2*_dx3
        maccre_south=-trapz((rhom_south*uum[1]*(uum[1]<0.)*gm)[nr,:], x=re.h[nr,:,0])*_dx2*_dx3
        maccre_east=-trapz((rhom_east*uum[1]*(uum[1]<0.)*gm)[nr,:], x=re.h[nr,:,0])*_dx2*_dx3
        mwind=-trapz((rhom*uum[1]*(uum[1]>0.)*gm)[nr,:], x=re.h[nr,:,0])*_dx2*_dx3
        laccre=-trapz((rho*fabs(old_div(ud[3],ud[0]))*uu[1]*(uu[1]<0.)*sqrt(old_div(gdet,gcov[1,1]))).mean(axis=2)[nr,:], x=re.h[nr,:,0])*_dx2*_dx3
        lwind=-trapz((rho*fabs(old_div(ud[3],ud[0]))*uu[1]*(uu[1]>0.)*sqrt(old_div(gdet,gcov[1,1]))).mean(axis=2)[nr,:], x=re.h[nr,:,0])*_dx2*_dx3
        # maccre=-((rho*uu[1]*sqrt(gdet/gcov[1,1]))[nr,:,:]).sum()*_dx2*_dx3
        fmdot.write(str(t)+" "+str(maccre)+" "+str(mwind)+" "+str(old_div(laccre,maccre))+" "+str(old_div(lwind,mwind))+" "+str(maccre_south)+" "+str(maccre_east)+"\n")
    
    fmdot.close()
Exemplo n.º 9
0
def corveerun(nfirst=None, nlast=None):
    re.rg("gdump")
    
    readndump(nfirst,nlast)
    #    corvee(nfirst, nlast)
    os.system('tar -cf mergereads.tar merge_*.dat')
Exemplo n.º 10
0
def corvee(n1,n2):
    
    re.rg("gdump")
    nx=re.nx ; ny=re.ny ; nz=re.nz
    gdet=re.gdet ; gcov=re.gcov ; _dx2=re._dx2 ; _dx3=re._dx3 ; drdx=re.drdx
    r=re.r ; h=re.h ; phi=re.ph # importing coordinate mesh

    # velocities:
    uufile='merge_uu.dat'
    udfile='merge_ud.dat'
    fuu=open(uufile, 'r')
    fud=open(udfile, 'r')
    #    s=str.split(str.strip(fuu.readline()))
    # mean velocity field
    uumean=zeros([4,nx,ny,nz], dtype=double)
    udmean=zeros([4,nx,ny,nz], dtype=double)

    for kx in arange(nx):
        for ky in arange(ny):
            s=str.split(str.strip(fuu.readline()))
            uumean[0,kx,ky,:]=double(s[0])
            uumean[1,kx,ky,:]=double(s[1])
            uumean[2,kx,ky,:]=double(s[2])
            uumean[3,kx,ky,:]=double(s[3])
            s=str.split(str.strip(fud.readline()))
            udmean[0,kx,ky,:]=double(s[0])
            udmean[1,kx,ky,:]=double(s[1])
            udmean[2,kx,ky,:]=double(s[2])
            udmean[3,kx,ky,:]=double(s[3])
    fuu.close()
    fud.close()

    # tetrad components:
    t0=tetrad_t(uumean, udmean)
    tr=tetrad_r(uumean, udmean)
    th=tetrad_h(uumean, udmean)
    tp=tetrad_p(uumean, udmean)
#    print shape(tr)

    nframes=n2-n1+1
    n=n1+arange(nframes)

    for k in n:
        fname=re.dumpname(k)
        re.rd(fname)
        uu=re.uu ; ud=re.ud ; rho=re.rho
        if(k==n1):
            rhomean=rho
            # velocity components:
            uu0=uu[0]*drdx[0,0]-uumean[0] ; ud0=old_div(ud[0],drdx[0,0])-udmean[0]
            uur=uu[1]*drdx[1,1]-uumean[1] ; udr=old_div(ud[1],drdx[1,1])-udmean[1]
            uuh=uu[2]*drdx[2,2]-uumean[2] ; udh=old_div(ud[2],drdx[2,2])-udmean[2]
            uup=uu[3]*drdx[3,3]-uumean[3] ; udp=old_div(ud[3],drdx[3,3])-udmean[3]
            tuur=(uu0*tr[0]+uur*tr[1]+uuh*tr[2]+uup*tr[3])  # co-moving velocity components
            tuuh=(uu0*th[0]+uur*th[1]+uuh*th[2]+uup*th[3])
            tuup=(uu0*tp[0]+uur*tp[1]+uuh*tp[2]+uup*tp[3])
            drh=rho*tuur*tuuh ; drp=rho*tuur*tuup ; dhp=rho*tuuh*tuup
            drr=rho*tuur*tuur ; dpp=rho*tuup*tuup ; dhh=rho*tuuh*tuuh
#            print(shape(drh))
#            print(shape(rho))
#            print(shape(tuur))
        else:
            rhomean+=rho
            # velocity components:
            uu0=uu[0]-uumean[0] ; ud0=ud[0]-udmean[0]
            uur=uu[1]-uumean[1] ; udr=ud[1]-udmean[1]
            uuh=uu[2]-uumean[2] ; udh=ud[2]-udmean[2]
            uup=uu[3]-uumean[3] ; udp=ud[3]-udmean[3]
            tuur=(uu0*tr[0]+uur*tr[1]+uuh*tr[2]+uup*tr[3])
            tuuh=(uu0*th[0]+uur*th[1]+uuh*th[2]+uup*th[3])
            tuup=(uu0*tp[0]+uur*tp[1]+uuh*tp[2]+uup*tp[3])
            drh+=rho*tuur*tuuh ; drp+=rho*tuur*tuup ; dhp+=rho*tuuh*tuup
            drr+=rho*tuur*tuur ; dpp+=rho*tuup*tuup ; dhh+=rho*tuuh*tuuh

    drh/=rhomean ;    drp/=rhomean ;    dhp/=rhomean 
    drr/=rhomean ;    dpp/=rhomean ;    dhh/=rhomean 
  
    drh=drh.mean(axis=2) ;  drp=drp.mean(axis=2) ;  dhp=dhp.mean(axis=2)
    drr=drr.mean(axis=2) ;  dpp=dpp.mean(axis=2) ;  dhh=dhh.mean(axis=2)
 
    fout=open('merge_corv.dat', 'w')
    for kx in arange(nx):
        for ky in arange(ny):
            # RR HH PP RH HP RP
            fout.write(str(drr[kx,ky])+' '+str(dhh[kx,ky])+' '+str(dpp[kx,ky])+' '+str(drh[kx,ky])+' '+str(dhp[kx,ky])+' '+str(drp[kx,ky])+'\n')
    fout.close()
Exemplo n.º 11
0
def readndump(n1, n2, rref=5.0):
    '''
    calculates mean maps for the frames n1-n2
    rref is reference radius where the mass accretion rate is calculated 
    '''
    #    run=unique(r)
    #    nr=run[where(run<rref)].argmax()

    re.rg("gdump")
    nx=re.nx ; ny=re.ny ; nz=re.nz
    gdet=re.gdet ; gcov=re.gcov ; _dx2=re._dx2 ; _dx3=re._dx3 ; drdx=re.drdx
    guu=re.guu ; gdd=re.gdd
    r=re.r ; h=re.h ; phi=re.ph # importing coordinate mesh
    if (n2<n1):
        print("readndump: invalid file number range")
        exit()

    fmdot=open("merge_mevol"+str(rref)+".dat", "w")

    nframes=n2-n1+1
    n=n1+arange(nframes)

    for k in n:
        fname=re.dumpname(k)
        re.rd(fname)
        Tcalcud()
        p=(re.gam-1.)*re.ug
        magp=old_div(re.bsq,2.)
        rho=re.rho ; uu=re.uu ; ud=re.ud 
        if(k==n1):
            rhomean=rho
            rhosqmean=rho**2
            # velocity components:
            uu0=uu[0]*rho ; ud0=ud[0]*rho
            uur=uu[1]*rho ; udr=ud[1]*rho
            uuh=uu[2]*rho ; udh=ud[2]*rho
            uup=uu[3]*rho ; udp=ud[3]*rho
            puu0=uu[0]*p ; pud0=ud[0]*p
            puur=uu[1]*p ; pudr=ud[1]*p
            puuh=uu[2]*p ; pudh=ud[2]*p
            puup=uu[3]*p ; pudp=ud[3]*p
            mpuu0=uu[0]*magp ; mpud0=ud[0]*magp
            mpuur=uu[1]*magp ; mpudr=ud[1]*magp
            mpuuh=uu[2]*magp ; mpudh=ud[2]*magp
            mpuup=uu[3]*magp ; mpudp=ud[3]*magp

            tudem=TudEM ; tudma=TudMA
            pmean=p
	    # unorm=uaver
            magp_mean=magp
            aphi=re.psicalc()
            dumpinfo(fname)
            os.system("cp dumps/"+fname+"_dinfo.dat merge_dinfo.dat")
        else:
            rhomean+=rho
            rhosqmean+=rho**2
            # velocity components:
            uu0+=uu[0]*rho ; ud0+=ud[0]*rho
            uur+=uu[1]*rho ; udr+=ud[1]*rho
            uuh+=uu[2]*rho ; udh+=ud[2]*rho
            uup+=uu[3]*rho ; udp+=ud[3]*rho
            puu0+=uu[0]*p ; pud0+=ud[0]*p
            puur+=uu[1]*p ; pudr+=ud[1]*p
            puuh+=uu[2]*p ; pudh+=ud[2]*p
            puup+=uu[3]*p ; pudp+=ud[3]*p
            mpuu0+=uu[0]*magp ; mpud0+=ud[0]*magp
            mpuur+=uu[1]*magp ; mpudr+=ud[1]*magp
            mpuuh+=uu[2]*magp ; mpudh+=ud[2]*magp
            mpuup+=uu[3]*magp ; mpudp+=ud[3]*magp

            pmean+=p
            magp_mean+=magp
            aphi+=re.psicalc()
            #	    unorm+=uaver
            tudem+=TudEM ; tudma+=TudMA
        maccre, mwind, laccre, lwind = mint(rref)
        fmdot.write(str(re.t)+" "+str(maccre)+" "+str(mwind)+" "+str(old_div(laccre,maccre))+" "+str(old_div(lwind,mwind))+"\n")
    fmdot.close()
    # velocity normalization:
    uu0*=old_div(drdx[0,0],rhomean) ; uur*=old_div(drdx[1,1],rhomean) ; uuh*=old_div(drdx[2,2],rhomean) ; uup*=old_div(drdx[3,3],rhomean)
    ud0/=drdx[0,0]*rhomean ; udr/=drdx[1,1]*rhomean ; udh/=drdx[2,2]*rhomean ; udp/=drdx[3,3]*rhomean
    puu0*=old_div(drdx[0,0],pmean) ; puur*=old_div(drdx[1,1],pmean) ; puuh*=old_div(drdx[2,2],pmean) ; puup*=old_div(drdx[3,3],pmean)
    pud0/=drdx[0,0]*pmean ; pudr/=drdx[1,1]*pmean ; pudh/=drdx[2,2]*pmean ; pudp/=drdx[3,3]*pmean
    mpuu0*=old_div(drdx[0,0],magp_mean) ; mpuur*=old_div(drdx[1,1],magp_mean) ; mpuuh*=old_div(drdx[2,2],magp_mean) ; mpuup*=old_div(drdx[3,3],magp_mean)
    mpud0/=drdx[0,0]*magp_mean ; mpudr/=drdx[1,1]*magp_mean ; mpudh/=drdx[2,2]*magp_mean ; mpudp/=drdx[3,3]*magp_mean

#    uu0/=unorm ; uur/=unorm ; uuh/=unorm ; uup/=unorm
#    ud0/=unorm ; udr/=unorm ; udh/=unorm ; udp/=unorm
    # averaging the density:
    rhomean=old_div(rhomean,double(nframes))
    rhodisp=old_div(rhosqmean,double(nframes))-rhomean**2
    pmean=old_div(pmean,double(nframes))
    magp_mean=old_div(magp_mean,double(nframes))
    tudem/=double(nframes) ; tudma/=double(nframes)
    aphi/=double(nframes)
    # physical stress-energy tensor components:
    for k in arange(4):
        for j in arange(4):
            tudem[k,j]*=drdx[k,k]/drdx[j,j]*sqrt(guu[k,k]*gdd[j,j])
            tudma[k,j]*=drdx[k,k]/drdx[j,j]*sqrt(guu[k,k]*gdd[j,j])

	#   ss=shape(rhomean) 
 	#   nx=ss[0]
  	#  ny=ss[1]
    # we need some 3D data
    rho3d=rhomean ;  p3d=pmean ; ur3d=uur ; uh3d=uuh ; up3d=uup
    fout=open('merge_rho3d.dat', 'w')
    for kx in arange(nx):
        for ky in arange(ny):
            for kz in arange(nz):
                fout.write(str(rho3d[kx,ky, kz])+'\n')
    fout.close()
    fout=open('merge_p3d.dat', 'w')
    for kx in arange(nx):
        for ky in arange(ny):
            for kz in arange(nz):
                fout.write(str(p3d[kx,ky, kz])+'\n')
    fout.close()
    fout=open('merge_u3d.dat', 'w')
    # uu on the mesh
    for kx in arange(nx):
        for ky in arange(ny):
            for kz in arange(nz):
            	fout.write(str(uu0[kx,ky,kz])+' '+str(uur[kx,ky,kz])+' '+str(uuh[kx,ky,kz])+' '+str(uup[kx,ky,kz])+'\n')
    fout.close()
    fout=open('merge_pu3d.dat', 'w')
    # uu on the mesh
    for kx in arange(nx):
        for ky in arange(ny):
            for kz in arange(nz):
                fout.write(str(puu0[kx,ky,kz])+' '+str(puur[kx,ky,kz])+' '+str(puuh[kx,ky,kz])+' '+str(puup[kx,ky,kz])+'\n')
    fout.close()
    fout=open('merge_mpu3d.dat', 'w')
    # uu on the mesh
    for kx in arange(nx):
        for ky in arange(ny):
            for kz in arange(nz):
                fout.write(str(mpuu0[kx,ky,kz])+' '+str(mpuur[kx,ky,kz])+' '+str(mpuuh[kx,ky,kz])+' '+str(mpuup[kx,ky,kz])+'\n')
    fout.close()

    # 2D or 3D? averaging over phi
    rhomean=rhomean.mean(axis=2)
    rhodisp=rhodisp.mean(axis=2)
    pmean=pmean.mean(axis=2) ; magp_mean=magp_mean.mean(axis=2)
    uu0=uu0.mean(axis=2) ; uur=uur.mean(axis=2) ; uuh=uuh.mean(axis=2) ; uup=uup.mean(axis=2)
    ud0=ud0.mean(axis=2) ; udr=udr.mean(axis=2) ; udh=udh.mean(axis=2) ; udp=udp.mean(axis=2)
    puu0=puu0.mean(axis=2) ; puur=puur.mean(axis=2) ; puuh=puuh.mean(axis=2) ; puup=puup.mean(axis=2)
    pud0=pud0.mean(axis=2) ; pudr=pudr.mean(axis=2) ; pudh=pudh.mean(axis=2) ; pudp=pudp.mean(axis=2)
    mpuu0=mpuu0.mean(axis=2) ; mpuur=mpuur.mean(axis=2) ; mpuuh=mpuuh.mean(axis=2) ; mpuup=mpuup.mean(axis=2)
    mpud0=mpud0.mean(axis=2) ; mpudr=mpudr.mean(axis=2) ; mpudh=mpudh.mean(axis=2) ; mpudp=mpudp.mean(axis=2)
    #  drp=drp.mean(axis=2)  ;  dhp=dhp.mean(axis=2)  ;  drh=drh.mean(axis=2)
    tudem=tudem.mean(axis=-1)
    tudma=tudma.mean(axis=-1)
    # aphi=aphi.mean(axis=-1)	
    # r mesh:
    fout=open('merge_r.dat', 'w')
    for kx in arange(nx):
        fout.write(str(r[kx,0,0])+'\n')
    fout.close()
    fout=open('merge_h.dat', 'w')
    # theta mesh
    for ky in arange(ny):
        fout.write(str(h[0,ky,0])+'\n')
    fout.close()
    fout=open('merge_phi.dat', 'w')
    # phi mesh
    for kz in arange(nz):
        fout.write(str(phi[0,0,kz])+'\n')
    fout.close()   
    fout=open('merge_rho.dat', 'w')
    # rho on the mesh
    for kx in arange(nx):
        for ky in arange(ny):
            fout.write(str(rhomean[kx,ky])+'\n')
    fout.close()
    # p on the mesh
    fout=open('merge_p.dat', 'w')
    for kx in arange(nx):
        for ky in arange(ny):
            fout.write(str(pmean[kx,ky])+'\n')
    fout.close()
    # magnetic pressure on the mesh
    fout=open('merge_mp.dat', 'w')
    for kx in arange(nx):
        for ky in arange(ny):
            fout.write(str(magp_mean[kx,ky])+'\n')
    fout.close()
    fout=open('merge_uu.dat', 'w')
    # uu on the mesh
    for kx in arange(nx):
        for ky in arange(ny):
            fout.write(str(uu0[kx,ky])+' '+str(uur[kx,ky])+' '+str(uuh[kx,ky])+' '+str(uup[kx,ky])+'\n')
    fout.close()
    fout=open('merge_ud.dat', 'w')
    # ud on the mesh
    for kx in arange(nx):
        for ky in arange(ny):
            fout.write(str(ud0[kx,ky])+' '+str(udr[kx,ky])+' '+str(udh[kx,ky])+' '+str(udp[kx,ky])+'\n')
    fout.close()
    fout=open('merge_puu.dat', 'w')
    # uu on the mesh
    for kx in arange(nx):
        for ky in arange(ny):
            fout.write(str(puu0[kx,ky])+' '+str(puur[kx,ky])+' '+str(puuh[kx,ky])+' '+str(puup[kx,ky])+'\n')
    fout.close()
    fout=open('merge_pud.dat', 'w')
    # ud on the mesh
    for kx in arange(nx):
        for ky in arange(ny):
            fout.write(str(pud0[kx,ky])+' '+str(pudr[kx,ky])+' '+str(pudh[kx,ky])+' '+str(pudp[kx,ky])+'\n')
    fout.close()
    fout=open('merge_mpuu.dat', 'w')
    # uu on the mesh
    for kx in arange(nx):
        for ky in arange(ny):
            fout.write(str(mpuu0[kx,ky])+' '+str(mpuur[kx,ky])+' '+str(mpuuh[kx,ky])+' '+str(mpuup[kx,ky])+'\n')
    fout.close()
    fout=open('merge_mpud.dat', 'w')
    # ud on the mesh
    for kx in arange(nx):
        for ky in arange(ny):
            fout.write(str(mpud0[kx,ky])+' '+str(mpudr[kx,ky])+' '+str(mpudh[kx,ky])+' '+str(mpudp[kx,ky])+'\n')
    fout.close()

    fout=open('merge_tudem.dat', 'w')
    # EM energy-stress tensor
    for kx in arange(nx):
        for ky in arange(ny):
            fout.write(str(tudem[0,0][kx,ky])+' '+str(tudem[0,1][kx,ky])+' '+str(tudem[0,2][kx,ky])+' '+str(tudem[0,3][kx,ky])+' ')
            fout.write(str(tudem[1,0][kx,ky])+' '+str(tudem[1,1][kx,ky])+' '+str(tudem[1,2][kx,ky])+' '+str(tudem[1,3][kx,ky])+' ')
            fout.write(str(tudem[2,0][kx,ky])+' '+str(tudem[2,1][kx,ky])+' '+str(tudem[2,2][kx,ky])+' '+str(tudem[2,3][kx,ky])+' ')
            fout.write(str(tudem[3,0][kx,ky])+' '+str(tudem[3,1][kx,ky])+' '+str(tudem[3,2][kx,ky])+' '+str(tudem[3,3][kx,ky])+'\n')
    fout.close()
    fout=open('merge_tudma.dat', 'w')
    # matter energy-stress tensor
    for kx in arange(nx):
        for ky in arange(ny):
            fout.write(str(tudma[0,0][kx,ky])+' '+str(tudma[0,1][kx,ky])+' '+str(tudma[0,2][kx,ky])+' '+str(tudma[0,3][kx,ky])+' ')
            fout.write(str(tudma[1,0][kx,ky])+' '+str(tudma[1,1][kx,ky])+' '+str(tudma[1,2][kx,ky])+' '+str(tudma[1,3][kx,ky])+' ')
            fout.write(str(tudma[2,0][kx,ky])+' '+str(tudma[2,1][kx,ky])+' '+str(tudma[2,2][kx,ky])+' '+str(tudma[2,3][kx,ky])+' ')
            fout.write(str(tudma[3,0][kx,ky])+' '+str(tudma[3,1][kx,ky])+' '+str(tudma[3,2][kx,ky])+' '+str(tudma[3,3][kx,ky])+'\n')
    fout.close()
    fout=open('merge_aphi.dat', 'w')
    # poloidal magnetic field lines	
    for kx in arange(nx):
        for ky in arange(ny):
            fout.write(str(aphi[kx,ky])+'\n')
    fout.close()