Skip to content

Commit

Permalink
Better handling of near singular matrices in GARCLED.py
Browse files Browse the repository at this point in the history
  • Loading branch information
pbienst committed Mar 4, 2008
1 parent 4806b1a commit 74eec48
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
2 changes: 2 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
- Better handling of near-singular matrices in GARCLED.py.

CAMFR 1.3:

- Added code to model spontaneous emission and light extraction in light
Expand Down
34 changes: 31 additions & 3 deletions camfr/GARCLED.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,34 @@ def horizontal_y(cav, kx0, ky0):



#############################################################################
#
# Safe matrix inversion, uses svd if matrix is singular.
#
#############################################################################

def inv_safe(A):

try:

return inv(A)

except:

print "Singularities dectected in matrix inversion."

# A = U * sigma * Vh

U, sigma, Vh = linalg.svd(A)

# inv_A = V * 1/sigma * Uh, but set 1/0 in sigma to 0.

inv_sigma = where(abs(sigma) <= 1e-11, 0, 1/sigma)

return dot(Vh.conj().T, dot(diag(inv_sigma), U.conj().T))



#############################################################################
#
# Calculate extracted field through a cavity, but only for waves
Expand Down Expand Up @@ -306,8 +334,8 @@ def P(cav, sources, kx0, ky0, single_pass_substrate=True, add_all=True,

N = cav.top.inc().N()

inv_1 = inv(identity(N) - dot(R_bot, R_top))
inv_2 = inv(identity(N) - dot(R_top, R_bot))
inv_1 = inv_safe(identity(N) - dot(R_bot, R_top))
inv_2 = inv_safe(identity(N) - dot(R_top, R_bot))

# Loop over the different sources.

Expand Down Expand Up @@ -398,7 +426,7 @@ def P(cav, sources, kx0, ky0, single_pass_substrate=True, add_all=True,
# Calculate transmitted power density.

if len(propagating) > 0: # LAPACK crashes on empty matrix.
inv_ = inv(identity(len(propagating)) - dot(R_cav, R_sub))
inv_ = inv_safe(identity(len(propagating)) - dot(R_cav, R_sub))
P_out = dot(T_sub, dot(inv_, P_sub_))
else:
P_out = [0.0]
Expand Down

0 comments on commit 74eec48

Please sign in to comment.