Ejemplo n.º 1
0
def field_with_streams(F, Vx, Vy, outname):
    fig, ax = plt.subplots()
    size = F.shape
    dx = 1. / nar(size)
    Y, X = np.mgrid[0:size[1]:1, 0:size[0]:1]
    ax.imshow(F, origin='lower', interpolation='nearest')
    Vxbar = np.mean(Vx)
    varx = Vx - Vxbar
    Vybar = np.mean(Vy)
    vary = Vy - Vybar
    stat(varx)
    ax.streamplot(X, Y, 10 * varx + 0.1 * Vxbar, 10 * vary + 0.1 * Vybar)
    fig.savefig(outname)
    plt.close(fig)
    turb_quan.plotter2(
        [Vx - np.mean(Vx), Vy - np.mean(Vy)],
        'p49c_plots/means.png',
        norm='ind',
        #axis_labels=stuff['axis_labels'],
        labs=['x-xbar', 'y-ybar'])
Ejemplo n.º 2
0
def wave_to_EB(wave, theta=np.pi / 4, size=16, mode_tool=modes_k001):
    state, fft = modes(theta=theta,
                       wave=wave,
                       h0=1,
                       size=size,
                       mode_tool=mode_tool)
    qu = state_to_qu(state, line_of_sight=0)
    #E,B,Eh,Bh,Qh,Uh = p49_QU2EB.EBfromQU(qu['Q'],qu['U'], return_quharm=True)
    EB = p49_QU2EB.EBfromQU(qu['Q'], qu['U'], return_quharm=True)
    turb_quan.plotter(qu['Q'],
                      qu['U'],
                      EB['E'],
                      EB['B'],
                      'p49c_plots/test_EB_%s_%02f.png' % (wave, theta),
                      norm='ind',
                      axis_labels=qu['axis_labels'])
    flat = {}
    for f in ['n', 'H2', 'Hh', 'Hv']:
        flat[f] = np.sum(qu[f], axis=0)
        flat[f].shape = (qu[f].shape[0], qu[f].shape[1])
    turb_quan.plotter2([flat['n'], flat['H2'], flat['Hh'], flat['Hv']],
                       'p49c_plots/test_Physics_%s_%0.2f.png' % (wave, theta),
                       labs=['n', 'B', 'Hh', 'Hv'],
                       norm='ind',
                       axis_labels=qu['axis_labels'])
    plots.field_with_streams(flat['n'], flat['Hh'], flat['Hv'],
                             'p49c_plots/with_streams.png')
    outname = 'p49c_plots/test_QhUhEhBh_%s_%0.2f.png' % (wave, theta)
    turb_quan.plotter(np.abs(EB['Qh']),
                      np.abs(EB['Uh']),
                      np.abs(EB['Eh']),
                      np.abs(EB['Bh']),
                      outname,
                      labs=['Qh', 'Uh', 'Eh', 'Bh'],
                      norm='positive',
                      axis_labels=qu['axis_labels'])
    print("Plot %s" % outname)
    output = {'fft': fft, 's': state}  #,'Q':qu['Q'],'U':qu['U'],'qu':qu}
    output.update(qu)
    output.update(EB)
    return output
Ejemplo n.º 3
0
        end = 1
    y, x = np.mgrid[0:end:dx, 0:end:dx]
kp_unit = nar([0, 1]) * 2 * np.pi
rho = (np.exp(1j * (kp_unit[0] * x + kp_unit[1] * y))).imag
if 0:
    kp_unit = nar([2, 1]) * 2 * np.pi
    rho += (np.exp(1j * (kp_unit[0] * x + kp_unit[1] * y))).imag

rhohat = np.fft.rfftn(rho)
plt.imshow(rhohat.real)
plt.imshow(rhohat.imag)
import turb_quan
reload(turb_quan)
turb_quan.plotter2([rho, rho, rho],
                   labs=['rho', 'rhohat', 'rhohat i', 'rhohat real'],
                   norm='ind',
                   fname='p49c_plots/2dgames_xspace.png',
                   share=False)
turb_quan.plotter2([np.abs(rhohat)],
                   npx=1,
                   zmin=1e-11,
                   labs=['rhohat', 'rhohat i', 'rhohat real'],
                   norm='positive',
                   fname='p49c_plots/2dgames_kspace.png',
                   share=True)
if 0:

    def su(x):
        y = np.abs(x).sum()
        if y < 1e-8:
            return "--"
Ejemplo n.º 4
0
def stuff_3d(A=[], B=[], K=[], Theta=[], linthreshy=1e-8):
    """in 3d!"""
    #x = np.arange(Q.size)
    output = {}
    size = 32
    twopi = np.pi * 2
    x, y, z = np.mgrid[0:1:1. / size, 0:1:1. / size, 0:1:1. / size]

    nmodes = len(A)
    consts = np.zeros([nmodes])
    linears = np.zeros([nmodes, size, size, size])
    sums = np.zeros([nmodes, size, size, size])
    for n in range(nmodes):
        consts[n] = A[n]
        linears[n, ...] = B[n] * (np.exp(
            1j * (2 * np.pi *
                  (K[n][0] * x + K[n][1] * y + K[n][2] * z) + Theta[n])))
        sums[n, ...] = consts[n] + linears[n, ...]

    plt.clf()
    slc = slice(3, 4), slice(3, 4), slice(None)
    the_x = z

    #slc = slice(None), slice(3,4),slice(3,4);the_x=x
    #slc = slice(None), slice(3,4),slice(3,4);the_x=x
    #slc = slice(None);the_x=x
    def xx(arr):
        return arr[slc].flatten()

    for n in range(nmodes):
        plt.plot(xx(the_x), xx(sums[n, ...]), label=n)
    Q_flat = np.prod(sums.real, axis=0)
    plt.plot(xx(the_x), xx(Q_flat), label='A*B*C')
    plt.legend(loc=0)
    plt.savefig('p49c_plots/fft_3db_test.png')
    actual_fft = np.fft.rfftn(Q_flat)
    qfft = actual_fft / (Q_flat.size)
    nonzero_k = nz(qfft)
    nonzero_v = nonzero(qfft)
    abs_qfft = np.abs(qfft)
    zmin = abs_qfft[abs_qfft > 0].min()
    turb_quan.plotter2(
        [
            lmax(qfft.real, 0),
            lmax(qfft.imag, 0),
            lmax(qfft.real, 1),
            lmax(qfft.imag, 1),
            lmax(qfft.real, 2),
            lmax(qfft.imag, 2)
        ],
        "p49c_plots/qfft_3da.png",
        norm='positive',
        zmin=zmin,
        labs=['real x', 'imag x', 'real y', 'imag y', 'real z', 'imag z'],
        share=False)

    def plotter3(arr, axis):
        dims = list(arr.shape)
        dims.pop(axis)
        flat = np.sum(arr, axis=axis)
        flat.shape = dims
        return flat

    turb_quan.plotter2([
        plotter3(sums[0, ...], 0),
        plotter3(sums[1, ...], 0),
        plotter3(sums[0, ...], 0),
        plotter3(Q_flat, 0)
    ],
                       "p49c_plots/real_x_3da.png",
                       labs=["Ax", "Bx", "Cx", "ABCx"],
                       share=False,
                       norm='ind')
    turb_quan.plotter2([
        plotter3(sums[0, ...], 1),
        plotter3(sums[1, ...], 1),
        plotter3(sums[0, ...], 1),
        plotter3(Q_flat, 1)
    ],
                       "p49c_plots/real_y_3da.png",
                       labs=["Ay", "By", "Cy", "ABCy"],
                       share=False,
                       norm='ind')
    turb_quan.plotter2([
        plotter3(sums[0, ...], 2),
        plotter3(sums[1, ...], 2),
        plotter3(sums[0, ...], 2),
        plotter3(Q_flat, 2)
    ],
                       "p49c_plots/real_z_3da.png",
                       labs=["Az", "Bz", "Cz", "ABCz"],
                       share=False,
                       norm='ind')

    values = {}
    values['A'] = nar(A)
    values['B'] = nar(B)
    values['K'] = nar(K)
    values['Theta'] = nar(Theta)
    reload(amp_tools)
    expect = amp_tools.modes_2(**values)
    output['labs'] = expect.pop('labs')
    output['full'] = expect.pop('full_history')

    output.update({'Q_flat': Q_flat, 'acutal_fft': actual_fft, 'qfft': qfft})
    output.update(values)
    output['expect'] = expect

    output['nonzero_v'] = nonzero_v
    output['nonzero_k'] = zip(*nonzero_k)
    output['nonz'] = dict(zip(output['nonzero_k'], output['nonzero_v']))

    def sanitize_real_vectors(ft):
        for v in ft:
            if v[2] == 0:
                pass

    #sanitize_real_vectors( output['nonz'])
    total_error = 0
    for n, vec in enumerate(output['nonzero_k']):
        if vec in expect:
            this_error = np.abs(expect[vec] - nonzero_v[n])
            total_error += this_error
            if this_error > 1e-12:
                print("expect off. actual %0.2e expect %0.2e diff %0.2d vec %s"%\
                      ( nonzero_v[n], expect[vec], this_error, str(vec)))
        else:
            print("expect missing vector %s" % str(vec))
    print("Total error: %0.2e" % total_error)

    #expect_k = nar(sorted(list(expect.keys())))
    #expect_v = nar([ expect[ key] for key in expect_k])
    #output['expect_v']=expect_v
    #output['expect_k']=expect_k
    output['total_error'] = total_error
    return output
Ejemplo n.º 5
0
def stuff_3d(save=None):
    """in 3d!"""
    #x = np.arange(Q.size)
    output={}
    size=32
    twopi=np.pi*2
    x,y,z = np.mgrid[0:1:1./size,0:1:1./size,0:1:1./size]
    #x = np.mgrid[0:1:1./size]#,0:1:1./size,0:1:1./size]
    #y=z=1
    #y=0
    #z=0
    #k_unit_int = nar([1,7,1])
    #k_unit_Q = np.array(k_unit_int)*twopi
    #ampl=0.1
    #Q_flat =  ampl* (np.exp(1j*(k_unit_Q[0]*x+k_unit_Q[1]*y+k_unit_Q[2]*z))).real

    A0 = 1
    A1 = np.sqrt(2.e-6)
    KAx=0
    KAy=0
    KAz=1
    KA = nar([KAx,KAy,KAz])
    Aa = A0
    Atheta = 0
    Ab = A1* (np.exp(1j*(2*np.pi*(KAx*x+KAy*y+KAz*z)+Atheta))).real
    A =  Aa+Ab

    B0 = 1
    B1 = np.sqrt(2.e-6)
    KBx=0
    KBy=0
    KBz=1
    KB = nar([KBx,KBy,KBz])
    Ba = B0
    Btheta = 0 ;-np.pi/2.
    Bb = B1* (np.exp(1j*(2*np.pi*(KBx*x+KBy*y+KBz*z)+Btheta))).real
    B =  Ba+Bb
    output['B']=B

    C0 = 1
    C1 = 0
    KCx=5
    KCy=0
    KCz=0
    KC = nar([KCx,KCy,KCz])
    Ca = C0
    Ctheta=1.2
    Cb = C1* (np.exp(1j*(2*np.pi*(KCx*x+KCy*y+KCz*z)+Ctheta))).real
    C =  Ca+Cb

    plt.clf()
    slc = slice(3,4),slice(3,4),slice(None);the_x = z
    #slc = slice(None), slice(3,4),slice(3,4);the_x=x
    #slc = slice(None), slice(3,4),slice(3,4);the_x=x
    #slc = slice(None);the_x=x
    def xx(arr):
        return arr[slc].flatten()


    plt.plot(xx(the_x),xx(A),label='A')
    plt.plot(xx(the_x),xx(B),label='B')
    plt.plot(xx(the_x),xx(C),label='C')
    plt.plot(xx(the_x),xx(A*B*C),label='A*B*C')
    plt.legend(loc=0)
    plt.savefig('p49c_plots/fft7_test.png')
    if save is None:
        Q_flat = (A*B*C)
        actual_fft=np.fft.rfftn(Q_flat)
    else:
        Q_flat = (A*B*C)[slc]
        Q_flat = save['Q_flat']
        Q_flat.shape = Q_flat.size
        actual_fft=np.fft.rfft(Q_flat)
    qfft=actual_fft/(Q_flat.size)
    nonzero_k= nz(qfft)
    nonzero_v = nonzero(qfft)
    turb_quan.plotter2( [lmax( qfft.real, 0), 
                         lmax( qfft.imag, 0),
                         lmax( qfft.real, 1), 
                         lmax( qfft.imag, 1),
                         lmax( qfft.real, 2), 
                         lmax( qfft.imag, 2)],
                       "p49c_plots/qfft_3da.png", 
                       labs=['real x','imag x','real y','imag y','real z','imag z'],
                       share=False)
    def plotter3(arr,axis):
        dims = list(arr.shape)
        dims.pop(axis)
        flat = np.sum(arr,axis=axis)
        flat.shape=dims
        return flat
    turb_quan.plotter2( [plotter3( A, 0), 
                         plotter3( B, 0),
                         plotter3( C, 0), 
                         plotter3( A*B*C, 0)],
                       "p49c_plots/real_x_3da.png", 
                       labs=["Ax","Bx","Cx","ABCx"],
                       share=False,norm='ind')
    turb_quan.plotter2( [plotter3( A, 1), 
                         plotter3( B, 1),
                         plotter3( C, 1), 
                         plotter3( A*B*C, 1)],
                       "p49c_plots/real_y_3da.png", 
                       labs=["Ay","By","Cy","ABCy"],
                       share=False,norm='ind')
    turb_quan.plotter2( [plotter3( A, 2), 
                         plotter3( B, 2),
                         plotter3( C, 2), 
                         plotter3( A*B*C, 2)],
                       "p49c_plots/real_z_3da.png", 
                       labs=["Az","Bz","Cz","ABCz"],
                       share=False,norm='ind')


    values={}
    values['KA']=KA[2]
    values['KB']=KB[2]
    values['KC']=KC[2]
    values['A0']=A0
    values['A1']=A1
    values['B0']=B0
    values['B1']=B1
    values['C0']=C0
    values['C1']=C1
    values['Atheta']=Atheta
    values['Btheta']=Btheta
    values['Ctheta']=Ctheta
    reload(amp_tools)
    expect = amp_tools.modes_1(**values)
    labs = expect.pop('labs')
    output.update({'Q_flat':Q_flat,'acutal_fft':actual_fft,'qfft':qfft})
    output.update(values)
    expect_k = nar(sorted(list(expect.keys())))
    expect_v = nar([ expect[ key] for key in expect_k])
    output['expect']=expect
    output['nonzero_v']=nonzero_v
    output['nonzero_k']=nonzero_k
    output['expect_v']=expect_v
    output['expect_k']=expect_k
    return output
Ejemplo n.º 6
0
                    if not os.path.exists(fname):
                        print("shoot.  Missing %s"%fname)
                        continue
                #if not os.path.exists(field):
                #    print("shoot.  Missing %s"%field)
                d[car.name]=di=pyfits.open(density,dtype=np.double)[0].data
                h[car.name]=hi=pyfits.open(field,dtype=np.double)[0].data
                q[car.name]=qi=pyfits.open(Q,dtype=np.double)[0].data
                u[car.name]=ui=pyfits.open(U,dtype=np.double)[0].data
                e[car.name]=ei=pyfits.open(E,dtype=np.double)[0].data
                b[car.name]=bi=pyfits.open(B,dtype=np.double)[0].data
                if 1:
                    outname = "p49six_%s_%s_%s.png"%(car.outname,set_output,ax)
                    turb_quan.plotter2([d[car.name], h[car.name],q[car.name],
                                        u[car.name],e[car.name],b[car.name]],
                                           outname,
                                           labs=['d','H','q','u','e','b'],
                                           norm='ind')
                    if 0:
                        outname = 'p49_qhist_%s_%s.png'%(car.outname,frame,set_output)
                        plt.hist(np.log(np.abs(q.flatten())),histtype='step',color='rgb'[nax])
                if 1:
                    outname_base = "%s__%04d_frb_%s_%s.png"
                    for name, array in  [ ['Q',qi],['U',ui],['E',ei],['B',bi]]:
                        outname = outname_base%( car.outname, frame, name, ax)
                        turb_quan.plotter2([array], outname, labs=[name], norm='ind',npx=1)

                
            if 0:
                outname = "p49six_density_%s_%s_%s.png"%(SERIES,set_output,ax)
                turb_quan.plotter2( [d[name] for name in car_list],