-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAedtProxy.py
182 lines (142 loc) · 5.2 KB
/
AedtProxy.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
#
# Copyright ANSYS, Inc. Unauthorized use, distribution, or duplication is prohibited.
#
import argparse
import numpy as np
import os
import pprint
import sys
import time
# Include paths necessary to find SCP library
if sys.platform.startswith("win"):
for p in os.environ["PYTHON_DLL_PATH"].split(os.pathsep):
os.add_dll_directory(p)
# Import SCP library
from pyExt import SystemCouplingParticipant as sysc
# Inclue paths necessary to find AEDT
aedtPath = os.environ["ANSYSEM_ROOT242"]
sys.path.append(aedtPath)
sys.path.append(os.path.join(aedtPath, "PythonFiles", "DesktopPlugin"))
# Import modules necessary to control AEDT
import ScriptEnv
import AedtRunner
# Parse through arguments
parser = argparse.ArgumentParser()
parser.add_argument("--schost", type=str, default="")
parser.add_argument("--scport", type=int, default=0)
parser.add_argument("--scname", type=str, default="")
parser.add_argument("--screstart", type=str, default="")
parser.add_argument("--gui", default=False, action="store_true")
parser.add_argument("--printsol", default=False, action="store_true")
""" Parse input arguments. """
args = parser.parse_args()
buildInfo = "Aedt Proxy"
nodeIds = dict()
nodeCoords = dict()
solutionData = dict()
def solve(oProject, currTime):
AedtRunner.run(oProject, nodeCoords, solutionData, currTime)
def init():
"""Start AEDT, read and fill in point coordinates and initial values."""
ScriptEnv.Initialize("Ansoft.ElectronicsDesktop", NG=not args.gui)
def fillRegion(regionName):
fileName = f"{regionName}.pts"
assert os.path.isfile(fileName)
ids = list()
coords = list()
with open(fileName) as inputFile:
for index, line in enumerate(inputFile.readlines()):
xStr, yStr, zStr = line.split(" ")
x, y, z = float(xStr), float(yStr), float(zStr)
ids.append(index)
coords.append([x, y, z])
nodeIds[regionName] = np.array(ids)
nodeCoords[regionName] = np.array(coords)
solutionData[regionName] = dict()
solutionData[regionName]["Loss Density"] = np.array([0.0] * len(ids))
solutionData[regionName]["Temperature"] = np.array([300.0] * len(ids))
fillRegion("Die1")
fillRegion("Die2")
def openPrj():
oDesktop.RestoreWindow()
oDesktop.OpenProject("Tee.aedt")
oProject = oDesktop.SetActiveProject("Tee")
return oProject
def shutdown():
ScriptEnv.Shutdown()
def getPointCloud(regionName):
return sysc.PointCloud(
sysc.OutputIntegerData(nodeIds[regionName]),
sysc.OutputVectorData(nodeCoords[regionName]),
)
def getOutputScalar(regionName, variableName):
return sysc.OutputScalarData(solutionData[regionName][variableName])
def getInputScalar(regionName, variableName):
return sysc.InputScalarData(solutionData[regionName][variableName])
def getRestartPoint():
return "1"
def getSystemCoupling(participantInfo):
try:
sc = sysc.SystemCoupling(participantInfo)
print("Connected to System Coupling.")
return sc
except Exception as e:
print(f"ERROR: {e}. Shutting down AEDT...")
print("done. Exiting...")
sys.exit(1)
partInfo = sysc.ParticipantInfo(args.schost, args.scport, args.scname, buildInfo)
# alternatively, set it to "batch.log" to display printouts from that file
partInfo.transcriptFilename = f"{args.scname}.stdout"
sc = getSystemCoupling(partInfo)
exitCode = 0
try:
sc.registerPointCloudAccess(getPointCloud)
sc.registerOutputScalarDataAccess(getOutputScalar)
sc.registerInputScalarDataAccess(getInputScalar)
sc.registerRestartPointCreation(getRestartPoint)
startTime = time.time()
init()
oProject = openPrj()
# perform initial solve to get initial values
print("Performing initial HFSS solve...")
solve(oProject, currTime=0.0)
print(f"Initialized HFSS. Time = {time.time() - startTime} [s]")
print(solutionData)
startTime = time.time()
print("Initializing the coupled analysis...")
sc.initializeAnalysis()
print(f"Initialized System Coupling. Time = {time.time() - startTime} [s]")
while sc.doTimeStep():
ts = sc.getCurrentTimeStep()
print(
f" Time step. Start time = {ts.startTime} [s], time step size = {ts.timeStepSize} [s]"
)
iter = 1
while sc.doIteration():
print(f" Iteration {iter}")
sc.updateInputs()
print(" Updated inputs. Solving HFSS...")
startTime = time.time()
solve(oProject, currTime=ts.startTime + ts.timeStepSize)
print(f" Solved HFSS. Time = {time.time() - startTime} [s]")
if args.printsol:
pprint.pprint(solutionData)
sc.updateOutputs(sysc.Complete)
print(" Updated outputs.")
iter += 1
print(f"Saving AEDT project...")
oProject.Save()
print(f"Disconnecting...")
sc.disconnect()
print("Shutdown.")
except Exception as e:
print(f"ERROR: {e}.")
try:
sc.fatalError(str(e))
finally:
exitCode = 1
finally:
# make sure to always shut down AEDT
shutdown()
print("SUCCESS! Exiting...")
sys.exit(exitCode)