2021年5月5日 星期三

逐個激發port並記錄每個面的近場到單一pickle檔案

# coding=UTF-8
import pickle
import ScriptEnv
import time

ScriptEnv.Initialize("Ansoft.ElectronicsDesktop")
oDesktop.RestoreWindow()
oDesktop.ClearMessages("", "", 2)
oProject = oDesktop.GetActiveProject()
oDesign = oProject.GetActiveDesign()
oEditor = oDesign.SetActiveEditor("3D Modeler")


class PortIterator():
def __init__(self):
oModule = oDesign.GetModule("BoundarySetup")
self.ports = [i.replace(':1', '') for i in oModule.GetExcitations()[::2]]
self.max_num = len(self.ports)
self.index = -1

def __iter__(self):
return self

def next(self):
oModule = oDesign.GetModule("Solutions")
self.index += 1
y = []
if self.index == self.max_num:
raise StopIteration

for i in range(self.max_num):
if i == self.index:
y.append(["Name:=", self.ports[i], "Magnitude:=", "1W", "Phase:=", "0deg"])
else:
y.append(["Name:=", self.ports[i], "Magnitude:=", "0W", "Phase:=", "0deg"])

oModule.EditSources([["IncludePortPostProcessing:=", False, "SpecifySystemPower:=", False], ] + y)
return self.ports[self.index]


def getSheets():
if oDesktop.GetVersion() == '2020.1.0':
seg_string = '{}:CreatePolyline:2:Segment{}'
else:
seg_string = '{}:CreatePolyline:1:Segment{}'

result = {}
for obj in oEditor.GetObjectsInGroup('Sheets'):
try:
num = oEditor.GetPropertyValue('Geometry3DCmdTab', '{}:CreatePolyline:1'.format(obj), 'Number of curves')
result[obj] = []
for i in range(int(num)):
cs = oEditor.GetPropertyValue('Geometry3DPolylineTab', seg_string.format(obj, i), 'Point1')
result[obj].append(map(float, cs.split(','))[0:2])
result[obj].append(result[obj][0])
except:
pass

for i in result:
result[i] = zip(*result[i])

return result


def getNFRectangle():
result = []
radiation = oDesign.GetChildObject('Radiation')
for i in radiation.GetChildNames():
if oDesign.GetPropertyValue('RadFieldSetupTab', 'RadField:{}'.format(i), 'Type') == 'Rectangle':
result.append(i)
return result


def getNearEH(solution, freq, nf):
oModule = oDesign.GetModule("ReportSetup")
EH = ["NearEX", "NearEY", "NearEZ", "NearHX", "NearHY", "NearHZ"]
try:
arr = oModule.GetSolutionDataPerVariation("Near Fields", solution, ["Context:=", nf], ['Freq:=', [freq]], EH)
except:
AddErrorMessage("無模擬資料可輸出!")
result = {}
result["v"] = list(arr[0].GetSweepValues("_v"))
x = list(arr[0].GetSweepValues("_u"))
result["u"] = x[0:int(len(x) / len(result["v"]))]

for i in EH:
result[i] = [complex(x, y) for x, y in zip(arr[0].GetRealDataValues(i), arr[0].GetImagDataValues(i))]
return result


def outputPickle(solution, freq, pickle_path):
result = {'sheet': getSheets()}

for portname in PortIterator():
result[portname] = {}
AddWarningMessage("激發端口: {}".format(portname))
for nf in getNFRectangle():
result[portname][nf] = getNearEH(solution, freq, nf)

with open(pickle_path, 'wb') as f:
pickle.dump(result, f)
AddInfoMessage("檔案輸出: {}".format(pickle_path))


outputPickle("Setup1 : LastAdaptive", "28GHz", 'd:/demo/data.pickle')

沒有留言:

張貼留言