def _main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--timeit", '-t', action="store_true") args = viscid.vutil.common_argparse(parser, default_verb=0) for nu in (False, True): viscid.logger.info("Test set, nonuniform = {0}".format(nu)) b = viscid.make_dipole(l=(-20, -6.4, -6.4), h=(20, 6.4, 6.4), n=(256, 128, 128), dtype='f4', nonuniform=nu) seed = viscid.Circle(p0=(0, 0, 0), pole=(0, 0, 1), r=5.5, n=int(1e4)) kwargs = dict(method='euler1', ibound=0.1) viscid.logger.info("Serial test...") l0, t0 = do(args.timeit, viscid.calc_streamlines, b, seed, nr_procs=1, threads=False, **kwargs) viscid.logger.info("Parallel test (processes)...") l1, t1 = do(args.timeit, viscid.calc_streamlines, b, seed, nr_procs=2, threads=False, **kwargs) viscid.logger.info("Parallel test (threads)...") l2, t2 = do(args.timeit, viscid.calc_streamlines, b, seed, nr_procs=2, threads=True, **kwargs) np.testing.assert_almost_equal(t0.data, t1.data) np.testing.assert_almost_equal(t0.data, t2.data) assert len(l0) > 0 assert len(l0) == len(l1) assert len(l0) == len(l2) for i in range(len(l0)): np.testing.assert_almost_equal(l0[i], l1[i]) np.testing.assert_almost_equal(l0[i], l2[i]) return 0
def check(): """Runtime check compiled modules""" import os import sys import numpy as np import viscid ret = 0 check_version() print() ##################################################### # run streamline calculation (checks cython modules) try: cotr = viscid.Cotr(dip_tilt=15.0, dip_gsm=21.0) # pylint: disable=not-callable m = cotr.get_dipole_moment(crd_system='gse') seeds = viscid.seed.Sphere((0.0, 0.0, 0.0), 2.0, pole=-m, ntheta=25, nphi=25, thetalim=(5, 90), philim=(5, 360), phi_endpoint=False) B = viscid.make_dipole(m=m, crd_system='gse', n=(32, 32, 32), l=(-25, -25, -25), h=(25, 25, 25), dtype='f8') lines, _ = viscid.calc_streamlines(B, seeds, ibound=1.0) for line in lines: if np.any(np.isnan(line)): raise ValueError("NaN in line") print("Cython module ran successfully") except Exception as e: print("Cython module has runtime errors.") print(str(e)) ret |= (1 << 0) print() #################################### # load a jrrle file (checks fortran) try: f3d = viscid.load_file( os.path.join(viscid.sample_dir, 'sample_jrrle.3df.*')) _ = np.array(f3d['pp'].data) print("Fortran module ran successfully") except Exception as e: print("Fortran module has runtime errors.") print(str(e)) ret |= (1 << 1) print() return ret
def _main(): parser = argparse.ArgumentParser() parser.add_argument("--notwo", dest="notwo", action="store_true") parser.add_argument("--nothree", dest="nothree", action="store_true") parser.add_argument("--show", "--plot", action="store_true") args = viscid.vutil.common_argparse(parser, default_verb=0) plot2d = not args.notwo plot3d = not args.nothree viscid.logger.info("Testing field lines on 2d field...") B = viscid.make_dipole(twod=True) line = viscid.seed.Line((0.2, 0.0, 0.0), (1.0, 0.0, 0.0), 10) obound0 = np.array([-4, -4, -4], dtype=B.data.dtype) obound1 = np.array([4, 4, 4], dtype=B.data.dtype) run_test( B, line, plot2d=plot2d, plot3d=plot3d, title="2D", show=args.show, ibound=0.07, obound0=obound0, obound1=obound1 ) viscid.logger.info("Testing field lines on 3d field...") B = viscid.make_dipole(m=[0.2, 0.3, -0.9]) sphere = viscid.seed.Sphere((0.0, 0.0, 0.0), 2.0, ntheta=20, nphi=10) obound0 = np.array([-4, -4, -4], dtype=B.data.dtype) obound1 = np.array([4, 4, 4], dtype=B.data.dtype) run_test( B, sphere, plot2d=plot2d, plot3d=plot3d, title="3D", show=args.show, ibound=0.07, obound0=obound0, obound1=obound1, method=viscid.RK12, )
def check(): """Runtime check compiled modules""" import os import sys import numpy as np import viscid ret = 0 check_version() print() ##################################################### # run streamline calculation (checks cython modules) try: cotr = viscid.Cotr(dip_tilt=15.0, dip_gsm=21.0) # pylint: disable=not-callable m = cotr.get_dipole_moment(crd_system='gse') seeds = viscid.seed.Sphere((0.0, 0.0, 0.0), 2.0, pole=-m, ntheta=25, nphi=25, thetalim=(5, 90), philim=(5, 360), phi_endpoint=False) B = viscid.make_dipole(m=m, crd_system='gse', n=(32, 32, 32), l=(-25, -25, -25), h=(25, 25, 25), dtype='f8') lines, _ = viscid.calc_streamlines(B, seeds, ibound=1.0) for line in lines: if np.any(np.isnan(line)): raise ValueError("NaN in line") print("Cython module ran successfully") except Exception as e: print("Cython module has runtime errors.") print(str(e)) ret |= (1 << 0) print() #################################### # load a jrrle file (checks fortran) try: f3d = viscid.load_file(os.path.join(viscid.sample_dir, 'sample_jrrle.3df.*')) _ = np.array(f3d['pp'].data) print("Fortran module ran successfully") except Exception as e: print("Fortran module has runtime errors.") print(str(e)) ret |= (1 << 1) print() return ret
def _main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--notwo", dest='notwo', action="store_true") parser.add_argument("--nothree", dest='nothree', action="store_true") parser.add_argument("--show", "--plot", action="store_true") args = viscid.vutil.common_argparse(parser, default_verb=0) plot2d = not args.notwo plot3d = not args.nothree # ################################################# # viscid.logger.info("Testing field lines on 2d field...") B = viscid.make_dipole(twod=True) line = viscid.seed.Line((0.2, 0.0, 0.0), (1.0, 0.0, 0.0), 10) obound0 = np.array([-4, -4, -4], dtype=B.data.dtype) obound1 = np.array([4, 4, 4], dtype=B.data.dtype) run_test(B, line, plot2d=plot2d, plot3d=plot3d, title='2D', show=args.show, ibound=0.07, obound0=obound0, obound1=obound1) ################################################# viscid.logger.info("Testing field lines on 3d field...") B = viscid.make_dipole(m=[0.2, 0.3, -0.9]) sphere = viscid.seed.Sphere((0.0, 0.0, 0.0), 2.0, ntheta=20, nphi=10) obound0 = np.array([-4, -4, -4], dtype=B.data.dtype) obound1 = np.array([4, 4, 4], dtype=B.data.dtype) run_test(B, sphere, plot2d=plot2d, plot3d=plot3d, title='3D', show=args.show, ibound=0.12, obound0=obound0, obound1=obound1, method=viscid.RK12) # The Remainder of this test makes sure higher order methods are indeed # more accurate than lower order methods... this could find a bug in # the integrators ################################################## # test accuracy of streamlines in an ideal dipole cotr = viscid.Cotr(dip_tilt=15.0, dip_gsm=21.0) # pylint: disable=not-callable m = cotr.get_dipole_moment(crd_system='gse') seeds = viscid.seed.Sphere((0.0, 0.0, 0.0), 2.0, pole=-m, ntheta=25, nphi=25, thetalim=(5, 90), philim=(5, 360), phi_endpoint=False) B = viscid.make_dipole(m=m, crd_system='gse', n=(256, 256, 256), l=(-25, -25, -25), h=(25, 25, 25), dtype='f8') seeds_xyz = seeds.get_points() # seeds_lsp = viscid.xyz2lsrlp(seeds_xyz, cotr=cotr, crd_system=B)[(0, 3), :] seeds_lsp = viscid.xyz2lsrlp(seeds_xyz, cotr=cotr, crd_system=B)[(0, 3), :] e1_lines, e1_lsps, t_e1 = lines_and_lsps(B, seeds, method='euler1', ibound=1.0, cotr=cotr) rk2_lines, rk2_lsps, t_rk2 = lines_and_lsps(B, seeds, method='rk2', ibound=1.0, cotr=cotr) rk4_lines, rk4_lsps, t_rk4 = lines_and_lsps(B, seeds, method='rk4', ibound=1.0, cotr=cotr) e1a_lines, e1a_lsps, t_e1a = lines_and_lsps(B, seeds, method='euler1a', ibound=1.0, cotr=cotr) rk12_lines, rk12_lsps, t_rk12 = lines_and_lsps(B, seeds, method='rk12', ibound=1.0, cotr=cotr) rk45_lines, rk45_lsps, t_rk45 = lines_and_lsps(B, seeds, method='rk45', ibound=1.0, cotr=cotr) def _calc_rel_diff(_lsp, _ideal_lsp, _d): _diffs = [] for _ilsp, _iideal in zip(_lsp, _ideal_lsp.T): _a = _ilsp[_d, :] _b = _iideal[_d] _diffs.append((_a - _b) / _b) return _diffs lshell_diff_e1 = _calc_rel_diff(e1_lsps, seeds_lsp, 0) phi_diff_e1 = _calc_rel_diff(e1_lsps, seeds_lsp, 1) lshell_diff_rk2 = _calc_rel_diff(rk2_lsps, seeds_lsp, 0) phi_diff_rk2 = _calc_rel_diff(rk2_lsps, seeds_lsp, 1) lshell_diff_rk4 = _calc_rel_diff(rk4_lsps, seeds_lsp, 0) phi_diff_rk4 = _calc_rel_diff(rk4_lsps, seeds_lsp, 1) lshell_diff_e1a = _calc_rel_diff(e1a_lsps, seeds_lsp, 0) phi_diff_e1a = _calc_rel_diff(e1a_lsps, seeds_lsp, 1) lshell_diff_rk12 = _calc_rel_diff(rk12_lsps, seeds_lsp, 0) phi_diff_rk12 = _calc_rel_diff(rk12_lsps, seeds_lsp, 1) lshell_diff_rk45 = _calc_rel_diff(rk45_lsps, seeds_lsp, 0) phi_diff_rk45 = _calc_rel_diff(rk45_lsps, seeds_lsp, 1) methods = [ 'Euler 1', 'Runge Kutta 2', 'Runge Kutta 4', 'Euler 1 Adaptive Step', 'Runge Kutta 12 Adaptive Step', 'Runge Kutta 45 Adaptive Step' ] wall_ts = [t_e1, t_rk2, t_rk4, t_e1a, t_rk12, t_rk45] all_lines = [ e1_lines, rk2_lines, rk4_lines, e1a_lines, rk12_lines, rk45_lines ] all_lshell_diffs = [ lshell_diff_e1, lshell_diff_rk2, lshell_diff_rk4, lshell_diff_e1a, lshell_diff_rk12, lshell_diff_rk45 ] lshell_diffs = [ np.abs(np.concatenate(lshell_diff_e1, axis=0)), np.abs(np.concatenate(lshell_diff_rk2, axis=0)), np.abs(np.concatenate(lshell_diff_rk4, axis=0)), np.abs(np.concatenate(lshell_diff_e1a, axis=0)), np.abs(np.concatenate(lshell_diff_rk12, axis=0)), np.abs(np.concatenate(lshell_diff_rk45, axis=0)) ] phi_diffs = [ np.abs(np.concatenate(phi_diff_e1, axis=0)), np.abs(np.concatenate(phi_diff_rk2, axis=0)), np.abs(np.concatenate(phi_diff_rk4, axis=0)), np.abs(np.concatenate(phi_diff_e1a, axis=0)), np.abs(np.concatenate(phi_diff_rk12, axis=0)), np.abs(np.concatenate(phi_diff_rk45, axis=0)) ] npts = [len(lsd) for lsd in lshell_diffs] lshell_75 = [np.percentile(lsdiff, 75) for lsdiff in lshell_diffs] # # 3D DEBUG PLOT:: for really getting under the covers # vlab.clf() # earth1 = viscid.seed.Sphere((0.0, 0.0, 0.0), 1.0, pole=-m, ntheta=60, nphi=120, # thetalim=(15, 165), philim=(0, 360)) # ls1 = viscid.xyz2lsrlp(earth1.get_points(), cotr=cotr, crd_system='gse')[0, :] # earth2 = viscid.seed.Sphere((0.0, 0.0, 0.0), 2.0, pole=-m, ntheta=60, nphi=120, # thetalim=(15, 165), philim=(0, 360)) # ls2 = viscid.xyz2lsrlp(earth2.get_points(), cotr=cotr, crd_system='gse')[0, :] # earth4 = viscid.seed.Sphere((0.0, 0.0, 0.0), 4.0, pole=-m, ntheta=60, nphi=120, # thetalim=(15, 165), philim=(0, 360)) # ls4 = viscid.xyz2lsrlp(earth4.get_points(), cotr=cotr, crd_system='gse')[0, :] # clim = [2.0, 6.0] # vlab.mesh_from_seeds(earth1, scalars=ls1, clim=clim, logscale=True) # vlab.mesh_from_seeds(earth2, scalars=ls2, clim=clim, logscale=True, opacity=0.5) # vlab.mesh_from_seeds(earth4, scalars=ls2, clim=clim, logscale=True, opacity=0.25) # vlab.plot3d_lines(e1_lines, scalars=[_e1_lsp[0, :] for _e1_lsp in e1_lsps], # clim=clim, logscale=True) # vlab.colorbar(title="L-Shell") # vlab.show() assert lshell_75[1] < lshell_75[0], "RK2 should have less error than Euler" assert lshell_75[2] < lshell_75[1], "RK4 should have less error than RK2" assert lshell_75[3] < lshell_75[ 0], "Euler 1a should have less error than Euler 1" assert lshell_75[4] < lshell_75[ 0], "RK 12 should have less error than Euler 1" assert lshell_75[5] < lshell_75[1], "RK 45 should have less error than RK2" try: if not plot2d: raise ImportError from matplotlib import pyplot as plt from viscid.plot import vpyplot as vlt # stats on error for all points on all lines _ = plt.figure(figsize=(15, 8)) ax1 = vlt.subplot(121) v = plt.violinplot(lshell_diffs, showextrema=False, showmedians=False, vert=False) colors = set_violin_colors(v) xl, xh = plt.gca().get_xlim() for i, txt, c in zip(count(), methods, colors): t_txt = ", took {0:.2e} seconds".format(wall_ts[i]) stat_txt = format_data_range(lshell_diffs[i]) plt.text(xl + 0.35 * (xh - xl), i + 1.15, txt + t_txt, color=c) plt.text(xl + 0.35 * (xh - xl), i + 0.85, stat_txt, color=c) ax1.get_yaxis().set_visible(False) plt.title('L-Shell') plt.xlabel('Relative Difference from Ideal (as fraction)') ax2 = vlt.subplot(122) v = plt.violinplot(phi_diffs, showextrema=False, showmedians=False, vert=False) colors = set_violin_colors(v) xl, xh = plt.gca().get_xlim() for i, txt, c in zip(count(), methods, colors): t_txt = ", took {0:.2e} seconds".format(wall_ts[i]) stat_txt = format_data_range(phi_diffs[i]) plt.text(xl + 0.35 * (xh - xl), i + 1.15, txt + t_txt, color=c) plt.text(xl + 0.35 * (xh - xl), i + 0.85, stat_txt, color=c) ax2.get_yaxis().set_visible(False) plt.title('Longitude') plt.xlabel('Relative Difference from Ideal (as fraction)') vlt.auto_adjust_subplots() vlt.savefig(next_plot_fname(__file__, series='q2')) if args.show: vlt.show() # stats for ds for all points on all lines _ = plt.figure(figsize=(10, 8)) ax1 = vlt.subplot(111) ds = [ np.concatenate([ np.linalg.norm(_l[:, 1:] - _l[:, :-1], axis=0) for _l in lines ]) for lines in all_lines ] v = plt.violinplot(ds, showextrema=False, showmedians=False, vert=False) colors = set_violin_colors(v) xl, xh = plt.gca().get_xlim() for i, txt, c in zip(count(), methods, colors): stat_txt = format_data_range(ds[i]) plt.text(xl + 0.01 * (xh - xl), i + 1.15, txt, color=c) plt.text(xl + 0.01 * (xh - xl), i + 0.85, stat_txt, color=c) ax1.get_yaxis().set_visible(False) plt.xscale('log') plt.title('Step Size') plt.xlabel('Absolute Step Size') vlt.savefig(next_plot_fname(__file__, series='q2')) if args.show: vlt.show() # random other information _ = plt.figure(figsize=(13, 10)) ## wall time for each method vlt.subplot(221) plt.scatter(range(len(methods)), wall_ts, color=colors, s=150, marker='s', edgecolors='none') for i, meth in enumerate(methods): meth = meth.replace(" Adaptive Step", "\nAdaptive Step") plt.annotate(meth, (i, wall_ts[i]), xytext=(0, 15.0), color=colors[i], horizontalalignment='center', verticalalignment='bottom', textcoords='offset points') plt.ylabel("Wall Time (s)") x_padding = 0.5 plt.xlim(-x_padding, len(methods) - x_padding) yl, yh = np.min(wall_ts), np.max(wall_ts) y_padding = 0.4 * (yh - yl) plt.ylim(yl - y_padding, yh + y_padding) plt.gca().get_xaxis().set_visible(False) for _which in ('right', 'top'): plt.gca().spines[_which].set_color('none') ## number of points calculated for each method vlt.subplot(222) plt.scatter(range(len(methods)), npts, color=colors, s=150, marker='s', edgecolors='none') for i, meth in enumerate(methods): meth = meth.replace(" Adaptive Step", "\nAdaptive Step") plt.annotate(meth, (i, npts[i]), xytext=(0, 15.0), color=colors[i], horizontalalignment='center', verticalalignment='bottom', textcoords='offset points') plt.ylabel("Number of Streamline Points Calculated") x_padding = 0.5 plt.xlim(-x_padding, len(methods) - x_padding) yl, yh = np.min(npts), np.max(npts) y_padding = 0.4 * (yh - yl) plt.ylim(yl - y_padding, yh + y_padding) plt.gca().get_xaxis().set_visible(False) for _which in ('right', 'top'): plt.gca().spines[_which].set_color('none') ## Wall time per segment, this should show the overhead of the method vlt.subplot(223) wall_t_per_seg = np.asarray(wall_ts) / np.asarray(npts) plt.scatter(range(len(methods)), wall_t_per_seg, color=colors, s=150, marker='s', edgecolors='none') for i, meth in enumerate(methods): meth = meth.replace(" Adaptive Step", "\nAdaptive Step") plt.annotate(meth, (i, wall_t_per_seg[i]), xytext=(0, 15.0), color=colors[i], horizontalalignment='center', verticalalignment='bottom', textcoords='offset points') plt.ylabel("Wall Time Per Line Segment") x_padding = 0.5 plt.xlim(-x_padding, len(methods) - x_padding) yl, yh = np.min(wall_t_per_seg), np.max(wall_t_per_seg) y_padding = 0.4 * (yh - yl) plt.ylim(yl - y_padding, yh + y_padding) plt.gca().get_xaxis().set_visible(False) plt.gca().xaxis.set_major_formatter(viscid.plot.mpl_extra.steve_axfmt) for _which in ('right', 'top'): plt.gca().spines[_which].set_color('none') ## 75th percentile of l-shell error for each method vlt.subplot(224) plt.scatter(range(len(methods)), lshell_75, color=colors, s=150, marker='s', edgecolors='none') plt.yscale('log') for i, meth in enumerate(methods): meth = meth.replace(" Adaptive Step", "\nAdaptive Step") plt.annotate(meth, (i, lshell_75[i]), xytext=(0, 15.0), color=colors[i], horizontalalignment='center', verticalalignment='bottom', textcoords='offset points') plt.ylabel("75th Percentile of Relative L-Shell Error") x_padding = 0.5 plt.xlim(-x_padding, len(methods) - x_padding) ymin, ymax = np.min(lshell_75), np.max(lshell_75) plt.ylim(0.75 * ymin, 2.5 * ymax) plt.gca().get_xaxis().set_visible(False) for _which in ('right', 'top'): plt.gca().spines[_which].set_color('none') vlt.auto_adjust_subplots(subplot_params=dict(wspace=0.25, hspace=0.15)) vlt.savefig(next_plot_fname(__file__, series='q2')) if args.show: vlt.show() except ImportError: pass try: if not plot3d: raise ImportError from viscid.plot import vlab try: fig = _global_ns['figure'] vlab.clf() except KeyError: fig = vlab.figure(size=[1200, 800], offscreen=not args.show, bgcolor=(1, 1, 1), fgcolor=(0, 0, 0)) _global_ns['figure'] = fig for i, method in zip(count(), methods): # if i in (3, 4): # next_plot_fname(__file__, series='q3') # print(i, "::", [line.shape[1] for line in all_lines[i]]) # # continue vlab.clf() _lshell_diff = [np.abs(s) for s in all_lshell_diffs[i]] vlab.plot3d_lines(all_lines[i], scalars=_lshell_diff) vlab.colorbar(title="Relative L-Shell Error (as fraction)") vlab.title(method, size=0.5) vlab.orientation_axes() vlab.view(azimuth=40, elevation=140, distance=80.0, focalpoint=[0, 0, 0]) vlab.savefig(next_plot_fname(__file__, series='q3')) if args.show: vlab.show() except ImportError: pass # prevent weird xorg bad-instructions on tear down if 'figure' in _global_ns and _global_ns['figure'] is not None: from viscid.plot import vlab vlab.mlab.close(_global_ns['figure']) return 0
def _main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--show", "--plot", action="store_true") args = viscid.vutil.common_argparse(parser, default_verb=0) viscid.logger.setLevel(viscid.logging.DEBUG) args.show = False cotr = viscid.Cotr(dip_tilt=20.0, dip_gsm=15.0) # pylint: disable=not-callable b = viscid.make_dipole(m=cotr.get_dipole_moment(), n=(32, 32, 32)) seeds = viscid.Circle(n=5, r=1.5, pole=[0, 0, 1]) lines, topo = viscid.calc_streamlines(b, seeds, ibound=1.4, method='rk45') for i in range(2): # make sure this works for lines with 0, 1, 2, 3 vertices if i == 1: lines[1] = lines[2][:, :0] lines[2] = lines[2][:, :1] lines[3] = lines[3][:, :2] lines[4] = lines[4][:, :3] viscid.logger.debug('---') viscid.logger.debug('{0}'.format(len(lines))) for line in lines: viscid.logger.debug('line shape: {0}'.format(line.shape)) viscid.logger.debug('---') do_test(lines, scalars=None, txt='given None', show=args.show) do_test(lines, scalars='#ff0000', txt='given a single 24bit rgb hex color', show=args.show) do_test(lines, scalars='#ff000066', txt='given a single 32bit rgba hex color', show=args.show) do_test(lines, scalars='#f00', txt='given a single 12bit rgb hex color', show=args.show) do_test(lines, scalars='#f006', txt='given a single 16bit rgba hex color', show=args.show) do_test(lines, scalars=['#ff0000', '#cc0000', '#aa0000', '#880000', '#660000'], txt='given a list of Nlines 24bit rgb hex colors', show=args.show) do_test(lines, scalars=['#ff000066', '#cc000066', '#aa000066', '#88000066', '#66000066'], txt='given a list of Nlines 32bit rgba hex colors', show=args.show) do_test(lines, scalars=['#f00', '#c00', '#a00', '#800', '#600'], txt='given a list of Nlines 12bit rgb hex colors', show=args.show) do_test(lines, scalars=['#f00a', '#c009', '#a008', '#8007', '#6006'], txt='given a list of Nlines 16bit rgba hex colors', show=args.show) do_test(lines, scalars=[0.8, 0.0, 0.2], txt='given a single rgb [0..1] color', show=args.show) do_test(lines, scalars=[0.8, 0.0, 0.2, 0.8], txt='given a single rgba [0..1] color', show=args.show) do_test(lines, scalars=[(0.8, 0.0, 0.2), (0.7, 0.0, 0.3), (0.6, 0.0, 0.4), (0.5, 0.0, 0.5), (0.4, 0.0, 0.6)], txt='given a list of Nlines rgb [0..1] tuples', show=args.show) do_test(lines, scalars=[(0.8, 0.0, 0.2, 1.0), (0.7, 0.0, 0.3, 0.9), (0.6, 0.0, 0.4, 0.8), (0.5, 0.0, 0.5, 0.7), (0.4, 0.0, 0.6, 0.6)], txt='given a list of Nlines rgba [0..1] tuples', show=args.show) do_test(lines, scalars=[250, 0, 250], txt='given a single rgb [0..255] color', show=args.show) do_test(lines, scalars=[250, 0, 250, 190], txt='given a single rgba [0..255] color', show=args.show) do_test(lines, scalars=[(204, 0, 51), (179, 0, 77), (153, 0, 102), (127, 0, 127), (0.4, 0, 102)], txt='given a list of Nlines rgb [0..255] tuples', show=args.show) do_test(lines, scalars=[(204, 0, 51, 255), (179, 0, 77, 230), (153, 0, 102, 204), (127, 0, 127, 179), (102, 0, 102, 153)], txt='given a list of Nlines rgba [0..255] tuples', show=args.show) do_test(lines, scalars=['#ff000088', 'blue', 'lavenderblush', 'c', '#4f4'], txt='given a mix of color hex/html color names', show=args.show) do_test(lines, scalars=topo, txt='scalars == topo value', show=args.show) do_test(lines, scalars=viscid.topology2color(topo), txt='scalars == topo2color value', show=args.show) do_test(lines, scalars=np.log(viscid.magnitude(b)), txt='given bmag', show=args.show) # prevent weird xorg bad-instructions on tear down if 'figure' in _global_ns and _global_ns['figure'] is not None: from viscid.plot import vlab vlab.mlab.close(_global_ns['figure']) 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=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 _main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--notwo", dest='notwo', action="store_true") parser.add_argument("--nothree", dest='nothree', action="store_true") parser.add_argument("--show", "--plot", action="store_true") args = viscid.vutil.common_argparse(parser, default_verb=0) plot2d = not args.notwo plot3d = not args.nothree # ################################################# # viscid.logger.info("Testing field lines on 2d field...") B = viscid.make_dipole(twod=True) line = viscid.seed.Line((0.2, 0.0, 0.0), (1.0, 0.0, 0.0), 10) obound0 = np.array([-4, -4, -4], dtype=B.data.dtype) obound1 = np.array([4, 4, 4], dtype=B.data.dtype) run_test(B, line, plot2d=plot2d, plot3d=plot3d, title='2D', show=args.show, ibound=0.07, obound0=obound0, obound1=obound1) ################################################# viscid.logger.info("Testing field lines on 3d field...") B = viscid.make_dipole(m=[0.2, 0.3, -0.9]) sphere = viscid.seed.Sphere((0.0, 0.0, 0.0), 2.0, ntheta=20, nphi=10) obound0 = np.array([-4, -4, -4], dtype=B.data.dtype) obound1 = np.array([4, 4, 4], dtype=B.data.dtype) run_test(B, sphere, plot2d=plot2d, plot3d=plot3d, title='3D', show=args.show, ibound=0.12, obound0=obound0, obound1=obound1, method=viscid.RK12) # The Remainder of this test makes sure higher order methods are indeed # more accurate than lower order methods... this could find a bug in # the integrators ################################################## # test accuracy of streamlines in an ideal dipole cotr = viscid.Cotr(dip_tilt=15.0, dip_gsm=21.0) # pylint: disable=not-callable m = cotr.get_dipole_moment(crd_system='gse') seeds = viscid.seed.Sphere((0.0, 0.0, 0.0), 2.0, pole=-m, ntheta=25, nphi=25, thetalim=(5, 90), philim=(5, 360), phi_endpoint=False) B = viscid.make_dipole(m=m, crd_system='gse', n=(256, 256, 256), l=(-25, -25, -25), h=(25, 25, 25), dtype='f8') seeds_xyz = seeds.get_points() # seeds_lsp = viscid.xyz2lsrlp(seeds_xyz, cotr=cotr, crd_system=B)[(0, 3), :] seeds_lsp = viscid.xyz2lsrlp(seeds_xyz, cotr=cotr, crd_system=B)[(0, 3), :] e1_lines, e1_lsps, t_e1 = lines_and_lsps(B, seeds, method='euler1', ibound=1.0, cotr=cotr) rk2_lines, rk2_lsps, t_rk2 = lines_and_lsps(B, seeds, method='rk2', ibound=1.0, cotr=cotr) rk4_lines, rk4_lsps, t_rk4 = lines_and_lsps(B, seeds, method='rk4', ibound=1.0, cotr=cotr) e1a_lines, e1a_lsps, t_e1a = lines_and_lsps(B, seeds, method='euler1a', ibound=1.0, cotr=cotr) rk12_lines, rk12_lsps, t_rk12 = lines_and_lsps(B, seeds, method='rk12', ibound=1.0, cotr=cotr) rk45_lines, rk45_lsps, t_rk45 = lines_and_lsps(B, seeds, method='rk45', ibound=1.0, cotr=cotr) def _calc_rel_diff(_lsp, _ideal_lsp, _d): _diffs = [] for _ilsp, _iideal in zip(_lsp, _ideal_lsp.T): _a = _ilsp[_d, :] _b = _iideal[_d] _diffs.append((_a - _b) / _b) return _diffs lshell_diff_e1 = _calc_rel_diff(e1_lsps, seeds_lsp, 0) phi_diff_e1 = _calc_rel_diff(e1_lsps, seeds_lsp, 1) lshell_diff_rk2 = _calc_rel_diff(rk2_lsps, seeds_lsp, 0) phi_diff_rk2 = _calc_rel_diff(rk2_lsps, seeds_lsp, 1) lshell_diff_rk4 = _calc_rel_diff(rk4_lsps, seeds_lsp, 0) phi_diff_rk4 = _calc_rel_diff(rk4_lsps, seeds_lsp, 1) lshell_diff_e1a = _calc_rel_diff(e1a_lsps, seeds_lsp, 0) phi_diff_e1a = _calc_rel_diff(e1a_lsps, seeds_lsp, 1) lshell_diff_rk12 = _calc_rel_diff(rk12_lsps, seeds_lsp, 0) phi_diff_rk12 = _calc_rel_diff(rk12_lsps, seeds_lsp, 1) lshell_diff_rk45 = _calc_rel_diff(rk45_lsps, seeds_lsp, 0) phi_diff_rk45 = _calc_rel_diff(rk45_lsps, seeds_lsp, 1) methods = ['Euler 1', 'Runge Kutta 2', 'Runge Kutta 4', 'Euler 1 Adaptive Step', 'Runge Kutta 12 Adaptive Step', 'Runge Kutta 45 Adaptive Step'] wall_ts = [t_e1, t_rk2, t_rk4, t_e1a, t_rk12, t_rk45] all_lines = [e1_lines, rk2_lines, rk4_lines, e1a_lines, rk12_lines, rk45_lines] all_lshell_diffs = [lshell_diff_e1, lshell_diff_rk2, lshell_diff_rk4, lshell_diff_e1a, lshell_diff_rk12, lshell_diff_rk45] lshell_diffs = [np.abs(np.concatenate(lshell_diff_e1, axis=0)), np.abs(np.concatenate(lshell_diff_rk2, axis=0)), np.abs(np.concatenate(lshell_diff_rk4, axis=0)), np.abs(np.concatenate(lshell_diff_e1a, axis=0)), np.abs(np.concatenate(lshell_diff_rk12, axis=0)), np.abs(np.concatenate(lshell_diff_rk45, axis=0))] phi_diffs = [np.abs(np.concatenate(phi_diff_e1, axis=0)), np.abs(np.concatenate(phi_diff_rk2, axis=0)), np.abs(np.concatenate(phi_diff_rk4, axis=0)), np.abs(np.concatenate(phi_diff_e1a, axis=0)), np.abs(np.concatenate(phi_diff_rk12, axis=0)), np.abs(np.concatenate(phi_diff_rk45, axis=0))] npts = [len(lsd) for lsd in lshell_diffs] lshell_75 = [np.percentile(lsdiff, 75) for lsdiff in lshell_diffs] # # 3D DEBUG PLOT:: for really getting under the covers # vlab.clf() # earth1 = viscid.seed.Sphere((0.0, 0.0, 0.0), 1.0, pole=-m, ntheta=60, nphi=120, # thetalim=(15, 165), philim=(0, 360)) # ls1 = viscid.xyz2lsrlp(earth1.get_points(), cotr=cotr, crd_system='gse')[0, :] # earth2 = viscid.seed.Sphere((0.0, 0.0, 0.0), 2.0, pole=-m, ntheta=60, nphi=120, # thetalim=(15, 165), philim=(0, 360)) # ls2 = viscid.xyz2lsrlp(earth2.get_points(), cotr=cotr, crd_system='gse')[0, :] # earth4 = viscid.seed.Sphere((0.0, 0.0, 0.0), 4.0, pole=-m, ntheta=60, nphi=120, # thetalim=(15, 165), philim=(0, 360)) # ls4 = viscid.xyz2lsrlp(earth4.get_points(), cotr=cotr, crd_system='gse')[0, :] # clim = [2.0, 6.0] # vlab.mesh_from_seeds(earth1, scalars=ls1, clim=clim, logscale=True) # vlab.mesh_from_seeds(earth2, scalars=ls2, clim=clim, logscale=True, opacity=0.5) # vlab.mesh_from_seeds(earth4, scalars=ls2, clim=clim, logscale=True, opacity=0.25) # vlab.plot3d_lines(e1_lines, scalars=[_e1_lsp[0, :] for _e1_lsp in e1_lsps], # clim=clim, logscale=True) # vlab.colorbar(title="L-Shell") # vlab.show() assert lshell_75[1] < lshell_75[0], "RK2 should have less error than Euler" assert lshell_75[2] < lshell_75[1], "RK4 should have less error than RK2" assert lshell_75[3] < lshell_75[0], "Euler 1a should have less error than Euler 1" assert lshell_75[4] < lshell_75[0], "RK 12 should have less error than Euler 1" assert lshell_75[5] < lshell_75[1], "RK 45 should have less error than RK2" try: if not plot2d: raise ImportError from viscid.plot import vpyplot as vlt from matplotlib import pyplot as plt # stats on error for all points on all lines _ = plt.figure(figsize=(15, 8)) ax1 = vlt.subplot(121) v = plt.violinplot(lshell_diffs, showextrema=False, showmedians=False, vert=False) colors = set_violin_colors(v) xl, xh = plt.gca().get_xlim() for i, txt, c in zip(count(), methods, colors): t_txt = ", took {0:.2e} seconds".format(wall_ts[i]) stat_txt = format_data_range(lshell_diffs[i]) plt.text(xl + 0.35 * (xh - xl), i + 1.15, txt + t_txt, color=c) plt.text(xl + 0.35 * (xh - xl), i + 0.85, stat_txt, color=c) ax1.get_yaxis().set_visible(False) plt.title('L-Shell') plt.xlabel('Relative Difference from Ideal (as fraction)') ax2 = vlt.subplot(122) v = plt.violinplot(phi_diffs, showextrema=False, showmedians=False, vert=False) colors = set_violin_colors(v) xl, xh = plt.gca().get_xlim() for i, txt, c in zip(count(), methods, colors): t_txt = ", took {0:.2e} seconds".format(wall_ts[i]) stat_txt = format_data_range(phi_diffs[i]) plt.text(xl + 0.35 * (xh - xl), i + 1.15, txt + t_txt, color=c) plt.text(xl + 0.35 * (xh - xl), i + 0.85, stat_txt, color=c) ax2.get_yaxis().set_visible(False) plt.title('Longitude') plt.xlabel('Relative Difference from Ideal (as fraction)') vlt.auto_adjust_subplots() vlt.savefig(next_plot_fname(__file__, series='q2')) if args.show: vlt.show() # stats for ds for all points on all lines _ = plt.figure(figsize=(10, 8)) ax1 = vlt.subplot(111) ds = [np.concatenate([np.linalg.norm(_l[:, 1:] - _l[:, :-1], axis=0) for _l in lines]) for lines in all_lines] v = plt.violinplot(ds, showextrema=False, showmedians=False, vert=False) colors = set_violin_colors(v) xl, xh = plt.gca().get_xlim() for i, txt, c in zip(count(), methods, colors): stat_txt = format_data_range(ds[i]) plt.annotate(txt, xy=(0.55, i / len(methods) + 0.1), color=c, xycoords='axes fraction') plt.annotate(stat_txt, xy=(0.55, i / len(methods) + 0.04), color=c, xycoords='axes fraction') ax1.get_yaxis().set_visible(False) plt.xscale('log') plt.title('Step Size') plt.xlabel('Absolute Step Size') vlt.savefig(next_plot_fname(__file__, series='q2')) if args.show: vlt.show() # random other information _ = plt.figure(figsize=(13, 10)) ## wall time for each method vlt.subplot(221) plt.scatter(range(len(methods)), wall_ts, color=colors, s=150, marker='s', edgecolors='none') for i, meth in enumerate(methods): meth = meth.replace(" Adaptive Step", "\nAdaptive Step") plt.annotate(meth, (i, wall_ts[i]), xytext=(0, 15.0), color=colors[i], horizontalalignment='center', verticalalignment='bottom', textcoords='offset points') plt.ylabel("Wall Time (s)") x_padding = 0.5 plt.xlim(-x_padding, len(methods) - x_padding) yl, yh = np.min(wall_ts), np.max(wall_ts) y_padding = 0.4 * (yh - yl) plt.ylim(yl - y_padding, yh + y_padding) plt.gca().get_xaxis().set_visible(False) for _which in ('right', 'top'): plt.gca().spines[_which].set_color('none') ## number of points calculated for each method vlt.subplot(222) plt.scatter(range(len(methods)), npts, color=colors, s=150, marker='s', edgecolors='none') for i, meth in enumerate(methods): meth = meth.replace(" Adaptive Step", "\nAdaptive Step") plt.annotate(meth, (i, npts[i]), xytext=(0, 15.0), color=colors[i], horizontalalignment='center', verticalalignment='bottom', textcoords='offset points') plt.ylabel("Number of Streamline Points Calculated") x_padding = 0.5 plt.xlim(-x_padding, len(methods) - x_padding) yl, yh = np.min(npts), np.max(npts) y_padding = 0.4 * (yh - yl) plt.ylim(yl - y_padding, yh + y_padding) plt.gca().get_xaxis().set_visible(False) for _which in ('right', 'top'): plt.gca().spines[_which].set_color('none') ## Wall time per segment, this should show the overhead of the method vlt.subplot(223) wall_t_per_seg = np.asarray(wall_ts) / np.asarray(npts) plt.scatter(range(len(methods)), wall_t_per_seg, color=colors, s=150, marker='s', edgecolors='none') for i, meth in enumerate(methods): meth = meth.replace(" Adaptive Step", "\nAdaptive Step") plt.annotate(meth, (i, wall_t_per_seg[i]), xytext=(0, 15.0), color=colors[i], horizontalalignment='center', verticalalignment='bottom', textcoords='offset points') plt.ylabel("Wall Time Per Line Segment") x_padding = 0.5 plt.xlim(-x_padding, len(methods) - x_padding) yl, yh = np.min(wall_t_per_seg), np.max(wall_t_per_seg) y_padding = 0.4 * (yh - yl) plt.ylim(yl - y_padding, yh + y_padding) plt.gca().get_xaxis().set_visible(False) plt.gca().xaxis.set_major_formatter(viscid.plot.mpl_extra.steve_axfmt) for _which in ('right', 'top'): plt.gca().spines[_which].set_color('none') ## 75th percentile of l-shell error for each method vlt.subplot(224) plt.scatter(range(len(methods)), lshell_75, color=colors, s=150, marker='s', edgecolors='none') plt.yscale('log') for i, meth in enumerate(methods): meth = meth.replace(" Adaptive Step", "\nAdaptive Step") plt.annotate(meth, (i, lshell_75[i]), xytext=(0, 15.0), color=colors[i], horizontalalignment='center', verticalalignment='bottom', textcoords='offset points') plt.ylabel("75th Percentile of Relative L-Shell Error") x_padding = 0.5 plt.xlim(-x_padding, len(methods) - x_padding) ymin, ymax = np.min(lshell_75), np.max(lshell_75) plt.ylim(0.75 * ymin, 2.5 * ymax) plt.gca().get_xaxis().set_visible(False) for _which in ('right', 'top'): plt.gca().spines[_which].set_color('none') vlt.auto_adjust_subplots(subplot_params=dict(wspace=0.25, hspace=0.15)) vlt.savefig(next_plot_fname(__file__, series='q2')) if args.show: vlt.show() except ImportError: pass try: if not plot3d: raise ImportError from viscid.plot import vlab try: fig = _global_ns['figure'] vlab.clf() except KeyError: fig = vlab.figure(size=[1200, 800], offscreen=not args.show, bgcolor=(1, 1, 1), fgcolor=(0, 0, 0)) _global_ns['figure'] = fig for i, method in zip(count(), methods): # if i in (3, 4): # next_plot_fname(__file__, series='q3') # print(i, "::", [line.shape[1] for line in all_lines[i]]) # # continue vlab.clf() _lshell_diff = [np.abs(s) for s in all_lshell_diffs[i]] vlab.plot3d_lines(all_lines[i], scalars=_lshell_diff) vlab.colorbar(title="Relative L-Shell Error (as fraction)") vlab.title(method, size=0.5) vlab.orientation_axes() vlab.view(azimuth=40, elevation=140, distance=80.0, focalpoint=[0, 0, 0]) vlab.savefig(next_plot_fname(__file__, series='q3')) if args.show: vlab.show() except ImportError: pass # prevent weird xorg bad-instructions on tear down if 'figure' in _global_ns and _global_ns['figure'] is not None: from viscid.plot import vlab vlab.mlab.close(_global_ns['figure']) return 0