Skip to main content

Analytical Jacobian builder for stencil-based systems (differnetial equation + boundary conditions)

Project description

PyJacAn – Automatic Analytic Jacobian Generator for Python

PyJacAn is an open-source Python package that automatically computes the analytic Jacobian matrix for systems of algebraic equations. This is particularly useful for solving systems with solvers like scipy.optimize.root that accept user-provided Jacobians, improving both accuracy and computational efficiency.



logo

Features

  • Automatic symbolic and numerical Jacobian generation using SymPy.
  • Supports single- and multi-equation systems, including differential and partial differential equations.
  • Works with user-defined residual functions and boundary conditions.
  • Reduces computation time and improves solver stability compared to numerical Jacobians.
  • Fully compatible with Python solvers accepting analytic Jacobians.

Installation

You can install PyJacAn in two ways:

1. Install via pip (recommended)

PyJacAn is pip-installable. Run:

pip install pyjacan

This will automatically install all dependencies:

  • numpy>=1.24
  • sympy>=1.12
  • scipy>=1.11
  • matplotlib>=3.8

2. Manual installation from GitHub

You can also clone the repository and install manually:

git clone https://github.com/mc8970/Function-to-Jacobian.git
cd Function-to-Jacobian
pip install -e .

This method also installs all required dependencies automatically.


Usage

from pyjacan import analytical_jacobian
import sympy as sym

# Define your residual functions
def leading(var, i, vars):
    # Example for coupled reaction-diffusion
    C, T = vars['C'], vars['T']
    dt, dx, D, alpha, gamma1, gamma2 = sym.symbols('dt dx D alpha gamma1 gamma2')
    if var == 'C':
        return C[i] + dt*(D*(C[i-1]-2*C[i]+C[i+1])/dx**2 - gamma2*T[i])
    elif var == 'T':
        return T[i] + dt*(alpha*(T[i-1]-2*T[i]+T[i+1])/dx**2 + gamma1*C[i])

# Define boundary conditions
def left_boundary(vars):
    return [vars['C'][0]-0.01, vars['T'][0]-293]

def right_boundary(vars):
    dx = sym.Symbol('dx')
    return [(vars['C'][-1]-vars['C'][-2])/dx, (vars['T'][-1]-vars['T'][-2])/dx]

# Define variables
var_and_lengths_dict = {'C':5, 'T':5}
lr_cut = [{'C':1,'T':1}, {'C':1,'T':1}]
values_dict = {'D':1e-9, 'alpha':1e-6, 'gamma1':0.05, 'gamma2':0.01, 'dt':0.1, 'dx':0.2}

# Generate Jacobian
J_sym, J_num = analytical_jacobian(leading, var_and_lengths_dict,
                        left_boundary, right_boundary,
                        lr_cut, values_dict)

Validation

PyJacAn has been tested with:

  1. Diffusion equation – verified against analytical Fourier series solutions.
  2. Coupled reaction–diffusion systems – multi-equation systems with mixed boundary conditions.
  3. Transient heat conduction – verified using automated Pytest unit tests.

Tests confirm that Jacobians are accurate, stable, and solver-ready, reducing computation time while preserving solution quality.


Running Tests

PyJacAn includes automated unit tests to ensure that the analytic Jacobian calculations are correct. These tests are implemented using Pytest.

Running all tests

From the project root directory, run:

python -m pytest -q

This will automatically discover all test files in the tests/ folder and report the results. For example, a passing test will show:

1 passed in 0.52s

Running a specific test file

To run only the test for the analytic Jacobian:

python -m pytest tests/test_analytical_jacobian.py -q

Notes

  • Tests are automatically set up to work with the src/pyjacan layout.
  • No additional configuration is needed if PyJacAn was installed via pip install -e . or dependencies were installed.
  • If a test fails, Pytest will show the failed assertion and the expected vs. computed values for easy debugging.

Dependencies


License


Citation

If you use PyJacAn in your research, please cite:

Jeraj, N. PyJacAn: Automatic Analytic Jacobian Generator, Zenodo, 2024. DOI: 10.5281/zenodo.14056930


Contact

For questions or feedback, contact the author via the GitHub repository.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyjacan-1.1.0.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyjacan-1.1.0-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file pyjacan-1.1.0.tar.gz.

File metadata

  • Download URL: pyjacan-1.1.0.tar.gz
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for pyjacan-1.1.0.tar.gz
Algorithm Hash digest
SHA256 249de38b7c6f1bf70d3d734b06d68c664cdb5ad8d3cead138d0564c09a35e317
MD5 d3294fd56d94f43fa339fb37193d0f96
BLAKE2b-256 04d970ac9d388d5cd6d7e259eb4386fd229141d9e6a2248ae5a686f298417954

See more details on using hashes here.

File details

Details for the file pyjacan-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: pyjacan-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for pyjacan-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3ae8f6b172254d3215d67ea4560e1fdee7d41c9cd511f4b82d9d305881b83cb4
MD5 f8ba3a3d79d9f1f9d3ef977752532ca9
BLAKE2b-256 67c2801099783170c7e5930da8b54001a33a9134bd374b088596c9c1f842d08c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page