zero-mech 0.1.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.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Henrik Finsberg
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,53 @@
1
+ Metadata-Version: 2.2
2
+ Name: zero_mech
3
+ Version: 0.1.0
4
+ Summary: A zero-dimensional mechanics solver
5
+ Author-email: Henrik Finsberg <henriknf@simula.no>
6
+ License: MIT
7
+ Project-URL: Homepage, https://github.com/ComputationalPhysiology/zero-mech
8
+ Keywords: mechanics,0D
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Programming Language :: Python :: 3
11
+ Requires-Python: >=3.10
12
+ Description-Content-Type: text/markdown
13
+ License-File: LICENSE
14
+ Requires-Dist: gotranx
15
+ Requires-Dist: numba
16
+ Requires-Dist: scipy
17
+ Requires-Dist: tqdm
18
+ Requires-Dist: numpy
19
+ Requires-Dist: matplotlib
20
+ Requires-Dist: sympy
21
+ Provides-Extra: dev
22
+ Requires-Dist: bump-my-version; extra == "dev"
23
+ Requires-Dist: ipython; extra == "dev"
24
+ Requires-Dist: pdbpp; extra == "dev"
25
+ Requires-Dist: pre-commit; extra == "dev"
26
+ Requires-Dist: twine; extra == "dev"
27
+ Requires-Dist: wheel; extra == "dev"
28
+ Provides-Extra: docs
29
+ Requires-Dist: jupyter-book; extra == "docs"
30
+ Requires-Dist: jupytext; extra == "docs"
31
+ Provides-Extra: test
32
+ Requires-Dist: pytest; extra == "test"
33
+ Requires-Dist: pytest-cov; extra == "test"
34
+
35
+ # ZeroMech - A 0D mechanics simulator
36
+
37
+ In this repo we implement simple 0D mechanics simulator.
38
+
39
+ ## Install
40
+ You can install the package with python
41
+ ```
42
+ python3 -m pip install zero-mech
43
+ ```
44
+
45
+ ## Quick start
46
+ Check of the [documentation](https://computationalphysiology.github.io/zero-mech/) for examples on how to use the simulator.
47
+
48
+
49
+ ## License
50
+ MIT
51
+
52
+ ## Author
53
+ - Henrik Finsberg
@@ -0,0 +1,19 @@
1
+ # ZeroMech - A 0D mechanics simulator
2
+
3
+ In this repo we implement simple 0D mechanics simulator.
4
+
5
+ ## Install
6
+ You can install the package with python
7
+ ```
8
+ python3 -m pip install zero-mech
9
+ ```
10
+
11
+ ## Quick start
12
+ Check of the [documentation](https://computationalphysiology.github.io/zero-mech/) for examples on how to use the simulator.
13
+
14
+
15
+ ## License
16
+ MIT
17
+
18
+ ## Author
19
+ - Henrik Finsberg
@@ -0,0 +1,151 @@
1
+ [build-system]
2
+ requires = ["setuptools>=61.2"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "zero_mech"
7
+ version = "0.1.0"
8
+ description = "A zero-dimensional mechanics solver"
9
+ authors = [{name = "Henrik Finsberg", email = "henriknf@simula.no"}]
10
+ license = {text = "MIT"}
11
+ classifiers = [
12
+ "License :: OSI Approved :: MIT License",
13
+ "Programming Language :: Python :: 3",
14
+ ]
15
+ keywords = ["mechanics", "0D"]
16
+ urls = {Homepage = "https://github.com/ComputationalPhysiology/zero-mech"}
17
+ requires-python = ">=3.10"
18
+ dependencies = [
19
+ "gotranx",
20
+ "numba",
21
+ "scipy",
22
+ "tqdm",
23
+ "numpy",
24
+ "matplotlib",
25
+ "sympy",
26
+ ]
27
+
28
+ [project.readme]
29
+ file = "README.md"
30
+ content-type = "text/markdown"
31
+
32
+ [project.optional-dependencies]
33
+ dev = [
34
+ "bump-my-version",
35
+ "ipython",
36
+ "pdbpp",
37
+ "pre-commit",
38
+ "twine",
39
+ "wheel",
40
+ ]
41
+ docs = [
42
+ "jupyter-book",
43
+ "jupytext",
44
+ ]
45
+ test = [
46
+ "pytest",
47
+ "pytest-cov",
48
+
49
+ ]
50
+
51
+ [project.scripts]
52
+ gotranx = "gotranx.cli:app"
53
+
54
+
55
+ [tool.setuptools]
56
+ include-package-data = true
57
+ package-dir = {"" = "src"}
58
+ zip-safe = false
59
+
60
+ [tool.setuptools.packages.find]
61
+ where = ["src"]
62
+ namespaces = false
63
+
64
+ [tool.setuptools.package-data]
65
+ "*" = ["*.lark"]
66
+
67
+ [tool.aliases]
68
+ test = "pytest"
69
+
70
+ [tool.pytest.ini_options]
71
+ testpaths = ["tests"]
72
+
73
+
74
+ [tool.mypy]
75
+ files = ["src/zero_mech", "tests"]
76
+ ignore_missing_imports = true
77
+ exclude = [
78
+ "docs",
79
+ "examples",
80
+ ]
81
+
82
+ [tool.coverage.run]
83
+ omit = ["src/zero_mech/__main__.py", "src/zero_mech/templates/__init__.py"]
84
+
85
+
86
+ [tool.ruff]
87
+
88
+ # Exclude a variety of commonly ignored directories.
89
+ exclude = [
90
+ "examples",
91
+ ".bzr",
92
+ ".direnv",
93
+ ".eggs",
94
+ ".git",
95
+ ".hg",
96
+ ".mypy_cache",
97
+ ".nox",
98
+ ".pants.d",
99
+ ".pytype",
100
+ ".ruff_cache",
101
+ ".svn",
102
+ ".tox",
103
+ ".venv",
104
+ "__pypackages__",
105
+ "_build",
106
+ "buck-out",
107
+ "build",
108
+ "dist",
109
+ "node_modules",
110
+ "venv",
111
+ ]
112
+
113
+ # Same as Black.
114
+ line-length = 100
115
+
116
+ # Assume Python 3.10.
117
+ target-version = "py310"
118
+
119
+ [tool.ruff.lint]
120
+ # Enable pycodestyle (`E`) and Pyflakes (`F`) codes by default.
121
+ select = ["E", "F"]
122
+ ignore = ["E402", "E741", "E743", "E731"]
123
+
124
+ # Allow autofix for all enabled rules (when `--fix`) is provided.
125
+ fixable = ["A", "B", "C", "D", "E", "F"]
126
+ unfixable = []
127
+
128
+ # Allow unused variables when underscore-prefixed.
129
+ dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"
130
+
131
+
132
+ [tool.ruff.lint.mccabe]
133
+ # Unlike Flake8, default to a complexity level of 10.
134
+ max-complexity = 10
135
+
136
+
137
+ [tool.bumpversion]
138
+ allow_dirty = false
139
+ commit = true
140
+ message = "Bump version: {current_version} → {new_version}"
141
+ tag = true
142
+ sign_tags = false
143
+ tag_name = "v{new_version}"
144
+ tag_message = "Bump version: {current_version} → {new_version}"
145
+ current_version = "0.1.0"
146
+
147
+
148
+ [[tool.bumpversion.files]]
149
+ filename = "pyproject.toml"
150
+ search = 'version = "{current_version}"'
151
+ replace = 'version = "{new_version}"'
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,10 @@
1
+ from . import experiments
2
+ from . import material
3
+ from . import compressibility
4
+ from . import model
5
+ from .model import Model
6
+ from . import active
7
+ from .active import ActiveStress
8
+
9
+
10
+ __all__ = ["experiments", "material", "compressibility", "model", "Model", "active", "ActiveStress"]
@@ -0,0 +1,54 @@
1
+ from dataclasses import dataclass, field
2
+ import sympy as sp
3
+
4
+ from .atoms import AbstractStrainEnergy
5
+
6
+
7
+ @dataclass(slots=True, frozen=True)
8
+ class ActiveStress(AbstractStrainEnergy):
9
+ Ta: sp.Symbol = sp.Symbol("Ta")
10
+ f0: sp.Matrix = field(default_factory=lambda: sp.Matrix([1, 0, 0]))
11
+
12
+ def strain_energy(self, F: sp.Matrix) -> sp.Expr:
13
+ f = F @ self.f0
14
+ I4f = f.T.dot(f)
15
+ return self.Ta * (I4f - 1)
16
+
17
+ def default_parameters(self):
18
+ return {self.Ta: 0.0}
19
+
20
+ @staticmethod
21
+ def str() -> str:
22
+ return "Ta * (I4f - 1)"
23
+
24
+
25
+ @dataclass(slots=True, frozen=True)
26
+ class ActiveStrain:
27
+ γ: sp.Symbol = sp.Symbol("γ")
28
+ f0: sp.Matrix = field(default_factory=lambda: sp.Matrix([1, 0, 0]))
29
+
30
+ def Fe(self, F: sp.Matrix) -> sp.Expr:
31
+ Fa = (1 + self.γ) * self.f0 @ self.f0.T + (1 + self.γ) ** (-0.5) * (
32
+ sp.eye(3) - self.f0 @ self.f0.T
33
+ )
34
+ return F * Fa.inv()
35
+
36
+ def default_parameters(self):
37
+ return {self.γ: 0.0}
38
+
39
+ @staticmethod
40
+ def str() -> str:
41
+ return "(1 + γ) * f0 @ f0.T + (1 + γ) ** (-0.5) * (eye(3) - f0 @ f0.T)"
42
+
43
+
44
+ @dataclass
45
+ class Passive(AbstractStrainEnergy):
46
+ def strain_energy(self, F: sp.Matrix) -> sp.Expr:
47
+ return sp.S.Zero
48
+
49
+ def default_parameters(self):
50
+ return {}
51
+
52
+ @staticmethod
53
+ def str() -> str:
54
+ return "0"
@@ -0,0 +1,39 @@
1
+ from typing import Any, Sequence
2
+ from abc import ABC, abstractmethod
3
+ import sympy as sp
4
+
5
+
6
+ class Atom(ABC):
7
+ def variables(self) -> dict[str, sp.Symbol]:
8
+ d = {}
9
+ if hasattr(self, "__slots__"):
10
+ slots: Sequence[str] = self.__slots__
11
+ items: list[tuple[str, Any]] = [(k, getattr(self, k)) for k in slots]
12
+ else:
13
+ items = list(self.__dict__.items())
14
+
15
+ for k, v in items:
16
+ if isinstance(v, sp.Symbol):
17
+ d[v.name] = v
18
+
19
+ if isinstance(v, Atom):
20
+ d.update(v.variables())
21
+ return d
22
+
23
+ def __getitem__(self, key):
24
+ return self.variables()[key]
25
+
26
+ def __contains__(self, key):
27
+ return key in self.variables()
28
+
29
+
30
+ class AbstractStrainEnergy(Atom, ABC):
31
+ @abstractmethod
32
+ def strain_energy(self, F: sp.Matrix) -> sp.Expr: ...
33
+
34
+ @abstractmethod
35
+ def default_parameters(self): ...
36
+
37
+ @staticmethod
38
+ @abstractmethod
39
+ def str() -> str: ...
@@ -0,0 +1,49 @@
1
+ from abc import abstractmethod
2
+ from dataclasses import dataclass
3
+ import sympy as sp
4
+
5
+
6
+ from .atoms import AbstractStrainEnergy
7
+
8
+
9
+ class Compressibility(AbstractStrainEnergy):
10
+ @abstractmethod
11
+ def is_compressible(self) -> bool: ...
12
+
13
+
14
+ @dataclass(slots=True, frozen=True)
15
+ class Incompressible(Compressibility):
16
+ p: sp.Symbol = sp.Symbol("p")
17
+
18
+ def is_compressible(self) -> bool:
19
+ return False
20
+
21
+ def strain_energy(self, F: sp.Matrix) -> sp.Expr:
22
+ J = F.det()
23
+ return self.p * (J - 1)
24
+
25
+ def default_parameters(self):
26
+ return {}
27
+
28
+ @staticmethod
29
+ def str() -> str:
30
+ return "p * (J - 1)"
31
+
32
+
33
+ @dataclass(slots=True, frozen=True)
34
+ class Compressible1(Compressibility):
35
+ κ: sp.Symbol = sp.Symbol("κ")
36
+
37
+ def strain_energy(self, F: sp.Matrix) -> sp.Expr:
38
+ J = F.det()
39
+ return self.κ / 2 * (J - 1) ** 2
40
+
41
+ def is_compressible(self) -> bool:
42
+ return True
43
+
44
+ def default_parameters(self):
45
+ return {self.κ: 1e3}
46
+
47
+ @staticmethod
48
+ def str() -> str:
49
+ return "κ / 2 * (J - 1) ** 2"
@@ -0,0 +1,141 @@
1
+ from dataclasses import dataclass
2
+ from typing import Literal
3
+ import sympy as sp
4
+
5
+
6
+ from .atoms import Atom
7
+
8
+
9
+ def I1(C: sp.Matrix) -> sp.Expr:
10
+ return sp.trace(C)
11
+
12
+
13
+ def I2(C: sp.Matrix) -> sp.Expr:
14
+ return 0.5 * (I1(C) ** 2 - sp.trace(C**2))
15
+
16
+
17
+ def I3(C: sp.Matrix) -> sp.Expr:
18
+ return C.det()
19
+
20
+
21
+ def I4a(F: sp.Matrix, a: sp.Matrix) -> sp.Expr:
22
+ return a.T @ (F.T @ F) @ a
23
+
24
+
25
+ def I8ab(F: sp.Matrix, a: sp.Matrix, b: sp.Matrix) -> sp.Expr:
26
+ return a.T @ (F.T @ F) @ b
27
+
28
+
29
+ @dataclass(slots=True, frozen=True)
30
+ class MechanicalExperiment(Atom):
31
+ F11: sp.Expr = sp.S.One
32
+ F22: sp.Expr = sp.S.One
33
+ F33: sp.Expr = sp.S.One
34
+ F12: sp.Expr = sp.S.Zero
35
+ F13: sp.Expr = sp.S.Zero
36
+ F21: sp.Expr = sp.S.Zero
37
+ F23: sp.Expr = sp.S.Zero
38
+ F31: sp.Expr = sp.S.Zero
39
+ F32: sp.Expr = sp.S.Zero
40
+
41
+ @property
42
+ def F(self) -> sp.Matrix:
43
+ return sp.Matrix(
44
+ [
45
+ [self.F11, self.F12, self.F13],
46
+ [self.F21, self.F22, self.F23],
47
+ [self.F31, self.F32, self.F33],
48
+ ]
49
+ )
50
+
51
+ @property
52
+ def F_inv(self) -> sp.Matrix:
53
+ return self.F.inv()
54
+
55
+ @property
56
+ def J(self) -> sp.Expr:
57
+ return self.F.det()
58
+
59
+ @property
60
+ def C(self) -> sp.Matrix:
61
+ return self.F.T * self.F
62
+
63
+ @property
64
+ def B(self) -> sp.Matrix:
65
+ return self.F @ self.F.T
66
+
67
+ @property
68
+ def E(self) -> sp.Matrix:
69
+ return 0.5 * (self.C - sp.eye(3))
70
+
71
+ @property
72
+ def I1(self) -> sp.Expr:
73
+ return sp.trace(self.C)
74
+
75
+ @property
76
+ def I2(self) -> sp.Expr:
77
+ return 0.5 * (self.I1**2 - sp.trace(self.C**2))
78
+
79
+ @property
80
+ def I3(self) -> sp.Expr:
81
+ return self.C.det()
82
+
83
+ def principal_stretches(self) -> list[sp.Expr]:
84
+ return [sp.sqrt(s) for s in self.C.eigenvals(multiple=True)]
85
+
86
+
87
+ def simple_shear(plane: Literal["fs", "sn", "fn", "sf", "ns", "nf"] = "fs") -> MechanicalExperiment:
88
+ gamma = sp.Symbol("gamma", real=True, postive=True)
89
+ if plane == "fs":
90
+ return MechanicalExperiment(F21=gamma)
91
+ elif plane == "fn":
92
+ return MechanicalExperiment(F31=gamma)
93
+ elif plane == "sn":
94
+ return MechanicalExperiment(F32=gamma)
95
+ elif plane == "sf":
96
+ return MechanicalExperiment(F12=gamma)
97
+ elif plane == "nf":
98
+ return MechanicalExperiment(F13=gamma)
99
+ elif plane == "ns":
100
+ return MechanicalExperiment(F23=gamma)
101
+ else:
102
+ raise ValueError(f"plane must be fs, sn, fn, sf, ns, or nf, got {plane}")
103
+
104
+
105
+ def uniaxial_tension(axis: int = 0) -> MechanicalExperiment:
106
+ lmbda = sp.Symbol("λ", real=True, positive=True)
107
+
108
+ F_tension = lmbda
109
+ F_trans = 1.0 / sp.sqrt(lmbda)
110
+
111
+ if axis == 0:
112
+ F11 = F_tension
113
+ F22 = F33 = F_trans
114
+ elif axis == 1:
115
+ F22 = F_tension
116
+ F11 = F33 = F_trans
117
+ elif axis == 2:
118
+ F33 = F_tension
119
+ F11 = F22 = F_trans
120
+ else:
121
+ raise ValueError("axis must be 0, 1, or 2")
122
+
123
+ return MechanicalExperiment(F11=F11, F22=F22, F33=F33)
124
+
125
+
126
+ def full_matrix() -> MechanicalExperiment:
127
+ """Return a MechanicalExperiment with all components
128
+ initialized as symbols.
129
+ """
130
+ F11, F22, F33, F12, F13, F21, F23, F31, F32 = sp.symbols("F11 F22 F33 F12 F13 F21 F23 F31 F32")
131
+ return MechanicalExperiment(
132
+ F11=F11,
133
+ F22=F22,
134
+ F33=F33,
135
+ F12=F12,
136
+ F13=F13,
137
+ F21=F21,
138
+ F23=F23,
139
+ F31=F31,
140
+ F32=F32,
141
+ )
@@ -0,0 +1,91 @@
1
+ import logging
2
+ from dataclasses import dataclass, field
3
+ import sympy as sp
4
+
5
+ from .atoms import AbstractStrainEnergy
6
+
7
+ logger = logging.getLogger(__name__)
8
+
9
+
10
+ @dataclass(slots=True, frozen=True)
11
+ class NeoHookean(AbstractStrainEnergy):
12
+ μ: sp.Symbol = sp.Symbol("μ")
13
+
14
+ def strain_energy(self, F: sp.Matrix) -> sp.Expr:
15
+ C = F.T @ F
16
+ I1 = sp.trace(C)
17
+ return self.μ / 2 * (I1 - 3)
18
+
19
+ def default_parameters(self):
20
+ return {self.μ: 15.0}
21
+
22
+ @staticmethod
23
+ def str() -> str:
24
+ return "μ / 2 * (I1 - 3)"
25
+
26
+
27
+ # class LinearElastic(AbstractStrainEnergy):
28
+ # mu: sp.Symbol = sp.Symbol("mu")
29
+ # lmbda: sp.Symbol = sp.Symbol("lmbda")
30
+
31
+ # def strain_energy(self, F: sp.Matrix) -> sp.Expr:
32
+ # C = F.T @ F
33
+ # I1 = sp.trace(C)
34
+
35
+
36
+ @dataclass(slots=True, frozen=True)
37
+ class HolzapfelOgden(AbstractStrainEnergy):
38
+ a: sp.Symbol = sp.Symbol("a")
39
+ b: sp.Symbol = sp.Symbol("b")
40
+ a_f: sp.Symbol = sp.Symbol("a_f")
41
+ b_f: sp.Symbol = sp.Symbol("b_f")
42
+ a_s: sp.Symbol = sp.Symbol("a_s")
43
+ b_s: sp.Symbol = sp.Symbol("b_s")
44
+ a_fs: sp.Symbol = sp.Symbol("a_fs")
45
+ b_fs: sp.Symbol = sp.Symbol("b_fs")
46
+ f0: sp.Matrix = field(default_factory=lambda: sp.Matrix([1, 0, 0]))
47
+ s0: sp.Matrix = field(default_factory=lambda: sp.Matrix([0, 1, 0]))
48
+ use_heaviside: bool = False
49
+
50
+ def strain_energy(self, F: sp.Matrix) -> sp.Expr:
51
+ C = F.T @ F
52
+
53
+ I1 = sp.trace(C)
54
+ logger.debug(f"I1: {I1}")
55
+ I4f = self.f0.dot(C * self.f0)
56
+ logger.debug(f"I4f: {I4f}")
57
+ I4s = self.s0.dot(C * self.s0)
58
+ logger.debug(f"I4s: {I4s}")
59
+ I8fs = self.f0.dot(C * self.s0)
60
+ logger.debug(f"I8fs: {I8fs}")
61
+
62
+ I4fm1 = I4f - 1 if not self.use_heaviside else sp.Piecewise((0.0, I4f < 1), (I4f - 1, True))
63
+ I4sm1 = I4s - 1 if not self.use_heaviside else sp.Piecewise((0.0, I4s < 1), (I4s - 1, True))
64
+
65
+ return (
66
+ (self.a / (2 * self.b)) * (sp.exp(self.b * (I1 - 3)) - 1)
67
+ + (self.a_f / (2 * self.b_f)) * (sp.exp(self.b_f * I4fm1**2) - 1)
68
+ + (self.a_s / (2 * self.b_s)) * (sp.exp(self.b_s * I4sm1**2) - 1)
69
+ + (self.a_fs / (2 * self.b_fs)) * (sp.exp(self.b_fs * I8fs**2) - 1)
70
+ )
71
+
72
+ def default_parameters(self):
73
+ return {
74
+ self.a: 0.059,
75
+ self.b: 8.023,
76
+ self.a_f: 18.472,
77
+ self.b_f: 16.026,
78
+ self.a_s: 2.481,
79
+ self.b_s: 11.120,
80
+ self.a_fs: 0.216,
81
+ self.b_fs: 11.436,
82
+ }
83
+
84
+ @staticmethod
85
+ def str() -> str:
86
+ return (
87
+ "(a / (2 * b)) * (exp(b * (I1 - 3)) - 1) + "
88
+ "(a_f / (2 * b_f)) * (exp(b_f * H(I4f - 1)**2) - 1) + "
89
+ "(a_s / (2 * b_s)) * (exp(b_s * H(I4f - 1)**2) - 1) + "
90
+ "(a_fs / (2 * b_fs)) * (exp(b_fs * I8fs**2) - 1)"
91
+ )
@@ -0,0 +1,72 @@
1
+ from dataclasses import dataclass, field
2
+ from typing import Protocol
3
+ import sympy as sp
4
+
5
+
6
+ from .experiments import full_matrix
7
+ from .atoms import Atom
8
+
9
+ from .active import Passive
10
+ from .compressibility import Incompressible
11
+ from .material import NeoHookean
12
+
13
+
14
+ class StrainEnergy(Protocol):
15
+ def strain_energy(self, F: sp.Matrix) -> sp.Expr: ...
16
+
17
+
18
+ class Compressibility(StrainEnergy, Protocol):
19
+ def is_compressible(self) -> bool: ...
20
+
21
+
22
+ @dataclass(slots=True, frozen=True)
23
+ class Model(Atom):
24
+ material: StrainEnergy = field(default_factory=NeoHookean)
25
+ compressibility: Compressibility = field(default_factory=Incompressible)
26
+ active: StrainEnergy = field(default_factory=Passive)
27
+
28
+ _full: sp.Matrix = field(default_factory=full_matrix, init=False, repr=False)
29
+
30
+ def _subs(self, A, F):
31
+ return A.subs(
32
+ {
33
+ self._full.F[0, 0]: F[0, 0],
34
+ self._full.F[1, 1]: F[1, 1],
35
+ self._full.F[2, 2]: F[2, 2],
36
+ self._full.F[0, 1]: F[0, 1],
37
+ self._full.F[0, 2]: F[0, 2],
38
+ self._full.F[1, 0]: F[1, 0],
39
+ self._full.F[1, 2]: F[1, 2],
40
+ self._full.F[2, 0]: F[2, 0],
41
+ self._full.F[2, 1]: F[2, 1],
42
+ }
43
+ )
44
+
45
+ @property
46
+ def _strain_energy(self):
47
+ F = self._full.F
48
+ if self.compressibility.is_compressible():
49
+ J = F.det()
50
+ F_dev = F * J ** (-1 / 3)
51
+ else:
52
+ F_dev = F
53
+ return (
54
+ self.material.strain_energy(F_dev)
55
+ + self.compressibility.strain_energy(F)
56
+ + self.active.strain_energy(F)
57
+ )
58
+
59
+ def strain_energy(self, F):
60
+ return self._subs(self._strain_energy, F)
61
+
62
+ def first_piola_kirchhoff(self, F):
63
+ return self._subs(sp.diff(self._strain_energy, self._full.F), F)
64
+
65
+ def cauchy_stress(self, F):
66
+ J = F.det()
67
+ P = self.first_piola_kirchhoff(F)
68
+ return P @ F.T / J
69
+
70
+ def second_piola_kirchhoff(self, F):
71
+ P = self.first_piola_kirchhoff(F)
72
+ return F.inv() @ P
@@ -0,0 +1,10 @@
1
+ from dataclasses import dataclass
2
+ import sympy as sp
3
+
4
+
5
+ @dataclass
6
+ class KelvinVoigt:
7
+ η: sp.Symbol = sp.Symbol("η")
8
+
9
+ def viscosity(self, F: sp.Matrix) -> sp.Expr:
10
+ return self.η * F.norm()
@@ -0,0 +1,53 @@
1
+ Metadata-Version: 2.2
2
+ Name: zero_mech
3
+ Version: 0.1.0
4
+ Summary: A zero-dimensional mechanics solver
5
+ Author-email: Henrik Finsberg <henriknf@simula.no>
6
+ License: MIT
7
+ Project-URL: Homepage, https://github.com/ComputationalPhysiology/zero-mech
8
+ Keywords: mechanics,0D
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Programming Language :: Python :: 3
11
+ Requires-Python: >=3.10
12
+ Description-Content-Type: text/markdown
13
+ License-File: LICENSE
14
+ Requires-Dist: gotranx
15
+ Requires-Dist: numba
16
+ Requires-Dist: scipy
17
+ Requires-Dist: tqdm
18
+ Requires-Dist: numpy
19
+ Requires-Dist: matplotlib
20
+ Requires-Dist: sympy
21
+ Provides-Extra: dev
22
+ Requires-Dist: bump-my-version; extra == "dev"
23
+ Requires-Dist: ipython; extra == "dev"
24
+ Requires-Dist: pdbpp; extra == "dev"
25
+ Requires-Dist: pre-commit; extra == "dev"
26
+ Requires-Dist: twine; extra == "dev"
27
+ Requires-Dist: wheel; extra == "dev"
28
+ Provides-Extra: docs
29
+ Requires-Dist: jupyter-book; extra == "docs"
30
+ Requires-Dist: jupytext; extra == "docs"
31
+ Provides-Extra: test
32
+ Requires-Dist: pytest; extra == "test"
33
+ Requires-Dist: pytest-cov; extra == "test"
34
+
35
+ # ZeroMech - A 0D mechanics simulator
36
+
37
+ In this repo we implement simple 0D mechanics simulator.
38
+
39
+ ## Install
40
+ You can install the package with python
41
+ ```
42
+ python3 -m pip install zero-mech
43
+ ```
44
+
45
+ ## Quick start
46
+ Check of the [documentation](https://computationalphysiology.github.io/zero-mech/) for examples on how to use the simulator.
47
+
48
+
49
+ ## License
50
+ MIT
51
+
52
+ ## Author
53
+ - Henrik Finsberg
@@ -0,0 +1,20 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ src/zero_mech/__init__.py
5
+ src/zero_mech/active.py
6
+ src/zero_mech/atoms.py
7
+ src/zero_mech/compressibility.py
8
+ src/zero_mech/experiments.py
9
+ src/zero_mech/material.py
10
+ src/zero_mech/model.py
11
+ src/zero_mech/viscosity.py
12
+ src/zero_mech.egg-info/PKG-INFO
13
+ src/zero_mech.egg-info/SOURCES.txt
14
+ src/zero_mech.egg-info/dependency_links.txt
15
+ src/zero_mech.egg-info/entry_points.txt
16
+ src/zero_mech.egg-info/not-zip-safe
17
+ src/zero_mech.egg-info/requires.txt
18
+ src/zero_mech.egg-info/top_level.txt
19
+ tests/test_experiments.py
20
+ tests/test_models.py
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ gotranx = gotranx.cli:app
@@ -0,0 +1,23 @@
1
+ gotranx
2
+ numba
3
+ scipy
4
+ tqdm
5
+ numpy
6
+ matplotlib
7
+ sympy
8
+
9
+ [dev]
10
+ bump-my-version
11
+ ipython
12
+ pdbpp
13
+ pre-commit
14
+ twine
15
+ wheel
16
+
17
+ [docs]
18
+ jupyter-book
19
+ jupytext
20
+
21
+ [test]
22
+ pytest
23
+ pytest-cov
@@ -0,0 +1 @@
1
+ zero_mech
@@ -0,0 +1,104 @@
1
+ import itertools as it
2
+ import sympy as sp
3
+
4
+ import pytest
5
+ import zero_mech
6
+
7
+
8
+ def equal(a, b):
9
+ return sp.simplify(a - b) == 0
10
+
11
+
12
+ @pytest.mark.parametrize("axis", range(3))
13
+ def test_uniaxial_tension(axis):
14
+ xx = axis
15
+ yy = (axis + 1) % 3
16
+ zz = (axis + 2) % 3
17
+
18
+ experiment = zero_mech.experiments.uniaxial_tension(axis=axis)
19
+ mat = zero_mech.material.NeoHookean()
20
+ comp = zero_mech.compressibility.Incompressible()
21
+ act = zero_mech.active.Passive()
22
+ model = zero_mech.Model(material=mat, compressibility=comp, active=act)
23
+
24
+ lmbda = experiment["λ"]
25
+
26
+ principal_stretches = experiment.principal_stretches()
27
+ assert len(principal_stretches) == 3
28
+ assert equal(principal_stretches[xx], lmbda)
29
+ assert equal(principal_stretches[yy], 1 / sp.sqrt(lmbda))
30
+ assert equal(principal_stretches[zz], 1 / sp.sqrt(lmbda))
31
+
32
+ mu = model["μ"]
33
+ p = model["p"]
34
+
35
+ P = model.first_piola_kirchhoff(experiment.F)
36
+
37
+ assert equal(P[xx, xx], lmbda * mu + p / lmbda)
38
+ assert equal(P[yy, yy], sp.sqrt(lmbda) * p + mu / sp.sqrt(lmbda))
39
+ assert equal(P[zz, zz], sp.sqrt(lmbda) * p + mu / sp.sqrt(lmbda))
40
+ for i, j in ((x, y) for (x, y) in it.product(range(3), repeat=2) if x != y):
41
+ assert sp.Eq(P[i, j], 0)
42
+
43
+ T = model.cauchy_stress(experiment.F)
44
+ assert equal(T[xx, xx], lmbda * P[xx, xx])
45
+ assert equal(T[yy, yy], P[yy, yy] / sp.sqrt(lmbda))
46
+ assert equal(T[zz, zz], P[zz, zz] / sp.sqrt(lmbda))
47
+ for i, j in ((x, y) for (x, y) in it.product(range(3), repeat=2) if x != y):
48
+ assert sp.Eq(T[i, j], 0)
49
+
50
+ S = model.second_piola_kirchhoff(experiment.F)
51
+ assert equal(S[xx, xx], P[xx, xx] / lmbda)
52
+ assert equal(S[yy, yy], P[yy, yy] * sp.sqrt(lmbda))
53
+ assert equal(S[zz, zz], P[zz, zz] * sp.sqrt(lmbda))
54
+ for i, j in ((x, y) for (x, y) in it.product(range(3), repeat=2) if x != y):
55
+ assert sp.Eq(S[i, j], 0)
56
+
57
+
58
+ @pytest.mark.parametrize("axis", range(3))
59
+ def test_active_uniaxial_tension(axis):
60
+ xx = axis
61
+ yy = (axis + 1) % 3
62
+ zz = (axis + 2) % 3
63
+
64
+ f0 = sp.Matrix([0, 0, 0])
65
+ f0[axis] = 1
66
+
67
+ experiment = zero_mech.experiments.uniaxial_tension(axis=axis)
68
+ mat = zero_mech.material.NeoHookean()
69
+ comp = zero_mech.compressibility.Incompressible()
70
+ act = zero_mech.active.ActiveStress(f0=f0)
71
+ model = zero_mech.Model(material=mat, compressibility=comp, active=act)
72
+
73
+ lmbda = experiment["λ"]
74
+
75
+ principal_stretches = experiment.principal_stretches()
76
+ assert len(principal_stretches) == 3
77
+ assert equal(principal_stretches[xx], lmbda)
78
+ assert equal(principal_stretches[yy], 1 / sp.sqrt(lmbda))
79
+ assert equal(principal_stretches[zz], 1 / sp.sqrt(lmbda))
80
+
81
+ mu = model["μ"]
82
+ Ta = model["Ta"]
83
+ p = model["p"]
84
+
85
+ P = model.first_piola_kirchhoff(experiment.F)
86
+ assert equal(P[xx, xx], 2 * Ta * lmbda + lmbda * mu + p / lmbda)
87
+ assert equal(P[yy, yy], sp.sqrt(lmbda) * p + mu / sp.sqrt(lmbda))
88
+ assert equal(P[zz, zz], sp.sqrt(lmbda) * p + mu / sp.sqrt(lmbda))
89
+ for i, j in ((x, y) for (x, y) in it.product(range(3), repeat=2) if x != y):
90
+ assert sp.Eq(P[i, j], 0)
91
+
92
+ T = model.cauchy_stress(experiment.F)
93
+ assert equal(T[xx, xx], lmbda * P[xx, xx])
94
+ assert equal(T[yy, yy], P[yy, yy] / sp.sqrt(lmbda))
95
+ assert equal(T[zz, zz], P[zz, zz] / sp.sqrt(lmbda))
96
+ for i, j in ((x, y) for (x, y) in it.product(range(3), repeat=2) if x != y):
97
+ assert sp.Eq(T[i, j], 0)
98
+
99
+ S = model.second_piola_kirchhoff(experiment.F)
100
+ assert equal(S[xx, xx], P[xx, xx] / lmbda)
101
+ assert equal(S[yy, yy], P[yy, yy] * sp.sqrt(lmbda))
102
+ assert equal(S[zz, zz], P[zz, zz] * sp.sqrt(lmbda))
103
+ for i, j in ((x, y) for (x, y) in it.product(range(3), repeat=2) if x != y):
104
+ assert sp.Eq(S[i, j], 0)
@@ -0,0 +1,12 @@
1
+ import zero_mech
2
+
3
+
4
+ def test_model():
5
+ material = zero_mech.material.NeoHookean()
6
+ mech_model = zero_mech.Model(
7
+ material=material,
8
+ compressibility=zero_mech.compressibility.Compressible1(),
9
+ active=zero_mech.active.Passive(),
10
+ )
11
+ print(mech_model.variables())
12
+ breakpoint()