Question

I seem to struggle with a modeling problem in Modelica (using Dymola), and I hereby seek your help. The idea is this:

I have modeled some components (models) that I want to connect together through their ports. The models are working. The thing is I want to connect several of the same unit (called myUnit in the pseudo code below) in a series. If the number of units were, let's say, only 5 then I could easily dragged them out 1-by-1 and connected them in a standard manner, but let's say I want 100 units. Is there an easy and elegant way to do this? I don't believe for loops to be working outside the equation part of the model, and I want an elegant way of naming the units. I'll try to illustrate this by doing a pseudo code below, explaining what I wish to achieve:

model someModel
    parameter Integer n = 100 "Length of series";
    parameter Real optionValue = 123;
    myLibrary.myStartUnit startUnit(someOption = optionValue); // First unit
    for i in 2:n-1 loop
        myLibrary.myUnit unit_'num2str(i)'(someOption = optionValue); // i'th unit
    end for;
    myLibrary.myEndUnit endUnit(someOption = optionValue);     // Last unit
equation
    connect(startUnit.outPort,unit_1.inPort);
    for i in 2:n-1 loop
        connect(unit_'num2str(i)'.outPort,unit_'num2str(i+1)'.inPort)
    end for;
    connect(unit_'num2str(n-1)'.outPort,endUnit.inport);
end someModel;

I hope I've managed to explain my problem properly, now.

Any tips for solving this problem? I greatly appreciate both strategic advice as to solve the problem as well as purely syntactic advice. In advance, thanks for all help. :)

Was it helpful?

Solution

You can use arrays to achieve this. For example, using models of the Modelica standard library:

model ArrayDemo

  parameter Integer n = 10 "Number of springs/masses";

  Modelica.Mechanics.Translational.Sources.ConstantForce constantForce(f_constant=100);
  Modelica.Mechanics.Translational.Components.Spring springs[n];
  Modelica.Mechanics.Translational.Components.Mass masses[n];
  Modelica.Mechanics.Translational.Components.Fixed fixed;

equation 
  connect(constantForce.flange, springs[1].flange_a);
  for i in 1 : n - 1 loop
    connect(springs[i].flange_b, masses[i].flange_a);
    connect(masses[i].flange_b, springs[i + 1].flange_a);
  end for;
  connect(springs[n].flange_b, masses[n].flange_a);
  connect(masses[n].flange_b, fixed.flange);

 end ArrayDemo;

The idea is to declare the components using arrays and then connect them using for loops.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top