Help - Search - Members - Calendar
Full Version: filling arrays
Modelica Forum > Modelica > Modelica
noname
Hi everyone
I am new to modelica/Dymola and i have a question about arrays.
I have seen in the Modelica language definition (V2.2) the following expression:
QUELLTEXT

Integer i;
Integer sign_of_i1=if i<0 then -1 elseif i==0 then 0 else 1;


Now I want to do something more or less similar with an array but it doesn't work. As you can see in the folowing code I want to use Parameter n to define the length of my Array and in a second step I want to fill the Array where the if-expression should take care of the length of the assigned Vector (The Values 1,2,3 and 4 are examples so lam=1:n is not a solution for me cool.gif ).

QUELLTEXT

parameter Integer n;
parameter Real lam[n] if n==4 then [1,2,3,4] elseif n==3 then [1,2,3] elseif (n==2) then [1,2] else [1];


Unfortuantely this piece of code causes the following error message:

ZITAT

Error: declaration window line 69 column 35, syntax error at "then" missing { EndOfFile IMPORT CLASS_ MODEL RECORD BLOCK CONNECTOR EXPANDABLE TYPE PACKAGE EXTERNAL FUNCTION END EXTENDS REPLACEABLE PARTIAL ENCAPSULATED REDECLARE EQUATION FINAL PUBLIC PROTECTED ALGORITHM ANNOTATION INNER OUTER INITIAL IDENT STRING type-prefix ) ; , }
Error: declaration window line 69 column 43, syntax error at "2" missing IDENT
Error: declaration window line 69 column 45, syntax error at "3" missing IDENT
Error: local redefinition of Error
Location of previous declaration:
Error: declaration window line 69 column 47, syntax error at "4" missing IDENT
Error: local redefinition of Error
Location of previous declaration:
Error: declaration window line 69 column 70, syntax error at "2" missing IDENT
Error: local redefinition of Error
Location of previous declaration:
Error: declaration window line 69 column 72, syntax error at "3" missing IDENT
Error: local redefinition of Error
Location of previous declaration:
Error: declaration window line 69 column 87, syntax error at ")" missing ;
Error: declaration window line 69 column 94, syntax error at "[" missing { EndOfFile EXTERNAL END }


What do i do wrong? Do you have any Idea?
Thanx in advance
Hannes Wagner
Carsten
Hi Hannes,

do not think to complicated. Modelica provides lots of possibilities to create array as you might know from Matlab.

To solve you question in the shortest way, you can write:
QUELLTEXT

parameter Integer n=4;
parameter Real lam[:] = 1:n;


maybe easier to understand the second option.

QUELLTEXT

parameter Integer n=4;
parameter Real lam[:] = {i for i in 1:n};


There are still more options, but the second one provides you a lot of posibilties to fill you array in the desired form.

Regards
Carsten
noname
Hi Carsten,
thx for your fast reply. I found a solution for this problem in between that i want to share but i think i shoud explain my problem a little bit betteer before.

Within Dymola i created a model to calculate the heatflow through a wall. It was set up in a way that you can could select the number of matherial in a dropdown menue as you can see in the Img. Depending of this number the dropdownmenues to slect the wall materials as well as the fields for the width are enabled. In this way i had plenty varables (eg delta_1...) that are pretty bad to handle.

IPB Bild

To get the values of these varables back in an array of the legth n i made a function like this function:

QUELLTEXT

function Fillarray "cool function"
  input Integer nn;
  input Real Value1;
  input Real Value2;
  input Real Value3;
  input Real Value4;
  output Real MyVec[nn];
algorithm
  for i in 1:nn loop
    if 1==1 then
      MyVec[1]:=Value1;
    end if;
    if 1==2 then
      MyVec[2]:=Value2;
    end if;
    if 1==3 then
      MyVec[3]:=Value3;
    end if;
    if 1==4 then
      MyVec[4]:=Value4;
    end if;
  end for;
end Fillarray;


so i could use it in my model like this:

QUELLTEXT

parameter Real delt[n]=Fillarray(n, delta_1, delta_2, delta_3, delta_4)



case solved.

Thanx again for the answer and Thanx as well for the forum
Hannes

ps: sorry somehow the image is a little small
Spartan Latoyia
why didn't you just call them about it?
noname
ZITAT(Spartan Latoyia @ Aug 26 2006, 07:11 PM) *

why didn't you just call them about it?

because they would have to hire plenty new guys if everyone in my University would call them. Other question: what are discussion boards for? huh.gif To ask and answer questions AFAIK. unsure.gif
regards
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2012 Invision Power Services, Inc.