def PsimSimulate(self, schFilePath_or_Obj, graphFilePath, **kwargs) -> PSIM_result:
res = None
try:
simview_flag = 0
NameValueList, simview_flag = PSIM.__private_kwargs_to_List(self, simview_flag, **kwargs)
res = PSIM.__private_Simulate2(self, 0, schFilePath_or_Obj, graphFilePath, simview_flag, NameValueList)
except Exception as e:
print(f"Error: {e}")
return res
def __private_kwargs_to_List(self, simview_flag, **kwargs):
res = None
try:
NameValueList = []
for key, value in kwargs.items():
if key.lower() == "simview":
try:
if "." in str(value):
i1 = int(float(value))
else:
i1 = int(value)
except (ValueError, TypeError) as e:
i1 = 1
if( i1 == -1):
simview_flag = 1 + 4
elif ( i1 == 0):
simview_flag = 1
else:
simview_flag = 0
else:
NameValueList.append(key)
NameValueList.append(str(value))
except Exception as e:
print(f"Error: {e}")
return NameValueList, simview_flag
def __private_Simulate2(self, simType, schFilePath_or_Obj, graphFilePath, simview_flag, NameValueList) -> PSIM_result:
result = PSIM_result(self)
try:
if not self.IsValid():
result.ErrorMessage = "PSIM object was not loaded."
result.Result = 0;
result.IsError = 1;
return result;
error_message = ""
sch_closeFile = False
schHandle = 0
schFilePath = ""
if isinstance(schFilePath_or_Obj, PSIM_schematic):
schHandle = schFilePath_or_Obj.value
elif isinstance(schFilePath_or_Obj, str):
schFilePath = schFilePath_or_Obj
SimOpenSchematicFileW = getattr(self.psimHandle, "SimOpenSchematicFileW")
if not SimOpenSchematicFileW:
raise AttributeError(self.VersionErrorMessage)
SimOpenSchematicFileW.restype = ctypes.c_int
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;
return result;
sch_closeFile =True
elif schFilePath_or_Obj is None:
print("in Simulate function, schFilePath_or_Obj is None")
else:
print("in Simulate function, schFilePath_or_Obj is of an unknown type")
array_type = ctypes.c_wchar_p * len(NameValueList)
w_array = array_type()
i = 0
for item in NameValueList:
w_array[i] = ctypes.c_wchar_p(str(item))
i = i+1
NameValList_size = len(NameValueList)
Simulation_Init = None
if(simType == 0):
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
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
elif(simType == 11):
Simulation_HyperSpice = getattr(self.psimHandle, "Simulation2_HyperSpice")
if not Simulation_HyperSpice:
raise AttributeError(self.VersionErrorMessage)
Simulation_HyperSpice.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_HyperSpice.restype = ctypes.c_int
dwOptions = simview_flag
pszError = ctypes.c_wchar_p()
nThreadIndex = Simulation_HyperSpice(schHandle, ctypes.c_uint32(dwOptions), ctypes.c_wchar_p(graphFilePath), ctypes.byref(pszError), w_array, NameValList_size)
error_message = pszError.value
elif(simType == 12):
Simulation_LTSpice = getattr(self.psimHandle, "Simulation2_LTSpice")
if not Simulation_LTSpice:
raise AttributeError(self.VersionErrorMessage)
Simulation_LTSpice.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,
ctypes.POINTER(ctypes.c_wchar_p)
]
Simulation_LTSpice.restype = ctypes.c_int
dwOptions = simview_flag
pszError = ctypes.c_wchar_p()
pszOutputFilePath = ctypes.c_wchar_p()
sim_result = Simulation_LTSpice(schHandle, ctypes.c_uint32(dwOptions), ctypes.c_wchar_p(graphFilePath), ctypes.byref(pszError), w_array, NameValList_size, ctypes.byref(pszOutputFilePath))
error_message = pszError.value
if(pszOutputFilePath.value):
graphFilePath = pszOutputFilePath.value
if((simType == 0) or (simType == 11)):
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;
return result;
elif(simType == 12):
nThreadIndex = 0;
if(simType == 0):
Thread_Simulation_Do = getattr(self.psimHandle, "Thread_Simulation_Do")
if not Thread_Simulation_Do:
raise AttributeError(self.VersionErrorMessage)
Thread_Simulation_Do(nThreadIndex)
if((simType == 0) or (simType == 11)):
GetSimulationResult = getattr(self.psimHandle, "GetSimulationResult")
if not GetSimulationResult:
raise AttributeError(self.VersionErrorMessage)
GetSimulationResult.restype = ctypes.c_int
sim_result = GetSimulationResult(ctypes.c_int(nThreadIndex))
while sim_result == 4:
sim_result = GetSimulationResult(ctypes.c_int(nThreadIndex))
result.Result = sim_result;
if(sim_result == 1):
nFlag = 0;
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
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)
result.ErrorMessage = error_message
result.IsError = 0
else:
result.IsError = 1
if(error_message and (len(error_message) > 0)):
result.ErrorMessage = error_message + "\n"
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
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
if sch_closeFile:
CloseSchematicFile = getattr(self.psimHandle, "SimCloseSchematicFile")
if not CloseSchematicFile:
raise AttributeError(self.VersionErrorMessage)
CloseSchematicFile.restype = ctypes.c_int
res = CloseSchematicFile(schHandle);
except Exception as e:
print(f"Error: {e}")
result.IsError = 1
result.Result = 0
result.ErrorMessage += f"Error: {e}"
return result;