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 _main(): import os from viscid.plot import vpyplot as vlt grid = viscid.grid.Grid(time=0.0) crds = viscid.arrays2crds([np.linspace(-1, 1, 32), np.linspace(-1, 1, 32)]) grid.add_field(viscid.full(crds, np.nan, name='V')) seeds0 = viscid.Circle(p0=[0.0, 0.0, 0.0], r=0.8, n=25).get_points() seeds1 = viscid.Circle(p0=[0.0, 0.0, 0.0], r=1.1, n=25).get_points() seeds3 = viscid.Line([-0.5, 0, 0], [0.5, 0, 0], n=5) delmask = np.zeros([seeds0.shape[1]], dtype='bool') curator = viscid.SeedCurator() seeds2 = curator.update(grid['V'], np.array(seeds1), delmask=delmask) vlt.plt.scatter(seeds1[0], seeds1[1], c=[0.0, 1.0, 0.0]) vlt.plt.scatter(seeds2[0], seeds2[1]) vlt.plt.axhline(-1) vlt.plt.axvline(-1) # pylint: disable=multiple-statements vlt.plt.axhline(1) vlt.plt.axvline(1) # pylint: disable=multiple-statements vlt.show() curator = viscid.ReplacementCurator(seeds0) seeds2 = curator.update(grid['V'], np.array(seeds1), delmask=delmask) vlt.plt.scatter(seeds1[0], seeds1[1], c=[0.0, 1.0, 0.0]) vlt.plt.scatter(seeds2[0], seeds2[1]) vlt.plt.axhline(-1) vlt.plt.axvline(-1) # pylint: disable=multiple-statements vlt.plt.axhline(1) vlt.plt.axvline(1) # pylint: disable=multiple-statements vlt.show() curator = viscid.ContinuousCurator(seeds3, cadence=-1) seeds2 = curator.update(grid['V'], np.array(seeds1), delmask=delmask) vlt.plt.scatter(seeds1[0], seeds1[1], c=[0.0, 1.0, 0.0]) vlt.plt.scatter(seeds2[0], seeds2[1]) vlt.plt.axhline(-1) vlt.plt.axvline(-1) # pylint: disable=multiple-statements vlt.plt.axhline(1) vlt.plt.axvline(1) # pylint: disable=multiple-statements vlt.show() target_dir = os.path.join(os.path.expanduser('~'), 'Desktop', 'fluid_movie') print("Attempting to make a movie in:", target_dir) f = viscid.load_file("~/dev/stage/otico_001/otico*.3d.xdmf") xl, xh = f.get_grid().xl_nc, f.get_grid().xh_nc seeds0 = viscid.Circle(p0=0.5 * (xl + xh), r=0.2 * np.max(xh - xl), pole=(0, 0, 1), n=10) seeds1 = viscid.Circle(p0=0.5 * (xl + xh), r=0.4 * np.max(xh - xl), pole=(0, 0, 1), n=10) seeds = viscid.Point( np.concatenate( [seeds0.get_points(), seeds1.get_points()], axis=1)) if not os.path.isdir(target_dir): os.mkdir(target_dir) target_fname = os.path.join(target_dir, 'fluid') viscid.follow_fluid(f, seeds, dt=0.0101926 / 2, callback_kwargs=dict(show=False, series_fname=target_fname)) 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("--show", "--plot", action="store_true") parser.add_argument("--interact", "-i", action="store_true") args = vutil.common_argparse(parser) f3d = viscid.load_file(os.path.join(sample_dir, 'sample_xdmf.3d.[0].xdmf')) f_iono = viscid.load_file( os.path.join(sample_dir, "sample_xdmf.iof.[0].xdmf")) b = f3d["b"] v = f3d["v"] pp = f3d["pp"] e = f3d["e_cc"] vlab.mlab.options.offscreen = not args.show vlab.figure(size=(1280, 800)) ########################################################## # make b a dipole inside 3.1Re and set e = 0 inside 4.0Re cotr = viscid.Cotr(time='1990-03-21T14:48', dip_tilt=0.0) # pylint: disable=not-callable moment = cotr.get_dipole_moment(crd_system=b) isphere_mask = viscid.make_spherical_mask(b, rmax=3.1) viscid.fill_dipole(b, m=moment, mask=isphere_mask) e_mask = viscid.make_spherical_mask(b, rmax=4.0) viscid.set_in_region(e, 0.0, alpha=0.0, mask=e_mask, out=e) ###################################### # plot a scalar cut plane of pressure pp_src = vlab.field2source(pp, center='node') scp = vlab.scalar_cut_plane(pp_src, plane_orientation='z_axes', opacity=0.5, transparent=True, view_controls=False, cmap="inferno", logscale=True) scp.implicit_plane.normal = [0, 0, -1] scp.implicit_plane.origin = [0, 0, 0] scp.enable_contours = True scp.contour.filled_contours = True scp.contour.number_of_contours = 64 cbar = vlab.colorbar(scp, title=pp.name, orientation='vertical') cbar.scalar_bar_representation.position = (0.01, 0.13) cbar.scalar_bar_representation.position2 = (0.08, 0.76) ###################################### # plot a vector cut plane of the flow vcp = vlab.vector_cut_plane(v, scalars=pp_src, plane_orientation='z_axes', view_controls=False, mode='arrow', cmap='Greens_r') vcp.implicit_plane.normal = [0, 0, -1] vcp.implicit_plane.origin = [0, 0, 0] ############################## # plot very faint isosurfaces vx_src = vlab.field2source(v['x'], center='node') iso = vlab.iso_surface(vx_src, contours=[0.0], opacity=0.008, cmap='Pastel1') ############################################################## # calculate B field lines && topology in Viscid and plot them seedsA = viscid.SphericalPatch([0, 0, 0], [2, 0, 1], 30, 15, r=5.0, nalpha=5, nbeta=5) seedsB = viscid.SphericalPatch([0, 0, 0], [1.9, 0, -20], 30, 15, r=5.0, nalpha=1, nbeta=5) seeds = np.concatenate([seedsA, seedsB], axis=1) b_lines, topo = viscid.calc_streamlines(b, seeds, ibound=3.5, obound0=[-25, -20, -20], obound1=[15, 20, 20], wrap=True) vlab.plot_lines(b_lines, scalars=viscid.topology2color(topo)) ###################################################################### # plot a random circle at geosynchronus orbit with scalars colored # by the Matplotlib viridis color map, just because we can; this is # a useful toy for debugging circle = viscid.Circle(p0=[0, 0, 0], r=6.618, n=128, endpoint=True) scalar = np.sin(circle.as_local_coordinates().get_crd('phi')) surf = vlab.plot_line(circle.get_points(), scalars=scalar, clim=0.8, cmap="Spectral_r") ###################################################################### # Use Mayavi (VTK) to calculate field lines using an interactive seed # These field lines are colored by E parallel epar = viscid.project(e, b) epar.name = "Epar" bsl2 = vlab.streamline(b, epar, seedtype='plane', seed_resolution=4, integration_direction='both', clim=(-0.05, 0.05)) # now tweak the VTK streamlines bsl2.stream_tracer.maximum_propagation = 20. bsl2.seed.widget.origin = [-11, -5.0, -2.0] bsl2.seed.widget.point1 = [-11, 5.0, -2.0] bsl2.seed.widget.point2 = [-11.0, -5.0, 2.0] bsl2.streamline_type = 'tube' bsl2.tube_filter.radius = 0.03 bsl2.stop() # this stop/start was a hack to get something to update bsl2.start() bsl2.seed.widget.enabled = False cbar = vlab.colorbar(bsl2, title=epar.name, label_fmt='%.3f', orientation='horizontal') cbar.scalar_bar_representation.position = (0.15, 0.01) cbar.scalar_bar_representation.position2 = (0.72, 0.10) ############################################################### # Make a contour at the open-closed boundary in the ionosphere seeds_iono = viscid.Sphere(r=1.063, pole=-moment, ntheta=256, nphi=256, thetalim=(0, 180), philim=(0, 360), crd_system=b) _, topo_iono = viscid.calc_streamlines(b, seeds_iono, ibound=1.0, nr_procs='all', output=viscid.OUTPUT_TOPOLOGY) topo_iono = np.log2(topo_iono) m = vlab.mesh_from_seeds(seeds_iono, scalars=topo_iono, opacity=1.0, clim=(0, 3), color=(0.992, 0.445, 0.0)) m.enable_contours = True m.actor.property.line_width = 4.0 m.contour.number_of_contours = 4 #################################################################### # Plot the ionosphere, note that the sample data has the ionosphere # at a different time, so the open-closed boundary found above # will not be consistant with the field aligned currents fac_tot = 1e9 * f_iono['fac_tot'] m = vlab.plot_ionosphere(fac_tot, bounding_lat=30.0, vmin=-300, vmax=300, opacity=0.75, rotate=cotr, crd_system=b) m.actor.property.backface_culling = True ######################################################################## # Add some markers for earth, i.e., real earth, and dayside / nightside # representation vlab.plot_blue_marble(r=1.0, lines=False, ntheta=64, nphi=128, rotate=cotr, crd_system=b) # now shade the night side with a transparent black hemisphere vlab.plot_earth_3d(radius=1.01, night_only=True, opacity=0.5, crd_system=b) #################### # Finishing Touches # vlab.axes(pp_src, nb_labels=5) oa = vlab.orientation_axes() oa.marker.set_viewport(0.75, 0.75, 1.0, 1.0) # note that resize won't work if the current figure has the # off_screen_rendering flag set # vlab.resize([1200, 800]) vlab.view(azimuth=45, elevation=70, distance=35.0, focalpoint=[-2, 0, 0]) ############## # Save Figure # print("saving png") # vlab.savefig('mayavi_msphere_sample.png') # print("saving x3d") # # x3d files can be turned into COLLADA files with meshlab, and # # COLLADA (.dae) files can be opened in OS X's preview # # # # IMPORTANT: for some reason, using bounding_lat in vlab.plot_ionosphere # # causes a segfault when saving x3d files # # # vlab.savefig('mayavi_msphere_sample.x3d') # print("done") vlab.savefig(next_plot_fname(__file__)) ########################### # Interact Programatically if args.interact: vlab.interact() ####################### # Interact Graphically if args.show: vlab.show() try: vlab.mlab.close() except AttributeError: pass return 0