-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcore.py
More file actions
478 lines (356 loc) · 14.5 KB
/
Copy pathcore.py
File metadata and controls
478 lines (356 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
import dataclasses
from copy import copy
from dataclasses import dataclass
from enum import Enum
from operator import itemgetter
from typing import Hashable, Iterable, NewType, Union, Optional
from warnings import warn
import numpy as np
# Set tolerances
_DEFAULT_TOL = 10 * np.finfo(float).eps
CONSTANT_R = 8.31446261815324 # J/mol·K
CONSTANT_F = 96485.33212 # C/mol
ZeroIdxID = NewType("ZeroIdxID", int)
OneIdxID = NewType("OneIdxID", int)
NodeID = NewType("NodeID", ZeroIdxID)
def _validate_dof(dof, body=False):
allowed_dofs = ["x1", "x2", "x3", "fluid", "temperature", "charge"]
if body:
allowed_dofs += ["α1", "α2", "α3"]
if not dof in allowed_dofs:
msg = f"{dof} is not a supported axis type. The supported degrees of freedom are {','.join(allowed_dofs)}."
raise ValueError(msg)
class NodeSet(frozenset):
"""Set of node IDs
Recall that in Waffleiron, node IDs are zero-indexed.
"""
# TODO: Set operations should return a NodeSet
class FaceSet(frozenset):
"""Set of facets (tuples of node IDs)"""
# A FaceSet stores tuples of node IDs because facets are not represented elsewhere
# by IDs or objects, so the FaceSet must contain all the information to define each
# facet.
class ElementSet(frozenset):
"""Set of element IDs."""
class ImplicitBody:
"""A geometric body defined by its interface with a mesh."""
# TODO: Get rid of material; it's an FEBio-ism
def __init__(self, mesh, interface: NodeSet, material=None):
"""Constructor for ImplicitBody object.
interface := a collection of node ids
At some point in the future, support may be added to define an interface
using a list of faces.
"""
# NameRegistry needs the interface node set to be hashable
if not isinstance(interface, NodeSet):
interface = NodeSet(interface)
self.mesh = mesh
self.interface = interface
self.material = material
class RigidInterface:
"""A rigid constraint between a rigid body and a node set."""
# Future enhancement: There is no particular reason that a rigid
# interface should be restricted to an explicit rigid body and a
# node set. Two node sets work fine; just create an implicit rigid
# body for one of them.
# TODO: What's the difference between a rigid interface with no associated explicit
# rigid body and an implicit rigid body?
def __init__(self, rigid_body, node_set):
self.rigid_body = rigid_body
self.node_set = node_set
class Body:
"""A geometric body of elements.
Explicit rigid bodies should be constructed using this type.
"""
def __init__(self, elements: Iterable, center_of_mass=None):
self.elements: Sequence = elements
# self.master_inode = elements[0].ids[0]
# TODO: calculate center of mass by volume averaging. Requires that Element
# support integration in space, not just its natural basis.
if center_of_mass is not None:
self.center_of_mass = center_of_mass
else:
M = np.array([0, 0, 0])
total_volume = 0
for e in self.elements:
c = e.centroid()
volume = e.integrate(lambda e, r: 1.0)
M = M + volume * c
total_volume += volume
self.center_of_mass = M / total_volume
def nodes(self):
"""Return (node_ids, xnodes) for this body."""
nids = set()
for e in self.elements:
nids.update(e.ids)
nids = np.array([i for i in nids])
parent_mesh = next(iter(self.elements)).mesh
# ^ assumption: all elements are from same mesh
xnodes = np.array([parent_mesh.nodes[i] for i in nids])
return nids, xnodes
@dataclass(eq=False)
class ContactConstraint(object):
"""A constraint defining contact between two surfaces."""
# Only include parameters here if they are supported by all contact algorithms,
# with the same default values.
leader: FaceSet
follower: FaceSet
two_pass: bool = False
penalty_factor: float = 1 # penalty in FEBio XML
auto_penalty: bool = False
use_augmented_lagrange: bool = False # laugon in FEBio XML
augmented_lagrange_rtol: float = 0.1 # tolerance in FEBio XML
augmented_lagrange_gapnorm_atol: Optional[float] = None # gaptol in FEBio XML
projection_tol: float = 0.01 # search_tol in FEBio XML
@property
def values(self):
return {
f.name: getattr(self, f.name)
for f in dataclasses.fields(self)
if f.name not in ("leader", "follower")
}
def __init__(self):
raise NotImplementedError(
f"{self.__class__} is a base class for specific contact implementations."
)
def __post_init__(self):
if not isinstance(self.leader, FaceSet):
self.leader = FaceSet(self.leader)
if not isinstance(self.follower, FaceSet):
self.follower = FaceSet(self.follower)
@dataclass(init=True, eq=False)
class ContactSlidingNodeOnFacet(ContactConstraint):
"""Sliding node on facet (N2F) contact.
'sliding-node-on-facet' in FEBio XML
"""
augmented_lagrange_minaug: int = 0
augmented_lagrange_maxaug: int = 10
max_segment_updates: Optional[int] = None
friction_coefficient: Optional[float] = None
friction_penalty: Optional[float] = None
# tangential_stiffness_scale not supported in FEBio 4.3.0, contrary to docs
@dataclass(init=True, eq=False)
class ContactSlidingFacetOnFacet(ContactConstraint):
"""Sliding facet on facet (F2F) contact.
'sliding-facet-on-facet' in FEBio XML.
"""
update_penalty: bool = False
augmented_lagrange_minaug: int = 0
augmented_lagrange_maxaug: int = 10
smoothed_lagrangian: bool = False
search_scale: float = 1.0
@dataclass(init=True, eq=False)
class ContactSlidingElastic(ContactConstraint):
"""Sliding node on facet (N2F) contact.
'sliding-node-on-facet' in FEBio XML
"""
update_penalty: bool = False
augmented_lagrange_minaug: int = 0
augmented_lagrange_maxaug: int = 10
smoothed_lagrangian: bool = False
symmetric_stiffness: bool = False
max_segment_updates: Optional[int] = None
search_scale: float = 1.0
friction_coefficient: Optional[float] = None
tension: bool = False
# tangential_stiffness_scale not supported in FEBio 4.3.0, contrary to docs
@dataclass(init=True, eq=False)
class ContactTiedElastic(ContactConstraint):
"""Tied elastic contact
'tied-elastic' in FEBio XML.
"""
augmented_lagrange_minaug: int = 0
augmented_lagrange_maxaug: int = 10
# pressure_penalty_factor: float = 1.0
# ^ only recognized by FEBio 4, so I guess treat it as FEBio XML ≥ 4 only?
symmetric_stiffness: bool = False
search_scale: float = 1.0
def _canonical_face(face):
"""Return the canonical face tuple.
The canonical face tuple is the ordering of the face nodes such
that the lowest node id is first. To achieve this ordering, only
rotational shifts are allowed.
"""
i, inode = min(enumerate(face), key=itemgetter(1))
face = tuple(face[i:] + face[:i])
return face
def _e_bb(elements):
"""Create bounding box array from element list."""
bb_max = np.vstack([np.max(e.nodes, axis=0) for e in elements])
bb_min = np.vstack([np.min(e.nodes, axis=0) for e in elements])
bb = (bb_min, bb_max)
return bb
class Interpolant(Enum):
STEP = "step"
LINEAR = "linear"
SPLINE = "spline"
class Extrapolant(Enum):
CONSTANT = "constant"
LINEAR = "linear"
REPEAT = "repeat"
REPEAT_CONTINUOUS = "repeat_continuous"
class Sequence:
"""A basic time-varying sequence.
Defined by control points + interpolation method + extrapolation
method.
"""
def __init__(
self,
seq,
interp: Union[Interpolant, str],
extrap: Union[Extrapolant, str],
steplocal: bool = True,
):
# Input checking. Technically, mypy should detect this, but
# leave it in for now as mypy is not that widespread
# (2021-02-09).
if isinstance(interp, str):
interp = Interpolant(interp)
if not isinstance(interp, Interpolant):
raise ValueError(
f"Function argument `interp` has value `{interp}` with type `{type(interp)}`, but must have type `{Interpolant}`."
)
if isinstance(extrap, str):
extrap = Extrapolant(extrap)
if not isinstance(extrap, Extrapolant):
raise ValueError(
f"Function argument `extrap` has value `{extrap}` with type `{type(extrap)}`, but must have type `{Extrapolant}`."
)
# Parameters
self.points = seq
self.interpolant = interp
self.extrapolant = extrap
self.steplocal = steplocal
# TODO: Make ScaledSequence a Sequence
class ScaledSequence:
"""A time-varying sequence proportional to another sequence.
Defined by a scaling factor and base sequence. Since multiple
objects may reference the same base sequence, ScaledSequence does
not provide functions to modify the base sequence.
"""
def __init__(self, sequence: Sequence, scale: float):
self.scale = scale
self.sequence = sequence
@property
def points(self):
return self.sequence.points
@property
def interpolant(self):
return self.sequence.interpolant
@property
def extrapolant(self):
return self.sequence.extrapolant
class NameRegistry:
"""Mapping between names and objects.
Provides `name + nametype → object` for all of an object's names and
`object → (names + nametype)s`. An object may have multiple names
of the same nametype.
"""
# TODO: It's very inconvenient to view all the names. One of the functions
# should list every name in every namespace.
def __init__(self):
# Don't use defaultdict because we don't want to create slots accidentally
self._from_name = {} # dict: nametype → (dict: name → obj)
self._from_name["canonical"] = {} # dict: name → obj
self._from_object = {} # dict: obj → (dict: nametype → set of names)
def __repr__(self):
return str(self._from_name)
def __getitem__(self, namespace):
return self.get_namespace(namespace)
def add(self, name, obj, nametype="canonical"):
"""Add a name for an object."""
# Add the name to the name → object map
if nametype not in self._from_name:
self._from_name[nametype] = {}
self._from_name[nametype][name] = obj
# Add the name to the object → names map
if obj not in self._from_object:
self._from_object[obj] = {}
if nametype not in self._from_object[obj]:
self._from_object[obj][nametype] = set()
self._from_object[obj][nametype].add(name)
def remove_name(self, name, nametype="canonical"):
"""Remove a name for an object."""
obj = self._from_name[nametype][name]
# Remove the name from the name → object map
del self._from_name[nametype][name]
# Remove the name from the object → names map
self._from_object[obj][nametype].remove(name)
def remove_object(self, obj):
"""Remove all names for an object"""
# Remove the object from the name → object maps
for nametype in self._from_object[obj]:
names = self._from_object[obj][nametype]
for name in names:
del self._from_name[nametype][name]
# Remove all applicable names from the object → names map
del self._from_object[obj]
def names(self, obj, nametype="canonical"):
"""Return sorted tuple of names for object.
The tuple is sorted so that picking the first name, for example,
is deterministic.
"""
return tuple(sorted(self._from_object[obj][nametype]))
def obj(self, name, nametype="canonical"):
"""Return object by name (and type of name)."""
if not nametype in self._from_name:
raise KeyError(f"namespace '{nametype}' does not exist")
return self._from_name[nametype][name]
def get_or_create_name(
self,
base_name: str,
item: Hashable,
nametype="canonical",
):
"""Get or create a unique name for an item (mutates!).
:param base_name: String used as the first part of the autogenerated name.
:param item: The item that needs a name to be retrieved / created.
Returns the new name, formatted accoridng to {base_name}_{i} where i is a
integer to disambiguate the name from any existing names in the registry.
"""
# Check if there's an existing name
try:
names = self.names(item, nametype=nametype)
except KeyError:
# Find the first integer not already used as a suffix for the name
i = 1
while f"{base_name}_{i}" in self.namespace(nametype):
i += 1
# Create a name using the unused integer
name = f"{base_name}_{i}"
# Update the dictionary so the new name persists
self.add(name, item, nametype)
return name
return names[0]
def nametypes(self):
"""Return nametypes"""
return self._from_name.keys()
def namespace(self, nametype="canonical"):
"""Return names of a given nametype."""
return self._from_name.setdefault(nametype, {}).keys()
def objects(self):
"""Return all named objects.
This function is analogous to dict.values().
"""
return self._from_object.keys()
def pairs(self, nametype="canonical"):
"""Return iterable over (name, obj) pairs for nametype.
This function is analogous to dict.items().
"""
return self._from_name.setdefault(nametype, {}).items()
def get_namespace(self, nametype="canonical"):
"""Return name → obj map (dict) for nametype."""
return self._from_name.setdefault(nametype, {})
def __copy__(self):
"""Copy dicts but not named objects."""
new = type(self)()
for nametype in self._from_name:
new._from_name[nametype] = copy(self._from_name[nametype])
new._from_object = {
obj: {
nametype: copy(self._from_object[obj][nametype])
for nametype in self._from_object[obj]
}
for obj in self._from_object
}
return new