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 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 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 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