From 3644ec760a76fc23575372e52c138dae193fddbc Mon Sep 17 00:00:00 2001 From: nikhitha79 <115790230+nikhitha79@users.noreply.github.com> Date: Fri, 4 Oct 2024 17:26:05 +0530 Subject: [PATCH 1/2] Create Bragg_angle.py --- physics/Bragg_angle.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 physics/Bragg_angle.py diff --git a/physics/Bragg_angle.py b/physics/Bragg_angle.py new file mode 100644 index 000000000000..050f097b72d9 --- /dev/null +++ b/physics/Bragg_angle.py @@ -0,0 +1,40 @@ +import math +def bragg_angle(distance: float, order: int, wavelength: float) -> float: + """ + Calculate the Bragg diffraction angle using the formula: + sin(θ) = (n * λ) / (2 * d) + + Parameters: + distance d (float): Distance between crystal planes (in meters). + order n (int): Order of reflection. + wavelength λ (float): Wavelength of the radiation (in meters). + + Returns: + float: The Bragg diffraction angle θ in degrees, rounded to the specified number of decimal places. + + Examples: + >>> bragg_angle(2.2e-10, 1, 2.2e-10) + 30.0 + + >>> bragg_angle(5e-10, 2, 1e-10) + 11.5 + + >>> bragg_angle(4e-10, 1, 4e-10) + 30.0 + + # Test case for an invalid sine value (out of range) + >>> bragg_angle(1e-10, 2, 3e-10) + Traceback (most recent call last): + ... + ValueError: The calculated sine value is out of the valid range. + """ + sin_theta = (order * wavelength) / (2 * distance) + if sin_theta > 1 or sin_theta < -1: + raise ValueError("The calculated sine value is out of the valid range.") + theta_radians = math.asin(sin_theta) + theta_degrees = math.degrees(theta_radians) + return round(theta_degrees, 1) + +if __name__ == "__main__": + import doctest + doctest.testmod(verbose=True) From a0065449458f7c7cd558cb7da3a3d31524f54d05 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 4 Oct 2024 11:57:54 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- physics/Bragg_angle.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/physics/Bragg_angle.py b/physics/Bragg_angle.py index 050f097b72d9..248a8205043d 100644 --- a/physics/Bragg_angle.py +++ b/physics/Bragg_angle.py @@ -1,27 +1,29 @@ import math + + def bragg_angle(distance: float, order: int, wavelength: float) -> float: """ Calculate the Bragg diffraction angle using the formula: sin(θ) = (n * λ) / (2 * d) - + Parameters: distance d (float): Distance between crystal planes (in meters). order n (int): Order of reflection. wavelength λ (float): Wavelength of the radiation (in meters). - + Returns: float: The Bragg diffraction angle θ in degrees, rounded to the specified number of decimal places. - + Examples: >>> bragg_angle(2.2e-10, 1, 2.2e-10) 30.0 - + >>> bragg_angle(5e-10, 2, 1e-10) 11.5 - + >>> bragg_angle(4e-10, 1, 4e-10) 30.0 - + # Test case for an invalid sine value (out of range) >>> bragg_angle(1e-10, 2, 3e-10) Traceback (most recent call last): @@ -35,6 +37,8 @@ def bragg_angle(distance: float, order: int, wavelength: float) -> float: theta_degrees = math.degrees(theta_radians) return round(theta_degrees, 1) + if __name__ == "__main__": import doctest + doctest.testmod(verbose=True)