|
| 1 | +from ast import LShift |
| 2 | +import numpy as np |
| 3 | +import pandas as pd |
| 4 | + |
| 5 | +from typing import Optional |
| 6 | +from LoopStructural.interpolators import ( |
| 7 | + get_interpolator, |
| 8 | +) |
| 9 | +from LoopStructural.utils import BoundingBox |
| 10 | +from LoopStructural.utils import getLogger |
| 11 | + |
| 12 | +logger = getLogger(__name__) |
| 13 | + |
| 14 | + |
| 15 | +class LoopInterpolator: |
| 16 | + def __init__( |
| 17 | + self, |
| 18 | + bounding_box: BoundingBox, |
| 19 | + dimensions: int = 3, |
| 20 | + type: str = "FDI", |
| 21 | + nelements: int = 1000, |
| 22 | + ): |
| 23 | + """Scikitlearn like interface for LoopStructural interpolators |
| 24 | + useful for quickly building an interpolator to apply to a dataset |
| 25 | + build a generic interpolation object speficying the bounding box |
| 26 | + and then fit to constraints and evaluate the interpolator |
| 27 | +
|
| 28 | + Parameters |
| 29 | + ---------- |
| 30 | + bounding_box : BoundingBox |
| 31 | + Area where the interpolation will work |
| 32 | + dimensions : int, optional |
| 33 | + number of dimensions e.g. 3d or 2d, by default 3 |
| 34 | + type : str, optional |
| 35 | + type of interpolation algorithm FDI- finite difference, PLI - linear finite elements, |
| 36 | + by default "FDI" |
| 37 | + nelements : int, optional |
| 38 | + degrees of freedom for interpolator, by default 1000 |
| 39 | + """ |
| 40 | + self.dimensions = dimensions |
| 41 | + self.type = "FDI" |
| 42 | + self.interpolator = get_interpolator(bounding_box, type, dimensions, nelements) |
| 43 | + |
| 44 | + def fit( |
| 45 | + self, |
| 46 | + values: Optional[np.ndarray] = None, |
| 47 | + tangent_vectors: Optional[np.ndarray] = None, |
| 48 | + normal_vectors: Optional[np.ndarray] = None, |
| 49 | + inequality_constraints: Optional[np.ndarray] = None, |
| 50 | + ): |
| 51 | + if values is not None: |
| 52 | + self.interpolator.set_value_constraints(values) |
| 53 | + if tangent_vectors is not None: |
| 54 | + self.interpolator.set_tangent_constraints(tangent_vectors) |
| 55 | + if normal_vectors is not None: |
| 56 | + self.interpolator.set_normal_constraints(normal_vectors) |
| 57 | + if inequality_constraints: |
| 58 | + pass |
| 59 | + |
| 60 | + self.interpolator.setup() |
| 61 | + |
| 62 | + def evalute_scalar_value(self, locations: np.ndarray): |
| 63 | + self.interpolator.update() |
| 64 | + return self.interpolator.evaluate_value(locations) |
| 65 | + |
| 66 | + def evaluate_gradient(self, locations: np.ndarray): |
| 67 | + self.interpolator.update() |
| 68 | + return self.interpolator.evaluate_gradient(locations) |
| 69 | + |
| 70 | + def fit_and_evaluate_value( |
| 71 | + self, |
| 72 | + values: Optional[np.ndarray] = None, |
| 73 | + tangent_vectors: Optional[np.ndarray] = None, |
| 74 | + normal_vectors: Optional[np.ndarray] = None, |
| 75 | + inequality_constraints: Optional[np.ndarray] = None, |
| 76 | + ): |
| 77 | + # get locations |
| 78 | + self.fit( |
| 79 | + values=values, |
| 80 | + tangent_vectors=tangent_vectors, |
| 81 | + normal_vectors=normal_vectors, |
| 82 | + inequality_constraints=inequality_constraints, |
| 83 | + ) |
| 84 | + |
| 85 | + return self.evalute_scalar_value(locations) |
| 86 | + |
| 87 | + def fit_and_evaluate_gradient( |
| 88 | + self, |
| 89 | + values: Optional[np.ndarray] = None, |
| 90 | + tangent_vectors: Optional[np.ndarray] = None, |
| 91 | + normal_vectors: Optional[np.ndarray] = None, |
| 92 | + inequality_constraints: Optional[np.ndarray] = None, |
| 93 | + ): |
| 94 | + self.fit( |
| 95 | + values=values, |
| 96 | + tangent_vectors=tangent_vectors, |
| 97 | + normal_vectors=normal_vectors, |
| 98 | + inequality_constraints=inequality_constraints, |
| 99 | + ) |
| 100 | + return self.evaluate_gradient(locations) |
0 commit comments