Ejemplo n.º 1
0
    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()
Ejemplo n.º 2
0
    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()
Ejemplo n.º 3
0
    def __EnsureDymolaIsOpen(this):
        if(not Dymola.__dymolaIsOpen):
            import win32ui
            import dde
            import time
            from PySimLib import Platform

            Dymola.__ddeServer = dde.CreateServer()
            Dymola.__ddeServer.Create("TestClient")
            Dymola.__ddeConversation = dde.CreateConversation(Dymola.__ddeServer)
            Dymola.__dymolaIsOpen = True

            Platform.Execute([GetConfigValue("Dymola", "PathExe")], False)
            time.sleep(float(GetConfigValue("Dymola", "StartupDelay")))
            Dymola.__ddeConversation.ConnectTo("dymola", " ")
            Dymola.__ddeConversation.Exec("OutputCPUtime:=true;")
Ejemplo n.º 4
0
    def Simulate(this, sim):
        from PySimLib.Exceptions.SimulationFailedException import SimulationFailedException

        mdl = sim.GetModel()

        #paths
        simInfoFilePath = mdl.simDir + os.sep + mdl.GetName() + "_info.json"

        #check if model is compiled
        this.__AssertModelCompiled(mdl)

        #make sure result folder exists
        if (not this._DirExists(mdl.resultDir)):
            os.makedirs(mdl.resultDir)

        #prepare init file
        this.__WriteInit(sim)

        #info file can't be specified as arg to openmodelica
        if (not (mdl.outputName == mdl.GetName())
                or not (mdl.simDir == mdl.outputDir)):
            shutil.copyfile(this.__GetInfoFilePath(mdl), simInfoFilePath)

        #run compiled model
        simArgs = [
            this.__GetExeFilePath(mdl),
            "-f",
            this.__GetSimInitFilePath(sim),
        ]

        exitCode = Platform.Execute(simArgs, True, mdl.simDir)
        if (not (exitCode == 0)):
            raise SimulationFailedException(sim, this)

        #keep things clean
        if (not (mdl.outputName == mdl.GetName())
                or not (mdl.simDir == mdl.outputDir)):
            this._DeleteFile(simInfoFilePath)
        this._DeleteFile(this.__GetSimInitFilePath(sim))

        #rename results
        this._RenameFile(mdl.simDir + os.sep + mdl.GetName() + "_res.mat",
                         this._GetSimResultFilePath(sim))
Ejemplo n.º 5
0
    def __GetExeFilePath(this, mdl):
        from PySimLib import Platform

        return mdl.outputDir + os.sep + mdl.outputName + Platform.GetExeFileExtension()
Ejemplo n.º 6
0
    def Simulate(this, sim):
        mdl = sim.GetModel()

        # paths
        dsinPaths = mdl.simDir + os.sep + "dsin.txt"

        # error checks
        this.__CheckIfModelIsCompiled(mdl)

        this._EnsureResultFolderExists(mdl)

        # prepare init file
        this.__WriteInit(sim)

        # simulate
        if(GetBoolConfigValue("Dymola", "SimByExe")):
            from PySimLib import Log, Platform

            args = [this.__GetExeFilePath(mdl), this.__GetSimInitFilePath(sim)]
            Platform.Execute(args, True, mdl.simDir)
        else:
            from PySimLib import Log, Platform
            # convert back to dsin
            args = [
                GetConfigValue("Dymola", "PathAlist"),
                "-a",
                this.__GetSimInitFilePath(sim),
                dsinPaths
            ]
            # Platform.Execute(args);

            # load simulation config --- apparently not working like this
            #Dymola.__ddeConversation.Exec("importInitial(\"" + this.__GetSimInitFilePath(sim).replace('\\', '/') + "\")");

            # run
            this.__EnsureDymolaIsOpen()
            Dymola.__ddeConversation.Exec("simulateModel(\"" + mdl.GetModelicaClassString(True, sim) + "\", startTime=" + str(sim.startTime) + ", stopTime=" + str(sim.stopTime) + ", method=\"" + str(sim.solver.GetName()) + "\", tolerance=" + str(sim.solver.tolerance) + ")")

            # we always need to close the model, so that dymola recompiles it
            Dymola.__ddeConversation.Exec("closeModel();")

            # delete dsin
            this._DeleteFile(dsinPaths)

        failed = False
        if(this._FileExists("failure")):
            failed = True

        # keep things clean
        # this._DeleteFile("status");
        # this._DeleteFile("success");
        # this._DeleteFile("failure");

        if(failed):
            from PySimLib.Exceptions.SimulationFailedException import SimulationFailedException
            this._DeleteFile("dsres.mat")
            raise SimulationFailedException(sim, 'Dymola')

        this._DeleteFile(this.__GetSimInitFilePath(sim))

        # rename results
        this._RenameFile(mdl.simDir + os.sep + "dsres.mat", this._GetSimResultFilePath(sim))

        this.__DeleteUnnecessaryFiles()
Ejemplo n.º 7
0
 def __GetExeFilePath(this, mdl):
     return mdl.outputDir + os.sep + mdl.outputName + Platform.GetExeFileExtension(
     )
Ejemplo n.º 8
0
    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))