def _get_rhov_e(self): rhox_e, rhoy_e, rhoz_e = self['rhovx_e'], self['rhovy_e'], self[ 'rhovz_e'] opts = dict(_force_layout=self.force_vector_layout) return field.scalar_fields_to_vector([rhox_e, rhoy_e, rhoz_e], name="rhov_e", **opts)
def main(): gsize = (2, 2, 2) x1 = -10.0; x2 = -5.0 # pylint: disable=C0321 y1 = -5.0; y2 = 5.0 # pylint: disable=C0321 z1 = -5.0; z2 = 5.0 # pylint: disable=C0321 vol = seed.Volume((z1, y1, x1), (z2, y2, x2), gsize) f3d = readers.load_file("/Users/kmaynard/dev/work/t1/t1.3df.004320.xdmf") fld_bx = f3d["bx"] fld_by = f3d["by"] fld_bz = f3d["bz"] B = field.scalar_fields_to_vector([fld_bx, fld_by, fld_bz], name="B_cc", _force_layout=field.LAYOUT_INTERLACED) topo_arr = np.empty(gsize, order='C', dtype='int') lines, topo = None, None t0 = time() cProfile.runctx("nsegs = py_get_topo(B, topo_arr, x1, x2, y1, y2, z1, z2)", globals(), locals(), "topo.prof") t1 = time() s = pstats.Stats("topo.prof") s.strip_dirs().sort_stats("tottime").print_stats() nsegs = py_get_topo(B, topo_arr, x1, x2, y1, y2, z1, z2) t = t1 - t0 print(topo_arr) # print("numba time: {0}s, {1}s/seg".format(t, t / nsegs)) print("numba time: {0}s".format(t))
def _get_rhov_i(self): rhox_i, rhoy_i, rhoz_i = self['rhovx_i'], self['rhovy_i'], self[ 'rhovz_i'] opts = dict(_force_layout=self.force_vector_layout) return field.scalar_fields_to_vector([rhox_i, rhoy_i, rhoz_i], name="rhov_i", **opts)
def _assemble_vector(self, base_name, comp_names="xyz", forget_source=True, **kwargs): opts = dict(forget_source=forget_source, **kwargs) if len(comp_names) == 3: vx = self[base_name + comp_names[0]] vy = self[base_name + comp_names[1]] vz = self[base_name + comp_names[2]] v = field.scalar_fields_to_vector([vx, vy, vz], name=base_name, **opts) else: comps = [self[base_name + c] for c in comp_names] v = field.scalar_fields_to_vector(comps, name=base_name, **opts) for comp in comps: comp.unload() return v
def main(): parser = argparse.ArgumentParser(description="Test xdmf") parser.add_argument("--show", "--plot", action="store_true") parser.add_argument('file', nargs="?", default=None) args = vutil.common_argparse(parser) # f3d = readers.load_file(_viscid_root + '/../../sample/sample.3df.xdmf') # b3d = f3d['b'] # bx, by, bz = b3d.component_fields() # pylint: disable=W0612 if args.file is None: args.file = "/Users/kmaynard/dev/work/cen4000/cen4000.3d.xdmf" f3d = readers.load_file(args.file) bx = f3d["bx"] by = f3d["by"] bz = f3d["bz"] B = field.scalar_fields_to_vector([bx, by, bz], name="B_cc", _force_layout=field.LAYOUT_INTERLACED) t0 = time() lines_single, topo = trace_cython(B, nr_procs=1) t1 = time() topo_single = vol.wrap_field(topo, name="CyTopo1") print("single proc:", t1 - t0, "s") nr_procs_list = np.array([1, 2, 3]) # nr_procs_list = np.array([1, 2, 3, 4, 5, 6, 7, 8]) times = np.empty_like(nr_procs_list, dtype="float") print("always parallel overhead now...") for i, nr_procs in enumerate(nr_procs_list): t0 = time() lines, topo = trace_cython(B, nr_procs=nr_procs, force_subprocess=True) t1 = time() fld = vol.wrap_field(topo, name="CyTopo") same_topo = (fld.data == topo_single.data).all() same_lines = True for j, line in enumerate(lines): same_lines = same_lines and (line == lines_single[j]).all() print("nr_procs:", nr_procs, "time:", t1 - t0, "s", "same topo:", same_topo, "same lines:", same_lines) times[i] = t1 - t0 plt.plot(nr_procs_list, times, 'k^') plt.plot(nr_procs_list, times[0] / nr_procs_list, 'b--') plt.xscale("log") plt.yscale("log") plt.show() if False: cmap = plt.get_cmap('spectral') levels = [4, 5, 6, 7, 8, 13, 14, 16, 17] norm = BoundaryNorm(levels, cmap.N) mpl.plot(topo_flds[-1], "y=0", cmap=cmap, norm=norm, show=False) #mpl.plot_streamlines2d(lines[::5], "y", topology=topo[::5], show=False) #mpl.plot_streamlines(lines, topology=topo, show=False) mpl.mplshow()
def trace_numba(fld_bx, fld_by, fld_bz): B = field.scalar_fields_to_vector([fld_bx, fld_by, fld_bz], name="B_cc", _force_layout=field.LAYOUT_INTERLACED) topo_arr = np.empty(gsize, order='C', dtype='int') lines, topo = None, None t0 = time() nsegs = topo_numba.get_topo(B, topo_arr, x1, x2, y1, y2, z1, z2) t1 = time() topo_fld = vol.wrap_field(topo, name="CyTopo") return t1 - t0, nsegs, lines, topo_fld
def _assemble_vector(self, base_name, comp_names="xyz", suffix="", forget_source=False, **kwargs): opts = dict(forget_source=forget_source, **kwargs) # caching behavior depends on self.longterm_field_caches comps = [self[base_name + c + suffix] for c in comp_names] return field.scalar_fields_to_vector(comps, name=base_name, **opts)
def trace_cython(fld_bx, fld_by, fld_bz): # print("Cython...") B = field.scalar_fields_to_vector([fld_bx, fld_by, fld_bz], name="B_cc", _force_layout=field.LAYOUT_INTERLACED) t0 = time() lines, topo = None, None lines, topo = streamline.streamlines(B, vol, ds0=0.02, ibound=3.7, maxit=5000, output=streamline.OUTPUT_BOTH, method=streamline.EULER1, tol_lo=0.005, tol_hi=0.1, fac_refine=0.75, fac_coarsen=1.5) t1 = time() topo_fld = vol.wrap_field(topo, name="CyTopo") # cmap = plt.get_cmap('spectral') # levels = [4, 5, 6, 7, 8, 13, 14, 16, 17] # norm = BoundaryNorm(levels, cmap.N) # mpl.plot(topo_fld, "y=0", cmap=cmap, norm=norm, show=False) # # mpl.plot_streamlines2d(lines[::5], "y", topology=topo[::5], show=False) # # mpl.plot_streamlines(lines, topology=topo, show=False) # mpl.mplshow() # topo_src = mvi.add_field(topo_fld, center='node') # mvi.plot_lines(mlab.pipeline, lines[::5], topo[::5], opacity=0.8, # tube_radius=0.02) # mvi.plod_earth_3d(mlab.pipeline) # mlab.show() nsegs = 1 # keep from divding by 0 is no streamlines if lines is not None: nsegs = 0 for line in lines: nsegs += len(line[0]) t = t1 - t0 print("total segments calculated: ", nsegs) print("time: {0:.4}s ... {1:.4}s/segment".format(t, t / float(nsegs))) return lines, topo_fld
def _get_e(self): ex, ey, ez = self['ex'], self['ey'], self['ez'] opts = dict(_force_layout=self.force_vector_layout) return field.scalar_fields_to_vector([ex, ey, ez], name="E", **opts)
def _get_j_e(self): jx_e, jy_e, jz_e = self['jx_e'], self['jy_e'], self['jz_e'] opts = dict(_force_layout=self.force_vector_layout) return field.scalar_fields_to_vector([jx_e, jy_e, jz_e], name="J_e", **opts)
def _get_v(self): vx, vy, vz = self['vx'], self['vy'], self['vz'] opts = dict(_force_layout=self.force_vector_layout) return field.scalar_fields_to_vector([vx, vy, vz], name="V", **opts)
def _get_b(self): bx, by, bz = self['bx'], self['by'], self['bz'] opts = dict(_force_layout=self.force_vector_layout) return field.scalar_fields_to_vector([bx, by, bz], name="B", **opts)
def main(): parser = argparse.ArgumentParser(description="Streamline a PSC file") parser.add_argument("-t", default="2000", help="which time to plot (finds closest)") parser.add_argument('infile', nargs=1, help='input file') args = vutil.common_argparse(parser) # f = readers.load_file("pfd.020000.xdmf") # ... or ... # using this way of loading files, one probably wants just to give # pfd.xdmf to the command line f = readers.load_file(args.infile[0]) f.activate_time(args.t) jz = f["jz"] # recreate hx as a field of 0 to keep streamlines from moving in # that direction hx = field.zeros_like(jz, name="hx") h1 = field.scalar_fields_to_vector([hx, f["hy"], f["hz"]], name="H", _force_layout="Interlaced", forget_source=True) e = field.scalar_fields_to_vector([f["ex"], f["ey"], f["ez"]], name="E", _force_layout="Interlaced", forget_source=True) # plot magnetic fields, just a sanity check # ax1 = plt.subplot(211) # mpl.plot(f["hy"], flip_plot=True) # ax2 = plt.subplot(212, sharex=ax1, sharey=ax1) # mpl.plot(f["hz"], flip_plot=True) # mpl.mplshow() # make a line of 30 seeds straight along the z axis (x, y, z ordered) seeds1 = seed.Line((0.0, 0.0, 2.0), (1022.0, 0.0, 0.0), 60) # set outer boundary limits for streamlines ds = 0.005 # spatial step along the stream line curve obound0 = np.array([1, -128, -1000], dtype=h1.dtype) obound1 = np.array([1023, 128, 1000], dtype=h1.dtype) # calc the streamlines lines1, topo1 = streamline.streamlines(h1, seeds1, ds0=ds, maxit=200000, obound0=obound0, obound1=obound1, ibound=0.0) # run with -v to see this logger.info("Topology flags: {0}".format(topo1)) # rotate plot puts the z axis along the horizontal flip_plot = True mpl.plot(jz, flip_plot=flip_plot, plot_opts="lin_-.05_.05") # mpl.plot_streamlines2d(lines1, "x", flip_plot=flip_plot, color='k') plt.xlim([0, 1024]) plt.ylim([-128, 128]) plt.show() # interpolate e onto each point of the first field line of lines1 e1 = cycalc.interp_trilin(e, seed.Point(lines1[0])) print(e1.shape, lines1[0].shape) plt.clf() plt.plot(np.linspace(0, ds * e1.shape[0], e1.shape[0]), e1[:, 0]) plt.xlabel("length along field line") plt.ylabel("Ex") plt.show() return 0
def _get_j_i(self): jx_i, jy_i, jz_i = self['jx_i'], self['jy_i'], self['jz_i'] opts = dict(_force_layout=self.force_vector_layout) return field.scalar_fields_to_vector([jx_i, jy_i, jz_i], name="J_i", **opts)