Skip to content

Commit

Permalink
Fixed issue seen for local trapping theorem with enstrophy on the von…
Browse files Browse the repository at this point in the history
… karman street. Think the problem is just very ill posed to switched to using pinv from cholesky for the coefficient solve. This is heuristic so added a warning too. Reran all the trapping notebooks to verify they were all working well. Found bug on mac that complains about qdldl package, introduced in osqp 0.6.2 (part of cvxpy) that I cannot get around, so reduced the cvxpy (and therefore the osqp) requirements. Added sympy requirement to dev.
  • Loading branch information
akaptano committed Jan 11, 2024
1 parent 8d46f27 commit d6ee083
Show file tree
Hide file tree
Showing 7 changed files with 612 additions and 636 deletions.
496 changes: 209 additions & 287 deletions examples/8_trapping_sindy_examples/dysts_examples.ipynb

Large diffs are not rendered by default.

284 changes: 135 additions & 149 deletions examples/8_trapping_sindy_examples/trapping_extended.ipynb

Large diffs are not rendered by default.

222 changes: 106 additions & 116 deletions examples/8_trapping_sindy_examples/trapping_sindy_paper_examples.ipynb

Large diffs are not rendered by default.

9 changes: 4 additions & 5 deletions examples/8_trapping_sindy_examples/trapping_utils.py
Expand Up @@ -129,7 +129,8 @@ def check_stability(r, Xi, sindy_opt, mean_val, mod_matrix=None):
Rm = np.linalg.norm(d) / np.abs(max_eigval)
Reff = Rm / mean_val
print("Estimate of trapping region size, Rm = ", Rm)
print("Normalized trapping region size, Reff = ", Reff)
if not np.isclose(mean_val, 1.0):
print("Normalized trapping region size, Reff = ", Reff)


def get_trapping_radius(max_eigval, eps_Q, r, d):
Expand Down Expand Up @@ -164,9 +165,6 @@ def check_local_stability(r, Xi, sindy_opt, mean_val, mod_matrix=None):
print("optimal m: ", opt_m)
print("As eigvals: ", np.sort(eigvals))
max_eigval = np.sort(eigvals)[-1]
# C = np.tensordot(PC_tensor, Xi, axes=([2, 1], [0, 1]))
# L = np.tensordot(PL_tensor_unsym, Xi, axes=([3, 2], [0, 1]))
# Q = np.tensordot(PQ_tensor, Xi, axes=([4, 3], [0, 1]))
C = mod_matrix @ np.tensordot(PC_tensor, Xi, axes=([2, 1], [0, 1]))
L = mod_matrix @ np.tensordot(PL_tensor_unsym, Xi, axes=([3, 2], [0, 1]))
Q = np.tensordot(
Expand All @@ -181,7 +179,8 @@ def check_local_stability(r, Xi, sindy_opt, mean_val, mod_matrix=None):
Rm, R_ls = get_trapping_radius(max_eigval, eps_Q, r, d)
Reff = Rm / mean_val
print("Estimate of trapping region size, Rm = ", Rm)
print("Normalized trapping region size, Reff = ", Reff)
if not np.isclose(mean_val, 1.0):
print("Normalized trapping region size, Reff = ", Reff)
print("Local stability size, R_ls= ", R_ls)
return Rm, R_ls

Expand Down
218 changes: 145 additions & 73 deletions examples/8_trapping_sindy_examples/von_karman_trapping_extended.ipynb

Large diffs are not rendered by default.

13 changes: 9 additions & 4 deletions pysindy/optimizers/trapping_sr3.py
Expand Up @@ -2,8 +2,6 @@

import cvxpy as cp
import numpy as np
from scipy.linalg import cho_factor
from scipy.linalg import cho_solve
from sklearn.exceptions import ConvergenceWarning

from ..utils import reorder_constraints
Expand Down Expand Up @@ -720,8 +718,15 @@ def _solve_nonsparse_relax_and_split(self, H, xTy, P_transpose_A, coef_prev):
H, xTy, P_transpose_A, coef_prev
).reshape(coef_prev.shape)
else:
cho = cho_factor(H)
coef_sparse = cho_solve(cho, xTy + P_transpose_A / self.eta).reshape(
# Alan Kaptanoglu: removed cho factor calculation here
# which has numerical issues in certain cases. Easier to chop
# things using pinv, but gives dumb results sometimes.
warnings.warn(
"TrappingSR3._solve_nonsparse_relax_and_split using "
"naive pinv() call here, be careful with rcond parameter."
)
Hinv = np.linalg.pinv(H, rcond=1e-15)
coef_sparse = Hinv.dot(xTy + P_transpose_A / self.eta).reshape(
coef_prev.shape
)
return coef_sparse
Expand Down
6 changes: 4 additions & 2 deletions requirements-dev.txt
Expand Up @@ -6,7 +6,10 @@ pytest-cov
pytest-lazy-fixture
flake8-builtins-unleashed
codecov
cvxpy
sympy
dysts
pymech
cvxpy==1.3.2
setuptools_scm
setuptools_scm_git_archive
jupyter
Expand All @@ -22,5 +25,4 @@ jupyter_contrib_nbextensions
pandas
seaborn
ipython
scs<=2.1.4
gurobipy>=9.5.1,<10.0

0 comments on commit d6ee083

Please sign in to comment.