How to sum up a column of an input file given to a CombiTimeTable in Modelica

126 Views Asked by At

I have used a .txt file (T_in1) as an input in a 2D-CombiTimeTable in Modelica. It contains an array with the size of (4,2); the first column is time and the second one is the time-dependent variable. I would like to sum or (to get an average of) the second column in every timestep. I would appreciate if there would be any help with this regard. The code is as follows:

model integration
    import Modelica.Fluid.Types;


  Modelica.Blocks.Sources.CombiTimeTable T_in1(
    extrapolation=Modelica.Blocks.Types.Extrapolation.LastTwoPoints,
    fileName="C:/Users/Tin1.txt",
    smoothness=Modelica.Blocks.Types.Smoothness.LinearSegments,
    tableName="tab1",
    tableOnFile=true,
    timeEvents=Modelica.Blocks.Types.TimeEvents.Always,
    timeScale(displayUnit="min") = 60)                                                                                                                                                                                   annotation (
    Placement(visible = true, transformation(origin={-61,32.2828},     extent = {{-6, -6}, {6, 6}}, rotation = 0)));

equation 
 for i in 1:3 loop
  ...
  ...   
 end for;
  annotation (Icon(coordinateSystem(preserveAspectRatio=false)), Diagram(
        coordinateSystem(preserveAspectRatio=false)),
    experiment(StopTime=240, __Dymola_Algorithm="Dassl"));
end integration;
1

There are 1 best solutions below

0
On

Thanks to built-in blocks of Modelica, there is no need to use for loop like other environments. I found combination of two blocks, called sampler (Modelica.Blocks.Discrete.Sampler) and integrator (Modelica.Blocks.Continuous.Integrator) useful as a solution. The first named bock needs to be connected to the CombiTimeTable and the integrator connected to the sampler. In this way, since I am going to use CombiTimeTable applying a linear fit between each point, this solution is to sample each time period, and then to integrate that value. Using integrator solely, would not provide results close to the expected ones due to its configuration and model definition. The following is the solution to sum up the values:

model integration
    import Modelica.Fluid.Types;


  Modelica.Blocks.Sources.CombiTimeTable T_in1(
    extrapolation=Modelica.Blocks.Types.Extrapolation.LastTwoPoints,
    fileName="C:/Users/Tin1.txt",
    smoothness=Modelica.Blocks.Types.Smoothness.LinearSegments,
    tableName="tab1",
    tableOnFile=true,
    timeEvents=Modelica.Blocks.Types.TimeEvents.Always,
    timeScale(displayUnit="min") = 60)                                                                                                                                                                                   annotation (
    Placement(visible = true, transformation(origin={-61,32.2828},     extent = {{-6, -6}, {6, 6}}, rotation = 0)));

  Modelica.Blocks.Discrete.Sampler sampler1(samplePeriod(displayUnit="min") = 60)
                                                              annotation(Placement(visible = true, transformation(origin={4,-64},      extent = {{-10, -10}, {10, 10}}, rotation = 0)));
  Modelica.Blocks.Continuous.Integrator integrator2 annotation(Placement(visible = true, transformation(origin={52,-66},    extent = {{-10, -10}, {10, 10}}, rotation = 0)));
equation 
 for i in 1:4 loop

 end for;
  connect(sampler1.y,integrator2. u) annotation(Line(points={{15,-64},{28,-64},{
          28,-66},{40,-66},{40,-66}},                                                                                color = {0, 0, 127}));
  connect(T_in1.y[1], sampler1.u) annotation (Line(points={{-54.4,32.2828},{-54.4,
          -64},{-8,-64}}, color={0,0,127}));
  annotation (Icon(coordinateSystem(preserveAspectRatio=false)), Diagram(
        coordinateSystem(preserveAspectRatio=false)),
    experiment(StopTime=40500, __Dymola_Algorithm="Dassl"));
end integration;

***However, the question that still remains is that how to make an average of values at each timestep?