virtual-casing-jax 0.0.2__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.
Files changed (45) hide show
  1. virtual_casing_jax-0.0.2/LICENSE +201 -0
  2. virtual_casing_jax-0.0.2/PKG-INFO +138 -0
  3. virtual_casing_jax-0.0.2/README.md +123 -0
  4. virtual_casing_jax-0.0.2/pyproject.toml +32 -0
  5. virtual_casing_jax-0.0.2/setup.cfg +4 -0
  6. virtual_casing_jax-0.0.2/tests/test_autodiff_gradb_parity.py +105 -0
  7. virtual_casing_jax-0.0.2/tests/test_chunk_blocking.py +36 -0
  8. virtual_casing_jax-0.0.2/tests/test_functional_api.py +387 -0
  9. virtual_casing_jax-0.0.2/tests/test_geom_assets.py +11 -0
  10. virtual_casing_jax-0.0.2/tests/test_integrals_parity.py +497 -0
  11. virtual_casing_jax-0.0.2/tests/test_kernels.py +135 -0
  12. virtual_casing_jax-0.0.2/tests/test_offsurface_adaptive_schedule.py +120 -0
  13. virtual_casing_jax-0.0.2/tests/test_simsopt_virtual_casing.py +235 -0
  14. virtual_casing_jax-0.0.2/tests/test_surface_geometry_parity.py +60 -0
  15. virtual_casing_jax-0.0.2/tests/test_surface_ops_basic.py +29 -0
  16. virtual_casing_jax-0.0.2/tests/test_surface_ops_parity.py +56 -0
  17. virtual_casing_jax-0.0.2/tests/test_testdata_parity.py +46 -0
  18. virtual_casing_jax-0.0.2/tests/test_utils.py +26 -0
  19. virtual_casing_jax-0.0.2/tests/test_virtual_casing_computeB_parity.py +147 -0
  20. virtual_casing_jax-0.0.2/tests/test_virtual_casing_computeBint_parity.py +94 -0
  21. virtual_casing_jax-0.0.2/tests/test_virtual_casing_gradb_offsurf_fd.py +112 -0
  22. virtual_casing_jax-0.0.2/tests/test_virtual_casing_gradb_parity.py +206 -0
  23. virtual_casing_jax-0.0.2/tests/test_virtual_casing_gradbint_parity.py +95 -0
  24. virtual_casing_jax-0.0.2/tests/test_virtual_casing_jit_batch.py +115 -0
  25. virtual_casing_jax-0.0.2/tests/test_virtual_casing_offsurf_parity.py +190 -0
  26. virtual_casing_jax-0.0.2/virtual_casing_jax/__init__.py +129 -0
  27. virtual_casing_jax-0.0.2/virtual_casing_jax/functional.py +1053 -0
  28. virtual_casing_jax-0.0.2/virtual_casing_jax/geom/LHD.npz +0 -0
  29. virtual_casing_jax-0.0.2/virtual_casing_jax/geom/Quas3.npz +0 -0
  30. virtual_casing_jax-0.0.2/virtual_casing_jax/geom/W7X.npz +0 -0
  31. virtual_casing_jax-0.0.2/virtual_casing_jax/geom/__init__.py +1 -0
  32. virtual_casing_jax-0.0.2/virtual_casing_jax/integrals.py +1618 -0
  33. virtual_casing_jax-0.0.2/virtual_casing_jax/kernels.py +160 -0
  34. virtual_casing_jax-0.0.2/virtual_casing_jax/simsopt_virtual_casing.py +298 -0
  35. virtual_casing_jax-0.0.2/virtual_casing_jax/singular_quadrature.py +237 -0
  36. virtual_casing_jax-0.0.2/virtual_casing_jax/surface_ops.py +274 -0
  37. virtual_casing_jax-0.0.2/virtual_casing_jax/testdata.py +463 -0
  38. virtual_casing_jax-0.0.2/virtual_casing_jax/utils.py +100 -0
  39. virtual_casing_jax-0.0.2/virtual_casing_jax/virtual_casing.py +1714 -0
  40. virtual_casing_jax-0.0.2/virtual_casing_jax/w7x_coeffs.py +178 -0
  41. virtual_casing_jax-0.0.2/virtual_casing_jax.egg-info/PKG-INFO +138 -0
  42. virtual_casing_jax-0.0.2/virtual_casing_jax.egg-info/SOURCES.txt +43 -0
  43. virtual_casing_jax-0.0.2/virtual_casing_jax.egg-info/dependency_links.txt +1 -0
  44. virtual_casing_jax-0.0.2/virtual_casing_jax.egg-info/requires.txt +4 -0
  45. virtual_casing_jax-0.0.2/virtual_casing_jax.egg-info/top_level.txt +1 -0
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,138 @@
1
+ Metadata-Version: 2.4
2
+ Name: virtual-casing-jax
3
+ Version: 0.0.2
4
+ Summary: JAX implementation of the virtual casing principle with high-order quadrature
5
+ Author: UW Plasma
6
+ License-Expression: Apache-2.0
7
+ Requires-Python: >=3.9
8
+ Description-Content-Type: text/markdown
9
+ License-File: LICENSE
10
+ Requires-Dist: jax
11
+ Requires-Dist: jaxlib
12
+ Requires-Dist: numpy
13
+ Requires-Dist: scipy
14
+ Dynamic: license-file
15
+
16
+ [![CI](https://github.com/uwplasma/virtual_casing_jax/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/uwplasma/virtual_casing_jax/actions/workflows/ci.yml)
17
+ [![CI-Large](https://github.com/uwplasma/virtual_casing_jax/actions/workflows/ci-large.yml/badge.svg)](https://github.com/uwplasma/virtual_casing_jax/actions/workflows/ci-large.yml)
18
+ [![Coverage](https://codecov.io/gh/uwplasma/virtual_casing_jax/branch/main/graph/badge.svg)](https://codecov.io/gh/uwplasma/virtual_casing_jax)
19
+ [![PyPI](https://img.shields.io/pypi/v/virtual-casing-jax.svg)](https://pypi.org/project/virtual-casing-jax/)
20
+ [![Python](https://img.shields.io/pypi/pyversions/virtual-casing-jax.svg)](https://pypi.org/project/virtual-casing-jax/)
21
+ [![License](https://img.shields.io/github/license/uwplasma/virtual_casing_jax.svg)](LICENSE)
22
+
23
+ # virtual_casing_jax
24
+
25
+ `virtual_casing_jax` is a JAX implementation of the virtual casing
26
+ principle for computing magnetic-field contributions from plasma currents
27
+ using high-order singular quadrature. It is based on the C++ reference
28
+ implementation in [`hiddenSymmetries/virtual-casing`](https://github.com/hiddenSymmetries/virtual-casing)
29
+ and on the SIMSOPT virtual-casing interface in
30
+ [`hiddenSymmetries/simsopt`](https://github.com/hiddenSymmetries/simsopt).
31
+
32
+ Documentation is available at
33
+ [`virtual-casing-jax.readthedocs.io`](https://virtual-casing-jax.readthedocs.io/).
34
+
35
+ ## Installation
36
+
37
+ Install the latest release from PyPI:
38
+
39
+ ```bash
40
+ pip install virtual-casing-jax
41
+ ```
42
+
43
+ Or install from a local source checkout:
44
+
45
+ ```bash
46
+ git clone https://github.com/uwplasma/virtual_casing_jax.git
47
+ cd virtual_casing_jax
48
+ pip install -e .
49
+ ```
50
+
51
+ ## Basic Usage
52
+
53
+ The SIMSOPT-compatible wrapper can be used as a drop-in virtual-casing
54
+ calculation when SIMSOPT is installed:
55
+
56
+ ```python
57
+ from virtual_casing_jax import VirtualCasing
58
+
59
+ vc = VirtualCasing.from_vmec(
60
+ "wout_example.nc",
61
+ src_nphi=32,
62
+ trgt_nphi=32,
63
+ trgt_ntheta=32,
64
+ filename="auto",
65
+ )
66
+
67
+ B_external_normal = vc.B_external_normal
68
+ ```
69
+
70
+ For lower-level JAX workflows, use `VirtualCasingJAX` directly after
71
+ preparing surface coordinates and magnetic-field arrays:
72
+
73
+ ```python
74
+ from virtual_casing_jax import VirtualCasingJAX
75
+
76
+ vc_jax = VirtualCasingJAX()
77
+ vc_jax.setup(digits, nfp, stellsym, Nt, Np, gamma, Nt, Np, Nt, Np)
78
+ B_external = vc_jax.compute_external_B(B_total)
79
+ ```
80
+
81
+ Performance features:
82
+ - Source/target tiling with auto-tuned chunk sizes.
83
+ - Rematerialization hooks for GradB singular correction.
84
+ - Optional target-scan mode to reduce GradB peak memory (`scan_targets`).
85
+ - Mixed-precision POU/patch tables with float64 outputs.
86
+ - Bundled Quas3/LHD/W7X geometry assets (converted from SCTL .mat).
87
+
88
+ SIMSOPT compatibility:
89
+ The package ships a SIMSOPT-compatible ``VirtualCasing`` class that
90
+ mirrors ``simsopt.mhd.virtual_casing.VirtualCasing`` while using the
91
+ JAX backend. Import it as ``from virtual_casing_jax import VirtualCasing``.
92
+ See `docs/using_simsopt.rst` and the examples in `examples/` for full scripts.
93
+
94
+ Bundled test data:
95
+ To make the SIMSOPT-style examples and tests self-contained, the repo
96
+ includes a small subset of SIMSOPT test assets under `tests/test_files/`
97
+ and the VMEC input `examples/inputs/input.QH_finitebeta`. These files
98
+ originated from the SIMSOPT repository ([SIMSOPT](https://github.com/hiddenSymmetries/simsopt))
99
+ and are used only for validation and example runs.
100
+
101
+ Docs
102
+ ----
103
+
104
+ Sphinx documentation lives in `docs/` and is configured for ReadTheDocs.
105
+ It includes the equations, numerics, implementation details, and validation
106
+ strategy. Run locally:
107
+
108
+ ```bash
109
+ pip install -r docs/requirements.txt
110
+ sphinx-build -b html docs docs/_build/html
111
+ ```
112
+
113
+ Profiling
114
+ ---------
115
+
116
+ Use the profiling harness to capture JAX traces and inspect performance:
117
+
118
+ ```bash
119
+ JAX_ENABLE_X64=1 python tools/profile_vc.py --case case_vc --op B --jit \
120
+ --repeat 5 --trace-dir /tmp/vc_trace
121
+
122
+ tensorboard --logdir /tmp/vc_trace
123
+ ```
124
+
125
+ For the new tuning knobs:
126
+
127
+ ```bash
128
+ JAX_ENABLE_X64=1 XLA_FLAGS="--xla_dump_to=/tmp/vc_xla --xla_dump_hlo_as_text" \
129
+ python tools/profile_vc.py --case case_vc_large --op GradB --jit \
130
+ --chunk-size auto --target-chunk-size auto --pou-dtype float32 --patch-dtype float32 \
131
+ --interp-block-size auto --remat --donate \
132
+ --repeat 2 --trace-dir /tmp/vc_trace_case_vc_large_GradB
133
+
134
+ tensorboard --logdir /tmp/vc_trace_case_vc_large_GradB
135
+ ```
136
+
137
+ This writes JAX traces under `/tmp/vc_trace_*` and HLO dumps under
138
+ `/tmp/vc_xla_*`. See `docs/performance.rst` for detailed interpretation.
@@ -0,0 +1,123 @@
1
+ [![CI](https://github.com/uwplasma/virtual_casing_jax/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/uwplasma/virtual_casing_jax/actions/workflows/ci.yml)
2
+ [![CI-Large](https://github.com/uwplasma/virtual_casing_jax/actions/workflows/ci-large.yml/badge.svg)](https://github.com/uwplasma/virtual_casing_jax/actions/workflows/ci-large.yml)
3
+ [![Coverage](https://codecov.io/gh/uwplasma/virtual_casing_jax/branch/main/graph/badge.svg)](https://codecov.io/gh/uwplasma/virtual_casing_jax)
4
+ [![PyPI](https://img.shields.io/pypi/v/virtual-casing-jax.svg)](https://pypi.org/project/virtual-casing-jax/)
5
+ [![Python](https://img.shields.io/pypi/pyversions/virtual-casing-jax.svg)](https://pypi.org/project/virtual-casing-jax/)
6
+ [![License](https://img.shields.io/github/license/uwplasma/virtual_casing_jax.svg)](LICENSE)
7
+
8
+ # virtual_casing_jax
9
+
10
+ `virtual_casing_jax` is a JAX implementation of the virtual casing
11
+ principle for computing magnetic-field contributions from plasma currents
12
+ using high-order singular quadrature. It is based on the C++ reference
13
+ implementation in [`hiddenSymmetries/virtual-casing`](https://github.com/hiddenSymmetries/virtual-casing)
14
+ and on the SIMSOPT virtual-casing interface in
15
+ [`hiddenSymmetries/simsopt`](https://github.com/hiddenSymmetries/simsopt).
16
+
17
+ Documentation is available at
18
+ [`virtual-casing-jax.readthedocs.io`](https://virtual-casing-jax.readthedocs.io/).
19
+
20
+ ## Installation
21
+
22
+ Install the latest release from PyPI:
23
+
24
+ ```bash
25
+ pip install virtual-casing-jax
26
+ ```
27
+
28
+ Or install from a local source checkout:
29
+
30
+ ```bash
31
+ git clone https://github.com/uwplasma/virtual_casing_jax.git
32
+ cd virtual_casing_jax
33
+ pip install -e .
34
+ ```
35
+
36
+ ## Basic Usage
37
+
38
+ The SIMSOPT-compatible wrapper can be used as a drop-in virtual-casing
39
+ calculation when SIMSOPT is installed:
40
+
41
+ ```python
42
+ from virtual_casing_jax import VirtualCasing
43
+
44
+ vc = VirtualCasing.from_vmec(
45
+ "wout_example.nc",
46
+ src_nphi=32,
47
+ trgt_nphi=32,
48
+ trgt_ntheta=32,
49
+ filename="auto",
50
+ )
51
+
52
+ B_external_normal = vc.B_external_normal
53
+ ```
54
+
55
+ For lower-level JAX workflows, use `VirtualCasingJAX` directly after
56
+ preparing surface coordinates and magnetic-field arrays:
57
+
58
+ ```python
59
+ from virtual_casing_jax import VirtualCasingJAX
60
+
61
+ vc_jax = VirtualCasingJAX()
62
+ vc_jax.setup(digits, nfp, stellsym, Nt, Np, gamma, Nt, Np, Nt, Np)
63
+ B_external = vc_jax.compute_external_B(B_total)
64
+ ```
65
+
66
+ Performance features:
67
+ - Source/target tiling with auto-tuned chunk sizes.
68
+ - Rematerialization hooks for GradB singular correction.
69
+ - Optional target-scan mode to reduce GradB peak memory (`scan_targets`).
70
+ - Mixed-precision POU/patch tables with float64 outputs.
71
+ - Bundled Quas3/LHD/W7X geometry assets (converted from SCTL .mat).
72
+
73
+ SIMSOPT compatibility:
74
+ The package ships a SIMSOPT-compatible ``VirtualCasing`` class that
75
+ mirrors ``simsopt.mhd.virtual_casing.VirtualCasing`` while using the
76
+ JAX backend. Import it as ``from virtual_casing_jax import VirtualCasing``.
77
+ See `docs/using_simsopt.rst` and the examples in `examples/` for full scripts.
78
+
79
+ Bundled test data:
80
+ To make the SIMSOPT-style examples and tests self-contained, the repo
81
+ includes a small subset of SIMSOPT test assets under `tests/test_files/`
82
+ and the VMEC input `examples/inputs/input.QH_finitebeta`. These files
83
+ originated from the SIMSOPT repository ([SIMSOPT](https://github.com/hiddenSymmetries/simsopt))
84
+ and are used only for validation and example runs.
85
+
86
+ Docs
87
+ ----
88
+
89
+ Sphinx documentation lives in `docs/` and is configured for ReadTheDocs.
90
+ It includes the equations, numerics, implementation details, and validation
91
+ strategy. Run locally:
92
+
93
+ ```bash
94
+ pip install -r docs/requirements.txt
95
+ sphinx-build -b html docs docs/_build/html
96
+ ```
97
+
98
+ Profiling
99
+ ---------
100
+
101
+ Use the profiling harness to capture JAX traces and inspect performance:
102
+
103
+ ```bash
104
+ JAX_ENABLE_X64=1 python tools/profile_vc.py --case case_vc --op B --jit \
105
+ --repeat 5 --trace-dir /tmp/vc_trace
106
+
107
+ tensorboard --logdir /tmp/vc_trace
108
+ ```
109
+
110
+ For the new tuning knobs:
111
+
112
+ ```bash
113
+ JAX_ENABLE_X64=1 XLA_FLAGS="--xla_dump_to=/tmp/vc_xla --xla_dump_hlo_as_text" \
114
+ python tools/profile_vc.py --case case_vc_large --op GradB --jit \
115
+ --chunk-size auto --target-chunk-size auto --pou-dtype float32 --patch-dtype float32 \
116
+ --interp-block-size auto --remat --donate \
117
+ --repeat 2 --trace-dir /tmp/vc_trace_case_vc_large_GradB
118
+
119
+ tensorboard --logdir /tmp/vc_trace_case_vc_large_GradB
120
+ ```
121
+
122
+ This writes JAX traces under `/tmp/vc_trace_*` and HLO dumps under
123
+ `/tmp/vc_xla_*`. See `docs/performance.rst` for detailed interpretation.
@@ -0,0 +1,32 @@
1
+ [build-system]
2
+ requires = ["setuptools>=68", "wheel"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "virtual-casing-jax"
7
+ version = "0.0.2"
8
+ description = "JAX implementation of the virtual casing principle with high-order quadrature"
9
+ readme = "README.md"
10
+ requires-python = ">=3.9"
11
+ license = "Apache-2.0"
12
+ license-files = ["LICENSE"]
13
+ authors = [
14
+ {name = "UW Plasma"},
15
+ ]
16
+ dependencies = [
17
+ "jax",
18
+ "jaxlib",
19
+ "numpy",
20
+ "scipy",
21
+ ]
22
+
23
+ [tool.setuptools.packages.find]
24
+ include = ["virtual_casing_jax*"]
25
+
26
+ [tool.setuptools.package-data]
27
+ virtual_casing_jax = ["geom/*.npz"]
28
+
29
+ [tool.pytest.ini_options]
30
+ markers = [
31
+ "large: large parity datasets (longer runtime)",
32
+ ]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,105 @@
1
+ from pathlib import Path
2
+
3
+ import numpy as np
4
+ import jax
5
+ import jax.numpy as jnp
6
+ import pytest
7
+
8
+ from virtual_casing_jax.integrals import field_period_target_coords
9
+ from virtual_casing_jax.surface_ops import rotate_toroidal, complete_vec_field
10
+ from virtual_casing_jax.virtual_casing import VirtualCasingJAX
11
+
12
+ # Allow direct import of dump_io when tests are not a package.
13
+ import sys
14
+ from pathlib import Path as _Path
15
+ sys.path.append(str(_Path(__file__).parent))
16
+ from dump_io import load_dump # noqa: E402
17
+
18
+ DATA_DIR = Path(__file__).parent / "data"
19
+
20
+
21
+ def _infer_setup(prefix: str):
22
+ X = load_dump(DATA_DIR / f"{prefix}_setup_X")
23
+ surf = load_dump(DATA_DIR / f"{prefix}_setup_surface_coord")
24
+ B0_complete = load_dump(DATA_DIR / f"{prefix}_computeB_B0_complete")
25
+
26
+ src_nt = X.shape[1]
27
+ src_np = X.shape[2]
28
+ nfp_eff = B0_complete.shape[1] // src_nt
29
+ half_period = surf.shape[1] == nfp_eff * (src_nt + 1)
30
+ nfp = nfp_eff // 2 if half_period else nfp_eff
31
+ return X, src_nt, src_np, nfp, nfp_eff, half_period
32
+
33
+
34
+ def _reconstruct_B0(prefix: str, src_nt: int, src_np: int, nfp: int, nfp_eff: int, half_period: bool, trg_nt: int):
35
+ B0_complete_ref = load_dump(DATA_DIR / f"{prefix}_computeB_B0_complete")
36
+ B0_complete_ref = jnp.asarray(B0_complete_ref)
37
+ B0_complete = B0_complete_ref
38
+
39
+ dtheta = 0.0
40
+ if half_period:
41
+ dtheta = np.pi * (1.0 / (nfp * trg_nt * 2) - 1.0 / (nfp * src_nt * 2))
42
+ B0_complete = rotate_toroidal(B0_complete, nfp_eff * src_nt, src_np, -dtheta)
43
+
44
+ B0 = B0_complete[:, :src_nt, :]
45
+
46
+ B0_re = complete_vec_field(B0, False, half_period, nfp, src_nt, src_np, dtheta)
47
+ num = np.linalg.norm(np.asarray(B0_re) - np.asarray(B0_complete_ref))
48
+ den = np.linalg.norm(np.asarray(B0_complete_ref)) + 1e-14
49
+ assert num / den < 1e-4
50
+
51
+ return np.asarray(B0)
52
+
53
+
54
+ @pytest.mark.parametrize("prefix", ["case_vc", "case_simsopt"])
55
+ def test_autodiff_gradb_matches_reference(prefix):
56
+ if not (DATA_DIR / f"{prefix}_computeGradB_gradBvc.bin").exists():
57
+ pytest.skip("parity dump not available")
58
+
59
+ X, src_nt, src_np, nfp, nfp_eff, half_period = _infer_setup(prefix)
60
+ gradBvc_ref = load_dump(DATA_DIR / f"{prefix}_computeGradB_gradBvc")
61
+ quad_coord = load_dump(DATA_DIR / f"{prefix}_computeB_quad_coord")
62
+
63
+ trg_nt = gradBvc_ref.shape[2]
64
+ trg_np = gradBvc_ref.shape[3]
65
+ quad_nt = quad_coord.shape[1]
66
+ quad_np = quad_coord.shape[2]
67
+
68
+ B0 = _reconstruct_B0(prefix, src_nt, src_np, nfp, nfp_eff, half_period, trg_nt)
69
+ X_trg = field_period_target_coords(jnp.asarray(quad_coord), trg_nt, trg_np, nfp_eff)
70
+
71
+ vc = VirtualCasingJAX()
72
+ vc.setup(
73
+ 5,
74
+ nfp,
75
+ half_period,
76
+ src_nt,
77
+ src_np,
78
+ X,
79
+ src_nt,
80
+ src_np,
81
+ trg_nt,
82
+ trg_np,
83
+ )
84
+
85
+ def f(xtrg):
86
+ return vc.compute_external_B_autodiff(
87
+ B0,
88
+ X_trg=xtrg,
89
+ quad_nt=quad_nt,
90
+ quad_np=quad_np,
91
+ digits=5,
92
+ chunk_size=1024,
93
+ hedgehog_order=8,
94
+ )
95
+
96
+ jac = jax.jacobian(f)(X_trg)
97
+ jac = np.asarray(jac)
98
+ diag = np.zeros_like(gradBvc_ref)
99
+ for i in range(trg_nt):
100
+ for j in range(trg_np):
101
+ diag[:, :, i, j] = jac[:, i, j, :, i, j]
102
+
103
+ rel = np.linalg.norm(diag - gradBvc_ref) / (np.linalg.norm(gradBvc_ref) + 1e-14)
104
+ tol = 5e-3 if prefix == "case_vc" else 7e-3
105
+ assert rel < tol
@@ -0,0 +1,36 @@
1
+ import numpy as np
2
+ import jax.numpy as jnp
3
+
4
+ from virtual_casing_jax.integrals import laplace_fxd_u_eval
5
+
6
+
7
+ def test_target_chunk_blocking_matches():
8
+ rng = np.random.default_rng(0)
9
+ X_src = jnp.asarray(rng.normal(size=(3, 6, 5)))
10
+ X_trg = jnp.asarray(rng.normal(size=(3, 4, 3)))
11
+ density = jnp.asarray(rng.normal(size=(6, 5)))
12
+ area_elem = jnp.asarray(np.abs(rng.normal(size=(6, 5))))
13
+
14
+ out_full = laplace_fxd_u_eval(
15
+ X_src,
16
+ X_trg,
17
+ density,
18
+ area_elem,
19
+ chunk_size=4,
20
+ target_chunk_size=None,
21
+ )
22
+ out_blocked = laplace_fxd_u_eval(
23
+ X_src,
24
+ X_trg,
25
+ density,
26
+ area_elem,
27
+ chunk_size=4,
28
+ target_chunk_size=2,
29
+ )
30
+
31
+ np.testing.assert_allclose(
32
+ np.asarray(out_full),
33
+ np.asarray(out_blocked),
34
+ rtol=1e-10,
35
+ atol=1e-10,
36
+ )