-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #95 from bjodah/sparse_jac
Sparse jac
- Loading branch information
Showing
12 changed files
with
338 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import numpy as np\n", | ||
"import matplotlib.pyplot as plt\n", | ||
"import sympy\n", | ||
"from pyodesys.symbolic import SymbolicSys\n", | ||
"from pyodesys.tests._robertson import get_ode_exprs\n", | ||
"sympy.init_printing()\n", | ||
"%matplotlib inline" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"logc, logt, reduced, powsimp = False, False, False, False\n", | ||
"odesys1 = SymbolicSys.from_callback(get_ode_exprs(logc, logt, reduced)[0],\n", | ||
" 2 if reduced else 3, 6 if reduced else 3)\n", | ||
"odesys1.exprs" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"odesys1._jac" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Converting to sparse representation of jacobian:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"sparse = SymbolicSys.from_other(odesys1, sparse=True)\n", | ||
"sparse.nnz, sparse._rowvals, sparse._colptrs" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"sparse._jac" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Converting back to dense representation of jacobian:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"odesys2 = SymbolicSys.from_other(sparse, sparse=False)\n", | ||
"assert odesys2.nnz == -1\n", | ||
"odesys2._jac" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Solving the initial value problem numerically:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"def solve_and_plot(odesys, integrate_kw, tout=[0, 1e11], y0=[1,0,0],\n", | ||
" params=[0.04, 1e4, 3e7], ax=None):\n", | ||
" result = odesys.integrate(tout, y0, params, nsteps=5000,\n", | ||
" integrator='cvode', **integrate_kw)\n", | ||
" result.plot(ax=ax, title_info=2)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import pycvodes\n", | ||
"has_klu = pycvodes.config['NO_KLU'] == '0'\n", | ||
"systems = [odesys1, sparse, odesys2]\n", | ||
"kws = [{}, dict(linear_solver='klu'), {}]\n", | ||
"fig, axes = plt.subplots(1, len(systems), figsize=(5*len(systems), 4),\n", | ||
" subplot_kw=dict(xscale='log', yscale='log'))\n", | ||
"for odesys, ax, kw in zip(systems, axes, kws):\n", | ||
" if kw.get('linear_solver', '') == 'klu' and not has_klu:\n", | ||
" continue\n", | ||
" solve_and_plot(odesys, kw, ax=ax)" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.