Ejemplo n.º 1
0
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
Ejemplo n.º 2
0
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
Ejemplo n.º 3
0
 def testDocker(self):
   om = OMPython.OMCSessionZMQ(docker="openmodelica/openmodelica:v1.16.1-minimal")
   assert(om.sendExpression("getVersion()") == "OpenModelica 1.16.1")
   omInner = OMPython.OMCSessionZMQ(dockerContainer=om._dockerCid)
   assert(omInner.sendExpression("getVersion()") == "OpenModelica 1.16.1")
   om2 = OMPython.OMCSessionZMQ(docker="openmodelica/openmodelica:v1.16.1-minimal", port=11111)
   assert(om2.sendExpression("getVersion()") == "OpenModelica 1.16.1")
   del(om2)
   del(omInner)
   del(om)
Ejemplo n.º 4
0
 def worker():
   origDir = os.getcwd()
   os.chdir(self.tmp)
   m = OMPython.ModelicaSystem("M.mo", "M")
   m.simulate()
   m.convertMo2Fmu(fmuType="me")
   os.chdir(origDir)
Ejemplo n.º 5
0
 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
Ejemplo n.º 6
0
    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")
Ejemplo n.º 7
0
def createDiscretizedLib():
    #os.chdir("./..")

    # copy all files:
    dirD = "Discretized"
    dirSB = "SnowBreathing"

    if os.path.exists(dirD):
        shutil.rmtree(dirD)
    os.mkdir(dirD)
    os.mkdir(dirD + "/" + dirSB)
    for d in os.listdir(dirSB):
        if d != ".git":
            src = dirSB + "/" + d
            dst = dirD + "/" + dirSB + "/" + d
            if os.path.isdir(src):
                shutil.copytree(src, dst)
            else:
                shutil.copy(src, dst)
    #discretize PDEModelica:
    toDiscretize = ["DifussionSphereCO2", "DifussionSphereCO2O2"]

    omc = OMPython.OMCSessionZMQ()
    #print("omc verze: " + omCommand("getVersion()", omc));
    #omCommandP("setCompilerFlags(\"--grammar=PDEModelica\")", omc);
    omCommandP("setCommandLineOptions(\"--grammar=PDEModelica\")", omc)
    omCommandP("loadModel(Modelica)", omc)
    omCommandP("loadFile(\"" + dirSB + "/package.mo" + "\")", omc)
    pOFile = open(dirD + "/" + dirSB + "/" + "Components/package.order", "a")
    for nameUnqual in toDiscretize:
        fileName = dirD + "/" + dirSB + "/" + "Components/" + nameUnqual + "_discretised.mo"
        nameQual = "SnowBreathing.Components." + nameUnqual
        modelStr = discretize(omc, nameUnqual, nameQual)
        dirname = os.path.dirname(fileName)
        if not os.path.exists(dirname):
            os.makedirs(dirname)
        F = open(fileName, "wt+")
        #F.write(modelStr.encode("utf8"))
        F.write(modelStr)
        F.close()
        shutil.copy("./AuxMo/" + nameUnqual + ".mo",
                    dirD + "/SnowBreathing/Components/" + nameUnqual + ".mo")
        pOFile.write(nameUnqual + "_discretised\n")
    pOFile.close()
    print(
        "Automatic discretization finished. You have to make this changes manually:\n"
        + "    - delete the fluxConcB_q = 0; equation\n" +
        "    - add the fluxConc_CO2(O2)In variable\n" +
        "           Real fluxConcB_CO2In(unit=\"1\") \"CO2 volume concentration\";\n"
        +
        "           Real fluxConcB_O2In(unit=\"1\") \"CO2 volume concentration\";\n"
        +
        "    - modify the boundary condition to incorporate the ..In var. \n" +
        "           CO2_ghostL = if exhale then fluxConcB_CO2In else 2.0 * CO2_1 - CO2_2;\n"
        +
        "           O2_ghostL = if exhale then fluxConcB_O2In else 2.0 * O2_1 - O2_2;\n"
    )
Ejemplo n.º 8
0
    def __init__(self, *args, **kwargs):
        super(ZMQTester, self).__init__(*args, **kwargs)
        self.simpleModel = """model M
  Real r = time;
end M;"""
        self.tmp = tempfile.mkdtemp(prefix='tmpOMPython.tests')
        self.origDir = os.getcwd()
        os.chdir(self.tmp)
        self.om = OMPython.OMCSessionZMQ()
        os.chdir(self.origDir)
Ejemplo n.º 9
0
    def setUp(self):
        model = 'SolarTherm.Test.TestWeatherTable'
        self.omc = OMPython.OMCSession()
        self.ex = self.omc.execute
        self.assertTrue(self.ex('loadModel(Modelica)'))
        self.assertTrue(self.ex('loadModel(' + model + ')'),
                        msg=self.ex('getErrorString()'))

        ans = self.ex('simulate(' + model + ', stopTime=3)')
        self.assertEqual(ans['SimulationResults']['messages'],
                         '""',
                         msg=self.ex('getErrorString()'))
Ejemplo n.º 10
0
def command_line():
  import argparse

  argparser = argparse.ArgumentParser()
  argparser.add_argument("root",
      help="the root of the modelica project")
  argparser.add_argument("script",
      help="the script to run")
  args = argparser.parse_args()

  shell = OMPython.OMShell(os.path.abspath(args.root),echo=True)

  print shell.run_script(os.path.abspath(args.script));
Ejemplo n.º 11
0
    def setUp(self):
        model = 'SolarTherm.Test.TestWeatherFileChecker'
        self.omc = OMPython.OMCSession()
        self.ex = self.omc.execute
        self.assertTrue(self.ex('loadModel(Modelica)'))
        self.assertTrue(self.ex('loadModel(' + model + ')'),
                        msg=self.ex('getErrorString()'))

        if os.path.isfile('resources/weatherfile2.motab'):
            os.remove('resources/weatherfile2.motab')
        ans = self.ex('simulate(' + model + ', stopTime=1)')
        #print(ans)
        self.assertEqual(ans['SimulationResults']['messages'],
                         '""',
                         msg=self.ex('getErrorString()'))
Ejemplo n.º 12
0
def load_model(folder_path, file_name, class_name):
    file_path = folder_path + file_name
    dependencies = ["Modelica"]
    for file in os.listdir(folder_path):
        if file.endswith(".mo") and not file == file_name:
            dependencies.append(folder_path + file)

    user_home = os.environ['HOME']
    dependencies.append(
        user_home +
        '/Dropbox/Tesisti/software/BioChem-1.0.1/BioChem/package.mo')
    dependencies.append(
        user_home +
        '/Dropbox/Tesisti/software/BioChem-1.0.1/new-models/reactions.mo')
    return OMPython.ModelicaSystem(file_path, class_name, dependencies)
Ejemplo n.º 13
0
    def setUp(self):
        model = 'SolarTherm.Test.TestSolarPosition'
        self.omc = OMPython.OMCSession()
        self.ex = self.omc.execute
        self.assertTrue(self.ex('loadModel(Modelica)'))
        self.assertTrue(self.ex('loadModel(' + model + ')'),
                        msg=self.ex('getErrorString()'))

        # One day 500 intervals:
        #ans = self.ex('simulate('+model+', stopTime=86400)')
        # One year 500 intervals per day:
        ans = self.ex('simulate(' + model +
                      ', stopTime=31536000, numberOfIntervals=182500)')
        self.assertEqual(ans['SimulationResults']['messages'],
                         '""',
                         msg=self.ex('getErrorString()'))
Ejemplo n.º 14
0
    def setUp(self):
        model = 'SolarTherm.Test.TestWeatherSource'
        self.omc = OMPython.OMCSession()
        self.ex = self.omc.execute
        self.assertTrue(self.ex('loadModel(Modelica)'))
        self.assertTrue(self.ex('loadModel(' + model + ')'),
                        msg=self.ex('getErrorString()'))

        # Azimuth can be very sensitive at high altitude so increasing number
        # of intervals.
        # Also because of val() interpolates between 0 and 360 at solar noon
        # if not a sampled instant.
        ans = self.ex('simulate(' + model +
                      ', stopTime=86400, numberOfIntervals=2000)')
        #print(ans)
        self.assertEqual(ans['SimulationResults']['messages'],
                         '""',
                         msg=self.ex('getErrorString()'))
Ejemplo n.º 15
0
        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)
Ejemplo n.º 16
0
	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)
Ejemplo n.º 17
0
	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
Ejemplo n.º 18
0
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()
Ejemplo n.º 19
0
def main():
    gh_auth = os.environ["GITHUB_AUTH"]
    g = Github(gh_auth)

    omc = OMPython.OMCSessionZMQ()

    data = json.load(open("repos.json"))
    if os.path.exists("rawdata.json"):
        serverdata = json.load(open("rawdata.json"))
    else:
        serverdata = {}

    if not os.path.exists("cache"):
        os.mkdir("cache")

    namesInFile = set()
    for key in data.keys():
        for name in data[key].get("names", []):
            if name in namesInFile:
                raise Exception(key + " exists multiple times")
            namesInFile.add(name)

    for key in sorted(data.keys()):
        entry = data[key]
        repopath = os.path.join("cache", key)
        if "ignore" in entry:
            continue

        if len(insensitive_glob(repopath)) > 1:
            for d in insensitive_glob(repopath):
                print("Cleaning duplicate case-insensitive names")
                shutil.rmtree(d)

        if "github" in entry or "git" in entry or "zipfiles" in entry:
            if "github" in entry:
                try:
                    r = g.get_repo(entry["github"])
                    branches = list(
                        (b.name, b.commit.sha) for b in r.get_branches())
                    tags = list((b.name, b.commit.sha) for b in r.get_tags())
                    giturl = "https://github.com/%s.git" % entry["github"]
                except:
                    print("Failed to get github entry: %s" % entry["github"])
                    raise
            elif "git" in entry:
                giturl = entry["git"]
                gitrepo = getgitrepo(giturl, repopath + ".git")
                branches = allbranches(gitrepo)
                if entry.get("bitbucket-api-downloads-instead-of-tags"):
                    tags = []
                    for download in bitbucket._get_paged(
                            "2.0/repositories/%s/downloads" % entry.get(
                                "bitbucket-api-downloads-instead-of-tags")):
                        name = download["name"]
                        if name.endswith(".zip") and name.startswith(key):
                            ver = common.VersionNumber(
                                name[len(key):-4].strip("-").strip(" "))
                            if ver.major == 0 and ver.minor == 0 and ver.patch == 0:
                                continue
                            tags.append(("v" + str(ver),
                                         download["links"]["self"]["href"]))
                else:
                    tags = alltags(gitrepo)
            elif "zipfiles" in entry:
                branches = []
                tags = list(entry["zipfiles"].items())
                giturl = ""

            if key not in serverdata:
                serverdata[key] = {}
                print("Did not have stored data for " + key)
            if "refs" not in serverdata[key]:
                serverdata[key]["refs"] = {}
            ignoreTags = set()
            if "ignore-tags" in entry:
                ignoreTags = set(entry["ignore-tags"])
            objects = []
            for (name, sha) in branches:
                if name in (entry.get("branches") or []):
                    objects.append((entry["branches"][name], sha))
            for (name, sha) in tags:
                if name not in ignoreTags:
                    objects.append((name, sha))

            if not objects:
                raise Exception("No commits or zip-files found for %s" % key)

            tagsDict = serverdata[key]["refs"]

            for (tagName, sha) in objects:
                if not isinstance(tagName, str):
                    names = tagName["names"]
                    tagName = tagName["version"]
                else:
                    names = entry["names"]
                v = common.VersionNumber(tagName)
                # v3.2.1+build.0-beta.1 is not a pre-release...
                for build in v.build:
                    if "-" in build:
                        raise Exception(
                            "Release build string looks like a pre-release: %s"
                            % v)
                if tagName not in tagsDict:
                    tagsDict[tagName] = {}
                thisTagBackup = tagsDict[tagName]
                thisTag = tagsDict[tagName]

                entrykind = "zip" if ("zipfiles" in entry
                                      or sha.startswith("http")) else "sha"

                if (entrykind not in thisTag) or (thisTag[entrykind] != sha):
                    if entrykind == "zip":
                        try:
                            os.unlink(repopath)
                        except:
                            pass
                        try:
                            shutil.rmtree(repopath)
                        except FileNotFoundError:
                            pass
                        os.mkdir(repopath)
                        zipfilepath = repopath + "-" + tagName + ".zip"
                        with open(zipfilepath, 'wb') as fout:
                            fout.write(
                                requests.get(sha,
                                             allow_redirects=True).content)
                        with zipfile.ZipFile(zipfilepath, 'r') as zip_ref:
                            zip_ref.extractall(repopath)
                    else:
                        gitrepo = getgitrepo(giturl, repopath + ".git")
                        try:
                            gitrepo.checkout_tree(
                                gitrepo.get(sha),
                                strategy=pygit2.GIT_CHECKOUT_FORCE
                                | pygit2.GIT_CHECKOUT_RECREATE_MISSING)
                            try:
                                os.unlink(repopath)
                            except:
                                pass
                            try:
                                shutil.rmtree(repopath)
                            except FileNotFoundError:
                                pass
                            shutil.copytree(repopath + ".git", repopath)
                        except:
                            print("Failed to checkout %s with SHA %s" %
                                  (tagName, sha))
                            raise
                    omc.sendExpression(
                        "OpenModelica.Scripting.getErrorString()")

                    provided = {}
                    for libname in names:
                        hits = insensitive_glob(
                            os.path.join(repopath, "package.mo"))
                        if len(hits) == 1:
                            if libname != entry["names"][0]:
                                continue
                        else:
                            hits = []
                            for extraPath in (
                                [""] +
                                (entry.get("search-extra-paths") or [])):
                                p = repopath
                                if extraPath != "":
                                    p = os.path.join(repopath, extraPath)
                                else:
                                    p = repopath
                                hitsNew = (
                                    insensitive_glob(
                                        os.path.join(p, libname, "package.mo"))
                                    + insensitive_glob(
                                        os.path.join(p, libname + " *",
                                                     "package.mo")) +
                                    insensitive_glob(
                                        os.path.join(p, libname + "-*",
                                                     "package.mo")) +
                                    insensitive_glob(
                                        os.path.join(p, libname + ".mo")) +
                                    insensitive_glob(
                                        os.path.join(p, libname + " *.mo")) +
                                    insensitive_glob(
                                        os.path.join(p, libname + "-*.mo")) +
                                    insensitive_glob(
                                        os.path.join(p, libname + "*",
                                                     libname + ".mo")) +
                                    insensitive_glob(
                                        os.path.join(p, libname + "*",
                                                     libname + "-*.mo")) +
                                    insensitive_glob(
                                        os.path.join(p, libname + "*",
                                                     libname + " *.mo")))
                                hits += hitsNew
                        if len(hits) != 1:
                            print(
                                str(len(hits)) + " hits for " + libname +
                                " in " + tagName + ": " + str(hits))
                            continue
                        omc.sendExpression("clear()")
                        if "standard" in entry:
                            grammar = common.findMatchingLevel(
                                tagName, entry["standard"])
                            if grammar is None:
                                grammar = "latest"
                        else:
                            grammar = "latest"
                        omc.sendExpression(
                            "setCommandLineOptions(\"--std=%s\")" % grammar)

                        if not omc.sendExpression(
                                "loadFile(\"%s\", uses=false)" % hits[0]):
                            print("Failed to load file %s in %s" %
                                  (hits[0], tagName))
                            continue
                        classNamesAfterLoad = omc.sendExpression(
                            "getClassNames()")
                        if libname not in classNamesAfterLoad:
                            print("Did not load the library? ")
                            print(classNamesAfterLoad)
                            continue
                        version = omc.sendExpression("getVersion(%s)" %
                                                     libname)
                        if version == "":
                            version = str(common.VersionNumber(tagName))
                        else:
                            v1 = common.VersionNumber(version)
                            v2 = common.VersionNumber(tagName)
                            if len(v2.prerelease) == 0 and entry.get(
                                    "semverTagOverridesAnnotation") and (
                                        v2 > v1 or
                                        entry["semverTagOverridesAnnotation"]
                                        == "alsoNewerVersions"):
                                v1 = v2
                            if len(v2.prerelease) > 0 and (
                                    len(v1.prerelease) == 0 or entry.get(
                                        "semverPrereleaseOverridesAnnotation")
                                    or tagName in ["master", "main", "trunk"]):
                                v1.prerelease = v2.prerelease
                                if tagName in ["master", "main", "trunk"
                                               ] and len(v2.build) == 0:
                                    v1.build = []
                            if v1.major == v2.major and v1.minor == v2.minor and v1.patch == v2.patch and len(
                                    v1.prerelease) == 0 and len(v1.build) == 0:
                                version = str(v2)
                            else:
                                version = str(v1)
                        if version.startswith("0.0.0-"):
                            version = version[6:]
                        if version.startswith("+"):
                            version = version[1:]
                        uses = sorted([[
                            e[0], str(common.VersionNumber(e[1]))
                        ] for e in omc.sendExpression("getUses(%s)" % libname)
                                       ])
                        # Get conversions
                        (withoutConversion,
                         withConversion) = omc.sendExpression(
                             "getConversionsFromVersions(%s)" % libname)
                        withoutConversion = list(
                            filter(None, [
                                str(ver) for ver in sorted([
                                    common.VersionNumber(v)
                                    for v in withoutConversion
                                ])
                            ]))
                        withConversion = list(
                            filter(None, [
                                str(ver) for ver in sorted([
                                    common.VersionNumber(v)
                                    for v in withConversion
                                ])
                            ]))
                        path = hits[0][len(repopath) + 1:]
                        if os.path.basename(hits[0]) == "package.mo":
                            path = os.path.dirname(path)
                        libentry = {"version": version, "path": path}
                        if len(uses) > 0:
                            libentry["uses"] = dict(uses)
                        if len(withoutConversion) > 0:
                            libentry["provides"] = withoutConversion
                        if len(withConversion) > 0:
                            libentry["convertFromVersion"] = withConversion
                        provided[libname] = libentry
                    try:
                        errorString = omc.sendExpression(
                            "OpenModelica.Scripting.getErrorString()")
                    except UnicodeDecodeError:
                        print("UnicodeDecodeError for %s %s" % (key, tagName))
                        raise
                    if len(provided) == 0:
                        print("Broken for " + key + " " +
                              tagName)  # + ":" + errorString)
                        tagsDict[tagName] = thisTagBackup
                        continue
                    thisTag["libs"] = provided
                    thisTag[entrykind] = sha
                    if "broken" in thisTag:
                        del thisTag["broken"]
                # level = getSupportLevel(tagName, entry["support"])
                # thisTag["support"] = level
            serverdata[key]["refs"] = tagsDict
        else:
            raise Exception("Don't know how to handle entry for %s: %s" %
                            (key, entry))
    with open("rawdata.json", "w") as io:
        json.dump(serverdata, io, sort_keys=True, indent=2)
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()
Ejemplo n.º 21
0
#! /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)
Ejemplo n.º 22
0
#! /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)
Ejemplo n.º 23
0
 def worker():
   origDir = os.getcwd()
   os.chdir(self.tmp)
   m = OMPython.ModelicaSystem("M.mo", "M")
   m.simulate()
   os.chdir(origDir)
Ejemplo n.º 24
0
def closeSimulationPlugin():
    try:
        OMPython.execute("quit()")
    except SystemExit:
        pass
Ejemplo n.º 25
0
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
Ejemplo n.º 27
0
	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():
    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
Ejemplo n.º 29
0
#!/usr/bin/env python3

import os.path
import json
import OMPython

with open(os.path.expanduser("~/.openmodelica/libraries/index.json")) as f:
    data = json.load(f)
om = OMPython.OMCSessionZMQ()
for lib in data['libs'].keys():
    om.sendExpression("installPackage(%s)" % lib)
    s = om.sendExpression("getErrorString()").strip()
    if s:
        print(s)
Ejemplo n.º 30
0
# file: test.py
#!/usr/bin/python
import OMPython
cmds =  ["loadModel(Modelica)",
        "model test end test;",
        "loadFile(\"../OpenBLDC/package.mo\")",
        "simulate(OpenBLDC.Tests.TestPWM, fileNamePrefix=\"Result\", stopTime=0.25)",
        "plotAll()"]

_omc = OMPython.OMCSession()
for cmd in cmds:
        answer = _omc.sendExpression(cmd)
        print "\nResult:\n%s" %answer
        if not answer:
                print _omc.sendExpression("getErrorString()")


Ejemplo n.º 31
0
def stop_openmodelica_server():
    OMPython.execute("quit()")