pyelastica-jax 0.0.1__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.
- pyelastica_jax-0.0.1/.gitignore +249 -0
- pyelastica_jax-0.0.1/LICENSE +21 -0
- pyelastica_jax-0.0.1/PKG-INFO +428 -0
- pyelastica_jax-0.0.1/README.md +379 -0
- pyelastica_jax-0.0.1/elastica_jax/__init__.py +46 -0
- pyelastica_jax-0.0.1/elastica_jax/_calculus.py +65 -0
- pyelastica_jax-0.0.1/elastica_jax/_linalg.py +96 -0
- pyelastica_jax-0.0.1/elastica_jax/_rotations.py +90 -0
- pyelastica_jax-0.0.1/elastica_jax/_synchronize_periodic_boundary.py +13 -0
- pyelastica_jax-0.0.1/elastica_jax/block_operation.py +108 -0
- pyelastica_jax-0.0.1/elastica_jax/checkpoint/__init__.py +27 -0
- pyelastica_jax-0.0.1/elastica_jax/checkpoint/block_checkpoint.py +317 -0
- pyelastica_jax-0.0.1/elastica_jax/contact/__init__.py +33 -0
- pyelastica_jax-0.0.1/elastica_jax/contact/capsule_contact.py +439 -0
- pyelastica_jax-0.0.1/elastica_jax/contact/kernels.py +215 -0
- pyelastica_jax-0.0.1/elastica_jax/contact/spatial_hash.py +158 -0
- pyelastica_jax-0.0.1/elastica_jax/execution_mesh.py +27 -0
- pyelastica_jax-0.0.1/elastica_jax/memory_block/__init__.py +7 -0
- pyelastica_jax-0.0.1/elastica_jax/memory_block/block_factory.py +87 -0
- pyelastica_jax-0.0.1/elastica_jax/memory_block/memory_block_rigid_body_jax.py +464 -0
- pyelastica_jax-0.0.1/elastica_jax/memory_block/memory_block_rod_jax.py +1204 -0
- pyelastica_jax-0.0.1/elastica_jax/memory_block/sharded_cosserat_rod_jax.py +493 -0
- pyelastica_jax-0.0.1/elastica_jax/modules/__init__.py +11 -0
- pyelastica_jax-0.0.1/elastica_jax/modules/block_connections.py +392 -0
- pyelastica_jax-0.0.1/elastica_jax/modules/jax_ops.py +198 -0
- pyelastica_jax-0.0.1/elastica_jax/modules/jax_ops_block.py +702 -0
- pyelastica_jax-0.0.1/elastica_jax/modules/jax_ops_rod_rigid_body.py +257 -0
- pyelastica_jax-0.0.1/elastica_jax/modules/jax_ops_rod_rod_block.py +211 -0
- pyelastica_jax-0.0.1/elastica_jax/operations.py +304 -0
- pyelastica_jax-0.0.1/elastica_jax/protocol.py +99 -0
- pyelastica_jax-0.0.1/elastica_jax/py.typed +0 -0
- pyelastica_jax-0.0.1/elastica_jax/rod_rigid_body_operation.py +52 -0
- pyelastica_jax-0.0.1/elastica_jax/rod_rod_operation.py +58 -0
- pyelastica_jax-0.0.1/elastica_jax/timestepper/__init__.py +0 -0
- pyelastica_jax-0.0.1/elastica_jax/timestepper/jax_steppers.py +443 -0
- pyelastica_jax-0.0.1/elastica_jax/timestepper/protocol.py +23 -0
- pyelastica_jax-0.0.1/elastica_jax/typing.py +1 -0
- pyelastica_jax-0.0.1/elastica_jax/utils.py +213 -0
- pyelastica_jax-0.0.1/elastica_jax/version.py +6 -0
- pyelastica_jax-0.0.1/pyproject.toml +175 -0
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
*.swp
|
|
10
|
+
*.h5
|
|
11
|
+
*.hdf5
|
|
12
|
+
.vscode
|
|
13
|
+
docs/_gallery
|
|
14
|
+
docs/gen_modules
|
|
15
|
+
docs/sg_execution_times.rst
|
|
16
|
+
|
|
17
|
+
# Distribution / packaging
|
|
18
|
+
.Python
|
|
19
|
+
build/
|
|
20
|
+
develop-eggs/
|
|
21
|
+
dist/
|
|
22
|
+
downloads/
|
|
23
|
+
eggs/
|
|
24
|
+
.eggs/
|
|
25
|
+
lib/
|
|
26
|
+
lib64/
|
|
27
|
+
parts/
|
|
28
|
+
sdist/
|
|
29
|
+
var/
|
|
30
|
+
wheels/
|
|
31
|
+
pip-wheel-metadata/
|
|
32
|
+
share/python-wheels/
|
|
33
|
+
*.egg-info/
|
|
34
|
+
.installed.cfg
|
|
35
|
+
*.egg
|
|
36
|
+
MANIFEST
|
|
37
|
+
|
|
38
|
+
# PyInstaller
|
|
39
|
+
# Usually these files are written by a python script from a template
|
|
40
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
41
|
+
*.manifest
|
|
42
|
+
*.spec
|
|
43
|
+
|
|
44
|
+
# Installer logs
|
|
45
|
+
pip-log.txt
|
|
46
|
+
pip-delete-this-directory.txt
|
|
47
|
+
|
|
48
|
+
# Unit test / coverage reports
|
|
49
|
+
htmlcov/
|
|
50
|
+
.tox/
|
|
51
|
+
.nox/
|
|
52
|
+
.coverage
|
|
53
|
+
.coverage.*
|
|
54
|
+
.cache
|
|
55
|
+
nosetests.xml
|
|
56
|
+
coverage.xml
|
|
57
|
+
*.cover
|
|
58
|
+
*.py,cover
|
|
59
|
+
.hypothesis/
|
|
60
|
+
.pytest_cache/
|
|
61
|
+
|
|
62
|
+
# Translations
|
|
63
|
+
*.mo
|
|
64
|
+
*.pot
|
|
65
|
+
|
|
66
|
+
# Django stuff:
|
|
67
|
+
*.log
|
|
68
|
+
local_settings.py
|
|
69
|
+
db.sqlite3
|
|
70
|
+
db.sqlite3-journal
|
|
71
|
+
|
|
72
|
+
# Flask stuff:
|
|
73
|
+
instance/
|
|
74
|
+
.webassets-cache
|
|
75
|
+
|
|
76
|
+
# Scrapy stuff:
|
|
77
|
+
.scrapy
|
|
78
|
+
|
|
79
|
+
# Sphinx documentation
|
|
80
|
+
docs/_build/
|
|
81
|
+
|
|
82
|
+
# PyBuilder
|
|
83
|
+
target/
|
|
84
|
+
|
|
85
|
+
# Jupyter Notebook
|
|
86
|
+
.ipynb_checkpoints
|
|
87
|
+
|
|
88
|
+
# IPython
|
|
89
|
+
profile_default/
|
|
90
|
+
ipython_config.py
|
|
91
|
+
|
|
92
|
+
# pyenv
|
|
93
|
+
.python-version
|
|
94
|
+
|
|
95
|
+
# pipenv
|
|
96
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
97
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
98
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
99
|
+
# install all needed dependencies.
|
|
100
|
+
#Pipfile.lock
|
|
101
|
+
|
|
102
|
+
# celery beat schedule file
|
|
103
|
+
celerybeat-schedule
|
|
104
|
+
|
|
105
|
+
# SageMath parsed files
|
|
106
|
+
*.sage.py
|
|
107
|
+
|
|
108
|
+
# Environments
|
|
109
|
+
.env
|
|
110
|
+
.venv
|
|
111
|
+
env/
|
|
112
|
+
venv/
|
|
113
|
+
ENV/
|
|
114
|
+
env.bak/
|
|
115
|
+
venv.bak/
|
|
116
|
+
|
|
117
|
+
# Spyder project settings
|
|
118
|
+
.spyderproject
|
|
119
|
+
.spyproject
|
|
120
|
+
|
|
121
|
+
# Rope project settings
|
|
122
|
+
.ropeproject
|
|
123
|
+
|
|
124
|
+
# mkdocs documentation
|
|
125
|
+
/site
|
|
126
|
+
|
|
127
|
+
# mypy
|
|
128
|
+
.mypy_cache/
|
|
129
|
+
.dmypy.json
|
|
130
|
+
dmypy.json
|
|
131
|
+
|
|
132
|
+
# Pyre type checker
|
|
133
|
+
.pyre/
|
|
134
|
+
|
|
135
|
+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
|
|
136
|
+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
|
|
137
|
+
|
|
138
|
+
# User-specific stuff
|
|
139
|
+
.idea
|
|
140
|
+
.idea/**/workspace.xml
|
|
141
|
+
.idea/**/tasks.xml
|
|
142
|
+
.idea/**/usage.statistics.xml
|
|
143
|
+
.idea/**/dictionaries
|
|
144
|
+
.idea/**/shelf
|
|
145
|
+
|
|
146
|
+
# Generated files
|
|
147
|
+
.idea/**/contentModel.xml
|
|
148
|
+
|
|
149
|
+
# Sensitive or high-churn files
|
|
150
|
+
.idea/**/dataSources/
|
|
151
|
+
.idea/**/dataSources.ids
|
|
152
|
+
.idea/**/dataSources.local.xml
|
|
153
|
+
.idea/**/sqlDataSources.xml
|
|
154
|
+
.idea/**/dynamic.xml
|
|
155
|
+
.idea/**/uiDesigner.xml
|
|
156
|
+
.idea/**/dbnavigator.xml
|
|
157
|
+
|
|
158
|
+
# Gradle
|
|
159
|
+
.idea/**/gradle.xml
|
|
160
|
+
.idea/**/libraries
|
|
161
|
+
|
|
162
|
+
# Gradle and Maven with auto-import
|
|
163
|
+
# When using Gradle or Maven with auto-import, you should exclude module files,
|
|
164
|
+
# since they will be recreated, and may cause churn. Uncomment if using
|
|
165
|
+
# auto-import.
|
|
166
|
+
# .idea/modules.xml
|
|
167
|
+
# .idea/*.iml
|
|
168
|
+
# .idea/modules
|
|
169
|
+
# *.iml
|
|
170
|
+
# *.ipr
|
|
171
|
+
|
|
172
|
+
# CMake
|
|
173
|
+
cmake-build-*/
|
|
174
|
+
|
|
175
|
+
# Mongo Explorer plugin
|
|
176
|
+
.idea/**/mongoSettings.xml
|
|
177
|
+
|
|
178
|
+
# File-based project format
|
|
179
|
+
*.iws
|
|
180
|
+
|
|
181
|
+
# IntelliJ
|
|
182
|
+
out/
|
|
183
|
+
|
|
184
|
+
# mpeltonen/sbt-idea plugin
|
|
185
|
+
.idea_modules/
|
|
186
|
+
|
|
187
|
+
# JIRA plugin
|
|
188
|
+
atlassian-ide-plugin.xml
|
|
189
|
+
|
|
190
|
+
# Cursive Clojure plugin
|
|
191
|
+
.idea/replstate.xml
|
|
192
|
+
|
|
193
|
+
# Crashlytics plugin (for Android Studio and IntelliJ)
|
|
194
|
+
com_crashlytics_export_strings.xml
|
|
195
|
+
crashlytics.properties
|
|
196
|
+
crashlytics-build.properties
|
|
197
|
+
fabric.properties
|
|
198
|
+
|
|
199
|
+
# Editor-based Rest Client
|
|
200
|
+
.idea/httpRequests
|
|
201
|
+
|
|
202
|
+
# Android studio 3.1+ serialized cache file
|
|
203
|
+
.idea/caches/build_file_checksums.ser
|
|
204
|
+
|
|
205
|
+
# My TODO list
|
|
206
|
+
TODO.md
|
|
207
|
+
my_sim.py
|
|
208
|
+
stretching_rod.py
|
|
209
|
+
bent_butterfly_rod.py
|
|
210
|
+
timoshenko_beam.py
|
|
211
|
+
sample_prog.py
|
|
212
|
+
|
|
213
|
+
# Figures and pictures
|
|
214
|
+
*.png
|
|
215
|
+
|
|
216
|
+
# data files
|
|
217
|
+
*.dat
|
|
218
|
+
|
|
219
|
+
# Operating System Files
|
|
220
|
+
*.DS_Store
|
|
221
|
+
|
|
222
|
+
# txt files
|
|
223
|
+
*.txt
|
|
224
|
+
!CMakelists.txt
|
|
225
|
+
|
|
226
|
+
# movie or video file formats
|
|
227
|
+
*.mp4
|
|
228
|
+
|
|
229
|
+
# CMA-ES output files
|
|
230
|
+
outcmaes/*
|
|
231
|
+
|
|
232
|
+
# line profiling outputs
|
|
233
|
+
*.lprof
|
|
234
|
+
|
|
235
|
+
# python npy files
|
|
236
|
+
*.npy
|
|
237
|
+
*.npz
|
|
238
|
+
|
|
239
|
+
# pickle files
|
|
240
|
+
*.pickle
|
|
241
|
+
|
|
242
|
+
# zip files
|
|
243
|
+
*.zip
|
|
244
|
+
|
|
245
|
+
# csv files
|
|
246
|
+
*.csv
|
|
247
|
+
|
|
248
|
+
# ./backend dependencies
|
|
249
|
+
deps
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 skim0119
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,428 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pyelastica-jax
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Jax integration to PyElastica for GPU integration and better batch processing.
|
|
5
|
+
Author-email: Seung Hyun Kim <skim0119@gmail.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Classifier: Development Status :: 4 - Beta
|
|
9
|
+
Classifier: Intended Audience :: Education
|
|
10
|
+
Classifier: Intended Audience :: Science/Research
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Programming Language :: Python
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
15
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
16
|
+
Requires-Python: >=3.12
|
|
17
|
+
Requires-Dist: click
|
|
18
|
+
Requires-Dist: h5py
|
|
19
|
+
Requires-Dist: jax
|
|
20
|
+
Requires-Dist: pyelastica
|
|
21
|
+
Requires-Dist: tqdm
|
|
22
|
+
Provides-Extra: dev
|
|
23
|
+
Requires-Dist: autoflake; extra == 'dev'
|
|
24
|
+
Requires-Dist: black; extra == 'dev'
|
|
25
|
+
Requires-Dist: click; extra == 'dev'
|
|
26
|
+
Requires-Dist: codecov; extra == 'dev'
|
|
27
|
+
Requires-Dist: coverage; extra == 'dev'
|
|
28
|
+
Requires-Dist: flake8; extra == 'dev'
|
|
29
|
+
Requires-Dist: mypy; extra == 'dev'
|
|
30
|
+
Requires-Dist: mypy-extensions; extra == 'dev'
|
|
31
|
+
Requires-Dist: pre-commit; extra == 'dev'
|
|
32
|
+
Requires-Dist: pytest; extra == 'dev'
|
|
33
|
+
Requires-Dist: pytest-cov; extra == 'dev'
|
|
34
|
+
Requires-Dist: pytest-html; extra == 'dev'
|
|
35
|
+
Requires-Dist: pytest-mock; extra == 'dev'
|
|
36
|
+
Requires-Dist: pytest-rng; extra == 'dev'
|
|
37
|
+
Provides-Extra: docs
|
|
38
|
+
Requires-Dist: docutils>=0.18; extra == 'docs'
|
|
39
|
+
Requires-Dist: myst-parser>=1.0; extra == 'docs'
|
|
40
|
+
Requires-Dist: numpydoc>=1.3.1; extra == 'docs'
|
|
41
|
+
Requires-Dist: readthedocs-sphinx-search<0.4.0,>=0.1.1; extra == 'docs'
|
|
42
|
+
Requires-Dist: sphinx-autodoc-typehints>=1.21; extra == 'docs'
|
|
43
|
+
Requires-Dist: sphinx-book-theme>=1.0; extra == 'docs'
|
|
44
|
+
Requires-Dist: sphinx-gallery>=0.19.0; extra == 'docs'
|
|
45
|
+
Requires-Dist: sphinx>=6.1; extra == 'docs'
|
|
46
|
+
Requires-Dist: sphinxcontrib-mermaid>=0.9.2; extra == 'docs'
|
|
47
|
+
Requires-Dist: sphinxcontrib-video>=0.4.1; extra == 'docs'
|
|
48
|
+
Description-Content-Type: text/markdown
|
|
49
|
+
|
|
50
|
+
<div align='center'>
|
|
51
|
+
<h1> PyElastica-JAX </h1>
|
|
52
|
+
</div>
|
|
53
|
+
|
|
54
|
+
JAX integration to [`PyElastica`](https://github.com/GazzolaLab/PyElastica) framework for simulating assemblies of Cosserat Rods.
|
|
55
|
+
This framework extension supports more directly handling of the `block` concept.
|
|
56
|
+
|
|
57
|
+
The goal is to scale. If your problem is on order of 100~ elements, this framework might not give any speed-up, but you are welcome to still explore the features.
|
|
58
|
+
|
|
59
|
+
> Currently under development. All features are experimental, and interface are subject to change without detailed discussion. Please leave issues if you have any suggestions or recommendations.
|
|
60
|
+
> Due to the style difference between `JAX` and `numba`, the implementation in original PyElastica package would be mostly not compatible. The style of setting up the simulation is kept same.
|
|
61
|
+
|
|
62
|
+
## Before you start
|
|
63
|
+
|
|
64
|
+
`JAX`'s default floating point precision is `float32`, but the Cosserat rod numerics is not very stable with `float32`. User should enable `float64` for more stable numerics. It is not decided yet whether to make this the default behavior within `PyElastica-JAX`.
|
|
65
|
+
|
|
66
|
+
```py
|
|
67
|
+
jax.config.update('jax_enable_x64', True)
|
|
68
|
+
```
|
|
69
|
+
or
|
|
70
|
+
```bash
|
|
71
|
+
JAX_ENABLE_X64=1 python ...
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## Basic Tutorial
|
|
75
|
+
|
|
76
|
+
In `PyElastica`, a system, like `CosseratRod`, is collected and stored in a blocked memory space. This operation occurs during `finalize()`. Using `simulator.enable_block_supports(...)`, the type of block can be specified and configured.
|
|
77
|
+
|
|
78
|
+
```py
|
|
79
|
+
import jax
|
|
80
|
+
import jax.numpy as jnp
|
|
81
|
+
import elastica as ea
|
|
82
|
+
import elastica_jax as eaj
|
|
83
|
+
|
|
84
|
+
class JAXSimulator(
|
|
85
|
+
ea.BaseSystemCollection, # From PyElastica
|
|
86
|
+
eaj.JAXOps
|
|
87
|
+
):
|
|
88
|
+
pass
|
|
89
|
+
|
|
90
|
+
simulator = JAXSimulator()
|
|
91
|
+
rod_block = eaj.configure_rod_block()
|
|
92
|
+
simulator.enable_block_supports( # <-- assign the JAX block for CosseratRod
|
|
93
|
+
ea.CosseratRod,
|
|
94
|
+
rod_block,
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
# Append rods
|
|
98
|
+
n_rods = 10
|
|
99
|
+
rods = [] # Serves as a rod view
|
|
100
|
+
for _ in range(n_rods):
|
|
101
|
+
rod = ea.CosseratRod.straight_rod(...)
|
|
102
|
+
rods.append(rod)
|
|
103
|
+
simulator.append(rod)
|
|
104
|
+
simulator.finalize()
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
The syntax is almost similar to `PyElastica`. To use position-verlet scheme, load `eaj.PositionVerletJAX`.
|
|
108
|
+
Unlike `PyElastica`, stepper does not provide single `step` method, instead it provides `integrate` method. This is due to the architecture design of `JAX` that is not same as `numba`: the amortized optimization of `jit` compilation yield a lot more efficient runtime with `fori_loop`, which is ineffective to run single-step at a time.
|
|
109
|
+
|
|
110
|
+
```py
|
|
111
|
+
timestepper = eaj.PositionVerletJAX()
|
|
112
|
+
time = 0.0
|
|
113
|
+
final_time = 10.0
|
|
114
|
+
dt = 0.001
|
|
115
|
+
time = timestepper.integrate(
|
|
116
|
+
simulator,
|
|
117
|
+
time=time,
|
|
118
|
+
final_time=final_time,
|
|
119
|
+
dt=dt,
|
|
120
|
+
)
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
### How to collect data?
|
|
124
|
+
|
|
125
|
+
In `PyElastica`, data is collected by attaching `ea.Callback` to the system. In `PyElastica-JAX`, similar callback syntax is _not available_, as retrieving data from the block state is not supported.
|
|
126
|
+
|
|
127
|
+
> **_NOTE:_** This is key differnce between original PyElastica and PyElastica-JAX. The block rod (handled within the simulator during) is not always synchronized with the reference rods (created by user). This is to support multi-device and heterogeneous (GPU, CPU+GPU, etc.) execution more smoothly within JAX syntax pattern.
|
|
128
|
+
> In detail, callback in PyElastica interrupt the integrate loop and collect data for I/O. Most of the time, due to large number of simulation steps, branch like `step % step_skip == 0` is used to collect data intermittently. This could be achieved using `jax.pure_callback` (probably) but it defeats the point of using JIT. Hence, collection is taken cared outside, closer to user's control.
|
|
129
|
+
|
|
130
|
+
Here is the recommended way to collect data. The integration could be broken into chunks, at desirable frame rate.
|
|
131
|
+
User can fetch the data from the block state, and copy. For example, the following snippet could be used to collect the position to create animation.
|
|
132
|
+
|
|
133
|
+
```py
|
|
134
|
+
current_time = 0.0
|
|
135
|
+
final_time = 10.0
|
|
136
|
+
dt = 0.001
|
|
137
|
+
fps = 25.0
|
|
138
|
+
frame_dt = 1.0 / fps
|
|
139
|
+
steps_per_frame = int(round(frame_dt / dt))
|
|
140
|
+
n_frames = int(round(final_time / frame_dt)) + 1
|
|
141
|
+
for frame_idx in tqdm(range(n_frames)):
|
|
142
|
+
jax.block_until_ready(rod_block)
|
|
143
|
+
|
|
144
|
+
# Not recommended: access block memory directly. It may
|
|
145
|
+
# includes ghost elements that may not be intuitive unless
|
|
146
|
+
# user is aware of the block implementation details.
|
|
147
|
+
# Although it could be useful for more advanced use cases.
|
|
148
|
+
# shape would be (3, n_block_length)
|
|
149
|
+
position_collection = rod_block.position_collection.copy()
|
|
150
|
+
|
|
151
|
+
# Recommended: sync rod views from block to reference rods.
|
|
152
|
+
rod_block.from_device()
|
|
153
|
+
positions = []
|
|
154
|
+
for rod_view in rods:
|
|
155
|
+
positions.append(rod_view.position_collection.copy())
|
|
156
|
+
# shape would be (n_rods, 3, n_nodes)
|
|
157
|
+
position_collection = np.concatenate(positions, axis=0)
|
|
158
|
+
|
|
159
|
+
# I/O
|
|
160
|
+
save(
|
|
161
|
+
f"results/frame_{frame_idx:06d}.h5",
|
|
162
|
+
position_collection,
|
|
163
|
+
time=current_time,
|
|
164
|
+
frame_idx=frame_idx,
|
|
165
|
+
n_workers=4, # For very large case, it is recommended to parallelize I/O
|
|
166
|
+
)
|
|
167
|
+
|
|
168
|
+
# Integrate
|
|
169
|
+
chunk_final_time = current_time + frame_dt
|
|
170
|
+
stepper.integrate(
|
|
171
|
+
simulator,
|
|
172
|
+
time=current_time,
|
|
173
|
+
final_time=chunk_final_time,
|
|
174
|
+
dt=dt,
|
|
175
|
+
)
|
|
176
|
+
current_time = chunk_final_time
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
> This part is actively under development for more flexible and better API. Leave issues if you have any suggestions or recommendations.
|
|
180
|
+
|
|
181
|
+
### Defining Operation
|
|
182
|
+
|
|
183
|
+
In `PyElastica`, operations could be inserted using modules such as `ea.Forcing`, `ea.Damping`, `ea.Constraints`, etc. Depending on modules, user can add custom operation at the end of kinematic steps (`constrain_values`), end of dynamic steps (`constrain_rates`), or add loads before the dynamic steps (`synchronize`). This gives great flexibility and customizability to the simulation to define wide range of physics and behaviors for problems.
|
|
184
|
+
|
|
185
|
+
`PyElastica-JAX` follows the same concepts and philosophy, but with some simplification and changes according to `JAX`'s pure function pattern. This gives better optimization and amortized compilation for entire JIT timestepping loop.
|
|
186
|
+
|
|
187
|
+
In `PyElastica-JAX`, adding rod-wise operation is done with mixin module `eaj.JAXOps` into simulator class. This gives ability to run `operate` method to add operation to a specific system.
|
|
188
|
+
|
|
189
|
+
```py
|
|
190
|
+
class JAXSimulator(ea.BaseSystemCollection, eaj.JAXOps):
|
|
191
|
+
pass
|
|
192
|
+
|
|
193
|
+
simulator = JAXSimulator()
|
|
194
|
+
rod_block = eaj.configure_rod_block()
|
|
195
|
+
simulator.enable_block_supports(ea.CosseratRod, rod_block)
|
|
196
|
+
|
|
197
|
+
rod = ea.CosseratRod.straight_rod(...)
|
|
198
|
+
simulator.append(rod)
|
|
199
|
+
|
|
200
|
+
simulator.operate(rod).using(
|
|
201
|
+
MyOperation,
|
|
202
|
+
... # Arguments for MyOperation
|
|
203
|
+
)
|
|
204
|
+
|
|
205
|
+
simulator.finalize()
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
Similar to `PyElastica`, module has associated template class `eaj.NoOpsJax` that user can derive from to implement the operation. Deriving this class is compulsory to be properly registered into the operation group.
|
|
209
|
+
|
|
210
|
+
By overriding following hooks, user can implement the oepration.
|
|
211
|
+
|
|
212
|
+
- `jax_operate_synchronize` — Executed before the dynamic step.
|
|
213
|
+
- `jax_operate_constrain_values` — Executed after the kinematic step.
|
|
214
|
+
- `jax_operate_constrain_rates` — Executed after the dynamic step.
|
|
215
|
+
|
|
216
|
+
> Similar to `PyElastica`, this flexibility gives user greater ability to tweak and customize the simulation; hence, it is user's responsiblity to ensure the consistency of the modeling and physics.
|
|
217
|
+
|
|
218
|
+
Each hook receives a `rod_view` (JAX arrays for one rod inside the block) and returns the updated view. This function should be _pure_ JAX function. See [`src/elastica_jax/operations.py`](https://github.com/skim0119/PyElastica-JAX/blob/main/src/elastica_jax/operations.py) for built-in operations.
|
|
219
|
+
|
|
220
|
+
Here is the example script of implementing gravity on the rod with one-end-fixed.
|
|
221
|
+
|
|
222
|
+
```py
|
|
223
|
+
type Vector = jax.Array
|
|
224
|
+
class CantileverOperation(eaj.NoOpsJax):
|
|
225
|
+
"""Apply uniform gravitational acceleration to nodal masses."""
|
|
226
|
+
|
|
227
|
+
def __init__(
|
|
228
|
+
self,
|
|
229
|
+
acc_gravity: Vector,
|
|
230
|
+
*,
|
|
231
|
+
_system=None,
|
|
232
|
+
) -> None:
|
|
233
|
+
self.acc_gravity = np.asarray(acc_gravity, dtype=np.float64)
|
|
234
|
+
self.fixed_position_collection = np.asarray(
|
|
235
|
+
_system.position_collection[..., 0].copy()
|
|
236
|
+
)
|
|
237
|
+
self.fixed_directors_collection = np.asarray(
|
|
238
|
+
_system.director_collection[..., 0].copy()
|
|
239
|
+
)
|
|
240
|
+
|
|
241
|
+
def jax_operate_constrain_values(self, rod_view, time):
|
|
242
|
+
rod_view.position_collection = rod_view.position_collection.at[:, 0].set(
|
|
243
|
+
self.fixed_position_collection
|
|
244
|
+
)
|
|
245
|
+
rod_view.director_collection = rod_view.director_collection.at[:, :, 0].set(
|
|
246
|
+
self.fixed_directors_collection
|
|
247
|
+
)
|
|
248
|
+
return rod_view
|
|
249
|
+
|
|
250
|
+
def jax_operate_constrain_rates(self, rod_view, time):
|
|
251
|
+
rod_view.velocity_collection = rod_view.velocity_collection.at[:, 0].set(
|
|
252
|
+
jnp.zeros(3, dtype=rod_view.velocity_collection.dtype)
|
|
253
|
+
)
|
|
254
|
+
rod_view.omega_collection = rod_view.omega_collection.at[:, 0].set(
|
|
255
|
+
jnp.zeros(3, dtype=rod_view.omega_collection.dtype)
|
|
256
|
+
)
|
|
257
|
+
return rod_view
|
|
258
|
+
|
|
259
|
+
def jax_operate_synchronize(self, rod_view, time):
|
|
260
|
+
rod_view.external_forces = (
|
|
261
|
+
rod_view.external_forces
|
|
262
|
+
+ self.acc_gravity[:, None] * rod_view.mass[None, :]
|
|
263
|
+
)
|
|
264
|
+
return rod_view
|
|
265
|
+
|
|
266
|
+
# Register on a specific rod; `_system` is injected by the framework at finalize().
|
|
267
|
+
simulator.operate(rod).using(
|
|
268
|
+
CantileverOperation,
|
|
269
|
+
acc_gravity=np.array([0.0, 0.0, -9.80665]),
|
|
270
|
+
)
|
|
271
|
+
|
|
272
|
+
simulator.finalize()
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
### Defining Block Operation
|
|
276
|
+
|
|
277
|
+
`PyElastica-JAX` supports block-wise operation. This is partially tested on `PyElastica` have shown great performance boost, but not fully included due to difficulties in customizing every operation to be block-wise and JIT compatible. This enables faster implementation-execution of large-scale (many rods) simulations, especially when operation needs to be applied to all rods at each step.
|
|
278
|
+
|
|
279
|
+
This operation module provides two style of operation: __block__ and __per-rod__.
|
|
280
|
+
Block-rod is essentially a single-rod view of all rods in contiguous memory space, with ghost elements/memory padding to safely handle spanwise operations (differential and quadratures).
|
|
281
|
+
__Block__ operation treats entire rod at once. It is useful to define all-element operations, such as gravity, dissipation, field forcing, etc. __Per-rod__ operation treats each rod at once. Underneat, it uses `jax.vmap` to batch the operation across all rods.
|
|
282
|
+
|
|
283
|
+
> When using block operation with spanwise equation, carefully treat the ghost ghost elements and their behavior. Ghost padding is only to separate the numerics to be isolated within the rod, but does not have any safety to keep those values zeros. <TODO: add more details in documentation>
|
|
284
|
+
|
|
285
|
+
To use block operation, add the `eaj.JAXOpsBlock` mixin and register with `operate_block`:
|
|
286
|
+
|
|
287
|
+
```py
|
|
288
|
+
class JAXSimulator(ea.BaseSystemCollection, eaj.JAXOpsBlock):
|
|
289
|
+
pass
|
|
290
|
+
|
|
291
|
+
simulator = JAXSimulator()
|
|
292
|
+
rod_block = eaj.configure_rod_block()
|
|
293
|
+
simulator.enable_block_supports(ea.CosseratRod, rod_block)
|
|
294
|
+
|
|
295
|
+
for _ in range(n_rods):
|
|
296
|
+
simulator.append(ea.CosseratRod.straight_rod(...))
|
|
297
|
+
|
|
298
|
+
simulator.operate_block(rod_block).using(
|
|
299
|
+
MyBlockOperation,
|
|
300
|
+
... # Arguments for MyBlockOperation
|
|
301
|
+
)
|
|
302
|
+
|
|
303
|
+
simulator.finalize()
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
To define the operator, derive from `eaj.NoBlockOpJax` and override block-stage hooks. Similar to regular oepration, following functions could be overridden:
|
|
307
|
+
|
|
308
|
+
- `jax_block_operate_synchronize` — Executed before the dynamic step.
|
|
309
|
+
- `jax_block_operate_constrain_values` — Executed after the kinematic step.
|
|
310
|
+
- `jax_block_operate_constrain_rates` — Executed after the dynamic step.
|
|
311
|
+
- `jax_per_rod_operate_synchronize` — Executed before the dynamic step.
|
|
312
|
+
- `jax_per_rod_operate_constrain_values` — Executed after the kinematic step.
|
|
313
|
+
- `jax_per_rod_operate_constrain_rates` — Executed after the dynamic step.
|
|
314
|
+
|
|
315
|
+
`jax_block_operate_*` functions receive the entire block state, and return the updated state.
|
|
316
|
+
`jax_per_rod_operate_*` functions receive the single rod view and return the updated state.
|
|
317
|
+
|
|
318
|
+
Block-wide gravity on all rods and one-end-fixed on each rods:
|
|
319
|
+
|
|
320
|
+
```py
|
|
321
|
+
type Vector = jax.Array
|
|
322
|
+
type Director = jax.Array
|
|
323
|
+
|
|
324
|
+
# class GravityBlockOp:
|
|
325
|
+
class GravityBlockOp(eaj.NoBlockOpJax):
|
|
326
|
+
def __init__(self, acc_gravity: Vector, tip_position: Vector, tip_director: Director) -> None:
|
|
327
|
+
self.acc_gravity = acc_gravity
|
|
328
|
+
|
|
329
|
+
def jax_per_rod_operate_constrain_values(self, rod_view, time):
|
|
330
|
+
rod_view.position_collection = rod_view.position_collection.at[:, 0].set(
|
|
331
|
+
self.fixed_position_collection
|
|
332
|
+
)
|
|
333
|
+
rod_view.director_collection = rod_view.director_collection.at[:, :, 0].set(
|
|
334
|
+
self.fixed_directors_collection
|
|
335
|
+
)
|
|
336
|
+
return rod_view
|
|
337
|
+
|
|
338
|
+
def jax_per_rod_operate_constrain_rates(self, rod_view, time):
|
|
339
|
+
rod_view.velocity_collection = rod_view.velocity_collection.at[:, 0].set(
|
|
340
|
+
jnp.zeros(3, dtype=rod_view.velocity_collection.dtype)
|
|
341
|
+
)
|
|
342
|
+
rod_view.omega_collection = rod_view.omega_collection.at[:, 0].set(
|
|
343
|
+
jnp.zeros(3, dtype=rod_view.omega_collection.dtype)
|
|
344
|
+
)
|
|
345
|
+
return rod_view
|
|
346
|
+
|
|
347
|
+
def jax_block_operate_synchronize(self, block_state, time):
|
|
348
|
+
block_state.external_forces = block_state.external_forces + self.acc_gravity[:, None] * block_state.mass[None, :]
|
|
349
|
+
return updated
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
simulator.operate_block(rod_block).using(
|
|
353
|
+
GravityBlockOp,
|
|
354
|
+
acc_gravity=jnp.array([0.0, 0.0, -9.80665]),
|
|
355
|
+
tip_position=jnp.array([0.0, 0.0, 0.0]),
|
|
356
|
+
tip_director=jnp.eye(3),
|
|
357
|
+
)
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
## Advanced Usage
|
|
361
|
+
|
|
362
|
+
### GPU Execution
|
|
363
|
+
|
|
364
|
+
Pass an explicit backend to `configure_rod_block` so rod state is allocated on that
|
|
365
|
+
device before `finalize()`:
|
|
366
|
+
|
|
367
|
+
```py
|
|
368
|
+
rod_block = eaj.configure_rod_block(device="cuda", device_dtype=np.float64)
|
|
369
|
+
simulator.enable_block_supports(ea.CosseratRod, rod_block)
|
|
370
|
+
...
|
|
371
|
+
simulator.finalize()
|
|
372
|
+
```
|
|
373
|
+
|
|
374
|
+
### Multi-device Block Execution
|
|
375
|
+
|
|
376
|
+
> Work-in-progress
|
|
377
|
+
|
|
378
|
+
Use separate blocks when each rod group is independent. Each block is assigned to
|
|
379
|
+
one device and compiled as its own JIT `fori_loop` rollout:
|
|
380
|
+
|
|
381
|
+
```py
|
|
382
|
+
devices = eaj.resolve_backend_devices("cuda")
|
|
383
|
+
block_0 = eaj.configure_rod_block(device=devices[0])
|
|
384
|
+
block_1 = eaj.configure_rod_block(device=devices[1])
|
|
385
|
+
|
|
386
|
+
# Distinct rod types select which configured block owns each rod.
|
|
387
|
+
simulator.enable_block_supports(RodTypeOnGPU0, block_0)
|
|
388
|
+
simulator.enable_block_supports(RodTypeOnGPU1, block_1)
|
|
389
|
+
simulator.operate_block(block_0).using(MyBlockOp)
|
|
390
|
+
simulator.operate_block(block_1).using(MyBlockOp)
|
|
391
|
+
```
|
|
392
|
+
|
|
393
|
+
See the [explicit multi-block tutorial](tutorial/01-block-multi-device.py) for a
|
|
394
|
+
complete example.
|
|
395
|
+
|
|
396
|
+
Use one sharded block when the rods should remain one logical block. Rods are split
|
|
397
|
+
evenly across the requested devices:
|
|
398
|
+
|
|
399
|
+
```py
|
|
400
|
+
devices = eaj.resolve_backend_devices("cuda")[:2]
|
|
401
|
+
rod_block = eaj.configure_rod_block_sharded(
|
|
402
|
+
devices=devices,
|
|
403
|
+
device_dtype=np.float64,
|
|
404
|
+
)
|
|
405
|
+
simulator.enable_block_supports(ea.CosseratRod, rod_block)
|
|
406
|
+
simulator.operate_block(rod_block).using(MyBlockOp)
|
|
407
|
+
```
|
|
408
|
+
|
|
409
|
+
See the [sharded block tutorial](tutorial/02-block-multi-device-sharded.py) for a
|
|
410
|
+
complete example.
|
|
411
|
+
|
|
412
|
+
Both layouts run compiled loops without a Python timestep fallback. Cross-device
|
|
413
|
+
operators that couple independent blocks are currently rejected.
|
|
414
|
+
|
|
415
|
+
### Multi-core Multi-host CPU Execution
|
|
416
|
+
|
|
417
|
+
TODO: `mpi4jax` integration for multi-core MPI execution.
|
|
418
|
+
|
|
419
|
+
## Terminology
|
|
420
|
+
|
|
421
|
+
This repository expand the usage of `block` concept directly, along with `JAX`'s concept of mapping memory and function.
|
|
422
|
+
|
|
423
|
+
- `memory_block` is a block that is a collection of rods (systems).
|
|
424
|
+
- `sharded_block` is a block that is sharded into multiple blocks.
|
|
425
|
+
|
|
426
|
+
## Features that are extended from PyElastica
|
|
427
|
+
|
|
428
|
+
- `configure_rod_block_sharded(..., block_checkpoint=path)`: pass the checkpoint path when configuring the block. During `finalize()` → `construct_memory_block_structures` → block `__init__`, an existing checkpoint skips packing rod data into the block and loads saved state instead; a missing file triggers a save after block construction.
|