|
| 1 | +""" |
| 2 | +Find the Bragg angle of a crystal given the wavelength of radiation, distane between the planes of crystal and the order of diffraction. |
| 3 | + |
| 4 | +In many areas of science, Bragg's law, Wulff–Bragg's condition, or Laue–Bragg interference are a special case of Laue diffraction, |
| 5 | +giving the angles for coherent scattering of waves from a large crystal lattice. |
| 6 | +It describes how the superposition of wave fronts scattered by lattice planes leads to a strict relation between the wavelength and scattering angle. |
| 7 | +This law was initially formulated for X-rays, but it also applies to all types of matter waves including neutron and electron waves if there are a large number of atoms, |
| 8 | +as well as visible light with artificial periodic microscale lattices. |
| 9 | + |
| 10 | +Reference: https://en.wikipedia.org/wiki/Bragg%27s_law |
| 11 | + |
| 12 | +""" |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +import math |
| 17 | + |
| 18 | +def bragg_angle(distance: float, n: int, wavelength: float) -> float: |
| 19 | + """ |
| 20 | + Calculate the Bragg diffraction angle in degrees. |
| 21 | + |
| 22 | + The Bragg diffraction angle is given by |
| 23 | + sin(θ) = (n * λ) / (2 * d) |
| 24 | + |
| 25 | + Parameters: |
| 26 | + distance (float): Distance between crystal planes (in meters). |
| 27 | + n (int): Order of reflection. |
| 28 | + wavelength (float): Wavelength of the radiation (in meters). |
| 29 | + |
| 30 | + Returns: |
| 31 | + float: The Bragg diffraction angle θ in degrees. |
| 32 | + |
| 33 | + Example: |
| 34 | + >>> bragg_angle(2.2e-10, 1, 2.2e-10) |
| 35 | + 30.0 |
| 36 | + |
| 37 | + >>> bragg_angle(5.0e-10, 2, 1.0e-10) |
| 38 | + 11.5 |
| 39 | + |
| 40 | + >>> bragg_angle(4.0e-10, 1, 8.0e-10) |
| 41 | + 90.0 |
| 42 | + |
| 43 | + # Test case for an invalid sine value that is out of range |
| 44 | + >>> bragg_angle(1e-10, 2, 3e-10) |
| 45 | + Traceback (most recent call last): |
| 46 | + ... |
| 47 | + ValueError: The calculated sine value is out of the valid range. |
| 48 | + """ |
| 49 | + |
| 50 | + sine_theta = (n * wavelength) / (2 * distance) |
| 51 | + |
| 52 | + if sine_theta > 1 or sine_theta < -1: |
| 53 | + raise ValueError("The calculated sine value is out of the valid range.") |
| 54 | + |
| 55 | + theta_radians = math.asin(sine_theta) |
| 56 | + |
| 57 | + theta_degrees = math.degrees(theta_radians) |
| 58 | + |
| 59 | + return round(theta_degrees, 1) |
| 60 | + |
| 61 | + |
| 62 | +if __name__ == "__main__": |
| 63 | + import doctest |
| 64 | + doctest.testmod(verbose=True) |
0 commit comments