aboutsummaryrefslogtreecommitdiff
path: root/Tools/DevUtils/compare_wx_w_3d.ipynb
blob: d4b0520dd36e313b5e9f05192e4a8afa33a67a59 (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
225
226
227
228
229
230
231
232
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Overview\n",
    "\n",
    "This a notebook that inspects the results of a WarpX simulation and a Warp simulation at the same time. Typically used to compare the results of both codes for the same set of parameters.\n",
    "\n",
    "# Instructions\n",
    "\n",
    "- Enter the paths for the WarpX and the Warp simulations\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
   },
   "outputs": [],
   "source": [
    "# Import statements\n",
    "import sys\n",
    "from tqdm import tqdm\n",
    "import yt, glob\n",
    "yt.funcs.mylog.setLevel(50)\n",
    "from IPython.display import clear_output\n",
    "import numpy as np\n",
    "from ipywidgets import interact, RadioButtons, IntSlider\n",
    "from openpmd_viewer import OpenPMDTimeSeries\n",
    "from yt.units import volt\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Define path way for WarpX results and find iterations\n",
    "path_warpx = '/Users/mthevenet/warpX_directory/res_warpx/valid_pwfa/'\n",
    "file_list_warpx = glob.glob(path_warpx + 'plt?????')\n",
    "iterations_warpx = [ int(file_name[len(file_name)-5:]) for file_name in file_list_warpx ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Define path way for Warp results and find iterations\n",
    "path_warp = '/Users/mthevenet/warp_results/valid_pwfa/'\n",
    "file_list_warp = glob.glob(path_warp + 'diags/hdf5/data????????.h5')\n",
    "iterations_warp = [ int(file_name[len(file_name)-11:len(file_name)-3]) for file_name in file_list_warp ]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Functions to plot the fields\n",
    "\n",
    "Note that the data are loaded with yt and plotted using matplotlib"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def plot_field( iteration, field, slicing_direction='y'):\n",
    "    \n",
    "    # First dataset\n",
    "    ds = yt.load( path_warpx + '/plt%05d/' %iteration )\n",
    "    all_data_level_0 = ds.covering_grid(level=0, \n",
    "                        left_edge=ds.domain_left_edge, dims=ds.domain_dimensions)\n",
    "  \n",
    "    # Second dataset\n",
    "#     ts = OpenPMDTimeSeries(path_warp + 'diags/hdf5/')    \n",
    "    ds2 = yt.load( path_warp + 'diags/hdf5/data%08d.h5' %iteration )\n",
    "    all_data_level_02 = ds2.covering_grid(level=0, \n",
    "                        left_edge=ds2.domain_left_edge, dims=ds2.domain_dimensions)\n",
    "\n",
    "    # first dataset loaded via yt\n",
    "    left_edge = ds.domain_left_edge.convert_to_mks()*1e6\n",
    "    right_edge = ds.domain_right_edge.convert_to_mks()*1e6\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",
    "\n",
    "    # second dataset loaded via yt\n",
    "    left_edge = ds2.domain_left_edge.convert_to_mks()*1e6\n",
    "    right_edge = ds2.domain_right_edge.convert_to_mks()*1e6\n",
    "    field_reformat = field[0].upper() + '_' + field[1]\n",
    "    if slicing_direction == 'x':\n",
    "        n = int( ds2.domain_dimensions[0]//2 )\n",
    "        data2d2 = all_data_level_02[field_reformat][n, :, :]\n",
    "        extent2 = [ left_edge[2], right_edge[2], left_edge[1], right_edge[1] ]\n",
    "    elif slicing_direction == 'y':\n",
    "        n = int( ds2.domain_dimensions[1]//2 )\n",
    "        data2d2 = all_data_level_02[field_reformat][:, n, :]\n",
    "        extent2 = [ left_edge[2], right_edge[2], left_edge[0], right_edge[0] ]\n",
    "    elif slicing_direction == 'z':\n",
    "        n = int( ds2.domain_dimensions[2]//2 )\n",
    "        data2d2 = all_data_level_02[field_reformat][:, :, n]\n",
    "        extent2 = [ left_edge[1], right_edge[1], left_edge[0], right_edge[0] ]\n",
    "\n",
    "    if slicing_direction == 'x':\n",
    "        yaxis_label = 'y'\n",
    "    if slicing_direction == 'y':\n",
    "        yaxis_label = 'x'\n",
    "    if slicing_direction == 'z':\n",
    "        yaxis_label = 'y'\n",
    "\n",
    "#         xlim = [5,25]\n",
    "#         ylim = [-20,20]\n",
    "\n",
    "    plt.clf()\n",
    "    # first dataset\n",
    "    plt.subplot(2,1,1)\n",
    "    plt.title(\"WarpX %s at iteration %d\" %(field, iteration) )\n",
    "    plt.imshow( data2d, interpolation='nearest', cmap='viridis',\n",
    "              origin='lower', extent=extent, aspect='auto')\n",
    "    plt.ylabel(yaxis_label + ' (um)')\n",
    "    vmin, vmax = plt.gci().get_clim()\n",
    "    plt.colorbar()\n",
    "    if 'xlim' in locals():\n",
    "        plt.xlim(xlim)\n",
    "    if 'ylim' in locals():\n",
    "        plt.ylim(ylim)\n",
    "    plt.xticks([])\n",
    "    \n",
    "    # second dataset\n",
    "    plt.subplot(2,1,2)\n",
    "    plt.title(\"Warp %s at iteration %d\" %(field, iteration) )\n",
    "    plt.imshow( data2d2, interpolation='nearest', cmap='viridis',\n",
    "              origin='lower', extent=extent2, aspect='auto',vmin=vmin,vmax=vmax)\n",
    "    plt.colorbar()\n",
    "    if 'xlim' in locals():\n",
    "        plt.xlim(xlim)\n",
    "    if 'ylim' in locals():\n",
    "        plt.ylim(ylim)\n",
    "    plt.xlabel('z (um)')\n",
    "    plt.ylabel(yaxis_label + ' (um)')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Interactive viewer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "iterations = iterations_warp\n",
    "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='jx'),\n",
    "         slicing_direction = RadioButtons( options=[ 'x', 'y', 'z'], value='x'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "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.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}