Skip to content

Commit 601c358

Browse files
author
Matthew Townson
authored
Merge pull request #57 from andrewpaulreeves/master
Inifinite Phase Screen startup performance improvement
2 parents 025e059 + c079baf commit 601c358

File tree

2 files changed

+38
-9
lines changed

2 files changed

+38
-9
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ before_install:
2727
- ls
2828
- conda update --yes conda
2929
install:
30-
- conda install --yes python=$TRAVIS_PYTHON_VERSION numpy scipy nose matplotlib
30+
- conda install --yes python=$TRAVIS_PYTHON_VERSION numpy scipy nose matplotlib numba
3131
- pip install codecov pypandoc
3232
- python setup.py install
3333
script:

aotools/turbulence/infinitephasescreen.py

+37-8
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@
1111
import numpy
1212
from numpy import pi
1313

14+
# Numba compiles python code to machine code for faster execution
15+
try:
16+
import numba
17+
except:
18+
numba = None
19+
20+
1421
from . import phasescreen, turb
1522

1623
__all__ = ["PhaseScreenVonKarman", "PhaseScreenKolmogorov"]
@@ -109,14 +116,19 @@ def calc_seperations(self):
109116
positions = numpy.append(self.stencil_positions, self.X_positions, axis=0)
110117
self.seperations = numpy.zeros((len(positions), len(positions)))
111118

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
116130

117-
delta_r = numpy.sqrt(delta_x ** 2 + delta_y ** 2)
118131

119-
self.seperations[i, j] = delta_r
120132

121133
def make_covmats(self):
122134
"""
@@ -139,7 +151,9 @@ def makeAMatrix(self):
139151
cf = linalg.cho_factor(self.cov_mat_zz)
140152
inv_cov_zz = linalg.cho_solve(cf, numpy.identity(self.cov_mat_zz.shape[0]))
141153
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")
143157

144158
self.A_mat = self.cov_mat_xz.dot(inv_cov_zz)
145159

@@ -398,4 +412,19 @@ def get_new_row(self):
398412

399413
def __repr__(self):
400414
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

Comments
 (0)