test_bvp.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. import sys
  2. try:
  3. from StringIO import StringIO
  4. except ImportError:
  5. from io import StringIO
  6. import numpy as np
  7. from numpy.testing import (assert_, assert_array_equal, assert_allclose,
  8. assert_equal)
  9. from pytest import raises as assert_raises
  10. from scipy.sparse import coo_matrix
  11. from scipy.special import erf
  12. from scipy.integrate._bvp import (modify_mesh, estimate_fun_jac,
  13. estimate_bc_jac, compute_jac_indices,
  14. construct_global_jac, solve_bvp)
  15. def exp_fun(x, y):
  16. return np.vstack((y[1], y[0]))
  17. def exp_fun_jac(x, y):
  18. df_dy = np.empty((2, 2, x.shape[0]))
  19. df_dy[0, 0] = 0
  20. df_dy[0, 1] = 1
  21. df_dy[1, 0] = 1
  22. df_dy[1, 1] = 0
  23. return df_dy
  24. def exp_bc(ya, yb):
  25. return np.hstack((ya[0] - 1, yb[0]))
  26. def exp_bc_complex(ya, yb):
  27. return np.hstack((ya[0] - 1 - 1j, yb[0]))
  28. def exp_bc_jac(ya, yb):
  29. dbc_dya = np.array([
  30. [1, 0],
  31. [0, 0]
  32. ])
  33. dbc_dyb = np.array([
  34. [0, 0],
  35. [1, 0]
  36. ])
  37. return dbc_dya, dbc_dyb
  38. def exp_sol(x):
  39. return (np.exp(-x) - np.exp(x - 2)) / (1 - np.exp(-2))
  40. def sl_fun(x, y, p):
  41. return np.vstack((y[1], -p[0]**2 * y[0]))
  42. def sl_fun_jac(x, y, p):
  43. n, m = y.shape
  44. df_dy = np.empty((n, 2, m))
  45. df_dy[0, 0] = 0
  46. df_dy[0, 1] = 1
  47. df_dy[1, 0] = -p[0]**2
  48. df_dy[1, 1] = 0
  49. df_dp = np.empty((n, 1, m))
  50. df_dp[0, 0] = 0
  51. df_dp[1, 0] = -2 * p[0] * y[0]
  52. return df_dy, df_dp
  53. def sl_bc(ya, yb, p):
  54. return np.hstack((ya[0], yb[0], ya[1] - p[0]))
  55. def sl_bc_jac(ya, yb, p):
  56. dbc_dya = np.zeros((3, 2))
  57. dbc_dya[0, 0] = 1
  58. dbc_dya[2, 1] = 1
  59. dbc_dyb = np.zeros((3, 2))
  60. dbc_dyb[1, 0] = 1
  61. dbc_dp = np.zeros((3, 1))
  62. dbc_dp[2, 0] = -1
  63. return dbc_dya, dbc_dyb, dbc_dp
  64. def sl_sol(x, p):
  65. return np.sin(p[0] * x)
  66. def emden_fun(x, y):
  67. return np.vstack((y[1], -y[0]**5))
  68. def emden_fun_jac(x, y):
  69. df_dy = np.empty((2, 2, x.shape[0]))
  70. df_dy[0, 0] = 0
  71. df_dy[0, 1] = 1
  72. df_dy[1, 0] = -5 * y[0]**4
  73. df_dy[1, 1] = 0
  74. return df_dy
  75. def emden_bc(ya, yb):
  76. return np.array([ya[1], yb[0] - (3/4)**0.5])
  77. def emden_bc_jac(ya, yb):
  78. dbc_dya = np.array([
  79. [0, 1],
  80. [0, 0]
  81. ])
  82. dbc_dyb = np.array([
  83. [0, 0],
  84. [1, 0]
  85. ])
  86. return dbc_dya, dbc_dyb
  87. def emden_sol(x):
  88. return (1 + x**2/3)**-0.5
  89. def undefined_fun(x, y):
  90. return np.zeros_like(y)
  91. def undefined_bc(ya, yb):
  92. return np.array([ya[0], yb[0] - 1])
  93. def big_fun(x, y):
  94. f = np.zeros_like(y)
  95. f[::2] = y[1::2]
  96. return f
  97. def big_bc(ya, yb):
  98. return np.hstack((ya[::2], yb[::2] - 1))
  99. def big_sol(x, n):
  100. y = np.ones((2 * n, x.size))
  101. y[::2] = x
  102. return x
  103. def big_fun_with_parameters(x, y, p):
  104. """ Big version of sl_fun, with two parameters.
  105. The two differential equations represented by sl_fun are broadcast to the
  106. number of rows of y, rotating between the parameters p[0] and p[1].
  107. Here are the differential equations:
  108. dy[0]/dt = y[1]
  109. dy[1]/dt = -p[0]**2 * y[0]
  110. dy[2]/dt = y[3]
  111. dy[3]/dt = -p[1]**2 * y[2]
  112. dy[4]/dt = y[5]
  113. dy[5]/dt = -p[0]**2 * y[4]
  114. dy[6]/dt = y[7]
  115. dy[7]/dt = -p[1]**2 * y[6]
  116. .
  117. .
  118. .
  119. """
  120. f = np.zeros_like(y)
  121. f[::2] = y[1::2]
  122. f[1::4] = -p[0]**2 * y[::4]
  123. f[3::4] = -p[1]**2 * y[2::4]
  124. return f
  125. def big_fun_with_parameters_jac(x, y, p):
  126. # big version of sl_fun_jac, with two parameters
  127. n, m = y.shape
  128. df_dy = np.zeros((n, n, m))
  129. df_dy[range(0, n, 2), range(1, n, 2)] = 1
  130. df_dy[range(1, n, 4), range(0, n, 4)] = -p[0]**2
  131. df_dy[range(3, n, 4), range(2, n, 4)] = -p[1]**2
  132. df_dp = np.zeros((n, 2, m))
  133. df_dp[range(1, n, 4), 0] = -2 * p[0] * y[range(0, n, 4)]
  134. df_dp[range(3, n, 4), 1] = -2 * p[1] * y[range(2, n, 4)]
  135. return df_dy, df_dp
  136. def big_bc_with_parameters(ya, yb, p):
  137. # big version of sl_bc, with two parameters
  138. return np.hstack((ya[::2], yb[::2], ya[1] - p[0], ya[3] - p[1]))
  139. def big_bc_with_parameters_jac(ya, yb, p):
  140. # big version of sl_bc_jac, with two parameters
  141. n = ya.shape[0]
  142. dbc_dya = np.zeros((n + 2, n))
  143. dbc_dyb = np.zeros((n + 2, n))
  144. dbc_dya[range(n // 2), range(0, n, 2)] = 1
  145. dbc_dyb[range(n // 2, n), range(0, n, 2)] = 1
  146. dbc_dp = np.zeros((n + 2, 2))
  147. dbc_dp[n, 0] = -1
  148. dbc_dya[n, 1] = 1
  149. dbc_dp[n + 1, 1] = -1
  150. dbc_dya[n + 1, 3] = 1
  151. return dbc_dya, dbc_dyb, dbc_dp
  152. def big_sol_with_parameters(x, p):
  153. # big version of sl_sol, with two parameters
  154. return np.vstack((np.sin(p[0] * x), np.sin(p[1] * x)))
  155. def shock_fun(x, y):
  156. eps = 1e-3
  157. return np.vstack((
  158. y[1],
  159. -(x * y[1] + eps * np.pi**2 * np.cos(np.pi * x) +
  160. np.pi * x * np.sin(np.pi * x)) / eps
  161. ))
  162. def shock_bc(ya, yb):
  163. return np.array([ya[0] + 2, yb[0]])
  164. def shock_sol(x):
  165. eps = 1e-3
  166. k = np.sqrt(2 * eps)
  167. return np.cos(np.pi * x) + erf(x / k) / erf(1 / k)
  168. def nonlin_bc_fun(x, y):
  169. # laplace eq.
  170. return np.stack([y[1], np.zeros_like(x)])
  171. def nonlin_bc_bc(ya, yb):
  172. phiA, phipA = ya
  173. phiC, phipC = yb
  174. kappa, ioA, ioC, V, f = 1.64, 0.01, 1.0e-4, 0.5, 38.9
  175. # Butler-Volmer Kinetics at Anode
  176. hA = 0.0-phiA-0.0
  177. iA = ioA * (np.exp(f*hA) - np.exp(-f*hA))
  178. res0 = iA + kappa * phipA
  179. # Butler-Volmer Kinetics at Cathode
  180. hC = V - phiC - 1.0
  181. iC = ioC * (np.exp(f*hC) - np.exp(-f*hC))
  182. res1 = iC - kappa*phipC
  183. return np.array([res0, res1])
  184. def nonlin_bc_sol(x):
  185. return -0.13426436116763119 - 1.1308709 * x
  186. def test_modify_mesh():
  187. x = np.array([0, 1, 3, 9], dtype=float)
  188. x_new = modify_mesh(x, np.array([0]), np.array([2]))
  189. assert_array_equal(x_new, np.array([0, 0.5, 1, 3, 5, 7, 9]))
  190. x = np.array([-6, -3, 0, 3, 6], dtype=float)
  191. x_new = modify_mesh(x, np.array([1], dtype=int), np.array([0, 2, 3]))
  192. assert_array_equal(x_new, [-6, -5, -4, -3, -1.5, 0, 1, 2, 3, 4, 5, 6])
  193. def test_compute_fun_jac():
  194. x = np.linspace(0, 1, 5)
  195. y = np.empty((2, x.shape[0]))
  196. y[0] = 0.01
  197. y[1] = 0.02
  198. p = np.array([])
  199. df_dy, df_dp = estimate_fun_jac(lambda x, y, p: exp_fun(x, y), x, y, p)
  200. df_dy_an = exp_fun_jac(x, y)
  201. assert_allclose(df_dy, df_dy_an)
  202. assert_(df_dp is None)
  203. x = np.linspace(0, np.pi, 5)
  204. y = np.empty((2, x.shape[0]))
  205. y[0] = np.sin(x)
  206. y[1] = np.cos(x)
  207. p = np.array([1.0])
  208. df_dy, df_dp = estimate_fun_jac(sl_fun, x, y, p)
  209. df_dy_an, df_dp_an = sl_fun_jac(x, y, p)
  210. assert_allclose(df_dy, df_dy_an)
  211. assert_allclose(df_dp, df_dp_an)
  212. x = np.linspace(0, 1, 10)
  213. y = np.empty((2, x.shape[0]))
  214. y[0] = (3/4)**0.5
  215. y[1] = 1e-4
  216. p = np.array([])
  217. df_dy, df_dp = estimate_fun_jac(lambda x, y, p: emden_fun(x, y), x, y, p)
  218. df_dy_an = emden_fun_jac(x, y)
  219. assert_allclose(df_dy, df_dy_an)
  220. assert_(df_dp is None)
  221. def test_compute_bc_jac():
  222. ya = np.array([-1.0, 2])
  223. yb = np.array([0.5, 3])
  224. p = np.array([])
  225. dbc_dya, dbc_dyb, dbc_dp = estimate_bc_jac(
  226. lambda ya, yb, p: exp_bc(ya, yb), ya, yb, p)
  227. dbc_dya_an, dbc_dyb_an = exp_bc_jac(ya, yb)
  228. assert_allclose(dbc_dya, dbc_dya_an)
  229. assert_allclose(dbc_dyb, dbc_dyb_an)
  230. assert_(dbc_dp is None)
  231. ya = np.array([0.0, 1])
  232. yb = np.array([0.0, -1])
  233. p = np.array([0.5])
  234. dbc_dya, dbc_dyb, dbc_dp = estimate_bc_jac(sl_bc, ya, yb, p)
  235. dbc_dya_an, dbc_dyb_an, dbc_dp_an = sl_bc_jac(ya, yb, p)
  236. assert_allclose(dbc_dya, dbc_dya_an)
  237. assert_allclose(dbc_dyb, dbc_dyb_an)
  238. assert_allclose(dbc_dp, dbc_dp_an)
  239. ya = np.array([0.5, 100])
  240. yb = np.array([-1000, 10.5])
  241. p = np.array([])
  242. dbc_dya, dbc_dyb, dbc_dp = estimate_bc_jac(
  243. lambda ya, yb, p: emden_bc(ya, yb), ya, yb, p)
  244. dbc_dya_an, dbc_dyb_an = emden_bc_jac(ya, yb)
  245. assert_allclose(dbc_dya, dbc_dya_an)
  246. assert_allclose(dbc_dyb, dbc_dyb_an)
  247. assert_(dbc_dp is None)
  248. def test_compute_jac_indices():
  249. n = 2
  250. m = 4
  251. k = 2
  252. i, j = compute_jac_indices(n, m, k)
  253. s = coo_matrix((np.ones_like(i), (i, j))).toarray()
  254. s_true = np.array([
  255. [1, 1, 1, 1, 0, 0, 0, 0, 1, 1],
  256. [1, 1, 1, 1, 0, 0, 0, 0, 1, 1],
  257. [0, 0, 1, 1, 1, 1, 0, 0, 1, 1],
  258. [0, 0, 1, 1, 1, 1, 0, 0, 1, 1],
  259. [0, 0, 0, 0, 1, 1, 1, 1, 1, 1],
  260. [0, 0, 0, 0, 1, 1, 1, 1, 1, 1],
  261. [1, 1, 0, 0, 0, 0, 1, 1, 1, 1],
  262. [1, 1, 0, 0, 0, 0, 1, 1, 1, 1],
  263. [1, 1, 0, 0, 0, 0, 1, 1, 1, 1],
  264. [1, 1, 0, 0, 0, 0, 1, 1, 1, 1],
  265. ])
  266. assert_array_equal(s, s_true)
  267. def test_compute_global_jac():
  268. n = 2
  269. m = 5
  270. k = 1
  271. i_jac, j_jac = compute_jac_indices(2, 5, 1)
  272. x = np.linspace(0, 1, 5)
  273. h = np.diff(x)
  274. y = np.vstack((np.sin(np.pi * x), np.pi * np.cos(np.pi * x)))
  275. p = np.array([3.0])
  276. f = sl_fun(x, y, p)
  277. x_middle = x[:-1] + 0.5 * h
  278. y_middle = 0.5 * (y[:, :-1] + y[:, 1:]) - h/8 * (f[:, 1:] - f[:, :-1])
  279. df_dy, df_dp = sl_fun_jac(x, y, p)
  280. df_dy_middle, df_dp_middle = sl_fun_jac(x_middle, y_middle, p)
  281. dbc_dya, dbc_dyb, dbc_dp = sl_bc_jac(y[:, 0], y[:, -1], p)
  282. J = construct_global_jac(n, m, k, i_jac, j_jac, h, df_dy, df_dy_middle,
  283. df_dp, df_dp_middle, dbc_dya, dbc_dyb, dbc_dp)
  284. J = J.toarray()
  285. def J_block(h, p):
  286. return np.array([
  287. [h**2*p**2/12 - 1, -0.5*h, -h**2*p**2/12 + 1, -0.5*h],
  288. [0.5*h*p**2, h**2*p**2/12 - 1, 0.5*h*p**2, 1 - h**2*p**2/12]
  289. ])
  290. J_true = np.zeros((m * n + k, m * n + k))
  291. for i in range(m - 1):
  292. J_true[i * n: (i + 1) * n, i * n: (i + 2) * n] = J_block(h[i], p[0])
  293. J_true[:(m - 1) * n:2, -1] = p * h**2/6 * (y[0, :-1] - y[0, 1:])
  294. J_true[1:(m - 1) * n:2, -1] = p * (h * (y[0, :-1] + y[0, 1:]) +
  295. h**2/6 * (y[1, :-1] - y[1, 1:]))
  296. J_true[8, 0] = 1
  297. J_true[9, 8] = 1
  298. J_true[10, 1] = 1
  299. J_true[10, 10] = -1
  300. assert_allclose(J, J_true, rtol=1e-10)
  301. df_dy, df_dp = estimate_fun_jac(sl_fun, x, y, p)
  302. df_dy_middle, df_dp_middle = estimate_fun_jac(sl_fun, x_middle, y_middle, p)
  303. dbc_dya, dbc_dyb, dbc_dp = estimate_bc_jac(sl_bc, y[:, 0], y[:, -1], p)
  304. J = construct_global_jac(n, m, k, i_jac, j_jac, h, df_dy, df_dy_middle,
  305. df_dp, df_dp_middle, dbc_dya, dbc_dyb, dbc_dp)
  306. J = J.toarray()
  307. assert_allclose(J, J_true, rtol=2e-8, atol=2e-8)
  308. def test_parameter_validation():
  309. x = [0, 1, 0.5]
  310. y = np.zeros((2, 3))
  311. assert_raises(ValueError, solve_bvp, exp_fun, exp_bc, x, y)
  312. x = np.linspace(0, 1, 5)
  313. y = np.zeros((2, 4))
  314. assert_raises(ValueError, solve_bvp, exp_fun, exp_bc, x, y)
  315. fun = lambda x, y, p: exp_fun(x, y)
  316. bc = lambda ya, yb, p: exp_bc(ya, yb)
  317. y = np.zeros((2, x.shape[0]))
  318. assert_raises(ValueError, solve_bvp, fun, bc, x, y, p=[1])
  319. def wrong_shape_fun(x, y):
  320. return np.zeros(3)
  321. assert_raises(ValueError, solve_bvp, wrong_shape_fun, bc, x, y)
  322. S = np.array([[0, 0]])
  323. assert_raises(ValueError, solve_bvp, exp_fun, exp_bc, x, y, S=S)
  324. def test_no_params():
  325. x = np.linspace(0, 1, 5)
  326. x_test = np.linspace(0, 1, 100)
  327. y = np.zeros((2, x.shape[0]))
  328. for fun_jac in [None, exp_fun_jac]:
  329. for bc_jac in [None, exp_bc_jac]:
  330. sol = solve_bvp(exp_fun, exp_bc, x, y, fun_jac=fun_jac,
  331. bc_jac=bc_jac)
  332. assert_equal(sol.status, 0)
  333. assert_(sol.success)
  334. assert_equal(sol.x.size, 5)
  335. sol_test = sol.sol(x_test)
  336. assert_allclose(sol_test[0], exp_sol(x_test), atol=1e-5)
  337. f_test = exp_fun(x_test, sol_test)
  338. r = sol.sol(x_test, 1) - f_test
  339. rel_res = r / (1 + np.abs(f_test))
  340. norm_res = np.sum(rel_res**2, axis=0)**0.5
  341. assert_(np.all(norm_res < 1e-3))
  342. assert_(np.all(sol.rms_residuals < 1e-3))
  343. assert_allclose(sol.sol(sol.x), sol.y, rtol=1e-10, atol=1e-10)
  344. assert_allclose(sol.sol(sol.x, 1), sol.yp, rtol=1e-10, atol=1e-10)
  345. def test_with_params():
  346. x = np.linspace(0, np.pi, 5)
  347. x_test = np.linspace(0, np.pi, 100)
  348. y = np.ones((2, x.shape[0]))
  349. for fun_jac in [None, sl_fun_jac]:
  350. for bc_jac in [None, sl_bc_jac]:
  351. sol = solve_bvp(sl_fun, sl_bc, x, y, p=[0.5], fun_jac=fun_jac,
  352. bc_jac=bc_jac)
  353. assert_equal(sol.status, 0)
  354. assert_(sol.success)
  355. assert_(sol.x.size < 10)
  356. assert_allclose(sol.p, [1], rtol=1e-4)
  357. sol_test = sol.sol(x_test)
  358. assert_allclose(sol_test[0], sl_sol(x_test, [1]),
  359. rtol=1e-4, atol=1e-4)
  360. f_test = sl_fun(x_test, sol_test, [1])
  361. r = sol.sol(x_test, 1) - f_test
  362. rel_res = r / (1 + np.abs(f_test))
  363. norm_res = np.sum(rel_res ** 2, axis=0) ** 0.5
  364. assert_(np.all(norm_res < 1e-3))
  365. assert_(np.all(sol.rms_residuals < 1e-3))
  366. assert_allclose(sol.sol(sol.x), sol.y, rtol=1e-10, atol=1e-10)
  367. assert_allclose(sol.sol(sol.x, 1), sol.yp, rtol=1e-10, atol=1e-10)
  368. def test_singular_term():
  369. x = np.linspace(0, 1, 10)
  370. x_test = np.linspace(0.05, 1, 100)
  371. y = np.empty((2, 10))
  372. y[0] = (3/4)**0.5
  373. y[1] = 1e-4
  374. S = np.array([[0, 0], [0, -2]])
  375. for fun_jac in [None, emden_fun_jac]:
  376. for bc_jac in [None, emden_bc_jac]:
  377. sol = solve_bvp(emden_fun, emden_bc, x, y, S=S, fun_jac=fun_jac,
  378. bc_jac=bc_jac)
  379. assert_equal(sol.status, 0)
  380. assert_(sol.success)
  381. assert_equal(sol.x.size, 10)
  382. sol_test = sol.sol(x_test)
  383. assert_allclose(sol_test[0], emden_sol(x_test), atol=1e-5)
  384. f_test = emden_fun(x_test, sol_test) + S.dot(sol_test) / x_test
  385. r = sol.sol(x_test, 1) - f_test
  386. rel_res = r / (1 + np.abs(f_test))
  387. norm_res = np.sum(rel_res ** 2, axis=0) ** 0.5
  388. assert_(np.all(norm_res < 1e-3))
  389. assert_allclose(sol.sol(sol.x), sol.y, rtol=1e-10, atol=1e-10)
  390. assert_allclose(sol.sol(sol.x, 1), sol.yp, rtol=1e-10, atol=1e-10)
  391. def test_complex():
  392. # The test is essentially the same as test_no_params, but boundary
  393. # conditions are turned into complex.
  394. x = np.linspace(0, 1, 5)
  395. x_test = np.linspace(0, 1, 100)
  396. y = np.zeros((2, x.shape[0]), dtype=complex)
  397. for fun_jac in [None, exp_fun_jac]:
  398. for bc_jac in [None, exp_bc_jac]:
  399. sol = solve_bvp(exp_fun, exp_bc_complex, x, y, fun_jac=fun_jac,
  400. bc_jac=bc_jac)
  401. assert_equal(sol.status, 0)
  402. assert_(sol.success)
  403. sol_test = sol.sol(x_test)
  404. assert_allclose(sol_test[0].real, exp_sol(x_test), atol=1e-5)
  405. assert_allclose(sol_test[0].imag, exp_sol(x_test), atol=1e-5)
  406. f_test = exp_fun(x_test, sol_test)
  407. r = sol.sol(x_test, 1) - f_test
  408. rel_res = r / (1 + np.abs(f_test))
  409. norm_res = np.sum(np.real(rel_res * np.conj(rel_res)),
  410. axis=0) ** 0.5
  411. assert_(np.all(norm_res < 1e-3))
  412. assert_(np.all(sol.rms_residuals < 1e-3))
  413. assert_allclose(sol.sol(sol.x), sol.y, rtol=1e-10, atol=1e-10)
  414. assert_allclose(sol.sol(sol.x, 1), sol.yp, rtol=1e-10, atol=1e-10)
  415. def test_failures():
  416. x = np.linspace(0, 1, 2)
  417. y = np.zeros((2, x.size))
  418. res = solve_bvp(exp_fun, exp_bc, x, y, tol=1e-5, max_nodes=5)
  419. assert_equal(res.status, 1)
  420. assert_(not res.success)
  421. x = np.linspace(0, 1, 5)
  422. y = np.zeros((2, x.size))
  423. res = solve_bvp(undefined_fun, undefined_bc, x, y)
  424. assert_equal(res.status, 2)
  425. assert_(not res.success)
  426. def test_big_problem():
  427. n = 30
  428. x = np.linspace(0, 1, 5)
  429. y = np.zeros((2 * n, x.size))
  430. sol = solve_bvp(big_fun, big_bc, x, y)
  431. assert_equal(sol.status, 0)
  432. assert_(sol.success)
  433. sol_test = sol.sol(x)
  434. assert_allclose(sol_test[0], big_sol(x, n))
  435. f_test = big_fun(x, sol_test)
  436. r = sol.sol(x, 1) - f_test
  437. rel_res = r / (1 + np.abs(f_test))
  438. norm_res = np.sum(np.real(rel_res * np.conj(rel_res)), axis=0) ** 0.5
  439. assert_(np.all(norm_res < 1e-3))
  440. assert_(np.all(sol.rms_residuals < 1e-3))
  441. assert_allclose(sol.sol(sol.x), sol.y, rtol=1e-10, atol=1e-10)
  442. assert_allclose(sol.sol(sol.x, 1), sol.yp, rtol=1e-10, atol=1e-10)
  443. def test_big_problem_with_parameters():
  444. n = 30
  445. x = np.linspace(0, np.pi, 5)
  446. x_test = np.linspace(0, np.pi, 100)
  447. y = np.ones((2 * n, x.size))
  448. for fun_jac in [None, big_fun_with_parameters_jac]:
  449. for bc_jac in [None, big_bc_with_parameters_jac]:
  450. sol = solve_bvp(big_fun_with_parameters, big_bc_with_parameters, x,
  451. y, p=[0.5, 0.5], fun_jac=fun_jac, bc_jac=bc_jac)
  452. assert_equal(sol.status, 0)
  453. assert_(sol.success)
  454. assert_allclose(sol.p, [1, 1], rtol=1e-4)
  455. sol_test = sol.sol(x_test)
  456. for isol in range(0, n, 4):
  457. assert_allclose(sol_test[isol],
  458. big_sol_with_parameters(x_test, [1, 1])[0],
  459. rtol=1e-4, atol=1e-4)
  460. assert_allclose(sol_test[isol + 2],
  461. big_sol_with_parameters(x_test, [1, 1])[1],
  462. rtol=1e-4, atol=1e-4)
  463. f_test = big_fun_with_parameters(x_test, sol_test, [1, 1])
  464. r = sol.sol(x_test, 1) - f_test
  465. rel_res = r / (1 + np.abs(f_test))
  466. norm_res = np.sum(rel_res ** 2, axis=0) ** 0.5
  467. assert_(np.all(norm_res < 1e-3))
  468. assert_(np.all(sol.rms_residuals < 1e-3))
  469. assert_allclose(sol.sol(sol.x), sol.y, rtol=1e-10, atol=1e-10)
  470. assert_allclose(sol.sol(sol.x, 1), sol.yp, rtol=1e-10, atol=1e-10)
  471. def test_shock_layer():
  472. x = np.linspace(-1, 1, 5)
  473. x_test = np.linspace(-1, 1, 100)
  474. y = np.zeros((2, x.size))
  475. sol = solve_bvp(shock_fun, shock_bc, x, y)
  476. assert_equal(sol.status, 0)
  477. assert_(sol.success)
  478. assert_(sol.x.size < 110)
  479. sol_test = sol.sol(x_test)
  480. assert_allclose(sol_test[0], shock_sol(x_test), rtol=1e-5, atol=1e-5)
  481. f_test = shock_fun(x_test, sol_test)
  482. r = sol.sol(x_test, 1) - f_test
  483. rel_res = r / (1 + np.abs(f_test))
  484. norm_res = np.sum(rel_res ** 2, axis=0) ** 0.5
  485. assert_(np.all(norm_res < 1e-3))
  486. assert_allclose(sol.sol(sol.x), sol.y, rtol=1e-10, atol=1e-10)
  487. assert_allclose(sol.sol(sol.x, 1), sol.yp, rtol=1e-10, atol=1e-10)
  488. def test_nonlin_bc():
  489. x = np.linspace(0, 0.1, 5)
  490. x_test = x
  491. y = np.zeros([2, x.size])
  492. sol = solve_bvp(nonlin_bc_fun, nonlin_bc_bc, x, y)
  493. assert_equal(sol.status, 0)
  494. assert_(sol.success)
  495. assert_(sol.x.size < 8)
  496. sol_test = sol.sol(x_test)
  497. assert_allclose(sol_test[0], nonlin_bc_sol(x_test), rtol=1e-5, atol=1e-5)
  498. f_test = nonlin_bc_fun(x_test, sol_test)
  499. r = sol.sol(x_test, 1) - f_test
  500. rel_res = r / (1 + np.abs(f_test))
  501. norm_res = np.sum(rel_res ** 2, axis=0) ** 0.5
  502. assert_(np.all(norm_res < 1e-3))
  503. assert_allclose(sol.sol(sol.x), sol.y, rtol=1e-10, atol=1e-10)
  504. assert_allclose(sol.sol(sol.x, 1), sol.yp, rtol=1e-10, atol=1e-10)
  505. def test_verbose():
  506. # Smoke test that checks the printing does something and does not crash
  507. x = np.linspace(0, 1, 5)
  508. y = np.zeros((2, x.shape[0]))
  509. for verbose in [0, 1, 2]:
  510. old_stdout = sys.stdout
  511. sys.stdout = StringIO()
  512. try:
  513. sol = solve_bvp(exp_fun, exp_bc, x, y, verbose=verbose)
  514. text = sys.stdout.getvalue()
  515. finally:
  516. sys.stdout = old_stdout
  517. assert_(sol.success)
  518. if verbose == 0:
  519. assert_(not text, text)
  520. if verbose >= 1:
  521. assert_("Solved in" in text, text)
  522. if verbose >= 2:
  523. assert_("Max residual" in text, text)