aboutsummaryrefslogtreecommitdiff
path: root/Tools/Visualization.ipynb
blob: 6a598db02b270b3c847e0c3fa299d5db549a8e39 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
{
 "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": [
       "<function __main__.plot_field>"
      ]
     },
     "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
}