From a641a0c3b264c060073c4437b4cf3b716303f578 Mon Sep 17 00:00:00 2001 From: Remi Lehe Date: Thu, 26 Oct 2023 11:06:55 -0700 Subject: [PATCH 1/6] Add `show` method to visualize the laser --- docs/source/tutorials/gaussian_laser.ipynb | 30 ++++++++++++++++++++- lasy/laser.py | 31 ++++++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/docs/source/tutorials/gaussian_laser.ipynb b/docs/source/tutorials/gaussian_laser.ipynb index 850bfa26..e9b99f20 100644 --- a/docs/source/tutorials/gaussian_laser.ipynb +++ b/docs/source/tutorials/gaussian_laser.ipynb @@ -78,6 +78,24 @@ "laser = Laser(dimensions,lo,hi,num_points,laser_profile)" ] }, + { + "cell_type": "markdown", + "id": "8ac8f985-4a09-4b9a-a9ff-11ce2ef94caa", + "metadata": {}, + "source": [ + "The laser pulse can be visualized with the `show` method." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "bb8dc634-b3c4-4448-834b-fe9acb6c9645", + "metadata": {}, + "outputs": [], + "source": [ + "laser.show()" + ] + }, { "cell_type": "markdown", "id": "292c6e63-0480-4fb9-b1ad-6b679a3472d0", @@ -97,6 +115,16 @@ "laser.propagate(-z_R) # Propagate the pulse upstream of the focal plane" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "3158e51d-e331-438d-90c1-5f8c63bf9841", + "metadata": {}, + "outputs": [], + "source": [ + "laser.show()" + ] + }, { "cell_type": "markdown", "id": "8df816dc-9b47-4cb0-8716-600352fafb72", @@ -143,7 +171,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.5" + "version": "3.11.6" } }, "nbformat": 4, diff --git a/lasy/laser.py b/lasy/laser.py index 7b7503ea..e093f879 100644 --- a/lasy/laser.py +++ b/lasy/laser.py @@ -276,3 +276,34 @@ def write_to_file( self.profile.pol, save_as_vector_potential, ) + + + def show( self, **kw ): + """ + Show a 2D image of the laser amplitude + + Parameters: + ---------- + **kw: additional arguments to be passed to matplotlib's imshow command + """ + if self.dim == "rt": + # Show field above and below axis, with proper sign + E = [ + np.concatenate( ( (-1)**m * self.grid.field[0,::-1], self.grid.field[0] ) ) \ + for m in self.grid.azimuthal_modes + ] + E = sum(E) + extent = [self.grid.lo[-1], self.grid.hi[-1], -self.grid.hi[0], self.grid.hi[0]] + + else: + # In 3D show an image in the xt plane + i_slice = int(self.grid.field.shape[1]//2) + E = self.grid.field[:,i_slice,:] + extent=[self.grid.lo[-1], self.grid.hi[-1], self.grid.lo[0], self.grid.hi[0]] + + import matplotlib.pyplot as plt + plt.imshow( abs(E), extent=extent, aspect='auto', origin='lower', **kw ) + cb = plt.colorbar() + cb.set_label('$|E_{envelope}|$ (V/m)') + plt.xlabel('t (s)') + plt.ylabel('x (m)') \ No newline at end of file From 8c611351739df2a909af0561085f3691a3a3c1bf Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 26 Oct 2023 18:10:56 +0000 Subject: [PATCH 2/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- lasy/laser.py | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/lasy/laser.py b/lasy/laser.py index e093f879..2713153e 100644 --- a/lasy/laser.py +++ b/lasy/laser.py @@ -277,8 +277,7 @@ def write_to_file( save_as_vector_potential, ) - - def show( self, **kw ): + def show(self, **kw): """ Show a 2D image of the laser amplitude @@ -289,21 +288,34 @@ def show( self, **kw ): if self.dim == "rt": # Show field above and below axis, with proper sign E = [ - np.concatenate( ( (-1)**m * self.grid.field[0,::-1], self.grid.field[0] ) ) \ + np.concatenate( + ((-1) ** m * self.grid.field[0, ::-1], self.grid.field[0]) + ) for m in self.grid.azimuthal_modes ] E = sum(E) - extent = [self.grid.lo[-1], self.grid.hi[-1], -self.grid.hi[0], self.grid.hi[0]] + extent = [ + self.grid.lo[-1], + self.grid.hi[-1], + -self.grid.hi[0], + self.grid.hi[0], + ] else: # In 3D show an image in the xt plane - i_slice = int(self.grid.field.shape[1]//2) - E = self.grid.field[:,i_slice,:] - extent=[self.grid.lo[-1], self.grid.hi[-1], self.grid.lo[0], self.grid.hi[0]] + i_slice = int(self.grid.field.shape[1] // 2) + E = self.grid.field[:, i_slice, :] + extent = [ + self.grid.lo[-1], + self.grid.hi[-1], + self.grid.lo[0], + self.grid.hi[0], + ] import matplotlib.pyplot as plt - plt.imshow( abs(E), extent=extent, aspect='auto', origin='lower', **kw ) + + plt.imshow(abs(E), extent=extent, aspect="auto", origin="lower", **kw) cb = plt.colorbar() - cb.set_label('$|E_{envelope}|$ (V/m)') - plt.xlabel('t (s)') - plt.ylabel('x (m)') \ No newline at end of file + cb.set_label("$|E_{envelope}|$ (V/m)") + plt.xlabel("t (s)") + plt.ylabel("x (m)") From 8c4ba85d579aee55c59122c5ddbaff63d59f9713 Mon Sep 17 00:00:00 2001 From: Remi Lehe Date: Thu, 26 Oct 2023 11:12:32 -0700 Subject: [PATCH 3/6] Fix doc --- lasy/laser.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lasy/laser.py b/lasy/laser.py index 2713153e..0e7d9520 100644 --- a/lasy/laser.py +++ b/lasy/laser.py @@ -279,9 +279,9 @@ def write_to_file( def show(self, **kw): """ - Show a 2D image of the laser amplitude + Show a 2D image of the laser amplitude. - Parameters: + Parameters ---------- **kw: additional arguments to be passed to matplotlib's imshow command """ From 773381f7693c2ad5e1c0d64868d23103327ecd62 Mon Sep 17 00:00:00 2001 From: Remi Lehe Date: Thu, 26 Oct 2023 11:24:04 -0700 Subject: [PATCH 4/6] Apply suggestions from code review --- lasy/laser.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lasy/laser.py b/lasy/laser.py index 0e7d9520..8a2e70c4 100644 --- a/lasy/laser.py +++ b/lasy/laser.py @@ -286,7 +286,7 @@ def show(self, **kw): **kw: additional arguments to be passed to matplotlib's imshow command """ if self.dim == "rt": - # Show field above and below axis, with proper sign + # Show field in the plane y=0, above and below axis, with proper sign for each mode E = [ np.concatenate( ((-1) ** m * self.grid.field[0, ::-1], self.grid.field[0]) From 88474053993f59c7467cfd3de7e1aea356858330 Mon Sep 17 00:00:00 2001 From: Remi Lehe Date: Wed, 1 Nov 2023 06:57:17 -0600 Subject: [PATCH 5/6] Update lasy/laser.py --- lasy/laser.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lasy/laser.py b/lasy/laser.py index 8a2e70c4..1a036c08 100644 --- a/lasy/laser.py +++ b/lasy/laser.py @@ -293,7 +293,7 @@ def show(self, **kw): ) for m in self.grid.azimuthal_modes ] - E = sum(E) + E = sum(E) # Sum all the modes extent = [ self.grid.lo[-1], self.grid.hi[-1], From 2fcb6737c1b7b204e690f24cc31300597b0e9c22 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 1 Nov 2023 12:57:25 +0000 Subject: [PATCH 6/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- lasy/laser.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lasy/laser.py b/lasy/laser.py index 1a036c08..f7eff8a5 100644 --- a/lasy/laser.py +++ b/lasy/laser.py @@ -293,7 +293,7 @@ def show(self, **kw): ) for m in self.grid.azimuthal_modes ] - E = sum(E) # Sum all the modes + E = sum(E) # Sum all the modes extent = [ self.grid.lo[-1], self.grid.hi[-1],