|
| 1 | +from typing import Optional |
| 2 | + |
| 3 | +from . import interpolator_map, InterpolatorType, support_interpolator_map |
| 4 | +from LoopStructural.utils import BoundingBox |
| 5 | +from typing import Optional |
| 6 | +import numpy as np |
| 7 | + |
| 8 | +from ..interpolators.supports import SupportFactory |
| 9 | + |
| 10 | + |
| 11 | +class InterpolatorFactory: |
| 12 | + @staticmethod |
| 13 | + def create_interpolator( |
| 14 | + interpolatortype: str, |
| 15 | + boundingbox: BoundingBox, |
| 16 | + nelements: int, |
| 17 | + element_volume: Optional[float] = None, |
| 18 | + support=None, |
| 19 | + ): |
| 20 | + if interpolatortype == None: |
| 21 | + raise ValueError("No interpolator type specified") |
| 22 | + if boundingbox == None: |
| 23 | + raise ValueError("No bounding box specified") |
| 24 | + if nelements == None: |
| 25 | + raise ValueError("No number of elements specified") |
| 26 | + if type(interpolatortype) == str: |
| 27 | + interpolatortype = InterpolatorType._member_map_[interpolatortype].numerator |
| 28 | + if support is None: |
| 29 | + supporttype = support_interpolator_map[interpolatortype] |
| 30 | + support = SupportFactory.create_support_from_bbox( |
| 31 | + supporttype, |
| 32 | + bounding_box=boundingbox, |
| 33 | + nelements=nelements, |
| 34 | + element_volume=element_volume, |
| 35 | + ) |
| 36 | + return interpolator_map[interpolatortype](support) |
| 37 | + |
| 38 | + @staticmethod |
| 39 | + def from_dict(d): |
| 40 | + d = d.copy() |
| 41 | + interpolator_type = d.pop("type", None) |
| 42 | + if interpolator_type is None: |
| 43 | + raise ValueError("No interpolator type specified") |
| 44 | + return InterpolatorFactory.create_interpolator(interpolator_type, **d) |
| 45 | + |
| 46 | + @staticmethod |
| 47 | + def get_supported_interpolators(): |
| 48 | + return interpolator_map.keys() |
| 49 | + |
| 50 | + @staticmethod |
| 51 | + def create_interpolator_with_data( |
| 52 | + interpolatortype: str, |
| 53 | + boundingbox: BoundingBox, |
| 54 | + nelements: int, |
| 55 | + element_volume: Optional[float] = None, |
| 56 | + support=None, |
| 57 | + value_constraints: Optional[np.ndarray] = None, |
| 58 | + gradient_norm_constraints: Optional[np.ndarray] = None, |
| 59 | + gradient_constraints: Optional[np.ndarray] = None, |
| 60 | + ): |
| 61 | + if interpolatortype == None: |
| 62 | + raise ValueError("No interpolator type specified") |
| 63 | + if boundingbox == None: |
| 64 | + raise ValueError("No bounding box specified") |
| 65 | + if nelements == None: |
| 66 | + raise ValueError("No number of elements specified") |
| 67 | + if type(interpolatortype) == str: |
| 68 | + interpolatortype = InterpolatorType._member_map_[interpolatortype].numerator |
| 69 | + if support is None: |
| 70 | + supporttype = support_interpolator_map[interpolatortype] |
| 71 | + support = SupportFactory.create_support( |
| 72 | + supporttype, boundingbox, nelements, element_volume |
| 73 | + ) |
| 74 | + interpolator = interpolator_map[interpolatortype](support) |
| 75 | + if value_constraints is not None: |
| 76 | + interpolator.add_value_constraints(value_constraints) |
| 77 | + if gradient_norm_constraints is not None: |
| 78 | + interpolator.add_gradient_constraints(gradient_norm_constraints) |
| 79 | + if gradient_constraints is not None: |
| 80 | + interpolator.add_gradient_constraints(gradient_constraints) |
| 81 | + return interpolator |
0 commit comments