|
1 | | -import abc |
2 | | -from typing import Tuple |
3 | | - |
4 | 1 | import jax.numpy as jnp |
5 | 2 | from jax.tree_util import register_pytree_node_class |
6 | 3 |
|
7 | | - |
8 | | -class Material(abc.ABC): |
9 | | - """Base material class.""" |
10 | | - |
11 | | - _props: Tuple[str, ...] |
12 | | - |
13 | | - def __init__(self, material_properties): |
14 | | - """Initialize material properties. |
15 | | -
|
16 | | - Parameters |
17 | | - ---------- |
18 | | - material_properties: dict |
19 | | - A key-value map for various material properties. |
20 | | - """ |
21 | | - self.properties = material_properties |
22 | | - |
23 | | - # @abc.abstractmethod |
24 | | - def tree_flatten(self): |
25 | | - """Flatten this class as PyTree Node.""" |
26 | | - return (tuple(), self.properties) |
27 | | - |
28 | | - # @abc.abstractmethod |
29 | | - @classmethod |
30 | | - def tree_unflatten(cls, aux_data, children): |
31 | | - """Unflatten this class as PyTree Node.""" |
32 | | - del children |
33 | | - return cls(aux_data) |
34 | | - |
35 | | - @abc.abstractmethod |
36 | | - def __repr__(self): |
37 | | - """Repr for Material class.""" |
38 | | - ... |
39 | | - |
40 | | - @abc.abstractmethod |
41 | | - def compute_stress(self): |
42 | | - """Compute stress for the material.""" |
43 | | - ... |
44 | | - |
45 | | - def validate_props(self, material_properties): |
46 | | - for key in self._props: |
47 | | - if key not in material_properties: |
48 | | - raise KeyError( |
49 | | - f"'{key}' should be present in `material_properties` " |
50 | | - f"for {self.__class__.__name__} materials." |
51 | | - ) |
| 4 | +from ._base import _Material |
52 | 5 |
|
53 | 6 |
|
54 | 7 | @register_pytree_node_class |
55 | | -class LinearElastic(Material): |
| 8 | +class LinearElastic(_Material): |
56 | 9 | """Linear Elastic Material.""" |
57 | 10 |
|
58 | 11 | _props = ("density", "youngs_modulus", "poisson_ratio") |
@@ -114,18 +67,3 @@ def compute_stress(self, dstrain): |
114 | 67 | """Compute material stress.""" |
115 | 68 | dstress = self.de @ dstrain |
116 | 69 | return dstress |
117 | | - |
118 | | - |
119 | | -@register_pytree_node_class |
120 | | -class SimpleMaterial(Material): |
121 | | - _props = ("E", "density") |
122 | | - |
123 | | - def __init__(self, material_properties): |
124 | | - self.validate_props(material_properties) |
125 | | - self.properties = material_properties |
126 | | - |
127 | | - def __repr__(self): |
128 | | - return f"SimpleMaterial(props={self.properties})" |
129 | | - |
130 | | - def compute_stress(self, dstrain): |
131 | | - return dstrain * self.properties["E"] |
0 commit comments