I'm running a test problem to set up larger problems. Solving the simple unsteady heat equation via finite differences:
$$ \frac{\partial T}{\partial t} = \alpha \frac{\partial^2T}{\partial x^2}$$
$\alpha = 0.001$
$T(x=0,t) = 60°C$
$T(x=L,t) = 25°C$
$T(x,t=0) = 25°C$
import matplotlib.pyplot as plt
import numpy as np
import sympy as sp
from scipy.optimize import fsolve
from scipy import optimize
import pandas as pd
from numba import njit
t_max = 25
x_max = 1
N_points_1 = 61
N_points_2 = 11
dt = t_max/(N_points_1-1)
dx = x_max/(N_points_2-1)
N_variables = N_points_1 * N_points_2
alpha = 0.01
T_x_0 = 60
T_x_L = 25
T_t_0 = 25
@njit
def EDP (v):
T_N = np.ones((N_points_1, N_points_2))
#Variables
k=0
for i in range(N_points_1):
for j in range(N_points_2):
T_N[i,j]=v[k]
k=k+1
#Model
sys = []
##Boundary Condition x = 0
for i in range(N_points_1):
BC1 = T_N[i,0] - T_x_0
sys.append(BC1)
##Boundary Condition x = L
for i in range(N_points_1):
BC2 = T_N[i,-1] - T_x_L
sys.append(BC2)
##Initial Condition
for j in range(1,N_points_2-1):
IC1 = T_N[0,j] - T_t_0
sys.append(IC1)
## Energy Balance
for i in range(N_points_1-1):
for j in range (1,N_points_2 -1):
EB = alpha*(T_N[i,j+1]-2*T_N[i,j]+T_N[i,j-1])/dx**2 - (T_N[i+1,j]-T_N[i,j])/dt
sys.append(EB)
sys=np.array(sys)
return sys
initial_guess = np.linspace(25,60,N_variables)
function_compile = EDP(initial_guess)
I tested the following scipy.root algorithms:
Solution_T = fsolve(EDP,initial_guess)
Solution_T = optimize.anderson(EDP,initial_guess, verbose = True,maxiter = 1000)
Solution_T = optimize.newton_krylov(EDP,initial_guess,verbose=True,maxiter = 1000)
Solution_T = optimize.broyden1(EDP,initial_guess,verbose=True,maxiter = 1000)
Solution_T = optimize.broyden2(EDP,initial_guess,verbose=True,maxiter = 1000)
And ONLY fsolve
converges (instantly, in fact) but since I am just running a test for a larger run (which will contain about 50000 variables), fsolve
won't be able to handle that and the scipy docs recommend the algorithms mentioned above, but none of them converge.
They either diverge with the objective function overflowing or remain stuck in the same value, not converging. As for why I am not using linear solvers, it's because I intend to input more complex non-linear equations later.
scipy.integrate.solve_ivp
. $\endgroup$events
callback insolve_ivp
serves a different purpose, say, to find out when does a particular point reach a certain temperature. $\endgroup$