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 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 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 = 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') setf = SetFields(fields, str_f, pt0, pt1, is_array) # generate random source if is_array: shape = common.shape_two_points(pt0, pt1, len(str_fs)) value = np.random.rand(*shape).astype(fields.dtype) split_value = np.split(value, len(str_fs)) split_value_dict = dict( zip(str_fs, split_value) ) else: value = np.random.ranf() # host allocations ehs = [np.zeros(fields.ns, dtype=fields.dtype) for i in range(6)] eh_dict = dict( zip(['ex', 'ey', 'ez', 'hx', 'hy', 'hz'], ehs) ) # verify for str_f in str_fs: if is_array: eh_dict[str_f][slice_xyz] = split_value_dict[str_f] else: eh_dict[str_f][slice_xyz] = value setf.set_fields(value) fields.enqueue_barrier() for str_f in str_fs: original = eh_dict[str_f] copy = fields.get(str_f)[:,:,fields.slice_z] norm = np.linalg.norm(original - copy) self.assertEqual(norm, 0, '%s, %g' % (self.args, norm))
def runTest(self): nx, ny, nz, str_f, pt0, pt1, is_array = 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') setf = SetFields(fields, str_f, pt0, pt1, is_array) # generate random source if is_array: shape = common.shape_two_points(pt0, pt1, len(str_fs)) value = np.random.rand(*shape).astype(fields.dtype) split_value = np.split(value, len(str_fs)) split_value_dict = dict(zip(str_fs, split_value)) else: value = np.random.ranf() # host allocations ehs = [np.zeros(fields.ns, dtype=fields.dtype) for i in range(6)] eh_dict = dict(zip(['ex', 'ey', 'ez', 'hx', 'hy', 'hz'], ehs)) # verify for str_f in str_fs: if is_array: eh_dict[str_f][slice_xyz] = split_value_dict[str_f] else: eh_dict[str_f][slice_xyz] = value setf.set_fields(value) fields.enqueue_barrier() for str_f in str_fs: original = eh_dict[str_f] copy = fields.get(str_f)[:, :, fields.slice_z] norm = np.linalg.norm(original - copy) self.assertEqual(norm, 0, '%s, %g' % (self.args, norm))
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))
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))
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): axis, nx, ny, nz, mpi_type = self.args fields = Fields(nx, ny, nz, mpi_type=mpi_type) self.assertRaises(ValueError, Pbc, fields, axis)
import numpy as np import sys, os sys.path.append(os.path.expanduser('~')) from kemp.fdtd3d.cpu import QueueTask, Fields, Core, Pbc, IncidentDirect, GetFields nx, ny, nz = 160, 140, 32 tmax, tgap = 150, 10 # instances fields = Fields(QueueTask(), nx, ny, nz, 'e') Core(fields) Pbc(fields, 'xyz') """ tfunc = lambda tstep: np.sin(0.05 * tstep) IncidentDirect(fields, 'ez', (20, 0, 0), (20, -1, -1), tfunc) #IncidentDirect(fields, 'ez', (0, 20, 0), (-1, 20, -1), tfunc) getf = GetFields(fields, 'ez', (0, 0, 0.5), (-1, -1, 0.5)) 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()
# Target : CPU # Created : 2012-02-13 # Modified: import numpy as np import sys 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()
sys.path.append(os.path.expanduser('~')) from kemp.fdtd3d.cpu import QueueTask, Fields, Core, Pbc, IncidentDirect tmax = 150 tfunc = lambda tstep: np.sin(0.05 * tstep) # plot import matplotlib.pyplot as plt import matplotlib as mpl mpl.rc('image', interpolation='nearest', origin='lower') fig = plt.figure(figsize=(14, 8)) # z-axis nx, ny, nz = 180, 160, 2 fields = Fields(QueueTask(), nx, ny, nz) Core(fields) Pbc(fields, 'xyz') IncidentDirect(fields, 'ey', (20, 0, 0), (20, -1, -1), tfunc) IncidentDirect(fields, 'ex', (0, 20, 0), (-1, 20, -1), tfunc) for tstep in xrange(1, tmax + 1): fields.update_e() fields.update_h() fields.enqueue_barrier() ax1 = fig.add_subplot(2, 3, 1) ax1.imshow(fields.get('ey')[:, :, nz / 2].T, vmin=-1.1, vmax=1.1) ax1.set_title('%s, ey[20,:,:]' % repr(fields.ns)) ax1.set_xlabel('x') ax1.set_ylabel('y')
import numpy as np import sys, os sys.path.append(os.path.expanduser('~')) from kemp.fdtd3d.cpu import Fields, Core, Pbc, IncidentDirect, GetFields #nx, ny, nz = 160, 140, 32 #nx, ny, nz = 512, 512, 512 # 4608 MB #nx, ny, nz = 480, 480, 480 # 3796 MB nx, ny, nz = 256, 256, 256 # 576 MB #nx, ny, nz = 128, 128, 128 # 72 MB tmax, tgap = 1000, 10 # instances fields = Fields(nx, ny, nz, coeff_use='e', precision_float='single') Core(fields) print 'ns_pitch', fields.ns_pitch print 'nbytes (MB)', nx * ny * nz * 9 * 4. / (1024**2) ''' 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
import sys, os sys.path.append( os.path.expanduser('~') ) from kemp.fdtd3d.cpu import QueueTask, Fields, Core, Pbc, IncidentDirect, GetFields try: ny = int(sys.argv[1]) except: ny = 256 nx, nz = 2, 256 tmax, tgap = 1000, 10 # instances fields = Fields(QueueTask(), nx, ny, nz, coeff_use='e', precision_float='single', use_cpu_core=1) Core(fields) fields2 = Fields(QueueTask(), nx, ny, nz, coeff_use='e', precision_float='single', use_cpu_core=1) Core(fields2) #print 'ns_pitch', fields.ns_pitch #print 'nbytes (MB)', nx*ny*nz * 9 * 4. / (1024**2) ''' 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('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 if __name__ == '__main__': nx, ny, nz = 200, 220, 256 gpu_id = 0 fdtd = Fields(nx, ny, nz, coeff_use='') print('-' * 47 + '\nTest GetFields') testget = TestGetFields(fdtd, nx, ny, nz) testget.test() testget.test_boundary() print('-' * 47 + '\nTest SetFields') testset = TestSetFields(fdtd, nx, ny, nz) testset.test() testset.test_boundary()
sys.path.append(os.path.expanduser('~')) from kemp.fdtd3d.cpu import QueueTask, Fields, Core, Pbc, IncidentDirect, GetFields try: ny = int(sys.argv[1]) except: ny = 256 nx, nz = 2, 256 tmax, tgap = 1000, 10 # instances fields = Fields(QueueTask(), nx, ny, nz, coeff_use='e', precision_float='single', use_cpu_core=1) Core(fields) fields2 = Fields(QueueTask(), nx, ny, nz, coeff_use='e', precision_float='single', use_cpu_core=1) Core(fields2) #print 'ns_pitch', fields.ns_pitch #print 'nbytes (MB)', nx*ny*nz * 9 * 4. / (1024**2)
import numpy as np import sys, os 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
sys.path.append(os.path.expanduser('~')) from kemp.fdtd3d.cpu import QueueTask, Fields, Core, Pbc, IncidentDirect, GetFields #nx, ny, nz = 160, 140, 32 #nx, ny, nz = 512, 512, 512 # 4608 MB #nx, ny, nz = 480, 480, 480 # 3796 MB nx, ny, nz = 3, 256, 256 # 576 MB #nx, ny, nz = 128, 128, 128 # 72 MB tmax, tgap = 1000, 10 # instances qtask = QueueTask() fields = Fields(qtask, nx, ny, nz, coeff_use='e', precision_float='single', use_cpu_core=0) Core(fields) print 'ns_pitch', fields.ns_pitch print 'nbytes (MB)', nx * ny * nz * 9 * 4. / (1024**2) ''' 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))
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))
def runTest(self): ufunc, nx, ny, nz, coeff_use, precision_float, use_cpu_core, split, tmax = self.args fields = Fields(nx, ny, nz, coeff_use, precision_float, use_cpu_core) core = Core(fields) strf_list = ['ex', 'ey', 'ez', 'hx', 'hy', 'hz'] slice_xyz = [slice(None, None), slice(None, None), fields.slice_z] # allocations ns = fields.ns dtype = fields.dtype ehs = common_update.generate_random_ehs(nx, ny, nz, dtype, ufunc) fields.set_ehs(*ehs) ces, chs = common_update.generate_random_cs(coeff_use, nx, ny, nz, dtype) if 'e' in coeff_use: fields.set_ces(*ces) if 'h' in coeff_use: fields.set_chs(*chs) # update if ufunc == 'e': for tstep in xrange(0, tmax): fields.update_e() common_update.update_e(ehs, ces) fields.enqueue_barrier() for strf, eh in zip(strf_list, ehs)[:3]: norm = np.linalg.norm(eh - fields.get(strf)[slice_xyz]) max_diff = np.abs(eh - fields.get(strf)[slice_xyz]).max() self.assertEqual( norm, 0, '%s, %s, %g, %g' % (self.args, strf, norm, max_diff)) if fields.pad != 0: if strf == 'ez': norm2 = np.linalg.norm( fields.get(strf)[:, :, -fields.pad:]) else: norm2 = np.linalg.norm( fields.get(strf)[:, :, -fields.pad - 1:]) self.assertEqual( norm2, 0, '%s, %s, %g, padding' % (self.args, strf, norm2)) elif ufunc == 'h': for tstep in xrange(0, tmax): fields.update_h() common_update.update_h(ehs, chs) fields.enqueue_barrier() for strf, eh in zip(strf_list, ehs)[3:]: norm = np.linalg.norm(eh - fields.get(strf)[slice_xyz]) max_diff = np.abs(eh - fields.get(strf)[slice_xyz]).max() self.assertEqual(norm, 0, '%s, %s, %g, %g' % \ (self.args, strf, norm, max_diff) ) if fields.pad != 0: if strf == 'hz': norm2 = np.linalg.norm( fields.get(strf)[:, :, -fields.pad:]) else: norm2 = np.linalg.norm( fields.get(strf)[:, :, -fields.pad:]) self.assertEqual( norm2, 0, '%s, %s, %g, padding' % (self.args, strf, norm2))
#!/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)
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))
def runTest(self): ufunc, nx, ny, nz, coeff_use, precision_float, use_cpu_core, split, tmax = self.args fields = Fields(nx, ny, nz, coeff_use, precision_float, use_cpu_core) core = Core(fields) strf_list = ['ex', 'ey', 'ez', 'hx', 'hy', 'hz'] slice_xyz = [slice(None, None), slice(None, None), fields.slice_z] # allocations ns = fields.ns dtype = fields.dtype ehs = common_update.generate_random_ehs(nx, ny, nz, dtype, ufunc) fields.set_ehs(*ehs) ces, chs = common_update.generate_random_cs(coeff_use, nx, ny, nz, dtype) if 'e' in coeff_use: fields.set_ces(*ces) if 'h' in coeff_use: fields.set_chs(*chs) # update if ufunc == 'e': for tstep in xrange(0, tmax): fields.update_e() common_update.update_e(ehs, ces) fields.enqueue_barrier() for strf, eh in zip(strf_list, ehs)[:3]: norm = np.linalg.norm(eh - fields.get(strf)[slice_xyz]) max_diff = np.abs(eh - fields.get(strf)[slice_xyz]).max() self.assertEqual(norm, 0, '%s, %s, %g, %g' % (self.args, strf, norm, max_diff) ) if fields.pad != 0: if strf == 'ez': norm2 = np.linalg.norm(fields.get(strf)[:,:,-fields.pad:]) else: norm2 = np.linalg.norm(fields.get(strf)[:,:,-fields.pad-1:]) self.assertEqual(norm2, 0, '%s, %s, %g, padding' % (self.args, strf, norm2) ) elif ufunc == 'h': for tstep in xrange(0, tmax): fields.update_h() common_update.update_h(ehs, chs) fields.enqueue_barrier() for strf, eh in zip(strf_list, ehs)[3:]: norm = np.linalg.norm(eh - fields.get(strf)[slice_xyz]) max_diff = np.abs(eh - fields.get(strf)[slice_xyz]).max() self.assertEqual(norm, 0, '%s, %s, %g, %g' % \ (self.args, strf, norm, max_diff) ) if fields.pad != 0: if strf == 'hz': norm2 = np.linalg.norm(fields.get(strf)[:,:,-fields.pad:]) else: norm2 = np.linalg.norm(fields.get(strf)[:,:,-fields.pad:]) self.assertEqual(norm2, 0, '%s, %s, %g, padding' % (self.args, strf, norm2) )
tmax = 150 tfunc = lambda tstep: np.sin(0.05 * tstep) # plot import matplotlib.pyplot as plt import matplotlib as mpl mpl.rc('image', interpolation='nearest', origin='lower') plt.ion() fig = plt.figure(figsize=(14,8)) # z-axis nx, ny, nz = 180, 160, 2 fields = Fields(nx, ny, nz) Core(fields) Pbc(fields, 'xyz') IncidentDirect(fields, 'ey', (20, 0, 0), (20, ny-1, nz-1), tfunc) IncidentDirect(fields, 'ex', (0, 20, 0), (nx-1, 20, nz-1), tfunc) for tstep in xrange(1, tmax+1): fields.update_e() fields.update_h() fields.enqueue_barrier() ax1 = fig.add_subplot(2, 3, 1) ax1.imshow(fields.get('ey')[:,:,nz/2].T, vmin=-1.1, vmax=1.1) ax1.set_title('%s, ey[20,:,:]' % repr(fields.ns)) ax1.set_xlabel('x') ax1.set_ylabel('y')
#!/usr/bin/env python import sys sys.path.append('/home/kifang') from kemp.fdtd3d.common_cpu import QueueTask, LockQueueTask from kemp.fdtd3d.cpu import Fields, DirectSrc, GetFields import numpy as np nx, ny, nz = 240, 320, 320 tmax, tgap = 200, 1 qtask = QueueTask() 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