FrankDev
FrankDev

Reputation: 17

I override variables with OMPython but the simulation starts with the default values

I'm trying to work on a system made in openmodelica and now i want to simulate it multiple time. On every iteration I have to change some value and i'm using OMPython to do it. This is the part of code where i override the interested parameters:

#Overwrite parameters
    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")
        for i in range(len(fault)):
            for j in range(len(fault[i])):
                f.write("fault.transMatrix["+str(i+1)+","+str(j+1)+"]="+str(fault[i][j])+"\n")
        for i in range(3):
            f.write("const.flyZone["+str(i+1)+"]="+str(flyZone)+"\n")
        f.flush()
        os.fsync(f)
 
    os.system("./System -overrideFile=newValues.txt >> LogOverride.txt")

Now, if i try to take all the values overrided from the .mat file, for example, with this method:

    print(str(omc.sendExpression("val(const.N," + str(stopTime) + ", \"System_res.mat\")")))

They are changed, but the simulation starts with the default value.

For example, if i set

const.N = 7

and in the .om file it is

const.N = 5

The simulation count N as 5 and not 7.

For more info, this is the github repository: https://github.com/BigMautone/Drones.git

The python script is in pythonScripts/testing.py and most of the parameter changed is in constant.mo

EDIT 1: For more context, i'm explaining the purpose of the system and why i can't see the changes made it with the script. The system have to simulate a swarm of drones and implements different algorithms for pathfinding and for obstacle avoidance. Inside the record class K there are all the parameters used in the system, like the N parameter, defining the number of drones in the swarm. Because i want to simulate the system multiple times with different parameters, i have to change it. So, if the default value of K.N is 5 and i'm changing it with 7 inside the python script, i'm expecting to see 7 drones doing something. Instead, the system is simulated with the default values!

i'm adding here part of the code and where i can't see the changes made with the overrideFile option.

"""
Here i load all the file and model needed...
"""
omc.sendExpression("buildModel(System, stopTime=180)")
omc.sendExpression("getErrorString()")

#This is the function i made for run the simulation multiple times
def startSimulation(n, intr, miss, statObs, fault, flyZone):
    #Overwrite parameters
    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")
        for i in range(len(fault)):
            for j in range(len(fault[i])):
                f.write("fault.transMatrix["+str(i+1)+","+str(j+1)+"]="+str(fault[i][j])+"\n")
        for i in range(3):
            f.write("const.flyZone["+str(i+1)+"]="+str(flyZone)+"\n")
        f.flush()
        os.fsync(f)

    os.system("./System -overrideFile=newValues.txt >> LogOverride.txt")
    os.system("rm -f newValues.txt")      # .... to be on the safe side
    
    #Down there i extract some values...
    for j in range(1,n+1):
        arrivalTime = omc.sendExpression("val(sucMo.arrivalTime[" + str(j) + "]," + str(stopTime) + ", \"System_res.mat\")")
        droneArrived = omc.sendExpression("val(sucMo.arrived[" + str(j) + "]," + str(stopTime) + ", \"System_res.mat\")")
        droneInfo[j] = (droneArrived, arrivalTime) 


#Here i call the function and try to execute it
startSimulation(7,1,1,1,noFault,100)


If i print the droneInfo dictionary, it will have obviously 7 keys, but if the default value N is equal to 5, then i'll get this output:

{1: (1.0, 8.0), 2: (1.0, 5.0), 3: (1.0, 8.0), 4: (1.0, 9.0), 5: (1.0, 12.0), 6: ('NaN', 'NaN'), 7: ('NaN', 'NaN')} 0.0

EDIT 2: I have made some changes. I had not instantiated the K class, which i use a lot for array lenght and for loops. Now that i've done it in all other models, the problem persists. Also, now all the value of K class have the isValueChangeble flag to false

Upvotes: 0

Views: 300

Answers (1)

Adrian Pop
Adrian Pop

Reputation: 4231

I tried to reproduce this. Made this small script to read the value at the end:

adrpo33@ida-0030 MINGW64 /c/home/adrpo33/dev/modelica/Drones
# cat val.mos
val(const.N, 9, "System_res.mat"); getErrorString();

Then ran the run.mos script:

adrpo33@ida-0030 MINGW64 /c/home/adrpo33/dev/modelica/Drones
# omc run.mos

record SimulationResult
    resultFile = "C:/home/adrpo33/dev/modelica/Drones/System_res.mat",
    simulationOptions = "startTime = 0.0, stopTime = 30.0, numberOfIntervals = 500, tolerance = 1e-06, method = 'dassl', fileNamePrefix = 'System', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = ''",
    messages = "LOG_SUCCESS       | info    | The initialization finished successfully without homotopy method.
[C:/home/adrpo33/dev/modelica/Drones/Monitors/MonitorSuccess.mo:52:28-52:116:writable]
stdout            | info    | Simulation call terminate() at time 9.000000
|                 | |       | Message : Tutti i droni hanno raggiunto la destinazione oppure hanno avuto collisioni
LOG_SUCCESS       | info    | The simulation finished successfully.
",
    timeFrontend = 0.3274193,
    timeBackend = 1.4592339,
    timeSimCode = 0.5092729,
    timeTemplates = 0.1533226,
    timeCompile = 24.1450323,
    timeSimulation = 3.3621366,
    timeTotal = 29.9572641
end SimulationResult;
"Warning: The initial conditions are not fully specified. For more information set -d=initialization. In OMEdit Tools->Options->Simulation->Show additional information from the initialization process, in OMNotebook call setCommandLineOptions(\"-d=initialization\").
"

Take the value out:

adrpo33@ida-0030 MINGW64 /c/home/adrpo33/dev/modelica/Drones
# omc val.mos
5.0
""

Override the value:

adrpo33@ida-0030 MINGW64 /c/home/adrpo33/dev/modelica/Drones
# ./System.exe -override const.N=7
LOG_SUCCESS       | info    | The initialization finished successfully without homotopy method.
[C:/home/adrpo33/dev/modelica/Drones/Monitors/MonitorSuccess.mo:52:28-52:116:writable]
stdout            | info    | Simulation call terminate() at time 9.000000
|                 | |       | Message : Tutti i droni hanno raggiunto la destinazione oppure hanno avuto collisioni
LOG_SUCCESS       | info    | The simulation finished successfully.

Take the value out:

adrpo33@ida-0030 MINGW64 /c/home/adrpo33/dev/modelica/Drones
# omc val.mos
7.0
""

So I cannot really reproduce your behavior. When I change something the .mat file reflects the change.

Upvotes: 2

Related Questions