def runTest(self): nx, ny, nz, str_f, pt0, pt1, is_array = 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 gpu_devices = common_gpu.gpu_device_list(print_info=False) context = cl.Context(gpu_devices) mainf_list = [gpu.Fields(context, device, nx, ny, nz) \ for device in gpu_devices] mainf_list.append( cpu.Fields(nx, ny, nz) ) nodef = NodeFields(mainf_list) dtype = nodef.dtype anx = nodef.accum_nx_list tfunc = lambda tstep: np.sin(0.03*tstep) incident = NodeDirectIncident(nodef, str_f, pt0, pt1, tfunc, value) # allocations for verify eh = np.zeros(nodef.ns, dtype) getf = NodeGetFields(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.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 gpu_devices = common_gpu.gpu_device_list(print_info=False) context = cl.Context(gpu_devices) mainf_list = [gpu.Fields(context, device, nx, ny, nz) \ for device in gpu_devices] mainf_list.append(cpu.Fields(nx, ny, nz)) nodef = NodeFields(mainf_list) dtype = nodef.dtype anx = nodef.accum_nx_list tfunc = lambda tstep: np.sin(0.03 * tstep) incident = NodeDirectIncident(nodef, str_f, pt0, pt1, tfunc, value) # allocations for verify eh = np.zeros(nodef.ns, dtype) getf = NodeGetFields(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))
plt.ion() for anx in nodef.accum_nx_list[1:]: plt.plot((anx, anx), (0, ny), color='w', linewidth=0.2) imag = plt.imshow(np.zeros((nodef.nx, ny), nodef.dtype).T, cmap=plt.cm.hot, origin='lower', vmin=0, vmax=2.) plt.colorbar() # main loop from datetime import datetime t0 = datetime.now() for tstep in xrange(1, tmax+1): nodef.update_e() pbc_x.update_e() exchange.update_e() nodef.update_h() pbc_x.update_h() exchange.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( np.abs(getf.get_fields().T) ) #plt.savefig('./png/%.6d.png' % tstep) plt.draw() plt.show() #getf.wait() #print('\n[%s] %d/%d (%d %%)' % (datetime.now() - t0, tstep, tmax, float(tstep)/tmax*100))