Jack
Jack

Reputation: 1232

smooth the curve in Dymola

In Dymola, after plotting the result, how could I smooth the curve? enter image description here

Upvotes: 2

Views: 558

Answers (3)

Rene Just Nielsen
Rene Just Nielsen

Reputation: 3413

I personally find it easier to express the 'amount' of filtering in terms of a time constant than a cutoff frequency. If you don't want to mind writing a couple of lines of code you could write the equation of a first-order filter to achieve something similar to the answer of kabdelhak, i.e.

model Preheater_Model_Validation
  Modelica.SIunits.MassFlowRate m_flow_filtered;
  parameter Modelica.SIunits.Time tau=120 "filter time constant" annotation(Evaluate=false);

  ... (other declarations)
initial equation
  der(m_flow_filtered) = 0 "steady-state initialization";
equation
  tau*der(m_flow_filtered) = hex.summary.m_flow_in - m_flow_filtered;
end Preheater_Model_Validation;

The Evaluate=false annotation in the code means that you can tweak the time constant in the 'Variable browser' in Dymola without re-translating the code.

Best regards

Rene Just Nielsen

Upvotes: 3

marco
marco

Reputation: 6655

The plot windows in Dymola feature some signal operators, which you can use for post processing. But they do not feature a smoothing function.

screenshot with Dymola signal operators

If you want to do it in Dymola, the easiest option is to continuously compute the averaged values during the simulation, like kabdelhak suggested.

An alternative would be to apply signal processing filters in Matlab or in Python on the .mat result file created by Dymola.

Upvotes: 3

kabdelhak
kabdelhak

Reputation: 697

I don't know a way of doing it in the plot window, but smoothing a curve could be done in multiple ways. You have to be aware that you are manipulating the actual simulation result with that.

I would recommend using a filter yourself and just create a smoothed signal without influencing the actual simulation. I made a small sample model with an original and filtered signal using a Butterworth filter from the MSL.

I just copied and modified an example slightly, please disregard most of the inline comments. You have to fiddle a bit with the f_cut such that it cuts the correct high frequencies for your case.

model FilterTest "Demonstrates the Continuous.Filter block with various options"
  extends Modelica.Icons.Example;

  Real original = add.y;
  Real filtered = Butterworth.y;
  protected
  parameter Integer order=3;
  parameter Modelica.SIunits.Frequency f_cut=2;
  parameter Modelica.Blocks.Types.FilterType filterType=Modelica.Blocks.Types.FilterType.LowPass
    "Type of filter (LowPass/HighPass)";
  parameter Modelica.Blocks.Types.Init init=Modelica.Blocks.Types.Init.SteadyState
    "Type of initialization (no init/steady state/initial state/initial output)";
  parameter Boolean normalized=true;
  Modelica.Blocks.Continuous.Filter Butterworth(

    analogFilter = Modelica.Blocks.Types.AnalogFilter.Butterworth,
    f_cut= 100,
    f_min=1,
    filterType=Modelica.Blocks.Types.FilterType.LowPass, gain = 1,
    init=init,normalized=normalized,
    order=order)
    annotation (Placement(visible = true, transformation(extent = {{38, 18}, {58, 38}}, rotation = 0)));
  Modelica.Blocks.Sources.Sine sineHigh(freqHz = 200)  annotation(
    Placement(visible = true, transformation(origin = {-62, 54}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
  Modelica.Blocks.Sources.Sine sineLow(amplitude = 10, freqHz = 3)  annotation(
    Placement(visible = true, transformation(origin = {-56, 2}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
  Modelica.Blocks.Math.Add add annotation(
    Placement(visible = true, transformation(origin = {-8, 28}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
equation
  connect(add.u1, sineHigh.y) annotation(
    Line(points = {{-20, 34}, {-20, 55}, {-51, 55}, {-51, 54}}, color = {0, 0, 127}));
  connect(add.u2, sineLow.y) annotation(
    Line(points = {{-20, 22}, {-33.5, 22}, {-33.5, 2}, {-45, 2}}, color = {0, 0, 127}));
  connect(Butterworth.u, add.y) annotation(
    Line(points = {{36, 28}, {3, 28}}, color = {0, 0, 127}));
  annotation(
    experiment(StopTime = 0.9),
    Documentation(info = "<html>

<p>
This example demonstrates various options of the
<a href=\"modelica://Modelica.Blocks.Continuous.Filter\">Filter</a> block.
A step input starts at 0.1 s with an offset of 0.1, in order to demonstrate
the initialization options. This step input drives 4 filter blocks that
have identical parameters, with the only exception of the used analog filter type
(CriticalDamping, Bessel, Butterworth, Chebyshev of type I). All the main options
can be set via parameters and are then applied to all the 4 filters.
The default setting uses low pass filters of order 3 with a cut-off frequency of
2 Hz resulting in the following outputs:
</p>

<img src=\"modelica://Modelica/Resources/Images/Blocks/Filter1.png\"
   alt=\"Filter1.png\">
</html>"),
    uses(Modelica(version = "3.2.2")));
end FilterTest;

Upvotes: 3

Related Questions