Пример #1
0
def fieldstrength(pointdata):
    bx = read.extract(pointdata, 'b1', attribute_mode='point')
    by = read.extract(pointdata, 'b2', attribute_mode='point')
    bz = read.extract(pointdata, 'b3', attribute_mode='point')

    lfac = read.extract(pointdata, 'lfac', attribute_mode='point')

    b = np.sqrt(np.square(bx) + np.square(by) + np.square(bz))

    m = b <= 1.e-7
    m = m | (lfac >= 9.)

    b = np.ma.masked_array(b, m)

    print '=== Done with field strength ==='

    return [b, m]
Пример #2
0
def fieldstrength(pointdata):
    bx = read.extract(pointdata, "b1", attribute_mode="point")
    by = read.extract(pointdata, "b2", attribute_mode="point")
    bz = read.extract(pointdata, "b3", attribute_mode="point")

    lfac = read.extract(pointdata, "lfac", attribute_mode="point")

    b = np.sqrt(np.square(bx) + np.square(by) + np.square(bz))

    m = b <= 1.0e-7
    m = m | (lfac >= 9.0)

    b = np.ma.masked_array(b, m)

    print "=== Done with field strength ==="

    return [b, m]
Пример #3
0
def get_bcyl(pointdata):

    bx = read.extract(pointdata, "b1", attribute_mode="point")
    by = read.extract(pointdata, "b2", attribute_mode="point")
    bz = read.extract(pointdata, "b3", attribute_mode="point")

    points = ah.vtk2array(pointdata.GetPoints().GetData())

    r = np.sqrt(np.square(points[:, 0]) + np.square(points[:, 1]))
    m = r <= 0.0

    cosphi = np.ma.array(points[:, 0] / r, mask=m, fill_value=0.0)
    sinphi = np.ma.array(points[:, 1] / r, mask=m, fill_value=0.0)

    br = bx * cosphi + by * sinphi
    bphi = by * cosphi - bx * sinphi

    return np.array([br, bphi, bz])
Пример #4
0
def get_bcyl(pointdata):

    bx = read.extract(pointdata, 'b1', attribute_mode='point')
    by = read.extract(pointdata, 'b2', attribute_mode='point')
    bz = read.extract(pointdata, 'b3', attribute_mode='point')

    points = ah.vtk2array(pointdata.GetPoints().GetData())

    r = np.sqrt(np.square(points[:, 0]) + np.square(points[:, 1]))
    m = r <= 0.

    cosphi = np.ma.array(points[:, 0] / r, mask=m, fill_value=0.)
    sinphi = np.ma.array(points[:, 1] / r, mask=m, fill_value=0.)

    br = bx * cosphi + by * sinphi
    bphi = by * cosphi - bx * sinphi

    return np.array([br, bphi, bz])
Пример #5
0
def integral(data,val,unit,los):
    '''get optically thin emission in specific wave length'''
    print '=== Getting emission ==='
# peel data:
    pointdata=data.get('pointdata')
# points:
    emiss=read.extract(pointdata,val,attribute_mode='point')*unit/los
# mask out the wind zone:
    print '=== Done with integral value ==='
    return {'emiss': emiss, 'points': data.get('points')}
Пример #6
0
def integral(data, val, unit, los):
    '''get optically thin emission in specific wave length'''
    print '=== Getting emission ==='
    # peel data:
    pointdata = data.get('pointdata')
    # points:
    emiss = read.extract(pointdata, val, attribute_mode='point') * unit / los
    # mask out the wind zone:
    print '=== Done with integral value ==='
    return {'emiss': emiss, 'points': data.get('points')}
Пример #7
0
def get_bsphere(pointdata):

    bx = read.extract(pointdata, "b1", attribute_mode="point")
    by = read.extract(pointdata, "b2", attribute_mode="point")
    bz = read.extract(pointdata, "b3", attribute_mode="point")

    points = ah.vtk2array(pointdata.GetPoints().GetData())

    x = points[:, 0]
    y = points[:, 1]
    z = points[:, 2]

    rcyl = np.sqrt(np.square(points[:, 0]) + np.square(points[:, 1]))
    r = np.sqrt(np.square(points[:, 0]) + np.square(points[:, 1]) + np.square(points[:, 2]))

    br = 1.0 / r * (x * bx + y * by + z * bz)
    bphi = 1.0 / rcyl * (-y * bx + x * by)
    btheta = 1.0 / r * (1.0 / rcyl * z * x * bx + 1.0 / rcyl * z * y * by - rcyl * bz)

    return {"br": br, "bphi": bphi, "btheta": btheta}
Пример #8
0
def get_bsphere(pointdata):

    bx = read.extract(pointdata, 'b1', attribute_mode='point')
    by = read.extract(pointdata, 'b2', attribute_mode='point')
    bz = read.extract(pointdata, 'b3', attribute_mode='point')

    points = ah.vtk2array(pointdata.GetPoints().GetData())

    x = points[:, 0]
    y = points[:, 1]
    z = points[:, 2]

    rcyl = np.sqrt(np.square(points[:, 0]) + np.square(points[:, 1]))
    r = np.sqrt(
        np.square(points[:, 0]) + np.square(points[:, 1]) +
        np.square(points[:, 2]))

    br = 1. / r * (x * bx + y * by + z * bz)
    bphi = 1. / rcyl * (-y * bx + x * by)
    btheta = 1. / r * (1. / rcyl * z * x * bx + 1. / rcyl * z * y * by -
                       rcyl * bz)

    return {'br': br, 'bphi': bphi, 'btheta': btheta}
Пример #9
0
def emiss(data, ion, Teunit, nunit, Lunit, othick=0):
    '''get optically thin emission in specific wave length'''
    print '=== Getting emission ==='
    # peel data:
    pointdata = data.get('pointdata')
    # points:
    rho = read.extract(pointdata, 'rho', attribute_mode='point')
    Te = read.extract(pointdata, 'Te', attribute_mode='point')
    rho = rho * nunit
    Te = Te * Teunit
    # read in emissivity table
    tabledic = loadGtable(ion, filenm='aia')
    logT = tabledic['logt']
    logn = tabledic['n_e_lg']
    gmat = tabledic['goft_mat']
    ce = findintable(ion, Te, rho, logT, logn, gmat)
    # emission flux in unit of DN s^-1 per unit length
    emiss = rho**2 * ce * Lunit
    if othick != 0:
        emiss = np.where(rho > 2.e10, 0., emiss)
# mask out the wind zone:
    print '=== Done with emission! ==='
    return {'emiss': emiss, 'points': data.get('points')}
Пример #10
0
def emiss(data,ion,Teunit,nunit,Lunit,othick=0):
    '''get optically thin emission in specific wave length'''
    print '=== Getting emission ==='
# peel data:
    pointdata=data.get('pointdata')
# points:
    rho=read.extract(pointdata,'rho',attribute_mode='point')
    Te=read.extract(pointdata,'Te',attribute_mode='point')
    rho=rho*nunit
    Te=Te*Teunit
# read in emissivity table
    tabledic=loadGtable(ion,filenm='aia')
    logT=tabledic['logt']
    logn=tabledic['n_e_lg']
    gmat=tabledic['goft_mat']
    ce=findintable(ion,Te,rho,logT,logn,gmat)
# emission flux in unit of DN s^-1 per unit length
    emiss = rho**2 * ce * Lunit
    if othick != 0:
        emiss = np.where(rho>2.e10,0.,emiss)
# mask out the wind zone:
    print '=== Done with emission! ==='
    return {'emiss': emiss, 'points': data.get('points')}
Пример #11
0
def vtu_values(offset,varname,filenameout='data',attribute_mode='cell',type='vtu'):

    if type == 'vtu':
        filename=''.join([filenameout,repr(offset).zfill(4),'.vtu'])
        datareader = v.vtkXMLUnstructuredGridReader()
    elif type == 'pvtu':
        filename=''.join([filenameout,repr(offset).zfill(4),'.pvtu'])
        datareader = v.vtkXMLPUnstructuredGridReader()
        
    datareader.SetFileName(filename)
    datareader.Update()
    data = datareader.GetOutput()
    
    return read.extract(data,varname,attribute_mode=attribute_mode)
Пример #12
0
def vtu_values(offset,
               varname,
               filenameout='data',
               attribute_mode='cell',
               type='vtu'):

    if type == 'vtu':
        filename = ''.join([filenameout, repr(offset).zfill(4), '.vtu'])
        datareader = v.vtkXMLUnstructuredGridReader()
    elif type == 'pvtu':
        filename = ''.join([filenameout, repr(offset).zfill(4), '.pvtu'])
        datareader = v.vtkXMLPUnstructuredGridReader()

    datareader.SetFileName(filename)
    datareader.Update()
    data = datareader.GetOutput()

    return read.extract(data, varname, attribute_mode=attribute_mode)
Пример #13
0
def get(pointdata):

    print "=== Calculating local anisotropy ==="

    bcyl = get_bcyl(pointdata)

    m = np.square(bcyl[0]) + np.square(bcyl[2]) + np.square(bcyl[1]) <= 1.0e-14

    lfac = read.extract(pointdata, "lfac", attribute_mode="point")
    m = m | (lfac >= 9.0)

    aniso = np.square(bcyl[0]) + np.square(bcyl[2]) / (np.square(bcyl[0]) + np.square(bcyl[2]) + np.square(bcyl[1]))

    m = m | (np.isnan(aniso))
    aniso = np.ma.masked_array(aniso, m)

    print "=== Done with local anisotropy ==="

    return [aniso, m]
Пример #14
0
def get_bdash(data):

    c  = 2.99792458e10 # cm s^-1

    bx=read.extract(data,'b1',attribute_mode='point')
    by=read.extract(data,'b2',attribute_mode='point')
    bz=read.extract(data,'b3',attribute_mode='point')
    ux=read.extract(data,'u1',attribute_mode='point')
    uy=read.extract(data,'u2',attribute_mode='point')
    uz=read.extract(data,'u3',attribute_mode='point')
    lfac=read.extract(data,'lfac',attribute_mode='point')
    
    bdash = np.empty((len(bx),3))
    bdash[:,0] = bx/lfac + ux/lfac*(bx*ux+by*uy+bz*uz)/c/c
    bdash[:,1] = by/lfac + uy/lfac*(bx*ux+by*uy+bz*uz)/c/c
    bdash[:,2] = bz/lfac + uz/lfac*(bx*ux+by*uy+bz*uz)/c/c

    return bdash
Пример #15
0
def get_bdash(data):

    c  = 2.99792458e10 # cm s^-1

    bx=read.extract(data,'b1',attribute_mode='point')
    by=read.extract(data,'b2',attribute_mode='point')
    bz=read.extract(data,'b3',attribute_mode='point')
    ux=read.extract(data,'u1',attribute_mode='point')
    uy=read.extract(data,'u2',attribute_mode='point')
    uz=read.extract(data,'u3',attribute_mode='point')
    lfac=read.extract(data,'lfac',attribute_mode='point')
    
    bdash = np.empty((len(bx),3))
    bdash[:,0] = bx/lfac + ux/lfac*(bx*ux+by*uy+bz*uz)/c/c
    bdash[:,1] = by/lfac + uy/lfac*(bx*ux+by*uy+bz*uz)/c/c
    bdash[:,2] = bz/lfac + uz/lfac*(bx*ux+by*uy+bz*uz)/c/c

    return bdash
Пример #16
0
def get(pointdata):

    print '=== Calculating local anisotropy ==='

    bcyl = get_bcyl(pointdata)

    m = np.square(bcyl[0]) + np.square(bcyl[2]) + np.square(bcyl[1]) <= 1.e-14

    lfac = read.extract(pointdata, 'lfac', attribute_mode='point')
    m = m | (lfac >= 9.)

    aniso = np.square(bcyl[0]) + np.square(bcyl[2]) / (
        np.square(bcyl[0]) + np.square(bcyl[2]) + np.square(bcyl[1]))

    m = m | (np.isnan(aniso))
    aniso = np.ma.masked_array(aniso, m)

    print '=== Done with local anisotropy ==='

    return [aniso, m]
Пример #17
0
def emiss(data,phi,theta,nu,alpha,recipe=1,polarized=1):

    print '=== Getting emissivities... ==='

# peel data:
    pointdata=data.get('pointdata')

# Synchrotron constants:
    
    c  = 2.99792458e10 # cm s^-1
    c1 = 6.27e18 # cm^-7/2 g^-5/2 s^4
    c2 = 2.368e-3 # g^-2 cm^-1 s^3
    me = 9.1093897e-28 # g

# Euclidian norm:
    norm = lambda x: np.sqrt(
    np.square(x[:,0]) + np.square(x[:,1]) + np.square(x[:,2])
    )
# Scalar product:
    scalar = lambda x,y: x[:,0]*y[:,0] + x[:,1]*y[:,1] + x[:,2]*y[:,2]

# Gamma from alpha:
    Gamma = 2.*alpha + 1.

# points:
    
    lfac=read.extract(pointdata,'lfac',attribute_mode='point')
    ux=read.extract(pointdata,'v1',attribute_mode='point')*lfac
    uy=read.extract(pointdata,'v2',attribute_mode='point')*lfac
    uz=read.extract(pointdata,'v3',attribute_mode='point')*lfac

    nelem = len(ux)


# get line of sight vector:
# phi and theta should be given in deg:
    n = np.empty((nelem,3))
    sintheta = np.sin(np.pi/180.*theta)
    costheta = np.cos(np.pi/180.*theta)
    sinphi   = np.sin(np.pi/180.*phi)
    cosphi   = np.cos(np.pi/180.*phi)
    n[:,0] = sintheta*cosphi
    n[:,1] = sintheta*sinphi
    n[:,2] = costheta


# get doppler factor:
    beta = np.empty((nelem,3))
    beta[:,0] = ux/lfac
    beta[:,1] = uy/lfac
    beta[:,2] = uz/lfac

    D = np.empty(nelem)
    D = 1./(
    lfac*(1.-(scalar(n,beta)))
    )

    print 'min and max Doppler factors: ', D.min(), D.max()

# get ndash:
    ndash = np.empty((nelem,3))
    ndash[:,0] = D*n[:,0] - (D+1.) * lfac/(lfac+1) * beta[:,0]
    ndash[:,1] = D*n[:,1] - (D+1.) * lfac/(lfac+1) * beta[:,1]
    ndash[:,2] = D*n[:,2] - (D+1.) * lfac/(lfac+1) * beta[:,2]

    print 'got ndash min and max:', ndash[:,0].min(), ndash[:,0].max(), ndash[:,1].min(), ndash[:,1].max(), ndash[:,2].min(), ndash[:,2].max()

# get bdash:
    if recipe ==1 or recipe ==2:
        bdash = get_bdash(pointdata)

        print 'got bdash min and max:', bdash[:,0].min(), bdash[:,0].max(), bdash[:,1].min(), bdash[:,1].max(), bdash[:,2].min(), bdash[:,2].max()

# bdash perp:
        bdashp = np.empty(nelem)
        tmp    = np.empty((nelem,3))
        tmp    = np.cross(ndash,bdash)
        bdashp = norm(tmp)

    else:
        bdashp = np.sqrt(read.extract(pointdata,'p',attribute_mode='point'))

    print 'got bdashp min and max: ', bdashp.min(), bdashp.max()

# emissivity:
    if recipe ==1:
        rhoe=read.extract(pointdata,'rhoe',attribute_mode='point')
        rhoe_0=read.extract(pointdata,'rhoe_0',attribute_mode='point')
    elif recipe ==2:
        rhoe=read.extract(pointdata,'n',attribute_mode='point')
        rhoe_0=read.extract(pointdata,'n0',attribute_mode='point')

    if recipe ==1 or recipe ==2:
# electron densities and cutoff:
        eps_inf=read.extract(pointdata,'eps_inf',attribute_mode='point')
        
        emiss = (c2*rhoe_0/me*c2*np.power(c1,(Gamma-3.)/2.)/(8.*np.pi) *
                 np.power(D,2.+(Gamma-1)/2.) *
                 powerorzero(bdashp,(Gamma+1.)/2.)*
        np.power(rhoe_0/rhoe,-((Gamma+2.)/3.)) *
        np.power(nu,(1.-Gamma)/2.) *
        powerorzero((1. - np.sqrt(nu)/(np.sqrt(c1*bdashp)*eps_inf)),Gamma-2.)
        )
# mask out the wind zone:
        m = lfac >9.
        emiss = np.ma.filled(np.ma.masked_array(emiss,m),0.)
    else:
        rho=read.extract(pointdata,'rho',attribute_mode='point')
        emiss = rho*np.power(D,2.+(Gamma-1)/2.)*powerorzero(bdashp,(Gamma+1.)/2.)
            
    if polarized !=1:
        print '=== Done with emissivities! ==='
    return {'emiss': emiss, 'points': data.get('points'),
            'phi': phi, 'theta': theta, 'nu': nu, 'alpha': alpha, 'recipe': recipe}
# The following only if polarized emission is requested:

# direction of emission:
    q       = np.empty((nelem,3))
    absb    = np.empty(nelem)
    b       = np.empty((nelem,3))
    ehat    = np.empty((nelem,3))
    absehat = np.empty((nelem))
    lhat    = np.empty((nelem,3))
    coschie = np.empty(nelem)
    sinchie = np.empty(nelem)
    cos2chie= np.empty(nelem)
    sin2chie= np.empty(nelem)
     
    b[:,0] =read.extract(pointdata,'b1',attribute_mode='point')
    b[:,1] =read.extract(pointdata,'b2',attribute_mode='point')
    b[:,2] =read.extract(pointdata,'b3',attribute_mode='point')

    absb = norm(b)

    q = b + np.cross(n,np.cross(beta,b))
    q[:,0] = q[:,0]/absb
    q[:,1] = q[:,1]/absb
    q[:,2] = q[:,2]/absb

    ehat      = np.cross(n,q) 
    absehat   = norm(ehat)
    ehat[:,0] = ehat[:,0]/absehat
    ehat[:,1] = ehat[:,1]/absehat
    ehat[:,2] = ehat[:,2]/absehat

    lhat[:,0] = -costheta*cosphi
    lhat[:,1] = -costheta*sinphi
    lhat[:,2] = sintheta

    coschie = scalar(lhat,ehat)
    sinchie = scalar(n,np.cross(lhat,ehat))

# complex phase with Doppelwinkelsatz:
    sin2chie = 2.*sinchie*coschie
    cos2chie = np.square(coschie)-np.square(sinchie)

# assemble complex polarized emissivity:
    piem = (Gamma+1)/(Gamma+7./3.)
    emisspol = np.ma.filled(np.ma.masked_array(piem * emiss * (cos2chie + 1j* sin2chie),absb==0),0.)

    print '=== Done with emissivities! ==='
    return {'emiss': emiss, 'emisspol': emisspol, 'points': data.get('points'),
            'phi': phi, 'theta': theta, 'nu': nu, 'alpha': alpha, 'recipe': recipe}
Пример #18
0
def rot2D(offset,nphi,filenameout='data',type='pvtu'):
    """
    creates a mock vtu datastructure with rotated values.
    No connectivity is set, vtu is just used as a container for pointdata     since
    the following routines expect to get a vtu unstructured datatype.
    """

    data=get_pointdata(offset,filenameout=filenameout,type=type)
    points2D=data.get('points')
    data3D = v.vtkUnstructuredGrid()

    nelem2D = len(points2D)
    nelem3D = nelem2D*nphi

    print 'preparing ',nelem3D, 'points'

    points3D = np.empty((nelem3D,3))

    phis=np.linspace(0.,2.*np.pi*float(nphi)/float(nphi+1),nphi)

    i3D = 0
    for i2D in range(nelem2D):
# pick next 2D point:
        point = points2D[i2D]
        r = point[0]
        z = point[1]
        for phi in phis:
            sinphi = np.sin(phi)
            cosphi = np.cos(phi)
            points3D[i3D,0] = r*cosphi
            points3D[i3D,1] = r*sinphi
            points3D[i3D,2] = z
            i3D=i3D+1

    vtkpoints=ah.array2vtkPoints(points3D)
    data3D.SetPoints(vtkpoints)

# rotate variables:
    ivar = 0
    while ivar < data.get('pointdata').GetPointData().GetNumberOfArrays():
        var = data.get('pointdata').GetPointData().GetArrayName(ivar)
        print 'rotating variable:',ivar,var
        i3D=0
# Treat vectors:
        if var == 'u1' or var == 'b1' or var == 'v1':
            array2D_vec = np.empty((nelem2D,3))
            array3D_vec = np.empty((nelem3D,3))
            bx = np.empty(nelem3D)
            by = np.empty(nelem3D)
            bz = np.empty(nelem3D)
            array2D_vec[:,0] = read.extract(data.get('pointdata'),data.get('pointdata').GetPointData().GetArrayName(ivar),attribute_mode='point')
            array2D_vec[:,1] = read.extract(data.get('pointdata'),data.get('pointdata').GetPointData().GetArrayName(ivar+1),attribute_mode='point')
            array2D_vec[:,2] = read.extract(data.get('pointdata'),data.get('pointdata').GetPointData().GetArrayName(ivar+2),attribute_mode='point')
            for i2D in range(nelem2D):
                for phi in phis:
# Should have three components (makes no sense otherwise):
# Input in cylindrical coordinates, convention:
# b1==br; b2==bz; b3==bphi  and similar for velocities.
                    array3D_vec[i3D,0] = array2D_vec[i2D,0]
                    array3D_vec[i3D,1] = array2D_vec[i2D,1]
                    array3D_vec[i3D,2] = array2D_vec[i2D,2]
# Now create the rotated vectors:
                    sinphi = np.sin(phi)
                    cosphi = np.cos(phi)
                    bz[i3D] = array3D_vec[i3D,1]  
                    bx[i3D] = array3D_vec[i3D,0] * cosphi - array3D_vec[i3D,2] * sinphi
                    by[i3D] = array3D_vec[i3D,2] * cosphi + array3D_vec[i3D,0] * sinphi
                    i3D=i3D+1

            vtkarray3D = ah.array2vtk(bx)
            vtkarray3D.SetName(data.get('pointdata').GetPointData().GetArrayName(ivar))
            data3D.GetPointData().AddArray(vtkarray3D)

            vtkarray3D = ah.array2vtk(by)
            vtkarray3D.SetName(data.get('pointdata').GetPointData().GetArrayName(ivar+1))
            data3D.GetPointData().AddArray(vtkarray3D)
 
            vtkarray3D = ah.array2vtk(bz)
            vtkarray3D.SetName(data.get('pointdata').GetPointData().GetArrayName(ivar+2))
            data3D.GetPointData().AddArray(vtkarray3D)
  
            ivar = ivar+3
        else:
# Treat scalars:
            array2D = np.empty(nelem2D)
            array3D = np.empty(nelem3D)
            array2D = read.extract(data.get('pointdata'),data.get('pointdata').GetPointData().GetArrayName(ivar),attribute_mode='point')
            for i2D in range(nelem2D):
                for phi in phis:
                    array3D[i3D] = array2D[i2D]
                    i3D=i3D+1
            ivar = ivar+1

            vtkarray3D = ah.array2vtk(array3D)
            vtkarray3D.SetName(var)
            data3D.GetPointData().AddArray(vtkarray3D)
    
    data3D.Update()
    return {'pointdata': data3D, 'points': points3D}
Пример #19
0
def emissFrom2D(data,theta=0,npixel=[200,200,200],L=[100,100,100],x0=[0,0,0],nu=1,alpha=0.7,delta=0):

    # Scalar product:
    scalar = lambda x,y: x[:,0]*y[:,0] + x[:,1]*y[:,1] + x[:,2]*y[:,2]

    # Gamma from alpha:
    Gamma = 2.*alpha + 1.

    # Since this is from an axisymmetric run, we put phi=0
    phi=0
    grid=make_grid(theta,phiy,phi,npixel,L,delta=delta,x0=x0)

    # get back the points:
    points=data.get('points')


    # get velocities for Doppler factor:
    ur = ontoGrid(read.extract(data.get('pointdata'),'u1',attribute_mode='point')
                  ,theta,npixel,L,points,delta=delta,x0=x0)
    uz = ontoGrid(read.extract(data.get('pointdata'),'u2',attribute_mode='point')
                  ,theta,npixel,L,points,delta=delta,x0=x0)
    uphi = ontoGrid(read.extract(data.get('pointdata'),'u3',attribute_mode='point')
                  ,theta,npixel,L,points,delta=delta,x0=x0)

    # Lorentz factor:
    lfac = np.sqrt(1.+(ur**2+uz**2+uphi**2))

    # Cartesian velocity components:
    rcyl = np.sqrt(grid[0]**2+grid[1]**2)
    cosphi = grid[0] / rcyl
    sinphi = grid[1] / rcyl

    ux = (ur*cosphi - uphi*sinphi)
    uy = (ur*sinphi + uphi*cosphi)
    # Clear memory:
    del ur, uphi, cosphi, sinphi, rcyl

    nelem = len(ux)


# get line of sight vector:
# phi and theta should be given in deg:
    n = np.empty((nelem,3))
    sintheta = np.sin(np.pi/180.*theta)
    costheta = np.cos(np.pi/180.*theta)
    sinphi   = 0.
    cosphi   = 1.
    n[:,0] = sintheta*cosphi
    n[:,1] = sintheta*sinphi
    n[:,2] = costheta

    beta = np.empty((nelem,3))
    beta[:,0] = ux/lfac
    beta[:,1] = uy/lfac
    beta[:,2] = uz/lfac

    # Doppler factor:
    D = np.empty(nelem)
    D = 1./(
    lfac*(1.-(scalar(n,beta)))
    )

    # Clear memory:
    del n, beta, lfac

    print '=== Minimum and maximum Doppler factor: ', D.min(), D.max(), ' ==='

    # say, get var p:
    p=ontoGrid(read.extract(data.get('pointdata'),'p',attribute_mode='point')
               ,theta,npixel,L,points,delta=delta,x0=x0)

    emissivity=D**(2.+alpha)*p**((3.+alpha)/2.) * nu**(-alpha)
#    emissivity=p

    return {'emiss':emissivity,'points':points,
            'phi': phi, 'theta': theta, 'delta': delta, 'nu': nu, 'alpha': alpha, 'recipe': 0,
            'L':L,'npixel':npixel,'x0':x0}
Пример #20
0
def emiss(data,phi,theta,nu,alpha,recipe=1,polarized=1,delta=0,noswing=None,epsb=0.):

    print '=== Getting emissivities... ==='

# peel data:
    pointdata=data.get('pointdata')

# Synchrotron constants:
    
    c  = 2.99792458e10 # cm s^-1
    c1 = 6.27e18 # cm^-7/2 g^-5/2 s^4
    c2 = 2.368e-3 # g^-2 cm^-1 s^3
    me = 9.1093897e-28 # g

# Euclidian norm:
    norm = lambda x: np.sqrt(
    np.square(x[:,0]) + np.square(x[:,1]) + np.square(x[:,2])
    )
# Scalar product:
    scalar = lambda x,y: x[:,0]*y[:,0] + x[:,1]*y[:,1] + x[:,2]*y[:,2]

# Gamma from alpha:
    Gamma = 2.*alpha + 1.

# points:
    ux=read.extract(pointdata,'u1',attribute_mode='point')
    uy=read.extract(pointdata,'u2',attribute_mode='point')
    uz=read.extract(pointdata,'u3',attribute_mode='point')
    lfac=read.extract(pointdata,'lfac',attribute_mode='point')

    nelem = len(ux)


# get line of sight vector:
# phi and theta should be given in deg:
    n = np.empty((nelem,3))
    sintheta = np.sin(np.pi/180.*theta)
    costheta = np.cos(np.pi/180.*theta)
    sinphi   = np.sin(np.pi/180.*phi)
    cosphi   = np.cos(np.pi/180.*phi)
    n[:,0] = sintheta*cosphi
    n[:,1] = sintheta*sinphi
    n[:,2] = costheta


# get doppler factor:
    beta = np.empty((nelem,3))
    beta[:,0] = ux/lfac/c
    beta[:,1] = uy/lfac/c
    beta[:,2] = uz/lfac/c

    D = np.empty(nelem)
    D = 1./(
    lfac*(1.-(scalar(n,beta)))
    )

    print 'min and max Doppler factors: ', D.min(), D.max()

# get ndash:
    ndash = np.empty((nelem,3))
    ndash[:,0] = D*n[:,0] - (D+1.) * lfac/(lfac+1) * beta[:,0]
    ndash[:,1] = D*n[:,1] - (D+1.) * lfac/(lfac+1) * beta[:,1]
    ndash[:,2] = D*n[:,2] - (D+1.) * lfac/(lfac+1) * beta[:,2]

    print 'got ndash min and max:', ndash[:,0].min(), ndash[:,0].max(), ndash[:,1].min(), ndash[:,1].max(), ndash[:,2].min(), ndash[:,2].max()

# get bdash:
    bdash = get_bdash(pointdata)

    if epsb > 0. :
# keep direction of magnetic field, but assume magnitude from equipartition 
# with thermal energy.  
# Equipartition fraction is epsb, such that E_b = epsb * 3 * p
        p = read.extract(pointdata,'p',attribute_mode='point')
        normbdash = norm(bdash)
        e = 3.*p + normbdash**2/(8.*np.pi)
        underCutoff = normbdash**2/(8.*np.pi) < epsb * 3.* p

        bdash[underCutoff,0] = bdash[underCutoff,0] * powerorzero(normbdash[underCutoff],-1) * np.sqrt(8.*np.pi*e[underCutoff]/(1./epsb+1.))
        bdash[underCutoff,1] = bdash[underCutoff,1] * powerorzero(normbdash[underCutoff],-1) * np.sqrt(8.*np.pi*e[underCutoff]/(1./epsb+1.))
        bdash[underCutoff,2] = bdash[underCutoff,2] * powerorzero(normbdash[underCutoff],-1) * np.sqrt(8.*np.pi*e[underCutoff]/(1./epsb+1.))



    print 'got bdash min and max:', bdash[:,0].min(), bdash[:,0].max(), bdash[:,1].min(), bdash[:,1].max(), bdash[:,2].min(), bdash[:,2].max()

# bdash perp:
    bdashp = np.empty(nelem)
    tmp    = np.empty((nelem,3))
    tmp    = np.cross(ndash,bdash)
    bdashp = norm(tmp)

    print 'got bdashp min and max: ', bdashp.min(), bdashp.max()

# electron densities and cutoff:
    eps_inf=read.extract(pointdata,'eps_inf',attribute_mode='point')

# emissivity:
    if recipe ==1:
        rhoe=read.extract(pointdata,'rhoe',attribute_mode='point')
        rhoe_0=read.extract(pointdata,'rhoe_0',attribute_mode='point')
    elif recipe ==2:
        rhoe=read.extract(pointdata,'n',attribute_mode='point')
        rhoe_0=read.extract(pointdata,'n0',attribute_mode='point')

    if recipe == 1 or recipe ==2:
        emiss = (c2*rhoe_0/me*c2*np.power(c1,(Gamma-3.)/2.)/(8.*np.pi) *
                 np.power(D,2.+(Gamma-1)/2.) *
                 powerorzero(bdashp,(Gamma+1.)/2.)*
                 np.power(rhoe_0/rhoe,-((Gamma+2.)/3.)) *
                 np.power(nu,(1.-Gamma)/2.) *
                 powerorzero((1. - np.sqrt(nu)/(np.sqrt(c1*bdashp)*eps_inf)),Gamma-2.)
             )

    if recipe == 3:
        emiss = (norm(bdash)**2 *
                 np.power(D,2.+(Gamma-1)/2.) *
                 powerorzero(bdashp,(Gamma+1.)/2.)*
                 np.power(nu,(1.-Gamma)/2.)
             )


    if recipe == 4:
        p = read.extract(pointdata,'p',attribute_mode='point')
        emiss = (p *
                 np.power(D,2.+(Gamma-1)/2.) *
                 powerorzero(bdashp,(Gamma+1.)/2.)*
                 np.power(nu,(1.-Gamma)/2.)
             )

# mask out the wind zone:
    m = lfac >9.
    emiss = np.ma.filled(np.ma.masked_array(emiss,m),0.)

    if polarized !=1:
        print '=== Done with emissivities! ==='
        return {'emiss': emiss, 'points': data.get('points'),
                'phi': phi, 'theta': theta, 'delta': delta, 'nu': nu, 'alpha': alpha, 'recipe': recipe, 'epsb': epsb}
# The following only if polarized emission is requested:

# direction of emission:
    q       = np.empty((nelem,3))
    absb    = np.empty(nelem)
    b       = np.empty((nelem,3))
    ehat    = np.empty((nelem,3))
    absehat = np.empty((nelem))
    lhat    = np.empty((nelem,3))
    coschie = np.empty(nelem)
    sinchie = np.empty(nelem)
    cos2chie= np.empty(nelem)
    sin2chie= np.empty(nelem)
     
    b[:,0] =read.extract(pointdata,'b1',attribute_mode='point')
    b[:,1] =read.extract(pointdata,'b2',attribute_mode='point')
    b[:,2] =read.extract(pointdata,'b3',attribute_mode='point')

    absb = norm(b)

# to test the effect of the swing you can use the noswing parameter:
    if noswing==None:
        q = b + np.cross(n,np.cross(beta,b))
    else:
        q = b

    q[:,0] = q[:,0]/absb
    q[:,1] = q[:,1]/absb
    q[:,2] = q[:,2]/absb

    ehat      = np.cross(n,q) 
    absehat   = norm(ehat)
    ehat[:,0] = ehat[:,0]/absehat
    ehat[:,1] = ehat[:,1]/absehat
    ehat[:,2] = ehat[:,2]/absehat

    lhat1 = -costheta*cosphi
    lhat2 = -costheta*sinphi
    lhat3 = sintheta

    if delta!=0:
        cosa = np.cos(delta*np.pi/180.)
        sina = np.sin(delta*np.pi/180.)

        n1 = sintheta*cosphi
        n2 = sintheta*sinphi
        n3 = costheta
        
        xiddd = np.empty(nelem)
        yiddd = np.empty(nelem)
        ziddd = np.empty(nelem)
        
        omca  = 1.-cosa
        lhatd1 = (n1**2*omca+cosa)*lhat1 + (n1*n2*omca-n3*sina)*lhat2 + (n1*n3*omca+n2*sina)*lhat3
        lhatd2 = (n2*n1*omca+n3*sina)*lhat1 + (n2**2*omca+cosa)*lhat2 + (n2*n3*omca-n1*sina)*lhat3
        lhatd3 = (n3*n1*omca-n2*sina)*lhat1 + (n3*n2*omca+n1*sina)*lhat2 + (n3**2*omca+cosa)*lhat3

        lhat1=lhatd1
        lhat2=lhatd2
        lhat3=lhatd3
        
    lhat[:,0] = lhat1
    lhat[:,1] = lhat2
    lhat[:,2] = lhat3


    coschie = scalar(lhat,ehat)
    sinchie = scalar(n,np.cross(lhat,ehat))

# complex phase with Doppelwinkelsatz:
    sin2chie = 2.*sinchie*coschie
    cos2chie = np.square(coschie)-np.square(sinchie)

# assemble complex polarized emissivity:
    piem = (Gamma+1)/(Gamma+7./3.)
    emisspol = np.ma.filled(np.ma.masked_array(piem * emiss * (cos2chie + 1j* sin2chie),absb==0),0.)

    print '=== Done with emissivities! ==='
    return {'emiss': emiss, 'emisspol': emisspol, 'points': data.get('points'),
            'phi': phi, 'theta': theta, 'delta': delta, 'nu': nu, 'alpha': alpha, 'recipe': recipe, 'epsb': epsb}
Пример #21
0
def simpleEmiss(data,phi,theta,var='p',delta=0):

    return {'emiss': read.extract(data.get('pointdata'),var,attribute_mode='point'),'points':data.get('points'),'phi':phi,'theta':theta,'delta':delta,'nu':1e12,'alpha':0.6,'recipe':1}
Пример #22
0
def rot2D(offset,nphi,filenameout='data',type='pvtu'):
    """
    creates a mock vtu datastructure with rotated values.
    No connectivity is set, vtu is just used as a container for pointdata     since
    the following routines expect to get a vtu unstructured datatype.
    """

    data=get_pointdata(offset,filenameout=filenameout,type='pvtu')
    points2D=data.get('points')
    data3D = v.vtkUnstructuredGrid()

    nelem2D = len(points2D)
    nelem3D = nelem2D*nphi

    print 'preparing ',nelem3D, 'points'

    points3D = np.empty((nelem3D,3))

    phis=np.linspace(0.,2.*np.pi*float(nphi)/float(nphi+1),nphi)

    i3D = 0
    for i2D in range(nelem2D):
# pick next 2D point:
        point = points2D[i2D]
        r = point[0]
        z = point[1]
        for phi in phis:
            sinphi = np.sin(phi)
            cosphi = np.cos(phi)
            points3D[i3D,0] = r*cosphi
            points3D[i3D,1] = r*sinphi
            points3D[i3D,2] = z
            i3D=i3D+1

    vtkpoints=ah.array2vtkPoints(points3D)
    data3D.SetPoints(vtkpoints)

# rotate variables:
    ivar = 0
    while ivar < data.get('pointdata').GetPointData().GetNumberOfArrays():
        var = data.get('pointdata').GetPointData().GetArrayName(ivar)
        print 'rotating variable:',ivar,var
        i3D=0
# Treat vectors:
        if var == 'u1' or var == 'b1' or var == 'v1':
            array2D_vec = np.empty((nelem2D,3))
            array3D_vec = np.empty((nelem3D,3))
            bx = np.empty(nelem3D)
            by = np.empty(nelem3D)
            bz = np.empty(nelem3D)
            array2D_vec[:,0] = read.extract(data.get('pointdata'),data.get('pointdata').GetPointData().GetArrayName(ivar),attribute_mode='point')
            array2D_vec[:,1] = read.extract(data.get('pointdata'),data.get('pointdata').GetPointData().GetArrayName(ivar+1),attribute_mode='point')
            array2D_vec[:,2] = read.extract(data.get('pointdata'),data.get('pointdata').GetPointData().GetArrayName(ivar+2),attribute_mode='point')
            for i2D in range(nelem2D):
                for phi in phis:
# Should have three components (makes no sense otherwise):
# Input in cylindrical coordinates, convention:
# b1==br; b2==bz; b3==bphi  and similar for velocities.
                    array3D_vec[i3D,0] = array2D_vec[i2D,0]
                    array3D_vec[i3D,1] = array2D_vec[i2D,1]
                    array3D_vec[i3D,2] = array2D_vec[i2D,2]
# Now create the rotated vectors:
                    sinphi = np.sin(phi)
                    cosphi = np.cos(phi)
                    bz[i3D] = array3D_vec[i3D,1]  
                    bx[i3D] = array3D_vec[i3D,0] * cosphi - array3D_vec[i3D,2] * sinphi
                    by[i3D] = array3D_vec[i3D,2] * cosphi + array3D_vec[i3D,0] * sinphi
                    i3D=i3D+1

            vtkarray3D = ah.array2vtk(bx)
            vtkarray3D.SetName(data.get('pointdata').GetPointData().GetArrayName(ivar))
            data3D.GetPointData().AddArray(vtkarray3D)

            vtkarray3D = ah.array2vtk(by)
            vtkarray3D.SetName(data.get('pointdata').GetPointData().GetArrayName(ivar+1))
            data3D.GetPointData().AddArray(vtkarray3D)
 
            vtkarray3D = ah.array2vtk(bz)
            vtkarray3D.SetName(data.get('pointdata').GetPointData().GetArrayName(ivar+2))
            data3D.GetPointData().AddArray(vtkarray3D)
  
            ivar = ivar+3
        else:
# Treat scalars:
            array2D = np.empty(nelem2D)
            array3D = np.empty(nelem3D)
            array2D = read.extract(data.get('pointdata'),data.get('pointdata').GetPointData().GetArrayName(ivar),attribute_mode='point')
            for i2D in range(nelem2D):
                for phi in phis:
                    array3D[i3D] = array2D[i2D]
                    i3D=i3D+1
            ivar = ivar+1

            vtkarray3D = ah.array2vtk(array3D)
            vtkarray3D.SetName(var)
            data3D.GetPointData().AddArray(vtkarray3D)
    
    data3D.Update()
    return {'pointdata': data3D, 'points': points3D}