mclements / ROpenModelica

R package to script OpenModelica

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ROpenModelica: an R package to script OpenModelica.

This is a small R package for scripting OpenModelica.

One can define a model using a string:

model <- "
model CalledbyR
Real x(start=1.0),y(start=2.0);
parameter Real b = 2.0;
equation
der(x) = -b*y;
der(y) = x;
end CalledbyR;"

Then the model can be simulated thus:

out <- callOpenModelica("CalledbyR", model)

The function callOpenModelica returns a data-frame, which can be plotted:

plot(y~time, data=out, type="l")

The default script can be altered to change different arguments to simulate(). So:

out <- callOpenModelica("CalledbyR", 
                        model, 
                        script=defaultScript("CalledbyR",stopTime=10))
plot(y~time, data=out, type="l")

This also allows for multiple simulations with different parameter values:

out <- callOpenModelica("CalledbyR",
                        model,
                        script=defaultScriptWithParameter("CalledbyR", "b", stopTime=10), 
                        values=c(0.5,1,2))
if (require(ggplot2))
   qplot(x=time, y=y, data=transform(out, value=factor(value)), color=value, geom="line")

About

R package to script OpenModelica


Languages

Language:R 100.0%