Please consider the following small Modelica model and function:
model VectorizeDemo
parameter Integer na=5;
final parameter Integer nb=2*na;
final parameter Real a[na] = {2*i for i in 1:na};
final parameter Real b[nb] = {3*i for i in 1:nb};
Real c[na];
Real d[na,nb];
protected
function myFun
input Real A;
input Real B;
output Real C;
algorithm
C:=tanh(A)*sin(B);
end myFun;
equation
c = sin(a);
//d = myFun(a,b);
// inner loop first
d = {myFun(a[i], b[j]) for j in 1:nb, i in 1:na};
end VectorizeDemo;
This will compile and simulate in Dymola, but looking at the C code in dsmodel.c every array element is declared as a new variable:
...
DeclareVariable("d[4, 10]", "", 38.0, 0.0,0.0,0.0,0,513)
DeclareVariable("d[5, 1]", "", 13.0, 0.0,0.0,0.0,0,513)
DeclareVariable("d[5, 2]", "", 16.0, 0.0,0.0,0.0,0,513)
DeclareVariable("d[5, 3]", "", 19.0, 0.0,0.0,0.0,0,513)
...
So, if I increase the array size by setting na=1000 I will have 1000*2000 variables declared. The shown example will still compile, even though it takes very long, but my more complex use case fails with compiler warning C4049: compiler limit, terminating line number emission or with C1002 compiler is out of heap space.
Sidenotes: The larger example will also take several minutes to check, and after simulation the GUI will be blocked for ages when unfolding the variables in the variable browser.
Is there any workaround, like rewriting my code or setting some flag? Temporarily increase heap space? I need to run the model only once. Any insight in what is happening would also be appreciated. Using Dymola 2020, with VisualStudio 2017.
Yes, at least the compilation issue can preliminarily be avoided as follows in Dymola 2020 (and possibly earlier versions):
Real d[na,nb] annotation(__Dymola_HideArray=true);
However, there might be other possibilities as well - and the example doesn't fully clarify how it is intended be used. In particular I noted that 'd' can be evaluated and isn't used at all.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With