loop-interpolation 0.0.1__py3-none-any.whl
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.
- loop_interpolation/__init__.py +152 -0
- loop_interpolation/_builders.py +149 -0
- loop_interpolation/_constant_norm.py +232 -0
- loop_interpolation/_diagnostics.py +102 -0
- loop_interpolation/_discrete_fold_interpolator.py +242 -0
- loop_interpolation/_discrete_interpolator.py +1405 -0
- loop_interpolation/_fd_fold_interpolator.py +237 -0
- loop_interpolation/_finite_difference_interpolator.py +1348 -0
- loop_interpolation/_fold_event.py +185 -0
- loop_interpolation/_fold_norm_alignment.py +118 -0
- loop_interpolation/_fold_setup.py +39 -0
- loop_interpolation/_geological_interpolator.py +920 -0
- loop_interpolation/_interpolator_builder.py +251 -0
- loop_interpolation/_interpolator_factory.py +167 -0
- loop_interpolation/_interpolatortype.py +20 -0
- loop_interpolation/_operator.py +44 -0
- loop_interpolation/_p1interpolator.py +299 -0
- loop_interpolation/_p2interpolator.py +317 -0
- loop_interpolation/_regularisation.py +110 -0
- loop_interpolation/_solver_pipeline.py +138 -0
- loop_interpolation/_solver_strategy.py +218 -0
- loop_interpolation/_surfe_wrapper.py +211 -0
- loop_interpolation/_svariogram.py +133 -0
- loop_interpolation/_validation.py +604 -0
- loop_interpolation/constraints.py +473 -0
- loop_interpolation/fold_function/__init__.py +39 -0
- loop_interpolation/fold_function/_base_fold_rotation_angle.py +219 -0
- loop_interpolation/fold_function/_fourier_series_fold_rotation_angle.py +131 -0
- loop_interpolation/fold_function/_lambda_fold_rotation_angle.py +53 -0
- loop_interpolation/loopsolver/__init__.py +2 -0
- loop_interpolation/loopsolver/admm_constant_norm.py +118 -0
- loop_interpolation/loopsolver/admm_method.py +25 -0
- loop_interpolation/loopsolver/admm_solver.py +463 -0
- loop_interpolation/loopsolver/version.py +1 -0
- loop_interpolation-0.0.1.dist-info/METADATA +34 -0
- loop_interpolation-0.0.1.dist-info/RECORD +39 -0
- loop_interpolation-0.0.1.dist-info/WHEEL +5 -0
- loop_interpolation-0.0.1.dist-info/licenses/LICENSE +21 -0
- loop_interpolation-0.0.1.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Piecewise linear interpolator
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
import logging
|
|
6
|
+
|
|
7
|
+
import numpy as np
|
|
8
|
+
from scipy.spatial import KDTree
|
|
9
|
+
|
|
10
|
+
from . import InterpolatorType
|
|
11
|
+
from ._discrete_interpolator import DiscreteInterpolator
|
|
12
|
+
|
|
13
|
+
logger = logging.getLogger(__name__)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def compute_weighting(grid_points, constraint_points, alpha=10.0, sigma=1.0):
|
|
17
|
+
"""Compute spatially decaying regularisation weights around constraints."""
|
|
18
|
+
tree = KDTree(constraint_points)
|
|
19
|
+
distances, _ = tree.query(grid_points, k=1)
|
|
20
|
+
return 1 + alpha * np.exp(-(distances**2) / (2 * sigma**2))
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class P1Interpolator(DiscreteInterpolator):
|
|
24
|
+
def __init__(self, mesh):
|
|
25
|
+
"""
|
|
26
|
+
Piecewise Linear Interpolator
|
|
27
|
+
Approximates scalar field by finding coefficients to a piecewise linear
|
|
28
|
+
equation on a tetrahedral mesh. Uses constant gradient regularisation.
|
|
29
|
+
|
|
30
|
+
Parameters
|
|
31
|
+
----------
|
|
32
|
+
mesh - TetMesh
|
|
33
|
+
interpolation support
|
|
34
|
+
"""
|
|
35
|
+
self.shape = "rectangular"
|
|
36
|
+
DiscreteInterpolator.__init__(self, mesh)
|
|
37
|
+
# whether to assemble a rectangular matrix or a square matrix
|
|
38
|
+
self.support = mesh
|
|
39
|
+
|
|
40
|
+
self.interpolation_weights = {
|
|
41
|
+
"cgw": 0.1,
|
|
42
|
+
"cpw": 1.0,
|
|
43
|
+
"npw": 1.0,
|
|
44
|
+
"gpw": 1.0,
|
|
45
|
+
"tpw": 1.0,
|
|
46
|
+
"ipw": 1.0,
|
|
47
|
+
}
|
|
48
|
+
self.type = InterpolatorType.PIECEWISE_LINEAR
|
|
49
|
+
self.use_regularisation_weight_scale = False
|
|
50
|
+
self.regularisation_weight_sigma = None
|
|
51
|
+
|
|
52
|
+
def _update_regularisation_scale_from_norm_constraints(self):
|
|
53
|
+
points = self.get_norm_constraints()
|
|
54
|
+
if points.shape[0] == 0:
|
|
55
|
+
return
|
|
56
|
+
|
|
57
|
+
_, _, inside = self.support.evaluate_shape(points[:, : self.dimensions])
|
|
58
|
+
if not np.any(inside):
|
|
59
|
+
return
|
|
60
|
+
|
|
61
|
+
sigma = self.regularisation_weight_sigma
|
|
62
|
+
if sigma is None:
|
|
63
|
+
sigma = getattr(self.support, "nsteps", np.array([1.0], dtype=float))[0] * 10
|
|
64
|
+
self.regularisation_scale += compute_weighting(
|
|
65
|
+
self.support.nodes,
|
|
66
|
+
points[inside, : self.dimensions],
|
|
67
|
+
sigma=sigma,
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
def add_gradient_constraints(self, w=1.0):
|
|
71
|
+
pass
|
|
72
|
+
|
|
73
|
+
def add_norm_constraints(self, w=1.0):
|
|
74
|
+
points = self.get_norm_constraints()
|
|
75
|
+
if points.shape[0] > 0:
|
|
76
|
+
grad, elements, inside = self.support.evaluate_shape_derivatives(
|
|
77
|
+
points[:, : self.dimensions]
|
|
78
|
+
)
|
|
79
|
+
size = self.support.element_scale[elements[inside]]
|
|
80
|
+
wt = np.ones(size.shape[0])
|
|
81
|
+
wt *= w # s* size
|
|
82
|
+
elements = np.tile(self.support.elements[elements[inside]], (self.dimensions, 1, 1))
|
|
83
|
+
|
|
84
|
+
elements = elements.swapaxes(0, 1)
|
|
85
|
+
# elements = elements.swapaxes(0, 2)
|
|
86
|
+
# grad = grad.swapaxes(1, 2)
|
|
87
|
+
# elements = elements.swapaxes(1, 2)
|
|
88
|
+
|
|
89
|
+
self.add_constraints_to_least_squares(
|
|
90
|
+
grad[inside, :, :],
|
|
91
|
+
points[inside, self.dimensions : self.dimensions * 2],
|
|
92
|
+
elements,
|
|
93
|
+
w=wt,
|
|
94
|
+
name="norm",
|
|
95
|
+
)
|
|
96
|
+
self.up_to_date = False
|
|
97
|
+
|
|
98
|
+
def add_value_constraints(self, w=1.0):
|
|
99
|
+
points = self.get_value_constraints()
|
|
100
|
+
if points.shape[0] > 0:
|
|
101
|
+
N, elements, inside = self.support.evaluate_shape(points[:, : self.dimensions])
|
|
102
|
+
size = self.support.element_size[elements[inside]]
|
|
103
|
+
|
|
104
|
+
wt = np.ones(size.shape[0])
|
|
105
|
+
wt *= w # * size
|
|
106
|
+
self.add_constraints_to_least_squares(
|
|
107
|
+
N[inside, :],
|
|
108
|
+
points[inside, self.dimensions],
|
|
109
|
+
self.support.elements[elements[inside], :],
|
|
110
|
+
w=wt,
|
|
111
|
+
name="value",
|
|
112
|
+
)
|
|
113
|
+
self.up_to_date = False
|
|
114
|
+
|
|
115
|
+
def minimise_edge_jumps(self, w=0.1, vector_func=None, vector=None, name="edge jump"):
|
|
116
|
+
# NOTE: imposes \phi_T1(xi)-\phi_T2(xi) dot n =0
|
|
117
|
+
# iterate over all triangles
|
|
118
|
+
# flag inidicate which triangles have had all their relationships added
|
|
119
|
+
v1 = self.support.nodes[self.support.shared_elements][:, 0, :]
|
|
120
|
+
v2 = self.support.nodes[self.support.shared_elements][:, 1, :]
|
|
121
|
+
bc_t1 = self.support.barycentre[self.support.shared_element_relationships[:, 0]]
|
|
122
|
+
bc_t2 = self.support.barycentre[self.support.shared_element_relationships[:, 1]]
|
|
123
|
+
norm = self.support.shared_element_norm
|
|
124
|
+
# shared_element_scale = self.support.shared_element_scale
|
|
125
|
+
|
|
126
|
+
# evaluate normal if using vector func for cp2
|
|
127
|
+
if vector_func:
|
|
128
|
+
norm = vector_func((v1 + v2) / 2)
|
|
129
|
+
if vector is not None and bc_t1.shape[0] == vector.shape[0]:
|
|
130
|
+
norm = vector
|
|
131
|
+
# evaluate the shape function for the edges for each neighbouring triangle
|
|
132
|
+
Dt, tri1, _inside = self.support.evaluate_shape_derivatives(
|
|
133
|
+
bc_t1, elements=self.support.shared_element_relationships[:, 0]
|
|
134
|
+
)
|
|
135
|
+
Dn, tri2, _inside = self.support.evaluate_shape_derivatives(
|
|
136
|
+
bc_t2, elements=self.support.shared_element_relationships[:, 1]
|
|
137
|
+
)
|
|
138
|
+
# constraint for each cp is triangle - neighbour create a Nx12 matrix
|
|
139
|
+
const_t = np.einsum("ij,ijk->ik", norm, Dt)
|
|
140
|
+
const_n = -np.einsum("ij,ijk->ik", norm, Dn)
|
|
141
|
+
# const_t_cp2 = np.einsum('ij,ikj->ik',normal,cp2_Dt)
|
|
142
|
+
# const_n_cp2 = -np.einsum('ij,ikj->ik',normal,cp2_Dn)
|
|
143
|
+
# shared_element_size = self.support.shared_element_size
|
|
144
|
+
# const_t /= shared_element_size[:, None] # normalise by element size
|
|
145
|
+
# const_n /= shared_element_size[:, None] # normalise by element size
|
|
146
|
+
const = np.hstack([const_t, const_n])
|
|
147
|
+
|
|
148
|
+
# get vertex indexes
|
|
149
|
+
tri_cp1 = np.hstack([self.support.elements[tri1], self.support.elements[tri2]])
|
|
150
|
+
regularisation_w = w
|
|
151
|
+
if self.use_regularisation_weight_scale:
|
|
152
|
+
scale_t = self.regularisation_scale[self.support.elements[tri1]].mean(axis=1)
|
|
153
|
+
scale_n = self.regularisation_scale[self.support.elements[tri2]].mean(axis=1)
|
|
154
|
+
edge_scale = 0.5 * (scale_t + scale_n)
|
|
155
|
+
regularisation_w = edge_scale * w
|
|
156
|
+
# tri_cp2 = np.hstack([self.support.elements[cp2_tri1],self.support.elements[tri2]])
|
|
157
|
+
# add cp1 and cp2 to the least squares system
|
|
158
|
+
|
|
159
|
+
self.add_constraints_to_least_squares(
|
|
160
|
+
const,
|
|
161
|
+
np.zeros(const.shape[0]),
|
|
162
|
+
tri_cp1,
|
|
163
|
+
w=regularisation_w,
|
|
164
|
+
name=name,
|
|
165
|
+
)
|
|
166
|
+
self.up_to_date = False
|
|
167
|
+
# p2.add_constraints_to_least_squares(const_cp2*e_len[:,None]*w,np.zeros(const_cp1.shape[0]),tri_cp2, name='edge jump cp2')
|
|
168
|
+
|
|
169
|
+
def get_regularisation_sample_points(self) -> np.ndarray:
|
|
170
|
+
return self.support.nodes[self.support.shared_elements].mean(axis=1)
|
|
171
|
+
|
|
172
|
+
def _add_directional_regularisation(
|
|
173
|
+
self,
|
|
174
|
+
weight: float,
|
|
175
|
+
vectors: np.ndarray,
|
|
176
|
+
name: str = "directional regularisation",
|
|
177
|
+
):
|
|
178
|
+
self.minimise_edge_jumps(w=weight, vector=vectors, name=name)
|
|
179
|
+
|
|
180
|
+
def setup_interpolator(self, **kwargs):
|
|
181
|
+
"""
|
|
182
|
+
Searches through kwargs for any interpolation weights and updates
|
|
183
|
+
the dictionary.
|
|
184
|
+
Then adds the constraints to the linear system using the
|
|
185
|
+
interpolation weights values
|
|
186
|
+
|
|
187
|
+
Parameters
|
|
188
|
+
----------
|
|
189
|
+
kwargs -
|
|
190
|
+
interpolation weights
|
|
191
|
+
|
|
192
|
+
Returns
|
|
193
|
+
-------
|
|
194
|
+
|
|
195
|
+
"""
|
|
196
|
+
# can't reset here, clears fold constraints
|
|
197
|
+
self.reset()
|
|
198
|
+
regularisation_config = self.resolve_regularisation_config(
|
|
199
|
+
regularisation=kwargs.get("regularisation", None),
|
|
200
|
+
directional_regularisation=kwargs.get("directional_regularisation", None),
|
|
201
|
+
)
|
|
202
|
+
self._apply_isotropic_regularisation_weight(
|
|
203
|
+
regularisation_config.isotropic,
|
|
204
|
+
("cgw",),
|
|
205
|
+
)
|
|
206
|
+
self._apply_interpolation_weight_kwargs(
|
|
207
|
+
kwargs,
|
|
208
|
+
skip_keys=("regularisation", "directional_regularisation"),
|
|
209
|
+
)
|
|
210
|
+
|
|
211
|
+
self.use_regularisation_weight_scale = kwargs.get("use_regularisation_weight_scale", False)
|
|
212
|
+
self.regularisation_weight_sigma = kwargs.get("regularisation_weight_sigma", None)
|
|
213
|
+
if self.use_regularisation_weight_scale:
|
|
214
|
+
self._update_regularisation_scale_from_norm_constraints()
|
|
215
|
+
|
|
216
|
+
if self.interpolation_weights["cgw"] > 0.0:
|
|
217
|
+
self.up_to_date = False
|
|
218
|
+
self.minimise_edge_jumps(self.interpolation_weights["cgw"])
|
|
219
|
+
# direction_feature=kwargs.get("direction_feature", None),
|
|
220
|
+
# direction_vector=kwargs.get("direction_vector", None),
|
|
221
|
+
# )
|
|
222
|
+
# self.minimise_grad_steepness(
|
|
223
|
+
# w=self.interpolation_weights.get("steepness_weight", 0.01),
|
|
224
|
+
# wtfunc=self.interpolation_weights.get("steepness_wtfunc", None),
|
|
225
|
+
# )
|
|
226
|
+
logger.info(
|
|
227
|
+
"Using constant gradient regularisation w = {:f}".format(self.interpolation_weights["cgw"])
|
|
228
|
+
)
|
|
229
|
+
self.add_directional_regularisation(regularisation_config.directional)
|
|
230
|
+
|
|
231
|
+
logger.info(
|
|
232
|
+
"Added %i gradient constraints, %i normal constraints,"
|
|
233
|
+
"%i tangent constraints and %i value constraints",
|
|
234
|
+
self.n_g,
|
|
235
|
+
self.n_n,
|
|
236
|
+
self.n_t,
|
|
237
|
+
self.n_i,
|
|
238
|
+
)
|
|
239
|
+
self.add_gradient_constraints(self.interpolation_weights["gpw"])
|
|
240
|
+
self.add_norm_constraints(self.interpolation_weights["npw"])
|
|
241
|
+
self.add_value_constraints(self.interpolation_weights["cpw"])
|
|
242
|
+
self.add_tangent_constraints(self.interpolation_weights["tpw"])
|
|
243
|
+
self.add_value_inequality_constraints()
|
|
244
|
+
self.add_inequality_pairs_constraints()
|
|
245
|
+
# self.add_interface_constraints(self.interpolation_weights["ipw"])
|
|
246
|
+
return self.finalize_setup_diagnostics_report()
|
|
247
|
+
|
|
248
|
+
def add_gradient_orthogonal_constraints(
|
|
249
|
+
self,
|
|
250
|
+
points: np.ndarray,
|
|
251
|
+
vectors: np.ndarray,
|
|
252
|
+
w: float = 1.0,
|
|
253
|
+
b: float = 0,
|
|
254
|
+
name="undefined gradient orthogonal constraint",
|
|
255
|
+
):
|
|
256
|
+
"""
|
|
257
|
+
Constraints scalar field to be orthogonal to a given vector
|
|
258
|
+
|
|
259
|
+
Parameters
|
|
260
|
+
----------
|
|
261
|
+
points : np.darray
|
|
262
|
+
location to add gradient orthogonal constraint
|
|
263
|
+
vector : np.darray
|
|
264
|
+
vector to be orthogonal to, should be the same shape as points
|
|
265
|
+
w : double
|
|
266
|
+
B : np.array
|
|
267
|
+
|
|
268
|
+
Returns
|
|
269
|
+
-------
|
|
270
|
+
|
|
271
|
+
"""
|
|
272
|
+
if points.shape[0] > 0:
|
|
273
|
+
grad, elements, inside = self.support.evaluate_shape_derivatives(
|
|
274
|
+
points[:, : self.dimensions]
|
|
275
|
+
)
|
|
276
|
+
size = self.support.element_size[elements[inside]]
|
|
277
|
+
wt = np.ones(size.shape[0])
|
|
278
|
+
wt *= w * size
|
|
279
|
+
elements = self.support.elements[elements[inside], :]
|
|
280
|
+
# elements = np.tile(self.support.elements[elements[inside]], (3, 1, 1))
|
|
281
|
+
|
|
282
|
+
# elements = elements.swapaxes(0, 1)
|
|
283
|
+
# elements = elements.swapaxes(0, 2)
|
|
284
|
+
# grad = grad.swapaxes(1, 2)
|
|
285
|
+
# elements = elements.swapaxes(1, 2)
|
|
286
|
+
norm = np.linalg.norm(vectors, axis=1)
|
|
287
|
+
vectors[norm > 0, :] /= norm[norm > 0, None]
|
|
288
|
+
A = np.einsum("ij,ijk->ik", vectors[inside, :3], grad[inside, :, :])
|
|
289
|
+
B = np.zeros(points[inside, :].shape[0]) + b
|
|
290
|
+
self.add_constraints_to_least_squares(A, B, elements, w=wt, name=name)
|
|
291
|
+
if np.sum(inside) <= 0:
|
|
292
|
+
logger.warning(
|
|
293
|
+
f"{np.sum(~inside)} \
|
|
294
|
+
gradient constraints not added: outside of model bounding box"
|
|
295
|
+
)
|
|
296
|
+
self.up_to_date = False
|
|
297
|
+
|
|
298
|
+
def add_interface_constraints(self, w: float = 1):
|
|
299
|
+
raise NotImplementedError
|
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Piecewise quadratic interpolator
|
|
3
|
+
"""
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
import logging
|
|
7
|
+
from typing import Callable
|
|
8
|
+
|
|
9
|
+
import numpy as np
|
|
10
|
+
|
|
11
|
+
from . import InterpolatorType
|
|
12
|
+
from ._discrete_interpolator import DiscreteInterpolator
|
|
13
|
+
|
|
14
|
+
logger = logging.getLogger(__name__)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class P2Interpolator(DiscreteInterpolator):
|
|
18
|
+
""" """
|
|
19
|
+
|
|
20
|
+
def __init__(self, mesh):
|
|
21
|
+
"""
|
|
22
|
+
Piecewise Quadratic Interpolator
|
|
23
|
+
Approximates scalar field by finding coefficients to a piecewise quadratic
|
|
24
|
+
equation on a tetrahedral mesh. Uses constant gradient regularisation.
|
|
25
|
+
|
|
26
|
+
Parameters
|
|
27
|
+
----------
|
|
28
|
+
mesh - TetMesh
|
|
29
|
+
interpolation support
|
|
30
|
+
"""
|
|
31
|
+
self.shape = "rectangular"
|
|
32
|
+
DiscreteInterpolator.__init__(self, mesh)
|
|
33
|
+
# whether to assemble a rectangular matrix or a square matrix
|
|
34
|
+
self.interpolator_type = "P2"
|
|
35
|
+
self.support = mesh
|
|
36
|
+
|
|
37
|
+
self.interpolation_weights = {
|
|
38
|
+
"cgw": 0.1,
|
|
39
|
+
"cpw": 1.0,
|
|
40
|
+
"npw": 1.0,
|
|
41
|
+
"gpw": 1.0,
|
|
42
|
+
"tpw": 1.0,
|
|
43
|
+
"ipw": 1.0,
|
|
44
|
+
}
|
|
45
|
+
self.type = InterpolatorType.PIECEWISE_QUADRATIC
|
|
46
|
+
|
|
47
|
+
def setup_interpolator(self, **kwargs):
|
|
48
|
+
"""
|
|
49
|
+
Searches through kwargs for any interpolation weights and updates
|
|
50
|
+
the dictionary.
|
|
51
|
+
Then adds the constraints to the linear system using the
|
|
52
|
+
interpolation weights values
|
|
53
|
+
|
|
54
|
+
Parameters
|
|
55
|
+
----------
|
|
56
|
+
kwargs -
|
|
57
|
+
interpolation weights
|
|
58
|
+
|
|
59
|
+
Returns
|
|
60
|
+
-------
|
|
61
|
+
|
|
62
|
+
"""
|
|
63
|
+
self.reset()
|
|
64
|
+
regularisation_config = self.resolve_regularisation_config(
|
|
65
|
+
regularisation=kwargs.get("regularisation", None),
|
|
66
|
+
directional_regularisation=kwargs.get("directional_regularisation", None),
|
|
67
|
+
)
|
|
68
|
+
self._apply_isotropic_regularisation_weight(
|
|
69
|
+
regularisation_config.isotropic,
|
|
70
|
+
("cgw",),
|
|
71
|
+
)
|
|
72
|
+
self._apply_interpolation_weight_kwargs(
|
|
73
|
+
kwargs,
|
|
74
|
+
skip_keys=("regularisation", "directional_regularisation"),
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
if self.interpolation_weights["cgw"] > 0.0:
|
|
78
|
+
self.up_to_date = False
|
|
79
|
+
self.minimise_edge_jumps(self.interpolation_weights["cgw"])
|
|
80
|
+
self.minimise_grad_steepness(
|
|
81
|
+
w=self.interpolation_weights.get("steepness_weight", 0.01),
|
|
82
|
+
wtfunc=self.interpolation_weights.get("steepness_wtfunc", None),
|
|
83
|
+
)
|
|
84
|
+
logger.info(
|
|
85
|
+
"Using constant gradient regularisation w = {:f}".format(self.interpolation_weights["cgw"])
|
|
86
|
+
)
|
|
87
|
+
self.add_directional_regularisation(regularisation_config.directional)
|
|
88
|
+
|
|
89
|
+
logger.info(
|
|
90
|
+
"Added %i gradient constraints, %i normal constraints,"
|
|
91
|
+
"%i tangent constraints and %i value constraints",
|
|
92
|
+
self.n_g,
|
|
93
|
+
self.n_n,
|
|
94
|
+
self.n_t,
|
|
95
|
+
self.n_i,
|
|
96
|
+
)
|
|
97
|
+
self.add_gradient_constraints(self.interpolation_weights["gpw"])
|
|
98
|
+
self.add_norm_constraints(self.interpolation_weights["npw"])
|
|
99
|
+
self.add_value_constraints(self.interpolation_weights["cpw"])
|
|
100
|
+
self.add_tangent_constraints(self.interpolation_weights["tpw"])
|
|
101
|
+
# self.add_interface_constraints(self.interpolation_weights["ipw"])
|
|
102
|
+
return self.finalize_setup_diagnostics_report()
|
|
103
|
+
|
|
104
|
+
def copy(self):
|
|
105
|
+
return P2Interpolator(self.support)
|
|
106
|
+
|
|
107
|
+
def add_gradient_constraints(self, w: float = 1.0):
|
|
108
|
+
points = self.get_gradient_constraints()
|
|
109
|
+
if points.shape[0] > 0:
|
|
110
|
+
grad, elements = self.support.evaluate_shape_derivatives(points[:, : self.dimensions])
|
|
111
|
+
inside = elements > -1
|
|
112
|
+
area = self.support.element_size[elements[inside]]
|
|
113
|
+
wt = np.ones(area.shape[0])
|
|
114
|
+
wt *= w * area
|
|
115
|
+
A = np.einsum(
|
|
116
|
+
"ikj,ij->ik",
|
|
117
|
+
grad[inside, :],
|
|
118
|
+
points[inside, self.dimensions : self.dimensions * 2],
|
|
119
|
+
)
|
|
120
|
+
B = np.zeros(A.shape[0])
|
|
121
|
+
elements = self.support.elements[elements[inside]]
|
|
122
|
+
self.add_constraints_to_least_squares(A * wt[:, None], B, elements, name="gradient")
|
|
123
|
+
|
|
124
|
+
def add_gradient_orthogonal_constraints(
|
|
125
|
+
self, points: np.ndarray, vector: np.ndarray, w=1.0, B=0
|
|
126
|
+
):
|
|
127
|
+
"""
|
|
128
|
+
Constraints scalar field to be orthogonal to a given vector
|
|
129
|
+
|
|
130
|
+
Parameters
|
|
131
|
+
----------
|
|
132
|
+
position
|
|
133
|
+
normals
|
|
134
|
+
w
|
|
135
|
+
B
|
|
136
|
+
|
|
137
|
+
Returns
|
|
138
|
+
-------
|
|
139
|
+
|
|
140
|
+
"""
|
|
141
|
+
if points.shape[0] > 0:
|
|
142
|
+
grad, elements = self.support.evaluate_shape_derivatives(points[:, : self.dimensions])
|
|
143
|
+
inside = elements > -1
|
|
144
|
+
area = self.support.element_size[elements[inside]]
|
|
145
|
+
wt = np.ones(area.shape[0])
|
|
146
|
+
wt *= w * area
|
|
147
|
+
A = np.einsum("ijk,ij->ik", grad[inside, :], vector[inside, :])
|
|
148
|
+
B = np.zeros(A.shape[0])
|
|
149
|
+
elements = self.support.elements[elements[inside]]
|
|
150
|
+
self.add_constraints_to_least_squares(
|
|
151
|
+
A * wt[:, None], B, elements, name="gradient orthogonal"
|
|
152
|
+
)
|
|
153
|
+
|
|
154
|
+
def add_norm_constraints(self, w: float = 1.0):
|
|
155
|
+
points = self.get_norm_constraints()
|
|
156
|
+
if points.shape[0] > 0:
|
|
157
|
+
grad, elements = self.support.evaluate_shape_derivatives(points[:, : self.dimensions])
|
|
158
|
+
inside = elements > -1
|
|
159
|
+
area = self.support.element_size[elements[inside]]
|
|
160
|
+
wt = np.ones(area.shape[0])
|
|
161
|
+
wt *= w * area
|
|
162
|
+
elements = np.tile(self.support.elements[elements[inside]], (self.dimensions, 1, 1))
|
|
163
|
+
elements = elements.swapaxes(0, 1)
|
|
164
|
+
self.add_constraints_to_least_squares(
|
|
165
|
+
grad[inside, :, :] * wt[:, None, None],
|
|
166
|
+
points[inside, self.dimensions : self.dimensions * 2] * wt[:, None],
|
|
167
|
+
elements,
|
|
168
|
+
name="norm",
|
|
169
|
+
)
|
|
170
|
+
|
|
171
|
+
def add_value_constraints(self, w: float = 1.0):
|
|
172
|
+
points = self.get_value_constraints()
|
|
173
|
+
if points.shape[0] > 0:
|
|
174
|
+
N, elements, mask = self.support.evaluate_shape(points[:, : self.dimensions])
|
|
175
|
+
# mask = elements > 0
|
|
176
|
+
size = self.support.element_size[elements[mask]]
|
|
177
|
+
wt = np.ones(size.shape[0])
|
|
178
|
+
wt *= w
|
|
179
|
+
self.add_constraints_to_least_squares(
|
|
180
|
+
N[mask, :],
|
|
181
|
+
points[mask, self.dimensions],
|
|
182
|
+
self.support.elements[elements[mask], :],
|
|
183
|
+
w=wt,
|
|
184
|
+
name="value",
|
|
185
|
+
)
|
|
186
|
+
|
|
187
|
+
def minimise_grad_steepness(
|
|
188
|
+
self,
|
|
189
|
+
w: float = 0.1,
|
|
190
|
+
maskall: bool = False,
|
|
191
|
+
wtfunc: Callable[[np.ndarray], np.ndarray] | None = None,
|
|
192
|
+
):
|
|
193
|
+
"""This constraint minimises the second derivative of the gradient
|
|
194
|
+
mimimising the 2nd derivative should prevent high curvature solutions
|
|
195
|
+
It is not added on the borders
|
|
196
|
+
|
|
197
|
+
Parameters
|
|
198
|
+
----------
|
|
199
|
+
w : float, optional
|
|
200
|
+
[description], by default 0.1
|
|
201
|
+
maskall : bool, default False
|
|
202
|
+
whether to apply on all elements or just internal elements (default)
|
|
203
|
+
wtfunc : callable, optional
|
|
204
|
+
a function that returns the weight to be applied at xyz. Called on the barycentre
|
|
205
|
+
of the tetrahedron
|
|
206
|
+
"""
|
|
207
|
+
elements = np.arange(0, len(self.support.elements))
|
|
208
|
+
mask = np.ones(self.support.neighbours.shape[0], dtype=bool)
|
|
209
|
+
if not maskall:
|
|
210
|
+
mask[:] = np.all(self.support.neighbours > 3, axis=1)
|
|
211
|
+
|
|
212
|
+
d2 = self.support.evaluate_shape_d2(elements[mask])
|
|
213
|
+
# d2 shape is [ele_idx, deriv, node]
|
|
214
|
+
wt = np.ones(d2.shape[0])
|
|
215
|
+
wt *= w # * self.support.element_size[mask]
|
|
216
|
+
if callable(wtfunc):
|
|
217
|
+
logger.info("Using function to weight gradient steepness")
|
|
218
|
+
wt = wtfunc(self.support.barycentre) * self.support.element_size[mask]
|
|
219
|
+
idc = self.support.elements[elements[mask]]
|
|
220
|
+
for i in range(d2.shape[1]):
|
|
221
|
+
self.add_constraints_to_least_squares(
|
|
222
|
+
d2[:, i, :],
|
|
223
|
+
np.zeros(d2.shape[0]),
|
|
224
|
+
idc[:, :],
|
|
225
|
+
w=wt,
|
|
226
|
+
name=f"gradsteepness_{i}",
|
|
227
|
+
)
|
|
228
|
+
|
|
229
|
+
def minimise_edge_jumps(
|
|
230
|
+
self,
|
|
231
|
+
w: float = 0.1,
|
|
232
|
+
wtfunc: Callable[[np.ndarray], np.ndarray] | None = None,
|
|
233
|
+
vector_func: Callable[[np.ndarray], np.ndarray] | None = None,
|
|
234
|
+
quadrature_points: int | None = None,
|
|
235
|
+
):
|
|
236
|
+
"""_summary_
|
|
237
|
+
|
|
238
|
+
Parameters
|
|
239
|
+
----------
|
|
240
|
+
w : float, optional
|
|
241
|
+
_description_, by default 0.1
|
|
242
|
+
wtfunc : callable, optional
|
|
243
|
+
_description_, by default None
|
|
244
|
+
vector_func : callable, optional
|
|
245
|
+
_description_, by default None
|
|
246
|
+
"""
|
|
247
|
+
# NOTE: imposes \phi_T1(xi)-\phi_T2(xi) dot n =0
|
|
248
|
+
# iterate over all triangles
|
|
249
|
+
|
|
250
|
+
get_qp_kwargs = {} if quadrature_points is None else {"npts": quadrature_points}
|
|
251
|
+
cp, weight = self.support.get_quadrature_points(**get_qp_kwargs)
|
|
252
|
+
|
|
253
|
+
norm = self.support.shared_element_norm
|
|
254
|
+
|
|
255
|
+
# evaluate normal if using vector func for cp1
|
|
256
|
+
for i in range(cp.shape[1]):
|
|
257
|
+
if callable(vector_func):
|
|
258
|
+
norm = vector_func(cp[:, i, :])
|
|
259
|
+
# evaluate the shape function for the edges for each neighbouring triangle
|
|
260
|
+
cp_Dt, cp_tri1 = self.support.evaluate_shape_derivatives(
|
|
261
|
+
cp[:, i, :], elements=self.support.shared_element_relationships[:, 0]
|
|
262
|
+
)
|
|
263
|
+
cp_Dn, cp_tri2 = self.support.evaluate_shape_derivatives(
|
|
264
|
+
cp[:, i, :], elements=self.support.shared_element_relationships[:, 1]
|
|
265
|
+
)
|
|
266
|
+
# constraint for each cp is triangle - neighbour create a Nx12 matrix
|
|
267
|
+
const_t_cp = np.einsum("ij,ijk->ik", norm, cp_Dt)
|
|
268
|
+
const_n_cp = -np.einsum("ij,ijk->ik", norm, cp_Dn)
|
|
269
|
+
|
|
270
|
+
const_cp = np.hstack([const_t_cp, const_n_cp])
|
|
271
|
+
tri_cp = np.hstack([self.support.elements[cp_tri1], self.support.elements[cp_tri2]])
|
|
272
|
+
wt = np.zeros(tri_cp.shape[0])
|
|
273
|
+
wt[:] = w * weight[:, i]
|
|
274
|
+
if wtfunc:
|
|
275
|
+
wt = wtfunc(tri_cp)
|
|
276
|
+
self.add_constraints_to_least_squares(
|
|
277
|
+
const_cp,
|
|
278
|
+
np.zeros(const_cp.shape[0]),
|
|
279
|
+
tri_cp,
|
|
280
|
+
w=wt,
|
|
281
|
+
name=f"shared element jump cp{i}",
|
|
282
|
+
)
|
|
283
|
+
|
|
284
|
+
def evaluate_d2(self, evaluation_points: np.ndarray) -> np.ndarray:
|
|
285
|
+
"""Evaluate second derivatives of the interpolant at given points.
|
|
286
|
+
|
|
287
|
+
Parameters
|
|
288
|
+
----------
|
|
289
|
+
evaluation_points : np.ndarray
|
|
290
|
+
Array of shape (n_points, 3) containing point coordinates
|
|
291
|
+
|
|
292
|
+
Returns
|
|
293
|
+
-------
|
|
294
|
+
np.ndarray
|
|
295
|
+
Array of shape (n_points, 6) containing second derivatives:
|
|
296
|
+
[d2x, dxdy, d2y, dxdz, dydz, d2z]
|
|
297
|
+
"""
|
|
298
|
+
evaluation_points = np.array(evaluation_points)
|
|
299
|
+
mask = np.isnan(evaluation_points).any(axis=1)
|
|
300
|
+
valid_points = evaluation_points[~mask, :]
|
|
301
|
+
|
|
302
|
+
if valid_points.shape[0] == 0:
|
|
303
|
+
# Preserve historical shape when all rows are invalid.
|
|
304
|
+
return np.zeros(evaluation_points.shape[0])
|
|
305
|
+
|
|
306
|
+
valid_d2 = np.asarray(self.support.evaluate_d2(valid_points, self.c))
|
|
307
|
+
if valid_d2.ndim == 1:
|
|
308
|
+
evaluated = np.zeros(evaluation_points.shape[0])
|
|
309
|
+
evaluated[~mask] = valid_d2
|
|
310
|
+
return evaluated
|
|
311
|
+
|
|
312
|
+
evaluated = np.full((evaluation_points.shape[0], valid_d2.shape[1]), np.nan)
|
|
313
|
+
evaluated[~mask] = valid_d2
|
|
314
|
+
return evaluated
|
|
315
|
+
|
|
316
|
+
def add_interface_constraints(self, w: float = 1):
|
|
317
|
+
raise NotImplementedError
|