-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathdiscrete_field.py
61 lines (47 loc) · 1.68 KB
/
discrete_field.py
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
from typing import NamedTuple, Optional, List
import numpy as np
from numpy import ndarray
class DiscreteField(NamedTuple):
"""A function defined at the global quadrature points."""
value: Optional[ndarray] = None
grad: Optional[ndarray] = None
div: Optional[ndarray] = None
curl: Optional[ndarray] = None
hess: Optional[ndarray] = None
hod: Optional[ndarray] = None
@property
def f(self):
"""For backwards compatibility; used by old style form decorators."""
return self.value
@property
def df(self):
"""For backwards compatibility; used by old style form decorators."""
if self.grad is not None:
return self.grad
elif self.div is not None:
return self.div
elif self.curl is not None:
return self.curl
return None
@property
def ddf(self):
"""For backwards compatibility; used by old style form decorators."""
return self.hess
def __array__(self):
return self.f
def __mul__(self, other):
if isinstance(other, DiscreteField):
return self.f * other.f
return self.f * other
def _split(self):
"""Split all components based on their first dimension."""
return [DiscreteField(*[f[i] for f in self if f is not None])
for i in range(self.f.shape[0])]
def zeros_like(self):
"""Return zero :class:`~skfem.element.DiscreteField` with same size."""
def zero_or_none(x):
if x is None:
return None
return np.zeros_like(x)
return DiscreteField(*[zero_or_none(field) for field in self])
__rmul__ = __mul__