{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Overview\n", "\n", "This a notebook that inspects the results of a WarpX simulation.\n", "\n", "# Instruction\n", "\n", "Enter the path of the data you wish to visualize below. Then execute the cells one by one, by selecting them with your mouse and typing `Shift + Enter`" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "data_path = '../Example/Langmuir/'" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Import statements\n", "import os, glob\n", "import yt ; yt.funcs.mylog.setLevel(50)\n", "from IPython.display import clear_output\n", "import numpy as np\n", "from ipywidgets import interact, ToggleButtons, IntSlider\n", "import matplotlib.pyplot as plt\n", "%matplotlib" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# YT basic usage. Read this for practical examples\n", "# ------------------------------------------------\n", "\n", "# Create a dataset object\n", "ds = yt.load( './pml/plt00400' )\n", "# List all fields in the datasert\n", "ds.field_list\n", "# Get All Data from the dataset\n", "ad = ds.all_data()\n", "# Get some data. \".v\" converts arrays from units-aware yt arrays to numpy arrays.\n", "# Magnetic field\n", "b = ad['boxlib', 'Bx'].v\n", "# particle weight and z coordinate for species particle0\n", "w = ad['particle0', 'particle_momentum_z'].v\n", "z = ad['particle0', 'particle_weight'].v\n", "\n", "# Create a sliceplot object\n", "sl = yt.SlicePlot(ds, 2, 'Ex')\n", "# Set labels\n", "sl.set_xlabel(r'$x (\\mu m)$')\n", "sl.set_ylabel(r'$z (\\mu m)$')\n", "# Set figure size\n", "sl.figure_size = (9, 7)\n", "# Plot particles\n", "sl.annotate_particles(width=1., p_size=2, ptype='particle0', col='black')\n", "# Show grids\n", "sl.annotate_grids()\n", "# show the plot\n", "sl.show()\n", "# Save image\n", "# sl.save('./toto.pdf')\n", "# This returns the domain boundaries\n", "sl.bounds" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Functions to plot the fields" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def plot_field( iteration, field, slicing_direction='y', plotter='matplotlib' ):\n", " ds = yt.load( os.path.join(data_path, 'plt%05d/' %iteration) )\n", " all_data_level_0 = ds.covering_grid(level=0, \n", " left_edge=ds.domain_left_edge, \n", " dims=ds.domain_dimensions)\n", " \n", " if plotter == 'yt':\n", " sl = yt.SlicePlot(ds, slicing_direction, field)\n", " sl.set_log( field, False)\n", " sl.annotate_grids()\n", " # Show the new plot\n", " clear_output()\n", " sl.show()\n", "\n", " elif plotter == 'matplotlib':\n", "\n", " left_edge = ds.domain_left_edge.convert_to_mks()*1.e6\n", " right_edge = ds.domain_right_edge.convert_to_mks()*1.e6\n", " \n", " if ds.dimensionality == 3:\n", " if slicing_direction == 'x':\n", " n = int( ds.domain_dimensions[0]//2 )\n", " data2d = all_data_level_0[field][n, :, :]\n", " extent = [ left_edge[2], right_edge[2], left_edge[1], right_edge[1] ]\n", " elif slicing_direction == 'y':\n", " n = int( ds.domain_dimensions[1]//2 )\n", " data2d = all_data_level_0[field][:, n, :]\n", " extent = [ left_edge[2], right_edge[2], left_edge[0], right_edge[0] ]\n", " elif slicing_direction == 'z':\n", " n = int( ds.domain_dimensions[2]//2 )\n", " data2d = all_data_level_0[field][:, :, n]\n", " extent = [ left_edge[1], right_edge[1], left_edge[0], right_edge[0] ]\n", " elif ds.dimensionality == 2:\n", " data2d = all_data_level_0[field][:,:,0].T\n", " extent = [ left_edge[1], right_edge[1], left_edge[0], right_edge[0] ]\n", "\n", " plt.clf()\n", " plt.imshow( data2d.to_ndarray(), interpolation='nearest', cmap='viridis',\n", " origin='lower', extent=extent )\n", " plt.colorbar()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Interactive viewer" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "51d3a2c04a684310a3e87ee33674f728" } }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Find iterations\n", "file_list = glob.glob(os.path.join( data_path, 'plt?????') )\n", "iterations = [ int(file_name[-5:]) for file_name in file_list ]\n", "\n", "interact(plot_field, \n", " iteration = IntSlider(min=min(iterations), max=max(iterations), step=iterations[1]-iterations[0]),\n", " field = ToggleButtons( options=['jx', 'jy', 'jz', 'Ex', 'Ey', 'Ez', 'Bx', 'By', 'Bz'], value='jz'),\n", " slicing_direction = ToggleButtons( options=[ 'x', 'y', 'z'], value='y'),\n", " plotter = ToggleButtons( options=['matplotlib', 'yt'] ) )" ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.14" }, "widgets": { "state": { "11d243e9f5074fe1b115949d174d59de": { "views": [ { "cell_index": 6 } ] } }, "version": "1.2.0" } }, "nbformat": 4, "nbformat_minor": 1 }