def PsimASimulate(self, maxThread, simList) -> []:
res = None
try:
res = PSIM.__private_A_Simulate2(self, 0, maxThread, simList)
except Exception as e:
print(f"Error: {e}")
return res
def __private_A_Simulate2(self, simType, maxThread, simList) -> []:
result = PSIM_result(self)
AllResult = []
schHandles = []
AllThreadHandle = []
AllSimState = []
AllSimStateIndices = []
if not self.IsValid():
result.ErrorMessage = "PSIM object was not loaded."
result.Result = 0;
result.IsError = 1;
AllResult.append(result)
return AllResult;
simCount = len(simList)
SimOpenSchematicFileW = getattr(self.psimHandle, "SimOpenSchematicFileW")
if not SimOpenSchematicFileW:
raise AttributeError(self.VersionErrorMessage)
SimOpenSchematicFileW.restype = ctypes.c_int
Simulation_Init = getattr(self.psimHandle, "SimulationInit2")
if not Simulation_Init:
raise AttributeError(self.VersionErrorMessage)
Simulation_Init.argtypes = [
ctypes.c_int,
ctypes.c_uint32,
ctypes.c_wchar_p,
ctypes.POINTER(ctypes.c_wchar_p),
ctypes.POINTER(ctypes.c_wchar_p),
ctypes.c_int
]
Simulation_Init.restype = ctypes.c_int
for i in range(0, simCount):
error_message = ""
nThreadIndex = 0
result = PSIM_result(self)
schHandle = 0
sd = simList[i]
schFilePath = sd.SchFilePath
graphFilePath = sd.GraphFilePath
simview_flag = sd.SimviewFlag
schHandle = SimOpenSchematicFileW(ctypes.c_wchar_p(schFilePath), ctypes.c_int (1));
if schHandle == 0 :
result.ErrorMessage = "Unable to open schematic file " + schFilePath
result.Result = 0
result.IsError = 1
schHandles.append(schHandle)
AllResult.append(result)
AllThreadHandle.append(nThreadIndex)
AllSimState.append(-1)
else:
result.IsError = 0;
array_type = ctypes.c_wchar_p * len(simList[i].NameValueList)
w_array = array_type()
i2 = 0
for item in simList[i].NameValueList:
w_array[i2] = ctypes.c_wchar_p(str(item))
i2 += 1
NameValList_size = len(simList[i].NameValueList)
simview_flag = 1
if(simType == 0):
dwOptions = simview_flag
pszError = ctypes.c_wchar_p()
nThreadIndex = Simulation_Init(schHandle, ctypes.c_uint32(dwOptions), ctypes.c_wchar_p(graphFilePath), ctypes.byref(pszError), w_array, NameValList_size)
error_message = pszError.value
if (nThreadIndex == 0):
if error_message:
result.ErrorMessage = "Simulation failed for file: " + schFilePath + "\n" + error_message
else:
result.ErrorMessage = "Simulation failed for file: " + schFilePath
result.Result = 0;
result.IsError = 1;
schHandles.append(schHandle)
AllResult.append(result)
AllThreadHandle.append(nThreadIndex)
AllSimState.append(-1)
schHandles.append(schHandle)
AllResult.append(result)
AllThreadHandle.append(nThreadIndex)
AllSimState.append(0)
AllSimStateIndices.append(i)
Thread_Simulation_Do2 = getattr(self.psimHandle, "Thread_Simulation_Do2")
if not Thread_Simulation_Do2:
raise AttributeError(self.VersionErrorMessage)
GetSimulationResult_wait = getattr(self.psimHandle, "GetSimulationResult_wait")
if not GetSimulationResult_wait:
raise AttributeError(self.VersionErrorMessage)
GetSimulationResult_wait.restype = ctypes.c_int
Get_SimErrorMessages = getattr(self.psimHandle, "Get_SimErrorMessages")
if not Get_SimErrorMessages:
raise AttributeError(self.VersionErrorMessage)
Get_SimErrorMessages.argtypes = [
ctypes.c_int,
ctypes.POINTER(ctypes.c_int),
ctypes.POINTER(ctypes.c_int),
ctypes.POINTER(ctypes.c_wchar)
]
Get_SimErrorMessages.restype = ctypes.c_int
GetSimulationGraphRef = getattr(self.psimHandle, "GetSimulationGraphRef")
if not GetSimulationGraphRef:
raise AttributeError(self.VersionErrorMessage)
GetSimulationGraphRef.restype = ctypes.c_int
Graph_GetColCount = getattr(self.psimHandle, "Graph_GetColCount")
if not Graph_GetColCount:
raise AttributeError(self.VersionErrorMessage)
Graph_GetRowCount = getattr(self.psimHandle, "Graph_GetRowCount")
if not Graph_GetRowCount:
raise AttributeError(self.VersionErrorMessage)
Graph_GetColInfo = getattr(self.psimHandle, "Graph_GetColInfo")
if not Graph_GetColInfo:
raise AttributeError(self.VersionErrorMessage)
Graph_GetColCount.restype = ctypes.c_int
Graph_GetRowCount.restype = ctypes.c_int
Graph_GetColInfo.argtypes = [ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.POINTER(ctypes.POINTER(ctypes.c_wchar)), ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.POINTER(ctypes.c_double)), ctypes.POINTER(ctypes.POINTER(ctypes.c_byte))]
Graph_GetColInfo.restype = ctypes.c_int
if((maxThread <= 0) or (maxThread > simCount)):
maxThread = simCount
runningCount = 0
EverythingRunning = False
while True:
i3 = 0
i3_count = len(AllSimStateIndices)
if(i3_count == 0):
break
while( (EverythingRunning == False) and (runningCount < maxThread) and (i3 < i3_count)):
i3_index = AllSimStateIndices[i3];
nState = AllSimState[i3_index]
if(nState == 0):
nThreadIndex = AllThreadHandle[i3_index]
if( nThreadIndex > 0):
Thread_Simulation_Do2(nThreadIndex, 1)
AllSimState[i3_index] = 1
runningCount += 1;
i3 += 1;
if(i3 == i3_count):
EverythingRunning = True
i3 = 0
while(i3 < i3_count):
i3_index = AllSimStateIndices[i3];
nState = AllSimState[i3_index]
if(nState == 1):
nThreadIndex = AllThreadHandle[i3_index]
if( nThreadIndex > 0):
i3_res = GetSimulationResult_wait(nThreadIndex, 100)
if (i3_res != 4):
AllSimState[i3_index] = 2
result = AllResult[i3_index]
if(i3_res == 0):
result.Result = i3_res;
result.IsError = 1
nErrorCount = ctypes.c_int(0)
nErrorSize = ctypes.c_int(0)
ret = Get_SimErrorMessages(ctypes.c_int(nThreadIndex), ctypes.byref(nErrorCount), ctypes.byref(nErrorSize), None)
if((nErrorCount.value > 0) and (nErrorSize.value > 0)):
ptrErrors = ctypes.create_unicode_buffer(nErrorSize.value+100)
ret = Get_SimErrorMessages(ctypes.c_int(nThreadIndex), ctypes.byref(nErrorCount), ctypes.byref(nErrorSize), ptrErrors)
result.ErrorMessage += ptrErrors.value
elif (i3_res == 1):
result.Result = i3_res;
result.IsError = 0
nFlag = 0;
pnRows = ctypes.c_int(0)
pnIncDec = ctypes.c_int(0)
if((simType == 0) or (simType == 11)):
grf = GetSimulationGraphRef(ctypes.c_int(nThreadIndex))
elif(simType == 12):
grf = PSIM.__private_OpenGraphFile(self, graphFilePath)
if(grf > 0):
result.graphHandle = grf
numberOfCol = Graph_GetColCount(ctypes.c_int(grf))
numberOfRows = Graph_GetRowCount(ctypes.c_int(grf))
for col in range(0 , numberOfCol):
ppValues1_B = (ctypes.c_double * numberOfRows)()
ppValidState1_B = (ctypes.c_byte * numberOfRows)()
ppValues = ctypes.cast(ppValues1_B, ctypes.POINTER(ctypes.c_double))
ppValidState = ctypes.cast(ppValidState1_B, ctypes.POINTER(ctypes.c_byte))
ptrName1 = (ctypes.c_wchar * 100)()
ptrName = ctypes.cast(ptrName1, ctypes.POINTER(ctypes.c_wchar))
b = Graph_GetColInfo(ctypes.c_int(grf), ctypes.c_int (0), ctypes.c_int(col), ctypes.byref(ptrName), ctypes.byref(pnRows), ctypes.byref(pnIncDec), ctypes.byref(ppValues), ctypes.byref(ppValidState))
ptrName3 = ctypes.cast(ptrName, ctypes.POINTER(ctypes.c_wchar * 100))
crv = PSIM_curve(pnRows.value, ptrName3.contents.value, ppValues, ppValidState)
result.Graph.add_curve(crv)
AllResult[i3_index] = result
AllSimStateIndices.pop(i3)
i3_count -= 1
runningCount -= 1
break
CloseSchematicFile = getattr(self.psimHandle, "SimCloseSchematicFile")
if not CloseSchematicFile:
raise AttributeError(self.VersionErrorMessage)
CloseSchematicFile.restype = ctypes.c_int
nCount = len(schHandles)
for i in range(0, nCount):
schHandle = schHandles[i]
if(schHandle > 0):
n1 = CloseSchematicFile(schHandle)
return AllResult;