I have been trying to reproduce the weak SINDy compressible example by instead creating the function library using the GeneralizedLibrary feature. However, the models produced at the end are not the same and non-unique terms are generated in the feature library. I have included the code to produce this, the first section is the same as the notebook.
import matplotlib.pyplot as plt
import numpy as np
from scipy.integrate import solve_ivp
from sklearn.linear_model import Lasso
from scipy.io import loadmat
from sklearn.metrics import mean_squared_error
from pysindy.utils.odes import lorenz
import pysindy as ps
# Ignore matplotlib deprecation warnings
import warnings
"""
Solve the 2D system of equations
u_t = -(u.d)u + (-dP + mu d^2 u)/rho
using the ideal gas equation for compressibility.
"""
warnings.filterwarnings("ignore", category=UserWarning)
warnings.filterwarnings("ignore", category=FutureWarning)
# Seed the random number generators for reproducibility
np.random.seed(100)
# integration keywords for solve_ivp, typically needed for chaotic systems
integrator_keywords = {}
integrator_keywords["rtol"] = 1e-12
integrator_keywords["method"] = "LSODA"
integrator_keywords["atol"] = 1e-12
def compressible(t, U, dx, N, mu, RT):
u = U.reshape(N, N, 3)[:, :, 0]
v = U.reshape(N, N, 3)[:, :, 1]
rho = U.reshape(N, N, 3)[:, :, 2]
ux = ps.differentiation.FiniteDifference(
d=1,
axis=0,
periodic=True,
)._differentiate(u, dx)
uy = ps.differentiation.FiniteDifference(
d=1,
axis=1,
periodic=True,
)._differentiate(u, dx)
uxx = ps.differentiation.FiniteDifference(
d=2,
axis=0,
periodic=True,
)._differentiate(u, dx)
uyy = ps.differentiation.FiniteDifference(
d=2,
axis=1,
periodic=True,
)._differentiate(u, dx)
vx = ps.differentiation.FiniteDifference(
d=1,
axis=0,
periodic=True,
)._differentiate(v, dx)
vy = ps.differentiation.FiniteDifference(
d=1,
axis=1,
periodic=True,
)._differentiate(v, dx)
vxx = ps.differentiation.FiniteDifference(
d=2,
axis=0,
periodic=True,
)._differentiate(v, dx)
vyy = ps.differentiation.FiniteDifference(
d=2,
axis=1,
periodic=True,
)._differentiate(v, dx)
px = ps.differentiation.FiniteDifference(
d=1,
axis=0,
periodic=True,
)._differentiate(rho * RT, dx)
py = ps.differentiation.FiniteDifference(
d=1,
axis=1,
periodic=True,
)._differentiate(rho * RT, dx)
ret = np.zeros((N, N, 3))
ret[:, :, 0] = -(u * ux + v * uy) - (px - mu * (uxx + uyy)) / rho
ret[:, :, 1] = -(u * vx + v * vy) - (py - mu * (vxx + vyy)) / rho
ret[:, :, 2] = -(u * px / RT + v * py / RT + rho * ux + rho * vy)
return ret.reshape(3 * N * N)
N = 64
Nt = 100
L = 5
T = 0.5
mu = 1
RT = 1
t = np.linspace(0, T, Nt)
t.shape
x = np.arange(0, N) * L / N
y = np.arange(0, N) * L / N
dx = x[1] - x[0]
# some arbitrary initial conditions
y0 = np.zeros((N, N, 3))
y0[:, :, 0] = (
-np.sin(2 * np.pi / L * x)[:, np.newaxis]
+ 0.5 * np.cos(2 * 2 * np.pi / L * y)[np.newaxis, :]
)
y0[:, :, 1] = (
0.5 * np.cos(2 * np.pi / L * x)[:, np.newaxis]
- np.sin(2 * 2 * np.pi / L * y)[np.newaxis, :]
)
y0[:, :, 2] = (
1
+ 0.5
* np.cos(2 * np.pi / L * x)[:, np.newaxis]
* np.cos(2 * 2 * np.pi / L * y)[np.newaxis, :]
)
sol = solve_ivp(
compressible,
(t[0], t[-1]),
y0=y0.reshape(3 * N * N),
t_eval=t,
args=(dx, N, mu, RT),
method="RK45",
rtol=1e-8,
atol=1e-8,
)
u_shaped_noiseless = sol.y.reshape(N, N, 3, -1).transpose(0, 1, 3, 2)
u_dot_noiseless = ps.FiniteDifference(d=1, axis=2)._differentiate(u_shaped_noiseless, t)
patiotemporal_grid = np.zeros((N, N, Nt, 3))
spatiotemporal_grid[:, :, :, 0] = x[:, np.newaxis, np.newaxis]
spatiotemporal_grid[:, :, :, 1] = y[np.newaxis, :, np.newaxis]
spatiotemporal_grid[:, :, :, 2] = t[np.newaxis, np.newaxis, :]
N = 64
Nt = 100
L = 5
T = 0.5
mu = 1
RT = 1
t = np.linspace(0, T, Nt)
x = np.arange(0, N) * L / N
y = np.arange(0, N) * L / N
dx = x[1] - x[0]
# some arbitrary initial conditions
y0 = np.zeros((N, N, 3))
y0[:, :, 0] = (
-np.sin(2 * np.pi / L * x)[:, np.newaxis]
+ 0.5 * np.cos(2 * 2 * np.pi / L * y)[np.newaxis, :]
)
y0[:, :, 1] = (
0.5 * np.cos(2 * np.pi / L * x)[:, np.newaxis]
- np.sin(2 * 2 * np.pi / L * y)[np.newaxis, :]
)
y0[:, :, 2] = (
1
+ 0.5
* np.cos(2 * np.pi / L * x)[:, np.newaxis]
* np.cos(2 * 2 * np.pi / L * y)[np.newaxis, :]
)
sol = solve_ivp(
compressible,
(t[0], t[-1]),
y0=y0.reshape(3 * N * N),
t_eval=t,
args=(dx, N, mu, RT),
method="RK45",
rtol=1e-8,
atol=1e-8,
)
I then define the feature and function libraries in two different forms, one of which is the same as the Jupyter-notebook, and the other using GeneralizedLibrary.
So the models are not the same. If I print the feature names using model.get_feature_names(), we can see that the second fit contains 15 more terms. These additional terms are actually terms which have already been included in the library, so are correlated with themselves. The terms which appear twice in the library are
Hi
I have been trying to reproduce the weak SINDy compressible example by instead creating the function library using the
GeneralizedLibrary
feature. However, the models produced at the end are not the same and non-unique terms are generated in the feature library. I have included the code to produce this, the first section is the same as the notebook.I then define the feature and function libraries in two different forms, one of which is the same as the Jupyter-notebook, and the other using
GeneralizedLibrary
.I then fit the libraries separately.
which gives the resulting models as
So the models are not the same. If I print the feature names using
model.get_feature_names()
, we can see that the second fit contains 15 more terms. These additional terms are actually terms which have already been included in the library, so are correlated with themselves. The terms which appear twice in the library areHave I made a mistake forming the generalized library?