componavt's picture
+ temp old streamlit
723b602
Raw
History Blame Contribute Delete
3.37 kB
import numpy as np
from scipy.integrate import solve_ivp
# Constants for integration
DEFAULT_SOLVER_METHOD = 'DOP853'
DEFAULT_TOLERANCE = 1e-9
class BaseSolver:
"""
Base class for ODE solvers.
"""
def solve(self, rhs, x0, t_eval):
raise NotImplementedError("Subclasses must implement solve method")
class SciPySolver(BaseSolver):
"""
Solver using scipy.integrate.solve_ivp
"""
def __init__(self, method=DEFAULT_SOLVER_METHOD, rtol=DEFAULT_TOLERANCE, atol=DEFAULT_TOLERANCE):
self.method = method
self.rtol = rtol
self.atol = atol
def solve(self, rhs, x0, t_eval):
"""
Solve ODE using scipy.integrate.solve_ivp
Args:
rhs: Right-hand side function of the ODE system
x0: Initial conditions
t_eval: Time points to evaluate the solution
Returns:
Tuple of (solution_successful, x_solution, y_solution)
"""
try:
sol = solve_ivp(rhs, (t_eval[0], t_eval[-1]), x0, method=self.method,
rtol=self.rtol, atol=self.atol, t_eval=t_eval)
if sol.success:
return True, sol.y[0], sol.y[1]
else:
return False, None, None
except Exception:
return False, None, None
class NeuralFlowSolver(BaseSolver):
"""
Neural network solver that learns the vector field (x, y) -> (dx/dt, dy/dt)
"""
def __init__(self, model=None, epochs=2000, lr=1e-3):
self.model = model
self.epochs = epochs
self.lr = lr
self.trained = False
def train(self, rhs, x0, t_train, y_train):
"""
Train the neural network to learn the vector field
Args:
rhs: Right-hand side function of the ODE system (used for generating training data)
x0: Initial conditions
t_train: Time points for training
y_train: Target values for training (derivatives)
"""
# This is a placeholder implementation - a real implementation would involve
# training a neural network to approximate the vector field
# For now, we'll just store the target data
self.t_train = t_train
self.y_train = y_train
self.trained = True
def solve(self, rhs, x0, t_eval):
"""
Solve ODE using the trained neural network
Args:
rhs: Right-hand side function of the ODE system
x0: Initial conditions
t_eval: Time points to evaluate the solution
Returns:
Tuple of (solution_successful, x_solution, y_solution)
"""
if not self.trained:
raise ValueError("Model must be trained before solving")
# This is a placeholder implementation
# A real implementation would use the trained neural network to solve the ODE
# For now, we'll fall back to scipy solver if model is not implemented
try:
sol = solve_ivp(rhs, (t_eval[0], t_eval[-1]), x0, method=DEFAULT_SOLVER_METHOD, t_eval=t_eval)
if sol.success:
return True, sol.y[0], sol.y[1]
else:
return False, None, None
except Exception:
return False, None, None