1

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

Hâla
  • 11
  • 2
  • Maybe these parameters get evaluated? Or Dymola considers them as structural variables? Also see https://stackoverflow.com/questions/35907544/current-version-of-the-modelica-translator-can-only-handle-array-of-components-w/35909692#35909692 and https://stackoverflow.com/questions/32739428/modelica-parameter-studies-with-python – matth Dec 06 '17 at 08:13
  • You should also try and translate the model directly in the Dymola GUI, and then check whether you can change the parameters between simulations without translating again. – matth Dec 06 '17 at 09:03
  • I can't change the parameters without translating because every change in the parameters leads to a change in the system of equations studied. I tried to set in Dymola: parameter Real eq1=0 annotation(Evaluate=false); parameter Real eq2=1 annotation(Evaluate=false); but this doesn't work , i get errors: Errors or failure to expand vector or matrix expressions. – Hâla Dec 06 '17 at 09:27
  • So this is not Python related, it is a pure Modelica issue. Certain changes just require a new translation. – matth Dec 06 '17 at 10:19
  • Can you please give me some advice of how do i make a new translation possible? – Hâla Dec 08 '17 at 09:37

1 Answers1

1

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.

Sid
  • 11
  • 1