11
11
import numpy
12
12
from numpy import pi
13
13
14
+ # Numba compiles python code to machine code for faster execution
15
+ try :
16
+ import numba
17
+ except :
18
+ numba = None
19
+
20
+
14
21
from . import phasescreen , turb
15
22
16
23
__all__ = ["PhaseScreenVonKarman" , "PhaseScreenKolmogorov" ]
@@ -109,14 +116,19 @@ def calc_seperations(self):
109
116
positions = numpy .append (self .stencil_positions , self .X_positions , axis = 0 )
110
117
self .seperations = numpy .zeros ((len (positions ), len (positions )))
111
118
112
- for i , (x1 , y1 ) in enumerate (positions ):
113
- for j , (x2 , y2 ) in enumerate (positions ):
114
- delta_x = x2 - x1
115
- delta_y = y2 - y1
119
+ if numba :
120
+ calc_seperations_fast (positions , self .seperations )
121
+ else :
122
+ for i , (x1 , y1 ) in enumerate (positions ):
123
+ for j , (x2 , y2 ) in enumerate (positions ):
124
+ delta_x = x2 - x1
125
+ delta_y = y2 - y1
126
+
127
+ delta_r = numpy .sqrt (delta_x ** 2 + delta_y ** 2 )
128
+
129
+ self .seperations [i , j ] = delta_r
116
130
117
- delta_r = numpy .sqrt (delta_x ** 2 + delta_y ** 2 )
118
131
119
- self .seperations [i , j ] = delta_r
120
132
121
133
def make_covmats (self ):
122
134
"""
@@ -139,7 +151,9 @@ def makeAMatrix(self):
139
151
cf = linalg .cho_factor (self .cov_mat_zz )
140
152
inv_cov_zz = linalg .cho_solve (cf , numpy .identity (self .cov_mat_zz .shape [0 ]))
141
153
except linalg .LinAlgError :
142
- raise linalg .LinAlgError ("Could not invert Covariance Matrix to for A and B Matrices. Try with a larger pixel scale" )
154
+ # print("Cholesky solve failed. Performing SVD inversion...")
155
+ # inv_cov_zz = numpy.linalg.pinv(self.cov_mat_zz)
156
+ raise linalg .LinAlgError ("Could not invert Covariance Matrix to for A and B Matrices. Try with a larger pixel scale or smaller L0" )
143
157
144
158
self .A_mat = self .cov_mat_xz .dot (inv_cov_zz )
145
159
@@ -398,4 +412,19 @@ def get_new_row(self):
398
412
399
413
def __repr__ (self ):
400
414
return str (self .scrn )
401
-
415
+
416
+
417
+
418
+ @numba .jit (nopython = True , parallel = True )
419
+ def calc_seperations_fast (positions , seperations ):
420
+
421
+ for i in numba .prange (len (positions )):
422
+ x1 , y1 = positions [i ]
423
+ for j in range (len (positions )):
424
+ x2 , y2 = positions [j ]
425
+ delta_x = x2 - x1
426
+ delta_y = y2 - y1
427
+
428
+ delta_r = numpy .sqrt (delta_x ** 2 + delta_y ** 2 )
429
+
430
+ seperations [i , j ] = delta_r
0 commit comments