Skip to content

Commit afcd50d

Browse files
Change ShapeClassification to check for json data
1 parent cb177a3 commit afcd50d

12 files changed

Lines changed: 872 additions & 20 deletions

src/ShapesClassification.py

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from enum import Enum
2+
import json
13
from typing import Any, Tuple, List, Dict
24

35
import gmsh
@@ -7,55 +9,85 @@
79

810
class ShapesClassification:
911
isOpenCase:bool
12+
crossSectionData: Dict
13+
pecs: Dict
14+
dielectrics: Dict
1015

1116

12-
def __init__(self, shapes):
17+
def __init__(self, shapes, jsonFile:str):
1318
gmsh.model.occ.synchronize()
1419

1520
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_")
21+
with open(jsonFile) as f:
22+
jsonData = json.load(f)
23+
self.crossSectionData = jsonData['CrossSection']
24+
self.pecs = self.get_pecs(shapes)
25+
self.dielectrics = self.get_dielectrics(shapes)
26+
self.shieldReference = dict()
1927
self.vacuum = dict()
2028

2129
self.isOpenCase = self.isOpenProblem()
2230

23-
if len(self.open) > 1:
24-
raise ValueError("Only one open region is allowed.")
2531

2632
@staticmethod
2733
def getNumberFromName(entity_name: str, label: str):
2834
ini = entity_name.rindex(label) + len(label)
2935
num = int(entity_name[ini:])
3036
return num
3137

32-
@staticmethod
33-
def get_surfaces_with_label(entity_tags, label: str):
34-
surfaces = dict()
38+
def get_pecs(self, entity_tags):
39+
pecNames = self.__getGeometryNamesByMaterialType('PEC')
40+
pecs = dict()
3541
for s in entity_tags:
36-
name = gmsh.model.get_entity_name(*s)
37-
if s[0] != 2 or label not in name:
42+
name = gmsh.model.get_entity_name(*s).split('/')[-1]
43+
if s[0] != 2 or name not in pecNames:
3844
continue
39-
num = ShapesClassification.getNumberFromName(name, label)
40-
surfaces[num] = [s]
45+
pecs[name] = [s]
4146

42-
return surfaces
47+
return pecs
48+
49+
def get_dielectrics(self, entity_tags):
50+
dielectricNames = self.__getGeometryNamesByMaterialType('Dielectric')
51+
dielectrics = dict()
52+
for s in entity_tags:
53+
name = gmsh.model.get_entity_name(*s).split('/')[-1]
54+
if s[0] != 2 or name not in dielectricNames:
55+
continue
56+
dielectrics[name] = [s]
57+
58+
return dielectrics
59+
60+
def __getGeometryNamesByMaterialType(self, materialType:str) -> List[str]:
61+
names = [
62+
geometry['name']
63+
for geometry in self.crossSectionData
64+
if geometry['material']['type'] == materialType
65+
]
66+
return names
4367

4468
def isOpenProblem(self):
4569
elements = list(chain(self.pecs.values()))
70+
isOpenCase = True
4671
for idx, element in enumerate(elements):
47-
for otheridx, otherElement in enumerate(elements[idx+1:]):
72+
intersectWithAll = True
73+
intersect = []
74+
for otheridx, otherElement in enumerate(elements):
4875
if element != otherElement:
4976
intersect = gmsh.model.occ.intersect(
5077
element,
5178
otherElement,
5279
removeObject=False,
5380
tag=300+otheridx,
5481
removeTool=False
55-
)[0]
56-
if intersect:
57-
return False
58-
return True
82+
)[0]
83+
if len(intersect) == 0:
84+
intersectWithAll = False
85+
else:
86+
isOpenCase = False
87+
if intersectWithAll:
88+
print(element, otherElement)
89+
self.shieldReference = {list(self.pecs.keys())[idx] : element}
90+
return isOpenCase
5991

6092
def removeConductorsFromDielectrics(self):
6193
for num, diel in self.dielectrics.items():

src/mesher.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ def meshFromStep(self, inputFile: str, caseName: str, meshingOptions=None):
4646

4747
gmsh.model.add(caseName)
4848
allShapes = ShapesClassification(
49-
gmsh.model.occ.importShapes(inputFile, highestDimOnly=False)
49+
gmsh.model.occ.importShapes(inputFile, highestDimOnly=False),
50+
inputFile.strip('.step') + '.json'
5051
)
5152

5253
# --- Geometry manipulation ---

test/test_ShapesClassification.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import os
2+
import unittest
3+
import gmsh
4+
import json
5+
6+
from src.ShapesClassification import ShapesClassification
7+
8+
9+
class TestShapesClassification(unittest.TestCase):
10+
11+
@classmethod
12+
def setUpClass(cls):
13+
cls.dirPath = os.path.dirname(os.path.realpath(__file__)) + '/'
14+
cls.testdataPath = cls.dirPath + '/../testData/'
15+
16+
@classmethod
17+
def tearDownClass(cls):
18+
del cls.dirPath
19+
del cls.testdataPath
20+
21+
def setUp(self):
22+
gmsh.initialize()
23+
24+
def tearDown(self):
25+
gmsh.finalize()
26+
27+
def inputFileFromCaseName(self, caseName):
28+
return self.testdataPath + caseName + '/' + caseName + ".step"
29+
30+
def initShapeClassification(self, inputFile:str) -> None:
31+
jsonFile = inputFile.strip('.step') + '.json'
32+
self.shapeClassification = ShapesClassification(
33+
gmsh.model.occ.importShapes(inputFile, highestDimOnly=False),
34+
jsonFile
35+
)
36+
37+
def testDielectricShieldedPairClassification(self) -> None:
38+
case = 'DielectricShieldedPair'
39+
filepath = self.inputFileFromCaseName(case)
40+
self.initShapeClassification(filepath)
41+
expectedShapes = [
42+
(2, 1),(2, 2),(2, 3),(2, 4),(2, 5),
43+
(1, 1),(1, 2),(1, 3),(1, 4),(1, 5),
44+
(0, 1),(0, 1),(0, 2),(0, 2),(0, 3),(0, 3),(0, 4),(0, 4),(0, 5),(0, 5)
45+
]
46+
expectedPecs = {
47+
'RightConductor': [(2,1)],
48+
'ExternalShield': [(2,2)],
49+
'LeftConductor': [(2,3)],
50+
}
51+
expectedDielectrics = {
52+
'RightDielectric': [(2,4)],
53+
'LeftDielectric': [(2,5)],
54+
}
55+
expectedShieldReference = {
56+
'ExternalShield': [(2,2)],
57+
}
58+
self.assertListEqual(self.shapeClassification.allShapes, expectedShapes)
59+
self.assertDictEqual(self.shapeClassification.pecs, expectedPecs)
60+
self.assertDictEqual(self.shapeClassification.dielectrics, expectedDielectrics)
61+
self.assertDictEqual(self.shapeClassification.shieldReference, expectedShieldReference)
62+
self.assertFalse(self.shapeClassification.isOpenCase)
63+
64+
def testDielectricUnshieldedPairClassification(self) -> None:
65+
case = 'DielectricUnshieldedPair'
66+
filepath = self.inputFileFromCaseName(case)
67+
self.initShapeClassification(filepath)
68+
expectedShapes = [
69+
(2, 1),(2, 2),(2, 3),(2, 4),
70+
(1, 1),(1, 2),(1, 3),(1, 4),
71+
(0, 1),(0, 1),(0, 2),(0, 2),(0, 3),(0, 3),(0, 4),(0, 4),
72+
]
73+
expectedPecs = {
74+
'RightConductor': [(2,1)],
75+
'LeftConductor': [(2,2)],
76+
}
77+
expectedDielectrics = {
78+
'RightDielectric': [(2,3)],
79+
'LeftDielectric': [(2,4)],
80+
}
81+
expectedShieldReference = {}
82+
self.assertListEqual(self.shapeClassification.allShapes, expectedShapes)
83+
self.assertDictEqual(self.shapeClassification.pecs, expectedPecs)
84+
self.assertDictEqual(self.shapeClassification.dielectrics, expectedDielectrics)
85+
self.assertDictEqual(self.shapeClassification.shieldReference, expectedShieldReference)
86+
self.assertTrue(self.shapeClassification.isOpenCase)

test/test_mesher.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ def setUpClass(cls):
1919
cls.dirPath = os.path.dirname(os.path.realpath(__file__)) + '/'
2020
cls.testdataPath = cls.dirPath + '/../testData/'
2121

22+
@classmethod
23+
def tearDownClass(cls):
24+
del cls.dirPath
25+
del cls.testdataPath
26+
2227
def setUp(self):
2328
gmsh.initialize()
2429

Binary file not shown.
13.3 KB
Binary file not shown.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"CrossSection":[
3+
{
4+
"name":"ExternalShield",
5+
"material":{
6+
"type":"PEC"
7+
}
8+
},
9+
{
10+
"name":"RightConductor",
11+
"material":{
12+
"type":"PEC"
13+
}
14+
},
15+
{
16+
"name":"LeftConductor",
17+
"material":{
18+
"type":"PEC"
19+
}
20+
},
21+
{
22+
"name":"LeftDielectric",
23+
"material":{
24+
"type":"Dielectric",
25+
"permittivity": 2.0
26+
}
27+
},
28+
{
29+
"name":"RightDielectric",
30+
"material":{
31+
"type":"Dielectric",
32+
"permittivity":2.0
33+
}
34+
}
35+
]
36+
}

0 commit comments

Comments
 (0)