def Compile(this, mdl): from PySimLib import Platform this.__EnsureDymolaIsOpen() # open all needed mo files for x in mdl.GetFiles(): this.__OpenFile(mdl, x) # go to sim dir Dymola.__ddeConversation.Exec("cd(\"" + mdl.simDir.replace('\\', '/') + "\")") # simulate to run model Dymola.__ddeConversation.Exec("translateModel(\"" + mdl.GetModelicaClassString() + "\")") # Dymola.__ddeConversation.Exec("simulateModel(\"" + mdl.GetModelicaClassString() + "\", stopTime=0)"); #method=\"" + this.solver.name + "\" this._EnsureOutputFolderExists(mdl) # Convert the dsin args = [ GetConfigValue("Dymola", "PathAlist"), "-b", mdl.simDir + os.sep + "dsin.txt", this.__GetInitFilePath(mdl) ] Platform.Execute(args) # this._DeleteFile("dsres.mat"); # Rename important files this._RenameFile("dymosim" + Platform.GetExeFileExtension(), this.__GetExeFilePath(mdl)) this.__DeleteUnnecessaryFiles()
def Compile(this, mdl): from PySimLib import Platform this.__EnsureDymolaIsOpen() # open all needed mo files for x in mdl.GetFiles(): this.__OpenFile(mdl, x) # go to sim dir Dymola2.__interface.cd(mdl.simDir) # simulate to run model suc = Dymola2.__interface.translateModel(mdl.GetModelicaClassString()) if not suc: this.Close() print( 'translating the model did not work out too well:\n{}'.format( Dymola2.__interface.getLastError())) this._EnsureOutputFolderExists(mdl) # Convert the dsin args = [ GetConfigValue("Dymola2", "PathAlist"), "-b", mdl.simDir + os.sep + "dsin.txt", this.__GetInitFilePath(mdl) ] Platform.Execute(args) # this._DeleteFile("dsres.mat"); # Rename important files this._RenameFile("dymosim" + Platform.GetExeFileExtension(), this.__GetExeFilePath(mdl)) this.__DeleteUnnecessaryFiles() this.Close()
def __GetExeFilePath(this, mdl): from PySimLib import Platform return mdl.outputDir + os.sep + mdl.outputName + Platform.GetExeFileExtension()
def __GetExeFilePath(this, mdl): return mdl.outputDir + os.sep + mdl.outputName + Platform.GetExeFileExtension( )
def Compile(this, mdl): from PySimLib.Exceptions.CompilationFailedException import CompilationFailedException #write a mos file to be executed by openmodelica SCRIPTNAME = mdl.simDir + os.sep + mdl.outputName + ".mos" mosfile = open(SCRIPTNAME, 'w', 1) mosfile.write("loadModel(Modelica);\r\n") for item in mdl.GetFiles(): mosfile.write("loadFile(\"" + item + "\");\r\n") #evaluate parameters #TODO looks like structural parameters cant be set in openmodelica overrideString = "" """ if(mdl.parameters): overrideString = ', simflags="-override '; for k in mdl.parameters: overrideString += k + "=" + str(mdl.parameters[k]) + " "; overrideString += '"'; """ # do a dummy simulation since it is not yet possible to do a translation only #mosfile.write("simulate(" + mdl.GetModelicaClassString() + ", startTime = " + str(0) + ", stopTime = " + str(1) + ", method = \"" + "dassl" + "\", outputFormat=\"mat\");\r\n"); mosfile.write("simulate(" + mdl.GetName() + ", startTime = " + str(0) + ", stopTime = " + str(1) + ", method = \"" + "dassl" + "\", outputFormat=\"mat\"" + overrideString + ");\r\n") mosfile.close() #call the open modelica compiler omcArgs = [ GetConfigValue("OpenModelica", "PathExe"), os.path.abspath(SCRIPTNAME), "Modelica" ] this._DeleteFile(mdl.outputName + Platform.GetExeFileExtension()) #delete old exe to be sure that a new one is created Platform.Execute(omcArgs) #omc seems to return always 0 this._DeleteFile(SCRIPTNAME) #we don't need the mos script anymore if (not this._FileExists(mdl.GetName() + Platform.GetExeFileExtension()) ): #if simulation failed there will be no exe raise CompilationFailedException(mdl, this) #make sure output folder exists if (not this._DirExists(mdl.outputDir)): os.makedirs(mdl.outputDir) #Remove unnecessary files this._DeleteFile(mdl.GetName() + ".c") this._DeleteFile(mdl.GetName() + ".libs") this._DeleteFile(mdl.GetName() + ".log") this._DeleteFile(mdl.GetName() + ".makefile") this._DeleteFile(mdl.GetName() + ".o") this._DeleteFile(mdl.GetName() + "_01exo.c") this._DeleteFile(mdl.GetName() + "_01exo.o") this._DeleteFile(mdl.GetName() + "_02nls.c") this._DeleteFile(mdl.GetName() + "_02nls.o") this._DeleteFile(mdl.GetName() + "_03lsy.c") this._DeleteFile(mdl.GetName() + "_03lsy.o") this._DeleteFile(mdl.GetName() + "_04set.c") this._DeleteFile(mdl.GetName() + "_04set.o") this._DeleteFile(mdl.GetName() + "_05evt.c") this._DeleteFile(mdl.GetName() + "_05evt.o") this._DeleteFile(mdl.GetName() + "_06inz.c") this._DeleteFile(mdl.GetName() + "_06inz.o") this._DeleteFile(mdl.GetName() + "_07dly.c") this._DeleteFile(mdl.GetName() + "_07dly.o") this._DeleteFile(mdl.GetName() + "_08bnd.c") this._DeleteFile(mdl.GetName() + "_08bnd.o") this._DeleteFile(mdl.GetName() + "_09alg.c") this._DeleteFile(mdl.GetName() + "_09alg.o") this._DeleteFile(mdl.GetName() + "_10asr.c") this._DeleteFile(mdl.GetName() + "_10asr.o") this._DeleteFile(mdl.GetName() + "_11mix.c") this._DeleteFile(mdl.GetName() + "_11mix.o") this._DeleteFile(mdl.GetName() + "_11mix.h") this._DeleteFile(mdl.GetName() + "_12jac.c") this._DeleteFile(mdl.GetName() + "_12jac.h") this._DeleteFile(mdl.GetName() + "_12jac.o") this._DeleteFile(mdl.GetName() + "_13opt.c") this._DeleteFile(mdl.GetName() + "_13opt.o") this._DeleteFile(mdl.GetName() + "_13opt.h") this._DeleteFile(mdl.GetName() + "_14lnz.c") this._DeleteFile(mdl.GetName() + "_14lnz.o") this._DeleteFile(mdl.GetName() + "_15syn.c") this._DeleteFile(mdl.GetName() + "_15syn.o") this._DeleteFile(mdl.GetName() + "_16dae.h") this._DeleteFile(mdl.GetName() + "_16dae.c") this._DeleteFile(mdl.GetName() + "_16dae.o") this._DeleteFile(mdl.GetName() + "_functions.c") this._DeleteFile(mdl.GetName() + "_functions.h") this._DeleteFile(mdl.GetName() + "_functions.o") this._DeleteFile(mdl.GetName() + "_includes.h") this._DeleteFile(mdl.GetName() + "_literals.h") this._DeleteFile(mdl.GetName() + "_model.h") this._DeleteFile(mdl.GetName() + "_records.c") this._DeleteFile(mdl.GetName() + "_records.o") this._DeleteFile(mdl.GetName() + "_res.mat") #Rename important files this._RenameFile(mdl.GetName() + Platform.GetExeFileExtension(), this.__GetExeFilePath(mdl)) this._RenameFile(mdl.GetName() + "_init.xml", this.__GetInitFilePath(mdl)) this._RenameFile(mdl.GetName() + "_info.json", this.__GetInfoFilePath(mdl))