Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Tests

on:
push:
branches: [main]
pull_request:

permissions:
contents: read

jobs:
test:
name: py${{ matrix.python-version }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version: ["3.10", "3.11", "3.12"]

steps:
- uses: actions/checkout@v4

- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

# CPU torch only: the generator builds autograd graphs, it does not need a GPU,
# and the CUDA wheel is several gigabytes no runner here would use.
- name: Install
run: |
python -m pip install --upgrade pip
python -m pip install torch --index-url https://download.pytorch.org/whl/cpu
python -m pip install sympy pytest

- name: Run the tests
run: python -m pytest tests -q -rs
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,27 @@ this roadmap item with a small, self-contained addition.

## License
Apache License 2.0.
You may use, modify, and distribute this project under the terms of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0).
You may use, modify, and distribute this project under the terms of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0).

## Relation to `pinneapple_physics.symbolic_pde`

**Decision: this stays a small independent package, not a wrapper.**

The overlap is real and worth stating plainly: `pinneapple_physics.symbolic_pde` in the PINNeAPPle library converts SymPy PDE residuals into callable PyTorch autograd functions and provides hard and soft boundary conditions — the same job `pinn_generator.py` does in 129 lines.

The reasons to keep them separate:

- **Installation weight.** This package depends on `torch` and `sympy` and nothing else. The library is a git dependency of several hundred megabytes; turning this into a wrapper would mean that installing a 129-line helper pulls the whole toolkit.
- **A published API.** Version 0.1.0 is on PyPI. A wrapper that re-implements the same surface would either duplicate the code it was meant to remove, or break callers who use `PINNFactory` today.
- **An independent check.** Two implementations of the same translation, one of them small enough to read in one sitting, are easier to test against each other than one implementation with no second opinion.

The cost of this decision is duplication, and it is a real cost: a fix in one will not reach the other. If that ever becomes the reason to merge them, the merge should be into the library — not into a wrapper that hides the dependency.

## Tests

```bash
pip install -r requirements.txt pytest
pytest tests -q
```

Five tests, all of them about the generator rather than the examples: that a factory builds a loss from real SymPy strings and reports its components, that the loss is differentiable with respect to the network, that the data term is the mean squared error checked against a hand computation, that an inverse parameter is registered and receives a gradient, and that a loss weight scales only its own term.
Binary file added __pycache__/__init__.cpython-311.pyc
Binary file not shown.
Binary file added __pycache__/pinn_generator.cpython-311.pyc
Binary file not shown.
Binary file not shown.
114 changes: 114 additions & 0 deletions tests/test_pinn_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
"""Tests for the symbolic-to-loss generator.

The package is on PyPI, so the point of these is that a published version has something
checking it: that a factory builds a loss from real SymPy strings, that the loss is a
genuine differentiable function of the network's parameters, and that the data term is the
mean squared error it claims to be rather than something that merely looks like one.
"""
from __future__ import annotations

import sys
from pathlib import Path

import pytest
import torch

sys.path.insert(0, str(Path(__file__).resolve().parents[1]))

from pinn_generator import NeuralNetwork, PINN, PINNFactory # noqa: E402


def _heat_factory(**kwargs) -> PINNFactory:
return PINNFactory(
pde_residuals=["Derivative(u(t, x), t) - 0.1 * Derivative(u(t, x), x, 2)"],
conditions=[{"equation": "u(t, x) - 0"}],
independent_vars=["t", "x"],
dependent_vars=["u"],
**kwargs,
)


def _model(seed: int = 0, **kwargs) -> PINN:
torch.manual_seed(seed)
return PINN(NeuralNetwork(num_inputs=2, num_outputs=1, num_layers=2, num_neurons=8), **kwargs)


def test_a_factory_builds_a_loss_and_reports_its_components():
loss_fn = _heat_factory().generate_loss_function()
t = torch.rand(16, 1)
x = torch.rand(16, 1)

total, components = loss_fn(_model(), {"collocation": (t, x), "conditions": [(t, x)]})

assert torch.is_tensor(total)
assert torch.isfinite(total)
assert set(components) >= {"pde", "conditions", "total"}
assert components["pde"] >= 0 and components["conditions"] >= 0
assert total.item() == pytest.approx(components["total"])


def test_the_loss_is_differentiable_with_respect_to_the_network():
loss_fn = _heat_factory().generate_loss_function()
model = _model()
t, x = torch.rand(32, 1), torch.rand(32, 1)

total, _ = loss_fn(model, {"collocation": (t, x)})
total.backward()

gradients = [p.grad for p in model.parameters() if p.requires_grad]
assert gradients, "the network has no trainable parameters"
assert any(g is not None and torch.any(g != 0) for g in gradients), \
"the loss produced no gradient anywhere, so it is not training anything"


def test_the_data_term_is_the_mean_squared_error():
"""Checked against a hand computation, not against the value the code happens to return."""
loss_fn = PINNFactory(
pde_residuals=[], conditions=[], independent_vars=["t", "x"], dependent_vars=["u"],
).generate_loss_function()
model = _model()
t, x = torch.rand(20, 1), torch.rand(20, 1)
truth = torch.rand(20, 1)

total, components = loss_fn(model, {"data": ((t, x), truth)})

with torch.no_grad():
expected = torch.mean((model(t, x) - truth) ** 2)
assert components["data"] == pytest.approx(expected.item(), rel=1e-6)
assert total.item() == pytest.approx(expected.item(), rel=1e-6)


def test_inverse_parameters_are_registered_and_receive_a_gradient():
"""An inverse problem is only solvable if the unknown is a parameter the loss touches."""
model = _model(inverse_params_names=["alpha"], initial_guesses={"alpha": 0.3})
assert "alpha" in model.inverse_params
assert model.inverse_params["alpha"].item() == pytest.approx(0.3)

factory = PINNFactory(
pde_residuals=["Derivative(u(t, x), t) - alpha * Derivative(u(t, x), x, 2)"],
conditions=[{"equation": "u(t, x) - 0"}],
independent_vars=["t", "x"],
dependent_vars=["u"],
inverse_params=["alpha"],
)
t, x = torch.rand(16, 1), torch.rand(16, 1)

total, _ = factory.generate_loss_function()(model, {"collocation": (t, x)})
total.backward()

gradient = model.inverse_params["alpha"].grad
assert gradient is not None, "the inverse parameter never entered the loss"


def test_a_loss_weight_scales_only_its_own_term():
maker = lambda w: PINNFactory( # noqa: E731
pde_residuals=[], conditions=[], independent_vars=["t", "x"], dependent_vars=["u"],
loss_weights={"data": w},
).generate_loss_function()
model = _model()
t, x, truth = torch.rand(8, 1), torch.rand(8, 1), torch.rand(8, 1)

plain, _ = maker(1.0)(model, {"data": ((t, x), truth)})
doubled, _ = maker(2.0)(model, {"data": ((t, x), truth)})

assert doubled.item() == pytest.approx(2 * plain.item(), rel=1e-6)
Loading