def test_get_neighbors_latlon(): ''' LatlonGridRemap.get_neighbors(): nlat=180, nlon=360 (regular) ''' from cube_remap import LatlonGridRemap nlat, nlon = 180, 360 ll = LatlonGridRemap(nlat, nlon, 'regular') ret = ll.get_neighbors(nlon+1) expect = (0, 1, 2, nlon, nlon+2, 2*nlon, 2*nlon+1, 2*nlon+2) a_equal(expect, ret) ret = ll.get_neighbors(nlon) expect = (nlon-1, 0, 1, 2*nlon-1, nlon+1, 3*nlon-1, 2*nlon, 2*nlon+1) a_equal(expect, ret) ret = ll.get_neighbors(1) expect = (0, 2, nlon, nlon+1, nlon+2) a_equal(expect, ret) ret = ll.get_neighbors(0) expect = (nlon-1, 1, 2*nlon-1, nlon, nlon+1) a_equal(expect, ret) ret = ll.get_neighbors((nlat-1)*nlon) expect = ((nlat-1)*nlon-1, (nlat-2)*nlon, (nlat-2)*nlon+1, nlat*nlon-1, (nlat-1)*nlon+1) a_equal(expect, ret)
def test_intersect_two_polygons(): ''' intersect_two_polygons(): inclusion, partial ''' from sphere import intersect_two_polygons from math import pi from convert_coord.cart_ll import latlon2xyz # inclusion ll_poly1 = [(pi/2,0), (0,0), (0,pi/2)] ll_poly2 = [(pi/6,pi/6), (pi/6,pi/3), (pi/3,pi/3), (pi/3,pi/6)] xyz_poly1 = [latlon2xyz(*ll) for ll in ll_poly1] xyz_poly2 = [latlon2xyz(*ll) for ll in ll_poly2] ret = intersect_two_polygons(xyz_poly1, xyz_poly2) a_equal(ret, xyz_poly2) # partial ll_poly1 = [(pi/2,0), (0,0), (0,pi/2)] ll_poly2 = [(pi/2,0), (0,pi/3), (0,2*pi/3)] xyz_poly1 = [latlon2xyz(*ll) for ll in ll_poly1] xyz_poly2 = [latlon2xyz(*ll) for ll in ll_poly2] ret = intersect_two_polygons(xyz_poly1, xyz_poly2) ll_expect = [(pi/2,0), (0,pi/3), (0,pi/2)] xyz_expect = [latlon2xyz(*ll) for ll in ll_expect] a_equal(ret, xyz_expect)
def main(): ''' main() ''' # # fibonacci() 함수 테스트 # fib_list = list() for fib in fibonacci(): if fib > 89: break else: fib_list.append(fib) a_equal(fib_list, [1, 2, 3, 5, 8, 13, 21, 34, 55, 89]) # # sum_fibonacci_under() 함수 테스트 # equal(sum_fibonacci_under(89), sum([2, 8, 34])) # # 문제 풀이 # summed = sum_fibonacci_under(4000000) print('짝수이면서 4백만 이하인 피보나치 수열의 합?', summed)
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_get_surround_4_uids(): ''' CubeGridRemap.get_surround_4_uids(): ne=30 ''' from cube_remap import CubeGridRemap from pkg.convert_coord.cs_ll import abp2latlon ne, ngq = 30, 4 rotated = False cube = CubeGridRemap(ne, ngq, rotated) td = (np.pi/2)/ne/3/2 # tiny delta ij = (1,2,1,1,1) gid = cube.ij2gid[ij] alpha, beta = cube.alpha_betas[gid] lat, lon = abp2latlon(alpha+td, beta+td, ij[0]) ret_uids = cube.get_surround_4_uids(lat, lon) a_equal(ret_uids, [3,16,7,19]) ij = (1,2,1,2,3) gid = cube.ij2gid[ij] alpha, beta = cube.alpha_betas[gid] lat, lon = abp2latlon(alpha+td, beta+td, ij[0]) ret_uids = cube.get_surround_4_uids(lat, lon) a_equal(ret_uids, [22,23,25,26]) '''
def test_polygon_line(): ''' intersection between polygon and line ''' #------------------------------------------------- # inclusion #------------------------------------------------- poly = Polygon([(0,0),(0,1),(1,1),(1,0)]) line = LineString([(0,0), (0.5,0.5)]) iline = poly.intersection(line) equal(np.sqrt(2)*0.5, iline.length) a_equal(line, np.array(iline.coords)) #------------------------------------------------- # partially #------------------------------------------------- poly = Polygon([(0,0),(0,1),(1,1),(1,0)]) line = LineString([(0.5,0.5),(1.5,1.5)]) iline = poly.intersection(line) equal(np.sqrt(2)*0.5, iline.length) a_equal(LineString([(0.5,0.5),(1,1)]), np.array(iline.coords)) #------------------------------------------------- # not intersection #------------------------------------------------- poly = Polygon([(0,0),(0,1),(1,1),(1,0)]) line = LineString([(1,1),(2,2)]) iline = poly.intersection(line) equal(0, iline.length)
def check_sparse_matrix_on_mvp(f, dsts, srcs, weights, mvps): ''' Check same values on the MVP with random numbers... ''' #----------------------------------------------------- # Avg the element boundary for the spectral element method # using the given sparse matrix #----------------------------------------------------- unique_dsts, index_dsts = np.unique(dsts, return_index=True) dst_group = list(index_dsts) + [len(dsts)] tmp = np.zeros(len(unique_dsts), 'f8') for u_seq, (start, end) in enumerate(zip(dst_group[:-1], dst_group[1:])): ws_list = [weights[i]*f[srcs[i]] for i in range(start,end)] tmp[u_seq] = fsum(ws_list) for u_seq, u_dst in enumerate(unique_dsts): f[u_dst] = tmp[u_seq] #----------------------------------------------------- # Check if mvps have same values #----------------------------------------------------- for seq, mvp in enumerate(mvps): eff_mvp = [k for k in mvp if k != -1] for m in eff_mvp: a_equal(f[seq], f[m])
def main(): ''' main() ''' x = np.random.rand() mymath = MyMath() f = mymath.cos(x) a_equal(f, np.cos(x))
def check_consistency_mvps(mvps): for seq, mvp in enumerate(mvps): mvp0 = [k for k in mvp if k != -1] for m in mvp0: mvp1 = [k for k in mvps[m] if k != -1] for i in xrange(4): mvp1_roll = np.roll(mvp1,i) if mvp1_roll[0] == seq: break a_equal(mvp0, mvp1_roll)
def test_get_surround_4_gids(): """ CubeGridRemap.get_surround_4_gids(): ne=30 """ from cube_remap import CubeGridRemap ne, ngq = 30, 4 rotated = False cube = CubeGridRemap(ne, ngq, rotated) lat, lon = -0.78973737977, 0.0 abp, ret_gids = cube.get_surround_4_gids(lat, lon) a_equal(ret_gids, [71754, 71755, 71758, 71759])
def test_line_line(): ''' intersection between line and line ''' #------------------------------------------------- # intersection #------------------------------------------------- line1 = LineString([(0,0), (1,1)]) line2 = LineString([(1,0), (0,1)]) ist = line1.intersection(line2) equal(ist.geom_type, 'Point') a_equal([0.5,0.5], np.array(ist.coords)[0]) #------------------------------------------------- # parallel # line intersection #------------------------------------------------- line1 = LineString([(0,0), (1,1)]) line2 = LineString([(-1,-1), (0.5,0.5)]) ist = line1.intersection(line2) equal(ist.geom_type, 'LineString') a_equal([(0,0),(0.5,0.5)], np.array(ist.coords)) #------------------------------------------------- # parallel # not intersection #------------------------------------------------- line1 = LineString([(0,0), (1,1)]) line2 = LineString([(0,-1), (1,0)]) ist = line1.intersection(line2) equal(True, ist.is_empty) #------------------------------------------------- # not intersection #------------------------------------------------- line1 = LineString([(0,0), (1,1)]) line2 = LineString([(3,0), (0,3)]) ist = line1.intersection(line2) equal(True, ist.is_empty)
def main(): ''' main() ''' # # 1~10 사이의 어떤 수로도 나누어 떨어지는 가장 작은 수 2520 테스트 # a_equal(least_common_multiple(range(1, 11)), 2520) # # 문제 풀이 # lcm = least_common_multiple(range(1, 21)) print('1~20 사이의 어떤 수로도 나누어 떨어지는 가장 작은 수: {}'.format(lcm))
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))
def test_nvidia_gpu_cuda(): ''' DevicePlatform, c = a + b: NVIDIA_GPU, CUDA ''' src = ''' __global__ void add(int shift_gid, int nx, double *a, double *b, double *c) { int gid = blockDim.x * blockIdx.x + threadIdx.x + shift_gid; if (gid >= nx) return; c[gid] = a[gid] + b[gid]; } ''' from device_platform import NVIDIA_GPU_CUDA platform = NVIDIA_GPU_CUDA() lib = platform.source_compile(src) add = platform.get_function(lib, 'add') #------------------------------------------- # call directly #------------------------------------------- nx = 1000000 a = np.random.rand(nx) b = np.random.rand(nx) c = np.zeros(nx) cuda = platform.cuda nx_cu = np.int32(nx) a_cu = cuda.to_device(a) b_cu = cuda.to_device(b) c_cu = cuda.mem_alloc_like(c) add(np.int32(0), nx_cu, a_cu, b_cu, c_cu, block=(512,1,1), grid=(nx//512+1,1)) cuda.memcpy_dtoh(c, c_cu) a_equal(a+b, c) #---------------------------------------------------------- # call using the array wrapper #---------------------------------------------------------- aa = platform.ArrayAs(a) bb = platform.ArrayAs(b) cc = platform.Array(aa.size, aa.dtype) add.prepare('iooo', nx, aa, bb, cc, gsize=nx) add.prepared_call() a_equal(aa.get()+bb.get(), cc.get())
def test_avg_sequential_3_4_1(): ''' CubeMPI for AVG: Exact squential values (ne=3, ngq=4, nproc=1) ''' ne, ngq = 3, 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 sequential field on the cubed-sphere #----------------------------------------------------- f = np.arange(cubegrid.local_ep_size, dtype='f8') #----------------------------------------------------- # 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 #----------------------------------------------------- fs = [f] ranks, lids = cubegrid.ranks, cubegrid.lids 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 gid in eff_mvp: rank, lid = ranks[gid], lids[gid] ok_( feq(fs[rank][lid], np.mean(eff_mvp), 15) )
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 main(): ''' main() ''' # # prime_factors() 함수 테스트 # a_equal(prime_factors(13195), [5, 7, 13, 29]) # # 문제 풀이 # number = 600851475143 p_factors = prime_factors(number) print('{} 의 소인수들: {}'.format(number, p_factors)) print('{} 의 최대소인수: {}'.format(number, p_factors[-1]))
def test_cpu_f90(): ''' DevicePlatform, c = a + b: CPU, F90 ''' src = ''' SUBROUTINE add(nx, a, b, c) IMPLICIT NONE INTEGER, INTENT(IN) :: nx REAL(8), DIMENSION(nx), INTENT(IN) :: a, b REAL(8), DIMENSION(nx), INTENT(INOUT) :: c INTEGER :: ii DO ii=1,nx c(ii) = a(ii) + b(ii) END DO END SUBROUTINE ''' from device_platform import DevicePlatform platform = DevicePlatform('CPU', 'F90') lib, out, err = capture(platform.source_compile)(src, compiler='gnu', flags='', opt_flags='-O3') add = platform.get_function(lib, 'add') #---------------------------------------------------------- # call directly #---------------------------------------------------------- nx = 1000000 a = np.random.rand(nx) b = np.random.rand(nx) c = np.zeros(nx) add(nx, a, b, c) a_equal(a+b, c) #---------------------------------------------------------- # call using the array wrapper #---------------------------------------------------------- aa = platform.ArrayAs(a) bb = platform.ArrayAs(b) cc = platform.Array(aa.size, aa.dtype) add.prepare('iooo', nx, aa, bb, cc) add.prepared_call() a_equal(aa.get()+bb.get(), cc.get())
def test_cpu_c(): ''' DevicePlatform, c = a + b: CPU, C ''' src = ''' void add(int nx, double *a, double *b, double *c) { // size and intent of array arguments for f2py // a :: nx, in // b :: nx, in // c :: nx, inout int i; for (i=0; i<nx; i++) { c[i] = a[i] + b[i]; } } ''' from device_platform import CPU_C platform = CPU_C() lib, out, err = capture(platform.source_compile)(src, compiler='gnu', flags='', opt_flags='-O3') add = platform.get_function(lib, 'add') #---------------------------------------------------------- # call directly #---------------------------------------------------------- nx = 1000000 a = np.random.rand(nx) b = np.random.rand(nx) c = np.zeros(nx) add(nx, a, b, c) a_equal(a+b, c) #---------------------------------------------------------- # call using the array wrapper #---------------------------------------------------------- aa = platform.ArrayAs(a) bb = platform.ArrayAs(b) cc = platform.Array(aa.size, aa.dtype) add.prepare('iooo', nx, aa, bb, cc) add.prepared_call() a_equal(aa.get()+bb.get(), cc.get())
def main(): ''' main() ''' nx, ny = 1000, 1200 in_1d = np.random.rand(nx) out_1d = np.zeros(nx, 'f8') in_2d = np.random.rand(nx*ny).reshape((nx, ny), order='F') out_2d = np.zeros((nx, ny), 'f8', order='F') mymath = MyMath(nx, ny) mymath.cos(in_1d, out_1d) a_equal(out_1d, np.cos(in_1d)) mymath.cos2d(in_2d, out_2d) a_equal(out_2d, np.cos(in_2d))
def main(): ''' main() ''' # # 두 자리 수로 만들어진 가장 큰 대칭수 9009(91x99) 테스트 # a_equal(find_max_palindrome(2), (9009, 91, 99)) # # 문제 풀이 # num_digit = 3 p_factors = find_max_palindrome(num_digit) number, factor1, factor2 = find_max_palindrome(3) print('{} 자리수로 만들어진 최대 대칭수: {} ({}x{})'.format( \ num_digit, number, factor1, factor2))
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)
def test_get_surround_elem_gids(): """ CubeGridRemap.get_surround_elem_gids(): ne=30 """ from cube_remap import CubeGridRemap from util.convert_coord.cs_ll import abp2latlon ne, ngq = 30, 4 rotated = False cube = CubeGridRemap(ne, ngq, rotated) td = (np.pi / 2) / ne / 3 / 2 # tiny delta ij = (1, 2, 1, 1, 1) gid = cube.ij2gid[ij] alpha, beta = cube.alpha_betas[gid] lat, lon = abp2latlon(alpha + td, beta + td, ij[0]) ret_gids = cube.get_surround_elem_gids(lat, lon) a_equal(ret_gids, [16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31]) ret_uids = [cube.uids[gid] for gid in ret_gids] a_equal(ret_uids, [3, 16, 17, 18, 7, 19, 20, 21, 11, 22, 23, 24, 15, 25, 26, 27])
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') y2 = y.copy() t1 = datetime.now() saxpy_numpy(a, x, y) dt_numpy = datetime.now() - t1 obj = SAXPY() t2 = datetime.now() obj.saxpy_c(n, a, x, y2) dt_c = datetime.now() - t2 print('n={}'.format(n)) print('numpy: {}'.format(dt_numpy)) print('c : {}'.format(dt_c)) a_equal(y, y2) print('Check result: OK!)
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') y2 = y.copy() t1 = datetime.now() saxpy_numpy(a, x, y) dt_numpy = datetime.now() - t1 obj = SAXPY_C() t2 = datetime.now() obj.saxpy_c(n, a, x, y2) dt_c = datetime.now() - t2 print('n={}'.format(n)) print('numpy: {}'.format(dt_numpy)) print('c : {}'.format(dt_c)) a_equal(y, y2) print('Check result: OK!')
def test_compile_using_f2py_c(): ''' compile_using_f2py: add.c ''' from source_module import compile_using_f2py, get_module_from_file # compile and import src = ''' void add(int nx, double *a, double *b, double *c) { // size and intent of array arguments for f2py // a :: nx, in // b :: nx, in // c :: nx, inout int i; for (i=0; i<nx; i++) { c[i] = a[i] + b[i]; } } ''' code_type = 'c' dpath = os.path.join(current_dpath, 'src') build_dpath = os.path.join(dpath, 'build') src_fpath = os.path.join(dpath, 'add.'+code_type) with open(src_fpath, 'w') as f: f.write(src) ret, out, err = capture(compile_using_f2py)(src_fpath, compiler='gnu') mod = get_module_from_file(build_dpath, 'add', code_type) # setup nx = 1000000 a = np.random.rand(nx) b = np.random.rand(nx) c = np.zeros(nx) mod.add(nx, a, b, c) a_equal(a+b, c)
def test_compile_using_f2py_f90(): ''' compile_using_f2py: add.f90 ''' from source_module import compile_using_f2py, get_module_from_file # compile and import src = ''' SUBROUTINE add(nx, a, b, c) IMPLICIT NONE INTEGER, INTENT(IN) :: nx REAL(8), DIMENSION(nx), INTENT(IN) :: a, b REAL(8), DIMENSION(nx), INTENT(INOUT) :: c INTEGER :: ii DO ii=1,nx c(ii) = a(ii) + b(ii) END DO END SUBROUTINE ''' code_type = 'f90' dpath = os.path.join(current_dpath, 'src') build_dpath = os.path.join(dpath, 'build') src_fpath = os.path.join(dpath, 'add.'+code_type) with open(src_fpath, 'w') as f: f.write(src) ret, out, err = capture(compile_using_f2py)(src_fpath, compiler='gnu') mod = get_module_from_file(build_dpath, 'add', code_type) # setup nx = 1000000 a = np.random.rand(nx) b = np.random.rand(nx) c = np.zeros(nx) mod.add(nx, a, b, c) a_equal(a+b, c)
def test_sort_ccw_idxs(): ''' sort_ccw_idxs(): normal case, straight line, duplicated ''' from sphere import sort_ccw_idxs from duplicate import remove_duplicates from convert_coord.cart_ll import latlon2xyz # normal lls = [(0.79,0.79), (0.78,0.77), (0.78,0.79), (0.79,0.77), (0.80,0.78)] xyzs = [latlon2xyz(*ll) for ll in lls] ret = sort_ccw_idxs(xyzs) a_equal(ret, [0,4,3,1,2]) # straight line lls = [(0.79,0.79), (0.78,0.77), (0.78,0.79), \ (0.79,0.77), (0.80,0.78), (0.78,0.78)] xyzs = [latlon2xyz(*ll) for ll in lls] ret = sort_ccw_idxs(xyzs) a_equal(ret, [0,4,3,1,5,2]) #----------------------------------------------------- # duplicated #----------------------------------------------------- lls = [(-0.34784230590688509, 6.1959188445798699), (-0.3478423059068852, 0.08726646259971646), (-0.52194946399942688, 0.08726646259971646), (-0.52194946399942688, 6.1959188445798699), (-0.52194946399942688, 6.1959188445798699)] xyzs = [latlon2xyz(*ll) for ll in lls] unique_xyzs = remove_duplicates(xyzs) ret = sort_ccw_idxs(unique_xyzs) a_equal(ret, [0,3,2,1]) #----------------------------------------------------- lls = [(-1.3956102462281967, 0.43633231299858227), (-1.3956102462281967, 0.26179938779914985), (-1.5707963267948966, 0), (-1.5707963267948966, 0)] xyzs = [latlon2xyz(*ll) for ll in lls] unique_xyzs = remove_duplicates(xyzs) ret = sort_ccw_idxs(unique_xyzs) a_equal(ret, [0,1,2])
def test_get_surround_idxs(): ''' LatlonGridRemap.get_surround_idxs(): nlat=180, nlon=360 (regular) ''' from cube_remap import LatlonGridRemap nlat, nlon = 180, 360 tx = 1e-3 ll = LatlonGridRemap(nlat, nlon, 'regular') # Near south pole lat0 = ll.tmp_lats[1] lon0 = ll.tmp_lons[7] ret_idxs = ll.get_surround_idxs(lat0-tx,lon0+tx) equal(ret_idxs, (7,-1,-1,-1)) # Near north pole lat0 = ll.tmp_lats[-2] lon0 = ll.tmp_lons[-2] ret_idxs = ll.get_surround_idxs(lat0+tx,lon0+tx) equal(ret_idxs, (nlat*nlon-1,-1,-1,-1)) # First box lat0 = ll.tmp_lats[1] lon0 = ll.tmp_lons[0] ret_idxs = ll.get_surround_idxs(lat0+tx,lon0+tx) a_equal(ret_idxs, [0,1,nlon,nlon+1]) # Last box lat0 = ll.tmp_lats[-2] lon0 = ll.tmp_lons[-2] ret_idxs = ll.get_surround_idxs(lat0-tx,lon0+tx) a_equal(ret_idxs, [(nlat-1)*nlon-1, (nlat-2)*nlon, nlat*nlon-1, (nlat-1)*nlon]) # Near Meridian lat0 = ll.tmp_lats[1] lon0 = ll.tmp_lons[-2] ret_idxs = ll.get_surround_idxs(lat0+tx,lon0+tx) a_equal(ret_idxs, [nlon-1,0,nlon*2-1,nlon]) # Error cases lat, lon = -0.785398163397, 6.28318530718 ret_idxs = ll.get_surround_idxs(lat,lon) a_equal(ret_idxs, [16199, 15840, 16559, 16200])
def test_set_elem_proc_30_16(): ''' elem_proc(HOMME): ne=30, nproc=16 ''' from glob import glob import re import netCDF4 as nc from cube_mpi import CubeGridMPI from path import cs_grid_dpath ne, ngq = 30, 4 nproc = 16 cubegrid = CubeGridMPI(ne, ngq, nproc, myrank=0, cs_grid_dpath=cs_grid_dpath, homme_style=True) my_elem_gid = cubegrid.local_gids[::ngq*ngq]//(ngq*ngq) + 1 homme_elem_gid = np.array([275, 276, 301, 302, 303, 304, 305, 306, 331, 332, 333, 334, 335, 336, 361, 362, 363, 364, 365, 366, 367, 368, 369, 370, 371, 372, 373, 374, 375, 376, 377, 378, 391, 392, 393, 394, 395, 396, 397, 398, 399, 400, 401, 402, 403, 404, 405, 406, 407, 408, 421, 422, 423, 424, 425, 426, 427, 428, 429, 430, 431, 432, 433, 434, 435, 436, 437, 438, 451, 452, 453, 454, 455, 456, 457, 458, 459, 460, 461, 462, 463, 464, 465, 466, 467, 468, 481, 482, 483, 484, 485, 486, 487, 488, 489, 490, 491, 492, 493, 494, 495, 496, 497, 498, 511, 512, 513, 514, 515, 516, 517, 518, 519, 520, 521, 522, 523, 524, 525, 526, 527, 528, 541, 542, 543, 544, 545, 546, 547, 548, 549, 550, 551, 552, 553, 554, 555, 556, 557, 558, 571, 572, 573, 574, 575, 576, 577, 578, 579, 580, 581, 582, 583, 584, 585, 586, 587, 588, 601, 602, 603, 604, 605, 606, 607, 608, 609, 610, 611, 612, 613, 614, 615, 616, 617, 618, 631, 632, 633, 634, 635, 636, 637, 638, 639, 640, 641, 642, 643, 644, 645, 646, 647, 648, 661, 662, 663, 664, 665, 666, 667, 668, 669, 670, 671, 672, 673, 674, 675, 676, 677, 678, 691, 692, 693, 694, 695, 696, 697, 698, 699, 700, 701, 702, 703, 704, 705, 706, 707, 708, 721, 722, 723, 724, 725, 726, 727, 728, 729, 730, 731, 732, 733, 734, 735, 736, 737, 738, 751, 752, 753, 754, 755, 756, 757, 758 ,759, 760, 761, 762, 763, 764, 765, 766, 767, 768, 781, 782, 783, 784, 785, 786, 787, 788, 789, 790, 791, 792, 793, 794, 795, 796, 797, 798, 811, 812, 813, 814, 815, 816, 817, 818, 819, 820, 821, 822, 823, 824, 825, 826, 827, 828, 841, 842, 843, 844, 845, 846, 847, 848, 849, 850, 851, 852, 853, 854, 855, 856, 857, 858, 871, 872, 873, 874, 875, 876, 877, 878, 879, 880, 881, 882, 883, 884, 885, 886, 887, 888], 'i4') a_equal(my_elem_gid, homme_elem_gid) cubegrid = CubeGridMPI(ne, ngq, nproc, myrank=1, cs_grid_dpath=cs_grid_dpath, homme_style=True) my_elem_gid = cubegrid.local_gids[::ngq*ngq]//(ngq*ngq) + 1 homme_elem_gid = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, 292, 295, 296, 297, 298, 299, 300, 307, 308, 309, 310, 311, 312, 313, 314, 315, 316, 317, 318, 319, 320, 321, 322, 325, 326, 327, 328, 329, 330, 337, 338, 339, 340, 341, 342, 343, 344, 345, 346, 347, 348, 349, 350, 351, 352, 355, 356, 357, 358, 359, 360], 'i4') a_equal(my_elem_gid, homme_elem_gid) cubegrid = CubeGridMPI(ne, ngq, nproc, myrank=2, cs_grid_dpath=cs_grid_dpath, homme_style=True) my_elem_gid = cubegrid.local_gids[::ngq*ngq]//(ngq*ngq) + 1 homme_elem_gid = np.array([263, 264, 293, 294, 323, 324, 353, 354, 379, 380, 381, 382, 383, 384, 385, 386, 387, 388, 389, 390, 409, 410, 411, 412, 413, 414, 415, 416, 417, 418, 419, 420, 439, 440, 441, 442, 443, 444, 445, 446, 447, 448, 449, 450, 469, 470, 471, 472, 473, 474, 475, 476, 477, 478, 479, 480, 499, 500, 501, 502, 503, 504, 505, 506, 507, 508, 509, 510, 529, 530, 531, 532, 533, 534, 535, 536, 537, 538, 539, 540, 559, 560, 561, 562, 563, 564, 565, 566, 567, 568, 569, 570, 589, 590, 591, 592, 593, 594, 595, 596, 597, 598, 599, 600, 619, 620, 621, 622, 623, 624, 625, 626, 627, 628, 629, 630, 649, 650, 651, 652, 653, 654, 655, 656, 657, 658, 659, 660, 679, 680, 681, 682, 683, 684, 685, 686, 687, 688, 689, 690, 709, 710, 711, 712, 713, 714, 715, 716, 717, 718, 719, 720, 739, 740, 741, 742, 743, 744, 745, 746, 747, 748, 749, 750, 769, 770, 771, 772, 773, 774, 775, 776, 777, 778, 779, 780, 799, 800, 801, 802, 803, 804, 805, 806, 807, 808, 809, 810, 829, 830, 831, 832, 833, 834, 835, 836, 837, 838, 839, 840, 859, 860, 861, 862, 863, 864, 865, 866, 867, 868, 869, 870, 889, 890, 891, 892, 893, 894, 895, 896, 897, 898, 899, 900, 1573, 1574, 1575, 1603, 1604, 1605, 1621, 1622, 1623, 1624, 1625, 1626, 1627, 1628, 1629, 1630, 1631, 1632, 1633, 1634, 1635, 1636, 1637, 1638, 1651, 1652, 1653, 1654, 1655, 1656, 1657, 1658, 1659, 1660, 1661, 1662, 1663, 1664, 1665, 1666, 1667, 1668, 1681, 1682, 1683, 1684, 1685, 1686, 1687, 1688, 1689, 1690, 1691, 1692, 1693, 1694, 1695, 1696, 1697, 1698, 1711, 1712, 1713, 1714, 1715, 1716, 1717, 1718, 1719, 1720, 1721, 1722, 1723, 1724, 1725, 1726, 1727, 1728, 1741, 1742, 1743, 1744, 1745, 1746, 1747, 1748, 1749, 1750, 1751, 1752, 1753, 1754, 1755, 1756, 1757, 1758, 1771, 1772, 1773, 1774, 1775, 1776, 1777, 1778, 1779, 1780, 1781, 1782, 1783, 1784, 1785, 1786, 1787, 1788, ], 'i4') a_equal(my_elem_gid, homme_elem_gid) fpath_list = glob('./KIM_lid_gid_ne30_nproc16/nproc16_rank*.nc') for fpath in fpath_list: ncf = nc.Dataset(fpath, 'r', format='NETCDF3_CLASSIC') local_element_size = len( ncf.dimensions['local_element_size'] ) lid_elements = ncf.variables['lid_elements'][:] gid_elements = ncf.variables['gid_elements'][:] rank = int( re.search('rank([0-9]+).nc',fpath).group(1) ) cubegrid = CubeGridMPI(ne, ngq, nproc, rank, homme_style=True) my_elem_gid = cubegrid.local_gids[::ngq*ngq]//(ngq*ngq) + 1 a_equal(my_elem_gid, gid_elements)
def test_set_factor_list(): """ factor_list: ne=30, nproc=1, 6*2*2, 6*3*3 """ from cube_partition import CubePartition cube = CubePartition(ne=2 * 3 * 5, nproc=1) a_equal(cube.factor_list, [2, 3, 5]) cube = CubePartition(ne=2 * 3 * 5, nproc=6 * 2 * 2) a_equal(cube.factor_list, [3, 5, 2]) cube = CubePartition(ne=2 * 3 * 5, nproc=6 * 3 * 3) a_equal(cube.factor_list, [2, 5, 3])