diff --git a/examples/1_feature_overview/example.py b/examples/1_feature_overview/example.py index 37ecd288..f621d583 100644 --- a/examples/1_feature_overview/example.py +++ b/examples/1_feature_overview/example.py @@ -239,6 +239,8 @@ def ignore_specific_warnings(): # ### Discrete time dynamical system (map) # In[14]: + + def f(x): return 3.6 * x * (1 - x) diff --git a/examples/8_trapping_sindy_examples/example.py b/examples/8_trapping_sindy_examples/example.py index 382f49ad..9d9c6c3d 100644 --- a/examples/8_trapping_sindy_examples/example.py +++ b/examples/8_trapping_sindy_examples/example.py @@ -138,7 +138,7 @@ # compute relative Frobenius error in the model coefficients terms = sindy_library.get_feature_names() Xi_meanfield = np.zeros(Xi.shape) -Xi_meanfield[1: r + 1, :] = np.asarray([[0.01, -1, 0], [1, 0.01, 0], [0, 0, -1]]).T +Xi_meanfield[1 : r + 1, :] = np.asarray([[0.01, -1, 0], [1, 0.01, 0], [0, 0, -1]]).T Xi_meanfield[terms.index("x0 x2"), 0] = -1 Xi_meanfield[terms.index("x1 x2"), 1] = -1 Xi_meanfield[terms.index("x0^2"), 2] = 1 @@ -275,7 +275,7 @@ # compute relative Frobenius error in the model coefficients terms = sindy_library.get_feature_names() Xi_oscillator = np.zeros(Xi.shape) -Xi_oscillator[1: r + 1, :] = np.asarray( +Xi_oscillator[1 : r + 1, :] = np.asarray( [[mu1, 0, 0], [0, mu2, omega], [0, -omega, mu2]] ).T Xi_oscillator[terms.index("x0 x1"), 0] = sigma @@ -426,7 +426,7 @@ beta = 8.0 / 3.0 terms = sindy_library.get_feature_names() Xi_lorenz = np.zeros(Xi.shape) -Xi_lorenz[1: r + 1, :] = np.asarray( +Xi_lorenz[1 : r + 1, :] = np.asarray( [[-sigma, sigma, 0], [rho, -1, 0], [0, 0, -beta]] ).T Xi_lorenz[terms.index("x0 x2"), 1] = -1 @@ -637,7 +637,10 @@ # get analytic L and Q operators and galerkin model L, Q = burgers_galerkin(sigma, nu, U) -def rhs(t, a): return galerkin_model(a, L, Q) # noqa: E731 + + +def rhs(t, a): + return galerkin_model(a, L, Q) # noqa: E731 # Generate initial condition from unstable eigenvectors @@ -740,7 +743,10 @@ def rhs(t, a): return galerkin_model(a, L, Q) # noqa: E731 galerkin5["L"] = galerkin9["L"][inds5] inds5 = np.ix_([0, 1, 2, 3, -1], [0, 1, 2, 3, -1], [0, 1, 2, 3, -1]) galerkin5["Q"] = galerkin9["Q"][inds5] -def model5(t, a): return galerkin_model(a, galerkin5["L"], galerkin5["Q"]) # noqa: E731 + + +def model5(t, a): + return galerkin_model(a, galerkin5["L"], galerkin5["Q"]) # noqa: E731 # make the 3D, 5D, and 9D POD-Galerkin trajectories diff --git a/examples/8_trapping_sindy_examples/trapping_extended.py b/examples/8_trapping_sindy_examples/trapping_extended.py index 822a845d..ba2913b2 100644 --- a/examples/8_trapping_sindy_examples/trapping_extended.py +++ b/examples/8_trapping_sindy_examples/trapping_extended.py @@ -168,7 +168,7 @@ terms = sindy_library.get_feature_names() Xi_lorenz = np.zeros(Xi.shape) -Xi_lorenz[1: r + 1, :] = np.array([[-sigma, sigma, 0], [rho, -1, 0], [0, 0, -beta]]).T +Xi_lorenz[1 : r + 1, :] = np.array([[-sigma, sigma, 0], [rho, -1, 0], [0, 0, -beta]]).T Xi_lorenz[terms.index("x0 x2"), 1] = -1 Xi_lorenz[terms.index("x0 x1"), 2] = 1 diff --git a/examples/8_trapping_sindy_examples/von_karman_trapping_extended.py b/examples/8_trapping_sindy_examples/von_karman_trapping_extended.py index fbed66b6..333a2a6f 100644 --- a/examples/8_trapping_sindy_examples/von_karman_trapping_extended.py +++ b/examples/8_trapping_sindy_examples/von_karman_trapping_extended.py @@ -12,34 +12,32 @@ # # ### This notebook will show that both the energy and enstrophy can be used with the trapping theorem to promote global stability. # %% -from scipy.optimize import dual_annealing as anneal_algo -from trapping_utils import ( - galerkin_model, - integrator_keywords, - nel, - nGLL, - interp, - get_velocity, - get_vorticity, - obj_function, - sindy_library_no_bias, - check_stability, - check_local_stability, - make_trap_progress_plots, - make_bar, - nx, - ny, - plot_field, - make_progress_plots, -) -import pymech.neksuite as nek import warnings import matplotlib.gridspec as gridspec import numpy as np +import pymech.neksuite as nek import scipy.io as sio from matplotlib import pyplot as plt from scipy.integrate import solve_ivp +from scipy.optimize import dual_annealing as anneal_algo +from trapping_utils import check_local_stability +from trapping_utils import check_stability +from trapping_utils import galerkin_model +from trapping_utils import get_velocity +from trapping_utils import get_vorticity +from trapping_utils import integrator_keywords +from trapping_utils import interp +from trapping_utils import make_bar +from trapping_utils import make_progress_plots +from trapping_utils import make_trap_progress_plots +from trapping_utils import nel +from trapping_utils import nGLL +from trapping_utils import nx +from trapping_utils import ny +from trapping_utils import obj_function +from trapping_utils import plot_field +from trapping_utils import sindy_library_no_bias import pysindy as ps @@ -85,12 +83,14 @@ ) / 6.0 ) -def model9(t, a): return galerkin_model(a, galerkin9["L"], galerkin9["Q"]) # noqa: E731 -def model9_ep(t, a): return galerkin_model( # noqa: E731 - a, galerkin9["L"], galerkin9["Q_ep"] -) +def model9(t, a): + return galerkin_model(a, galerkin9["L"], galerkin9["Q"]) # noqa: E731 + + +def model9_ep(t, a): + return galerkin_model(a, galerkin9["L"], galerkin9["Q_ep"]) # noqa: E731 # Generate initial condition from unstable eigenvectors @@ -107,7 +107,10 @@ def model9_ep(t, a): return galerkin_model( # noqa: E731 inds5 = np.ix_([0, 1, 2, 3, -1], [0, 1, 2, 3, -1], [0, 1, 2, 3, -1]) galerkin5["Q"] = galerkin9["Q"][inds5] galerkin5["Q_ep"] = galerkin9["Q_ep"][inds5] -def model5(t, a): return galerkin_model(a, galerkin5["L"], galerkin5["Q"]) # noqa: E731 + + +def model5(t, a): + return galerkin_model(a, galerkin5["L"], galerkin5["Q"]) # noqa: E731 # make the 5D POD-Galerkin model trajectories @@ -171,7 +174,8 @@ def model5(t, a): return galerkin_model(a, galerkin5["L"], galerkin5["Q"]) # no ) -def filename(t_idx): return "cyl0.f{0:05d}".format(t_idx) # noqa: E731 +def filename(t_idx): + return "cyl0.f{0:05d}".format(t_idx) # noqa: E731 # plot mean + leading POD modes @@ -211,12 +215,14 @@ def filename(t_idx): return "cyl0.f{0:05d}".format(t_idx) # noqa: E731 # %% mass_matrix = np.loadtxt("../data/vonKarman_pod/pod_modes/mass_matrix.dat") -def ip1(a, b): return np.dot(mass_matrix * a, b) # noqa: E731 -def ip2(a, b, c, d): return np.dot(a * mass_matrix, c) + np.dot( # noqa: E731 - b * mass_matrix, d -) +def ip1(a, b): + return np.dot(mass_matrix * a, b) # noqa: E731 + + +def ip2(a, b, c, d): + return np.dot(a * mass_matrix, c) + np.dot(b * mass_matrix, d) # noqa: E731 energy_integrals = np.zeros((6, 6)) diff --git a/pysindy/optimizers/constrained_sr3.py b/pysindy/optimizers/constrained_sr3.py index 84ea5601..293ad7c2 100644 --- a/pysindy/optimizers/constrained_sr3.py +++ b/pysindy/optimizers/constrained_sr3.py @@ -246,7 +246,9 @@ def _create_var_and_part_cost( ) -> Tuple[cp.Variable, cp.Expression]: xi = cp.Variable(var_len) cost = cp.sum_squares(x_expanded @ xi - y.flatten()) - penalty = self._calculate_penalty(self.regularizer, np.ravel(self.reg_weight_lam), xi) + penalty = self._calculate_penalty( + self.regularizer, np.ravel(self.reg_weight_lam), xi + ) return xi, cost + penalty def _update_coef_cvxpy(self, xi, cost, var_len, coef_prev, tol): diff --git a/pysindy/optimizers/stable_linear_sr3.py b/pysindy/optimizers/stable_linear_sr3.py index 31cd695d..9742e2e1 100644 --- a/pysindy/optimizers/stable_linear_sr3.py +++ b/pysindy/optimizers/stable_linear_sr3.py @@ -161,7 +161,9 @@ def _create_var_and_part_cost( cost = cost + cp.sum_squares(xi - coef_neg_def.flatten()) / ( 2 * self.relax_coeff_nu ) - penalty = self._calculate_penalty(self.thresholder, np.ravel(self.reg_weight_lam), xi) + penalty = self._calculate_penalty( + self.thresholder, np.ravel(self.reg_weight_lam), xi + ) return xi, cost + penalty def _update_coef_cvxpy(self, x, y, coef_sparse, coef_negative_definite):