Search code examples
pythonmodelicaopenmodelica

How to override an array in a modelica system with OMPython?


I'm working on a project, using openmodelica. I'm trying to simulate the system multiple times and i wrote a python script to automatize it. I also override parameters on each iteration of the simulation, but if i try to override an array i'll get this on console:

Traceback (most recent call last):
  File "pythonScripts/testing.py", line 164, in <module>
    startSimulation(5,2,1,4, noFault,flyZone[1])
  File "pythonScripts/testing.py", line 118, in startSimulation
    vars = omc.sendExpression("readSimulationResult(\"System_res.mat\",{time})")
  File "/home/francesco/.local/lib/python3.8/site-packages/OMPython/__init__.py", line 779, in sendExpression
    answer = OMTypedParser.parseString(result)
  File "/home/francesco/.local/lib/python3.8/site-packages/OMPython/OMTypedParser.py", line 120, in parseString
    res = omcGrammar.parseString(string)
  File "/home/francesco/.local/lib/python3.8/site-packages/pyparsing/core.py", line 1143, in parse_string
    raise exc.with_traceback(None)
pyparsing.exceptions.ParseException: Expected end of text, found '('  (at char 4), (line:1, col:5)

This is an example of how i did it:

def startSimulation(n, intr, miss, statObs, fault, flyZone):
  with open("newValues.txt", 'wt') as f:
        f.write("const.N="+str(n)+"\n")
        f.write("const.nIntr="+str(intr)+"\n")
        f.write("const.nRocket="+str(miss)+"\n")
        f.write("const.nStatObs="+str(statObs)+"\n")
        f.write("fault.transMatrix[4,4]="+str(fault)+"\n")
        f.write("const.flyZone[3]="+flyZone+"\n")
        f.flush()
        os.fsync(f)
  os.system("./System -overrideFile=newValues.txt >> LogOverride")

noFault = "[1, 0, 0, 0; 1, 0, 0, 0; 1, 0, 0, 0; 1, 0, 0, 0]"
flyZone = ["{100,100,100}","{150,150,150}","{200,200,200}"]

startSimulation(5,2,1,4, noFault,flyZone[1])

Any suggestion?


Solution

  • OpenModelica will expand arrays into scalars when the C code is generated so you need to override each of the array elements separately in newValues.txt:

    fault.transMatrix[1,1]=1
    fault.transMatrix[1,2]=0
    fault.transMatrix[1,3]=0
    fault.transMatrix[1,4]=0
    fault.transMatrix[2,1]=1
    fault.transMatrix[2,2]=0
    fault.transMatrix[2,3]=0
    fault.transMatrix[2,4]=0
    ...