From 4f04f544c9c8f88b2ce78cf7dace0612fc3ef17f Mon Sep 17 00:00:00 2001 From: Kale Weichman Date: Thu, 19 Dec 2024 17:10:40 -0500 Subject: [PATCH 1/7] add function to normalize average intensity --- lasy/laser.py | 7 +++++-- lasy/utils/laser_utils.py | 25 ++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/lasy/laser.py b/lasy/laser.py index 1c240c8f..ea9ae1c1 100644 --- a/lasy/laser.py +++ b/lasy/laser.py @@ -7,6 +7,7 @@ normalize_energy, normalize_peak_field_amplitude, normalize_peak_intensity, + normalize_average_intensity, ) from lasy.utils.openpmd_output import write_to_openpmd_file @@ -147,7 +148,7 @@ def __init__( def normalize(self, value, kind="energy"): """ - Normalize the pulse either to the energy, peak field amplitude or peak intensity. + Normalize the pulse either to the energy, peak field amplitude, peak intensity, or average intensity. Parameters ---------- @@ -155,7 +156,7 @@ def normalize(self, value, kind="energy"): Value to which to normalize the field property that is defined in ``kind`` kind : string (optional) Distance by which the laser pulse should be propagated - Options: ``'energy``', ``'field'``, ``'intensity'`` (default is ``'energy'``) + Options: ``'energy``', ``'field'``, ``'intensity'``, ``'average_intensity'`` (default is ``'energy'``) """ if kind == "energy": normalize_energy(self.dim, value, self.grid) @@ -163,6 +164,8 @@ def normalize(self, value, kind="energy"): normalize_peak_field_amplitude(value, self.grid) elif kind == "intensity": normalize_peak_intensity(value, self.grid) + elif kind == "average_intensity": + normalize_average_intensity(value, self.grid) else: raise ValueError(f'kind "{kind}" not recognized') diff --git a/lasy/utils/laser_utils.py b/lasy/utils/laser_utils.py index 55be1d3c..df10c396 100644 --- a/lasy/utils/laser_utils.py +++ b/lasy/utils/laser_utils.py @@ -130,8 +130,31 @@ def normalize_peak_intensity(peak_intensity, grid): if input_peak_intensity == 0.0: print("Field is zero everywhere, normalization will be skipped") else: - grid.set_temporal_field(np.sqrt(peak_intensity / input_peak_intensity)) + field *= np.sqrt(peak_intensity / input_peak_intensity) + grid.set_temporal_field(field) + +def normalize_average_intensity(average_intensity, grid): + """ + Normalize energy of the laser pulse contained in grid. + Parameters + ---------- + average_intensity : scalar (W/m^2) + Average field amplitude of the laser pulse after normalization. + + grid : a Grid object + Contains value of the laser envelope and metadata. + """ + if average_intensity is not None: + field = grid.get_temporal_field() + intensity = np.abs(epsilon_0 * field**2 / 2 * c) + input_average_intensity = intensity.mean() + if input_average_intensity == 0.0: + print("Field is zero everywhere, normalization will be skipped") + else: + field *= np.sqrt(average_intensity / input_average_intensity) + grid.set_temporal_field(field) + def get_full_field(laser, theta=0, slice=0, slice_axis="x", Nt=None): """ From dd90c316423fb599e33b9c027fc6179760ca31c9 Mon Sep 17 00:00:00 2001 From: Kale Weichman Date: Fri, 20 Dec 2024 16:23:33 -0500 Subject: [PATCH 2/7] added tests for laser normalization --- tests/test_laser_utils.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/test_laser_utils.py b/tests/test_laser_utils.py index 0fbe926a..34b9f154 100644 --- a/tests/test_laser_utils.py +++ b/tests/test_laser_utils.py @@ -1,4 +1,5 @@ import numpy as np +from scipy.constants import c, epsilon_0 from lasy.laser import Laser from lasy.profiles.gaussian_profile import GaussianProfile @@ -47,6 +48,25 @@ def test_laser_analysis_utils(): tau_rms = get_duration(laser.grid, dim) np.testing.assert_approx_equal(2 * tau_rms, laser.profile.tau, significant=3) + # Check functionality of normalization functions + laser.normalize(1,kind="energy") + energy = compute_laser_energy(dim, laser.grid) + np.testing.assert_approx_equal(1, np.abs(field.max()), significant=10) + # peak amplitude + laser.normalize(1,kind="field") + field = laser.grid.get_temporal_field() + np.testing.assert_approx_equal(1, np.abs(field.max()), significant=10) + # peak intensity + laser.normalize(1,kind="intensity") + field = laser.grid.get_temporal_field() + intensity = np.abs(epsilon_0 * field**2 / 2 * c) + np.testing.assert_approx_equal(1, intensity.max(), significant=10) + # average intensity + laser.normalize(1,kind="average_intensity") + field = laser.grid.get_temporal_field() + intensity = np.abs(epsilon_0 * field**2 / 2 * c) + np.testing.assert_approx_equal(1, intensity.mean(), significant=10) + if __name__ == "__main__": test_laser_analysis_utils() From 5f442fec0c2a20788ca532222202c95f9107fa6c Mon Sep 17 00:00:00 2001 From: Kale Weichman Date: Fri, 20 Dec 2024 16:31:40 -0500 Subject: [PATCH 3/7] fixed bug --- tests/test_laser_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_laser_utils.py b/tests/test_laser_utils.py index 34b9f154..7d794dce 100644 --- a/tests/test_laser_utils.py +++ b/tests/test_laser_utils.py @@ -51,7 +51,7 @@ def test_laser_analysis_utils(): # Check functionality of normalization functions laser.normalize(1,kind="energy") energy = compute_laser_energy(dim, laser.grid) - np.testing.assert_approx_equal(1, np.abs(field.max()), significant=10) + np.testing.assert_approx_equal(1, energy, significant=10) # peak amplitude laser.normalize(1,kind="field") field = laser.grid.get_temporal_field() From f78ab145e15ca8c5f8ba2aa8d9c4fb55751c6a01 Mon Sep 17 00:00:00 2001 From: Kale Weichman Date: Fri, 20 Dec 2024 16:43:07 -0500 Subject: [PATCH 4/7] cleaned up tests --- tests/test_laser_utils.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/tests/test_laser_utils.py b/tests/test_laser_utils.py index 7d794dce..c521824a 100644 --- a/tests/test_laser_utils.py +++ b/tests/test_laser_utils.py @@ -48,20 +48,29 @@ def test_laser_analysis_utils(): tau_rms = get_duration(laser.grid, dim) np.testing.assert_approx_equal(2 * tau_rms, laser.profile.tau, significant=3) - # Check functionality of normalization functions + +def test_laser_normalization_utils(): + """Test the different laser normalization utilities in both geometries.""" + for dim in ["xyt", "rt"]: + laser = get_gaussian_laser(dim) + + # Check energy normalization laser.normalize(1,kind="energy") energy = compute_laser_energy(dim, laser.grid) np.testing.assert_approx_equal(1, energy, significant=10) - # peak amplitude + + # Check peak field normalization laser.normalize(1,kind="field") field = laser.grid.get_temporal_field() np.testing.assert_approx_equal(1, np.abs(field.max()), significant=10) - # peak intensity + + # Check peak intensity normalization laser.normalize(1,kind="intensity") field = laser.grid.get_temporal_field() intensity = np.abs(epsilon_0 * field**2 / 2 * c) np.testing.assert_approx_equal(1, intensity.max(), significant=10) - # average intensity + + # Check average intensity normalization laser.normalize(1,kind="average_intensity") field = laser.grid.get_temporal_field() intensity = np.abs(epsilon_0 * field**2 / 2 * c) @@ -70,3 +79,4 @@ def test_laser_analysis_utils(): if __name__ == "__main__": test_laser_analysis_utils() + test_laser_normalization_utils() From 6f43b9a20b894185afcb1da348ca14692ad33202 Mon Sep 17 00:00:00 2001 From: Kale Weichman Date: Fri, 20 Dec 2024 16:51:48 -0500 Subject: [PATCH 5/7] added clarification in documentation --- lasy/laser.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lasy/laser.py b/lasy/laser.py index ea9ae1c1..d9427f61 100644 --- a/lasy/laser.py +++ b/lasy/laser.py @@ -157,6 +157,10 @@ def normalize(self, value, kind="energy"): kind : string (optional) Distance by which the laser pulse should be propagated Options: ``'energy``', ``'field'``, ``'intensity'``, ``'average_intensity'`` (default is ``'energy'``) + + Notes + ----- + The normalization to average intensity operates on the intensity envelope """ if kind == "energy": normalize_energy(self.dim, value, self.grid) From 5957b9c2e64f2b85846babed4df4864b2dc7581d Mon Sep 17 00:00:00 2001 From: Kale Weichman Date: Fri, 20 Dec 2024 17:02:59 -0500 Subject: [PATCH 6/7] cleaned up documentation --- lasy/laser.py | 6 +----- lasy/utils/laser_utils.py | 4 ++-- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/lasy/laser.py b/lasy/laser.py index d9427f61..ded36110 100644 --- a/lasy/laser.py +++ b/lasy/laser.py @@ -148,7 +148,7 @@ def __init__( def normalize(self, value, kind="energy"): """ - Normalize the pulse either to the energy, peak field amplitude, peak intensity, or average intensity. + Normalize the pulse either to the energy, peak field amplitude, peak intensity, or average intensity. The average intensity option operates on the envelope. Parameters ---------- @@ -157,10 +157,6 @@ def normalize(self, value, kind="energy"): kind : string (optional) Distance by which the laser pulse should be propagated Options: ``'energy``', ``'field'``, ``'intensity'``, ``'average_intensity'`` (default is ``'energy'``) - - Notes - ----- - The normalization to average intensity operates on the intensity envelope """ if kind == "energy": normalize_energy(self.dim, value, self.grid) diff --git a/lasy/utils/laser_utils.py b/lasy/utils/laser_utils.py index df10c396..d76cee8f 100644 --- a/lasy/utils/laser_utils.py +++ b/lasy/utils/laser_utils.py @@ -118,7 +118,7 @@ def normalize_peak_intensity(peak_intensity, grid): Parameters ---------- peak_intensity : scalar (W/m^2) - Peak field amplitude of the laser pulse after normalization. + Peak intensity of the laser pulse after normalization. grid : a Grid object Contains value of the laser envelope and metadata. @@ -140,7 +140,7 @@ def normalize_average_intensity(average_intensity, grid): Parameters ---------- average_intensity : scalar (W/m^2) - Average field amplitude of the laser pulse after normalization. + Average intensity of the laser pulse envelope after normalization. grid : a Grid object Contains value of the laser envelope and metadata. From d923883d3e00c5a983a2377a006f2076f7106a5e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 20 Dec 2024 22:16:36 +0000 Subject: [PATCH 7/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- lasy/laser.py | 2 +- lasy/utils/laser_utils.py | 3 ++- tests/test_laser_utils.py | 18 +++++++++--------- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/lasy/laser.py b/lasy/laser.py index ded36110..36273daa 100644 --- a/lasy/laser.py +++ b/lasy/laser.py @@ -4,10 +4,10 @@ from lasy.utils.grid import Grid, time_axis_indx from lasy.utils.laser_utils import ( + normalize_average_intensity, normalize_energy, normalize_peak_field_amplitude, normalize_peak_intensity, - normalize_average_intensity, ) from lasy.utils.openpmd_output import write_to_openpmd_file diff --git a/lasy/utils/laser_utils.py b/lasy/utils/laser_utils.py index d76cee8f..f9de3aa7 100644 --- a/lasy/utils/laser_utils.py +++ b/lasy/utils/laser_utils.py @@ -133,6 +133,7 @@ def normalize_peak_intensity(peak_intensity, grid): field *= np.sqrt(peak_intensity / input_peak_intensity) grid.set_temporal_field(field) + def normalize_average_intensity(average_intensity, grid): """ Normalize energy of the laser pulse contained in grid. @@ -154,7 +155,7 @@ def normalize_average_intensity(average_intensity, grid): else: field *= np.sqrt(average_intensity / input_average_intensity) grid.set_temporal_field(field) - + def get_full_field(laser, theta=0, slice=0, slice_axis="x", Nt=None): """ diff --git a/tests/test_laser_utils.py b/tests/test_laser_utils.py index c521824a..5446622f 100644 --- a/tests/test_laser_utils.py +++ b/tests/test_laser_utils.py @@ -48,34 +48,34 @@ def test_laser_analysis_utils(): tau_rms = get_duration(laser.grid, dim) np.testing.assert_approx_equal(2 * tau_rms, laser.profile.tau, significant=3) - + def test_laser_normalization_utils(): """Test the different laser normalization utilities in both geometries.""" for dim in ["xyt", "rt"]: laser = get_gaussian_laser(dim) # Check energy normalization - laser.normalize(1,kind="energy") + laser.normalize(1, kind="energy") energy = compute_laser_energy(dim, laser.grid) np.testing.assert_approx_equal(1, energy, significant=10) - + # Check peak field normalization - laser.normalize(1,kind="field") + laser.normalize(1, kind="field") field = laser.grid.get_temporal_field() np.testing.assert_approx_equal(1, np.abs(field.max()), significant=10) - + # Check peak intensity normalization - laser.normalize(1,kind="intensity") + laser.normalize(1, kind="intensity") field = laser.grid.get_temporal_field() intensity = np.abs(epsilon_0 * field**2 / 2 * c) np.testing.assert_approx_equal(1, intensity.max(), significant=10) - + # Check average intensity normalization - laser.normalize(1,kind="average_intensity") + laser.normalize(1, kind="average_intensity") field = laser.grid.get_temporal_field() intensity = np.abs(epsilon_0 * field**2 / 2 * c) np.testing.assert_approx_equal(1, intensity.mean(), significant=10) - + if __name__ == "__main__": test_laser_analysis_utils()