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 = 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.


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.0.0.tar.gz (5.8 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.0.0-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for pyjacan-1.0.0.tar.gz
Algorithm Hash digest
SHA256 b3d45a9ac6ecec4c15f18bc7a809cceb254953a24d0265873d14ecf2fb5e5254
MD5 02e460c7fe0df111c7de303aad99c521
BLAKE2b-256 5e2d18ee9f852e48a152e55bc9d81938768c29272853d978eda570fac6257a55

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for pyjacan-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7c2e507ca4e17b156630f5b3a0d40cacdb1230f2592829d2a9988d8a8525df56
MD5 63dc75135ed9c20b17172e98fb130e00
BLAKE2b-256 5455bd1581326de7650553476ac04830bef7cec0f64f8ed002add60d6a697dca

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