def execute(command): string = OMPython.execute(command) error = OMPython.execute('''getErrorString()''').strip()[1:-1] re_error = re.compile('error',re.I) if re_error.match(error): raise Exception(error) return string
def compileModel(self): """ This function is needed to load the data into the VariablesBrowser before simulating the model with parameters. """ if len(self.fileName) == 1: if not os.path.isfile(self.fileName[0]): raise FileDoesNotExist("File '" + self.fileName[0] + "' does not exist") # load the OpenModelica Standard library # OMPython.execute("loadModel(Modelica)") OMPython.execute("loadFile(\"" + self.fileName[0] + "\")") # set the working directory in OMC pwd = os.path.abspath('.').replace('\\', '/') workdir = OMPython.execute("cd(\"" + pwd + "\")") # simulate the model simResult = OMPython.execute("simulate(" + self.name + ")") # always print the messages if there are any messages = OMPython.get(simResult, "SimulationResults.messages") if messages != '""': print messages # call getErrorString() to get complete error. errorString = OMPython.execute("getErrorString()") if errorString != '""': print errorString # read the result file self.resFile = OMPython.get(simResult, "SimulationResults.resultFile")
def setInitXmlValues(modelName, varName, value): modelName = modelName + "_init.xml" OMPython.execute("setInitXmlStartValue(\"" + modelName + "\",variableName=\"" + varName + "\",startValue=\"" + value + "\",outputFile=\"temp.xml\")") if os.path.exists(modelName): os.remove(modelName) os.rename("temp.xml", modelName) return
def compile_model(simulate_options): if self.fileName != None: OMPython.execute("loadFile(\"" + self.fileName[0] + "\")") s = self.integrationSettings # set the working directory in OMC pwd = os.path.abspath('.').replace('\\', '/') workdir = OMPython.execute("cd(\"" + pwd + "\")") # prepare the simulate command string if simulate_options != '': simulate_string = "simulate(" + self.name + simulate_options + ")" else: simulate_string = "simulate(" + self.name + ")" # simulate the model sim_results = OMPython.execute(simulate_string) # always print the messages if there are any messages = OMPython.get(sim_results, "SimulationResults.messages") if messages != '""': print messages # call getErrorString() to get complete error. errorString = OMPython.execute("getErrorString()") if errorString != '""': print errorString # rename the OpenModelica result file result_file = OMPython.get(sim_results, 'SimulationResults.resultFile') result_file = (result_file).strip('\"') result_file = os.path.join(result_file) old_file_name = os.path.basename(result_file) old_file_name = old_file_name.strip('\"') file_path = result_file.replace(old_file_name, '').strip() file_path = file_path.strip('\"') file_path = os.path.join(result_file) if self.name + "_" in result_file: if os.path.exists(s.resultFileName): shutil.copy(s.resultFileName, (file_path + "temp.mat")) os.remove(result_file) os.remove(s.resultFileName) os.rename((file_path + "temp.mat"), s.resultFileName) else: os.rename(result_file, s.resultFileName)
def simulate(self): ''' TODO: LOG all command omc ''' tic= timeit.default_timer() objCOMC= comc.CommandOMC() '''Load Modelica library''' OMPython.execute("loadModel(Modelica)") command= objCOMC.loadFile(self.libPath, self.libFile) print '1) Command', command OMPython.execute(command) '''loading the model we want to simulate''' command= objCOMC.loadFile(self.moPath, self.moFile) print '2) Command', command success= OMPython.execute(command) if (success): ''' TODO: parametrized the input values, in case they are needed for the model ''' #command= objCOMC.simulate(self.moModel, self.simOptions, 'vf1=0.1,pm1=0.001') command= objCOMC.simulate(self.moModel, self.simOptions, False) print '3) Command', command result= OMPython.execute(command) print '4) Result', result filename = OMPython.get(result,'SimulationResults.resultFile') print '5) Result file ', filename resultfile= objCOMC.saveResult(filename, self.outPath) toc= timeit.default_timer() print 'Simulation time ', toc- tic '''TODO: study the units of elapsed time ''' return resultfile
def omp_create_results_dict(self, res_filename): """ Creates dictionary of results using result file named "res_filename" """ import OMPython as omp result_dict = {} # read all names of variables from "res_filename" res = omp.execute('readSimulationResultVars("'+res_filename+'")') # create list of variables' names temp_vars_list = omp.get(res, 'SET1.Values')[0].split(',') # delete first and last " symbols vars_list = [variable[1:-1] for variable in temp_vars_list] for key in vars_list: values_list = omp.execute('readSimulationResult("'+res_filename+'", '+key+')') # it is a workaround, because we have troubles with getting variables with # complex names, for example, der(der(mass1.flange_b.s)) (names with brackets) if len(values_list): # delete last value ([:-1]), because it is the same as penultimate result_dict[key] = omp.get(values_list, 'SET2.Set1')[:-1] omp.execute('closeSimulationResultFile()') return result_dict
def omp_compile(self, mo_filename, class_name, simulate_params): """ :param mo_filename: name of mo-file :param class_name: name of class inside mo-file :param simulate_params: parameters for simulation :return: constants.SUCCESS_STATUS if compilation was successful constants.ERROR_STATUS otherwise """ import OMPython as omp log = [] if not omp.execute('loadModel(Modelica)'): omp.execute('getErrorString()') return constants.ERROR_STATUS load_file = 'loadFile("' + mo_filename + '")' if not omp.execute(load_file): logger.error('ERROR: could not load file "'+mo_filename+'"') omp.execute('getErrorString()') return constants.ERROR_STATUS simulate = 'simulate(' + class_name + ', ' + simulate_params + ', outputFormat="plt")' log.append(omp.execute(simulate)) log.append(omp.execute('getErrorString()')) return self.omp_check(log)
#! /bin/env python2 import OMPython OMPython = OMPython.OMCSession() cmds = [ 'loadModel(Modelica)', 'loadModel(SolarTherm)', 'simulate(SolarTherm.Examples.EnergySystem, stopTime=31536000, numberOfIntervals=730000)', #'simulate(SolarTherm.Examples.EnergySystem, stopTime=86400, numberOfIntervals=500)', 'getErrorString()', 'plot({wea.wbus.dni})', #'plot({tnk.E}, xRange={0.0, 86400})', #'plot({tnk.E})', #'plot({spl.p_o1.P, tnk.p.P})', #'plot({spl.p_o1.P, pmp.p_o.P})', #'plot({gen.P_ele})', #'plot({spl.frac, pmp.frac})', ] for c in cmds: ans = OMPython.execute(c) print(ans)
#! /bin/env python2 import OMPython OMPython = OMPython.OMCSession() cmds = [ 'loadModel(Modelica)', 'loadModel(SolarTherm.Examples.SystemSimple)', 'simulate(SolarTherm.Examples.SystemSimple, stopTime=20)', 'getErrorString()', 'plot({sol.port1.P, tank.E, gen.P})', ] for c in cmds: ans = OMPython.execute(c) print(ans)
def main(): # get all tests # license problem, if you don't have ModelManagement license on dymola this won't work. #Trying to find a solution #missingTests = getExamples(OMLIBRARY) for i in simulationTests: if i.endswith('.mos'): simulationTests[simulationTests.index(i)] = i[:i.rfind('.mos')] # generate ref files generateRefFiles(simulationTests) # go one folder up, now I should be in the folder where tests must be put absPath = os.path.abspath(os.path.curdir) logFile = open(log_file, 'w') # select kind of test for each model. OMPython.execute("clear()") command = 'loadModel(Modelica,{"%s"})' % MSLVERSION res = OMPython.execute(command) if not res: print('Loading modelica library failed') return if EXTRALIB: for lib in EXTRALIB: lib = lib.replace('\\', '/') res = OMPython.execute('loadFile("%s")' % lib) if not res: print('Loading library %s failed' % lib) # build a dictionary containing the list of tests to be created and the kind of functionality to be tested #the dictionary TestingType = {} logFile.write('List of models tested with test kind\n') for modelName in simulationTests: # do not run tests, where no ref file exists refFile = '%s/ReferenceFiles/%s.mat' % (absPath, modelName) if not (os.path.exists(refFile)): logFile.write("Skipping %s because there is no ref-file\n" % modelName) continue # do not run tests, that have already been set up mos = modelName + '.mos' if (os.path.exists(mos)): logFile.write("Skipping %s because mos-File already exists\n" % modelName) continue print('Analyzing the current status of %s' % modelName) command = 'simulate(%s)' % modelName #try to simulate res = OMPython.execute(command) resultMess = res['SimulationResults']['messages'] if res['SimulationResults']['resultFile'] <> '""': #if an output file exists we should check results but still have to think on how to do it if resultMess == '""': TestingType[modelName] = Kind.VerifiedSimulation logFile.write('%s : VerifiedSimulation\n' % modelName) continue #else if i simulate with messages else: TestingType[modelName] = Kind.SuppressedSimulation logFile.write('%s : SuppressedSimulation\n' % modelName) continue #if there is no result file else: command = 'buildModel(%s)' % modelName commandtransl = 'translateModel(%s)' % modelName res = OMPython.execute(command) if res['SET1']['Values'][0] <> '"",""': TestingType[modelName] = Kind.SuppressedSimulation logFile.write('%s : SuppressedSimulation\n' % modelName) continue elif OMPython.execute(commandtransl): TestingType[modelName] = Kind.Compilation logFile.write('%s : Compilation\n' % modelName) continue else: command = 'instantiateModel(%s)' % modelName res = OMPython.execute(command) if res <> '""\n': TestingType[modelName] = Kind.Translation logFile.write('%s : Translation\n' % modelName) continue else: TestingType[modelName] = Kind.Instantiation logFile.write('%s : Instantiation\n' % modelName) continue #now we have the tests list AND the functionality to be tested for each test now I have to generate the file warnings = False logFile.write('\n\nWarnings:\n\n') for model, test in TestingType.iteritems(): if test == Kind.Instantiation: stateset = '' else: stateset = findStateSet(model) if not stateset: logFile.write( 'Could not find states for test %s, moving test to simple simulation\n' % model) TestingType[model] = Kind.SimpleSimulation test = Kind.SimpleSimulation stateset = '""' else: stateset = [i for i in stateset if i.find('STATESET') == -1] stateset = '"' + '","'.join(stateset) + '"' mosTest = '%s.mos' % model print('creating %s' % mosTest) shutil.copy('../Common/Template.mos', mosTest) replaceInFile(mosTest, ['testingType', 'modelName', 'states'], [testingString(test), model, stateset]) ret = subprocess.check_output( [OPENMODELICAHOME + '/bin/omc.exe', mosTest]) ret = ' ' + ret.replace('\r\n', '\n// ') ret = ret[:ret.rfind('\n// ')] #erase current directory from paths ret = ret.replace(absPath.replace('\\', '/') + '/', '') if ret.find('Files not Equal') > -1: logFile.write('Warning: %s output not equal to reference file\n' % model) warnings = True #header does not begin with comment statement because is alredy in the file header = ' name: %s\n' % model header = header + '// keywords: %s\n' % testkeywords header = header + '// status: correct\n//\n// Simulation Results\n// Modelica Standard Library\n//\n' replaceInFile(mosTest, ['header', 'footer'], [header, ret]) print('test case for model:%s created!' % model) #clean directory subprocess.call([absPath + '/cleanAllbutMat.cmd']) print('Finished! check files before uploading') if warnings: print('There are warnings, check the log file') logFile.close()
def setNewParameters(cmd): OMPython.execute(cmd); global parameters_changed # Set this variable if the parameters are changed parameters_changed = True return
def main(): print 'Get all classes and detect test benches... using getTestBenches.mos' # generate test_benches.json file subprocess.check_output([ os.path.join(os.environ['OPENMODELICAHOME'], 'bin', 'omc'), 'getTestBenches.mos' ]) test_benches = {} with open('test_benches.json', 'r') as f_p: test_benches = json.load(f_p) OMPython.execute("cd()") modelica_lib_loaded = OMPython.execute('loadModel(Modelica,{"3.2"})') print modelica_lib_loaded c2m2l_loaded = OMPython.execute('loadFile("C2M2L_Ext/package.mo")') print c2m2l_loaded # allClassNames = OMPython.execute('getClassNames(C2M2L_Ext, qualified = true, recursive=true, sort=true)') # allClassNames = allClassNames['SET1']['Set1'] # test_benches_new = {} # num_all = len(allClassNames) # index = 0 # tbs = 0 # for item in allClassNames: # index = index + 1 # sys.stdout.write('{0}/{1} [{2} test benches] {3}\r'.format(index, num_all, tbs, item)) # sys.stdout.flush() # isExtends = False # #isExtends = OMPython.execute('extendsFrom({0}, Icons.TestBench)'.format(item)) # isExtends = isExtends or OMPython.execute('extendsFrom({0}, C2M2L_Ext.Icons.TestBench)'.format(item)) # if isExtends: # tbs = tbs + 1 # test_benches_new[item] = isExtends # print '{0} {1}'.format(isExtends, item) # print test_benches_new # return test_benches_out = {} num_all = len(test_benches.keys()) index = 0 tbs = 0 w_r = 0 print 'Detecting replacable components in test benches...' for class_name in test_benches.keys(): index = index + 1 sys.stdout.write( '{0}/{1} [{2} test benches {3} with replacable] {4}\r'.format( index, num_all, tbs, w_r, class_name)) sys.stdout.flush() if test_benches[class_name]: tbs = tbs + 1 #print class_name command = 'getComponents({0})'.format(class_name) #print command try: components = OMPython.execute(command) except Exception as e: #print 'Failed: {0}'.format(command) pass # iterate through SET2 for k in components['SET2'].keys(): component = components['SET2'][k] # replacable if len(component) > 7 and component[7] == 'true': w_r = w_r + 1 if not class_name in test_benches_out: test_benches_out[class_name] = {'parameters': []} #print ' - {0} {1}'.format(component[0], component[1]) test_benches_out[class_name]['parameters'].append({ 'constraint_clause': component[0], 'name': component[1], 'default_instance': None }) with open('test_benches_out.json', 'w') as f_p: json.dump(test_benches_out, f_p) return test_benches_out
def stop_openmodelica_server(): OMPython.execute("quit()")
def omp_run(self, loadcase, input_params): import OMPython as omp cwd = os.getcwd() # how did we come in directory "loadcases"? if not os.path.exists(loadcase.name): os.makedirs(loadcase.name) os.chdir(loadcase.name) try: omp.execute('cd("' + os.getcwd() + '")') mo_filename = ntpath.basename(loadcase.scheme) lrp_filename = "last_run_parameters.txt" par_filename = 'pl.txt' res_filename = 'results.plt' if not loadcase.is_filetransfer: create_file(loadcase.inData, mo_filename) params_string = generate_params_string(loadcase.solver_params) class_name = get_class_name_by_mo(mo_filename) recomp_flag = self.omp_recompilation(lrp_filename, mo_filename, class_name, loadcase.solver_params) #create mo-file on worker create_file(lrp_file_content(mo_filename, class_name, params_string), lrp_filename) # create file with input parameters using dictionaries of input parameters self.create_par_files_from_dicts(par_filename, input_params) if recomp_flag: if self.omp_compile(mo_filename, class_name, params_string) == constants.ERROR_STATUS: loadcase.status = constants.ERROR_STATUS logger.error("ERROR in compilation") return None if sys.platform.startswith('win'): exec_filename = class_name + '.exe' elif sys.platform.startswith('linux'): exec_filename = './' + class_name else: logger.info("Can not determine type of your OS") loadcase.status = constants.ERROR_STATUS return None #logger.info("Begin executing") if omp.execute('system("'+exec_filename+' -overrideFile '+par_filename+' -r '+res_filename+'")') != 0: loadcase.status = constants.ERROR_STATUS logger.error("ERROR in execution process") return None #logger.info("End executing") # if file transfer doesn't need parse file result in memory # otherwise return path to result file on worker if not loadcase.is_filetransfer: # getting dictionary of output parameters result = self.create_results_dict(res_filename) else: # store result file in FTP directory with name = task.id + .plt filename = str(loadcase.task_id) + '.plt' result_file_path = os.path.join(configworker.FTP_PATH, filename) # move result file shutil.move(res_filename, result_file_path) # store host and path to file in result result = dict() result['host'] = configworker.IP_ADDRESS result['file'] = filename loadcase.status = constants.SUCCESS_STATUS os.chdir(cwd) return result finally: os.chdir(cwd)
def main(): # get all tests # license problem, if you don't have ModelManagement license on dymola this won't work. #Trying to find a solution #missingTests = getExamples(OMLIBRARY) for i in simulationTests: if i.endswith('.mos'): simulationTests[simulationTests.index(i)]=i[:i.rfind('.mos')] # generate ref files generateRefFiles(simulationTests) # go one folder up, now I should be in the folder where tests must be put absPath = os.path.abspath(os.path.curdir); logFile=open(log_file,'w') # select kind of test for each model. OMPython.execute("clear()") command='loadModel(Modelica,{"%s"})'%MSLVERSION res=OMPython.execute(command) if not res: print('Loading modelica library failed') return if EXTRALIB: for lib in EXTRALIB: lib=lib.replace('\\','/') res=OMPython.execute('loadFile("%s")'%lib) if not res: print('Loading library %s failed'%lib) # build a dictionary containing the list of tests to be created and the kind of functionality to be tested #the dictionary TestingType={} logFile.write('List of models tested with test kind\n') for modelName in simulationTests: # do not run tests, where no ref file exists refFile = '%s/ReferenceFiles/%s.mat'%(absPath,modelName) if not (os.path.exists(refFile)): logFile.write("Skipping %s because there is no ref-file\n"%modelName) continue # do not run tests, that have already been set up mos=modelName+'.mos' if (os.path.exists(mos)): logFile.write("Skipping %s because mos-File already exists\n"%modelName) continue print('Analyzing the current status of %s'%modelName) command='simulate(%s)'%modelName #try to simulate res=OMPython.execute(command) resultMess=res['SimulationResults']['messages'] if res['SimulationResults']['resultFile']<>'""': #if an output file exists we should check results but still have to think on how to do it if resultMess=='""': TestingType[modelName]=Kind.VerifiedSimulation logFile.write('%s : VerifiedSimulation\n'%modelName) continue #else if i simulate with messages else: TestingType[modelName]=Kind.SuppressedSimulation logFile.write('%s : SuppressedSimulation\n'%modelName) continue #if there is no result file else: command='buildModel(%s)'%modelName commandtransl='translateModel(%s)'%modelName res=OMPython.execute(command) if res['SET1']['Values'][0]<>'"",""': TestingType[modelName]=Kind.SuppressedSimulation logFile.write('%s : SuppressedSimulation\n'%modelName) continue elif OMPython.execute(commandtransl): TestingType[modelName]=Kind.Compilation logFile.write('%s : Compilation\n'%modelName) continue else: command='instantiateModel(%s)'%modelName res=OMPython.execute(command) if res<>'""\n': TestingType[modelName]=Kind.Translation logFile.write('%s : Translation\n'%modelName) continue else: TestingType[modelName]=Kind.Instantiation logFile.write('%s : Instantiation\n'%modelName) continue #now we have the tests list AND the functionality to be tested for each test now I have to generate the file warnings=False logFile.write('\n\nWarnings:\n\n') for model,test in TestingType.iteritems(): if test==Kind.Instantiation: stateset=''; else: stateset=findStateSet(model) if not stateset: logFile.write('Could not find states for test %s, moving test to simple simulation\n'%model) TestingType[model]=Kind.SimpleSimulation test=Kind.SimpleSimulation stateset='""' else: stateset=[i for i in stateset if i.find('STATESET')==-1] stateset='"'+'","'.join(stateset)+'"' mosTest='%s.mos'%model print('creating %s'%mosTest) shutil.copy('../Common/Template.mos', mosTest) replaceInFile(mosTest,['testingType','modelName','states'],[testingString(test),model,stateset]) ret = subprocess.check_output([OPENMODELICAHOME + '/bin/omc.exe', mosTest]) ret=' '+ret.replace('\r\n','\n// ') ret=ret[:ret.rfind('\n// ')] #erase current directory from paths ret=ret.replace(absPath.replace('\\','/')+'/','') if ret.find('Files not Equal')>-1: logFile.write('Warning: %s output not equal to reference file\n'%model) warnings=True #header does not begin with comment statement because is alredy in the file header=' name: %s\n'%model header=header + '// keywords: %s\n'%testkeywords header=header + '// status: correct\n//\n// Simulation Results\n// Modelica Standard Library\n//\n' replaceInFile(mosTest,['header','footer'],[header,ret]) print('test case for model:%s created!'%model) #clean directory subprocess.call([absPath+'/cleanAllbutMat.cmd']) print('Finished! check files before uploading') if warnings: print('There are warnings, check the log file') logFile.close()
def main(): print 'Get all classes and detect test benches... using getTestBenches.mos' # generate test_benches.json file subprocess.check_output([os.path.join(os.environ['OPENMODELICAHOME'], 'bin', 'omc'), 'getTestBenches.mos']) test_benches = {} with open('test_benches.json', 'r') as f_p: test_benches = json.load(f_p) OMPython.execute("cd()") modelica_lib_loaded = OMPython.execute('loadModel(Modelica,{"3.2"})') print modelica_lib_loaded c2m2l_loaded = OMPython.execute('loadFile("C2M2L_Ext/package.mo")') print c2m2l_loaded # allClassNames = OMPython.execute('getClassNames(C2M2L_Ext, qualified = true, recursive=true, sort=true)') # allClassNames = allClassNames['SET1']['Set1'] # test_benches_new = {} # num_all = len(allClassNames) # index = 0 # tbs = 0 # for item in allClassNames: # index = index + 1 # sys.stdout.write('{0}/{1} [{2} test benches] {3}\r'.format(index, num_all, tbs, item)) # sys.stdout.flush() # isExtends = False # #isExtends = OMPython.execute('extendsFrom({0}, Icons.TestBench)'.format(item)) # isExtends = isExtends or OMPython.execute('extendsFrom({0}, C2M2L_Ext.Icons.TestBench)'.format(item)) # if isExtends: # tbs = tbs + 1 # test_benches_new[item] = isExtends # print '{0} {1}'.format(isExtends, item) # print test_benches_new # return test_benches_out = {} num_all = len(test_benches.keys()) index = 0 tbs = 0 w_r = 0 print 'Detecting replacable components in test benches...' for class_name in test_benches.keys(): index = index + 1 sys.stdout.write('{0}/{1} [{2} test benches {3} with replacable] {4}\r'.format(index, num_all, tbs, w_r, class_name)) sys.stdout.flush() if test_benches[class_name]: tbs = tbs + 1 #print class_name command = 'getComponents({0})'.format(class_name) #print command try: components = OMPython.execute(command) except Exception as e: #print 'Failed: {0}'.format(command) pass # iterate through SET2 for k in components['SET2'].keys(): component = components['SET2'][k] # replacable if len(component) > 7 and component[7] == 'true': w_r = w_r + 1 if not class_name in test_benches_out: test_benches_out[class_name] = {'parameters': []} #print ' - {0} {1}'.format(component[0], component[1]) test_benches_out[class_name]['parameters'].append({'constraint_clause':component[0], 'name':component[1], 'default_instance':None}) with open('test_benches_out.json', 'w') as f_p: json.dump(test_benches_out, f_p) return test_benches_out
def closeSimulationPlugin(): try: OMPython.execute("quit()") except SystemExit: pass