Metadata-Version: 2.4
Name: daehn
Version: 2.0.0
Summary: Differential-algebraic neural modeling with hard constraint projections in JAX
Author: SOULS-TAMU contributors
License-Expression: Apache-2.0
Project-URL: Documentation, https://dae-hardnet.readthedocs.io/en/latest/
Project-URL: Homepage, https://github.com/SOULS-TAMU/dae-hardnet/tree/main
Project-URL: Repository, https://github.com/SOULS-TAMU/dae-hardnet
Project-URL: Issues, https://github.com/SOULS-TAMU/dae-hardnet/issues
Keywords: jax,constrained-learning,differentiable-optimization
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3.15
Classifier: Operating System :: OS Independent
Classifier: Intended Audience :: Science/Research
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
License-File: NOTICE
Requires-Dist: ohagi[export]<2.0.0,>=1.0.0
Requires-Dist: purin<2.0.0,>=1.0.0
Provides-Extra: dev
Requires-Dist: pytest>=8; extra == "dev"
Requires-Dist: build; extra == "dev"
Requires-Dist: ruff; extra == "dev"
Requires-Dist: osqp; extra == "dev"
Requires-Dist: absl-py; extra == "dev"
Requires-Dist: flatbuffers; extra == "dev"
Provides-Extra: docs
Requires-Dist: Sphinx<9,>=8.2; extra == "docs"
Requires-Dist: sphinx-rtd-theme<4,>=3.1; extra == "docs"
Requires-Dist: myst-parser<5,>=4; extra == "docs"
Provides-Extra: plotting
Requires-Dist: matplotlib>=3.8; extra == "plotting"
Provides-Extra: export
Requires-Dist: absl-py; extra == "export"
Requires-Dist: flatbuffers; extra == "export"
Provides-Extra: cuda12
Requires-Dist: jax[cuda12]>=0.4.30; extra == "cuda12"
Dynamic: license-file

# DAE-HardNet

**Learn differential-algebraic systems with differentiable hard constraint projections.**

[![CI](https://github.com/SOULS-TAMU/dae-hardnet/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/SOULS-TAMU/dae-hardnet/actions/workflows/ci.yml?query=branch%3Amain)
[![Validation](https://github.com/SOULS-TAMU/dae-hardnet/actions/workflows/validation.yml/badge.svg?branch=main)](https://github.com/SOULS-TAMU/dae-hardnet/actions/workflows/validation.yml?query=branch%3Amain)
[![CodeQL](https://github.com/SOULS-TAMU/dae-hardnet/actions/workflows/codeql.yml/badge.svg?branch=main)](https://github.com/SOULS-TAMU/dae-hardnet/actions/workflows/codeql.yml?query=branch%3Amain)
[![Dependency review](https://github.com/SOULS-TAMU/dae-hardnet/actions/workflows/dependency-review.yml/badge.svg?branch=main)](https://github.com/SOULS-TAMU/dae-hardnet/actions/workflows/dependency-review.yml?query=branch%3Amain)
[![Coverage](https://raw.githubusercontent.com/SOULS-TAMU/dae-hardnet/codex/coverage-main/coverage.svg)](https://github.com/SOULS-TAMU/dae-hardnet/tree/codex/coverage-main)

[![Python 3.10–3.15](https://img.shields.io/badge/Python-3.10%E2%80%933.15-3776AB?logo=python&logoColor=white)](https://github.com/SOULS-TAMU/dae-hardnet/blob/main/.github/workflows/release.yml)
[![CPU platforms](https://img.shields.io/badge/CPU-Linux%20%7C%20Windows%20%7C%20macOS-555555)](https://github.com/SOULS-TAMU/dae-hardnet/blob/main/.github/workflows/ci.yml)
[![JAX](https://img.shields.io/badge/Powered_by-JAX-8A2BE2)](https://docs.jax.dev/)
[![License: Apache 2.0](https://img.shields.io/badge/License-Apache_2.0-blue)](https://github.com/SOULS-TAMU/dae-hardnet/blob/main/LICENSE)
[![Citation: BibTeX](https://img.shields.io/badge/Cite-BibTeX-6F4E37)](https://github.com/SOULS-TAMU/dae-hardnet/blob/main/CITATION.bib)

Define coordinates, states, derivatives and algebraic constraints, then pass the
processed problem to OHAGI's shared learning engine and PURIN's projection solvers.

![DAE-HardNet architecture: neural predictions and differential terms pass through a KKT projection.](https://raw.githubusercontent.com/SOULS-TAMU/dae-hardnet/main/docs/_static/architecture.png)

- **Differential processing:** declared derivative specifications, discretization
  strategies and reconstruction of physical states and derivatives.
- **Hard algebraic constraints:** projected predictions under the selected solver's
  numerical tolerances.
- **Flexible backbones:** functional JAX networks and optional Flax adapters,
  with optional Orbax training checkpoints through OHAGI.
- **Reusable deployment:** a shared JAX bundle and the `DAEHardNet().load(...)` facade.

[Installation](#installation) · [Basic API](#basic-api) · [Load and predict](#load-and-predict) · [Documentation](#documentation) · [Contributing](#contributing) · [Citation](#citation)

## Installation

Requires **Python 3.10 or newer**. Install from PyPI:

```shell
python -m pip install daehn
```

For editable installs and optional Flax/Orbax integrations, see
[installation options](https://github.com/SOULS-TAMU/dae-hardnet/blob/main/docs/INSTALL.md).

## Basic API

### Learn a simple differential equation

Learn the decay law **dy/dt + y = 0** from observations of `y` and `dy/dt`.
The example is self-contained: create data, define the equation, prepare the
projection, train and predict.

```python
import jax
import numpy as np
from daehn import MLPBackbone
from daehn.dae import (
    Coordinate, DifferentialState, DerivativeSpec, EquationBlock, DAESystem,
    TaylorNeighborhood, compile_dae, DAETrainer, DAETrainingConfig,
)

jax.config.update("jax_enable_x64", True)

# 1. Data: time, observed state, observed derivative, physical parameters.
t = np.linspace(0, 1, 8)[:, None]
y = np.exp(-t)
dy = -y
p = np.empty((len(t), 0))  # This equation has no physical parameters.

# 2. Problem: declare y(t), dy/dt, and the residual dy/dt + y.
state = DifferentialState("y")
system = DAESystem(
    coordinates=(Coordinate("t"),),
    differential_states=(state,),
    derivatives=(DerivativeSpec(state, (1,)),),
    differential_equations=EquationBlock(lambda t, y, dy, p: dy + y, size=1),
)

# 3. Projection: process the differential equation using a Taylor neighborhood.
compiled = compile_dae(system, discretization=TaylorNeighborhood(deltas=(0.01,)))
projection = compiled.prepare_projection(batch_size=2)

# 4. Network and training: use the first four samples for training.
trainer = DAETrainer(
    compiled=compiled,
    prepared_projection=projection,
    backbone=MLPBackbone(hidden_sizes=(8,)),
    config=DAETrainingConfig(
        epochs=2, pretrain_epochs=1, batch_size=2, hidden_sizes=(8,), print_every=0,
    ),
)
result = trainer.fit(
    train_coordinates=t[:4], train_parameters=p[:4],
    train_targets=y[:4], train_derivative_targets=dy[:4],
    validation_coordinates=t[4:], validation_parameters=p[4:],
    validation_targets=y[4:], validation_derivative_targets=dy[4:],
)

# 5. Predict the physical state y at a new time.
_, prediction, *_ = trainer.predict(result.parameters, [[0.7]], np.empty((1, 0)))
print(prediction)
```

`DerivativeSpec(state, (1,))` declares the first time derivative. `EquationBlock`
returns residuals that should equal zero. `DAETrainingConfig` controls training
and loss weights; the tiny two-epoch budget above is only a runnable quick start.

Use `DAETrainer` to train differential models and `DAEHardNet` to load exported
inference models. See [problem definitions](https://github.com/SOULS-TAMU/dae-hardnet/blob/main/docs/PROBLEM.md) for algebraic
constraints, initial conditions, discretization and custom backbones.

## Load and predict

Load a previously exported physical-state deployment bundle:

```python
from daehn import DAEHardNet

model = DAEHardNet().load("deployment/metadata.json")
prediction = model.predict([0.7])
```

For differential models, export the desired physical-state output graph explicitly;
[the inference guide](https://github.com/SOULS-TAMU/dae-hardnet/blob/main/docs/INFERENCE.md) shows how to export the example above.

The same deployment bundle also works through OHAGI:

```python
from ohagi.inference import load

predictor = load("deployment/metadata.json")
for value in [0.4, 0.7, 1.0]:
    prediction = predictor.predict([value])
```

Keep the referenced `.jax_export` file with `metadata.json`. Load once and reuse
the predictor. `predict` returns synchronized NumPy values; `predict_jax` keeps
outputs on the JAX device. Deployment uses the shared JAX pathway.

## Data convention

Coordinates, physical parameters, states and derivatives are separate arrays,
with samples on the first axis and columns in declared/compiled order. Use
`(n_samples, 0)` when no physical parameters are present.

## The HardNet stack

- **[KKTHN](https://github.com/SOULS-TAMU/kkt-hardnet/tree/main),
  [DAEHN](https://github.com/SOULS-TAMU/dae-hardnet/tree/main) and
  [NLPOPTNet](https://github.com/SOULS-TAMU/nlpoptnet)** supply problem formulations
  and framework-specific processing.
- **[OHAGI](https://github.com/SOULS-TAMU/Ohagi/tree/development)** composes models,
  runs the shared learning engine, and handles checkpoints and JAX deployment.
- **[PURIN](https://github.com/SOULS-TAMU/Purin/tree/development)** owns projection
  problems, numerical solvers, sparse kernels and projection differentiation.

## Compatibility and validation

The PyPI CPU installation matrix covers **Python 3.10–3.15** on Linux, Windows and macOS,
plus Linux ARM64 on Python 3.13. It uses current compatible JAX releases;
regular CI additionally tests JAX 0.6.2 on Python 3.12. See the [release workflow](https://github.com/SOULS-TAMU/dae-hardnet/blob/main/.github/workflows/release.yml)
for exact environments; accelerator installation follows the
[JAX guide](https://docs.jax.dev/en/latest/installation.html).

- **CI:** tests, distribution builds and installation checks.
- **Validation:** public APIs, numerical references, documentation and code quality.
- **Coverage:** at least **90% combined statement and branch coverage**; the badge
  links to the measured report for this branch.
- **Security:** CodeQL analysis and dependency review.

The frontend validation policy also checks API compatibility, checkpoint resume,
types, dependency boundaries and cognitive complexity. See
[validation details](https://github.com/SOULS-TAMU/dae-hardnet/blob/main/.github/validation/README.md).

## Documentation

**[Read the full documentation](https://dae-hardnet.readthedocs.io/en/latest/)** — installation, tutorials, user guide and API reference.

The full Sphinx guide follows the Read the Docs style: [contents](https://github.com/SOULS-TAMU/dae-hardnet/blob/main/docs/index.rst),
[runnable examples](https://github.com/SOULS-TAMU/dae-hardnet/blob/main/docs/examples/index.rst), [API reference](https://github.com/SOULS-TAMU/dae-hardnet/blob/main/docs/api/index.rst)
and [build instructions](https://github.com/SOULS-TAMU/dae-hardnet/blob/main/docs/BUILD.md).

| Guide | Contents |
| --- | --- |
| [Installation](https://github.com/SOULS-TAMU/dae-hardnet/blob/main/docs/INSTALL.md) | Source installs, optional integrations and packaging |
| [Problem definitions](https://github.com/SOULS-TAMU/dae-hardnet/blob/main/docs/PROBLEM.md) | Modeling, backbones and training |
| [Inference](https://github.com/SOULS-TAMU/dae-hardnet/blob/main/docs/INFERENCE.md) | Export, loading and repeated prediction |
| [Version notes](https://github.com/SOULS-TAMU/dae-hardnet/blob/main/docs/VERSION.md) | Current development behavior |
| [Changelog](https://github.com/SOULS-TAMU/dae-hardnet/blob/main/CHANGELOG.md) | Recorded changes |

## Contributing

Bug reports, examples and focused pull requests are welcome. Read
[CONTRIBUTING.md](https://github.com/SOULS-TAMU/dae-hardnet/blob/main/CONTRIBUTING.md) for setup, numerical checks and compatibility
requirements. Keep generated results, environments and checkpoints out of Git.

## Citation

Use [CITATION.bib](https://github.com/SOULS-TAMU/dae-hardnet/blob/main/CITATION.bib) for a copyable BibTeX entry or GitHub's
**Cite this repository** menu for [citation metadata](https://github.com/SOULS-TAMU/dae-hardnet/blob/main/CITATION.cff).

## License

[Apache License 2.0](https://github.com/SOULS-TAMU/dae-hardnet/blob/main/LICENSE). Authorship and upstream attribution are recorded
in [AUTHORS](https://github.com/SOULS-TAMU/dae-hardnet/blob/main/AUTHORS) and [NOTICE](https://github.com/SOULS-TAMU/dae-hardnet/blob/main/NOTICE).

[Changelog](https://github.com/SOULS-TAMU/dae-hardnet/blob/main/CHANGELOG.md) · [Report an issue](https://github.com/SOULS-TAMU/dae-hardnet/issues)
