modelica / ModelicaSpecification

Specification of the Modelica Language
https://specification.modelica.org
Creative Commons Attribution Share Alike 4.0 International
102 stars 41 forks source link

Remove unnecessary restrictions to vector expressions #2208

Closed modelica-trac-importer closed 5 years ago

modelica-trac-importer commented 6 years ago

Reported by henrikt on 13 Oct 2017 11:32 UTC Searching the specification for vector expression (also including vector-expression) gives 12 matches in three different contexts:

I am unable to find the definition of vector expression, but if recall correctly it is used to mean an expression that evaluates to an array of scalars.

I find the restriction to vector expressions unnecessary in the case of modifiers as well as in the case of looping constructs.

The restriction still makes sense in the cases of when conditions and array subscripts, but I find the two to have different needs. For the case of when conditions, I doubt that the intention is to allow any expression that evaluates to an array of scalars (like a function returning an array with dimensions depending in its inputs), but that the intention is to allow several scalar expressions separated by commas and enclosed in braces. For the case of array subscripts, I allowing expressions that evaluate to 1-dimensional arrays of indices is useful, and probably the intention of the specification.

Before giving the reasons for removing the restriction for modifiers and looping constructs, I would suggest removing the terminology of vector expression altogether from the specification, and rewrite the remaining occurrences in terms of existing terminology (for example, … scalar expression or comma-separated list of scalar expressions enclosed in braces, or expression of array with scalar elements).

For the case of modifiers, the given example is

model C
  parameter Real a [3];
  parameter Real d;
end C;
model B
  C c[5](each a ={1,2,3}, d={1,2,3,4,5});
  parameter Real b=0;
end B;

I don't see why the following shouldn't be allowed:

type T = Real[2];
model C
  parameter T[3] a;
end C;
model B
  parameter T t0 = {1, 2};
  parameter T t1 = {3, 4};
  parameter T t2 = {5, 6};
  C c[5](each a = {t1, t2, t3});
end B;

Similarly, if I have a type T, a function from T to Real, and a vector with elements of type T; why should I not be able to do a sum reduction of my elements?

partial model ReductionOfVectors

  type T = Real[3];

  function f
    input T x;
    output Real y;
  algorithm
    y := x[1] * x[2] + x[3];
  end f;

  T[5] xx;
  Real acc;

equation

  acc = sum( f(x) for x in xx );

end ReductionOfVectors;

All in all, removing the unnecessary restrictions seems like a necessary (and welcome) consequence of not making a difference between nested and multi-dimensional arrays.


Migrated-From: https://trac.modelica.org/Modelica/ticket/2208

modelica-trac-importer commented 6 years ago

Modified by hansolsson on 13 Oct 2017 15:48 UTC

modelica-trac-importer commented 6 years ago

Comment by hansolsson on 19 Oct 2017 15:20 UTC Preliminary agree - except prefer when having array of Boolean scalars with size being parameter expression [that can be evaluated at compile time]. But should test-implement treating e.g. for x in {{1,2},{3,4}} before final decision.

modelica-trac-importer commented 6 years ago

Comment by henrikt on 19 Oct 2017 15:23 UTC Here are fixed variants of the examples above. These already work in SystemModeler.

package P

  type T = Real[2];

  model C
    parameter T[3] a;
  end C;

  model B
    parameter T t1 = {1, 2};
    parameter T t2 = {3, 4};
    parameter T t3 = {5, 6};
    C c[5](each a = {t1, t2, t3});
  end B;

end P;
model ReductionOfVectors

  type T = Real[3];

  function f
    input T x;
    output Real y;
  algorithm
    y := x[1] * x[2] + x[3];
  end f;

  T[5] xx;
  Real acc;

equation

  acc = sum( f(x) for x in xx );
  der(xx) = ones(5, 3);

end ReductionOfVectors;
modelica-trac-importer commented 6 years ago

Comment by stefanv on 19 Oct 2017 15:35 UTC The first example also already works in MapleSim. The second currently gives (as currently expected):

Error, invalid sum reduction range expression: ReductionOfVectors.xx

modelica-trac-importer commented 6 years ago

Comment by sjoelund.se on 19 Oct 2017 15:40 UTC Both already work in OpenModelica (second flattens to acc = ReductionOfVectors.f({xx[1,1], xx[1,2], xx[1,3]}) + ReductionOfVectors.f({xx[2,1], xx[2,2], xx[2,3]}) + ReductionOfVectors.f({xx[3,1], xx[3,2], xx[3,3]}) + ReductionOfVectors.f({xx[4,1], xx[4,2], xx[4,3]}) + ReductionOfVectors.f({xx[5,1], xx[5,2], xx[5,3]});)

modelica-trac-importer commented 6 years ago

Modified by beutlich on 10 Jan 2018 08:23 UTC

HansOlsson commented 5 years ago

I thought more about this, and regarding when-constructs and array indexing it could be clearer, but it seems the intent is "vector of Booleans" and "vector of Integers".

E.g., "The expression of a when-statement shall be a discrete-time Boolean scalar or vector expression." makes sense if "scalar or vector" is sort of grouped together. I don't know how to formulate that in the best way.

HansOlsson commented 5 years ago

And it has now been implemented in Dymola and 3D Experience Platforbm, in case we want to add it. Allowing the iterator expression to be a vector/matrix was fairly straightforward when we already allow records, #1521.