def benchmark_interp_trilin(precompile=True, profile=True): which = "interp_trilin" print(which) print('-' * len(which)) f0, seeds = make_scalar_fld() print("Timing", which) ft_stats, cy_stats = dict(), dict() retFT = viscid.timeit(fort_interp_trilin, f0, seeds, timeit_repeat=10, timeit_stats=ft_stats) retCY = viscid.timeit(viscid.interp_trilin, f0, seeds, timeit_repeat=10, timeit_stats=cy_stats) print_seedup("Cython", cy_stats['min'], "Fortran", ft_stats['min'], prefix="@ ") assert np.allclose(retCY.data, retFT.data)
def _main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--prof", action="store_true") parser.add_argument("--show", "--plot", action="store_true") args = vutil.common_argparse(parser) dtype = 'float64' # use 512 512 256 to inspect memory related things x = np.array(np.linspace(-0.5, 0.5, 256), dtype=dtype) y = np.array(np.linspace(-0.5, 0.5, 256), dtype=dtype) z = np.array(np.linspace(-0.5, 0.5, 64), dtype=dtype) v = viscid.empty([x, y, z], name="V", nr_comps=3, center="cell", layout="interlaced") exact_cc = viscid.empty([x, y, z], name="exact_cc", center='cell') Xcc, Ycc, Zcc = exact_cc.get_crds_cc(shaped=True) # pylint: disable=W0612 if HAS_NUMEXPR: v['x'] = ne.evaluate("(sin(Xcc))") # + Zcc v['y'] = ne.evaluate("(cos(Ycc))") # + Xcc# + Zcc v['z'] = ne.evaluate("-((sin(Zcc)))") # + Xcc# + Ycc exact_cc[:, :, :] = ne.evaluate("cos(Xcc) - sin(Ycc) - cos(Zcc)") else: v['x'] = (np.sin(Xcc)) # + Zcc v['y'] = (np.cos(Ycc)) # + Xcc# + Zcc v['z'] = -((np.sin(Zcc))) # + Xcc# + Ycc exact_cc[:, :, :] = np.cos(Xcc) - np.sin(Ycc) - np.cos(Zcc) if args.prof: print("Without boundaries") viscid.timeit(viscid.div, v, bnd=False, timeit_repeat=10, timeit_print_stats=True) print("With boundaries") viscid.timeit(viscid.div, v, bnd=True, timeit_repeat=10, timeit_print_stats=True) logger.info("node centered tests") v_nc = v.as_centered('node') exact_nc = viscid.empty_like(v_nc['x']) X, Y, Z = exact_nc.get_crds_nc(shaped=True) # pylint: disable=W0612 if HAS_NUMEXPR: exact_nc[:, :, :] = ne.evaluate("cos(X) - sin(Y) - cos(Z)") else: exact_nc[:, :, :] = np.cos(X) - np.sin(Y) - np.cos(Z) # FIXME: why is the error so much larger here? run_div_test(v_nc, exact_nc, title='Node Centered', show=args.show, ignore_inexact=True) logger.info("cell centered tests") v_cc = v_nc.as_centered('cell') run_div_test(v_cc, exact_cc, title="Cell Centered", show=args.show) return 0
def lines_and_lsps(B, seeds, cotr=None, **kwargs): """Return a list of streamlines and the l-shell,phi for each point""" tstats = dict() lines, _ = viscid.timeit(viscid.calc_streamlines, B, seeds, timeit_quiet=True, timeit_stats=tstats, **kwargs) walltime = tstats['max'] # lsrlps = [viscid.xyz2lsrlp(line, cotr=cotr, crd_system=B) for line in lines] lsrlps = [viscid.xyz2lsrlp(line, cotr=cotr, crd_system=B) for line in lines] lsps = [np.array(lsrlp[(0, 3), :]) for lsrlp in lsrlps] return lines, lsps, walltime
def benchmark_interp_nearest(precompile=True, profile=True): which = "interp_nearest" print(which) print('-' * len(which)) f0, seeds = make_scalar_fld() print("Timing", which) cy_stats = dict() retCY = viscid.timeit(viscid.interp_nearest, f0, seeds, timeit_repeat=10, timeit_stats=cy_stats) print("Cython Min:", cy_stats['min']) assert np.all(f0.data == retCY.data)
def _main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--prof", action="store_true") parser.add_argument("--show", "--plot", action="store_true") args = vutil.common_argparse(parser) b = viscid.make_dipole(l=(-5, -5, -5), h=(5, 5, 5), n=(255, 255, 127), m=(0, 0, -1)) b2 = np.sum(b * b, axis=b.nr_comp) if args.prof: print("Without boundaries") viscid.timeit(viscid.grad, b2, bnd=False, timeit_repeat=10, timeit_print_stats=True) print("With boundaries") viscid.timeit(viscid.grad, b2, bnd=True, timeit_repeat=10, timeit_print_stats=True) grad_b2 = viscid.grad(b2) grad_b2.pretty_name = r"$\nabla$ B$^2$" conv = viscid.convective_deriv(b) conv.pretty_name = r"(B $\cdot \nabla$) B" _ = plt.figure(figsize=(9, 4.2)) ax1 = vlt.subplot(231) vlt.plot(b2['z=0f'], logscale=True) vlt.plot(b2['z=0f'], logscale=True, style='contour', levels=10, colors='grey') # vlt.plot2d_quiver(viscid.normalize(b['z=0f']), step=16, pivot='mid') ax2 = vlt.subplot(234) vlt.plot(b2['y=0f'], logscale=True) vlt.plot(b2['y=0f'], logscale=True, style='contour', levels=10, colors='grey') vlt.plot2d_quiver(viscid.normalize(b['y=0f'], preferred='numpy'), step=16, pivot='mid') vlt.subplot(232, sharex=ax1, sharey=ax1) vlt.plot(1e-4 + viscid.magnitude(grad_b2['z=0f']), logscale=True) vlt.plot(1e-4 + viscid.magnitude(grad_b2['z=0f']), logscale=True, style='contour', levels=10, colors='grey') vlt.plot2d_quiver(viscid.normalize(grad_b2['z=0f']), step=16, pivot='mid') vlt.subplot(235, sharex=ax2, sharey=ax2) vlt.plot(1e-4 + viscid.magnitude(grad_b2['y=0f']), logscale=True) vlt.plot(1e-4 + viscid.magnitude(grad_b2['y=0f']), logscale=True, style='contour', levels=10, colors='grey') vlt.plot2d_quiver(viscid.normalize(grad_b2['y=0f']), step=16, pivot='mid') vlt.subplot(233, sharex=ax1, sharey=ax1) vlt.plot(viscid.magnitude(conv['z=0f']), logscale=True) vlt.plot(viscid.magnitude(conv['z=0f']), logscale=True, style='contour', levels=10, colors='grey') vlt.plot2d_quiver(viscid.normalize(conv['z=0f']), step=16, pivot='mid') vlt.subplot(236, sharex=ax2, sharey=ax2) vlt.plot(viscid.magnitude(conv['y=0f']), logscale=True) vlt.plot(viscid.magnitude(conv['y=0f']), logscale=True, style='contour', levels=10, colors='grey') vlt.plot2d_quiver(viscid.normalize(conv['y=0f']), step=16, pivot='mid') vlt.auto_adjust_subplots() plt.savefig(next_plot_fname(__file__)) if args.show: vlt.show() return 0
def _main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--prof", action="store_true") parser.add_argument("--show", "--plot", action="store_true") args = vutil.common_argparse(parser) b = viscid.make_dipole(l=(-5, -5, -5), h=(5, 5, 5), n=(255, 255, 127), m=(0, 0, -1)) b2 = np.sum(b * b, axis=b.nr_comp) if args.prof: print("Without boundaries") viscid.timeit(viscid.grad, b2, bnd=False, timeit_repeat=10, timeit_print_stats=True) print("With boundaries") viscid.timeit(viscid.grad, b2, bnd=True, timeit_repeat=10, timeit_print_stats=True) grad_b2 = viscid.grad(b2) grad_b2.pretty_name = r"$\nabla$ B$^2$" conv = viscid.convective_deriv(b) conv.pretty_name = r"(B $\cdot \nabla$) B" _ = plt.figure(figsize=(9, 4.2)) ax1 = vlt.subplot(231) vlt.plot(b2['z=0j'], logscale=True) vlt.plot(b2['z=0j'], logscale=True, style='contour', levels=10, colors='grey') # vlt.plot2d_quiver(viscid.normalize(b['z=0j']), step=16, pivot='mid') ax2 = vlt.subplot(234) vlt.plot(b2['y=0j'], logscale=True) vlt.plot(b2['y=0j'], logscale=True, style='contour', levels=10, colors='grey') vlt.plot2d_quiver(viscid.normalize(b['y=0j'], preferred='numpy'), step=16, pivot='mid') vlt.subplot(232, sharex=ax1, sharey=ax1) vlt.plot(1e-4 + viscid.magnitude(grad_b2['z=0j']), logscale=True) vlt.plot(1e-4 + viscid.magnitude(grad_b2['z=0j']), logscale=True, style='contour', levels=10, colors='grey') vlt.plot2d_quiver(viscid.normalize(grad_b2['z=0j']), step=16, pivot='mid') vlt.subplot(235, sharex=ax2, sharey=ax2) vlt.plot(1e-4 + viscid.magnitude(grad_b2['y=0j']), logscale=True) vlt.plot(1e-4 + viscid.magnitude(grad_b2['y=0j']), logscale=True, style='contour', levels=10, colors='grey') vlt.plot2d_quiver(viscid.normalize(grad_b2['y=0j']), step=16, pivot='mid') vlt.subplot(233, sharex=ax1, sharey=ax1) vlt.plot(viscid.magnitude(conv['z=0j']), logscale=True) vlt.plot(viscid.magnitude(conv['z=0j']), logscale=True, style='contour', levels=10, colors='grey') vlt.plot2d_quiver(viscid.normalize(conv['z=0j']), step=16, pivot='mid') vlt.subplot(236, sharex=ax2, sharey=ax2) vlt.plot(viscid.magnitude(conv['y=0j']), logscale=True) vlt.plot(viscid.magnitude(conv['y=0j']), logscale=True, style='contour', levels=10, colors='grey') vlt.plot2d_quiver(viscid.normalize(conv['y=0j']), step=16, pivot='mid') vlt.auto_adjust_subplots() plt.savefig(next_plot_fname(__file__)) if args.show: vlt.show() return 0
def benchmark_streamline(precompile=True, profile=True, scale=True, plot=True): which = "streamline" print(which) print('-' * len(which)) f0, seeds = make_vector_fld() print("Timing", which) sl_kwargs = dict(ibound=3.7, ds0=0.020) lines, _ = viscid.streamlines(f0, seeds, **sl_kwargs) nsegs_cython = np.sum([line.shape[1] for line in lines]) lines = None ft_stats, cy_stats = dict(), dict() bench_output_type = viscid.OUTPUT_TOPOLOGY sl_kwargs.update(output=bench_output_type) retFT = viscid.timeit(fort_topology, f0, seeds, timeit_repeat=6, timeit_stats=ft_stats) _, retCY = viscid.timeit(viscid.streamlines, f0, seeds, timeit_repeat=6, timeit_stats=cy_stats, **sl_kwargs) fort_per_seg = ft_stats['min'] / retFT.get_info('nsegs') cy_per_seg = cy_stats['min'] / nsegs_cython print("Segs Fortran", retFT.get_info('nsegs')) print("Segs Cython ", nsegs_cython) print("Fortran took {0:.3g} sec/seg".format(fort_per_seg)) print("Cython took {0:.3g} sec/seg".format(cy_per_seg)) print_seedup("Cython", cy_per_seg, "Fortran", fort_per_seg, prefix="@ ") if plot: from viscid.plot import mpl mpl.clf() mpl.subplot(121, projection='polar') mpl.plot(retCY, hemisphere='north') mpl.subplot(122, projection='polar') mpl.plot(retCY, hemisphere='south') mpl.show() if scale: thetas = np.logspace(np.log10(3), np.log10(144), 8).astype('i') cy_nsegs = [None] * len(thetas) fort_nsegs = [None] * len(thetas) cy_mintime = [None] * len(thetas) fort_mintime = [None] * len(thetas) for i, ntheta in enumerate(thetas): seeds = viscid.Sphere(r=10.0, ntheta=ntheta, nphi=32) _stats = dict() topo = viscid.timeit(fort_topology, f0, seeds, timeit_repeat=5, timeit_stats=_stats, timeit_quiet=True) fort_nsegs[i] = topo.get_info('nsegs') fort_mintime[i] = _stats['min'] _, topo = viscid.timeit(viscid.calc_streamlines, f0, seeds, ibound=3.7, ds0=0.020, output=bench_output_type, timeit_repeat=5, timeit_stats=_stats, timeit_quiet=True) lines, _ = viscid.streamlines(f0, seeds, ibound=3.7, ds0=0.020) cy_nsegs[i] = np.sum([line.shape[1] for line in lines]) cy_mintime[i] = _stats['min'] from viscid.plot import mpl mpl.clf() mpl.plt.plot(cy_nsegs, cy_mintime, label="Cython") mpl.plt.plot(fort_nsegs, fort_mintime, label="Fortran") mpl.plt.legend(loc=0) mpl.plt.xlabel('Number of segments calculated') mpl.plt.ylabel('time to calculate') mpl.show() mpl.clf() cy_tperseg = np.array(cy_mintime) / np.array(cy_nsegs) fort_tperseg = np.array(fort_mintime) / np.array(fort_nsegs) mpl.plt.plot(thetas, cy_tperseg / fort_tperseg, label="over cython") mpl.plt.xlabel('ntheta') mpl.plt.ylabel('Fortran Speedup') mpl.show()