Ejemplo n.º 1
0
    def test_kernel_init(self):
        """ TODO: transform with unitary transformation and get original back"""

        NxMesh = 3
        NyMesh = 3

        #internal filter size is 3x Nx/y/zMesh
        NxF = NxMesh * 3
        NyF = NyMesh * 3

        flt = pytools.Filter(NxMesh, NyMesh)

        flt.init_kernel()
        kernel = np.zeros((NxF, NyF))
        kernel[0, 0] = 1.0
        flt.set_kernel(flatten(kernel))

        #image  = np.random.rand(NxF, NyF)
        #flt.set_image(  flatten(image) )

        k2 = reshape(flt.get_kernel(), NxF, NyF)
        #img2 = reshape( flt.get_image() , NxF, NyF)

        #print()
        #print(k2)

        flt.fft_kernel()
        k3 = reshape(flt.get_kernel(), NxF, NyF)
Ejemplo n.º 2
0
    def test_init(self):
        """ write to kernel & image variables and read back"""

        NxMesh = 3
        NyMesh = 3

        #internal filter size is 3x Nx/y/zMesh
        NxF = NxMesh * 3
        NyF = NyMesh * 3

        flt = pytools.Filter(NxMesh, NyMesh)

        kernel = np.random.rand(NxF, NyF)
        image = np.random.rand(NxF, NyF)

        flt.set_kernel(flatten(kernel))
        flt.set_image(flatten(image))

        k2 = flt.get_kernel()
        img2 = flt.get_image()

        k2 = reshape(k2, NxF, NyF)
        img2 = reshape(img2, NxF, NyF)

        for i in range(NxF):
            for j in range(NyF):
                self.assertAlmostEqual(kernel[i, j], k2[i, j], places=4)
                self.assertAlmostEqual(image[i, j], img2[i, j], places=4)
Ejemplo n.º 3
0
    def test_fft_backandforth(self):
        """FFT transform image forward and then backward to see if we get the same result back
         NOTE: floating-point conversion if not exact so we need some error tolerance"""

        NxMesh = 3
        NyMesh = 4  #make Nx != Ny to catch additional index bugs

        #internal filter size is 3x Nx/y/zMesh
        NxF = NxMesh * 3
        NyF = NyMesh * 3

        flt = pytools.Filter(NxMesh, NyMesh)

        #flt.init_kernel()
        #kernel = np.random.rand(NxF, NyF)
        #flt.set_kernel( flatten(kernel) )

        #create and set image
        image = np.random.rand(NxF, NyF)
        flt.set_image(flatten(image))
        img1 = reshape(flt.get_image(), NxF, NyF)
        #print("orig")
        #print(img1)

        flt.fft_image_forward()
        img2 = reshape(flt.get_image(), NxF, NyF)
        #print("fft forward")
        #print(img2)

        flt.fft_image_backward()
        img3 = reshape(flt.get_image(), NxF, NyF)
        #print("fft backward")
        #print(img3)

        for i in range(NxF):
            for j in range(NyF):
                self.assertAlmostEqual(img1[i, j], img3[i, j], places=4)
Ejemplo n.º 4
0
    #try:
    plotNode(axs[0], grid, conf)
    #plotXmesh(axs[1], grid, conf, 0, "x")
    saveVisz(-1, grid, conf)
    #except:
    #    print()
    #    pass

    Nsamples = conf.Nt

    fldprop = pyfld.FDTD2()
    pusher = pypic.BorisPusher()
    fintp = pypic.LinearInterpolator()
    currint = pypic.ZigZag()
    analyzer = pypic.Analyzator()
    flt = pytools.Filter(conf.NxMesh, conf.NyMesh)
    flt.init_gaussian_kernel(2.0, 2.0)

    #simulation loop
    time = 0.0
    ifile = 0
    for lap in range(0, conf.Nt):

        # Tristan loop
        #advance B half
        #move particles
        #advance B half
        #advance E
        #reset current (j=0)
        #deposit
        #exchange particles
Ejemplo n.º 5
0
    def test_filters_in_action(self):

        #plt.fig = plt.figure(1, figsize=(4,6))
        #plt.rc('font', family='serif', size=12)
        #plt.rc('xtick')
        #plt.rc('ytick')
        #gs = plt.GridSpec(3, 1)
        #
        #axs = []
        #for ai in range(3):
        #    axs.append( plt.subplot(gs[ai]) )

        conf = Conf()
        conf.Nx = 3
        conf.Ny = 3
        conf.Nz = 1
        conf.NxMesh = 10
        conf.NyMesh = 10
        conf.NzMesh = 1

        grid = pycorgi.twoD.Grid(conf.Nx, conf.Ny)
        grid.set_grid_lims(conf.xmin, conf.xmax, conf.ymin, conf.ymax)
        loadTiles(grid, conf)
        insert_em(grid, conf, linear_ramp)
        #inject(grid, filler_no_velocity, conf) #injecting plasma particles

        flt = pytools.Filter(conf.NxMesh, conf.NyMesh)
        flt.init_gaussian_kernel(4.0, 4.0)

        flt.get_padded_current(grid.get_tile(1, 1), grid)

        img = reshape(flt.get_image(), conf.NxMesh * 3, conf.NyMesh * 3)
        #axs[0].imshow(img[conf.NxMesh:2*conf.NxMesh, conf.NyMesh:2*conf.NyMesh]) #, vmin=vmin, vmax=vmax)
        #axs[0].imshow(img, vmin=0.0, vmax=100.0) #, vmin=vmin, vmax=vmax)

        # reference array
        data = np.zeros((conf.Nx * conf.NxMesh, conf.Ny * conf.NyMesh,
                         conf.Nz * conf.NzMesh, 3))
        for cid in grid.get_tile_ids():
            c = grid.get_tile(cid)
            (i, j) = c.index

            yee = c.get_yee(0)
            for k in range(conf.NyMesh):
                for q in range(conf.NxMesh):
                    for r in range(conf.NzMesh):
                        data[i * conf.NxMesh + q, j * conf.NyMesh + k,
                             0 * conf.NzMesh + r, 0] = yee.jx[q, k, r]
                        data[i * conf.NxMesh + q, j * conf.NyMesh + k,
                             0 * conf.NzMesh + r, 1] = yee.jy[q, k, r]
                        data[i * conf.NxMesh + q, j * conf.NyMesh + k,
                             0 * conf.NzMesh + r, 2] = yee.jz[q, k, r]

        #img2 = data[:,:,0,0]
        #axs[1].imshow(img2, vmin=0.0, vmax=100.0) #, vmin=vmin, vmax=vmax)

        for i in range(0, 3 * conf.NxMesh):
            for j in range(0, 3 * conf.NyMesh):
                self.assertEqual(data[i, j, 0, 0], img[i, j])  #jx

        flt.fft_image_forward()
        flt.apply_kernel()
        flt.fft_image_backward()
Ejemplo n.º 6
0
    def skip_test_gaussian(self):

        plt.fig = plt.figure(1, figsize=(4, 6))
        plt.rc('font', family='serif', size=12)
        plt.rc('xtick')
        plt.rc('ytick')
        gs = plt.GridSpec(4, 2)

        axs = []
        for ai in range(8):
            axs.append(plt.subplot(gs[ai]))

        NxMesh = 100
        NyMesh = 100

        #internal filter size is 3x Nx/y/zMesh
        NxF = NxMesh * 3
        NyF = NyMesh * 3

        vmin = 0.0
        vmax = 1.0

        flt = pytools.Filter(NxMesh, NyMesh)

        ###################################################
        # init kernel and create 3-point image
        flt.init_kernel()
        flt.init_3point(1)

        image = np.zeros((NxF, NyF))
        for i in range(0, NxMesh):
            for j in range(0, NyMesh):
                image[i + NxMesh, j + NyMesh] = np.random.rand(1)
        flt.set_image(flatten(image))

        img = reshape(flt.get_image(), NxF, NyF)
        axs[0].imshow(img[NxMesh:2 * NxMesh, NyMesh:2 * NyMesh],
                      vmin=vmin,
                      vmax=vmax)

        flt.fft_kernel()
        flt.fft_image_forward()
        flt.apply_kernel()
        flt.fft_image_backward()

        img2 = reshape(flt.get_image(), NxF, NyF)
        #axs[1].imshow(img2, vmin=vmin, vmax=vmax)

        cimg1 = img2[NxMesh:2 * NxMesh, NyMesh:2 * NyMesh]
        axs[1].imshow(cimg1, vmin=vmin, vmax=vmax)

        ##################################################
        # Gaussian (in FFT space) for comparison

        flt2 = pytools.Filter(NxMesh, NyMesh)
        flt2.set_image(flatten(image))

        sigx = 6.5
        sigy = 6.5
        #flt2.init_gaussian_kernel(sigx, sigy)
        flt2.init_lowpass_fft_kernel(140)

        #plot kernel in Fourier space
        ker2 = reshape(flt.get_kernel(), NxF, NyF)
        img3 = reshape(flt2.get_image(), NxF, NyF)

        cker1 = reshape(flt.get_kernel(), NxF, NyF)

        axs[2].imshow(img3[NxMesh:2 * NxMesh,
                           NyMesh:2 * NyMesh])  #, vmin=vmin, vmax=vmax)
        axs[3].imshow(fftshift(ker2))  #, vmin=vmin, vmax=vmax)

        flt2.fft_image_forward()
        flt2.apply_kernel()
        flt2.fft_image_backward()

        # visualize
        ker4 = reshape(flt2.get_kernel(), NxF, NyF)
        img4 = reshape(flt2.get_image(), NxF, NyF)

        cimg2 = img4[NxMesh:2 * NxMesh, NyMesh:2 * NyMesh]
        cker2 = fftshift(ker4)
        axs[4].imshow(cimg2, vmin=vmin, vmax=vmax)
        axs[5].imshow(cker2)  #, vmin=vmin, vmax=vmax)

        ###################################################
        # compute error between the two methods
        ierr = (cimg1 - cimg2) / cimg1
        #print( err )
        axs[6].imshow(ierr, vmin=-1.0, vmax=1.0)

        kerr = (cker1 - cker2) / cker1
        axs[7].imshow(kerr, vmin=-1.0, vmax=1.0)

        #print("max img orig: {}".format(img4.max() ))
        #print("max img fft : {}".format(img3.max() ))
        #print("max img conv: {}".format(img5.max() ))
        #print("max error   : {}".format( np.abs(err).max() ))

        #for i in range(NxMesh):
        #    for j in range(NyMesh):
        #        self.assertAlmostEqual(cimg_dc[i,j], cimg_fft[i,j], places=10)

        print("max img orig: {}".format(img.max()))
        print("max img fft : {}".format(img2.max()))
        print("max img gaus: {}".format(img4.max()))
        print("max error   : {}".format(np.abs(ierr).max()))
Ejemplo n.º 7
0
    def test_smearing2(self):

        #plt.fig = plt.figure(1, figsize=(4,6))
        #plt.rc('font', family='serif', size=12)
        #plt.rc('xtick')
        #plt.rc('ytick')
        #gs = plt.GridSpec(4, 2)
        #
        #axs = []
        #for ai in range(8):
        #    axs.append( plt.subplot(gs[ai]) )

        NxMesh = 50
        NyMesh = 50

        #internal filter size is 3x Nx/y/zMesh
        NxF = NxMesh * 3
        NyF = NyMesh * 3

        vmin = 0.0
        vmax = 1.0

        flt = pytools.Filter(NxMesh, NyMesh)

        ###################################################
        # init kernel
        #print("initing kernel.....")
        flt.init_kernel()
        flt.init_3point(1)

        #kernel = reshape( flt.get_kernel(), NxF, NyF)
        #print(kernel)

        ##################################################
        # create image

        image = np.zeros((NxF, NyF))
        for i in range(0, NxMesh):
            for j in range(0, NyMesh):
                image[i + NxMesh, j + NyMesh] = np.random.rand(1)

        flt.set_image(flatten(image))

        ##################################################
        # visualize

        ker = reshape(flt.get_kernel(), NxF, NyF)
        img = reshape(flt.get_image(), NxF, NyF)
        #print(ker)

        #axs[0].imshow(ker )#, vmin=vmin, vmax=vmax)
        #axs[1].imshow(img, vmin=vmin, vmax=vmax)

        ##################################################
        # fft

        flt.fft_kernel()
        flt.fft_image_forward()
        ker2 = reshape(flt.get_kernel(), NxF, NyF)
        img2 = reshape(flt.get_image(), NxF, NyF)

        #print()
        #print(ker2)

        #print(ker2.min(), ker2.max())
        #print(img2.min(), img2.max())

        #axs[2].imshow(fftshift(ker2) )#, vmin=vmin, vmax=vmax)
        #axs[3].imshow(fftshift(img2) )#, vmin=vmin, vmax=vmax)

        ##################################################
        # apply kernel

        flt.apply_kernel()
        flt.fft_image_backward()
        ker3 = reshape(flt.get_kernel(), NxF, NyF)
        img3 = reshape(flt.get_image(), NxF, NyF)

        #print(ker3.min(), ker3.max())
        #print(img3.min(), img3.max())

        #axs[4].imshow(ker3, vmin=vmin, vmax=vmax)
        #axs[5].imshow(img3, vmin=vmin, vmax=vmax)

        cimg_fft = img3[NxMesh:2 * NxMesh, NyMesh:2 * NyMesh]
        #axs[5].imshow(cimg_fft, vmin=vmin, vmax=vmax)

        ###################################################
        # digital filtering for comparison
        flt.set_image(flatten(image))  # set original image back
        img4 = reshape(flt.get_image(), NxF, NyF)
        #axs[6].imshow(img4, vmin=vmin, vmax=vmax)

        for i in range(1):
            flt.direct_convolve_3point()  # direct convolve

        img5 = reshape(flt.get_image(), NxF, NyF)

        cimg_dc = img5[NxMesh:2 * NxMesh, NyMesh:2 * NyMesh]
        #axs[6].imshow(cimg_dc, vmin=vmin, vmax=vmax)

        ###################################################
        # compute error between the two methods
        err = cimg_dc - cimg_fft
        #print( err )
        #axs[7].imshow( err, vmin=-1.0, vmax=1.0)

        #print("max img orig: {}".format(img4.max() ))
        #print("max img fft : {}".format(img3.max() ))
        #print("max img conv: {}".format(img5.max() ))
        #print("max error   : {}".format( np.abs(err).max() ))

        #plt.savefig("filter2.png")

        for i in range(NxMesh):
            for j in range(NyMesh):
                self.assertAlmostEqual(cimg_dc[i, j], cimg_fft[i, j], places=4)
Ejemplo n.º 8
0
    def skip_smearing_test0(self):
        """ put Gaussian filter in, convolve, and compare to scipy.conv2d"""

        plt.fig = plt.figure(1, figsize=(4, 6))
        plt.rc('font', family='serif', size=12)
        plt.rc('xtick')
        plt.rc('ytick')

        gs = plt.GridSpec(4, 2)

        axs = []
        for ai in range(8):
            axs.append(plt.subplot(gs[ai]))

        NxMesh = 6
        NyMesh = 6

        #internal filter size is 3x Nx/y/zMesh
        NxF = NxMesh * 3
        NyF = NyMesh * 3

        vmin = 0.0
        vmax = 1.0

        flt = pytools.Filter(NxMesh, NyMesh)

        #flt.init_kernel()
        #flt.init_gaussian_kernel(5.0)

        #f0 = 2.0/NxMesh/2.0/np.pi
        #f0 = 0.1
        #flt.init_sinc_kernel(f0, f0)

        #flt.init_lowpass_fft_kernel(0)

        kernel = np.zeros((NxF, NyF))
        #kernel[ 0, 0] = 1.0
        #kernel[ 1, 0] = 1.0
        #kernel[ 0, 1] = 1.0
        #kernel[ 1, 1] = 1.0
        #kernel[ 2, 0] = 1.0
        #kernel[ 2, 1] = 1.0
        #kernel[ 1, 2] = 1.0
        #kernel[ 2, 0] = 1.0
        #kernel[ 2, 1] = 1.0
        #kernel[ 2, 2] = 1.0

        #kernel[-1,-1] = 1.0
        #kernel[ 0,-1] = 1.0
        #kernel[ 1,-1] = 1.0
        #kernel[-1, 0] = 1.0
        #kernel[-1, 1] = 1.0

        #kernel[ 2,-1] = 1.0
        #kernel[-1, 2] = 1.0

        #for i in range(0, NxMesh):
        #    for j in range(0,NyMesh):
        #        kernel[i,j] = 1

        print("initing kernel.....")
        #init_center(kernel)
        init_center(kernel, digi3)
        #init_center(kernel, digi5)
        print(kernel)

        #swap kernel
        #(-1)^(i + j + ...)
        #for i in range(NxF):
        #    for j in range(NyF):
        #        kernel[i,j] *= (-1.0)**(i+j)
        #print()
        #print(kernel)
        flt.set_kernel(flatten(kernel))

        #kernel = np.zeros((NxF, NyF))
        #kernel = sinc2d(kernel, X, Y)
        #flt.set_kernel( flatten(kernel) )

        ##################################################
        image = np.zeros((NxF, NyF))

        #centered on middle
        #image[NxMesh:NxMesh+NxMesh, NyMesh:NyMesh+NyMesh]  = np.random.rand(NxMesh, NyMesh)

        #centered on corner
        #image[0:NxMesh, 0:NyMesh]  = np.random.rand(NxMesh, NyMesh)

        # fill image
        if False:
            for i in range(-2, 1, 1):
                for j in range(-2, 1, 1):
                    iff = fftshift1d(i, NxF)
                    jff = fftshift1d(j, NyF)
                    #print("i={} j={} i2={} j2={}".format(i,j,iff,jff))
                    image[iff, jff] = np.random.rand(1)

        if True:
            for i in range(0, NxMesh):
                for j in range(0, NyMesh):
                    #image[i,j] = np.random.rand(1)
                    image[i + NxMesh, j + NyMesh] = np.random.rand(1)

        flt.set_image(flatten(image))

        ##################################################

        ker = reshape(flt.get_kernel(), NxF, NyF)
        img = reshape(flt.get_image(), NxF, NyF)

        print()
        print(ker)
        print(ker.min(), ker.max())
        print(img.min(), img.max())

        axs[0].imshow(ker, vmin=vmin, vmax=vmax)
        axs[1].imshow(img, vmin=vmin, vmax=vmax)

        flt.fft_kernel()
        flt.fft_image_forward()
        ker2 = reshape(flt.get_kernel(), NxF, NyF)
        img2 = reshape(flt.get_image(), NxF, NyF)

        print()
        print(ker2)

        print(ker2.min(), ker2.max())
        print(img2.min(), img2.max())

        axs[2].imshow(fftshift(ker2))  #, vmin=vmin, vmax=vmax)
        axs[3].imshow(fftshift(img2))  #, vmin=vmin, vmax=vmax)
        print(fftshift(ker2))
        print(fftshift(img2))

        #apply kernel
        flt.apply_kernel()
        flt.apply_kernel()
        flt.fft_image_backward()
        ker3 = reshape(flt.get_kernel(), NxF, NyF)
        img3 = reshape(flt.get_image(), NxF, NyF)

        print(ker3.min(), ker3.max())
        print(img3.min(), img3.max())

        axs[4].imshow(ker3, vmin=vmin, vmax=vmax)
        axs[5].imshow(img3, vmin=vmin, vmax=vmax)
        #axs[5].imshow(fftshift(img3), vmin=vmin, vmax=vmax)
        axs[5].imshow(flip(img3), vmin=vmin, vmax=vmax)

        #python conv2d for comparison
        #pimg = img[
        pyimg = convolve2d(img, ker, mode='same', boundary='wrap')
        print(pyimg.min(), pyimg.max())
        axs[6].imshow(fftshift(pyimg), vmin=vmin, vmax=vmax)

        pyimg2 = convolve(img, ker, mode='same')
        pyimg2 /= pyimg2.max()
        print(pyimg2.min(), pyimg2.max())
        #axs[7].imshow(pyimg2, vmin=vmin, vmax=vmax)

        err = fftshift(pyimg) - flip(img3)
        print(err)
        print("min = {}".format(err.min()))
        print("max = {}".format(err.max()))

        axs[7].imshow(err, vmin=-1.0, vmax=1.0)
Ejemplo n.º 9
0
    def skip_test_filters(self):
        """ filter integration test with rest of the PIC functions"""

        try:
            plt.fig = plt.figure(1, figsize=(5,7))
            plt.rc('font', family='serif', size=12)
            plt.rc('xtick')
            plt.rc('ytick')

            gs = plt.GridSpec(8, 1)

            axs = []
            for ai in range(8):
                axs.append( plt.subplot(gs[ai]) )
        except:
            pass


        conf = Conf()
        conf.Nx = 3
        conf.Ny = 3
        conf.Nz = 1
        conf.NxMesh = 3
        conf.NyMesh = 3
        conf.NzMesh = 1
        conf.ppc = 10
        conf.vel = 0.1
        conf.update_bbox()

        grid = pycorgi.twoD.Grid(conf.Nx, conf.Ny, conf.Nz)
        grid.set_grid_lims(conf.xmin, conf.xmax, conf.ymin, conf.ymax)
        loadTiles(grid, conf)
        #insert_em(grid, conf, linear_field)
        insert_em(grid, conf, zero_field)
        inject(grid, filler, conf) #injecting plasma particles
        #inject(grid, filler_xvel, conf) #injecting plasma particles

        #pusher   = pypic.BorisPusher()
        #fintp    = pypic.LinearInterpolator()
        currint  = pypic.ZigZag()
        analyzer = pypic.Analyzator()

        flt =  pytools.Filter(conf.NxMesh, conf.NyMesh)
        flt.init_gaussian_kernel(1.0, 1.0)


        #for lap in range(0, conf.Nt):
        for lap in range(1):

            #analyze
            for j in range(grid.get_Ny()):
                for i in range(grid.get_Nx()):
                    tile = grid.get_tile(i,j)
                    analyzer.analyze2d(tile)

            #update boundaries
            for j in range(grid.get_Ny()):
                for i in range(grid.get_Nx()):
                    tile = grid.get_tile(i,j)
                    tile.update_boundaries(grid)

            #deposit current
            for j in range(grid.get_Ny()):
                for i in range(grid.get_Nx()):
                    tile = grid.get_tile(i,j)
                    currint.solve(tile)

            #exchange currents
            for j in range(grid.get_Ny()):
                for i in range(grid.get_Nx()):
                    tile = grid.get_tile(i,j)
                    tile.exchange_currents(grid)

            try:
                plotNode(axs[0], grid, conf)
                #plot2dParticles(axs[0], grid, conf, downsample=0.1)
                plot2dYee(axs[1], grid, conf, 'rho')
                plot2dYee(axs[2], grid, conf, 'jx')
                plot2dYee(axs[3], grid, conf, 'jy')
                plot2dYee(axs[4], grid, conf, 'jz')
            except:
                pass

            yee_ref = getYee2D(grid, conf)

            #filter
            for j in range(grid.get_Ny()):
                for i in range(grid.get_Nx()):
                    print(" i j ({},{})".format(i,j))
                    tile = grid.get_tile(i,j)
                    flt.get_padded_current(tile, grid)

                    # fourier space filtering
                    flt.fft_image_forward()
                    flt.apply_kernel()
                    flt.fft_image_backward()

                    # direct filtering
                    #for fj in range(1):
                    #    flt.direct_convolve_3point()
                    flt.set_current(tile)

            #cycle new and temporary currents
            for j in range(grid.get_Ny()):
                for i in range(grid.get_Nx()):
                    tile = grid.get_tile(i,j)
                    tile.cycle_current()

            yee = getYee2D(grid, conf)

            try:
                plot2dYee(axs[5], grid, conf, 'jx')
                plot2dYee(axs[6], grid, conf, 'jy')
                plot2dYee(axs[7], grid, conf, 'jz')
                #saveVisz(lap, grid, conf)
            except:
                pass

            for j in range(conf.Ny*conf.NyMesh):
                for i in range(conf.Nx*conf.NxMesh):
                    #print("({},{})".format(i,j))
                    self.assertAlmostEqual( yee_ref['jx'][i,j], yee['jx'][i,j], places=6 )
                    self.assertAlmostEqual( yee_ref['jy'][i,j], yee['jy'][i,j], places=6 )
                    self.assertAlmostEqual( yee_ref['jz'][i,j], yee['jz'][i,j], places=6 )