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
  • Index
  • » Users
  • » cvan
  • » Profile

Posts

Posts

Jul-14-17 12:36:54
Category: Programming

Actually, ExternalObject does not help. For example, in the following code, I want to call test(5) with no input for 'obj' but what should be the default value for the 'obj'?

Code:


function test
   input Integer N;
   input ExtObj obj = ???;
algorithm
end test;

class TestModel
algorithm
    test(5)
end TestModel;

Jul-14-17 12:02:58
How to cache in Modelica
Category: Programming

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;

Jun-21-17 18:36:35
Category: Programming

Thank you! I saw External Object before but thought it were some kind of interface between Modelica and C. I did not know that ExternalObject can act as a global variable in Modelica.

Jun-21-17 17:35:53
Category: Programming

Hey all,

So I am implementing a caching mechanism. Modelica does not have global variable so I need to pass this cache around. However, I want this cache input to be optional in a function, like

function tempFunc
   input Cache cache = NULL;
end tempFunc;

But Modelica does not have NULL. Any suggestion? Thank you!

  • Index
  • » Users
  • » cvan
  • » Profile
You are here: