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

'Cache' in Modelica

'Cache' in Modelica

I have the following simple codes for caching. Basically, I need a cache that can be called inside from Modelica functions (ComputationClass.ar in this code). Modelica functions, however, cannot access variables except constant ones. One solution is to make the cache from the upmost level and pass the cache around. However, in my simulation, there are lots of places using the cache, so it is very tedious and inconvenient to pass it around, not to mention if the cache is large, Modelica gives memory error.

I also tried to use ExternalObject, but the problem is still there. I have to pass the external object around between the function. I tried to use 'constant' in front of the ExternalObject but Modelica constant object is not 'constant object' like in Java or C++. Modelica constant object re-initialized itself everytime I call that object. I guess it is just a pointer to an object.

I tried Inner/Outer but Inner/Outer only works between models/classes but not with 'function'.

It is interesting that such a small caching mechanism is so difficult in Modelica. Please, any advice or suggestion is appreciated! I have been working on this for a few days!

Code:


package CacheToy
  constant Integer N = 50;

  class MainClass
    //outer Real cache[N];
    ComputationClass cc;
    function pbflash
      input Real smth;
      input ExtCache inCache;
    algorithm
      for i in 1:3 loop
      //Modelica.Utilities.Streams.print(String(smth) + " " + String(ComputationClass.ar(cache[1,1])));
        Modelica.Utilities.Streams.print(String(cc.ar(smth+i, inCache)));
      end for;
    end pbflash;
  end MainClass;

  class ComputationClass
    //outer Real cache[N];

    function retrieveCache
      input Integer j;
      input ExtCache inCache;
      output Real ret;
    external "C" ret = retrieveCache(inCache, j)
    annotation(IncludeDirectory="modelica://MEMedia",
               Include="#include \"ToyCache.c\"");
    end retrieveCache;

    function ar
      input  Real t;
      input ExtCache inCache;
      output Real ret;
    algorithm
      for j in 1:3 loop
        //ret := ret + t^2;
        //ret := ret + t^2 + cache[1];
        ret := ret + t^2 + retrieveCache(1, inCache);
      end for;
    end ar;
  end ComputationClass;

  class ExtCache
    extends ExternalObject;
    function constructor
      input  Integer N;
      output ExtCache cache;
      external "C" cache=createCache(N)
        annotation(IncludeDirectory="modelica://MEMedia",
                   Include="#include \"ToyCache.c\"");
    end constructor;

    function destructor "Release storage"
      input ExtCache cache;
      external "C" destroyCache(cache)
        annotation(IncludeDirectory="modelica://MEMedia",
                   Include="#include \"ToyCache.c\"");
    end destructor;
  end ExtCache;

  model ToyModel
    //inner Real cache[N];
  protected
    MainClass obj1;
    ExtCache  cache = ExtCache(N);

    //     function fillCache
    //       output Real[N] ret;
    //
    //     algorithm
    //       for i in 1:N loop
    //         ret[i] :=1;
    //       end for;
    //     end fillCache;
  algorithm
    //cache := fillCache();
    for j in 1:2 loop
      obj1.pbflash(j, cache);
    end for;
  end ToyModel;
  annotation (Icon(coordinateSystem(preserveAspectRatio=false)), Diagram(
        coordinateSystem(preserveAspectRatio=false)));
end CacheToy;

Edited by: cvan - Jul-14-17 12:05:12
There are 0 guests and 0 other users also viewing this topic
You are here: