-
Notifications
You must be signed in to change notification settings - Fork 1
/
maxpy.py
427 lines (377 loc) · 14.5 KB
/
maxpy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
import datetime as t
import os
class Maxwell():
def __init__(self, fileName, project, design, gds, clear=False, mode = 'Electrostatic'):
self.gds = gds # name of gds file to import
self.mode = mode # mode for maxwell simulation, defaults to 'Electrostatic' for capacitance measurements
self.fileName = fileName # file to write script to
self.project = project # name of project
self.design = design # name of design
self.signalCount = 1 # counter for signal naming
self.signals = {} # dict going from layer to all signals in that layer
self.allSignals = [] # list for convenience of every signal
self.layerGuide={
5: ('0', '250nm'), #format~ layer: (height, thickness), follows LL standards
18: ('0', '250nm'),
11: ('250nm', '250nm'),
30: ('250nm', '1.6um'),
31: ('1850nm', '250nm')
}
if clear:
open(self.fileName, 'w').close() # clears file
def initialize(self):
"""
writes the header of the written script
info for what and when it was written
also initial imports and object creation that maxwell uses
"""
with open(self.fileName, 'a') as script:
td = str(t.datetime.now()) # gets current time info
time = td[td.index(' ')+1 : td.index('.')] # picks out the current time
date = td[ : td.index(' ')] # picks out the current data
# necessary imports and header prints
script.write(f'''
# ----------------------------------------------
# Script written by maxpy Version 1.3
# {time} {date}
# ----------------------------------------------\n\n''')
script.write('import ScriptEnv\n')
script.write('ScriptEnv.Initialize("Ansoft.ElectronicsDesktop")\n')
script.write('oDesktop.RestoreWindow()\n')
script.write('oProject = oDesktop.NewProject()\n')
script.write('oProject.Rename("' + os.getcwd().replace('\\', '\\\\') + '\\\\' + self.project + '.aedt", True)\n')
script.write(f'oProject.InsertDesign("Maxwell 3D", "{self.design}", "{self.mode}", "")\n')
script.write(f'oDesign = oProject.SetActiveDesign("{self.design}")\n')
script.write('oEditor = oDesign.SetActiveEditor("3D Modeler")\n')
def addLayers(self, layerDict):
"""
tells maxwell whichi layers from the input you would like to import
layerDict - dictionary of form {layer: elements} where elements is the number of discrete elements in each layer
"""
for layer in layerDict:
for signal in range(layerDict[layer]):
self.allSignals.append(f'Signal{str(layer)}_{str(self.signalCount)}')
if layer in self.signals:
self.signals[layer].append(f'Signal{str(layer)}_{str(self.signalCount)}')
else:
self.signals[layer] = [f'Signal{str(layer)}_{str(self.signalCount)}']
self.signalCount += 1
def addLayerInfo(self, info):
"""
used for adding information to the layer guide for layers that may not be standard
info - list of new layers to add in format: (layer (int), height (str), thickness (str))
"""
if type(info) == tuple:
info = [info]
for layer, height, thickness in info:
self.layerGuide[layer] = (height, thickness)
def importGDS(self):
"""
imports desired layers into the maxwell 3D design
"""
layerInput = ''
orderInput = '['
counter = 0
# formats all structures as necessary
for layer in self.signals:
nextInput = f'''
[
"NAME:LayerMapInfo",
"LayerNum:=" , {str(layer)},
"DestLayer:=" , "Signal{str(layer)}",
"layer_type:=" , "signal"
]
'''
layerInput += ', ' + nextInput
nextOrder = f'''"entry:=", ["order:=", {str(counter)}, "layer:=", "Signal{str(layer)}"], '''
orderInput += nextOrder
counter += 1
# final small touches
layerInput += ']'
orderInput = orderInput.rstrip(', ')
orderInput += ']'
#now puts into string with corrent format we constructed earlier
importInfo = f'''[
"NAME:options",
"FileName:=" , "''' + self.gds.replace('\\', '\\\\') + f'''",
"FlattenHierarchy:=" , True,
"ImportMethod:=" , 1,
[
"NAME:LayerMap"{layerInput},
"OrderMap:=" , {orderInput}
]'''
# now write to our file
with open(self.fileName, 'a') as script:
script.write('oEditor.ImportGDSII(\n ' + importInfo + ')\n')
# move the model to the origin, thickens, assigns materials, and moves to correct height
self.zero()
for layer in self.signals:
self.thicken(self.signals[layer], self.layerGuide[layer][1])
self.move(self.signals[layer], ['0','0', self.layerGuide[layer][0]])
self.assignMaterial(self.allSignals, 'Perfect Conductor')
def zero(self):
"""
moves the model to the origin
"""
with open(self.fileName, 'a') as script:
script.write('\nbound_box = oEditor.GetModelBoundingBox()\n')
script.write(f'''oEditor.Move(
[
"NAME:Selections",
"Selections:=" , "{','.join(self.allSignals)}",
"NewPartsModelFlag:=" , "Model"
],
[
"NAME:TranslateParameters",
"TranslateVectorX:=" , str(-1*float(bound_box[0])) + 'mm',
"TranslateVectorY:=" , str(-1*float(bound_box[1])) + 'mm',
"TranslateVectorZ:=" , str(-1*float(bound_box[2])) + 'mm'
]) \n''')
script.write(f'''oEditor.CreateBox(
[
"NAME:BoxParameters",
"XPosition:=" , "0",
"YPosition:=" , "0",
"ZPosition:=" , "0",
"XSize:=" , str(float(bound_box[3])-float(bound_box[0])) + 'mm' ,
"YSize:=" , str(float(bound_box[4])-float(bound_box[1])) + 'mm',
"ZSize:=" , "-350um"
],
[
"NAME:Attributes",
"Name:=" , "substrate",
"Flags:=" , "",
"Color:=" , "(143 175 143)",
"Transparency:=" , 0,
"PartCoordinateSystem:=", "Global",
"UDMId:=" , "",
"MaterialValue:=" , "\\"{'silicon'}\\"",
"SurfaceMaterialValue:=", "\\"\\"",
"SolveInside:=" , True,
"IsMaterialEditable:=" , True,
"UseMaterialAppearance:=", False,
"IsLightweight:=" , False
])
\n''')
def thicken(self, signals, thickness):
'''
thickens each of the signals by the desired thickness
'''
with open(self.fileName, 'a') as script:
script.write(f'''oEditor.ThickenSheet(
[
"NAME:Selections",
"Selections:=" , "{','.join(signals)}",
"NewPartsModelFlag:=" , "Model"
],
[
"NAME:SheetThickenParameters",
"Thickness:=" , "{thickness}",
"BothSides:=" , False
])\n''')
def move(self, signals, vector):
"""
moves each of the signals in signals up by vector: both are lists of strings
"""
if ['0']*3 == vector:
return -1
moveInfo = f'''["NAME:Selections",
"Selections:=" , "{','.join(signals)}",
"NewPartsModelFlag:=" , "Model"
],
[
"NAME:TranslateParameters",
"TranslateVectorX:=" , "{vector[0]}",
"TranslateVectorY:=" , "{vector[1]}",
"TranslateVectorZ:=" , "{vector[2]}"
]'''
with open(self.fileName, 'a') as script:
script.write('oEditor.Move(\n ' + moveInfo + ')\n')
def assignMaterial(self, signals, material):
"""
assigns material to every signal in list signals
"""
with open(self.fileName, 'a') as script:
script.write(f'''oEditor.AssignMaterial(
[
"NAME:Selections",
"AllowRegionDependentPartSelectionForPMLCreation:=", True,
"AllowRegionSelectionForPMLCreation:=", True,
"Selections:=" , "{','.join(signals)}"
],
[
"NAME:Attributes",
"MaterialValue:=" , "\\"{material}\\"",
"SolveInside:=" , False,
"IsMaterialEditable:=" , True,
"UseMaterialAppearance:=", False,
"IsLightweight:=" , False
])\n''')
def unite(self, signals):
"""
unites every element in signals
"""
with open(self.fileName, 'a') as script:
script.write(f'''oEditor.Unite(
[
"NAME:Selections",
"Selections:=" , "{','.join(signals)}"
],
[
"NAME:UniteParameters",
"KeepOriginals:=" , False
])
\n''')
def voltage(self, v, signal, name ):
"""
assigns voltage to signal with name for excitations
"""
with open(self.fileName, 'a') as script:
script.write('oModule = oDesign.GetModule("BoundarySetup")\n')
script.write(f'''oModule.AssignVoltage(
[
"NAME:{name}",
"Objects:=" , ["{signal}"],
"Voltage:=" , "{v}",
"CoordinateSystem:=" , ""
])
\n''')
def rename(self, signal, name):
"""
renames one signal with given name
"""
with open(self.fileName, 'a') as script:
script.write(f'''oEditor.ChangeProperty(
[
"NAME:AllTabs",
[
"NAME:Geometry3DAttributeTab",
[
"NAME:PropServers",
"{signal}"
],
[
"NAME:ChangedProps",
[
"NAME:Name",
"Value:=" , "{name}"
]
]
]
])
\n''')
def problemRegion(self, height=300):
"""
makes a problem region with the given height, defaults ot 300
"""
with open(self.fileName, 'a') as script:
script.write(f'''oEditor.CreateRegion(
[
"NAME:RegionParameters",
"+XPaddingType:=" , "Percentage Offset",
"+XPadding:=" , "0",
"-XPaddingType:=" , "Percentage Offset",
"-XPadding:=" , "0",
"+YPaddingType:=" , "Percentage Offset",
"+YPadding:=" , "0",
"-YPaddingType:=" , "Percentage Offset",
"-YPadding:=" , "0",
"+ZPaddingType:=" , "Percentage Offset",
"+ZPadding:=" , "{str(height)}",
"-ZPaddingType:=" , "Percentage Offset",
"-ZPadding:=" , "0"
],
[
"NAME:Attributes",
"Name:=" , "Region",
"Flags:=" , "Wireframe#",
"Color:=" , "(143 175 143)",
"Transparency:=" , 0,
"PartCoordinateSystem:=", "Global",
"UDMId:=" , "",
"MaterialValue:=" , "\\"vacuum\\"",
"SurfaceMaterialValue:=", "\\"\\"",
"SolveInside:=" , True,
"IsMaterialEditable:=" , True,
"UseMaterialAppearance:=", False,
"IsLightweight:=" , False
])
\n''')
def matrix(self, excitations, name):
"""
given a list of excitation names we would like to include, set up the capacitance matrix simulation, naming the matrix with name
"""
entries = ''
for each in excitations:
entries += f''' [
"NAME:MatrixEntry",
"Source:=" , "{each}",
"NumberOfTurns:=" , "1"
],\n'''
with open(self.fileName, 'a') as script:
script.write('oModule = oDesign.GetModule("MaxwellParameterSetup")\n')
script.write(f'''oModule.AssignMatrix(
[
"NAME:{name}",
[
"NAME:MatrixEntry",
''' + entries.rstrip(',\n') + '''
],
[
"NAME:MatrixGroup"
]
])
\n''')
def setup(self, name, minPass=2, maxPass=15, error=2):
"""
does the setup so we can analyze the file
"""
with open(self.fileName, 'a') as script:
script.write('oModule = oDesign.GetModule("AnalysisSetup")\n')
script.write(f'''oModule.InsertSetup("{self.mode}",
[
"NAME:{name}",
"Enabled:=" , True,
"MaximumPasses:=" , {str(maxPass)},
"MinimumPasses:=" , {str(minPass)},
"MinimumConvergedPasses:=", 1,
"PercentRefinement:=" , 30,
"SolveFieldOnly:=" , False,
"PercentError:=" , {str(error)},
"SolveMatrixAtLast:=" , True,
"PercentError:=" , {str(error)},
"UseIterativeSolver:=" , False,
"RelativeResidual:=" , 1E-06,
"NonLinearResidual:=" , 0.001
])
\n''')
def analyze(self, setup, matrix, loc):
with open(self.fileName, 'a') as script:
script.write('oProject.Save()\n')
script.write(f'oDesign.Analyze("{setup}")\n')
script.write(f'oModule = oDesign.GetModule("AnalysisSetup")\n')
script.write(f'''oModule.ExportSolnData("{setup} : LastAdaptive", "{matrix}", False, "", "{loc}")\n''')
class maxParse():
def __init__(self, fileName):
self.fileName = None
self.units = None
self.capacitance = {}
self.coupling = {}
self.update(fileName)
def update(self, fileName):
self.fileName = fileName
with open(fileName, 'r') as output:
data = output.read()
data = data.split('\n')
self.units = data[2][data[2].index(':')+2:]
excitations = data[5].lstrip('\t').split('\t')
for index in range(len(excitations)):
rowcap = data[6+index].lstrip('\t').split('\t')
rowcoup = data[9+len(excitations)+index].lstrip('\t').split('\t')
newcap = {}
newcoup = {}
for col in range(len(excitations)):
newcap[excitations[col]] = rowcap[1+col]
newcoup[excitations[col]] = rowcoup[1+col]
self.capacitance[excitations[index]] = newcap
self.coupling[excitations[index]] = newcoup