Archived OpenModelica forums. Posting is disabled.

Alternative forums include GitHub discussions or StackOverflow (make sure to read the Stack Overflow rules; you need to have well-formed questions)


Forgot password? | Forgot username? | Register

Newbie: initializing state variables from OMShell

Newbie: initializing state variables from OMShell

Suppose I want to run a simulation in OMShell (for instance, the Bouncing Ball demo simulation), but I want to play with different initial values of a state variable (h from the Bouncing Ball model). There must be a way of initializing h to a value without manually modifying and reloading the .mo file, but there doesn't seem to be any documentation on it, and it isn't at all obvious how to go about it. What's the trick?

Re: Newbie: initializing state variables from OMShell

Re: Newbie: initializing state variables from OMShell

Where do I find it, and how do I load it in OMEdit?

Re: Newbie: initializing state variables from OMShell

Excuse me, that should be how do I load it in OMShell?

Re: Newbie: initializing state variables from OMShell

Simulate the model to get the init xml file then call setInitXmlStartValue to update the start value of h then use the system command to run the simulation executable again.

Code:


setInitXmlStartValue("BouncingBall_init.xml", "h", "5", "BouncingBall_init.xml")
system("BouncingBall.exe")

Adeel.

Re: Newbie: initializing state variables from OMShell

Hi,

You can also use: buildModel to build the model and then run from command line or via system command:

Code:


./Model.exe -override h=5

Cheers,
Adrian Pop/

Re: Newbie: initializing state variables from OMShell

Maybe I'm missing something here, because the above don't make sense to me...

What I *think* I would like to do is enter an OMShell interactive session, load a model, set some initial values, and run a simulation. Something like:

> loadFile("BouncingBall.mo")
> h(start)=5
> simulate(BouncingBall, startTime=0.0, stopTime=10.0)
> plot(h)

I can run the commands in Adeel's example, and they seem to execute, but make no difference. Every time I run the simulate command, h is initialized to 0. No XML file seems to get loaded. Adrian's suggestion is for how to do this from a terminal shell, not, it seems, from OMShell.

If the preferred way to interact with modelica is from the command line, then that's OK, but if that's the case I'm confused about what OMShell is for. As I try to learn this, I feel like I'm missing a piece of documentation that tells me how to use OMShell. I get the language just fine, it's great, and it's well documented. But the tools that surround it don't seem to be documented well anywhere. Where should I look?

Re: Newbie: initializing state variables from OMShell

Run the following commands one by one in OMShell,

Code:


loadFile("BouncingBall.mo")
simulate(BouncingBall, startTime=0.0, stopTime=10.0)
plot(h)
setInitXmlStartValue("BouncingBall_init.xml", "h", "5", "BouncingBall_init.xml")
system("BouncingBall.exe")
plot(h, externalWindow=true)

When we simulate the model the init xml file is created which contains the start values. The setInitXmlStartValue command updates the file with new values and then we just simply run the simulation executable. If we run the simulate command after setInitXmlStartValue command then it will override the values back to the original values.
If you want to use the Adrian's suggestion,

Code:


loadFile("BouncingBall.mo")
buildModel(BouncingBall, startTime=0.0, stopTime=10.0)
system("BouncingBall.exe")
plot(h)
system("BouncingBall.exe -override h=5")
plot(h, externalWindow=true)

Adeel.

Re: Newbie: initializing state variables from OMShell

First, I really appreciate the help.

When I run either set of code, everything seems to execute - I don't get any errors - but the output is identical. h is not initialized to 5, it is initialized to 1.

In the directory where I have saved the BouncingBall.mo file, after doing the simulate or buildModel, there are a lot of generated files, but "BouncingBall.exe" isn't one of them. Repeat: there does not seem to be a BouncingBall.exe file anywhere I look. BouncingBall.c, BouncingBall.o, BouncingBall.libs, and so on, but no executable.

For what it's worth, I'm running OSX and installed using the openmodelica-devel port.

Re: Newbie: initializing state variables from OMShell

You will obviously won't get BouncingBall.exe on OSX current/smile. It will be BouncingBall. Remove .exe from the commands.

Adeel.

Re: Newbie: initializing state variables from OMShell

Yeah, I tried that. There *is* a "BouncingBall" file, and it is an executable and does run. But the above commands *still* don't seem to change the initial value of h. I always get the identical plot.

Re: Newbie: initializing state variables from OMShell

I'm guessing, but I think the problem may be that the system("foo") command isn't doing what I think it should be doing, ie it doesn't seem to actually execute BouncingBall. It returns "127" - what does that indicate?

In fact, the result is identical whether I do a system("BouncingBall") or a system("foo"), where foo does not exist - I still get a 127 return value.

Re: Newbie: initializing state variables from OMShell

Aha, success. It requires a fully qualified path name. Running system("Users/myname/Code/modelica/examples/BouncingBall -override h=5") works.

This is, needless to say, a pain in the butt. Is there any way to modify the behavior?

Re: Newbie: initializing state variables from OMShell

Hi,

You should be in that current directory so system("./BouncingBall -override h=5") should work.
Or add ./ to the PATH variable.

Why don't you use OMEdit?
There is another way to modify the variable bindings and start values using the API as OMEdit does it

Code:


loadModel("BouncingBall.mo"); getErrorString();
list(BouncingBall); getErrorString(); // h should have the value from the file
// to modify a parameter or variable binding use:
// setParameterValue(BouncingBall, h, 5): getErroString();
// to modify a variable start value use:
setComponentModifierValue(BouncingBall, h.start, $Code(=5));  getErrorString();
list(BouncingBall); getErrorString(); // h should have h(start=5)
simulate(BouncingBall);
// plot ....

Re: Newbie: initializing state variables from OMShell

I don't use OMEdit because it crashes on startup on my machine. I'm running OSX 10.8.2 and I'm using the openmodelica-devel port. OMNotebook also doesn't work, or at least that's what the port documentation says. So OMShell is the only interactive environment I have available.

There are 0 guests and 0 other users also viewing this topic
You are here: