def verify(s, pt0, pt1): print('pt0 = %s, pt1 = %s' % (pt0, pt1)) slidx = common.get_slice_index(pt0, pt1) shape = common.get_shape(pt0, pt1) for strf in s.strf_list: # non-spatial fset = SetFields(s.fdtd, strf, pt0, pt1) values = np.random.rand(*shape).astype(s.fdtd.dtype) fset.set_fields(values) fget = GetFields(s.fdtd, strf, pt0, pt1) fget.get_event().wait() copy = fget.get_fields(strf) assert np.linalg.norm(values - copy) == 0 if pt0 != pt1: for strf in s.strf_list: # spatial fset = SetFields(s.fdtd, strf, pt0, pt1, np.ndarray) values = np.random.rand(*shape).astype(s.fdtd.dtype) fset.set_fields(values) fget = GetFields(s.fdtd, strf, pt0, pt1) fget.get_event().wait() copy = fget.get_fields(strf) assert np.linalg.norm(values - copy) == 0
def __init__(self, buffer_fields, max_tstep): common.check_type('buffer_fields', buffer_fields, BufferFields) # local variables mainf = buffer_fields nx, ny, nz = mainf.ns dtype = mainf.dtype direction = mainf.direction target_rank = mainf.target_rank # create instances (getf, setf and mpi requests) if '+' in direction: # split h getf = GetFields(mainf, ['hy', 'hz'], (1, 0, 0), (1, ny - 1, nz - 1)) setf = SetFields(mainf, ['ey', 'ez'], (2, 0, 0), (2, ny - 1, nz - 1), True) if rank < target_rank: tag_send, tag_recv = 0, 1 elif rank > target_rank: # pbc tag_send, tag_recv = 2, 3 elif '-' in direction: # split e getf = GetFields(mainf, ['ey', 'ez'], (1, 0, 0), (1, ny - 1, nz - 1)) setf = SetFields(mainf, ['hy', 'hz'], (0, 0, 0), (0, ny - 1, nz - 1), True) if rank > target_rank: tag_send, tag_recv = 1, 0 elif rank < target_rank: # pbc tag_send, tag_recv = 3, 2 req_send = comm.Send_init(getf.host_array, target_rank, tag=tag_send) tmp_recv_list = [ np.zeros(getf.host_array.shape, dtype) for i in range(2) ] req_recv_list = [ comm.Recv_init(tmp_recv, target_rank, tag=tag_recv) for tmp_recv in tmp_recv_list ] # global variables and functions self.getf = getf self.setf = setf self.req_send = req_send self.req_recv_list = req_recv_list self.tmp_recv_list = tmp_recv_list self.switch = 0 self.max_tstep = max_tstep self.tstep = 1 # append to the update list self.priority_type = 'mpi' mainf.append_instance(self)
def test_boundary(s): print('\n-- test boundary (two fields) --') shape_dict = { 'x': (s.ny * 2, s.nz), 'y': (s.nx * 2, s.nz), 'z': (s.nx * 2, s.ny) } print('E fields') str_fs_dict = {'x': ['ey', 'ez'], 'y': ['ex', 'ez'], 'z': ['ex', 'ey']} pt0_dict = { 'x': (s.nx - 1, 0, 0), 'y': (0, s.ny - 1, 0), 'z': (0, 0, s.nz - 1) } pt1 = (s.nx - 1, s.ny - 1, s.nz - 1) for axis in str_fs_dict.keys(): print('direction : %s' % axis) str_fs = str_fs_dict[axis] pt0 = pt0_dict[axis] slidx = common.get_slice_index(pt0, pt1) fset = SetFields(s.fdtd, str_fs, pt0, pt1, np.ndarray) values = np.random.rand(*shape_dict[axis]).astype(s.fdtd.dtype) fset.set_fields(values) fget = GetFields(s.fdtd, str_fs, pt0, pt1) fget.get_event().wait() copy = fget.get_fields() assert np.linalg.norm(values - copy) == 0 print('H fields') str_fs_dict = {'x': ['hy', 'hz'], 'y': ['hx', 'hz'], 'z': ['hx', 'hy']} pt0 = (0, 0, 0) pt1_dict = { 'x': (0, s.ny - 1, s.nz - 1), 'y': (s.nx - 1, 0, s.nz - 1), 'z': (s.nx - 1, s.ny - 1, 0) } for axis in str_fs_dict.keys(): print('direction : %s' % axis) str_fs = str_fs_dict[axis] pt1 = pt1_dict[axis] slidx = common.get_slice_index(pt0, pt1) fset = SetFields(s.fdtd, str_fs, pt0, pt1, np.ndarray) values = np.random.rand(*shape_dict[axis]).astype(s.fdtd.dtype) fset.set_fields(values) fget = GetFields(s.fdtd, str_fs, pt0, pt1) fget.get_event().wait() copy = fget.get_fields() assert np.linalg.norm(values - copy) == 0
def __init__(self, buffer_fields, target_rank): common.check_type('buffer_fields', buffer_fields, BufferFields) common.check_type('target_rank', target_rank, int) # local variables mainf = buffer_fields nx, ny, nz = mainf.ns dtype = mainf.dtype direction = mainf.direction assert rank != target_rank, 'The target_rank %d is same as the my_rank %d.' % ( target_rank, rank) # create instances (getf, setf and mpi requests) if '+' in direction: # split h getf = GetFields(mainf, ['hy', 'hz'], (1, 0, 0), (1, ny - 1, nz - 1)) setf = SetFields(mainf, ['ey', 'ez'], (2, 0, 0), (2, ny - 1, nz - 1), True) if rank < target_rank: tag_send, tag_recv = 0, 1 elif rank > target_rank: # pbc tag_send, tag_recv = 2, 3 elif '-' in direction: # split e getf = GetFields(mainf, ['ey', 'ez'], (1, 0, 0), (1, ny - 1, nz - 1)) setf = SetFields(mainf, ['hy', 'hz'], (0, 0, 0), (0, ny - 1, nz - 1), True) if rank > target_rank: tag_send, tag_recv = 1, 0 elif rank < target_rank: # pbc tag_send, tag_recv = 3, 2 # global variables and functions self.mainf = mainf self.target_rank = target_rank self.getf = getf self.setf = setf self.tag_send = tag_send self.tag_recv = tag_recv self.tmp_recv = np.zeros(getf.host_array.shape, dtype) # global functions self.update_e = self.recv if '+' in direction else self.send self.update_h = self.send if '+' in direction else self.recv # append to the update list self.priority_type = 'mpi' mainf.append_instance(self)
def test(self): nx, ny, nz = 40, 50, 60 tmax = 10 # buffer instance if rank == 0: fields = Fields(10, ny, nz, mpi_type='x+') exmpi = ExchangeMpi(fields, 1, tmax) elif rank == 1: fields = Fields(3, ny, nz, mpi_type='x-') exmpi = ExchangeMpi(fields, 0, tmax) # generate random source nx, ny, nz = fields.ns ehs = common_update.generate_random_ehs(nx, ny, nz, fields.dtype) fields.set_ehs(*ehs) # verify for tstep in xrange(1, tmax + 1): fields.update_e() fields.update_h() getf_dict = {} if rank == 0: getf_dict['e'] = GetFields(fields, ['ey', 'ez'], \ (nx-1, 0, 0), (nx-1, ny-2, nz-2)) getf_dict['h'] = GetFields(fields, ['hy', 'hz'], \ (1, 1, 1), (1, ny-1, nz-1)) for eh in ['e', 'h']: getf = getf_dict[eh] getf.get_event().wait() g0 = getf.get_fields() g1 = np.zeros_like(g0) comm.Recv(g1, 1, tag=10) norm = np.linalg.norm(g0 - g1) self.assertEqual(norm, 0, '%g, %s, %s' % (norm, 'x', 'e')) elif rank == 1: getf_dict['e'] = GetFields(fields, ['ey', 'ez'], \ (nx-2, 0, 0), (nx-2, ny-2, nz-2)) getf_dict['h'] = GetFields(fields, ['hy', 'hz'], \ (0, 1, 1), (0, ny-1, nz-1)) for eh in ['e', 'h']: getf = getf_dict[eh] getf.get_event().wait() comm.Send(getf.get_fields(), 0, tag=10)
def runTest(self): nx, ny, nz, str_f, pt0, pt1, is_array, mpi_type = self.args slices = common.slice_index_two_points(pt0, pt1) # generate random source if is_array: shape = common.shape_two_points(pt0, pt1) value = np.random.rand(*shape).astype(np.float32) else: value = np.random.ranf() # instance fields = Fields(nx, ny, nz, '', 'single', 0, mpi_type=mpi_type) tfunc = lambda tstep: np.sin(0.03 * tstep) incident = DirectIncident(fields, str_f, pt0, pt1, tfunc, value) # host allocations eh = np.zeros(fields.ns_pitch, dtype=fields.dtype) getf = GetFields(fields, str_f, pt0, pt1) # verify eh[slices] = fields.dtype(value) * fields.dtype(tfunc(1)) fields.update_e() fields.update_h() fields.enqueue_barrier() original = eh[slices] getf.get_event().wait() copy = getf.get_fields() norm = np.linalg.norm(original - copy) self.assertEqual(norm, 0, '%s, %g' % (self.args, norm))
def __init__(self, fields, target_rank, tmax): common.check_type('fields', fields, Fields) # local variables nx, ny, nz = fields.ns dtype = fields.dtype mpi_type = fields.mpi_type common.check_value('mpi_type', mpi_type, \ ['x+', 'x-', 'y+', 'y-', 'z+', 'z-']) # create instances (getf, setf and mpi requests) if '+' in mpi_type: # split h getf = GetFields(fields, ['hy', 'hz'], \ (1, 1, 1), (1, ny-1, nz-1)) setf = SetFields(fields, ['ey', 'ez'], \ (nx-1, 0, 0), (nx-1, ny-2, nz-2), True) req_send = comm.Send_init(getf.host_array, target_rank, tag=1) tmp_recv = np.zeros(getf.host_array.shape, dtype) req_recv = comm.Recv_init(tmp_recv, target_rank, tag=2) elif '-' in mpi_type: # split e getf = GetFields(fields, ['ey', 'ez'], \ (nx-2, 0, 0), (nx-2, ny-2, nz-2)) setf = SetFields(fields, ['hy', 'hz'], \ (0, 1, 1), (0, ny-1, nz-1), True) req_send = comm.Send_init(getf.host_array, target_rank, tag=2) tmp_recv = np.zeros(getf.host_array.shape, dtype) req_recv = comm.Recv_init(tmp_recv, target_rank, tag=1) # global variables and functions self.mainf = fields self.getf = getf self.setf = setf self.tmp_recv = tmp_recv self.req_send = req_send self.req_recv = req_recv self.tmax = tmax self.tstep = 1 # append to the update list self.priority_type = 'mpi' self.mainf.append_instance(self)
def test_boundary(s): print('\n-- test boundary (two fields) --') print('E fields') str_fs_dict = {'x': ['ey', 'ez'], 'y': ['ex', 'ez'], 'z': ['ex', 'ey']} pt0 = (0, 0, 0) pt1_dict = { 'x': (0, s.ny - 1, s.nz - 1), 'y': (s.nx - 1, 0, s.nz - 1), 'z': (s.nx - 1, s.ny - 1, 0) } for axis in str_fs_dict.keys(): print('direction : %s' % axis) str_fs = str_fs_dict[axis] pt1 = pt1_dict[axis] slidx = common.get_slice_index(pt0, pt1) fget = GetFields(s.fdtd, str_fs, pt0, pt1) fget.get_event().wait() for strf in str_fs: original = s.fhosts[strf][slidx] copy = fget.get_fields(strf) assert np.linalg.norm(original - copy) == 0 print('H fields') str_fs_dict = {'x': ['hy', 'hz'], 'y': ['hx', 'hz'], 'z': ['hx', 'hy']} pt0_dict = { 'x': (s.nx - 1, 0, 0), 'y': (0, s.ny - 1, 0), 'z': (0, 0, s.nz - 1) } pt1 = (s.nx - 1, s.ny - 1, s.nz - 1) for axis in str_fs_dict.keys(): print('direction : %s' % axis) str_fs = str_fs_dict[axis] pt0 = pt0_dict[axis] slidx = common.get_slice_index(pt0, pt1) fget = GetFields(s.fdtd, str_fs, pt0, pt1) fget.get_event().wait() for strf in str_fs: original = s.fhosts[strf][slidx] copy = fget.get_fields(strf) assert np.linalg.norm(original - copy) == 0
def verify(s, pt0, pt1): print('pt0 = %s, pt1 = %s' % (pt0, pt1)) slidx = common.get_slice_index(pt0, pt1) for strf in s.strf_list: fget = GetFields(s.fdtd, strf, pt0, pt1) fget.get_event().wait() original = s.fhosts[strf][slidx] copy = fget.get_fields(strf) #print original, copy assert np.linalg.norm(original - copy) == 0
def runTest(self): nx, ny, nz, str_f, pt0, pt1 = self.args slice_xyz = common.slice_index_two_points(pt0, pt1) str_fs = common.convert_to_tuple(str_f) # instance fields = Fields(nx, ny, nz, '', 'single') getf = GetFields(fields, str_f, pt0, pt1) # host allocations ehs = common_update.generate_random_ehs(nx, ny, nz, fields.dtype) eh_dict = dict(zip(['ex', 'ey', 'ez', 'hx', 'hy', 'hz'], ehs)) fields.set_ehs(*ehs) # verify getf.get_event().wait() for str_f in str_fs: original = eh_dict[str_f][slice_xyz] copy = getf.get_fields(str_f) norm = np.linalg.norm(original - copy) self.assertEqual(norm, 0, '%s, %g' % (self.args, norm))
from kemp.fdtd3d.cpu import QueueTask, Fields, Core, Pbc, Pml, IncidentDirect, GetFields nx, ny, nz = 2, 250, 300 tmax, tgap = 300, 10 npml = 10 # instances fields = Fields(QueueTask(), nx, ny, nz, use_cpu_core=1) Pbc(fields, 'x') Pml(fields, ('', '+-', '+-'), npml) Core(fields) tfunc = lambda tstep: 50 * np.sin(0.05 * tstep) IncidentDirect(fields, 'ex', (0, 0.4, 0.3), (-1, 0.4, 0.3), tfunc) getf = GetFields(fields, 'ex', (0.5, 0, 0), (0.5, -1, -1)) print fields.instance_list # main loop from datetime import datetime t0 = datetime.now() for tstep in xrange(1, tmax+1): fields.update_e() fields.update_h() if tstep % tgap == 0: print('[%s] %d/%d (%d %%)\r' % (datetime.now() - t0, tstep, tmax, float(tstep)/tmax*100)), sys.stdout.flush()
# plot import matplotlib.pyplot as plt plt.ion() fig = plt.figure(figsize=(12,8)) imag = plt.imshow(np.zeros((nx, ny), fields.dtype).T, interpolation='nearest', origin='lower', vmin=-1.1, vmax=1.1) plt.colorbar() ''' # main loop from datetime import datetime from time import time t0 = datetime.now() t00 = time() gtmp = GetFields(fields, 'ez', (0, 0, 0), (0, 0, 0)) gtmp2 = GetFields(fields2, 'ez', (0, 0, 0), (0, 0, 0)) for tstep in xrange(1, tmax + 1): fields.update_e() fields2.update_e() fields.update_h() fields2.update_h() ''' if tstep % tgap == 0: print('[%s] %d/%d (%d %%)\r' % (datetime.now() - t0, tstep, tmax, float(tstep)/tmax*100)), sys.stdout.flush() getf.get_event().wait() imag.set_array( getf.get_fields().T ) #plt.savefig('./png/%.6d.png' % tstep) plt.draw()
sys.path.append( os.path.expanduser('~') ) from kemp.fdtd3d.cpu import Fields, Core, Pbc, IncidentDirect, GetFields nx, ny, nz = 160, 140, 32 tmax, tgap = 150, 10 # instances fields = Fields(nx, ny, nz) Core(fields) Pbc(fields, 'xyz') tfunc = lambda tstep: np.sin(0.05 * tstep) IncidentDirect(fields, 'ez', (20, 0, 0), (20, ny-1, nz-1), tfunc) #IncidentDirect(fields, 'ez', (0, 20, 0), (nx-1, 20, nz-1), tfunc) getf = GetFields(fields, 'ez', (0, 0, nz/2), (nx-1, ny-1, nz/2)) print fields.instance_list # plot import matplotlib.pyplot as plt plt.ion() fig = plt.figure(figsize=(12,8)) imag = plt.imshow(np.zeros((nx, ny), fields.dtype).T, interpolation='nearest', origin='lower', vmin=-1.1, vmax=1.1) plt.colorbar() # main loop from datetime import datetime t0 = datetime.now() for tstep in xrange(1, tmax+1):
# plot import matplotlib.pyplot as plt plt.ion() fig = plt.figure(figsize=(12,8)) imag = plt.imshow(np.zeros((nx, ny), fields.dtype).T, interpolation='nearest', origin='lower', vmin=-1.1, vmax=1.1) plt.colorbar() ''' # main loop from datetime import datetime from time import time t0 = datetime.now() t00 = time() gtmp = GetFields(fields, 'ez', (0, 0, 0), (0, 0, 0)) for tstep in xrange(1, tmax + 1): fields.update_e() fields.update_h() ''' if tstep % tgap == 0: print('[%s] %d/%d (%d %%)\r' % (datetime.now() - t0, tstep, tmax, float(tstep)/tmax*100)), sys.stdout.flush() getf.get_event().wait() imag.set_array( getf.get_fields().T ) #plt.savefig('./png/%.6d.png' % tstep) plt.draw() ''' #plt.show()
def runTest(self): axis, nx, ny, nz, mpi_type = self.args fields = Fields(nx, ny, nz, mpi_type=mpi_type) core = Core(fields) pbc = Pbc(fields, axis) # allocations ehs = common_update.generate_random_ehs(nx, ny, nz, fields.dtype) fields.set_ehs(*ehs) # update fields.update_e() fields.update_h() fields.enqueue_barrier() # verify getf0, getf1 = {}, {} strfs_e = { 'x': ['ey', 'ez'], 'y': ['ex', 'ez'], 'z': ['ex', 'ey'] }[axis] strfs_h = { 'x': ['hy', 'hz'], 'y': ['hx', 'hz'], 'z': ['hx', 'hy'] }[axis] pt0 = (0, 0, 0) pt1 = { 'x': (0, ny-2, nz-2), \ 'y': (nx-2, 0, nz-2), \ 'z': (nx-2, ny-2, 0) }[axis] getf0['e'] = GetFields(fields, strfs_e, pt0, pt1) pt0 = { 'x': (nx-1, 0, 0), \ 'y': (0, ny-1, 0), \ 'z': (0, 0, nz-1) }[axis] pt1 = { 'x': (nx-1, ny-2, nz-2), \ 'y': (nx-2, ny-1, nz-2), \ 'z': (nx-2, ny-2, nz-1) }[axis] getf1['e'] = GetFields(fields, strfs_e, pt0, pt1) pt0 = { 'x': (0, 1, 1), \ 'y': (1, 0, 1), \ 'z': (1, 1, 0) }[axis] pt1 = { 'x': (0, ny-1, nz-1), \ 'y': (nx-1, 0, nz-1), \ 'z': (nx-1, ny-1, 0) }[axis] getf0['h'] = GetFields(fields, strfs_h, pt0, pt1) pt0 = { 'x': (nx-1, 1, 1), \ 'y': (1, ny-1, 1), \ 'z': (1, 1, nz-1) }[axis] pt1 = (nx - 1, ny - 1, nz - 1) getf1['h'] = GetFields(fields, strfs_h, pt0, pt1) for getf in getf0.values() + getf1.values(): getf.get_event().wait() for eh in ['e', 'h']: g0 = getf0[eh].get_fields() g1 = getf1[eh].get_fields() norm = np.linalg.norm(g0 - g1) ''' print eh print g0 print g1 ''' self.assertEqual(norm, 0, '%g, %s, %s' % (norm, self.args, eh))
#!/usr/bin/env python import sys sys.path.append('/home/kifang') from kemp.fdtd3d.cpu import Fields, DirectSrc, GetFields import numpy as np nx, ny, nz = 240, 320, 320 tmax, tgap = 200, 10 fdtd = Fields(nx, ny, nz, coeff_use='', use_cpu_core=0) src = DirectSrc(fdtd, 'ez', (nx/5*4, ny/2, 0), (nx/5*4, ny/2, nz-1), lambda tstep: np.sin(0.1 * tstep)) output = GetFields(fdtd, 'ez', (0, 0, nz/2), (nx-1, ny-1, nz/2)) # Plot import matplotlib.pyplot as plt plt.ion() imag = plt.imshow(fdtd.ez[:,:,nz/2].T, cmap=plt.cm.hot, origin='lower', vmin=0, vmax=0.05) plt.colorbar() # Main loop from datetime import datetime t0 = datetime.now() for tstep in xrange(1, tmax+1): fdtd.update_e() src.update(tstep)