physverify 0.4.1__tar.gz → 0.5.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- physverify-0.5.0/PKG-INFO +92 -0
- physverify-0.5.0/README.md +51 -0
- {physverify-0.4.1 → physverify-0.5.0}/pyproject.toml +5 -4
- {physverify-0.4.1 → physverify-0.5.0}/src/physverify/__init__.py +1 -1
- {physverify-0.4.1 → physverify-0.5.0}/src/physverify/checks.py +1 -0
- {physverify-0.4.1 → physverify-0.5.0}/src/physverify/cli.py +51 -20
- {physverify-0.4.1 → physverify-0.5.0}/src/physverify/constants.py +17 -0
- physverify-0.5.0/src/physverify/demo.py +75 -0
- physverify-0.5.0/src/physverify/identities.py +157 -0
- physverify-0.5.0/src/physverify/lint.py +428 -0
- {physverify-0.4.1 → physverify-0.5.0}/src/physverify/loaders.py +122 -0
- {physverify-0.4.1 → physverify-0.5.0}/src/physverify/order.py +30 -2
- {physverify-0.4.1 → physverify-0.5.0}/src/physverify/report.py +20 -2
- {physverify-0.4.1 → physverify-0.5.0}/src/physverify/scaffold.py +8 -0
- physverify-0.5.0/src/physverify/scan.py +1037 -0
- {physverify-0.4.1 → physverify-0.5.0}/src/physverify/studies.py +71 -1
- physverify-0.5.0/src/physverify.egg-info/PKG-INFO +92 -0
- {physverify-0.4.1 → physverify-0.5.0}/src/physverify.egg-info/SOURCES.txt +11 -18
- {physverify-0.4.1 → physverify-0.5.0}/src/physverify.egg-info/requires.txt +5 -0
- physverify-0.5.0/tests/test_awkward_input.py +74 -0
- physverify-0.5.0/tests/test_batch8_lessons.py +263 -0
- physverify-0.5.0/tests/test_docs_examples.py +28 -0
- physverify-0.5.0/tests/test_figures.py +14 -0
- {physverify-0.4.1 → physverify-0.5.0}/tests/test_ising.py +1 -1
- {physverify-0.4.1 → physverify-0.5.0}/tests/test_method_bugs.py +2 -2
- {physverify-0.4.1 → physverify-0.5.0}/tests/test_mms.py +1 -1
- {physverify-0.4.1 → physverify-0.5.0}/tests/test_new_problems.py +1 -1
- {physverify-0.4.1 → physverify-0.5.0}/tests/test_properties.py +2 -2
- {physverify-0.4.1 → physverify-0.5.0}/tests/test_real_projects.py +2 -2
- {physverify-0.4.1 → physverify-0.5.0}/tests/test_reference_problems.py +2 -2
- physverify-0.5.0/tests/test_scan.py +534 -0
- {physverify-0.4.1 → physverify-0.5.0}/tests/test_seeded_bugs.py +2 -2
- {physverify-0.4.1 → physverify-0.5.0}/tests/test_studies.py +1 -1
- physverify-0.5.0/tests/test_versions.py +21 -0
- physverify-0.4.1/PKG-INFO +0 -220
- physverify-0.4.1/README.md +0 -183
- physverify-0.4.1/src/physverify/adapters/__init__.py +0 -14
- physverify-0.4.1/src/physverify/adapters/double_pendulum.py +0 -55
- physverify-0.4.1/src/physverify/adapters/lorenz.py +0 -152
- physverify-0.4.1/src/physverify/adapters/projectile_v1.py +0 -92
- physverify-0.4.1/src/physverify/adapters/projectile_v2.py +0 -94
- physverify-0.4.1/src/physverify/adapters/three_body.py +0 -283
- physverify-0.4.1/src/physverify/compare.py +0 -104
- physverify-0.4.1/src/physverify/matrix.py +0 -160
- physverify-0.4.1/src/physverify/method_bugs.py +0 -266
- physverify-0.4.1/src/physverify/problems/__init__.py +0 -6
- physverify-0.4.1/src/physverify/problems/_common.py +0 -89
- physverify-0.4.1/src/physverify/problems/heat1d.py +0 -217
- physverify-0.4.1/src/physverify/problems/kepler.py +0 -176
- physverify-0.4.1/src/physverify/problems/pendulum.py +0 -201
- physverify-0.4.1/src/physverify/problems/projectile.py +0 -240
- physverify-0.4.1/src/physverify/problems/sod.py +0 -291
- physverify-0.4.1/src/physverify/stochastic/matrix.py +0 -151
- physverify-0.4.1/src/physverify.egg-info/PKG-INFO +0 -220
- {physverify-0.4.1 → physverify-0.5.0}/LICENSE +0 -0
- {physverify-0.4.1 → physverify-0.5.0}/setup.cfg +0 -0
- {physverify-0.4.1 → physverify-0.5.0}/src/physverify/__main__.py +0 -0
- {physverify-0.4.1 → physverify-0.5.0}/src/physverify/agent.py +0 -0
- {physverify-0.4.1 → physverify-0.5.0}/src/physverify/butcher.py +0 -0
- {physverify-0.4.1 → physverify-0.5.0}/src/physverify/mms.py +0 -0
- {physverify-0.4.1 → physverify-0.5.0}/src/physverify/pytest_plugin.py +0 -0
- {physverify-0.4.1 → physverify-0.5.0}/src/physverify/references.py +0 -0
- {physverify-0.4.1 → physverify-0.5.0}/src/physverify/smoke.py +0 -0
- {physverify-0.4.1 → physverify-0.5.0}/src/physverify/steppers.py +0 -0
- {physverify-0.4.1 → physverify-0.5.0}/src/physverify/stochastic/__init__.py +0 -0
- {physverify-0.4.1 → physverify-0.5.0}/src/physverify/stochastic/calibrate.py +0 -0
- {physverify-0.4.1 → physverify-0.5.0}/src/physverify/stochastic/checks.py +0 -0
- {physverify-0.4.1 → physverify-0.5.0}/src/physverify/stochastic/exact.py +0 -0
- {physverify-0.4.1 → physverify-0.5.0}/src/physverify/stochastic/ising.py +0 -0
- {physverify-0.4.1 → physverify-0.5.0}/src/physverify/stochastic/reference.py +0 -0
- {physverify-0.4.1 → physverify-0.5.0}/src/physverify/stochastic/sampler.py +0 -0
- {physverify-0.4.1 → physverify-0.5.0}/src/physverify/stochastic/stats.py +0 -0
- {physverify-0.4.1 → physverify-0.5.0}/src/physverify/symbolic.py +0 -0
- {physverify-0.4.1 → physverify-0.5.0}/src/physverify.egg-info/dependency_links.txt +0 -0
- {physverify-0.4.1 → physverify-0.5.0}/src/physverify.egg-info/entry_points.txt +0 -0
- {physverify-0.4.1 → physverify-0.5.0}/src/physverify.egg-info/top_level.txt +0 -0
- {physverify-0.4.1 → physverify-0.5.0}/tests/test_agent.py +0 -0
- {physverify-0.4.1 → physverify-0.5.0}/tests/test_batch7_lessons.py +0 -0
- {physverify-0.4.1 → physverify-0.5.0}/tests/test_butcher_properties.py +0 -0
- {physverify-0.4.1 → physverify-0.5.0}/tests/test_cli.py +0 -0
- {physverify-0.4.1 → physverify-0.5.0}/tests/test_corpus_lessons.py +0 -0
- {physverify-0.4.1 → physverify-0.5.0}/tests/test_order.py +0 -0
- {physverify-0.4.1 → physverify-0.5.0}/tests/test_references.py +0 -0
- {physverify-0.4.1 → physverify-0.5.0}/tests/test_scaffold.py +0 -0
- {physverify-0.4.1 → physverify-0.5.0}/tests/test_v040_helpers.py +0 -0
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: physverify
|
|
3
|
+
Version: 0.5.0
|
|
4
|
+
Summary: Verification checks for physics simulation code: manufactured solutions, order-of-accuracy studies and a seeded-bug benchmark.
|
|
5
|
+
Author: Ram Chandra Giri
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/rcgiri-physics/physverify
|
|
8
|
+
Project-URL: Documentation, https://rcgiri-physics.github.io/physverify/
|
|
9
|
+
Project-URL: Issues, https://github.com/rcgiri-physics/physverify/issues
|
|
10
|
+
Project-URL: Changelog, https://github.com/rcgiri-physics/physverify/blob/main/CHANGELOG.md
|
|
11
|
+
Keywords: verification,method of manufactured solutions,numerical methods,physics,simulation,testing
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Topic :: Scientific/Engineering :: Physics
|
|
14
|
+
Classifier: Framework :: Pytest
|
|
15
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
21
|
+
Classifier: Operating System :: OS Independent
|
|
22
|
+
Classifier: Intended Audience :: Science/Research
|
|
23
|
+
Classifier: Intended Audience :: Education
|
|
24
|
+
Classifier: Topic :: Software Development :: Testing
|
|
25
|
+
Classifier: Development Status :: 3 - Alpha
|
|
26
|
+
Requires-Python: >=3.10
|
|
27
|
+
Description-Content-Type: text/markdown
|
|
28
|
+
License-File: LICENSE
|
|
29
|
+
Requires-Dist: numpy>=1.24
|
|
30
|
+
Provides-Extra: symbolic
|
|
31
|
+
Requires-Dist: sympy>=1.12; extra == "symbolic"
|
|
32
|
+
Provides-Extra: dev
|
|
33
|
+
Requires-Dist: pytest>=8; extra == "dev"
|
|
34
|
+
Requires-Dist: pytest-cov>=6.1; extra == "dev"
|
|
35
|
+
Requires-Dist: hypothesis>=6; extra == "dev"
|
|
36
|
+
Requires-Dist: sympy>=1.12; extra == "dev"
|
|
37
|
+
Provides-Extra: docs
|
|
38
|
+
Requires-Dist: mkdocs<2,>=1.6; extra == "docs"
|
|
39
|
+
Requires-Dist: mkdocs-material<10,>=9.5; extra == "docs"
|
|
40
|
+
Dynamic: license-file
|
|
41
|
+
|
|
42
|
+
# physverify
|
|
43
|
+
|
|
44
|
+
[](https://github.com/rcgiri-physics/physverify/actions/workflows/ci.yml) [](https://pypi.org/project/physverify/) [](https://pypi.org/project/physverify/) [](https://doi.org/10.5281/zenodo.23534512) [](https://rcgiri-physics.github.io/physverify/)
|
|
45
|
+
|
|
46
|
+
**Checks that a simulation solves the equations it claims, at the order of accuracy it claims, not just that it runs.**
|
|
47
|
+
|
|
48
|
+
physverify is actively evolving (v0.5.0), welcoming contributions and test cases from the computational physics community. Scope: numerical solutions of ODEs and PDEs (time integration, convergence, conservation), plus a few Monte Carlo checks. It needs only NumPy.
|
|
49
|
+
|
|
50
|
+

|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
pip install physverify
|
|
54
|
+
physverify demo
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
The demo checks a correct solver (all checks pass), then the same solver with the damping 10% too large (all fail):
|
|
58
|
+
|
|
59
|
+

|
|
60
|
+
|
|
61
|
+
New to this? [Start here](https://rcgiri-physics.github.io/physverify/start-here/) walks through installation and a first check in about ten minutes, with a prompt to paste if you use an AI coding assistant.
|
|
62
|
+
|
|
63
|
+
## Use it on your own code
|
|
64
|
+
|
|
65
|
+
- **Any project, no set-up:** `physverify scan PATH` reads the code (it runs nothing) and flags Runge-Kutta methods that do not have their stated order (written as tables, as class attributes or as stages in code), grid spacings that do not match their grid, near-miss physical constants, and inverse function pairs worth a round-trip test.
|
|
66
|
+
- **An ODE solver:** `physverify init verify_solver.py --solver mysolver --order 4` writes a spec; fill in the equations (from the physics, not from your code) and run `physverify run verify_solver.py`.
|
|
67
|
+
- **A Runge-Kutta table:** `check_tableau(A, b, c, claimed=3)` from `physverify.butcher` checks its order, stage order and stability from the coefficients alone.
|
|
68
|
+
- **Errors you already have:** `check_order(hs, errors, expected=2)` gives PASS, FAIL or INCONCLUSIVE with a reason.
|
|
69
|
+
|
|
70
|
+
## Run it in CI
|
|
71
|
+
|
|
72
|
+
A failed check fails the job, like a broken unit test ([CI guide](https://rcgiri-physics.github.io/physverify/ci/): options, pre-commit, pytest):
|
|
73
|
+
|
|
74
|
+
```yaml
|
|
75
|
+
- uses: rcgiri-physics/physverify@v0.5.0
|
|
76
|
+
with:
|
|
77
|
+
specs: tests/verify_*.py
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
[Getting started](https://rcgiri-physics.github.io/physverify/getting-started/) · [Recipes](https://rcgiri-physics.github.io/physverify/recipes/) · [What a PASS means](https://rcgiri-physics.github.io/physverify/assumptions/) · [Troubleshooting](https://rcgiri-physics.github.io/physverify/troubleshooting/) · [API](https://rcgiri-physics.github.io/physverify/api/)
|
|
81
|
+
|
|
82
|
+
## Does it catch bugs?
|
|
83
|
+
|
|
84
|
+
Of 37 bugs seeded into five small solvers, it flagged 35 (plain "compare with one answer" tests: 27), with no false alarms. The bugs and checks were written by the same person, so this shows the tool works as designed, not how it does on other people's code. On public physics repositories, 74 defects were reported to authors; 14 of them were found by a physverify check as such, 11 by simply running the code, and the rest by the per-repository test scripts or by reading the code. `physverify scan` alone, with no script, now finds 18 of the 74, up from 2 in its first version, and found 7 new defects in the same repositories ([which found what](https://github.com/rcgiri-physics/physverify/blob/main/results/corpus/FINDINGS_BY_METHOD.md)). The counts describe that sample only and are not a bug rate. Details: [results](https://rcgiri-physics.github.io/physverify/results/), and [results/README.md](https://github.com/rcgiri-physics/physverify/blob/main/results/README.md) to regenerate every table.
|
|
85
|
+
|
|
86
|
+
## Limits
|
|
87
|
+
|
|
88
|
+
Works best for ODEs; PDEs only in 1D so far. Your code needs a way to add a source term, or a small wrapper that can itself be wrong. A PASS means these checks found nothing wrong; it does not prove the simulation correct, and it is only as independent as the reference you wrote: see [What a PASS means](https://rcgiri-physics.github.io/physverify/assumptions/).
|
|
89
|
+
|
|
90
|
+
## Contributing, citing, licence
|
|
91
|
+
|
|
92
|
+
Help is welcome: a trial report on your own solver, seeded bugs written by someone other than the author, adapters, documentation. See [CONTRIBUTING.md](https://github.com/rcgiri-physics/physverify/blob/main/CONTRIBUTING.md), the [code of conduct](https://github.com/rcgiri-physics/physverify/blob/main/CODE_OF_CONDUCT.md), [SUPPORT.md](https://github.com/rcgiri-physics/physverify/blob/main/SUPPORT.md) for help, [SECURITY.md](https://github.com/rcgiri-physics/physverify/blob/main/SECURITY.md) on running untrusted code, and [AGENTS.md](https://github.com/rcgiri-physics/physverify/blob/main/AGENTS.md) for AI assistants. To cite, use [doi:10.5281/zenodo.23534512](https://doi.org/10.5281/zenodo.23534512) or [CITATION.cff](https://github.com/rcgiri-physics/physverify/blob/main/CITATION.cff). Changes: [CHANGELOG.md](https://github.com/rcgiri-physics/physverify/blob/main/CHANGELOG.md); plans: [ROADMAP.md](https://github.com/rcgiri-physics/physverify/blob/main/ROADMAP.md). MIT licence, copyright (c) 2026 Ram Chandra Giri.
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# physverify
|
|
2
|
+
|
|
3
|
+
[](https://github.com/rcgiri-physics/physverify/actions/workflows/ci.yml) [](https://pypi.org/project/physverify/) [](https://pypi.org/project/physverify/) [](https://doi.org/10.5281/zenodo.23534512) [](https://rcgiri-physics.github.io/physverify/)
|
|
4
|
+
|
|
5
|
+
**Checks that a simulation solves the equations it claims, at the order of accuracy it claims, not just that it runs.**
|
|
6
|
+
|
|
7
|
+
physverify is actively evolving (v0.5.0), welcoming contributions and test cases from the computational physics community. Scope: numerical solutions of ODEs and PDEs (time integration, convergence, conservation), plus a few Monte Carlo checks. It needs only NumPy.
|
|
8
|
+
|
|
9
|
+

|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
pip install physverify
|
|
13
|
+
physverify demo
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
The demo checks a correct solver (all checks pass), then the same solver with the damping 10% too large (all fail):
|
|
17
|
+
|
|
18
|
+

|
|
19
|
+
|
|
20
|
+
New to this? [Start here](https://rcgiri-physics.github.io/physverify/start-here/) walks through installation and a first check in about ten minutes, with a prompt to paste if you use an AI coding assistant.
|
|
21
|
+
|
|
22
|
+
## Use it on your own code
|
|
23
|
+
|
|
24
|
+
- **Any project, no set-up:** `physverify scan PATH` reads the code (it runs nothing) and flags Runge-Kutta methods that do not have their stated order (written as tables, as class attributes or as stages in code), grid spacings that do not match their grid, near-miss physical constants, and inverse function pairs worth a round-trip test.
|
|
25
|
+
- **An ODE solver:** `physverify init verify_solver.py --solver mysolver --order 4` writes a spec; fill in the equations (from the physics, not from your code) and run `physverify run verify_solver.py`.
|
|
26
|
+
- **A Runge-Kutta table:** `check_tableau(A, b, c, claimed=3)` from `physverify.butcher` checks its order, stage order and stability from the coefficients alone.
|
|
27
|
+
- **Errors you already have:** `check_order(hs, errors, expected=2)` gives PASS, FAIL or INCONCLUSIVE with a reason.
|
|
28
|
+
|
|
29
|
+
## Run it in CI
|
|
30
|
+
|
|
31
|
+
A failed check fails the job, like a broken unit test ([CI guide](https://rcgiri-physics.github.io/physverify/ci/): options, pre-commit, pytest):
|
|
32
|
+
|
|
33
|
+
```yaml
|
|
34
|
+
- uses: rcgiri-physics/physverify@v0.5.0
|
|
35
|
+
with:
|
|
36
|
+
specs: tests/verify_*.py
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
[Getting started](https://rcgiri-physics.github.io/physverify/getting-started/) · [Recipes](https://rcgiri-physics.github.io/physverify/recipes/) · [What a PASS means](https://rcgiri-physics.github.io/physverify/assumptions/) · [Troubleshooting](https://rcgiri-physics.github.io/physverify/troubleshooting/) · [API](https://rcgiri-physics.github.io/physverify/api/)
|
|
40
|
+
|
|
41
|
+
## Does it catch bugs?
|
|
42
|
+
|
|
43
|
+
Of 37 bugs seeded into five small solvers, it flagged 35 (plain "compare with one answer" tests: 27), with no false alarms. The bugs and checks were written by the same person, so this shows the tool works as designed, not how it does on other people's code. On public physics repositories, 74 defects were reported to authors; 14 of them were found by a physverify check as such, 11 by simply running the code, and the rest by the per-repository test scripts or by reading the code. `physverify scan` alone, with no script, now finds 18 of the 74, up from 2 in its first version, and found 7 new defects in the same repositories ([which found what](https://github.com/rcgiri-physics/physverify/blob/main/results/corpus/FINDINGS_BY_METHOD.md)). The counts describe that sample only and are not a bug rate. Details: [results](https://rcgiri-physics.github.io/physverify/results/), and [results/README.md](https://github.com/rcgiri-physics/physverify/blob/main/results/README.md) to regenerate every table.
|
|
44
|
+
|
|
45
|
+
## Limits
|
|
46
|
+
|
|
47
|
+
Works best for ODEs; PDEs only in 1D so far. Your code needs a way to add a source term, or a small wrapper that can itself be wrong. A PASS means these checks found nothing wrong; it does not prove the simulation correct, and it is only as independent as the reference you wrote: see [What a PASS means](https://rcgiri-physics.github.io/physverify/assumptions/).
|
|
48
|
+
|
|
49
|
+
## Contributing, citing, licence
|
|
50
|
+
|
|
51
|
+
Help is welcome: a trial report on your own solver, seeded bugs written by someone other than the author, adapters, documentation. See [CONTRIBUTING.md](https://github.com/rcgiri-physics/physverify/blob/main/CONTRIBUTING.md), the [code of conduct](https://github.com/rcgiri-physics/physverify/blob/main/CODE_OF_CONDUCT.md), [SUPPORT.md](https://github.com/rcgiri-physics/physverify/blob/main/SUPPORT.md) for help, [SECURITY.md](https://github.com/rcgiri-physics/physverify/blob/main/SECURITY.md) on running untrusted code, and [AGENTS.md](https://github.com/rcgiri-physics/physverify/blob/main/AGENTS.md) for AI assistants. To cite, use [doi:10.5281/zenodo.23534512](https://doi.org/10.5281/zenodo.23534512) or [CITATION.cff](https://github.com/rcgiri-physics/physverify/blob/main/CITATION.cff). Changes: [CHANGELOG.md](https://github.com/rcgiri-physics/physverify/blob/main/CHANGELOG.md); plans: [ROADMAP.md](https://github.com/rcgiri-physics/physverify/blob/main/ROADMAP.md). MIT licence, copyright (c) 2026 Ram Chandra Giri.
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "physverify"
|
|
7
|
-
version = "0.
|
|
7
|
+
version = "0.5.0"
|
|
8
8
|
description = "Verification checks for physics simulation code: manufactured solutions, order-of-accuracy studies and a seeded-bug benchmark."
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
requires-python = ">=3.10"
|
|
@@ -32,13 +32,14 @@ dependencies = ["numpy>=1.24"]
|
|
|
32
32
|
|
|
33
33
|
[project.urls]
|
|
34
34
|
Homepage = "https://github.com/rcgiri-physics/physverify"
|
|
35
|
-
Documentation = "https://
|
|
35
|
+
Documentation = "https://rcgiri-physics.github.io/physverify/"
|
|
36
36
|
Issues = "https://github.com/rcgiri-physics/physverify/issues"
|
|
37
37
|
Changelog = "https://github.com/rcgiri-physics/physverify/blob/main/CHANGELOG.md"
|
|
38
38
|
|
|
39
39
|
[project.optional-dependencies]
|
|
40
40
|
symbolic = ["sympy>=1.12"]
|
|
41
|
-
dev = ["pytest>=8", "hypothesis>=6", "sympy>=1.12"]
|
|
41
|
+
dev = ["pytest>=8", "pytest-cov>=6.1", "hypothesis>=6", "sympy>=1.12"]
|
|
42
|
+
docs = ["mkdocs>=1.6,<2", "mkdocs-material>=9.5,<10"]
|
|
42
43
|
|
|
43
44
|
[project.scripts]
|
|
44
45
|
physverify = "physverify.cli:main"
|
|
@@ -51,4 +52,4 @@ where = ["src"]
|
|
|
51
52
|
|
|
52
53
|
[tool.pytest.ini_options]
|
|
53
54
|
testpaths = ["tests"]
|
|
54
|
-
pythonpath = ["src"]
|
|
55
|
+
pythonpath = ["src", "benchmark"]
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
"""Command line.
|
|
2
2
|
|
|
3
3
|
physverify run SPEC.py [SPEC.py ...] [--json FILE|-] [--html FILE] [--markdown FILE] [--quiet]
|
|
4
|
+
physverify demo (a correct and a broken solver, no files needed)
|
|
4
5
|
physverify init [SPEC.py] [--solver MODULE] [--function NAME] [--dim N] [--order P]
|
|
5
|
-
physverify
|
|
6
|
-
|
|
6
|
+
physverify scan PATH (reads the source, runs nothing: constants, RK tables, grid spacing, inverse pairs,
|
|
7
|
+
undefined names, removed APIs, typo-like literals, unit tables)
|
|
7
8
|
physverify hook (Claude Code PostToolUse hook; see docs/agent.md)
|
|
8
9
|
physverify mcp (MCP server on stdio; see docs/agent.md)
|
|
9
10
|
|
|
@@ -42,7 +43,7 @@ def expand_specs(patterns) -> list[str]:
|
|
|
42
43
|
|
|
43
44
|
|
|
44
45
|
def _cmd_run(args) -> int:
|
|
45
|
-
from .report import load_spec, run_checks, to_html, to_json, to_markdown, to_text
|
|
46
|
+
from .report import NOTE, load_spec, run_checks, to_html, to_json, to_markdown, to_text
|
|
46
47
|
|
|
47
48
|
try:
|
|
48
49
|
args.specs = expand_specs(args.specs)
|
|
@@ -56,6 +57,9 @@ def _cmd_run(args) -> int:
|
|
|
56
57
|
except Exception as exc:
|
|
57
58
|
print(f"physverify: cannot load {spec}: {type(exc).__name__}: {exc}", file=sys.stderr)
|
|
58
59
|
return 2
|
|
60
|
+
if not checks:
|
|
61
|
+
print(f"physverify: {spec}: checks() returned no checks, so nothing was tested", file=sys.stderr)
|
|
62
|
+
return 2
|
|
59
63
|
outcomes += run_checks(checks, args.budget)
|
|
60
64
|
source = ", ".join(str(Path(s)) for s in args.specs)
|
|
61
65
|
if args.json == "-":
|
|
@@ -63,6 +67,7 @@ def _cmd_run(args) -> int:
|
|
|
63
67
|
else:
|
|
64
68
|
if not args.quiet:
|
|
65
69
|
print(to_text(outcomes))
|
|
70
|
+
print(NOTE)
|
|
66
71
|
if args.json:
|
|
67
72
|
Path(args.json).write_text(to_json(outcomes, source), encoding="utf-8")
|
|
68
73
|
if args.html:
|
|
@@ -87,16 +92,10 @@ def _cmd_init(args) -> int:
|
|
|
87
92
|
return 0
|
|
88
93
|
|
|
89
94
|
|
|
90
|
-
def
|
|
91
|
-
from . import
|
|
92
|
-
|
|
93
|
-
return matrix.main([args.out] if args.out else [])
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
def _cmd_compare(args) -> int:
|
|
97
|
-
from . import compare
|
|
95
|
+
def _cmd_demo(args) -> int:
|
|
96
|
+
from .demo import main as demo_main
|
|
98
97
|
|
|
99
|
-
return
|
|
98
|
+
return demo_main()
|
|
100
99
|
|
|
101
100
|
|
|
102
101
|
def _cmd_hook(args) -> int:
|
|
@@ -123,6 +122,37 @@ def _cmd_smoke(args) -> int:
|
|
|
123
122
|
return 1 if bad else 0
|
|
124
123
|
|
|
125
124
|
|
|
125
|
+
def _cmd_scan(args) -> int:
|
|
126
|
+
import json
|
|
127
|
+
|
|
128
|
+
from .scan import STAGES, WARN, scan, to_dict
|
|
129
|
+
|
|
130
|
+
stages = args.only.split(",") if args.only else STAGES
|
|
131
|
+
unknown = set(stages) - set(STAGES)
|
|
132
|
+
if unknown:
|
|
133
|
+
print(f"physverify: unknown stage {', '.join(sorted(unknown))}; choose from {', '.join(STAGES)}", file=sys.stderr)
|
|
134
|
+
return 2
|
|
135
|
+
if not Path(args.path).exists():
|
|
136
|
+
print(f"physverify: {args.path} does not exist", file=sys.stderr)
|
|
137
|
+
return 2
|
|
138
|
+
findings, unparsed = scan(args.path, stages=stages)
|
|
139
|
+
if args.json:
|
|
140
|
+
text = json.dumps(to_dict(findings, unparsed), indent=1)
|
|
141
|
+
if args.json == "-":
|
|
142
|
+
print(text)
|
|
143
|
+
else:
|
|
144
|
+
Path(args.json).write_text(text, encoding="utf-8")
|
|
145
|
+
if args.json != "-":
|
|
146
|
+
for f in findings:
|
|
147
|
+
print(f)
|
|
148
|
+
for u in unparsed:
|
|
149
|
+
print(f"NOTE parse {u}")
|
|
150
|
+
warn = sum(f.level == WARN for f in findings)
|
|
151
|
+
print(f"{warn} warnings, {len(findings) - warn} notes, {len(unparsed)} files not parsed"
|
|
152
|
+
" (the scan reads the code and runs nothing; a clean scan means only that these patterns were not found)")
|
|
153
|
+
return 1 if any(f.level == WARN for f in findings) else 0
|
|
154
|
+
|
|
155
|
+
|
|
126
156
|
def _cmd_references(args) -> int:
|
|
127
157
|
from . import references
|
|
128
158
|
|
|
@@ -149,6 +179,9 @@ def build_parser() -> argparse.ArgumentParser:
|
|
|
149
179
|
" and a check still running at twice the budget is reported INCONCLUSIVE")
|
|
150
180
|
run.set_defaults(func=_cmd_run)
|
|
151
181
|
|
|
182
|
+
d = sub.add_parser("demo", help="run the checks on a correct and on a broken example solver (no files needed)")
|
|
183
|
+
d.set_defaults(func=_cmd_demo)
|
|
184
|
+
|
|
152
185
|
i = sub.add_parser("init", help="write a starter spec for a fixed-step ODE solver")
|
|
153
186
|
i.add_argument("spec", nargs="?", default="verify_solver.py", help="spec file to write (default verify_solver.py)")
|
|
154
187
|
i.add_argument("--solver", default="solver", help="module that holds the solver, importable from the spec's folder (default solver)")
|
|
@@ -159,14 +192,6 @@ def build_parser() -> argparse.ArgumentParser:
|
|
|
159
192
|
i.add_argument("--force", action="store_true", help="overwrite an existing file")
|
|
160
193
|
i.set_defaults(func=_cmd_init)
|
|
161
194
|
|
|
162
|
-
m = sub.add_parser("matrix", help="run the seeded-bug benchmark")
|
|
163
|
-
m.add_argument("out", nargs="?", help="also write the tables next to this Markdown file")
|
|
164
|
-
m.set_defaults(func=_cmd_matrix)
|
|
165
|
-
|
|
166
|
-
c = sub.add_parser("compare", help="compare the families with plain tests on the seeded bugs")
|
|
167
|
-
c.add_argument("out", nargs="?", help="also write the table to this Markdown file")
|
|
168
|
-
c.set_defaults(func=_cmd_compare)
|
|
169
|
-
|
|
170
195
|
h = sub.add_parser("hook", help="Claude Code PostToolUse hook: reads the event on stdin, exits 2 on a failed check")
|
|
171
196
|
h.set_defaults(func=_cmd_hook)
|
|
172
197
|
|
|
@@ -175,6 +200,12 @@ def build_parser() -> argparse.ArgumentParser:
|
|
|
175
200
|
sm.add_argument("--timeout", type=float, default=60.0, help="seconds per script (default 60)")
|
|
176
201
|
sm.set_defaults(func=_cmd_smoke)
|
|
177
202
|
|
|
203
|
+
sc = sub.add_parser("scan", help="look for common defects without a spec file; reads the code and runs nothing")
|
|
204
|
+
sc.add_argument("path", help="a Python file or a folder")
|
|
205
|
+
sc.add_argument("--only", metavar="STAGES", help="comma-separated stages: constants,tables,grid,pairs,names,api,literals,units (default all)")
|
|
206
|
+
sc.add_argument("--json", metavar="FILE", help="write findings as JSON to FILE, or to stdout with -")
|
|
207
|
+
sc.set_defaults(func=_cmd_scan)
|
|
208
|
+
|
|
178
209
|
r = sub.add_parser("references", help="list the exact reference solutions in physverify.references")
|
|
179
210
|
r.set_defaults(func=_cmd_references)
|
|
180
211
|
|
|
@@ -34,6 +34,23 @@ KNOWN = {
|
|
|
34
34
|
"m_p": ((1.67262192369e-27, 1.67262192595e-27), "kg", "CODATA 2018: 1.67262192369e-27, 2022: 1.67262192595e-27"),
|
|
35
35
|
"epsilon_0": ((8.8541878128e-12,), "F/m", "8.8541878128e-12"),
|
|
36
36
|
"T_c (2D Ising)": ((2 / math.log(1 + math.sqrt(2)),), "J/k_B", "2 / ln(1 + sqrt 2) = 2.269185314"),
|
|
37
|
+
# exact by definition (SI 2019, IAU 2012, international units)
|
|
38
|
+
"N_A": ((6.02214076e23,), "1/mol", "exact: 6.02214076e23"),
|
|
39
|
+
"R (gas constant)": ((8.314462618,), "J/(mol K)", "N_A k_B = 8.314462618..."),
|
|
40
|
+
"sigma (Stefan-Boltzmann)": ((5.670374419e-8,), "W m^-2 K^-4", "5.670374419e-8"),
|
|
41
|
+
"astronomical unit": ((149597870700.0, 149597870.7, 1.495978707e13), "m, km or cm", "exact: 149597870700 m (IAU 2012)"),
|
|
42
|
+
"light year": ((9460730472580800.0,), "m", "exact: 9460730472580800 m (Julian year)"),
|
|
43
|
+
"parsec": ((3.0856775814913673e16,), "m", "648000/pi au = 3.0856775814913673e16 m"),
|
|
44
|
+
"standard atmosphere": ((101325.0,), "Pa", "exact: 101325 Pa"),
|
|
45
|
+
"torr": ((101325 / 760,), "Pa", "exact: 101325/760 = 133.32236842 Pa (mmHg is 133.322387415 Pa)"),
|
|
46
|
+
"nautical mile": ((1852.0,), "m", "exact: 1852 m"),
|
|
47
|
+
# the same constants in CGS, so a CGS block can be compared with an SI one
|
|
48
|
+
"h (CGS)": ((6.62607015e-27,), "erg s", "exact: 6.62607015e-27"),
|
|
49
|
+
"k_B (CGS)": ((1.380649e-16,), "erg/K", "exact: 1.380649e-16"),
|
|
50
|
+
"c (CGS)": ((2.99792458e10,), "cm/s", "exact: 2.99792458e10"),
|
|
51
|
+
"e (esu)": ((4.803204712570263e-10,), "statC", "4.803204712570263e-10"),
|
|
52
|
+
"m_e (g)": ((9.1093837015e-28, 9.1093837139e-28), "g", "CODATA 2018: 9.1093837015e-28, 2022: 9.1093837139e-28"),
|
|
53
|
+
"G (CGS)": ((6.67430e-8, 6.674e-8), "cm^3 g^-1 s^-2", "CODATA 2018: 6.67430e-8"),
|
|
37
54
|
}
|
|
38
55
|
|
|
39
56
|
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"""`physverify demo`: the damped-oscillator example, shipped in the package.
|
|
2
|
+
|
|
3
|
+
It runs the checks on a correct midpoint solver, then on a copy whose damping
|
|
4
|
+
is 10% too large, so a first run needs only `pip install physverify`.
|
|
5
|
+
The same solver and spec, as files to copy, are in examples/ in the repository.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import numpy as np
|
|
11
|
+
|
|
12
|
+
from .studies import ode_checks
|
|
13
|
+
from .mms import ManufacturedODE
|
|
14
|
+
|
|
15
|
+
OMEGA, GAMMA = 3.0, 0.4
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def make_solver(damping_factor: float = 1.0):
|
|
19
|
+
"""Midpoint method for x'' = -omega^2 x - 2 gamma x', with an additive source term."""
|
|
20
|
+
|
|
21
|
+
def rhs(t, y):
|
|
22
|
+
damping = 2.0 * GAMMA * damping_factor
|
|
23
|
+
return np.array([y[1], -(OMEGA**2) * y[0] - damping * y[1]])
|
|
24
|
+
|
|
25
|
+
def integrate(source, y0, t_end, n_steps, trajectory=False):
|
|
26
|
+
h = t_end / n_steps
|
|
27
|
+
y = np.array(y0, dtype=float)
|
|
28
|
+
states = [y]
|
|
29
|
+
for i in range(n_steps):
|
|
30
|
+
t = i * h
|
|
31
|
+
k1 = rhs(t, y) + source(t)
|
|
32
|
+
mid = y + 0.5 * h * k1
|
|
33
|
+
y = y + h * (rhs(t + 0.5 * h, mid) + source(t + 0.5 * h))
|
|
34
|
+
states.append(y)
|
|
35
|
+
return np.array(states) if trajectory else y
|
|
36
|
+
|
|
37
|
+
return integrate
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def reference_rhs(t, y):
|
|
41
|
+
"""The equations, written from the physics, not copied from the solver."""
|
|
42
|
+
return np.array([y[1], -(OMEGA**2) * y[0] - 2.0 * GAMMA * y[1]])
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def checks(damping_factor: float = 1.0):
|
|
46
|
+
integrate = make_solver(damping_factor)
|
|
47
|
+
mms = ManufacturedODE(lambda t: np.array([np.sin(1.3 * t) + 0.2, 1.04 * np.cos(1.3 * t)]), reference_rhs)
|
|
48
|
+
return ode_checks(
|
|
49
|
+
"midpoint damped oscillator",
|
|
50
|
+
integrate,
|
|
51
|
+
rhs_ref=reference_rhs,
|
|
52
|
+
mms=mms,
|
|
53
|
+
y0_free=np.array([1.0, 0.0]),
|
|
54
|
+
t_end=1.0,
|
|
55
|
+
ns=(10, 20, 40, 80, 160),
|
|
56
|
+
order=2.0,
|
|
57
|
+
patch=(np.array([0.5, -0.3]), np.array([0.8, 1.1])),
|
|
58
|
+
integrate_trajectory=lambda s, y0, t, n: integrate(s, y0, t, n, trajectory=True),
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def main() -> int:
|
|
63
|
+
"""Exit 0 when the correct solver passes and the broken one fails, as they should."""
|
|
64
|
+
from .report import NOTE, run_checks, to_text
|
|
65
|
+
|
|
66
|
+
print("1. A correct midpoint solver for a damped oscillator:\n")
|
|
67
|
+
good = run_checks(checks())
|
|
68
|
+
print(to_text(good))
|
|
69
|
+
print("\n2. The same solver with the damping 10% too large (a typical slip):\n")
|
|
70
|
+
bad = run_checks(checks(damping_factor=1.1))
|
|
71
|
+
print(to_text(bad))
|
|
72
|
+
ok = not any(o.result.failed for o in good) and any(o.result.failed for o in bad)
|
|
73
|
+
print(f"\n{NOTE}")
|
|
74
|
+
print("\nTo check your own solver, start a spec with: physverify init verify_solver.py --solver mysolver --order 2")
|
|
75
|
+
return 0 if ok else 1
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
"""Checks that need no exact solution: round trips, derivatives, sibling agreement and identities.
|
|
2
|
+
|
|
3
|
+
These are the checks that found most of the bugs in the batch-8 corpus study (popular libraries):
|
|
4
|
+
two functions that should agree but do not (an air-absorption term with the opposite sign in one
|
|
5
|
+
of two reverberation formulas), an operation that should be undone by its inverse but is not
|
|
6
|
+
(a zero-time rotation that shifts an image by half a pixel), an analytic derivative compared with
|
|
7
|
+
finite differences, and textbook identities (Poisson's equation, unitarity, conservation).
|
|
8
|
+
|
|
9
|
+
Each function returns a `Result` (PASS or FAIL with the worst case in the message); wrap it in a
|
|
10
|
+
`Check("limit", label, lambda: ...)` to run it with the others. Values may be real or complex scalars
|
|
11
|
+
or arrays. The reference side must come from the physics, not from the code under test: a round trip
|
|
12
|
+
or a sibling comparison can pass when both sides share the same mistake.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
17
|
+
from typing import Callable, Iterable
|
|
18
|
+
|
|
19
|
+
import numpy as np
|
|
20
|
+
|
|
21
|
+
from .checks import FAIL, PASS, Result
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _worst(got, ref, rtol: float, atol: float):
|
|
25
|
+
"""Largest violation of |got - ref| <= atol + rtol |ref|, as (excess, abs error, where)."""
|
|
26
|
+
g = np.asarray(got)
|
|
27
|
+
r = np.asarray(ref)
|
|
28
|
+
if g.shape != r.shape:
|
|
29
|
+
try:
|
|
30
|
+
g, r = np.broadcast_arrays(g, r)
|
|
31
|
+
except ValueError:
|
|
32
|
+
raise ValueError(f"shapes differ: got {np.shape(got)}, reference {np.shape(ref)}") from None
|
|
33
|
+
err = np.abs(g - r)
|
|
34
|
+
tol = atol + rtol * np.abs(r)
|
|
35
|
+
excess = err - tol
|
|
36
|
+
if excess.size == 0:
|
|
37
|
+
return -np.inf, 0.0, ()
|
|
38
|
+
if not np.all(np.isfinite(err)):
|
|
39
|
+
i = np.unravel_index(int(np.argmax(~np.isfinite(err))), err.shape)
|
|
40
|
+
return np.inf, np.inf, i
|
|
41
|
+
i = np.unravel_index(int(np.argmax(excess)), excess.shape)
|
|
42
|
+
return float(excess[i]), float(err[i]), i
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def _verdict(what: str, cases, rtol: float, atol: float) -> Result:
|
|
46
|
+
"""cases: iterable of (label, got, ref). PASS when every case is within tolerance."""
|
|
47
|
+
worst = None
|
|
48
|
+
n = 0
|
|
49
|
+
for label, got, ref in cases:
|
|
50
|
+
n += 1
|
|
51
|
+
excess, err, where = _worst(got, ref, rtol, atol)
|
|
52
|
+
if worst is None or excess > worst[0]:
|
|
53
|
+
worst = (excess, err, label, where, got, ref)
|
|
54
|
+
if worst is None:
|
|
55
|
+
raise ValueError(f"{what}: no inputs given")
|
|
56
|
+
excess, err, label, where, got, ref = worst
|
|
57
|
+
at = f" at index {tuple(int(k) for k in where)}" if where else ""
|
|
58
|
+
detail = f"worst case {label}{at}: |difference| = {err:.3g} (rtol {rtol:g}, atol {atol:g})"
|
|
59
|
+
if excess <= 0:
|
|
60
|
+
return Result(PASS, f"{what}: {n} case{'s' if n != 1 else ''} within tolerance; {detail}")
|
|
61
|
+
g = np.asarray(got)[where] if where else got
|
|
62
|
+
r = np.asarray(ref)[where] if where else ref
|
|
63
|
+
return Result(FAIL, f"{what}: {detail}; got {g!r}, expected {r!r}")
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def check_roundtrip(forward: Callable, inverse: Callable, inputs: Iterable, *, rtol: float = 1e-9, atol: float = 0.0) -> Result:
|
|
67
|
+
"""inverse(forward(x)) == x for every x in `inputs`.
|
|
68
|
+
|
|
69
|
+
Catches conversions that disagree with their own inverse: a reference pixel off by half a pixel,
|
|
70
|
+
a dewpoint formula that does not invert the saturation formula, a unit conversion with the wrong
|
|
71
|
+
constant on one side. Both functions come from the code under test, so a PASS says only that
|
|
72
|
+
they are consistent, not that either is right.
|
|
73
|
+
"""
|
|
74
|
+
cases = []
|
|
75
|
+
for x in inputs:
|
|
76
|
+
cases.append((repr(x) if np.ndim(x) == 0 else f"input of shape {np.shape(x)}", inverse(forward(x)), x))
|
|
77
|
+
return _verdict("round trip inverse(forward(x)) = x", cases, rtol, atol)
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def check_agree(a: Callable, b: Callable, inputs: Iterable, *, rtol: float = 1e-9, atol: float = 0.0,
|
|
81
|
+
names: tuple[str, str] = ("a", "b")) -> Result:
|
|
82
|
+
"""Two implementations that must give the same answer do: a(*x) == b(*x) for each x in `inputs`.
|
|
83
|
+
|
|
84
|
+
Use it for sibling code paths (a 2D and a 3D version, a fast path and a slow path, the `min` and
|
|
85
|
+
`max` variants, two formulas that coincide in a limit). Each element of `inputs` is a tuple of
|
|
86
|
+
arguments, or a single argument.
|
|
87
|
+
"""
|
|
88
|
+
cases = []
|
|
89
|
+
for x in inputs:
|
|
90
|
+
args = x if isinstance(x, tuple) else (x,)
|
|
91
|
+
cases.append((repr(x), a(*args), b(*args)))
|
|
92
|
+
return _verdict(f"{names[0]} agrees with {names[1]}", cases, rtol, atol)
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def fd_derivative(f: Callable, x, *, h: float | None = None, order: int = 1):
|
|
96
|
+
"""Central finite-difference derivative of f at x (scalar x), with one Richardson step.
|
|
97
|
+
|
|
98
|
+
`order` is 1 or 2 (first or second derivative). The default step is cbrt(machine epsilon) times
|
|
99
|
+
max(1, |x|) for the first derivative and its square root for the second; the Richardson step
|
|
100
|
+
combines h and h/2 to cancel the leading error term.
|
|
101
|
+
"""
|
|
102
|
+
x = float(x)
|
|
103
|
+
if order == 1:
|
|
104
|
+
h = h or np.cbrt(np.finfo(float).eps) * max(1.0, abs(x))
|
|
105
|
+
d = lambda s: (np.asarray(f(x + s)) - np.asarray(f(x - s))) / (2 * s) # noqa: E731
|
|
106
|
+
elif order == 2:
|
|
107
|
+
h = h or np.finfo(float).eps ** 0.25 * max(1.0, abs(x))
|
|
108
|
+
d = lambda s: (np.asarray(f(x + s)) - 2 * np.asarray(f(x)) + np.asarray(f(x - s))) / (s * s) # noqa: E731
|
|
109
|
+
else:
|
|
110
|
+
raise ValueError("order must be 1 or 2")
|
|
111
|
+
return (4 * d(h / 2) - d(h)) / 3
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def check_derivative(f: Callable, df: Callable, points: Iterable, *, order: int = 1, h: float | None = None,
|
|
115
|
+
rtol: float = 1e-6, atol: float = 1e-10) -> Result:
|
|
116
|
+
"""An analytic derivative `df` agrees with a finite-difference derivative of `f` at each point.
|
|
117
|
+
|
|
118
|
+
This is how a thermodynamic-consistency claim is settled cheaply: a reader's derivation said an
|
|
119
|
+
activity-coefficient derivative dropped a term, and comparing it with finite differences of the
|
|
120
|
+
activity coefficients themselves showed agreement to 1e-10, so the claim was wrong. The default
|
|
121
|
+
tolerance allows for the finite-difference error; tighten it only with a well-scaled `h`.
|
|
122
|
+
"""
|
|
123
|
+
cases = [(f"x = {p!r}", df(p), fd_derivative(f, p, h=h, order=order)) for p in points]
|
|
124
|
+
return _verdict(f"{'first' if order == 1 else 'second'} derivative agrees with finite differences", cases, rtol, atol)
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def check_identity(cases: Iterable, *, rtol: float = 1e-9, atol: float = 0.0, what: str = "identity") -> Result:
|
|
128
|
+
"""Each (label, lhs, rhs) in `cases` satisfies lhs == rhs.
|
|
129
|
+
|
|
130
|
+
For identities written from the physics: Poisson's equation (Laplacian of the potential equals
|
|
131
|
+
4 pi G rho), unitarity of a lossless scattering matrix (S^H S = I), E^2 = p^2 + m^2, a limit that
|
|
132
|
+
must reproduce a simpler formula. Compute the right-hand side yourself, not with the code under test.
|
|
133
|
+
"""
|
|
134
|
+
return _verdict(what, list(cases), rtol, atol)
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def laplacian_fd(f: Callable, point, *, h: float = 1e-3):
|
|
138
|
+
"""Cartesian Laplacian of a scalar field f(x) (x a 1D array) by second-order central differences."""
|
|
139
|
+
x = np.asarray(point, dtype=float)
|
|
140
|
+
f0 = f(x)
|
|
141
|
+
total = 0.0
|
|
142
|
+
for k in range(x.size):
|
|
143
|
+
e = np.zeros_like(x)
|
|
144
|
+
e[k] = h
|
|
145
|
+
total = total + (f(x + e) - 2 * f0 + f(x - e)) / (h * h)
|
|
146
|
+
return total
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
def check_unitary(matrix, *, atol: float = 1e-10) -> Result:
|
|
150
|
+
"""A matrix (or a stack of matrices, last two axes) is unitary: M^H M = I.
|
|
151
|
+
|
|
152
|
+
A lossless network's scattering matrix, a time-evolution operator, a rotation.
|
|
153
|
+
"""
|
|
154
|
+
m = np.asarray(matrix)
|
|
155
|
+
eye = np.eye(m.shape[-1])
|
|
156
|
+
prod = np.einsum("...ji,...jk->...ik", m.conj(), m)
|
|
157
|
+
return _verdict("unitarity M^H M = I", [("matrix", prod, np.broadcast_to(eye, prod.shape))], 0.0, atol)
|