Skip to content

Commit

Permalink
Adding tags to all component types
Browse files Browse the repository at this point in the history
Compatibility to examples in hangar:
1. Adding "add_tag_to_patches" to all component types
2. Checking for underlying_surf.tag in Curved, Rotated , Offset and Mirrored patches

ni != nj (in utilities.py)

Adding tags to 4 paths of swept patch (still need to resolve resolution)

Ingo SweptPatchMultiFace

Change tags to enum
  • Loading branch information
amirmit committed Feb 10, 2024
1 parent 193483a commit 5b59f31
Show file tree
Hide file tree
Showing 12 changed files with 349 additions and 57 deletions.
2 changes: 1 addition & 1 deletion hypervehicle/components/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from .fin import Fin
from .wing import Wing
from .component import Component
from .swept import SweptComponent
from .swept import SweptComponent, SweptComponentMultiFace
from .polygon import Cube, Sphere
from .revolved import RevolvedComponent
from .composite import CompositeComponent
67 changes: 46 additions & 21 deletions hypervehicle/components/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
import multiprocess as mp
from copy import deepcopy
from abc import abstractmethod
from typing import Callable, Union
from hypervehicle.geometry import Vector3
from gdtk.geom.sgrid import StructuredGrid
from hypervehicle.utilities import assign_tags_to_cell
from hypervehicle.utilities import parametricSurfce2stl, parametricSurfce2vtk
from typing import Callable, Union, Optional
from hypervehicle.utilities import PatchTag
from typing import Callable, Union, Optional, Dict
from hypervehicle.geometry import (
CurvedPatch,
RotatedPatch,
Expand Down Expand Up @@ -92,11 +92,12 @@ def analyse(self):
class Component(AbstractComponent):
def __init__(
self,
params: dict = None,
params: Dict = None,
stl_resolution: int = 2,
verbosity: int = 1,
name: str = None,
output_file_type: str = "stl",
patch_name_to_tags: Dict = None,
) -> None:
# Set verbosity
self.verbosity = verbosity
Expand All @@ -106,6 +107,7 @@ def __init__(

# Processed objects
self.patches = {} # Parametric patches (continuous)
self._patch_name_to_tags = patch_name_to_tags or dict()

# VTK Attributes
self.cells = None # Mesh cells
Expand Down Expand Up @@ -268,29 +270,35 @@ def wrapper(key: str, patch):

if "swept" in key:
# Swept fuselage component
res = (
int(stl_resolution / 4)
if "end" in key
else int(stl_resolution / 4) * 4
)
if "end" in key:
res_r = res_s = int(stl_resolution / 4)
elif any(
direction in key for direction in ("north", "south", "east", "west")
):
res_s = int(stl_resolution / 4)
res_r = res_s * 4
else:
res_r = res_s = int(stl_resolution / 4) * 4
flip = True if "1" in key else False
else:
res_r = res_s = res

surface = parametricSurfce2stl(
patch, res, flip_faces=flip, **self._clustering
patch, res_r, res_s, flip_faces=flip, **self._clustering
)

return (key, surface)

# Initialise surfaces and pool
self.surfaces = {}
pool = mp.Pool()

# Submit tasks single (debug mode)
# for a in self.patches.items():
# result = wrapper(a[0], a[1])
# self.surfaces[result[0]] = result[1]

# Submit tasks multi
pool = mp.Pool()
for result in pool.starmap(wrapper, self.patches.items()):
self.surfaces[result[0]] = result[1]

Expand All @@ -310,15 +318,21 @@ def wrapper(key: str, patch):

if "swept" in key:
# Swept fuselage component
res = (
int(stl_resolution / 4)
if "end" in key
else int(stl_resolution / 4) * 4
)
if "end" in key:
res_r = res_s = int(stl_resolution / 4)
elif any(
direction in key for direction in ("north", "south", "east", "west")
):
res_s = int(stl_resolution / 4)
res_r = res_s * 4
else:
res_r = res_s = int(stl_resolution / 4) * 4
flip = True if "1" in key else False
else:
res_r = res_s = res

vertices, cell_ids = parametricSurfce2vtk(
patch, res, flip_faces=flip, **self._clustering
patch, res_r, res_s, flip_faces=flip, **self._clustering
)

# Assign tags to the cells
Expand All @@ -328,14 +342,14 @@ def wrapper(key: str, patch):

# Initialise cells and pool
self.cells = {}
pool = mp.Pool()

# Submit tasks single (debug mode)
# for a in self.patches.items():
# result = wrapper(a[0], a[1])
# self.cells[result[0]] = (result[1], result[2], result[3])

# Submit tasks multi
pool = mp.Pool()
for result in pool.starmap(wrapper, self.patches.items()):
self.cells[result[0]] = (result[1], result[2], result[3])

Expand Down Expand Up @@ -373,10 +387,10 @@ def to_vtk(self, outfile: str = None):
tags = np.empty(0, dtype=int)

# Combine all Cell data
for sss in self.cells.items():
cell_ids = np.concatenate([cell_ids, sss[1][1] + len(vertices)])
vertices = np.concatenate([vertices, sss[1][0]])
tags = np.concatenate([tags, sss[1][2]])
for cell in self.cells.items():
cell_ids = np.concatenate([cell_ids, cell[1][1] + len(vertices)])
vertices = np.concatenate([vertices, cell[1][0]])
tags = np.concatenate([tags, cell[1][2]])

# Generate mesh in VTK format
cell_ids = [("triangle", cell_ids)]
Expand Down Expand Up @@ -417,3 +431,14 @@ def add_clustering_options(

if j_clustering_func:
self._clustering.update({"j_clustering_func": j_clustering_func})

def add_tag_to_patches(self):
if self.patches is None:
raise Exception("component has no patches")

for name, patch in self.patches.items():
# default tag is FREE_STREEM
if name not in self._patch_name_to_tags:
patch.tag = PatchTag.FREE_STREAM
continue
patch.tag = self._patch_name_to_tags[name]
2 changes: 2 additions & 0 deletions hypervehicle/components/composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ def __init__(
stl_resolution: int = 2,
verbosity: int = 1,
name: str = None,
tags: dict = None,
) -> None:
# Initialise base class
super().__init__(
params=None,
stl_resolution=stl_resolution,
verbosity=verbosity,
name=name,
patch_name_to_tags=tags,
)

self.components: List[Component] = []
Expand Down
1 change: 1 addition & 0 deletions hypervehicle/components/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
WING_COMPONENT = "wing"
COMPOSITE_COMPONENT = "composite"
SWEPT_COMPONENT = "swept"
SWEPT_COMPONENT_MULTI_FACE = "swept_multi_face"
REVOLVED_COMPONENT = "revolved"
CUBE = "cube"
SPHERE = "sphere"
10 changes: 9 additions & 1 deletion hypervehicle/components/fin.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def __init__(
stl_resolution: Optional[int] = 2,
verbosity: Optional[int] = 1,
name: Optional[str] = None,
tags: dict = None,
) -> None:
"""Creates a new fin component.
Expand Down Expand Up @@ -108,6 +109,8 @@ def __init__(
name : str, optional
The name tag for the component. The default is None.
tags : dict, optional
tags to be added to each patch (for generating a VTK file)
"""

if LE_wf is None:
Expand Down Expand Up @@ -135,7 +138,9 @@ def __init__(
"offset_function": offset_func,
}

super().__init__(params, stl_resolution, verbosity, name)
super().__init__(
params, stl_resolution, verbosity, name, patch_name_to_tags=tags
)

def generate_patches(self):
# Initialise
Expand Down Expand Up @@ -473,3 +478,6 @@ def generate_patches(self):

# Save patches
self.patches = fin_patch_dict

# Add tags to patches
self.add_tag_to_patches()
22 changes: 20 additions & 2 deletions hypervehicle/components/polygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def __init__(
stl_resolution: int = 2,
verbosity: int = 1,
name: str = None,
tags: dict = None,
) -> None:
"""
Parameters
Expand All @@ -25,13 +26,21 @@ def __init__(
"""
self.a = a
self.centre = centre
super().__init__(stl_resolution=stl_resolution, verbosity=verbosity, name=name)
super().__init__(
stl_resolution=stl_resolution,
verbosity=verbosity,
name=name,
patch_name_to_tags=tags,
)

def generate_patches(self):
faces = ["east", "west", "south", "north", "bottom", "top"]
for face in faces:
self.patches[face] = CubePatch(self.a, self.centre, face)

# Add tags to patches
self.add_tag_to_patches()


class Sphere(Component):
componenttype = SPHERE
Expand All @@ -43,6 +52,7 @@ def __init__(
stl_resolution: int = 2,
verbosity: int = 1,
name: str = None,
tags: dict = None,
) -> None:
"""
Parameters
Expand All @@ -55,10 +65,18 @@ def __init__(
"""
self.r = r
self.centre = centre
super().__init__(stl_resolution=stl_resolution, verbosity=verbosity, name=name)
super().__init__(
stl_resolution=stl_resolution,
verbosity=verbosity,
name=name,
patch_name_to_tags=tags,
)

def generate_patches(self):
faces = ["east", "west", "south", "north", "bottom", "top"]

for face in faces:
self.patches[face] = SpherePatch(self.r, self.centre, face)

# Add tags to patches
self.add_tag_to_patches()
11 changes: 10 additions & 1 deletion hypervehicle/components/revolved.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def __init__(
stl_resolution: int = 4,
verbosity: int = 1,
name: str = None,
tags: dict = None,
) -> None:
"""Create a revolved component.
Expand All @@ -22,10 +23,18 @@ def __init__(
A line to be revolved about the primary axis.
"""
self.revolve_line = revolve_line
super().__init__(stl_resolution=stl_resolution, verbosity=verbosity, name=name)
super().__init__(
stl_resolution=stl_resolution,
verbosity=verbosity,
name=name,
patch_name_to_tags=tags,
)

def generate_patches(self):
for i in range(4):
self.patches[f"revolved_fuse_{i}"] = RevolvedPatch(
self.revolve_line, i * np.pi / 2, (i + 1) * np.pi / 2
)

# Add tags to patches
self.add_tag_to_patches()
Loading

0 comments on commit 5b59f31

Please sign in to comment.