|
| 1 | +from typing import Any, Tuple, List, Dict |
| 2 | + |
| 3 | +import gmsh |
| 4 | +from .BoundingBox import BoundingBox |
| 5 | +from itertools import chain |
| 6 | +import numpy as np |
| 7 | + |
| 8 | +class ShapesClassification: |
| 9 | + isOpenCase:bool |
| 10 | + |
| 11 | + |
| 12 | + def __init__(self, shapes): |
| 13 | + gmsh.model.occ.synchronize() |
| 14 | + |
| 15 | + self.allShapes = shapes |
| 16 | + self.pecs = self.get_surfaces_with_label(shapes, "Conductor_") |
| 17 | + self.dielectrics = self.get_surfaces_with_label(shapes, "Dielectric_") |
| 18 | + self.open = self.get_surfaces_with_label(shapes, "OpenBoundary_") |
| 19 | + self.vacuum = dict() |
| 20 | + |
| 21 | + self.isOpenCase = self.isOpenProblem() |
| 22 | + |
| 23 | + if len(self.open) > 1: |
| 24 | + raise ValueError("Only one open region is allowed.") |
| 25 | + |
| 26 | + @staticmethod |
| 27 | + def getNumberFromName(entity_name: str, label: str): |
| 28 | + ini = entity_name.rindex(label) + len(label) |
| 29 | + num = int(entity_name[ini:]) |
| 30 | + return num |
| 31 | + |
| 32 | + @staticmethod |
| 33 | + def get_surfaces_with_label(entity_tags, label: str): |
| 34 | + surfaces = dict() |
| 35 | + for s in entity_tags: |
| 36 | + name = gmsh.model.get_entity_name(*s) |
| 37 | + if s[0] != 2 or label not in name: |
| 38 | + continue |
| 39 | + num = ShapesClassification.getNumberFromName(name, label) |
| 40 | + surfaces[num] = [s] |
| 41 | + |
| 42 | + return surfaces |
| 43 | + |
| 44 | + def isOpenProblem(self): |
| 45 | + elements = list(chain(self.pecs.values())) |
| 46 | + for idx, element in enumerate(elements): |
| 47 | + for otheridx, otherElement in enumerate(elements[idx+1:]): |
| 48 | + if element != otherElement: |
| 49 | + intersect = gmsh.model.occ.intersect( |
| 50 | + element, |
| 51 | + otherElement, |
| 52 | + removeObject=False, |
| 53 | + tag=300+otheridx, |
| 54 | + removeTool=False |
| 55 | + )[0] |
| 56 | + if intersect: |
| 57 | + return False |
| 58 | + return True |
| 59 | + |
| 60 | + def removeConductorsFromDielectrics(self): |
| 61 | + for num, diel in self.dielectrics.items(): |
| 62 | + pec_surfs = [] |
| 63 | + for num2, pec_surf in self.pecs.items(): |
| 64 | + if num2 == 0 and not self.isOpenCase: |
| 65 | + continue |
| 66 | + pec_surfs.extend(pec_surf) |
| 67 | + self.dielectrics[num] = gmsh.model.occ.cut(diel, pec_surfs, removeTool=False)[0] |
| 68 | + |
| 69 | + gmsh.model.occ.synchronize() |
| 70 | + |
| 71 | + def ensureDielectricsDoNotOverlap(self): |
| 72 | + for n1, diel1 in self.dielectrics.items(): |
| 73 | + others = list( |
| 74 | + chain( |
| 75 | + *[x[1] for x in self.dielectrics.items() if x[0] != n1] |
| 76 | + ) |
| 77 | + ) |
| 78 | + |
| 79 | + if len(others) == 0: |
| 80 | + continue |
| 81 | + |
| 82 | + self.dielectrics[n1] = gmsh.model.occ.cut( |
| 83 | + self.dielectrics[n1], others, removeObject=True, removeTool=False)[0] |
| 84 | + |
| 85 | + gmsh.model.occ.synchronize() |
| 86 | + |
| 87 | + def buildVacuumDomain(self): |
| 88 | + if self.isOpenCase and len(self.open) == 0: |
| 89 | + self.vacuum = self._buildDefaultVacuumDomain() |
| 90 | + elif self.isOpenCase and len(self.open) > 0: |
| 91 | + self.vacuum = self._buildVacuumDomainFromOpenBoundary() |
| 92 | + else: |
| 93 | + self.vacuum = self._buildClosedVacuumDomain() |
| 94 | + return self.vacuum |
| 95 | + |
| 96 | + def _buildVacuumDomainFromOpenBoundary(self) -> Dict[int, List[int]]: |
| 97 | + dom = self.open[0] |
| 98 | + |
| 99 | + surfsToRemove = [] |
| 100 | + for num, surf in self.pecs.items(): |
| 101 | + surfsToRemove.extend(surf) |
| 102 | + |
| 103 | + for _, surf in self.dielectrics.items(): |
| 104 | + surfsToRemove.extend(surf) |
| 105 | + |
| 106 | + dom = gmsh.model.occ.cut( |
| 107 | + dom, surfsToRemove, removeObject=False, removeTool=False)[0] |
| 108 | + gmsh.model.occ.synchronize() |
| 109 | + |
| 110 | + return dict([[0, dom]]) |
| 111 | + |
| 112 | + def _buildClosedVacuumDomain(self) -> Tuple[int, int]: |
| 113 | + dom = self.pecs[0] |
| 114 | + surfsToRemove = [] |
| 115 | + for num, surf in self.pecs.items(): |
| 116 | + if num == 0: |
| 117 | + continue |
| 118 | + surfsToRemove.extend(surf) |
| 119 | + |
| 120 | + for _, surf in self.dielectrics.items(): |
| 121 | + surfsToRemove.extend(surf) |
| 122 | + dom = gmsh.model.occ.cut( |
| 123 | + dom, surfsToRemove, removeObject=False, removeTool=False)[0] |
| 124 | + gmsh.model.occ.synchronize() |
| 125 | + return dict([[0, dom]]) |
| 126 | + |
| 127 | + def _buildDefaultVacuumDomain(self): |
| 128 | + nonVacuumSurfaces = [] |
| 129 | + for _, surf in self.pecs.items(): |
| 130 | + nonVacuumSurfaces.extend(surf) |
| 131 | + for _, surf in self.dielectrics.items(): |
| 132 | + nonVacuumSurfaces.extend(surf) |
| 133 | + |
| 134 | + boundingBox = BoundingBox.getBoundingBoxFromGroup(nonVacuumSurfaces) |
| 135 | + |
| 136 | + |
| 137 | + bbMaxLength = np.max(boundingBox.getLengths()) |
| 138 | + nVOrigin = tuple( |
| 139 | + np.subtract(boundingBox.getCenter(), |
| 140 | + (bbMaxLength, bbMaxLength, 0.0))) |
| 141 | + nearVacuum = [(2, gmsh.model.occ.addRectangle(*nVOrigin, *(bbMaxLength*2.0,)*2))] |
| 142 | + |
| 143 | + farVacuumDiameter = 4.0 * boundingBox.getDiagonal() |
| 144 | + farVacuum = [(2, gmsh.model.occ.addDisk( |
| 145 | + *boundingBox.getCenter(), |
| 146 | + farVacuumDiameter, farVacuumDiameter))] |
| 147 | + |
| 148 | + gmsh.model.occ.synchronize() |
| 149 | + |
| 150 | + farVacuum = gmsh.model.occ.cut( |
| 151 | + farVacuum, nearVacuum, removeObject=True, removeTool=False)[0] |
| 152 | + |
| 153 | + nearVacuum = gmsh.model.occ.cut( |
| 154 | + nearVacuum, nonVacuumSurfaces, removeObject=True, removeTool=False)[0] |
| 155 | + |
| 156 | + # -- Set mesh size for near vacuum region |
| 157 | + bb = BoundingBox( |
| 158 | + gmsh.model.getBoundingBox(2, nearVacuum[0][1])) |
| 159 | + minSide = np.min(np.array([bb.getLengths()[0], bb.getLengths()[1]])) |
| 160 | + |
| 161 | + innerRegion = gmsh.model.getBoundary(nearVacuum, recursive=True) |
| 162 | + gmsh.model.mesh.setSize(innerRegion, minSide / 20) |
| 163 | + |
| 164 | + |
| 165 | + self.open = dict([[0, gmsh.model.getBoundary(farVacuum)]]) |
| 166 | + gmsh.model.occ.synchronize() |
| 167 | + |
| 168 | + return dict([[0, nearVacuum], [1, farVacuum]]) |
| 169 | + |
| 170 | + |
0 commit comments