Patrick Lewis
Patrick Lewis

Reputation: 93

Solving set of Boundary Value Problems

I am trying to solve a set of boundary value problems given by 4 differential equations. I am using bvp_solver in python, and I am getting errors which state 'invalid value encountered in division'. I am assuming this means I am dividing by NaN or 0 at some point, but I am unsure where.

import numpy as np
from scipy.integrate import solve_bvp
import matplotlib.pyplot as plt
%matplotlib inline
alpha = 1
zeta = 1
C_k = 1
sigma = 1
Q = 30
U_0 = 0.1
gamma = 5/3
theta = 3
m = 1.5
def fun(x, y):
    U, dU, B, dB, T, dT, M, dM = y;
    d2U = -2*U_0*Q**2*(1/np.cosh(Q*x))**2*np.tanh(Q*x)-((alpha)/(C_k*sigma))*dB;   
    d2B = -(1/(C_k*zeta))*dU;
    d2T = (1/gamma - 1)*(sigma*dU**2 + zeta*alpha*dB**2);
    d2M = -(dM/T)*dT + (dM/T)*theta*(m+1) - (alpha/T)*B*dB
    return dU, d2U, dB, d2B, dT, d2T, dM, d2M 

def bc(ya, yb):

    return ya[0]+U_0*np.tanh(Q*0.5), yb[0]-U_0*np.tanh(Q*0.5), ya[2]-0, yb[2]-0, ya[4] - 1, yb[4] - 4, ya[6], yb[6] - 1

x = np.linspace(-0.5, 0.5, 500)
y = np.zeros((8, x.size))


sol = solve_bvp(fun, bc, x, y)

If I remove the last two equations for M and dM, then the solution works fine. I have had trouble in the past understanding the return arrays of bvp_solver, but I am confident I understand that now. But I continue to get errors each time I add more equations. Any help is greatly appreciated.

Upvotes: 1

Views: 759

Answers (1)

Lutz Lehmann
Lutz Lehmann

Reputation: 26040

Of course this will fail in the first step. You initialize everything to zero, and then in the derivatives function, you divide by T, which is zero from the initialization.

  • Find a more realistic initialization of T, for instance

    x = np.linspace(-0.5, 0.5, 15)
    y = np.zeros((8, x.size))
    y[4] = 2.5+3*x
    y[5] = 3+0*x
    

    or

  • desingularize the division, which usually is done similar to

    d2M = (-dM*dT + dM*theta*(m+1) - alpha*B*dB) * T/(1e-12+T*T)
    

It makes always sense to print after sol = solve_bvp(...) the error message print(sol.message). Now that there are more than a few components, I changed the construction of the output tableau to the systematic

%matplotlib inline
plt.figure(figsize=(10,2.5*4))
for k in range(4):
    v,c = ['U','B','T','M'][k],['-+b','-*r','-xg','-om'][k];
    plt.subplot(4,2,2*k+1); plt.plot(sol.x,sol.y[2*k  ],c, ms=2); plt.grid(); plt.legend(["$%c$"%v]);
    plt.subplot(4,2,2*k+2); plt.plot(sol.x,sol.y[2*k+1],c, ms=2); plt.grid(); plt.legend(["$%c'$"%v]);
plt.tight_layout(); plt.savefig("/tmp/bvp3.png"); plt.show(); plt.close()

Upvotes: 1

Related Questions