Search code examples
functionsummodelica

How to use MultiSum function in Modelica?


I've got a vector with a size of 5 and would like to sum each of its elements at each step in a variable (summ), but I don't know how to use MultiSum function in this case.

model add
import Modelica.Blocks.Math.MultiSum;
Real k[5]={1,2,3,4,5};
Real summ;
equation
for i in 1:4 loop
summ = MultiSum(???)
end for;

end add; 

I would appreciate if you could help.


Solution

  • The solution by MarkusA is good, but if you really wanted to use the block you could do something like this:

    model add
      import Modelica.Blocks.Math.MultiSum;
      Real k[5]={1,2,3,4,5};
      Modelica.Blocks.Sources.RealExpression realExpression[size(k, 1)](y=k);
      Modelica.Blocks.Interfaces.RealOutput summ;
      MultiSum multiSum(nu=size(k, 1));
    equation
      connect(multiSum.y, summ);
      connect(realExpression.y, multiSum.u);
    end add;
    

    or with annotations:

    model add
      import Modelica.Blocks.Math.MultiSum;
      Real k[5]={1,2,3,4,5};
      Modelica.Blocks.Sources.RealExpression realExpression[size(k, 1)](y=k)
        annotation (Placement(transformation(extent={{-110,16},{-90,36}})));
      Modelica.Blocks.Interfaces.RealOutput summ
        annotation (Placement(transformation(extent={{86,4},{106,24}})));
      MultiSum multiSum(nu=size(k, 1))
        annotation (Placement(transformation(extent={{-10,20},{2,32}})));
    equation 
      connect(multiSum.y, summ) annotation (Line(points={{3.02,26},{80,26},{80,14},{
              96,14}}, color={0,0,127}));
      connect(realExpression.y, multiSum.u)
        annotation (Line(points={{-89,26},{-10,26}}, color={0,0,127}));
      annotation (uses(Modelica(version="4.0.0")));
    end add;