def runTest(self): nx, ny, nz, str_f, pt0, pt1, is_array = self.args slices = common.slices_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 gpu_devices = common_gpu.gpu_device_list(print_info=False) context = cl.Context(gpu_devices) mainf_list = [cpu.Fields(nx, ny, nz)] mainf_list += [gpu.Fields(context, device, nx, ny, nz) \ for device in gpu_devices] nodef = Fields(mainf_list) dtype = nodef.dtype anx = nodef.accum_nx_list tfunc = lambda tstep: np.sin(0.03 * tstep) incident = IncidentDirect(nodef, str_f, pt0, pt1, tfunc, value) # allocations for verify eh = np.zeros(nodef.ns, dtype) getf = GetFields(nodef, str_f, pt0, pt1) # verify eh[slices] = dtype(value) * dtype(tfunc(1)) e_or_h = str_f[0] nodef.update_e() nodef.update_h() getf.wait() original = eh[slices] 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, is_array = self.args slices = common.slices_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 gpu_devices = common_gpu.gpu_device_list(print_info=False) context = cl.Context(gpu_devices) mainf_list = [ cpu.Fields(nx, ny, nz) ] mainf_list += [gpu.Fields(context, device, nx, ny, nz) \ for device in gpu_devices] nodef = Fields(mainf_list) dtype = nodef.dtype anx = nodef.accum_nx_list tfunc = lambda tstep: np.sin(0.03*tstep) incident = IncidentDirect(nodef, str_f, pt0, pt1, tfunc, value) # allocations for verify eh = np.zeros(nodef.ns, dtype) getf = GetFields(nodef, str_f, pt0, pt1) # verify eh[slices] = dtype(value) * dtype(tfunc(1)) e_or_h = str_f[0] nodef.update_e() nodef.update_h() getf.wait() original = eh[slices] copy = getf.get_fields(str_f) norm = np.linalg.norm(original - copy) self.assertEqual(norm, 0, '%s, %g' % (self.args, norm))
# 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() from datetime import datetime from time import time t0 = datetime.now() t00 = time() # main loop gtmp = gpu.GetFields(gpuf, 'ez', (0, 0, 0), (0, 0, 0)) for tstep in xrange(1, tmax+1): fields.update_e() fields.update_h() if tstep % tgap == 0 and rank == 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() gtmp.get_event().wait() if rank == 0: dt = time() - t00 print dt
# plot import matplotlib.pyplot as plt plt.ion() fig = plt.figure(figsize=(12,8)) for i in fields.accum_nx_list[1:]: plt.plot((i,i), (0,ny), color='k', linewidth=2) 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): 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.wait() imag.set_array( getf.get_fields().T ) #plt.savefig('./png/%.6d.png' % tstep) plt.draw() plt.show() print('\n[%s] %d/%d (%d %%)' % (datetime.now() - t0, tstep, tmax, float(tstep)/tmax*100)) print('')