1
1
import numpy as np
2
2
3
3
from .utils import gen_rot
4
- from .exceptions import NotOnSurfaceError
4
+ from .exceptions import NotOnSurfaceError , InvalidGeometry
5
5
from .index import glass_index
6
6
7
- from typing import Dict , List , Tuple
7
+ from typing import Dict , List , Tuple , Union , Optional
8
8
9
9
class geometry :
10
10
"""Class for the different surfaces in an optical system.
@@ -47,16 +47,24 @@ class geometry:
47
47
"""
48
48
49
49
def __init__ (self , params : Dict ):
50
- self .P = params ['P' ]
51
- self .D = np .array (params .get ('D' , [0. , 0. , 0. ]))
52
- self .action = params ['action' ]
53
- self .Diam = params ['Diam' ]
54
- self .N = params .get ('N' , 1. )
55
- self .kappa = params .get ('kappa' , None )
56
- self .diam = params .get ('diam' , 0. )
57
- self .c = params .get ('c' , 0. )
58
- self .name = params .get ('name' , None )
59
- self .R = gen_rot (self .D )
50
+ P = params .get ('P' )
51
+ # Allow on axis integer for P.
52
+ if isinstance (P , float ) or isinstance (P , int ):
53
+ P = np .array ([0. , 0. , P ])
54
+ elif isinstance (P , List ):
55
+ P = np .array (P )
56
+ else :
57
+ raise InvalidGeometry ()
58
+ self .P : np .ndarray = np .array (P )
59
+ self .D : np .ndarray = np .array (params .get ('D' , [0. , 0. , 0. ]))
60
+ self .action : str = params ['action' ]
61
+ self .Diam : Union [float , int ] = params ['Diam' ]
62
+ self .N : Union [float , int ] = params .get ('N' , 1. )
63
+ self .kappa : Optional [Union [float , int ]] = params .get ('kappa' )
64
+ self .diam : Union [float , int ] = params .get ('diam' , 0. )
65
+ self .c : Union [float , int ] = params .get ('c' , 0. )
66
+ self .name : str = params .get ('name' , None )
67
+ self .R : np .ndarray = gen_rot (self .D )
60
68
if params .get ('glass' ):
61
69
self .glass = glass_index (params .get ('glass' ))
62
70
self .check_params ()
@@ -66,22 +74,15 @@ def check_params(self) -> None:
66
74
67
75
Summary
68
76
-------
69
- If P is given as a float/int then it is converted to a np array
70
- with that float/int in the Z direction. If c != 0 (in the case
71
- of a conic) then kappa must be specified, and if kappa is greater
72
- than 0 then the value of c is redundant by boundary conditions of
73
- the conic equation. Lastly, if c == 0 in the case of a planar
74
- surface the None value of kappa needs to be set to a dummy value
75
- to avoid exceptions in calculating the conic equation. Note that
76
- this does not affect the calculation since c is 0.
77
+ If c != 0 (in the case of a conic) then kappa must be specified,
78
+ and if kappa is greater than 0 then the value of c is redundant
79
+ by boundary conditions of the conic equation. Lastly, if c == 0 in
80
+ the case of a planar surface the None value of kappa needs to be set
81
+ to a dummy value to avoid exceptions in calculating the conic equation.
82
+ Note that this does not affect the calculation since c is 0.
77
83
78
84
"""
79
85
80
- if isinstance (self .P , float ) or isinstance (self .P , int ):
81
- #Allow on axis integer for P.
82
- self .P = np .array ([0. , 0. , self .P ])
83
- else :
84
- self .P = np .array (self .P )
85
86
if self .c != 0 :
86
87
if self .kappa is None :
87
88
raise Exception ("Specify a kappa for this conic." )
@@ -131,6 +132,9 @@ def conics(self, point: np.ndarray) -> Tuple[float, List[float]]:
131
132
rho = np .sqrt (pow (X ,2 ) + pow (Y , 2 ))
132
133
if rho > self .Diam / 2. or rho < self .diam / 2. :
133
134
raise NotOnSurfaceError ()
135
+ # Ensure kappa is not None before using it in calculations
136
+ if self .kappa is None :
137
+ raise ValueError ("kappa must not be None for conic calculations" )
134
138
#Conic equation.
135
139
function = Z - self .c * pow (rho , 2 )/ (1 + pow ((1 - self .kappa * pow (self .c , 2 )* pow (rho ,2 )), 0.5 ))
136
140
#See Spencer, Murty section on rotational surfaces for definition of E.
@@ -160,5 +164,8 @@ def conics_plot(self, point: np.ndarray) -> np.ndarray:
160
164
nan_idx = (rho > self .Diam / 2. ) + (rho < self .diam / 2. )
161
165
rho = np .sqrt (pow (X [~ nan_idx ],2 ) + pow (Y [~ nan_idx ], 2 ))
162
166
function [nan_idx ] = np .nan
167
+ # Ensure kappa is not None before using it in calculations
168
+ if self .kappa is None :
169
+ raise ValueError ("kappa must not be None for conic plot calculations" )
163
170
function [~ nan_idx ] = self .c * pow (rho , 2 )/ (1 + pow ((1 - self .kappa * pow (self .c , 2 )* pow (rho ,2 )), 0.5 ))
164
171
return function
0 commit comments