def test_get_voronoi_latlon():
    """
    LatlonGridRemap.get_voronoi(): nlat=180, nlon=360 (regular)
    """
    from cube_remap import LatlonGridRemap
    from util.convert_coord.cart_ll import latlon2xyz

    nlat, nlon = 180, 360
    ll = LatlonGridRemap(nlat, nlon, "regular")

    ret = ll.get_voronoi(1)
    expect = [
        (-1.5707963267948966, 0),
        (-1.5447610285607269, 0.026179938779914945),
        (-1.5447610285607269, 0.008726646259971647),
    ]
    expect_xyz = [latlon2xyz(*latlon) for latlon in expect]
    aa_equal(expect_xyz, ret, 10)

    ret = ll.get_voronoi(nlon)
    expect = [
        (-1.5447610285607269, -0.00872664625997164),
        (-1.5447610285607269, 0.008726646259971647),
        (-1.5274041630712807, 0.008726646259971647),
        (-1.5274041630712807, -0.00872664625997164),
    ]
    expect_xyz = [latlon2xyz(*latlon) for latlon in expect]
    aa_equal(expect_xyz, ret, 10)
def test_cs_voronoi_area():
    """
    CubeGridRemap.get_voronoi(): check the sphere area, ne=3
    """
    from cube_remap import CubeGridRemap
    from util.geometry.sphere import area_polygon
    from math import fsum, pi
    from util.convert_coord.cart_cs import xyp2xyz

    ne, ngq = 3, 4
    rotated = False
    cube = CubeGridRemap(ne, ngq, rotated)

    area_list = list()
    for uid in xrange(cube.up_size):
        # xy_vts, vor_obj = cube.get_voronoi_scipy(uid)
        # xyzs = [xyp2xyz(x,y,1) for x,y in xy_vts]
        # area_list.append( area_polygon(xyzs) )

        xyzs = cube.get_voronoi(uid)
        area_list.append(area_polygon(xyzs))

    aa_equal(fsum(area_list), 4 * pi, 15)

    """
def test_ll_voronoi_area():
    """
    LatlonGridRemap.get_voronoi(): check the sphere area
    """
    from cube_remap import LatlonGridRemap
    from util.geometry.sphere import area_polygon
    from math import fsum, pi
    from util.convert_coord.cart_ll import latlon2xyz

    nlat, nlon = 90, 180
    # nlat, nlon = 180, 360
    # nlat, nlon = 360, 720
    # nlat, nlon = 720, 1440
    ll_obj = LatlonGridRemap(nlat, nlon)

    area_list = list()
    for idx in xrange(ll_obj.nsize):
        # latlons = ll_obj.get_voronoi(idx)
        # xyzs = [latlon2xyz(*latlon) for latlon in latlons]
        xyzs = ll_obj.get_voronoi(idx)
        area_list.append(area_polygon(xyzs))

    aa_equal(fsum(area_list), 4 * pi, 12)

    """
Exemple #4
0
def check_RungeKutta4_exact_multi(platform, func_src, func_pyf):
    from runge_kutta import RungeKutta


    #----------------------------------------------------
    # Allocate
    #----------------------------------------------------
    ps = PreSetup()
    nx, dt, tmax, yinit = ps.nx, ps.dt, ps.tmax, ps.yinit

    y = ArrayAs(platform, yinit, 'y')

    rk = RungeKutta(platform, nx, dt)


    #----------------------------------------------------
    # Core function
    #----------------------------------------------------
    lib = platform.source_compile(func_src, func_pyf)
    func_core = platform.get_function(lib, 'func')
    func_core.prepare('iDOO', nx)   # (t, y, ret)

    func = lambda t, y, ret: func_core.prepared_call(t,y,ret)
    comm = lambda k: None


    #----------------------------------------------------
    # RK4
    #----------------------------------------------------
    t = 0
    for tstep in xrange(tmax):
        rk.update_rk4(t, y, func, comm)
        t += dt

    aa_equal(ps.exact_func(t), y.get(), 13)
Exemple #5
0
def test_transform_matrix_inner():
    '''
    CubeTensor: Transform matrix, inner product test (ne=30, ngq=4)
    '''
    ne, ngq = 30, 4
    nproc, myrank = 1, 0

    cubegrid = CubeGridMPI(ne, ngq, nproc, myrank)
    cubetensor = CubeTensor(cubegrid)

    for idx in xrange(cubegrid.local_ep_size):
        AI = cubetensor.AI[idx*4:idx*4+4].reshape(2,2)
        A = np.linalg.inv(AI)
        g = np.dot(A.T,A)    # metric tensor
        
        # inner product in the latlon coordinates
        v1_lon, v1_lat = np.random.rand(2)
        v2_lon, v2_lat = np.random.rand(2)
        ip_ll = v1_lon*v2_lon + v1_lat*v2_lat

        # inner product in the cubed-sphere coordinates
        # latlon -> contravariant
        v1_0 = AI[0,0]*v1_lon + AI[0,1]*v1_lat
        v1_1 = AI[1,0]*v1_lon + AI[1,1]*v1_lat
        v2_0 = AI[0,0]*v2_lon + AI[0,1]*v2_lat
        v2_1 = AI[1,0]*v2_lon + AI[1,1]*v2_lat
        ip_xy = g[0,0]*v1_0*v2_0 + g[0,1]*v1_0*v2_1 \
              + g[1,0]*v1_1*v2_0 + g[1,1]*v1_1*v2_1

        aa_equal(ip_ll, ip_xy, 15)
Exemple #6
0
def test_rk4_exact():
    '''
    RK4: Exact solution dy/dt=-(t+1)*y => y(t)=y(0)*exp(-0.5*t-1)*t
    '''

    ps = PreSetup()
    nx, dt, tmax, func = ps.nx, ps.dt, ps.tmax, ps.func

    y = np.zeros(nx)
    k1 = np.zeros_like(y)
    k2 = np.zeros_like(y)
    k3 = np.zeros_like(y)
    k4 = np.zeros_like(y)

    #----------------------------------------------------
    # RK4
    #----------------------------------------------------
    t = 0
    y[:] = ps.yinit
    for tstep in xrange(tmax):
        k1[:] = func(t, y)
        k2[:] = func(t+0.5*dt, y+0.5*dt*k1) 
        k3[:] = func(t+0.5*dt, y+0.5*dt*k2) 
        k4[:] = func(t+dt, y+dt*k3) 

        y += (dt/6)*(k1 + 2*k2 + 2*k3 + k4)
        t += dt

    aa_equal(ps.exact_func(t), y, 13)
Exemple #7
0
def main():
    n = 2**25

    a = np.float32(np.random.rand())
    x = np.random.rand(n).astype('f4')
    y = np.random.rand(n).astype('f4')
    x_gpu = cuda.to_device(x)
    y_gpu = cuda.to_device(y)
    y2 = np.zeros(n, 'f4')

    t1 = datetime.now()
    saxpy_numpy(a, x, y)
    dt_numpy = datetime.now() - t1

    obj = SAXPY_CUDA()
    t2 = datetime.now()
    obj.saxpy_cuda(n, a, x_gpu, y_gpu)
    cuda.memcpy_dtoh(y2, y_gpu)
    dt_cuda = datetime.now() - t2

    print('n={}'.format(n))
    print('numpy: {}'.format(dt_numpy))
    print('cuda : {}'.format(dt_cuda))

    aa_equal(y, y2, 7)
    print('Check result: OK!')
Exemple #8
0
def test_cs_get_voronoi():
    '''
    CubeGridRemap.get_voronoi(): ne=30, some points
    '''
    from cube_remap import CubeGridRemap
    from scipy.spatial import voronoi_plot_2d
    import matplotlib.pyplot as plt

    ne, ngq = 30, 4
    rotated = False
    cube = CubeGridRemap(ne, ngq, rotated)

    uid = 0
    xy_vertices, vor = cube.get_voronoi(uid)
    expect = [( 4.54850726e-03, 3.80070853e-05), \
              ( 2.30716873e-03, 3.92011929e-03), \
              (-2.30716873e-03, 3.92011929e-03), \
              (-4.54850726e-03, 3.80070853e-05), \
              (-2.24133853e-03,-3.95812638e-03), \
              ( 2.24133853e-03,-3.95812638e-03)]
    aa_equal(expect, xy_vertices, 10)

    uid = 1
    xy_vertices, vor = cube.get_voronoi(uid)
    expect = [( 5.98890285e-03,-2.13793346e-03), \
              ( 5.01646021e-03, 3.93916864e-03), \
              (-2.27448976e-03, 3.93916864e-03), \
              (-4.54802687e-03,-7.61894981e-05), \
              (-7.97052613e-04,-6.32824066e-03), \
              ( 4.91440620e-03,-4.03563236e-03)]
    aa_equal(expect, xy_vertices, 10)
Exemple #9
0
def test_intersect_two_greatcircles():
    '''
    intersect_two_greatcircles(): axis circles, oblique circles, identical
    '''

    from sphere import plane_origin, intersect_two_greatcircles
    from convert_coord.cart_ll import latlon2xyz


    #---------------------------------------
    # axis circles
    #---------------------------------------
    xyz1 = (1,0,0)
    xyz2 = (0,1,0)
    xyz3 = (0,0,1)

    # x axis
    plane1 = plane_origin(xyz1, xyz2)
    plane2 = plane_origin(xyz1, xyz3)
    ret = intersect_two_greatcircles(plane1, plane2)
    equal(ret, [(1,0,0), (-1,0,0)])

    # y axis
    plane1 = plane_origin(xyz1, xyz2)
    plane2 = plane_origin(xyz2, xyz3)
    ret = intersect_two_greatcircles(plane1, plane2)
    equal(ret, [(0,1,0), (0,-1,0)])

    # z axis
    plane1 = plane_origin(xyz1, xyz3)
    plane2 = plane_origin(xyz2, xyz3)
    ret = intersect_two_greatcircles(plane1, plane2)
    equal(ret, [(0,0,1), (0,0,-1)])


    #---------------------------------------
    # oblique circles
    #---------------------------------------
    xyz1 = (0, 0, 1)
    xyz2 = latlon2xyz(pi/4, pi/4)
    xyz3 = (1,0,0)

    plane1 = plane_origin(xyz1, xyz2)
    plane2 = plane_origin(xyz2, xyz3)

    ret = intersect_two_greatcircles(plane1, plane2)
    aa_equal(ret, [xyz2, latlon2xyz(-pi/4, 5*pi/4)], 15)


    #---------------------------------------
    # identical
    #---------------------------------------
    xyz1 = (0, 0, 1)
    xyz2 = latlon2xyz(pi/4, pi/4)
    plane = plane_origin(xyz1, xyz2)
    ret = intersect_two_greatcircles(plane, plane)
    equal(ret, [None, None])
Exemple #10
0
def check_rk4_exact_multi(platform, func_src, func_pyf, daxpy_src, daxpy_pyf):
    #----------------------------------------------------
    # Allocate
    #----------------------------------------------------
    ps = PreSetup()
    nx, dt, tmax, yinit = ps.nx, ps.dt, ps.tmax, ps.yinit

    y = ArrayAs(platform, yinit, 'y')
    ytmp = Array(platform, nx, 'f8', 'ytmp')

    k1 = Array(platform, nx, 'f8', 'k1')
    k2 = Array(platform, nx, 'f8', 'k2')
    k3 = Array(platform, nx, 'f8', 'k3')
    k4 = Array(platform, nx, 'f8', 'k4')


    #----------------------------------------------------
    # Core function
    #----------------------------------------------------
    lib = platform.source_compile(func_src, func_pyf)
    func = platform.get_function(lib, 'func')
    func.prepare('iDOO', nx)   # (t, y, ret)


    #----------------------------------------------------
    # DAXPY(double a*x+y) function
    #----------------------------------------------------
    lib = platform.source_compile(daxpy_src, daxpy_pyf)
    daxpy = platform.get_function(lib, 'daxpy')
    rk4sum = platform.get_function(lib, 'rk4sum')
    daxpy.prepare('iooDO', nx, y, ytmp)
    rk4sum.prepare('idooooo', nx, dt, k1, k2, k3, k4, y)


    #----------------------------------------------------
    # RK4
    #----------------------------------------------------
    t = 0
    for tstep in xrange(tmax):
        func.prepared_call(t, y, k1)

        daxpy.prepared_call(0.5*dt, k1)
        func.prepared_call(t+0.5*dt, ytmp, k2) 

        daxpy.prepared_call(0.5*dt, k2)
        func.prepared_call(t+0.5*dt, ytmp, k3) 

        daxpy.prepared_call(dt, k3)
        func.prepared_call(t+dt, ytmp, k4) 

        # y += (dt/6)*(k1 + 2*k2 + 2*k3 + k4)
        rk4sum.prepared_call()
        t += dt

    aa_equal(ps.exact_func(t), y.get(), 13)
Exemple #11
0
def test_gq_integrate_legendre():
    from quadrature import GQIntegrate

    gqi = GQIntegrate()

    func = lambda x: 2*x*x
    intf = lambda x: 2/3*x**3
    x1, x2 = numpy.random.rand(2)

    ref = intf(x2) - intf(x1)
    aa_equal(ref, gqi.gq_integrate(x1, x2, func, qtype='legendre'), 15)
Exemple #12
0
def test_gq_integrate_2d():
    from quadrature import GQIntegrate

    gqi = GQIntegrate()

    func = lambda x,y: 2*x*x + y*y
    intf = lambda x,y: 2/3*x**3*y + 1/3*y**3*x
    x1, x2, y1, y2 = numpy.random.rand(4)

    ref = (intf(x2,y2) - intf(x1,y2)) - (intf(x2,y1) - intf(x1,y1)) 
    aa_equal(ref, gqi.gq_integrate_2d(x1, x2, y1, y2, func), 15)
Exemple #13
0
def test_legendre():
    from quadrature import legendre, deriv_legendre, recursive_L_dL
    x = numpy.arange(-1, 1, 2/100, 'f16')

    for N in xrange(9):
        L = legendre(N, x)
        dL = deriv_legendre(N, x)
        P, dP = recursive_L_dL(N, x)

        aa_equal(L, P, 14)
        aa_equal(dL, dP, 13)
Exemple #14
0
def test_gausslobatto():
    # setup
    gll_pts0 = [-1, -0.830223896278567, -0.468848793470714, 0]
    gll_wts0 = [4.761904761904762E-002, 0.276826047361566, 0.431745381209863, 0.487619047619048]

    # run
    from quadrature import gausslobatto
    gll_pts, gll_wts = gausslobatto(6)

    # verify
    aa_equal(gll_pts[:4], gll_pts0, 15)
    aa_equal(gll_wts[:4], gll_wts0, 15)
Exemple #15
0
def test_compare_homme():
    '''
    CubeTensor: Compare the transform matrix with HOMME (ne=30, ngq=4)
    '''
    ne, ngq = 3, 4
    nproc, myrank = 1, 0

    cubegrid = CubeGridMPI(ne, ngq, nproc, myrank)
    cubetensor = CubeTensor(cubegrid)
    cubetensor_homme = CubeTensor(cubegrid, homme_style=True)

    aa_equal(cubetensor.AI, cubetensor_homme.AI)
    aa_equal(cubetensor.J, cubetensor_homme.J)
Exemple #16
0
def test_circum_center_radius():
    '''
    circum_center_radius(): boundary, near lon=0, big triangle
    '''
    from numpy import pi
    from circumcircle import circum_center_radius
    from sphere import angle
    from convert_coord.cart_ll import latlon2xyz


    # boundary
    ll1, ll2, ll3 = (0,pi/3), (0,2/3*pi), (pi/6,pi/2)
    xyz1, xyz2, xyz3 = [latlon2xyz(*ll) for ll in [ll1,ll2,ll3]]
    center, radius = circum_center_radius(xyz1, xyz2, xyz3)
    equal(center, (0,1,0))


    # near lon=0
    ll1, ll2, ll3 = (pi/5, 2*pi-pi/6), (0,pi/7), (pi/7,pi/6)
    xyz1, xyz2, xyz3 = [latlon2xyz(*ll) for ll in [ll1,ll2,ll3]]
    center, radius = circum_center_radius(xyz1, xyz2, xyz3)

    d1 = angle(center, xyz1)
    d2 = angle(center, xyz2)
    d3 = angle(center, xyz3)
    aa_equal(d1, radius, 15)
    aa_equal(d2, radius, 15)
    aa_equal(d3, radius, 15)


    # big triangle
    ll1, ll2, ll3 = (pi/2,0), (0,0), (0,pi/2)
    xyz1, xyz2, xyz3 = [latlon2xyz(*ll) for ll in [ll1,ll2,ll3]]
    center, radius = circum_center_radius(xyz1, xyz2, xyz3)
    aa_equal(center, latlon2xyz(0.61547970867038737,pi/4), 15)
Exemple #17
0
    def check_ll_grid(self, remap_obj, lats, lons, lat_reverse=False, lon_shift=0):
        '''
        lat_reverse : True/False
        lon_shift   : number of shift points (used by np.roll)
        '''
        if lat_reverse: lats = lats[::-1]
        lons = np.roll(lons + (lons<0)*360, lon_shift)

        nlat, nlon = remap_obj.nlat, remap_obj.nlon
        ll_type = remap_obj.ll_type
        ref_lats, ref_lons = make_lats_lons(nlat, nlon, ll_type)

        aa_equal(np.rad2deg(ref_lats), lats, 13)
        aa_equal(np.rad2deg(ref_lons), lons, 13)
Exemple #18
0
def test_GreatCircle():
    '''
    GreatCircle: max_angle, pt3
    '''
    from numpy import deg2rad
    from greatcircle import GreatCircle
    from convert_coord.cart_ll import xyz2latlon


    #----------------------------------------------
    pt1 = (deg2rad(0),deg2rad(0))
    pt2 = (deg2rad(30),deg2rad(0))

    gc = GreatCircle(pt1, pt2)
    aa_equal(gc.max_angle, np.pi/6, 15)
    aa_equal(gc.latlon3, (np.pi/2,0), 15)


    #----------------------------------------------
    a = 1/np.sqrt(2)
    pt1 = xyz2latlon(1,0,0)
    pt2 = xyz2latlon(a,0.5,0.5)

    gc = GreatCircle(pt1, pt2)
    aa_equal(gc.max_angle, np.pi/4, 15)
    aa_equal(gc.xyz3, (0,a,a), 15)
Exemple #19
0
def test_abp2latlon_2():
    '''
    abp2latlon(): check (ne=120, ei=84, ej=79, panel=2)
    '''
    from cs_ll import ij2ab, abp2latlon, latlon2abp

    ne, ngq = 120, 4
    panel = 2
    gi1, gj1, ei1, ej1 = 4, 4, 84, 79
    gi2, gj2, ei2, ej2 = 1, 4, 85, 79
    gi3, gj3, ei3, ej3 = 1, 1, 85, 80
    gi4, gj4, ei4, ej4 = 4, 1, 84, 80

    a1, b1 = ij2ab(ne, ngq, ei1, ej1, gi1, gj1)
    a2, b2 = ij2ab(ne, ngq, ei2, ej2, gi2, gj2)
    a3, b3 = ij2ab(ne, ngq, ei3, ej3, gi3, gj3)
    a4, b4 = ij2ab(ne, ngq, ei4, ej4, gi4, gj4)

    lat1, lon1 = abp2latlon(a1,b1,panel)
    lat2, lon2 = abp2latlon(a2,b2,panel)
    lat3, lon3 = abp2latlon(a3,b3,panel)
    lat4, lon4 = abp2latlon(a4,b4,panel)

    aa_equal([lat1,lon1], [lat2,lon2], 15)
    aa_equal([lat3,lon3], [lat2,lon2], 15)
    aa_equal([lat3,lon3], [lat4,lon4], 15)
    aa_equal([lat1,lon1], [lat4,lon4], 15)
Exemple #20
0
def test_ij2ab():
    '''
    ij2ab(): center of panel, at panel border
    '''
    from cs_ll import ij2ab

    alpha, beta = ij2ab(ne=16, ngq=4, ei=1, ej=1, gi=1, gj=1)
    a_equal([alpha,beta], [-pi/4,-pi/4])

    alpha, beta = ij2ab(ne=16, ngq=4, ei=8, ej=8, gi=4, gj=4)
    a_equal([alpha,beta], [0,0])

    #------------------------------------------------
    # MVP accuracy test
    #------------------------------------------------
    ne, ngq = 120, 4
    panel = 2
    gi1, gj1, ei1, ej1 = 4, 4, 84, 79
    gi2, gj2, ei2, ej2 = 1, 4, 85, 79
    gi3, gj3, ei3, ej3 = 1, 1, 85, 80
    gi4, gj4, ei4, ej4 = 4, 1, 84, 80

    a1, b1 = ij2ab(ne, ngq, ei1, ej1, gi1, gj1)
    a2, b2 = ij2ab(ne, ngq, ei2, ej2, gi2, gj2)
    a3, b3 = ij2ab(ne, ngq, ei3, ej3, gi3, gj3)
    a4, b4 = ij2ab(ne, ngq, ei4, ej4, gi4, gj4)

    #print('')
    #print('{:.15f}, {:.15f}'.format(a1, b1))
    #print('{:.15f}, {:.15f}'.format(a2, b2))

    aa_equal([a1,b1], [a2,b2], 15)
    aa_equal([a2,b2], [a3,b3], 15)
    aa_equal([a3,b3], [a4,b4], 15)
    aa_equal([a1,b1], [a4,b4], 15)
def test_zero_identity():
    '''
    SEM on the plane: zero identity, vorticity(gradient())=0
    '''
    ngq = 4

    sep = SpectralElementPlane(ngq)

    scalar = np.random.rand(ngq,ngq)
    ret1 = np.zeros((ngq,ngq,2), 'f8')
    ret2 = np.zeros((ngq,ngq), 'f8')

    sep.gradient(scalar, ret1)
    sep.vorticity(ret1, ret2)
    aa_equal(ret2, np.zeros((ngq,ngq), 'f8'), 14)
def test_get_surround_elem_rotated():
    """
    CubeGridRemap.get_surround_elem() rotated: ne=30
    """
    from cube_remap import CubeGridRemap
    from util.convert_coord.cs_ll import abp2latlon

    ne, ngq = 30, 4
    rotated = True
    cube = CubeGridRemap(ne, ngq, rotated)

    lat, lon = np.deg2rad(38), np.deg2rad(127)
    (a, b), (panel, ei, ej) = cube.get_surround_elem(lat, lon)
    aa_equal((a, b), (0, 0), 15)
    a_equal((panel, ei, ej), (1, 15, 16))
Exemple #23
0
def test_latlon2xyz_xyz2latlon():
    """
    latlon2xyz() -> xyz2latlon() : check consistency, repeat 1000 times
    """
    from cart_ll import latlon2xyz, xyz2latlon

    N = 1000

    for i in range(N):
        lat = pi * rand() - pi / 2
        lon = 2 * pi * rand()

        X, Y, Z = latlon2xyz(lat, lon)
        lat2, lon2 = xyz2latlon(X, Y, Z)

        aa_equal((lat, lon), (lat2, lon2), 15)
Exemple #24
0
def test_avg_random():
    '''
    CubeMPI for AVG: Random values (ne=5, ngq=4, nproc=1)
    '''
    ne, ngq = 5, 4

    nproc, myrank = 1, 0
    cubegrid = CubeGridMPI(ne, ngq, nproc, myrank)
    cubempi = CubeMPI(cubegrid, method='AVG')

    a_equal(cubegrid.local_gids, np.arange(6*ne*ne*ngq*ngq))
    a_equal(cubempi.recv_schedule.shape, (0,3))
    a_equal(cubempi.send_schedule.shape, (0,3))
    a_equal(cubempi.recv_buf_size, 6*ne*ne*12)
    a_equal(cubempi.send_buf_size, 0)


    #-----------------------------------------------------
    # Generate a random field on the cubed-sphere
    #-----------------------------------------------------
    ep_size = cubegrid.ep_size

    f = np.random.rand(ep_size)


    #-----------------------------------------------------
    # Average the element boundary for the spectral element method
    #-----------------------------------------------------
    recv_buf = np.zeros(cubempi.recv_buf_size, 'f8')
    send_buf = np.zeros(cubempi.send_buf_size, 'f8')

    pre_send(cubempi, f, recv_buf, send_buf)
    post_recv(cubempi, f, recv_buf)


    #-----------------------------------------------------
    # Check if mvps have same values
    #-----------------------------------------------------
    cs_fpath = 'cs_grid_ne%dngq%d.nc'%(ne, ngq)
    cs_ncf = nc.Dataset(cs_fpath, 'r', format='NETCDF4')
    mvps = cs_ncf.variables['mvps'][:]

    for seq, mvp in enumerate(mvps):
        eff_mvp = [k for k in mvp if k != -1]

        for m in eff_mvp:
            aa_equal(f[seq], f[m], 15)
def test_laplacian():
    '''
    SEM on the plane: laplacian(), f=x^3+2*y^2 on [-1,1]^2
    '''
    ngq = 4

    gq_pts, gq_wts = gausslobatto(ngq-1) 
    x = gq_pts[:,np.newaxis]
    y = gq_pts[np.newaxis,:]

    scalar = np.zeros((ngq,ngq), 'f8')
    ret = np.zeros((ngq,ngq), 'f8')

    sep = SpectralElementPlane(ngq)
    scalar[:] = x*x*x + 2*y*y

    sep.laplacian(scalar, ret)
    aa_equal(ret, 6*x+0*y+4, 13)
    def __init__(self, ncf, ie):
        self.N = N = ncf.N
        self.ngll = ngll = ncf.ngll
        self.nelem = nelem = ncf.nelem
        self.ie = ie


        # transform matrix
        self.A = numpy.zeros((2,2,ngll,ngll,nelem), 'f8')
        self.AI = numpy.zeros((2,2,ngll,ngll,nelem), 'f8', order='F')
        self.J = numpy.zeros((ngll,ngll,nelem), 'f8')
        self.A[:] = ncf.variables['A'][:]
        self.AI[:] = ncf.variables['AI'][:]
        self.J[:] = ncf.variables['J'][:]

        #print 'J', self.J[1,1,0]


        # derivative matrix
        self.dvv = numpy.zeros((ngll,ngll), 'f8')
        self.dvvT = numpy.zeros((ngll,ngll), 'f8')
        self.dvv[:] = ncf.variables['dvv'][:]
        self.dvvT[:] = self.dvv.T


        # compare
        cubegrid = CubeGridMPI(N, ngll, nproc=1, myrank=0)
        cubetensor = CubeTensor(cubegrid)

        lonlat_coord = ncf.variables['lonlat_coord'][:]
        lons = lonlat_coord[0,:,:,:]
        lats = lonlat_coord[1,:,:,:]

        a_equal(lats.ravel(), cubegrid.local_latlons[:,0])
        a_equal(lons.ravel(), cubegrid.local_latlons[:,1])

        '''
        AI = ArrayAs(platform, cubetensor.AI, 'AI')     # local_ep_size*2*2
        J = ArrayAs(platform, cubetensor.J, 'J')        # local_ep_size
        dvv = ArrayAs(platform, cubetensor.dvv, 'dvvT') # ngq*ngq
        '''
        aa_equal(self.dvv.ravel(), cubetensor.dvv, 15)
        #aa_equal(self.J.ravel(), cubetensor.J, 15)
        aa_equal(self.AI.ravel(), cubetensor.AI, 15)
Exemple #27
0
def test_normal_vector():
    '''
    normal_vector(): yz plane circle, oblique circle
    '''

    from sphere import normal_vector
    from convert_coord.cart_ll import latlon2xyz


    #---------------------------------------
    # yz plane circle, +x direction
    #---------------------------------------
    vec1 = (0, 1, 0)
    vec2 = (0, 1/sqrt(2), 1/sqrt(2))
    nvec = normal_vector(vec1, vec2)
    equal(nvec, (sin(pi/4),0,0))

    unit_nvec = normal_vector(vec1, vec2, normalize=True)
    equal(unit_nvec, (1,0,0))


    #---------------------------------------
    # yz plane circle, -x direction
    #---------------------------------------
    vec1 = (0, -1, 0)
    vec2 = (0, 1/sqrt(2), 1/sqrt(2))
    nvec = normal_vector(vec1, vec2)
    equal(nvec, (-sin(pi/4),0,0))

    unit_nvec = normal_vector(vec1, vec2, normalize=True)
    equal(unit_nvec, (-1,0,0))


    #---------------------------------------
    # oblique circle
    #---------------------------------------
    vec1 = (0, 0, 1)
    vec2 = latlon2xyz(pi/4,pi/4)
    nvec = normal_vector(vec1, vec2)
    aa_equal(nvec, latlon2xyz(0,3*pi/4,R=sin(pi/4)), 15)

    unit_nvec = normal_vector(vec1, vec2, normalize=True)
    aa_equal(unit_nvec, (-1/sqrt(2),1/sqrt(2),0), 15)
def test_vorticity():
    '''
    SEM on the plane: vorticity(), F=(2*y^2,x^3) on [-1,1]^2
    '''
    ngq = 4

    gq_pts, gq_wts = gausslobatto(ngq-1) 
    x = gq_pts[:,np.newaxis]
    y = gq_pts[np.newaxis,:]

    vector = np.zeros((ngq,ngq,2), 'f8')
    ret = np.zeros((ngq,ngq), 'f8')

    sep = SpectralElementPlane(ngq)
    vector[:,:,0] = 2*y*y
    vector[:,:,1] = x*x*x

    sep.vorticity(vector, ret)
    aa_equal(ret, 3*x*x-4*y, 15)
def test_divergence():
    '''
    SEM on the plane: divergence(), F=(x^3,2*y^2) on [-1,1]^2
    '''
    ngq = 4

    gq_pts, gq_wts = gausslobatto(ngq-1) 
    x = gq_pts[:,np.newaxis]
    y = gq_pts[np.newaxis,:]

    vector = np.zeros((ngq,ngq,2), 'f8')
    ret = np.zeros((ngq,ngq), 'f8')

    sep = SpectralElementPlane(ngq)
    vector[:,:,0] = x*x*x
    vector[:,:,1] = 2*y*y

    sep.divergence(vector, ret)
    aa_equal(ret, 3*x*x+4*y, 15)
def test_gradient():
    '''
    SEM on the plane: gradient(), f=x^2+2*y^2 on [-1,1]^2
    '''
    ngq = 4

    gq_pts, gq_wts = gausslobatto(ngq-1) 
    x = gq_pts[:,np.newaxis]
    y = gq_pts[np.newaxis,:]

    scalar = np.zeros((ngq,ngq), 'f8')
    ret = np.zeros((ngq,ngq,2), 'f8')

    sep = SpectralElementPlane(ngq)
    scalar[:] = x*x + 2*y*y

    sep.gradient(scalar, ret)
    aa_equal(ret[:,:,0], 2*x+0*y, 15)
    aa_equal(ret[:,:,1], 0*x+4*y, 15)
def compare_float(var, ref, cut_digit=1, verbose=False):
    # x = mantissa * 2**exponent

    if type(var) != np.ndarray: var = np.array(var)
    if type(ref) != np.ndarray: ref = np.array(ref)

    m_var, e_var = np.frexp(var)
    m_ref, e_ref = np.frexp(ref)

    num_exp_diff = np.count_nonzero(e_var != e_ref)
    if num_exp_diff > 0:
        try:
            # check 0 and 1e-16
            idxs = np.where(e_var != e_ref)
            aa_equal(var[idxs], ref[idxs], 15)
            return True, 15
        except:
            if verbose:
                print("idxs   : ", idxs)
                print("Actual : ", var[idxs])
                print("Desired: ", ref[idxs])
            return False, "The exponents are not same at {} points".format(num_exp_diff)

    num_man_diff = np.count_nonzero(m_var != m_ref)
    if num_man_diff == 0:
        return True, "exact"

    digit = 17
    percents = []
    while(True):
        try:
            aa_equal(m_var, m_ref, digit-1)
            return True, "{}, ({})".format(digit, ', '.join(percents))

        except Exception as e:
            percent = float(re.findall('mismatch (\d+.\S+)%',str(e))[0])
            percents.insert(0, "{}:{:.2f}%".format(digit,percent))
            #print('>>>>', digit, str(e), percent, percents)

            if digit == cut_digit:
                return False, "{}, ({})".format(cut_digit, ', '.join(percents))
            else:
                digit -= 1
def main(): 
    nx, ny = 1000, 800
    tmax = 500

    # allocation
    f = np.zeros((nx, ny), 'f4')
    g = np.zeros((nx, ny), 'f4')
    f_gpu = cuda.to_device(f)
    g_gpu = cuda.to_device(g)
    f2 = np.zeros((nx,ny), 'f4')

    # time loop
    # numpy version
    t1 = datetime.now()
    for tstep in range(1, tmax+1):
        g[nx//2, ny//2] = np.sin(0.1*tstep)
        update_numpy(f, g)
        update_numpy(g, f)
    dt_numpy = datetime.now() - t1

    # cuda version
    obj = WAVE2D_CUDA()
    t2 = datetime.now()
    for tstep in range(1, tmax+1):
        obj.update_src_cuda(nx, ny, tstep, g_gpu)
        obj.update_cuda(nx, ny, f_gpu, g_gpu)
        obj.update_cuda(nx, ny, g_gpu, f_gpu)
    cuda.memcpy_dtoh(f2, f_gpu)
    dt_cuda = datetime.now() - t2

    print('\nnx={}, ny={}, tmax={}'.format(nx, ny, tmax))
    print('numpy: {}'.format(dt_numpy))
    print('cuda : {}'.format(dt_cuda))

    # check results
    aa_equal(f, f2, 6)
    print('Check result: OK!')

    # plot
    plt.imshow(f.T, cmap='hot', origin='lower', vmin=-0.1, vmax=0.1)
    plt.colorbar()
    plt.show()
Exemple #33
0
def main(): 
    nx, ny = 1000, 800
    tmax = 500

    # allocation
    f = np.zeros((nx, ny), 'f4')
    g = np.zeros((nx, ny), 'f4')
    f2 = np.zeros_like(f)
    g2 = np.zeros_like(f)

    # time loop
    # numpy version
    t1 = datetime.now()
    for tstep in range(1, tmax+1):
        g[nx//2, ny//2] = np.sin(0.1*tstep)
        update_numpy(f, g)
        update_numpy(g, f)
    dt_numpy = datetime.now() - t1

    # C version
    obj = WAVE2D_C()
    t2 = datetime.now()
    for tstep in range(1, tmax+1):
        g2[nx//2, ny//2] = np.sin(0.1*tstep)
        obj.update_c(nx, ny, f2.ravel(), g2.ravel())
        obj.update_c(nx, ny, g2.ravel(), f2.ravel())
    dt_cuda = datetime.now() - t2

    print('\nnx={}, ny={}, tmax={}'.format(nx, ny, tmax))
    print('numpy: {}'.format(dt_numpy))
    print('cuda : {}'.format(dt_cuda))

    # check results
    aa_equal(f, f2, 6)
    print('Check result: OK!')

    # plot
    plt.imshow(f.T, cmap='hot', origin='lower', vmin=-0.1, vmax=0.1)
    plt.colorbar()
    plt.show()