Search code examples
arraysvectormodelica

How to insert arguments of a vector inside another vector?


I have a vector of "barycenter" with size "5":

parameter Length barycenters[5] = {1, 2, 3, 4, 5};

and I would like to add the arguments of this vector inside another vector (b_prime) with size "7", meaning that intend to have b_prime = {0, 1, 2, 3, 4, 5, 10}. How is it possible to write a code line in Modelica to do so? I have tried the followings but I received errors:

parameter Length b_prime[7] = {0, barycenters[1:5], 10}; 

or

parameter Length b_prime[7] = {0, barycenters, 10}; 

I would appreciate your help.


Solution

  • The following code should do what you want:

    model CombineVectors
      import Modelica.SIunits.Length;
    
      parameter Length barycenters[5] = {1, 2, 3, 4, 5};
      parameter Length b_prime[7] = cat(1, {0}, barycenters, {10});
    
    end CombineVectors;
    

    Two important things to notice:

    1. The first argument of cat() is the dimension in which the arrays shall be concatinated
    2. The scalars are put in curly brackets to make them a vector. This is done to fulfill the first of the "rules" found below. Therefore every argument of cat() except the first has the one dimension.

    From the Modelica Language Specification, Section 10.4.2 Array Concatenation:

    The function cat(k,A,B,C,...) concatenates arrays A,B,C,... along dimension k according to the following rules:

    • Arrays A, B, C, ... must have the same number of dimensions, i.e., ndims(A) = ndims(B) = ...
    • Arrays A, B, C, ... must be type compatible expressions (Section 6.6) giving the type of the elements of the result. The maximally expanded types should be equivalent. Real and Integer subtypes can be mixed resulting in a Real result array where the Integer numbers have been transformed to Real numbers.
    • k has to characterize an existing dimension, i.e., 1 <= k <= ndims(A) = ndims(B) = ndims(C); k shall be an integer number.
    • Size matching: Arrays A, B, C, ... must have identical array sizes with the exception of the size of dimension k, i.e., size(A,j) = size(B,j), for 1 <= j <= ndims(A) and j <> k.