def ReadInit(this, mdl): this.__CheckIfModelIsCompiled(mdl) initMat = Mat() initMat.Load(this.__GetInitFilePath(mdl)) # read parameters varTypeFilter = { 1, # parameters } parameters = this.__ReadVarsFromMat(initMat.GetMatrix("initialName"), initMat.GetMatrix("initialValue"), varTypeFilter) for name in parameters: mdl.parameters[name] = parameters[name].start # read variables varTypeFilter = { 2, # state variable 6, # auxiliary variable } mdl.variables = this.__ReadVarsFromMat(initMat.GetMatrix("initialName"), initMat.GetMatrix("initialValue"), varTypeFilter) # read experiment values experiment = initMat.GetMatrix("experiment") mdl.startTime = experiment.GetValue(0, 0) mdl.stopTime = experiment.GetValue(0, 1) mdl.solver = this.__ReverseMapSolver(experiment.GetValue(0, 6)) # sim.solver.stepSize = TODO: ??? mdl.solver.tolerance = experiment.GetValue(0, 4)
def ReadResult(this, sim): from PySimLib.Mat.Mat import Mat from PySimLib.SimulationResult import SimulationResult mdl = sim.GetModel() result = {} m_res = Mat() m_res.Load(this._GetSimResultFilePath(sim)) # dsres matrices are all transposed-.- names = m_res.GetMatrix("name") names.Transpose() dataInfo = m_res.GetMatrix("dataInfo") dataInfo.Transpose() for i in range(0, names.GetNumberOfStrings()): if(i == 0 and dataInfo.GetValue(0, i) == 0): dataMatrixIndex = 2 # hm... this is not necessarily the case... we need the biggest abscissa else: dataMatrixIndex = dataInfo.GetValue(0, i) dataMatrix = m_res.GetMatrix("data_" + str(dataMatrixIndex)) k = dataInfo.GetValue(1, i) col = abs(k) - 1 if(k > 0): sign = 1 else: sign = -1 currentVar = [] for j in range(0, dataMatrix.GetNumberOfColumns()): currentVar.append(sign * dataMatrix.GetValue(j, col)) if(names.GetString(i) == "Time"): result["time"] = currentVar else: result[names.GetString(i)] = currentVar this._SetDerivedValuesFromSimulationResults(sim, result) return SimulationResult(result)
def ReadResult(this, sim): from PySimLib.SimulationResult import SimulationResult mdl = sim.GetModel() result = {} m_res = Mat() m_res.Load(this.__GetSimResultFilePath(sim)) for key in m_res.GetMatrices(): m = m_res.GetMatrix(key) data = [] for y in range(0, m.GetNumberOfRows()): data.append(m.GetValue(0, y)) result[key] = data this._SetDerivedValuesFromSimulationResults(sim, result) return SimulationResult(result)
def __WriteInit(this, sim): from PySimLib.Mat.OutputStream import OutputStream from PySimLib.Mat.MatrixTypeEvaluator import TYPE_INT32 mdl = sim.GetModel() mat = Mat() mat.Load(this.__GetInitFilePath(mdl)) # set experiment values experiment = mat.GetMatrix("experiment") experiment.SetValue(0, 0, sim.startTime) experiment.SetValue(0, 1, sim.stopTime) experiment.SetValue(0, 4, sim.solver.tolerance) experiment.SetValue(0, 6, this.__MapSolver(sim.solver)) # TODO: STEPSIZE # set variable start values names = mat.GetMatrix("initialName") values = mat.GetMatrix("initialValue") for i in range(0, names.GetNumberOfStrings()): name = names.GetString(i) if(name not in sim.variables): continue if(sim.variables[name].start is not None): value = sim.variables[name].start values.SetValue(1, i, value) # write output file = open(this.__GetSimInitFilePath(sim), "wb") stream = OutputStream(file) # mat.Write(stream); # we need to set precision values for the matrices or dymola wont accept the input settings = mat.GetMatrix("settings") settings.SetDesiredOutputPrecision(TYPE_INT32) mat.GetMatrix("initialDescription").SetString(0, "Dymola") # we need to write the matrices in the exact order or dymola can't read the file mat.GetMatrix("Aclass").Write("Aclass", stream) mat.GetMatrix("experiment").Write("experiment", stream) mat.GetMatrix("method").Write("method", stream) settings.Write("settings", stream) mat.GetMatrix("initialName").Write("initialName", stream) mat.GetMatrix("initialValue").Write("initialValue", stream) mat.GetMatrix("initialDescription").Write("initialDescription", stream) file.close()
import sys from PySimLib.Mat.Mat import Mat from PySimLib.Mat.TextMatrix import TextMatrix space = "\t\t" mat = Mat() mat.Load(sys.argv[1]) m = mat.GetMatrix(sys.argv[2]) if (type(m) == TextMatrix): t = 1 print("Type: ", "TextMatrix") print("Number of strings: ", m.GetNumberOfStrings()) else: t = 0 print("Type: ", "Matrix") print("Size ('Rows'x'Columns'): ", str(m.GetNumberOfRows()) + "x" + str(m.GetNumberOfColumns())) print("----") if (t == 1): for i in range(0, m.GetNumberOfStrings()): print(m.GetString(i)) else: for y in range(0, m.GetNumberOfRows()): line = "" for x in range(0, m.GetNumberOfColumns()): line += str(m.GetValue(x, y)) + ", " print(line)