physverify 0.4.2__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.4.2/src/physverify.egg-info → physverify-0.5.0}/PKG-INFO +8 -5
- {physverify-0.4.2 → physverify-0.5.0}/README.md +7 -4
- {physverify-0.4.2 → physverify-0.5.0}/pyproject.toml +2 -2
- {physverify-0.4.2 → physverify-0.5.0}/src/physverify/__init__.py +1 -1
- {physverify-0.4.2 → physverify-0.5.0}/src/physverify/cli.py +39 -22
- {physverify-0.4.2 → physverify-0.5.0}/src/physverify/constants.py +17 -0
- physverify-0.5.0/src/physverify/identities.py +157 -0
- physverify-0.5.0/src/physverify/lint.py +428 -0
- {physverify-0.4.2 → physverify-0.5.0}/src/physverify/loaders.py +122 -0
- {physverify-0.4.2 → physverify-0.5.0}/src/physverify/order.py +17 -0
- physverify-0.5.0/src/physverify/scan.py +1037 -0
- {physverify-0.4.2 → physverify-0.5.0}/src/physverify/studies.py +71 -1
- {physverify-0.4.2 → physverify-0.5.0/src/physverify.egg-info}/PKG-INFO +8 -5
- {physverify-0.4.2 → physverify-0.5.0}/src/physverify.egg-info/SOURCES.txt +5 -17
- physverify-0.5.0/tests/test_batch8_lessons.py +263 -0
- {physverify-0.4.2 → physverify-0.5.0}/tests/test_ising.py +1 -1
- {physverify-0.4.2 → physverify-0.5.0}/tests/test_method_bugs.py +2 -2
- {physverify-0.4.2 → physverify-0.5.0}/tests/test_mms.py +1 -1
- {physverify-0.4.2 → physverify-0.5.0}/tests/test_new_problems.py +1 -1
- {physverify-0.4.2 → physverify-0.5.0}/tests/test_properties.py +2 -2
- {physverify-0.4.2 → physverify-0.5.0}/tests/test_real_projects.py +2 -2
- {physverify-0.4.2 → physverify-0.5.0}/tests/test_reference_problems.py +2 -2
- physverify-0.5.0/tests/test_scan.py +534 -0
- {physverify-0.4.2 → physverify-0.5.0}/tests/test_seeded_bugs.py +2 -2
- {physverify-0.4.2 → physverify-0.5.0}/tests/test_studies.py +1 -1
- physverify-0.4.2/src/physverify/adapters/__init__.py +0 -14
- physverify-0.4.2/src/physverify/adapters/double_pendulum.py +0 -55
- physverify-0.4.2/src/physverify/adapters/lorenz.py +0 -152
- physverify-0.4.2/src/physverify/adapters/projectile_v1.py +0 -92
- physverify-0.4.2/src/physverify/adapters/projectile_v2.py +0 -94
- physverify-0.4.2/src/physverify/adapters/three_body.py +0 -283
- physverify-0.4.2/src/physverify/compare.py +0 -104
- physverify-0.4.2/src/physverify/matrix.py +0 -276
- physverify-0.4.2/src/physverify/method_bugs.py +0 -266
- physverify-0.4.2/src/physverify/problems/__init__.py +0 -6
- physverify-0.4.2/src/physverify/problems/_common.py +0 -89
- physverify-0.4.2/src/physverify/problems/heat1d.py +0 -217
- physverify-0.4.2/src/physverify/problems/kepler.py +0 -176
- physverify-0.4.2/src/physverify/problems/pendulum.py +0 -201
- physverify-0.4.2/src/physverify/problems/projectile.py +0 -240
- physverify-0.4.2/src/physverify/problems/sod.py +0 -291
- physverify-0.4.2/src/physverify/stochastic/matrix.py +0 -151
- {physverify-0.4.2 → physverify-0.5.0}/LICENSE +0 -0
- {physverify-0.4.2 → physverify-0.5.0}/setup.cfg +0 -0
- {physverify-0.4.2 → physverify-0.5.0}/src/physverify/__main__.py +0 -0
- {physverify-0.4.2 → physverify-0.5.0}/src/physverify/agent.py +0 -0
- {physverify-0.4.2 → physverify-0.5.0}/src/physverify/butcher.py +0 -0
- {physverify-0.4.2 → physverify-0.5.0}/src/physverify/checks.py +0 -0
- {physverify-0.4.2 → physverify-0.5.0}/src/physverify/demo.py +0 -0
- {physverify-0.4.2 → physverify-0.5.0}/src/physverify/mms.py +0 -0
- {physverify-0.4.2 → physverify-0.5.0}/src/physverify/pytest_plugin.py +0 -0
- {physverify-0.4.2 → physverify-0.5.0}/src/physverify/references.py +0 -0
- {physverify-0.4.2 → physverify-0.5.0}/src/physverify/report.py +0 -0
- {physverify-0.4.2 → physverify-0.5.0}/src/physverify/scaffold.py +0 -0
- {physverify-0.4.2 → physverify-0.5.0}/src/physverify/smoke.py +0 -0
- {physverify-0.4.2 → physverify-0.5.0}/src/physverify/steppers.py +0 -0
- {physverify-0.4.2 → physverify-0.5.0}/src/physverify/stochastic/__init__.py +0 -0
- {physverify-0.4.2 → physverify-0.5.0}/src/physverify/stochastic/calibrate.py +0 -0
- {physverify-0.4.2 → physverify-0.5.0}/src/physverify/stochastic/checks.py +0 -0
- {physverify-0.4.2 → physverify-0.5.0}/src/physverify/stochastic/exact.py +0 -0
- {physverify-0.4.2 → physverify-0.5.0}/src/physverify/stochastic/ising.py +0 -0
- {physverify-0.4.2 → physverify-0.5.0}/src/physverify/stochastic/reference.py +0 -0
- {physverify-0.4.2 → physverify-0.5.0}/src/physverify/stochastic/sampler.py +0 -0
- {physverify-0.4.2 → physverify-0.5.0}/src/physverify/stochastic/stats.py +0 -0
- {physverify-0.4.2 → physverify-0.5.0}/src/physverify/symbolic.py +0 -0
- {physverify-0.4.2 → physverify-0.5.0}/src/physverify.egg-info/dependency_links.txt +0 -0
- {physverify-0.4.2 → physverify-0.5.0}/src/physverify.egg-info/entry_points.txt +0 -0
- {physverify-0.4.2 → physverify-0.5.0}/src/physverify.egg-info/requires.txt +0 -0
- {physverify-0.4.2 → physverify-0.5.0}/src/physverify.egg-info/top_level.txt +0 -0
- {physverify-0.4.2 → physverify-0.5.0}/tests/test_agent.py +0 -0
- {physverify-0.4.2 → physverify-0.5.0}/tests/test_awkward_input.py +0 -0
- {physverify-0.4.2 → physverify-0.5.0}/tests/test_batch7_lessons.py +0 -0
- {physverify-0.4.2 → physverify-0.5.0}/tests/test_butcher_properties.py +0 -0
- {physverify-0.4.2 → physverify-0.5.0}/tests/test_cli.py +0 -0
- {physverify-0.4.2 → physverify-0.5.0}/tests/test_corpus_lessons.py +0 -0
- {physverify-0.4.2 → physverify-0.5.0}/tests/test_docs_examples.py +0 -0
- {physverify-0.4.2 → physverify-0.5.0}/tests/test_figures.py +0 -0
- {physverify-0.4.2 → physverify-0.5.0}/tests/test_order.py +0 -0
- {physverify-0.4.2 → physverify-0.5.0}/tests/test_references.py +0 -0
- {physverify-0.4.2 → physverify-0.5.0}/tests/test_scaffold.py +0 -0
- {physverify-0.4.2 → physverify-0.5.0}/tests/test_v040_helpers.py +0 -0
- {physverify-0.4.2 → physverify-0.5.0}/tests/test_versions.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: physverify
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.5.0
|
|
4
4
|
Summary: Verification checks for physics simulation code: manufactured solutions, order-of-accuracy studies and a seeded-bug benchmark.
|
|
5
5
|
Author: Ram Chandra Giri
|
|
6
6
|
License-Expression: MIT
|
|
@@ -45,7 +45,7 @@ Dynamic: license-file
|
|
|
45
45
|
|
|
46
46
|
**Checks that a simulation solves the equations it claims, at the order of accuracy it claims, not just that it runs.**
|
|
47
47
|
|
|
48
|
-
physverify is actively evolving (v0.
|
|
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
49
|
|
|
50
50
|

|
|
51
51
|
|
|
@@ -58,8 +58,11 @@ The demo checks a correct solver (all checks pass), then the same solver with th
|
|
|
58
58
|
|
|
59
59
|

|
|
60
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
|
+
|
|
61
63
|
## Use it on your own code
|
|
62
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.
|
|
63
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`.
|
|
64
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.
|
|
65
68
|
- **Errors you already have:** `check_order(hs, errors, expected=2)` gives PASS, FAIL or INCONCLUSIVE with a reason.
|
|
@@ -69,7 +72,7 @@ The demo checks a correct solver (all checks pass), then the same solver with th
|
|
|
69
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):
|
|
70
73
|
|
|
71
74
|
```yaml
|
|
72
|
-
- uses: rcgiri-physics/physverify@v0.
|
|
75
|
+
- uses: rcgiri-physics/physverify@v0.5.0
|
|
73
76
|
with:
|
|
74
77
|
specs: tests/verify_*.py
|
|
75
78
|
```
|
|
@@ -78,7 +81,7 @@ A failed check fails the job, like a broken unit test ([CI guide](https://rcgiri
|
|
|
78
81
|
|
|
79
82
|
## Does it catch bugs?
|
|
80
83
|
|
|
81
|
-
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
|
|
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.
|
|
82
85
|
|
|
83
86
|
## Limits
|
|
84
87
|
|
|
@@ -86,4 +89,4 @@ Works best for ODEs; PDEs only in 1D so far. Your code needs a way to add a sour
|
|
|
86
89
|
|
|
87
90
|
## Contributing, citing, licence
|
|
88
91
|
|
|
89
|
-
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). 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.
|
|
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.
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
**Checks that a simulation solves the equations it claims, at the order of accuracy it claims, not just that it runs.**
|
|
6
6
|
|
|
7
|
-
physverify is actively evolving (v0.
|
|
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
8
|
|
|
9
9
|

|
|
10
10
|
|
|
@@ -17,8 +17,11 @@ The demo checks a correct solver (all checks pass), then the same solver with th
|
|
|
17
17
|
|
|
18
18
|

|
|
19
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
|
+
|
|
20
22
|
## Use it on your own code
|
|
21
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.
|
|
22
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`.
|
|
23
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.
|
|
24
27
|
- **Errors you already have:** `check_order(hs, errors, expected=2)` gives PASS, FAIL or INCONCLUSIVE with a reason.
|
|
@@ -28,7 +31,7 @@ The demo checks a correct solver (all checks pass), then the same solver with th
|
|
|
28
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):
|
|
29
32
|
|
|
30
33
|
```yaml
|
|
31
|
-
- uses: rcgiri-physics/physverify@v0.
|
|
34
|
+
- uses: rcgiri-physics/physverify@v0.5.0
|
|
32
35
|
with:
|
|
33
36
|
specs: tests/verify_*.py
|
|
34
37
|
```
|
|
@@ -37,7 +40,7 @@ A failed check fails the job, like a broken unit test ([CI guide](https://rcgiri
|
|
|
37
40
|
|
|
38
41
|
## Does it catch bugs?
|
|
39
42
|
|
|
40
|
-
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
|
|
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.
|
|
41
44
|
|
|
42
45
|
## Limits
|
|
43
46
|
|
|
@@ -45,4 +48,4 @@ Works best for ODEs; PDEs only in 1D so far. Your code needs a way to add a sour
|
|
|
45
48
|
|
|
46
49
|
## Contributing, citing, licence
|
|
47
50
|
|
|
48
|
-
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). 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.
|
|
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"
|
|
@@ -52,4 +52,4 @@ where = ["src"]
|
|
|
52
52
|
|
|
53
53
|
[tool.pytest.ini_options]
|
|
54
54
|
testpaths = ["tests"]
|
|
55
|
-
pythonpath = ["src"]
|
|
55
|
+
pythonpath = ["src", "benchmark"]
|
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
physverify run SPEC.py [SPEC.py ...] [--json FILE|-] [--html FILE] [--markdown FILE] [--quiet]
|
|
4
4
|
physverify demo (a correct and a broken solver, no files needed)
|
|
5
5
|
physverify init [SPEC.py] [--solver MODULE] [--function NAME] [--dim N] [--order P]
|
|
6
|
-
physverify
|
|
7
|
-
|
|
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)
|
|
8
8
|
physverify hook (Claude Code PostToolUse hook; see docs/agent.md)
|
|
9
9
|
physverify mcp (MCP server on stdio; see docs/agent.md)
|
|
10
10
|
|
|
@@ -98,18 +98,6 @@ def _cmd_demo(args) -> int:
|
|
|
98
98
|
return demo_main()
|
|
99
99
|
|
|
100
100
|
|
|
101
|
-
def _cmd_matrix(args) -> int:
|
|
102
|
-
from . import matrix
|
|
103
|
-
|
|
104
|
-
return matrix.main([args.out] if args.out else [])
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
def _cmd_compare(args) -> int:
|
|
108
|
-
from . import compare
|
|
109
|
-
|
|
110
|
-
return compare.main([args.out] if args.out else [])
|
|
111
|
-
|
|
112
|
-
|
|
113
101
|
def _cmd_hook(args) -> int:
|
|
114
102
|
from .agent import hook_main
|
|
115
103
|
|
|
@@ -134,6 +122,37 @@ def _cmd_smoke(args) -> int:
|
|
|
134
122
|
return 1 if bad else 0
|
|
135
123
|
|
|
136
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
|
+
|
|
137
156
|
def _cmd_references(args) -> int:
|
|
138
157
|
from . import references
|
|
139
158
|
|
|
@@ -173,14 +192,6 @@ def build_parser() -> argparse.ArgumentParser:
|
|
|
173
192
|
i.add_argument("--force", action="store_true", help="overwrite an existing file")
|
|
174
193
|
i.set_defaults(func=_cmd_init)
|
|
175
194
|
|
|
176
|
-
m = sub.add_parser("matrix", help="run the seeded-bug benchmark")
|
|
177
|
-
m.add_argument("out", nargs="?", help="also write the tables next to this Markdown file")
|
|
178
|
-
m.set_defaults(func=_cmd_matrix)
|
|
179
|
-
|
|
180
|
-
c = sub.add_parser("compare", help="compare the families with plain tests on the seeded bugs")
|
|
181
|
-
c.add_argument("out", nargs="?", help="also write the table to this Markdown file")
|
|
182
|
-
c.set_defaults(func=_cmd_compare)
|
|
183
|
-
|
|
184
195
|
h = sub.add_parser("hook", help="Claude Code PostToolUse hook: reads the event on stdin, exits 2 on a failed check")
|
|
185
196
|
h.set_defaults(func=_cmd_hook)
|
|
186
197
|
|
|
@@ -189,6 +200,12 @@ def build_parser() -> argparse.ArgumentParser:
|
|
|
189
200
|
sm.add_argument("--timeout", type=float, default=60.0, help="seconds per script (default 60)")
|
|
190
201
|
sm.set_defaults(func=_cmd_smoke)
|
|
191
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
|
+
|
|
192
209
|
r = sub.add_parser("references", help="list the exact reference solutions in physverify.references")
|
|
193
210
|
r.set_defaults(func=_cmd_references)
|
|
194
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,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)
|