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

Indexing with Boolean or Enumeration Values

Indexing with Boolean or Enumeration Values

I wanted to implement an object using an array indexed by an enumeration value, as described in Section 10.5.1 of the Modelica Language Specification (`Indexing with Boolean or Enumeration Values'), but I couldn't quite get it to work.

That is, this works:


type Sizes = enumeration (small, medium);

model Test
  constant Real[Sizes] sz = {1.2, 3.5};
  parameter Real x0 = sz[Sizes.small];
  Real x;
equation
  x = x0;
end Test;



but it's not very interesting, since what I'd like to be able to do is instantiate a "Test" that is either small or medium (i.e. in the instantiation override the index to sz which goes into x0, not the value of x0 directly), but how I thought to do this fails:



type Sizes = enumeration (small, medium);

model Test
  constant Real[Sizes] sz = {1.2, 3.5};
  //parameter Real x0 = sz[Sizes.small];
  parameter Sizes size = Sizes.small;
  parameter Real x0 = sz[size];
  Real x;
equation
  x = x0;
end Test;



This does flatten happily enough (or is there already a sign of the problem in that the indices in the definition of the array have been converted to integers but the ones pertaining to accessing the array during the assignment to x0 haven't?):


class Test
  constant Real sz[1] = 1.2;
  constant Real sz[2] = 3.5;
  parameter enumeration(small, medium) size = Sizes.small;
  Real x;
  parameter Real x0 = sz[Sizes.small];
equation
  x = x0;
end Test;



but then compiling gives an error:


Test.c: In function ‘bound_parameters’:
Test.c:309:10: error: ‘$Psz$lBUNKNOWN_SUBSCRIPT$rB’ undeclared (first use in this function)



Since I actually only have two states in the enumeration in my application, I then tried to use a Boolean instead, again following the same section of the Specification, but


Real[Boolean] bb = {10.0, 11.0};



wouldn't even flatten:


[Test.mo:2:3-2:34:writable] Error: Variable Boolean not found in scope Test



Have I misunderstood the Modelica Language Specification, or is Indexing with Boolean and Enumeration Values not yet completely supported by OpenModelica? In the latter case, could anyone suggest a convenient workaround?

Thanks.

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