Hâla
Hâla

Reputation: 11

Translate model before Simulating it in Modelica using Python

I'm trying to run Dymola using Python, each time i change the values of the parameters, different equations are used through many "if elseif" that activates and disactivates the corresponding equations.

Im using this code:

def simulateModel(model, startTime, stopTime, resultFile, initialNames, initialValues, finalNames):

output = dymola.simulateExtendedModel(model,
                                      startTime=startTime,
                                      stopTime=stopTime,
                                      numberOfIntervals=500,
                                      outputInterval=0.0,
                                      method="Dassl",
                                      tolerance=0.0001,
                                      fixedstepsize=0.0,
                                      resultFile=resultFile,
                                      initialNames=initialNames,
                                      initialValues=initialValues,
                                      finalNames=finalNames,
                                      autoLoad=False)

status = output[0]

if not status:
    print("Simulation failed.")
    log = dymola.getLastError()
    print(log)
    return

result = output[1]
return result

The problem is that when i define my new values for the parameters, the script changes them in the model, but it keeps using the same set of equations of the last time i have ran the program on Dymola directly, which is not the case, the program is not taking into consideration that the parameters changes the system of equations to be used.

Any suggestion? Regards

Upvotes: 1

Views: 955

Answers (1)

Sid
Sid

Reputation: 11

Here's how I do it:

dymola = DymolaInterface() 

## This sends the string right to Dymola, in this case to open a Model.
dymola.ExecuteCommand('DymolaCommands.SimulatorAPI.openModel("Path\To\your\file.mo", changeDirectory=false);')

## Now Translate the Model for every time you want to assign certain variables
ModelTranslate = "Dymola.Path.To.Your.Model(Dymola.Path.to.Variable=1000)"
dymola.translateModel(ModelTranslate)
output = dymola.simulateExtendedModel("", 0.0, 2678400, 0, 0.0, "Dassl", 0.0001, 0.0, Result, [], [], ["Path.To.Output" ], True)

However, I only use the interface with Python 3 and I vaguely remember it saying something in the User Manual about only being compatible with Python 3. But you should find that in the User Manual 2 under other programming environments.

Upvotes: 1

Related Questions