aboutsummaryrefslogtreecommitdiff
path: root/Exec/uniform_plasma/Visualization.ipynb
blob: 559f3648225308c755c04ce31c0e8d97a5d15b1c (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
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# Overview\n",
    "\n",
    "This a notebook that inspects the results of a WarpX simulation.\n",
    "\n",
    "# Instructions\n",
    "\n",
    "Execute the cells below one by one, by selecting them with your mouse and typing `Shift + Enter`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# General imports\n",
    "import glob, sys\n",
    "# Import numpy and matplotlib\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib\n",
    "# Import yt\n",
    "import yt\n",
    "yt.funcs.mylog.setLevel(50) # Deactivate yt verbose\n",
    "\n",
    "# Import interactive notebook feature\n",
    "#from IPython.display import clear_output\n",
    "from ipywidgets import interact, IntSlider, RadioButtons\n",
    "\n",
    "# Find output iterations\n",
    "file_list = glob.glob('plt?????')\n",
    "iterations = [ int(file_name[3:]) for file_name in file_list ]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# Functions to extract and plot the fields"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def extra_data( field, iteration ):\n",
    "    \"\"\"\n",
    "    Extract the 3D array that corresponds to `field` at the \n",
    "    requested iteration, and returns as a `YTarray`, which \n",
    "    can be used exactly like a numpy array.\n",
    "    \"\"\"\n",
    "    ds = yt.load( './plt%05d/' %iteration )\n",
    "    data = ds.covering_grid(level=0, \n",
    "            left_edge=ds.domain_left_edge, \n",
    "            dims=ds.domain_dimensions)\n",
    "    return( data[field] ) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "def plot_field( iteration, field, slicing_direction='y' ):\n",
    "    # Get the data\n",
    "    field_array = extra_data( field, iteration )\n",
    "    \n",
    "    # Get the position of the edges\n",
    "    ds = yt.load( './plt%05d/' %iteration )\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 slicing_direction == 'x':\n",
    "        n = int( ds.domain_dimensions[0]//2 )\n",
    "        data2d = field_array[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 = field_array[:, 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 = field_array[:, :, n]\n",
    "        extent = [ left_edge[1], right_edge[1], left_edge[0], right_edge[0] ]\n",
    "    plt.clf()\n",
    "    plt.imshow( data2d, interpolation='nearest', cmap='viridis',\n",
    "              origin='lower', extent=extent )\n",
    "    plt.colorbar()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# Interactive viewer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "interact(plot_field, \n",
    "         iteration = IntSlider(min=min(iterations), max=max(iterations), step=iterations[1]-iterations[0]),\n",
    "         field = RadioButtons( options=['jx', 'jy', 'jz', 'Ex', 'Ey', 'Ez'], value='jz'),\n",
    "         slicing_direction = RadioButtons( options=[ 'x', 'y', 'z'], value='y') )"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  },
  "widgets": {
   "state": {
    "5ca3ffc563434f7487da7b1bd657761b": {
     "views": [
      {
       "cell_index": 5
      }
     ]
    }
   },
   "version": "1.2.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}