Coverage for tests\modelfiles\core_shell_sphere.py: 100%
17 statements
« prev ^ index » next coverage.py v7.5.0, created at 2024-04-30 17:14 +0800
« prev ^ index » next coverage.py v7.5.0, created at 2024-04-30 17:14 +0800
1"""
2A template of hollow sphere math model
3with various sld equal to the x coordinate of certain point
5# ! Do not change the class name and method name !
6"""
8import torch
9from torch import Tensor
10from model2sas import AbstractMathModel
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)
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 '''
24 def __init__(self) -> None:
25 """Define coord and other params here.
26 """
27 self.coord = 'sph'
29 self.R_core: float = 10
30 self.thickness: float = 5
31 self.sld_core: float = -2
32 self.sld_shell: float = 1
34 def bounding_box(self):
35 """re-generate boundary for every method call
36 in case that params are altered in software.
37 return coordinates in Cartesian coordinates.
39 Returns:
40 tuple[float, float, float, float, float, float]: xmin, ymin, zmin, xmax, ymax, zmax
41 """
42 return -(self.R_core + self.thickness), \
43 -(self.R_core + self.thickness), \
44 -(self.R_core + self.thickness), \
45 self.R_core + self.thickness, \
46 self.R_core + self.thickness, \
47 self.R_core + self.thickness
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';
56 Args:
57 u (Tensor): 1st coord
58 v (Tensor): 2nd coord
59 w (Tensor): 3rd coord
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_core)] = self.sld_core
67 sld[(u>=self.R_core) & (u<=(self.R_core+self.thickness))] = self.sld_shell
68 return sld