Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a block (based on time events) to calculate the extrema of a signal #3762

Merged
merged 11 commits into from
Mar 15, 2021

Conversation

dietmarw
Copy link
Member

@dietmarw dietmarw commented Feb 25, 2021

Based on an old discussion MO#109 and now triggered again via @tinrabuzin.
The implementation is based on Modelica.Blocks.Math.Mean

I specifically put this into draft mode and like to discuss this first.

Some notes:

  • I'm aware that there are some performance drawbacks, but these are the same as present in other block like Mean
  • I'm not 100% happy with the name yet. Ideally I would have called it MinMax but that one is taken although one could argue that that block really should be placed under Blocks.Routing

Based on an old discussion ([Modelica Trac modelica#109](https://trac.modelica.org/Modelica/ticket/109)) and now triggered again via @tinrabuzin.
The implementation is based on `Modelica.Blocks.Math.Mean`
@dietmarw dietmarw self-assigned this Feb 25, 2021
@dietmarw dietmarw added enhancement New feature or enhancement L: Blocks Issue addresses Modelica.Blocks labels Feb 25, 2021
@dietmarw dietmarw added this to the MSL4.1.0 milestone Feb 25, 2021
Copy link
Contributor

@HansOlsson HansOlsson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Some comments:
I don't understand why there is both y_min_last and y_min (etc) to me they could be the same variable.
And I would add two warnings as comments:

  • The extrema will be approximate as it only return the extremas at the sampling points.
  • The sampling will generate many events which may slow down the simulation.

@dietmarw
Copy link
Member Author

dietmarw commented Mar 1, 2021

I've added some more documentation. and removed the intermediate discrete variables. Basically I was orienting myself on the implementation of the Mean block which probably also could get compacted in the same way.

@dietmarw
Copy link
Member Author

dietmarw commented Mar 2, 2021

I guess since it has already gone through one review it can probably get elevated from the draft status. Still open to suggestion regarding the name.

@dietmarw dietmarw marked this pull request as ready for review March 2, 2021 12:01
@christiankral
Copy link
Contributor

I am very much in favor of the proposed min / max block. However, before adding my review here, I want to make to additional feature requests which are open for discussion:

  1. When the min and max values of the input signal are caught, it sometimes makes sense to know at what (sampling) time the min / max values occurred. So it were nice if the two points in time could be recorded as variables in the block, too. In this case one can at least access this variable by an equation.
  2. If I investigate an oscillation, I am sometimes also be interested, let's say in the second or third min / max value. In order to determine these values, different approaches are possible. Let me point out two options that came to my mind:
    1. Provide an option to the block to "reset" the min / max values. An example for such behavior is implemented in the integrator block.
    2. Make the output min / max values a vector and define a parameter n which defines the length of the two vectors. In this case n subsequent min / max values (and time instants, associated with option 1) are available.

This was done inside the when clause so does not add additional crossover events.
@dietmarw
Copy link
Member Author

dietmarw commented Mar 3, 2021

I've added two additional discrete variables t_min and t_max to store the time of when the min and max was reached.
@HansOlsson I did use discrete type for those times though am now really unsure if that is necessary at all and/or gives any benefit over using continuous signals here.

@christiankral As for your other request number 2, I really like to keep this block in the way the other blocks work and also keep it simple, I can understand that the functionality you are looking for might come in handy but is probably quite special to your applications. So I'm reluctant to add this to this more simple block. Also this implementation is rather slow due to the sampling nature. So if you want a more sophisticated solution you might also look at https://github.com/beutlich/LastLib which does not generate ANY time events is hence much faster but depends on using the provided FMU (since the functionality was never accepted in Modelica Spec but is part of FMI spec).

@beutlich beutlich assigned MartinOtter and unassigned dietmarw Mar 6, 2021
@christiankral
Copy link
Contributor

@dietmarw I fully understand your argument. That's OK with me.

@christiankral
Copy link
Contributor

Alternative name proposal SampleMinMax

Copy link
Contributor

@christiankral christiankral left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good

@dietmarw
Copy link
Member Author

dietmarw commented Mar 9, 2021

I don't think the name Sample(d)Something is a route to follow. A name of a component should be about what it does and not how it is achieving that. There are lots of sampled blocks (e.g., `Mean´) but we don't want to start renaming all those.

@AHaumer
Copy link
Contributor

AHaumer commented Mar 11, 2021

I don't think the name Sample(d)Something is a route to follow. A name of a component should be about what it does and not how it is achieving that. There are lots of sampled blocks (e.g., `Mean´) but we don't want to start renaming all those.

To be honest: The basic definitions of RMS and similar are sampled per basic period.
That's not the case for min / max - which have to be sampled at higher frequency.
I'm searching for a min / max detection without sampling - didn't find a solution up to now, maybe never ...
But if, how would you name that? ContinuousMinMax?
I'd prefer SampledMinMax.

@AHaumer
Copy link
Contributor

AHaumer commented Mar 11, 2021

Well I know that's the best solution we have (since we have no "continuous" implementation).
I think you should include a bold warning to set the sampleTime appropriately, and include 2 usages for the same signal: 1 with bad (too large) choice of sampleTime, 1 with sufficiently small sampleTime.
BTW, you forgot to intialize t_min and t_max ...
I've committed my suggestions for an example and the initialization of t_min and t_max.

Copy link
Contributor

@AHaumer AHaumer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine with me!

@AHaumer
Copy link
Contributor

AHaumer commented Mar 13, 2021

What's wrong with the CI/syntax_checks?

@AHaumer
Copy link
Contributor

AHaumer commented Mar 14, 2021

Sorry when adding comparisonSignals.txt, I realized that's not reasonable using random signals.
Therefore I adapted the example, then added comparisonSignals.txt.

Copy link
Contributor

@AHaumer AHaumer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

after adapting the example, it's fine for me.

@HansOlsson
Copy link
Contributor

What's wrong with the CI/syntax_checks?

It seems the file starts with a UTF8-Byte Order Mark and the CI-check doesn't like that.
If you are using Dymola I think you can avoid that by setting Hidden.SupportUnicodeFiles=3; but we will clean up that.

Copy link
Contributor

@HansOlsson HansOlsson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good now.

@dietmarw
Copy link
Member Author

dietmarw commented Mar 15, 2021

Nice example @AHaumer. With this example we can actually remove the test one I had added to ModelicaTest. No point in maintaining two models. So I will remove that one again .

@dietmarw
Copy link
Member Author

Also fine by me now. One of the library officers should merge this then.

Copy link
Contributor

@christiankral christiankral left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.

@christiankral christiankral merged commit 3bcfa2b into modelica:master Mar 15, 2021
@christiankral
Copy link
Contributor

Sorry for merging... I'm not a library officer.

@dietmarw dietmarw deleted the SignalExtrema branch March 26, 2021 17:51
@beutlich
Copy link
Member

I'm searching for a min / max detection without sampling - didn't find a solution up to now, maybe never ...
[...]
Well I know that's the best solution we have (since we have no "continuous" implementation).

@AHaumer Have a look at the FMU-based solution MinimumTest.Minimum block, which provides a continuous implementation. It is legal Modelica but the FMU import is tool-specific.

https://github.com/beutlich/LastLib/blob/543d88ecf891796640727f126250fe69a3c41549/MinimumTest.mo#L4-L21

See also https://tinyurl.com/lastlibfmu for some background information and the necessary tool-specific workarounds. The FMU (called Last.fmu) was recently reimplemented (FMI 1,0 ME) and is successfully used for parameter optimization tasks.

@beutlich beutlich removed the request for review from MartinOtter March 28, 2021 15:52
@beutlich beutlich changed the title Adds a block to calculate the extrema of a signal Add a block (based on time events) to calculate the extrema of a signal May 2, 2021
@christiankral
Copy link
Contributor

christiankral commented Jul 20, 2021

@dietmarw I have two questions:

  1. Is there a particular reason for the parameter names

    • Ts instead of samplePeriod and
    • t0 instead of startTime?
      The parameter names samplePeriod and startTime are consistently used in the package Modelica.Blocks.Discrete for sampling blocks.
  2. As you suggested to use the name MinMax I wonder if you think that it makes sense to move / rename the model Modelica.Blocks.Math.SignalExtrema to Modelica.Blocks.Discrete.MinMax. You might have thought about it before, but just want to make sure to explicitly ask this question.

As we also have a a continuous and a discrete implementation of the models TransferFunction

image

it would also make sense to have Modelica.Blocks.Continuous.MinMax one day in the future...

@dietmarw
Copy link
Member Author

@christiankral Thanks for the feedback. I had not considered putting this block inside Modelica.Blocks.Discrete before and hence was not aware of the nomenclature as used in there. Thinking about it, it might actually be a good solution placing it there as it then also emphasises the "sampled" nature plus I get the choose my preferred name MinMax. In that case I would also rename Ts and t0.
@AHaumer any objections?

As for the future perspective of one day having a non-sampled version of MinMax (as demonstrated by https://github.com/beutlich/LastLib) I think we also have the problem of clearly establishing what goes into Blocks.Continuous and what in Blocks.Math. At the moment it looks rather random. But that would be for a totally different (and low priority) ticket.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or enhancement L: Blocks Issue addresses Modelica.Blocks
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants