Coverage for tests\modelfiles\cylinder.py: 100%

15 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-05-09 17:12 +0800

1""" 

2A template of hollow sphere math model 

3with various sld equal to the x coordinate of certain point 

4 

5# ! Do not change the class name and method name ! 

6""" 

7 

8import torch 

9from torch import Tensor 

10from model2sas import AbstractMathModel 

11 

12 

13class MathModel(AbstractMathModel): 

14 '''to generate a 3D model from a mathematical description. 

15 For example: a spherical shell is "x**2+y**2+z**2 >= R_core**2 and x**2+y**2+z**2 <= (R_core+thickness)**2 

16 also, in spherical coordinates, a hollow sphere is (r >= R_core) and (r <= R_core+thickness) 

17 

18 coord: 

19 - 'car' in (x, y, z) 

20 - 'sph' in (r, theta, phi), theta: [0, 2pi) ; phi: [0, pi) 

21 - 'cyl' in (rho, theta, z), theta: [0, 2pi) 

22 ''' 

23 

24 def __init__(self) -> None: 

25 """Define coord and other params here. 

26 """ 

27 self.coord = 'cyl' 

28 

29 self.R: float = 10 

30 self.H: float = 30 

31 self.sld_value: float = 1 

32 

33 def bounding_box(self): 

34 """re-generate boundary for every method call 

35 in case that params are altered in software. 

36 return coordinates in Cartesian coordinates. 

37 

38 Returns: 

39 tuple[float, float, float, float, float, float]: xmin, ymin, zmin, xmax, ymax, zmax 

40 """ 

41 return \ 

42 -self.R, \ 

43 -self.R, \ 

44 -self.H/2, \ 

45 self.R, \ 

46 self.R, \ 

47 self.H/2 

48 

49 def sld(self, u: Tensor, v: Tensor, w: Tensor) -> Tensor: 

50 """Calculate sld values of certain coordinates. 

51 u, v, w means: 

52 x, y, z if self.coord=='car'; 

53 r, theta, phi if self.coord=='sph'; 

54 rho, theta, z if self.coord=='cyl'; 

55 

56 Args: 

57 u (Tensor): 1st coord 

58 v (Tensor): 2nd coord 

59 w (Tensor): 3rd coord 

60 

61 Returns: 

62 Tensor: sld values of each coordinates 

63 """ 

64 # u, v, w is r, theta, phi here for 'sph' coordinates 

65 sld = torch.zeros_like(u) 

66 sld[(u<=self.R)&(w>=-self.H/2)&(w<=self.H/2)] = self.sld_value 

67 return sld