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 __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, 3, sim.solver.numberOfIntervals) 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 in sim.variables: if (sim.variables[name].start is not None): value = sim.variables[name].start values.SetValue(1, i, value) elif name in sim.parameters: if sim.parameters[name] is not None: value = sim.parameters[name] values.SetValue(1, i, value) else: continue # 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()
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 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)
import sys from PySimLib.Mat.Mat import Mat from PySimLib.Mat.TextMatrix import TextMatrix space = "\t\t" mat = Mat() mat.Load(sys.argv[1]) matrices = mat.GetMatrices() print( "Size is either 'Rows x Columns' or, for a Textmatrix, the number of strings" ) print("Type", space, "Size", space, "Name") print("----") for key in matrices: if (type(matrices[key]) == TextMatrix): t = "TextMatrix\t" size = matrices[key].GetNumberOfStrings() else: t = "Matrix\t\t" size = str(matrices[key].GetNumberOfRows()) + "x" + str( matrices[key].GetNumberOfColumns()) print(t, size, space, key)