File size: 5,041 Bytes
66c9c8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from typing import Any

import warp as wp

from warp.fem.types import Sample
from warp.fem.space import FunctionSpace, SpacePartition
from warp.fem.geometry import Geometry, DeformedGeometry


class FieldLike:
    """Base class for integrable fields"""

    EvalArg: wp.codegen.Struct
    """Structure containing field-level arguments passed to device functions for field evaluation"""

    ElementEvalArg: wp.codegen.Struct
    """Structure combining geometry-level and field-level arguments passed to device functions for field evaluation"""

    def eval_arg_value(self, device) -> "EvalArg":
        """Value of the field-level arguments to be passed to device functions"""
        raise NotImplementedError

    @property
    def degree(self) -> int:
        """Polynomial degree of the field, used to estimate necessary quadrature order"""
        raise NotImplementedError

    @property
    def name(self) -> str:
        raise NotImplementedError

    @property
    def __str__(self) -> str:
        return self.name

    def eval_arg_value(self, device):
        """Value of arguments to be passed to device functions"""
        raise NotImplementedError

    @staticmethod
    def eval_inner(args: "ElementEvalArg", s: "Sample"):
        """Device function evaluating the inner field value at a sample point"""
        raise NotImplementedError

    @staticmethod
    def eval_grad_inner(args: "ElementEvalArg", s: "Sample"):
        """Device function evaluating the inner field gradient at a sample point"""
        raise NotImplementedError

    @staticmethod
    def eval_div_inner(args: "ElementEvalArg", s: "Sample"):
        """Device function evaluating the inner field divergence at a sample point"""
        raise NotImplementedError

    @staticmethod
    def eval_outer(args: "ElementEvalArg", s: "Sample"):
        """Device function evaluating the outer field value at a sample point"""
        raise NotImplementedError

    @staticmethod
    def eval_grad_outer(args: "ElementEvalArg", s: "Sample"):
        """Device function evaluating the outer field gradient at a sample point"""
        raise NotImplementedError

    @staticmethod
    def eval_div_outer(args: "ElementEvalArg", s: "Sample"):
        """Device function evaluating the outer field divergence at a sample point"""
        raise NotImplementedError


class SpaceField(FieldLike):
    """Base class for fields defined over a function space"""

    def __init__(self, space: FunctionSpace, space_partition: SpacePartition):
        self._space = space
        self._space_partition = space_partition

    @property
    def space(self) -> FunctionSpace:
        return self._space

    @property
    def space_partition(self) -> SpacePartition:
        return self._space_partition

    @property
    def degree(self) -> int:
        return self.space.degree

    @property
    def dtype(self) -> type:
        return self.space.dtype

    @property
    def dof_dtype(self) -> type:
        return self.space.dof_dtype

    def gradient_valid(self) -> bool:
        """Whether gradient operator can be computed. Only for scalar and vector fields as higher-order tensors are not support yet"""
        return not wp.types.type_is_matrix(self.dtype)

    def divergence_valid(self) -> bool:
        """Whether divergence of this field can be computed. Only for vector and tensor fields with same dimension as embedding geometry"""
        if wp.types.type_is_vector(self.dtype):
            return wp.types.type_length(self.dtype) == self.space.geometry.dimension
        if wp.types.type_is_matrix(self.dtype):
            return self.dtype._shape_[0] == self.space.geometry.dimension
        return False

    def _make_eval_degree(self):
        ORDER = self.space.ORDER
        from warp.fem import cache

        @cache.dynamic_func(suffix=self.name)
        def degree(args: self.ElementEvalArg):
            return ORDER

        return degree


class DiscreteField(SpaceField):
    """Explicitly-valued field defined over a partition of a discrete function space"""

    @property
    def dof_values(self) -> wp.array:
        """Array of degrees of freedom values"""
        raise NotImplementedError

    @dof_values.setter
    def dof_values(self, values: wp.array):
        """Sets degrees of freedom values from an array"""
        raise NotImplementedError

    def trace(self) -> "DiscreteField":
        """Trace of this field over a lower-dimensional function space"""
        raise NotImplementedError

    @staticmethod
    def set_node_value(args: "FieldLike.EvalArg", node_index: int, value: Any):
        """Device function setting the value at given node"""
        raise NotImplementedError

    @property
    def name(self) -> str:
        return f"{self.__class__.__qualname__}_{self.space.name}_{self.space_partition.name}"

    def make_deformed_geometry(self) -> Geometry:
        """Returns a deformed version of the underlying geometry using this field's values as displacement"""
        return DeformedGeometry(self)