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

Writing Equations in nested for loop

Writing Equations in nested for loop

Hiii

I wanted to write equations in for loop! the code i have written is following

model prog
parameter Real rho =1.1839;
parameter Real dia = 0.08;
parameter Real Temp = 298;
parameter Real Cv = 718;
Real area_of_intake= 3.14*0.04*0.04/4;
Real Alpha[:,:];
Real Beta[:,:];
Real Delta[:,:];
for i in 1:10 loop
for j in 2:10 loop
Alpha[1,1]= 1.1839;
Delta[1,1]= 1.1839*Cv*298+0.5*Beta[1,1]/area_of_intake^2*1.1839;
Alpha[10,1]= 1.1839;
Delta[10,1]= 1.1839*Cv*298+0.5*Beta[10,j]/area_of_intake^2*1.1839;
der(Alpha[i,j])= Beta[i,j-1]-Beta[i,j];
der(Beta[i,j])= 0.4*area_of_intake^2*(Alpha[i,j]*Delta[i,j]-Alpha[i,j+1]*Delta[i,j+1])-0.8*(Beta[i,j]/Alpha[i,j]-Beta[i,j+1]/Alpha[i,j+1]);
der(Delta[i,j]= 1.4*(Beta[i,j+1]*Delta[i,j+1]/Alpha[i,j+1] - Beta[i,j+2]*Delta[i,j+2]/Alpha[i,j+2]);
end for;
end for;
end prog;

The model does not compile and gives error

Syntax    15:41:02        10:1-10:4    No viable alternative near token: for

Thanks for your help!!

Re: Writing Equations in nested for loop

You're missing an equation keyword I guess.

Re: Writing Equations in nested for loop

hii

i tried with equation also

model prog
parameter Real rho =1.1839;
parameter Real dia = 0.08;
parameter Real Temp = 298;
parameter Real Cv = 718; 
Real area_of_intake= 3.14*0.04*0.04/4;
Real Alpha[:,:];
Real Beta[:,:]; 
Real Delta[:,:];
for i in 1:10 loop
for j in 2:10 loop
equation
Alpha[1,1]= 1.1839;
Delta[1,1]= 1.1839*Cv*298+0.5*Beta[1,1]/area_of_intake^2*1.1839;
Alpha[10,1]= 1.1839;
Delta[10,1]= 1.1839*Cv*298+0.5*Beta[10,j]/area_of_intake^2*1.1839;
der(Alpha[i,j])= Beta[i,j-1]-Beta[i,j];
der(Beta[i,j])= 0.4*area_of_intake^2*(Alpha[i,j]*Delta[i,j]-Alpha[i,j+1]*Delta[i,j+1])-0.8*(Beta[i,j]/Alpha[i,j]-Beta[i,j+1]/Alpha[i,j+1]);
der(Delta[i,j])= 1.4*(Beta[i,j+1]*Delta[i,j+1]/Alpha[i,j+1] - Beta[i,j+2]*Delta[i,j+2]/Alpha[i,j+2]);
end for;
end for;
end prog;



it does not work even with that same error!

Re: Writing Equations in nested for loop

Hiii

Can u also help me with another doubt!

I wanted to define 3D array which contains block matrices of 3 by 3!!

thanks a lot

Re: Writing Equations in nested for loop

Move the equation keyword up 2 lines. for-equations are equations and need to be in an equation sections.

Re: Writing Equations in nested for loop

3x3x3 arrays are declared as such:

Code:

Real x[3,3,3];

Re: Writing Equations in nested for loop

hii

thanks a lot ! moving the equation above helped!

3D array is not general one!
i want to use this 3D array in a for loop!! where i can change the first dimension to 10 or 20 but the elements will be matrices of 3by 3!
important thing is the diagonal elements are 3 by 3 matrix other elements are all zero

i hope u understand my question

Thanks again!

Re: Writing Equations in nested for loop

See, I don't understand at all what you want the matrix to look like current/smile

{diagonal(1:3) for i in 1:10} maybe?

Re: Writing Equations in nested for loop

Hiii

Hmmm

the matrix (n by n) is a 2D matrix with its elements as numbers (eg  A[3,3]={{1,2,3},{4,5,6},{7,8,9}} )
now say 10 of these A[3,3] have been arranged diagonally   (all other elements zero) to give a matrix  !! this cud be called a( 3D array)!!
I hope im clear now !!

thanks a lot

Re: Writing Equations in nested for loop

No, it cannot be called a 3D array. 4D maybe... And I still don't know how you would put a 3x3 matrix diagonally into something else... I would suggest you put a table of:

Code:

B[0,0,0,0] = ...

B[0,0,0,1] = ...

To see what you really mean.

Do you mean something like:
B[0,0,:,:] = A1
B[1,1,:,:] = A2
B[2,2,:,:] = A3

Where A1..AN are 3x3 matrices?

Re: Writing Equations in nested for loop

Hiii

Yup i understand now that its not a 3D array but a 4D array!! it will be a 10 by 10 matrix with 3 by 3 as its elements arranged diagonally and other elements zero!!


model fourD
  Real A[3,3]={{1,0,0},{0,10,0},{0,0,1}}; //This is the matrix which will be the elements of the 4D array//
input  Real B[:,:,:,:];   //4D arrray//
equation
for i in 1:10 loop
for j in 1:10 loop
for k in 1:10 loop
for l in 1:10 loop

if j=k
Real B[i,j,k,l]=A[3,3]  // this line makes sure that only daigonal elements of 4D array are assigned by A//
else
Real B[i,j,k,l]=0;     // All other elements are zero//
end for;
end for;
end for;
end for;
end fourD;


Can u please take a look at this code!

error is
Syntax    12:59:12        10:5-10:5    Parser error: Unexpected token near: = (EQUALS)

thanks a lot

krishna

Re: Writing Equations in nested for loop

You cannot declare new variables inside an equation section (so skip the Real keyword). Also, you are always assigning element A[3,3] to all elements in the array. And the dimensions of B must be known (10 instead of : ). So something more like:

Code:

model fourD 

  Real A[3,3]={{1,0,0},{0,10,0},{0,0,1}}; //This is the matrix which will be the elements of the 4D array//
  Real B[10,10,3,3];   //4D arrray
equation
  for i in 1:10 loop
    for j in 1:10 loop
      if i==j then
        B[i,j] = A;
      else
        B[i,j] = zeros(3,3);
      end if;
    end for;
  end for;
end fourD;

Re: Writing Equations in nested for loop

Hiii

Thank you! This is extremely helpfull !!

thanks

krishna

Re: Writing Equations in nested for loop

Hiii

This question is not on the coding side! building flow model using Modelica.Fluid library(package).
I want to model air flow in egr! i.e exhaust gases will enter intake manifold through valve!
I Read about the options and components availble in the modelica fluid library.

I did not understand much!

Thanks
krishna

Re: Writing Equations in nested for loop

Hiii

Can you please guide me,
I want to build a model which will be simulating the flow of air from egr  to intake manifold using the Modelica.Fluid library!
I tried a lot ! I built a model with pipes n sharp edged orifices(for venturi) but its not correct !
I Dont know what components to use? Do i need a source? do i need to use dynamics pipe instead of striaght pipe?
How to model venturi? are there specific options in omedit under fluids?

Kindly help

Thanks a lot
krishna

Re: Writing Equations in nested for loop

Hiiii

I have a problem!
This code is to solve differential equation ! wen i try to run i get error abt flattening !
I have no clue ! wat to change!

model try1  "Solve the differential equation involving the matrices"
  extends Modelica.Math;
  extends Modelica.SIunits;
  parameter SI.Pressure Intake_Pressure = 100000 "The pressure of air at the intake";
  parameter SI.Temperature Intake_Temperature = 298 "The temperature of air entering";
  parameter SI.Temperature Wall_Temperature = 310 "The temperature at the pipe wall";
  parameter SI.Denstiy Intake_Density = 1.186 "Density of the air entering";
  parameter SI.Velocity Intake_Velocity = 346 "The local velocity of air entering";
  parameter SI.Length Intake_Dia = 0.041 "The diameter of the intake pipe";
  parameter SI.Length Intake_length = 1 "The length of the intake pipe";
  parameter SI.Density Exit_Density = 1.2 "The density of air exiting ,could be into cylinder or atm";
  parameter SI.Temperature Exit_Temperature = 340 "The exit temperature of the gas";
  parameter Real no_of_vol = 5;
  SI.Area Intake_area_pipe;
  SI.MassFlowRate Intake_mfr;
  SI.MassFlowRate Exit_mfr;
  SI.InternalEnergy Stag_IntEnergy;
  SI.Enthalpy Stag_Enthaly;
  Real k_cylinder;
  Real h_convection;
  Real f_pipe;
  Real z;
  Real dl;
  Real A1[3,3] = {{1,0,0},{0,dl,0},{0,0,1}};
  Real A2[3,3] = {{dl,0,0},{0,dl,0},{0,0,dl}};
  Real B[6,6,3,3];
  Real Alpha[6,6];
  Real Beta[6,6];
  Real Delta[6,6];
  Real W[3,1];
  Real Rhs[3,1];
equation
  Intake_area_pipe = pi / 4 * Intake_dia ^ 2;
  Intake_mfr = Intake_Density * Intake_area_pipe * Intkae_Velocity;
  for i in 1:6 loop
  for k in 1:6 loop
  for j in 0.5:5.5 loop
  dl = Intake_length / no_of_vol;
  W = {Alpha(1, i),Beta(1, j),Delta(1, i)};
  z = i * k;
  if z == 1 and z == 36 then
    B[i,k] = A1;
  elseif i == k then
    B[i,k] = A2;
  else
    B[i,k] = zeros(3, 3);
  end if;
  Alpha(1, 1) = 1.186;
  ALpha(1, 6) = 1.204;
  Beta(0, -0.5) = 20;
  Beta(0, 5.5) = 28;
  Delta(1, 1) = 718 * Alpha(1, 1) * Intake_Temperature + (0.5 * Beta(0.0 - 0.5) ^ 2) / Alpha(1, 1) * Intake_area_pipe;
  Delta(1, 6) = 718 * ALpha(1, 6) * Exit_Temperature + (0.5 * Beta(0, 5.5) ^ 2) / Alpha(1, 6) * Intake_area_pipe;
  Rhs = {Beta(1, j - 1) - Beta(1, j),0.4 * Intake_are_pipe * ({Alpha(1, i) * Delta(1, i)} - {Alpha(1, i + 1) * Delta(1, i + 1)}) - 0.8 * ({Beta(1, j) ^ 2 / Alpha(1, i + 1)} - {Beta(1, j - 1) ^ 2 / Alpha(1, i)}),1.4 * ({Beta(1, j) * Delta(1, i)} - {Beta(1, j - 1) * Delta(1, i - 1)}) - 0.2 / Intake_area_pipe ^ 2 * ({Beta(1, j) ^ 3 / Alpha(1, i) ^ 2} - {Beta(1, j - 1) ^ 3 / Alpha(1, i - 1) ^ 2})};
  B * der(W) = Rhs;

  end for;

  end for;

  end for;
  //The loop for alpha and delta//
  //This im considering because i need two loops running from 1 to n to obtain the Wc diagonal matrix//
  //The loop for Beta//
  //This im using to define the two if condition those are BCL(i,k=1) n BCR(i,k=6)//
  //Implementing if condition fro BCL n BCR//
  //BCL,BCR//
  //The diagonal matrix for intermediate volumes//
  //All other elements are zero//
  //Initial value of the density,these are not exact mostly assumed//
  //Final value of density//
  //Massflowrate of air enterning pipe,exp data//
  //Massflowrate of air into the cylinder//
  //initial values of delta//
  //Final values of delta//
end try1;

kindly hlep
Thanks

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