Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type-hinting elastica #367

Merged
merged 175 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from 173 commits
Commits
Show all changes
175 commits
Select commit Hold shift + click to select a range
76aa17c
update: add cli for mypy run and cleanup
skim0119 Apr 27, 2024
6a4dd5f
feat: add mypy checker
skim0119 Apr 27, 2024
e75ca6b
Update main.yml
skim0119 Apr 27, 2024
e2f2e73
Update main.yml
skim0119 Apr 27, 2024
f1b7126
update mypy setup
skim0119 Apr 28, 2024
8a0ceb9
Merge branch 'update/mypy' of https://github.com/GazzolaLab/PyElastic…
skim0119 Apr 28, 2024
f3c650e
fix python-version string for mypy config
skim0119 Apr 28, 2024
0af3794
rename interface into protocol
skim0119 Apr 28, 2024
afcbe6e
change interface name to protocol
skim0119 Apr 28, 2024
26f7c3b
make separate tag module for easy typing
skim0119 Apr 28, 2024
016d69f
add system collection as native subtype
skim0119 Apr 28, 2024
220e7ef
Merge branch 'update-0.3.3' into typing/timestepper
skim0119 Apr 28, 2024
237dc2f
typehint is_system_a_collection
skim0119 Apr 28, 2024
63cf342
wip: typing symplectic stepper
skim0119 Apr 28, 2024
79fa1a0
Merge branch 'update-0.3.3' into update/mypy
skim0119 Apr 28, 2024
538721c
wip: timestepper typing
skim0119 Apr 29, 2024
6426bd1
add generic system protocol
skim0119 Apr 29, 2024
df67eb4
wip explicit stepper typing
skim0119 Apr 29, 2024
9b8266a
test: fix rod/rigidbody mocking
skim0119 Apr 29, 2024
577ff1b
wip: resolve circular dependencies due to protocol
skim0119 Apr 29, 2024
f0f5c5a
remove protocol from coverage
skim0119 Apr 29, 2024
9149576
Merge branch 'update/mypy' into typing/timestepper
skim0119 Apr 29, 2024
cba8e24
Update elastica/timestepper/symplectic_steppers.py
armantekinalp Apr 30, 2024
df474eb
formatting: remove any unused imports
skim0119 Apr 30, 2024
f95925d
Create py.typed
skim0119 Apr 30, 2024
b152c20
Merge pull request #372 from skim0119/typing/timestepper
skim0119 Apr 30, 2024
71a566c
refactor: Stateful -> Symplectic
skim0119 May 1, 2024
c033bd7
update: use string-literal taging instead of class-typing
skim0119 May 1, 2024
0b47d9a
build(deps): bump tqdm from 4.66.2 to 4.66.3
dependabot[bot] May 3, 2024
edcc4f9
redesign integration flow: mypy checked
skim0119 May 1, 2024
643fa7d
update: save operator sequence without running at each timestep
skim0119 May 2, 2024
c5a9c5a
test: remove symple system stepping - no longer support
skim0119 May 2, 2024
3fb7dac
fix: tuple is not supposed to be mutable
skim0119 May 2, 2024
7dcf238
feat: allow single system integration
skim0119 May 2, 2024
b615cd4
test: fix broken tests due to changes in steppig method
skim0119 May 2, 2024
0531ef8
remove: timestepper tag
skim0119 May 3, 2024
425e14a
update tutorial with new timestepper syntax
skim0119 May 3, 2024
52a77da
feat: add testing for single system integrate overload
skim0119 May 4, 2024
42bfd09
autoflake8 -> autoflake
skim0119 May 4, 2024
3a06cb4
use isinstance
skim0119 May 5, 2024
cf862d0
Merge pull request #375 from GazzolaLab/dependabot/pip/tqdm-4.66.3
skim0119 May 5, 2024
408d0bd
build(deps): bump jinja2 from 3.1.3 to 3.1.4
dependabot[bot] May 6, 2024
87a2e74
Merge pull request #377 from GazzolaLab/dependabot/pip/jinja2-3.1.4
skim0119 May 7, 2024
888bfbd
typing: base_system module
skim0119 May 7, 2024
ff4fbc2
wip: callback module typing
skim0119 May 7, 2024
98e9d0e
Merge pull request #373 from skim0119/typing/timestepper
skim0119 May 7, 2024
2efd4ec
Merge branch 'update/mypy' into typing/modules
skim0119 May 7, 2024
c3617fd
Merge branch 'update-0.3.3' into update/mypy
skim0119 May 11, 2024
b5f8fa9
Merge branch 'update/mypy' into typing/modules
skim0119 May 11, 2024
df61998
typing: callback module typing
skim0119 May 11, 2024
1994800
test: fix callback module related tests
skim0119 May 13, 2024
63705a7
Merge branch 'update-0.3.3' into update/mypy
skim0119 May 13, 2024
07dd6f4
typing: base system with requisite as part of system protocol
skim0119 May 13, 2024
3d33e4f
Merge remote-tracking branch 'origin' into typing/modules
skim0119 May 13, 2024
48c3961
Merge branch 'update/mypy' into typing/modules
skim0119 May 13, 2024
4279c68
memory block typing for rod, rigid body and rod base
armantekinalp May 13, 2024
f565be1
fix:typing for rod and rigid body data structures to make consistent
armantekinalp May 13, 2024
cde8aba
Merge branch 'update/mypy' into typing/memory_block
armantekinalp May 14, 2024
00b1fed
Update elastica/memory_block/memory_block_rigid_body.py
armantekinalp May 15, 2024
fdd2db9
Update elastica/memory_block/memory_block_rod.py
armantekinalp May 15, 2024
2a7b64b
update type hinting in memory block rigid body and rod and add system
armantekinalp May 15, 2024
4d05804
update elastica typing alias
skim0119 May 15, 2024
1e80963
Merge branch 'update/mypy' into typing/modules
skim0119 May 15, 2024
134b218
Merge branch 'update/mypy' into typing/memory_block
skim0119 May 15, 2024
7506ea0
typing SystemIdxType import added in memory block rigid body and rod
armantekinalp May 15, 2024
55f4944
fix requisite module type
skim0119 May 15, 2024
3111d79
add generic type for operator grouping
skim0119 May 16, 2024
35e78f1
typing: connections module and necessary changes for protocol
skim0119 May 16, 2024
1418d67
Improve typehinting at root directory
ankith26 May 19, 2024
b06b8f7
Improve type hinting in the rod directory
ankith26 May 19, 2024
856e901
KnotTheory remove protocol inherit
ankith26 May 20, 2024
03c00f4
Merge pull request #341 from ankith26/more-typing
skim0119 May 20, 2024
96ae5ca
Merge branch 'update/mypy' into typing/modules
skim0119 May 20, 2024
0fd9c1c
test: fix missing values
skim0119 May 20, 2024
424a9a8
ignore njit wrapper
skim0119 May 20, 2024
5e76784
Merge branch 'update/mypy' into typing/modules
skim0119 May 20, 2024
d7ba02e
update: mypy pass for connection module
skim0119 Jun 13, 2024
5dcfe75
remove unused protocol
skim0119 Jun 13, 2024
63ea063
Merge branch 'update-0.3.3' into update/mypy
skim0119 Jun 13, 2024
23546af
Merge branch 'update/mypy' into typing/modules
skim0119 Jun 13, 2024
2a08279
typing: edit FreeJoint base indexing type
skim0119 Jun 13, 2024
4ab8e4c
typing: finish constraints hinting
skim0119 Jun 13, 2024
9d75a89
typing: finish contact hinting
skim0119 Jun 13, 2024
842451f
test: update unittest for refactor codes
skim0119 Jun 13, 2024
b4d54d2
typing: damping module finish
skim0119 Jun 13, 2024
d32007d
typing: finish forcing module hinting
skim0119 Jun 13, 2024
1b0f109
typing: make memory block hinting agree to block module
skim0119 Jun 13, 2024
c1217b4
remove deprecated functions to avoid unnecessary type hinting
skim0119 Jun 13, 2024
7309e4a
Merge branch 'update/mypy' into typing/memory_block
skim0119 Jun 13, 2024
1e38e3a
Merge pull request #384 from armantekinalp/typing/memory_block
skim0119 Jun 13, 2024
d5dc4d6
Merge branch 'update/mypy' into typing/modules
skim0119 Jun 13, 2024
5d139d3
exclude experimental work from type-hinting
skim0119 Jun 13, 2024
3c7bc5a
chore: internal _typing.py for typedefs for rebase
sy-cui May 7, 2024
cb736c0
chore: typing for rigid_body.py
sy-cui May 7, 2024
ee7cc55
refactor: change J_omega to lower case
sy-cui May 7, 2024
b2e6ebf
chore: typing for cylinder.py
sy-cui May 7, 2024
0febaa0
chore: remove commented functions
sy-cui May 7, 2024
e194088
chore: typing for sphere.py
sy-cui May 7, 2024
8f0ad69
refactor: remove unused _bootstrap functions and state classes
sy-cui May 10, 2024
c9d1e95
chore: typing for rigid body data structure
sy-cui May 10, 2024
780ed6f
fix: fixed repetitive codes in cylinder.py caused by rebase merge
sy-cui Jun 14, 2024
60d951f
refactor: temporarily remove REQUISITE_MODULES
sy-cui Jun 14, 2024
62c3e21
typing: rearange protocol for system
skim0119 Jun 14, 2024
bf5306a
typing: minor fix that reference RodType
skim0119 Jun 14, 2024
0f053f2
typing: rod module and refactor knot theory protocol
skim0119 Jun 14, 2024
d7da645
refactor: remove unused bootstrap code from rigid body data_structure
sy-cui Jun 14, 2024
f50876c
Merge pull request #395 from sy-cui/typing/rigid_body
skim0119 Jun 14, 2024
87a4936
Merge branch 'update/mypy' into typing/modules
skim0119 Jun 15, 2024
51f01a9
typing: system utility
skim0119 Jun 13, 2024
d979304
feat: Euler Forward memory
skim0119 Jun 13, 2024
7ee48e6
Merge branch 'update/mypy' into typing/rods
skim0119 Jun 15, 2024
3e76a38
typing: contact related modules
skim0119 Jun 15, 2024
8a056a6
remove AllowedContactType union alias to distinguish cases
skim0119 Jun 15, 2024
7484502
typing: add basic properties for surface_base definition
skim0119 Jun 15, 2024
64e39a6
typing: include interaction module
skim0119 Jun 15, 2024
97c8bd7
Merge pull request #394 from skim0119/typing/systems
skim0119 Jun 15, 2024
a973ca7
Merge branch 'update/mypy' into typing/modules
skim0119 Jun 15, 2024
b5db63a
Merge pull request #382 from skim0119/typing/modules
skim0119 Jun 16, 2024
038746f
Merge branch 'update/mypy' into typing/rods
skim0119 Jun 16, 2024
32130fb
typing: other minor update for different operators
skim0119 Jun 16, 2024
ee57acc
Merge branch 'typing/rods' into typing/contact
skim0119 Jun 16, 2024
3a06dc9
remove deprecated joint functions
skim0119 Jun 16, 2024
0b38d36
refactor: mv memory related methods into separate file
skim0119 Jun 16, 2024
439dc3d
Merge pull request #396 from skim0119/typing/rods
skim0119 Jun 16, 2024
7fb82a9
Merge branch 'update/mypy' into typing/contact
skim0119 Jun 16, 2024
8f5af01
resolve circular import
skim0119 Jun 16, 2024
4df6150
test: remove unittest for ExternalContact and SelfContact
skim0119 Jun 16, 2024
0e1c9ab
typing: collision utils
skim0119 Jun 16, 2024
83ec4fe
wip: typing symplectic stepper
skim0119 Jun 16, 2024
267bca2
fix tests with refactoring symplectic system
skim0119 Jun 16, 2024
e4d9a13
remove deprecated functions in interaction
skim0119 Jun 16, 2024
090ac1e
remove deprecated anisotropic friction msg
skim0119 Jun 16, 2024
f6738e9
wip: fixing type disagreements
skim0119 Jun 16, 2024
5553400
resolve liskov error
skim0119 Jun 16, 2024
77952bb
wip: finalizing typing system and system collection relation
skim0119 Jun 17, 2024
6b39aea
refactor memory block related utility functions
skim0119 Jun 17, 2024
cdce69e
type: finish organizing prototype
skim0119 Jun 17, 2024
1a00700
tests: use tmp_path to save test files
skim0119 Jun 17, 2024
c1d4bdc
fix: formatting
skim0119 Jun 17, 2024
c323e56
fix: directly access number of elements
skim0119 Jun 17, 2024
6c20c3a
remove indexing feature for connection
skim0119 Jun 17, 2024
fc4a303
Merge pull request #397 from skim0119/typing/contact
armantekinalp Jun 17, 2024
d324217
refactor: rigid body numbers typing
skim0119 Jun 17, 2024
d2c7dac
Merge branch 'update-0.3.3' into update/mypy
skim0119 Jun 17, 2024
9267f38
add static system type
skim0119 Jun 17, 2024
45c6cce
Merge branch 'update/mypy' of https://github.com/GazzolaLab/PyElastic…
skim0119 Jun 17, 2024
13fd737
exclude pyvista untyped import
skim0119 Jun 19, 2024
e54536c
docs: add duck-typing structure into documentation
skim0119 Jun 24, 2024
26dd70f
docs: add duck-typing structure into documentation
skim0119 Jun 24, 2024
9c8d538
Merge branch 'update/mypy' of https://github.com/GazzolaLab/PyElastic…
skim0119 Jun 25, 2024
a036bc1
type: reset functions
skim0119 Jun 25, 2024
1e395ab
revert: catenary case
skim0119 Jun 25, 2024
256d85a
type: system factory function np.floating -> float
skim0119 Jun 25, 2024
210d172
type: feature API np.floating -> float
skim0119 Jun 25, 2024
e2c65f1
fix CI: minor typo, deprecated functions, and mypy
skim0119 Jun 25, 2024
d3ddc66
type: remove ignore during timestepper, use type-cast
skim0119 Jun 25, 2024
df8477a
bug: fix issue that boudnary condition was not properly beling applied
skim0119 Jun 25, 2024
06cc1e0
refactor: rename rod->system for proper inheritance syntax
skim0119 Jun 25, 2024
069e2cd
fix: disallow None default value, instead use empty tuple
skim0119 Jun 25, 2024
c4584f4
fix: experimental parallel connection to be batch-wise operation
skim0119 Jun 25, 2024
9b267af
check example cases, fix outdated syntax
skim0119 Jun 25, 2024
9d64f19
Update elastica/rod/cosserat_rod.py
armantekinalp Jun 25, 2024
2ff4e4d
Update elastica/external_forces.py
armantekinalp Jun 25, 2024
be228f0
Update elastica/modules/protocol.py
armantekinalp Jun 25, 2024
bb4b2c5
Update elastica/typing.py
armantekinalp Jun 25, 2024
e3ac13e
remove ambiguity: np.integer -> np.int32, np.floating -> np.float64
skim0119 Jun 26, 2024
3d99647
Merge branch 'update/mypy' of https://github.com/GazzolaLab/PyElastic…
skim0119 Jun 26, 2024
76f642c
test: fix few typings that has been ignored
skim0119 Jun 26, 2024
9fdc9f0
add ring-rod typehint check
skim0119 Jun 26, 2024
7039bab
refactor: clarify iterating `systems` vs `block_systems`
skim0119 Jun 26, 2024
0bd4705
type: remove None-type for connection index for simplicity
skim0119 Jun 26, 2024
901b19a
remove out-dated function
skim0119 Jun 26, 2024
e04358a
fix typo
skim0119 Jun 26, 2024
a8d9a6f
change: int_ -> int32
skim0119 Jun 27, 2024
92d8b91
Merge branch 'update/mypy' of https://github.com/GazzolaLab/PyElastic…
skim0119 Jun 27, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ jobs:
- name: Run tests
run: |
make test
- name: Typechecking
if: ${{ startsWith(runner.os, 'macOS') }}
armantekinalp marked this conversation as resolved.
Show resolved Hide resolved
run: |
make mypy
report-coverage: # Report coverage from python 3.10 and mac-os. May change later
runs-on: ${{ matrix.os }}
strategy:
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ __pycache__/
# C extensions
*.so

*.swp

# Distribution / packaging
.Python
build/
Expand Down
36 changes: 22 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#* Variables
PYTHON := python3
PYTHONPATH := `pwd`
AUTOFLAKE8_ARGS := -r --exclude '__init__.py' --keep-pass-after-docstring
AUTOFLAKE_ARGS := -r
#* Poetry
.PHONY: poetry-download
poetry-download:
Expand Down Expand Up @@ -47,19 +47,23 @@ flake8:
poetry run flake8 --version
poetry run flake8 elastica tests

.PHONY: autoflake8-check
autoflake8-check:
poetry run autoflake8 --version
poetry run autoflake8 $(AUTOFLAKE8_ARGS) elastica tests examples
poetry run autoflake8 --check $(AUTOFLAKE8_ARGS) elastica tests examples
.PHONY: autoflake-check
autoflake-check:
poetry run autoflake --version
poetry run autoflake $(AUTOFLAKE_ARGS) elastica tests examples
poetry run autoflake --check $(AUTOFLAKE_ARGS) elastica tests examples

.PHONY: autoflake8-format
autoflake8-format:
poetry run autoflake8 --version
poetry run autoflake8 --in-place $(AUTOFLAKE8_ARGS) elastica tests examples
.PHONY: autoflake-format
autoflake-format:
poetry run autoflake --version
poetry run autoflake --in-place $(AUTOFLAKE_ARGS) elastica tests examples

.PHONY: format-codestyle
format-codestyle: black flake8
format-codestyle: black autoflake-format

.PHONY: mypy
mypy:
poetry run mypy --config-file pyproject.toml elastica

.PHONY: test
test:
Expand All @@ -74,14 +78,14 @@ test_coverage_xml:
NUMBA_DISABLE_JIT=1 poetry run pytest --cov=elastica --cov-report=xml

.PHONY: check-codestyle
check-codestyle: black-check flake8 autoflake8-check
check-codestyle: black-check flake8 autoflake-check

.PHONY: formatting
formatting: format-codestyle

.PHONY: update-dev-deps
update-dev-deps:
poetry add -D pytest@latest coverage@latest pytest-html@latest pytest-cov@latest black@latest
poetry add -D mypy@latest pytest@latest coverage@latest pytest-html@latest pytest-cov@latest black@latest

#* Cleaning
.PHONY: pycache-remove
Expand All @@ -92,6 +96,10 @@ pycache-remove:
dsstore-remove:
find . | grep -E ".DS_Store" | xargs rm -rf

.PHONY: mypycache-remove
mypycache-remove:
find . | grep -E ".mypy_cache" | xargs rm -rf

.PHONY: ipynbcheckpoints-remove
ipynbcheckpoints-remove:
find . | grep -E ".ipynb_checkpoints" | xargs rm -rf
Expand All @@ -105,7 +113,7 @@ build-remove:
rm -rf build/

.PHONY: cleanup
cleanup: pycache-remove dsstore-remove ipynbcheckpoints-remove pytestcache-remove
cleanup: pycache-remove dsstore-remove ipynbcheckpoints-remove pytestcache-remove mypycache-remove

all: format-codestyle cleanup test

Expand Down
110 changes: 108 additions & 2 deletions docs/advanced/PackageDesign.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,114 @@
# Code Design: Mixin and Composition
# Code Design

## Mixin and Composition

Elastica package follows Mixin and composition design patterns that may be unfamiliar to users. Here is a collection of references that introduce the package design.

## References
### References

- [stackoverflow discussion on Mixin](https://stackoverflow.com/questions/533631/what-is-a-mixin-and-why-are-they-useful)
- [example of Mixin: python collections](https://docs.python.org/dev/library/collections.abc.html)

## Duck Typing

Elastica package uses duck typing to allow users to define their own classes and functions. Here is a `typing.Protocol` structure that is used in the package.

### Systems

``` {mermaid}
flowchart LR
direction RL
subgraph Systems Protocol
direction RL
SLBD(SlenderBodyGeometryProtool)
SymST["SymplecticSystem:\n• KinematicStates/Rates\n• DynamicStates/Rates"]
style SymST text-align:left
ExpST["ExplicitSystem:\n• States (Unused)"]
style ExpST text-align:left
P((position\nvelocity\nacceleration\n..)) --> SLBD
subgraph StaticSystemType
Surface
Mesh
end
subgraph SystemType
direction TB
Rod
RigidBody
end
SLBD --> SymST
SystemType --> SymST
SLBD --> ExpST
SystemType --> ExpST
end
subgraph Timestepper Protocol
direction TB
StP["StepperProtocol\n• step(SystemCollection, time, dt)"]
style StP text-align:left
SymplecticStepperProtocol["SymplecticStepperProtocol\n• PositionVerlet"]
style SymplecticStepperProtocol text-align:left
ExpplicitStepperProtocol["ExpplicitStepperProtocol\n(Unused)"]
end

subgraph SystemCollection

end
SymST --> SystemCollection --> SymplecticStepperProtocol
ExpST --> SystemCollection --> ExpplicitStepperProtocol
StaticSystemType --> SystemCollection

```

### System Collection (Build memory block)

``` {mermaid}
flowchart LR
Sys((Systems))
St((Stepper))
subgraph SystemCollectionType
direction LR
StSys["StaticSystem:\n• Surface\n• Mesh"]
style StSys text-align:left
DynSys["DynamicSystem:\n• Rod\n  • CosseratRod\n• RigidBody\n  • Sphere\n  • Cylinder"]
style DynSys text-align:left

BlDynSys["BlockSystemType:\n• BlockCosseratRod\n• BlockRigidBody"]
style BlDynSys text-align:left

F{{"Feature Group (OperatorGroup):\n• Synchronize\n• Constrain values\n• Constrain rates\n• Callback"}}
style F text-align:left
end
Sys --> StSys --> F
Sys --> DynSys -->|Finalize| BlDynSys --> St
DynSys --> F <--> St

```

### System Collection (Features)

``` {mermaid}
flowchart LR
Sys((Systems))
St((Stepper))
subgraph SystemCollectionType
direction LR
StSys["StaticSystem:\n• Surface\n• Mesh"]
style StSys text-align:left
DynSys["DynamicSystem:\n• Rod\n&nbsp;&nbsp;• CosseratRod\n• RigidBody\n&nbsp;&nbsp;• Sphere\n&nbsp;&nbsp;• Cylinder"]
style DynSys text-align:left

subgraph Feature
direction LR
Forcing -->|add_forcing_to| Synchronize
Constraints -->|constrain| ConstrainValues
Constraints -->|constrain| ConstrainRates
Contact -->|detect_contact_between| Synchronize
Connection -->|connect| Synchronize
Damping -->|dampen| ConstrainRates
Callback -->|collect_diagnosis| CallbackGroup
end
end
Sys --> StSys --> Feature
Sys --> DynSys
DynSys --> Feature <--> St

```
4 changes: 4 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#'sphinx.ext.napoleon',
'sphinx.ext.viewcode',
'sphinx.ext.mathjax',
"sphinxcontrib.mermaid",
'numpydoc',
'myst_parser',
]
Expand Down Expand Up @@ -98,3 +99,6 @@

# -- Options for numpydoc ---------------------------------------------------
numpydoc_show_class_members = False

# -- Mermaid configuration ---------------------------------------------------
mermaid_params = ['--theme', 'neutral']
7 changes: 0 additions & 7 deletions elastica/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from collections import defaultdict
from elastica.rod.knot_theory import (
KnotTheory,
KnotTheoryCompatibleProtocol,
compute_link,
compute_twist,
compute_writhe,
Expand All @@ -19,8 +17,6 @@
GeneralConstraint,
FixedConstraint,
HelicalBucklingBC,
FreeRod,
OneEndFixedRod,
)
from elastica.external_forces import (
NoForces,
Expand All @@ -38,10 +34,8 @@
)
from elastica.joint import (
FreeJoint,
ExternalContact,
FixedJoint,
HingeJoint,
SelfContact,
)
from elastica.contact_forces import (
NoContact,
Expand Down Expand Up @@ -79,7 +73,6 @@
)
from elastica._linalg import levi_civita_tensor
from elastica.utils import isqrt
from elastica.typing import RodType, SystemType, AllowedContactType
from elastica.timestepper import (
integrate,
PositionVerlet,
Expand Down
53 changes: 27 additions & 26 deletions elastica/_calculus.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
__doc__ = """ Quadrature and difference kernels """
import numpy as np
from numpy import zeros, empty
from numpy.typing import NDArray
import numba
from numba import njit
from elastica.reset_functions_for_block_structure._reset_ghost_vector_or_scalar import (
_reset_vector_ghost,
)
import functools


@functools.lru_cache(maxsize=2)
def _get_zero_array(dim, ndim):
if ndim == 1:
return 0.0
if ndim == 2:
return np.zeros((dim, 1))


@njit(cache=True)
def _trapezoidal(array_collection):
@njit(cache=True) # type: ignore
def _trapezoidal(array_collection: NDArray[np.float64]) -> NDArray[np.float64]:
skim0119 marked this conversation as resolved.
Show resolved Hide resolved
"""
Simple trapezoidal quadrature rule with zero at end-points, in a dimension agnostic way

Expand Down Expand Up @@ -62,8 +55,10 @@ def _trapezoidal(array_collection):
return temp_collection


@njit(cache=True)
def _trapezoidal_for_block_structure(array_collection, ghost_idx):
@njit(cache=True) # type: ignore
def _trapezoidal_for_block_structure(
array_collection: NDArray[np.float64], ghost_idx: NDArray[np.int32]
) -> NDArray[np.float64]:
"""
Simple trapezoidal quadrature rule with zero at end-points, in a dimension agnostic way. This form
specifically for the block structure implementation and there is a reset function call, to reset
Expand Down Expand Up @@ -114,8 +109,10 @@ def _trapezoidal_for_block_structure(array_collection, ghost_idx):
return temp_collection


@njit(cache=True)
def _two_point_difference(array_collection):
@njit(cache=True) # type: ignore
def _two_point_difference(
array_collection: NDArray[np.float64],
) -> NDArray[np.float64]:
"""
This function does differentiation.

Expand Down Expand Up @@ -155,8 +152,10 @@ def _two_point_difference(array_collection):
return temp_collection


@njit(cache=True)
def _two_point_difference_for_block_structure(array_collection, ghost_idx):
@njit(cache=True) # type: ignore
def _two_point_difference_for_block_structure(
array_collection: NDArray[np.float64], ghost_idx: NDArray[np.int32]
) -> NDArray[np.float64]:
"""
This function does the differentiation, for Cosserat rod model equations. This form
specifically for the block structure implementation and there is a reset function call, to
Expand Down Expand Up @@ -206,8 +205,8 @@ def _two_point_difference_for_block_structure(array_collection, ghost_idx):
return temp_collection


@njit(cache=True)
def _difference(vector):
@njit(cache=True) # type: ignore
def _difference(vector: NDArray[np.float64]) -> NDArray[np.float64]:
"""
This function computes difference between elements of a batch vector.

Expand Down Expand Up @@ -237,8 +236,8 @@ def _difference(vector):
return output_vector


@njit(cache=True)
def _average(vector):
@njit(cache=True) # type: ignore
def _average(vector: NDArray[np.float64]) -> NDArray[np.float64]:
"""
This function computes the average between elements of a vector.

Expand Down Expand Up @@ -267,8 +266,10 @@ def _average(vector):
return output_vector


@njit(cache=True)
def _clip_array(input_array, vmin, vmax):
@njit(cache=True) # type: ignore
def _clip_array(
input_array: NDArray[np.float64], vmin: np.float64, vmax: np.float64
) -> NDArray[np.float64]:
"""
This function clips an array values
between user defined minimum and maximum
Expand Down Expand Up @@ -303,8 +304,8 @@ def _clip_array(input_array, vmin, vmax):
return input_array


@njit(cache=True)
def _isnan_check(array):
@njit(cache=True) # type: ignore
def _isnan_check(array: NDArray) -> bool:
"""
This function checks if there is any nan inside the array.
If there is nan, it returns True boolean.
Expand All @@ -324,7 +325,7 @@ def _isnan_check(array):
Python version: 2.24 µs ± 96.1 ns per loop
This version: 479 ns ± 6.49 ns per loop
"""
return np.isnan(array).any()
return bool(np.isnan(array).any())


position_difference_kernel = _difference
Expand Down
Loading
Loading