def convertFromDymolaMatFile(matFilename, mtsfFilename=None): ''' Converts a Dymola result file (in mat-format) into the MTSF format. Returns the filename of the new result file ''' # Define file name of result file if mtsfFilename is None: if len(matFilename) >= 4: if matFilename[-4:] == '.mat': resultFileName = matFilename[:-4] + '.mtsf' else: resultFileName = matFilename + '.mtsf' else: resultFileName = matFilename + '.mtsf' else: if '.mtsf' in mtsfFilename: if mtsfFilename[-5:] == '.mtsf': resultFileName = mtsfFilename else: resultFileName = mtsfFilename + '.mtsf' else: resultFileName = mtsfFilename + '.mtsf' from ..DymolaMat import DymolaMat # Load mat-file res = DymolaMat.Results(matFilename) # Define basic structure of result file variable = collections.OrderedDict() # Search for aliases sortedVariables = [(i, res._dataInfo[i, 0], abs(res._dataInfo[i, 1])) for i in xrange(len(res._name))] sortedVariables.sort(key=itemgetter(2)) sortedVariables.sort(key=itemgetter(1)) aliasName = [None for i in xrange(len(res._name))] for i, var in enumerate(sortedVariables): index = var[0] alias = None j = i while j > 0: if sortedVariables[j - 1][1] != var[1] or sortedVariables[ j - 1][2] != var[2]: break else: j -= 1 if j < i: alias = res._name[sortedVariables[j][0]] aliasName[index] = alias dataIndexFixed = [] dataIndexContinuous = [] categoryIndex = pyMtsf.StandardCategoryNames.index( pyMtsf.CategoryMapping['Real']) for index, variableName in enumerate(res._name): aliasNegated = False if res._dataInfo[index, 0] == 1: variability = 'fixed' seriesIndex = 0 # Fixed else: variability = 'continuous' seriesIndex = 1 # Continuous if res._dataInfo[index, 1] < 0: aliasNegated = True if aliasName[index] is None: if variability == 'fixed': dataIndexFixed.append(abs(res._dataInfo[index, 1]) - 1) else: dataIndexContinuous.append(abs(res._dataInfo[index, 1]) - 1) variable[variableName] = pyMtsf.ScalarModelVariable( res._description[index], 'option', 0, # may be set later variability, seriesIndex, categoryIndex, aliasName[index], aliasNegated) modelVariables = pyMtsf.ModelVariables(variable, MtsfFmi.StandardSeriesForFmi, pyMtsf.StandardCategoryNames) timeData = res.data("Time") modelVariables.allSeries[1].initialRows = len(timeData) # Continuous simpleTypes = [] units = [] enumerations = [] simpleTypes.append( pyMtsf.SimpleType('Real without unit', pyMtsf.DataType["Real"], '', False, -1, '')) # No unit unitList = [(index, unit) for index, unit in enumerate(res._unit)] unitList.sort(key=itemgetter(1)) preUnit = '' for x in unitList: index = x[0] unit = x[1] if unit != '': if preUnit != unit: units.append(pyMtsf.Unit(unit, 1.0, 0.0, 0)) simpleTypes.append( pyMtsf.SimpleType('Real, Unit = ' + unit, pyMtsf.DataType["Real"], '', False, len(units) - 1, '')) preUnit = unit modelVariables.variable[ res._name[index]].simpleTypeRow = len(simpleTypes) - 1 else: modelVariables.variable[ res._name[index]].simpleTypeRow = len(simpleTypes) - 1 experimentSetup = pyMtsf.ExperimentSetup( startTime=timeData[0], stopTime=timeData[-1], algorithm="", relativeTolerance='', author="", description="", generationDateAndTime=time.strftime("%a, %d %b %Y %H:%M:%S", time.gmtime()), generationTool="Python", machine=os.getenv('COMPUTERNAME'), cpuTime="") modelDescription = pyMtsf.ModelDescription(resultFileName[:-5], '', '', '', '', '', 'structured') # Create result object mtsf = pyMtsf.MTSF(resultFileName, modelDescription, modelVariables, experimentSetup, simpleTypes, units, enumerations) # Write numeric data fixedValues = numpy.ndarray((len(dataIndexFixed, ))) for i, index in enumerate(dataIndexFixed): fixedValues[i] = res._data[0][0, index] continuousValues = numpy.ndarray((len(timeData), len(dataIndexContinuous))) for i, index in enumerate(dataIndexContinuous): continuousValues[:, i] = res._data[1][:, index] mtsf.results.series['Fixed'].category[ pyMtsf.CategoryMapping['Real']].writeData(fixedValues) mtsf.results.series['Continuous'].category[ pyMtsf.CategoryMapping['Real']].writeData(continuousValues) # Close file mtsf.close() return resultFileName
import time import numpy nPoints = 60 BlockSize = 100 # Prepare information from FMU name_fmu_file = u'Examples/fullRobot' (modelDescription, modelVariables, simpleTypes, units, enumerations) = convertFromFmi(name_fmu_file) modelVariables.allSeries[1].initialRows = nPoints * BlockSize # Continuous # Phase 1 of result file generation resultFileName = name_fmu_file + unicode(nPoints) + u'.mtsf' experimentSetup = pyMtsf.ExperimentSetup(startTime=0.0, stopTime=4.78, algorithm="Dassl", relativeTolerance=1e-7, author="", description="Test experiment", generationDateAndTime=time.strftime("%a, %d %b %Y %H:%M:%S", time.gmtime()), generationTool="Python", machine=os.getenv('COMPUTERNAME'), cpuTime="") startTime = time.clock() # Create result object mtsf = pyMtsf.MTSF(resultFileName, modelDescription, modelVariables, experimentSetup, simpleTypes, units, enumerations) # Some aliases realParameter = mtsf.results.series['Fixed'].category[pyMtsf.CategoryMapping['Real']] # integerParameter = mtsf.results.series['Fixed'].category[CategoryMapping['Integer']] booleanParameter = mtsf.results.series['Fixed'].category[pyMtsf.CategoryMapping['Boolean']] realContinuous = mtsf.results.series['Continuous'].category[pyMtsf.CategoryMapping['Real']] realDiscrete = mtsf.results.series['Discrete'].category[pyMtsf.CategoryMapping['Real']] # integerDiscrete = mtsf.results.series['Discrete'].category[CategoryMapping['Integer']] booleanDiscrete = mtsf.results.series['Discrete'].category[pyMtsf.CategoryMapping['Boolean']]