ModelicaStandardLibrary icon indicating copy to clipboard operation
ModelicaStandardLibrary copied to clipboard

Comparabilty of chaotic behaviour

Open AHaumer opened this issue 7 months ago • 19 comments

During regression testing we had a short discussion about comparabilty of chaotic behaviour - if I remember correctly, Modelica.Electrical.Analog.Examples.ChuaCircuit. Here we decided to shorten the interval for comparison to exclude chaotic behaviour. In my opinion it is possible in many cases to name parameterization for periodic behaviour which can be compared very well and different parameterization to demonstrate chaotic behaviour. I have implemented a small library ChaoticCircuits to demonstrate Chuas ciruit and three other examples from literature. They run fine in OM and Dymola. I provided comparisonSignals as well as referenceResults in subdirectory Resources/Reference. @maltelenz could you compare using WSM? @casella what's your opinion after having a look at the library? I would suggest to include these models into Modelica.Electrical.Analog.Examples.

AHaumer avatar Apr 21 '25 13:04 AHaumer

@AHaumer WSM gets different results for the models in ChaoticCircuits.ColpittsCircuit. Here is a plot of iL at the beginning of AnalyticEquations:

Image

Or at the end:

Image

Similar differences are present in the other variables, and in the IdealCircuit and ImprovedCircuit.

For ChaoticDiode, ChuasCircuit and ChaoticOscillator, the results from WSM match the reference results.

maltelenz avatar Apr 22 '25 09:04 maltelenz

Thanks a lot @maltelenz perfect! this shows that {ChaoticDiode, ChuasCircuit, ChaoticOscillator} can be parameterized such way to achieve periodic behaviour, even if you might have to continue the simulation as shown for ChaoticOscillator.AnalyticEquations:

Image

Image

I plotted y w.r.t. x. Changing the parameterization, we reach chaos. This means that small differences in initial conditions lead to dramatically different results. An of course, only slightly different implementation of the solver also has the same consequences. Therefore regression testing and comparison between tools is only possible for periodic behaviour - or very short duration of simulation.

I believe I have to investigate Colpitt's circuit a bit more to find really periodic behaviour - with the current parameterization chaos is not very visible but present (note the thickened line):

Image

Image

BTW for these diagrams I used different initial conditions; v1.start=5.65 V, v2.start=0.72V, iL.start=0 A.

It seems that the "periodic" results of all examples tumble within a narrow band, but ColpittsCircuit seems to be more sensitive.

AHaumer avatar Apr 22 '25 12:04 AHaumer

See also discussion in #4577

AHaumer avatar Apr 24 '25 11:04 AHaumer

I would prefer to keep the models chaotic (if they are intended for that), and adapt the comparisons to only run them a short while.

Obviously one can imagine more advanced solutions - but to me:

  • The examples are intended as examples, thus an example of a chaotic system should show chaotic behavior.
  • The comparisons are intended to ensure that models don't misbehave too much; we cannot expect bit-equality in simulation results; so only comparing the first part will be good enough to detect that.

Obviously we can do more, but trying to find more stable solution will be a fine balancing act (and it's not clear that periodic solutions are inherently more stable than chaotic ones for these models (*) ) and I think the effort should be spent elsewhere. Comparing the stable attractors themselves is also possible, but that would require a completely different comparison tool - and I don't see the benefit for MSL-development in setting up that.

*: The issue is the sensitivity of the trajectory (for one period in a periodic system). Generally the sensitivity varies during the trajectory, so it may be that the periodic solutions are slightly less sensitive than chaotic ones (as the chaotic ones often pass through some very unstable parts), but I doubt that the periodic solutions are actually stable..

HansOlsson avatar Apr 25 '25 08:04 HansOlsson

@HansOlsson there are oscillators designed for stable periodic behaviour. However, some scientists investigate them how to obtain chaotic behaviour to exploit chaos in various sciences, e.g. cryptograhpy.

AHaumer avatar Apr 25 '25 11:04 AHaumer

@HansOlsson there are oscillators designed for stable periodic behaviour. However, some scientists investigate them how to obtain chaotic behaviour to exploit chaos in various sciences, e.g. cryptograhpy.

I can understand that it varies, e.g., the Chua circuit is seen as an example of chaotic behavior, whereas the Colpitt oscillator was designed to generate a certain frequency. To me the settings in MSL should match that: so that if you run the Chua circuit you should get chaotic behavior whereas the Colpitt oscillator should generate an oscillation with a specific frequency.

Looking more at the graph of the Colpitt oscillator above my first guess is that it isn't chaos, but a superposition of a periodic solution and a very slow transient (explaining why you get Moiré pattern for large stretches; something that would seem odd for chaos).

HansOlsson avatar Apr 25 '25 11:04 HansOlsson

I adapted a little bit the original Modelica.Electrical.Analog.Examples.ChuaCircuit, comparing the results with Dymola2025x and OpenModelica v1.25.0: For periodic behaviour the maximum relative deviation is about 0,5%. Is this good enough for comparability? Changing one parameter (R=1500 Ohm instead of R=1900 Ohm) reveals chaotic behaviour. Yes the results run apart after short time. We could even reach a steady-state solution with constant states using R=2000 Ohm. Here are some small differences during initial transients but both reach the same terminal states.

I have attached the model and a reference result obtained for period behaviour with Tolerance=1e.7, @maltelenz may I kindly ask you to have a look how close WSM's results are to the reference?

ChuaCircuit.zip

AHaumer avatar Apr 25 '25 12:04 AHaumer

I adapted a little bit the original Modelica.Electrical.Analog.Examples.ChuaCircuit, comparing the results with Dymola2025x and OpenModelica v1.25.0: For periodic behaviour the maximum relative deviation is about 0,5%. Is this good enough for comparability? Changing one parameter (R=1500 Ohm instead of R=1900 Ohm) reveals chaotic behaviour. Yes the results run apart after short time. We could even reach a steady-state solution with constant states using R=2000 Ohm. Here are some small differences during initial transients but both reach the same terminal states.

Ah, so different parameters - not just different starting point.

As indicated I don't think that makes sense as example, as we would have a big explanation saying "this is normally seen as a chaotic circuit; but it is not chaotic for the given parameters, to get chaos you have to set these parameters...". That would be a step backwards - an example should demonstrate things as default, not requiring modifications.

Basically: I view examples as examples that as default should demonstrate something when you just simulate it, not primarily as testing models.

HansOlsson avatar Apr 25 '25 12:04 HansOlsson

BTW: After simulating Colpitt oscillator I feel confident to say that it is not chaotic for those parameters, it's just a transient (and depending on tolerance, numerical noise) that keeps if from a perfect periodic solution.

HansOlsson avatar Apr 25 '25 12:04 HansOlsson

For me a compromise looks fine: Default parameters for periodic behaviour, special parameters for chaos. Anyhow it's interesting to study the travel into chaos. Regarding Colpitt: M.P. Kennedy reported 1994 Chaos in the Colpitts oscillator Buscarino describes it in his Concise Guide to Chaotic Electronic Circuits (Chapter 1.3) Maybe I have to work on parameterization ...

AHaumer avatar Apr 25 '25 13:04 AHaumer

I compared the analytic equations of Chua's Circuit and Colpitt's Oscillator, plotting the phase diagram v2 vs. v1: Chua with R=1900 Ohm (periodic) and R=1500 Ohm (chaotic): Image Image Looking a bit closer: Chuas goes between 1900 and 1800 Ohm with bifurcation from periodic to chaotic. Colpitt with RE={50, 100, 200, 300, 500, 2000, 5000} Ohm: Image Image Image Image Image Image Image Looks like bifurcation with a chaotic phase between 100 and 300.

AHaumer avatar Apr 26 '25 10:04 AHaumer

I adapted a little bit the original Modelica.Electrical.Analog.Examples.ChuaCircuit, comparing the results with Dymola2025x and OpenModelica v1.25.0: For periodic behaviour the maximum relative deviation is about 0,5%. Is this good enough for comparability? Changing one parameter (R=1500 Ohm instead of R=1900 Ohm) reveals chaotic behaviour. Yes the results run apart after short time. We could even reach a steady-state solution with constant states using R=2000 Ohm. Here are some small differences during initial transients but both reach the same terminal states.

I have attached the model and a reference result obtained for period behaviour with Tolerance=1e.7, @maltelenz may I kindly ask you to have a look how close WSM's results are to the reference?

ChuaCircuit.zip

WSM results are extremely close to the reference. I didn't check exhaustively, but some manual sampling towards the end were within 0.3%.

maltelenz avatar Apr 28 '25 09:04 maltelenz

Thanks a lot @maltelenz I appreciate your support! This proves: At least this model can be compared with parameterization for periodic results, and bring chaotic results with changing one parameter. On the path from periodic to chaotic behaviour one could even see different modes (bifurcation) - this parameter variation has to be done by the user.

AHaumer avatar Apr 28 '25 13:04 AHaumer

I must say I agree with @HansOlsson; if a system is famous because of its ability to exhibit chaotic behavior, then our testing setup needs to support having default parameters exhibiting the chaotic behavior. As suggested in #3473 and many times before, the solution to this problem should be to have something like annotation(TestCase(StopTime = 0.5)) for setting a duration short enough to make it possible to compare trajectories over time in the usual manner.

Thus, I suggest we close this issue and move the discussion to https://github.com/modelica/ModelicaSpecification/issues/3473.

henrikt-ma avatar Apr 28 '25 22:04 henrikt-ma

I have investigated 10 chaotic circuits up to now: https://github.com/AHaumer/ChaoticCircuits including Lotka-Volterra, van der Pol, Lorenz, Rössler, Chua, chaotic diode (Pham2016), chaotic oscillator (Tamasevicius2005), Colpitt, Shinriki, Jerk system. Some of them were designed to deliver chaotic trajectories. some other as harmonic osciallators that can for certain parameter combinations reveal chaotic behaviour and some other are just equations for pyhsical effects that can be translated to electronic circuits and that can again for certain parameter combinations reveal chaotic behaviour. All of them are parameterized to deliver periodic results after an initial transient, and they seem to be comparable between simulators. Changing a parameter gradually, you can watch the path to chaos. I'm pretty sure some of them won't offer a comparable initial transient when parameterized for chaotic results.

AHaumer avatar May 12 '25 18:05 AHaumer

I'm pretty sure some of them won't offer a comparable initial transient when parameterized for chaotic results.

At least to me, this always seemed like a matter of choosing a suitable duration, so it would be very valuable if you could (or concluded that you couldn't) support this with actual examples.

Edit: That said, I am aware of a class of problems where there is no good choice of duration for looking at the initial transient, namely systems where the interesting part (projection) of the model starts at an unstable equilibrium, while the rest of the model is mainly contributing with numeric noise to push the interesting part away from the equilibrium. For such a model, it is easy to compare the initial transient of the non-interesting part of the model, but it is not possible to predict how quickly the interesting dynamics will leave the unstable initial conditions. The intermediate axis theorem is a very simple example of this, with the "interesting" part of the dynamics chosen to be the angle between the intermediate axis and the axis of rotation.

henrikt-ma avatar May 12 '25 21:05 henrikt-ma

@henrikt-ma I think you are right, we always can choose a very short time span for comparison. But for me the question is what is a better comparison: Either set one parameter to "periodic" and compare a periodic result with initial transient for a longer period of time or set one parameter to "chaotic" and compare the initial transient of the chaotic result for a short period of time before chaos starts. The following results are the Shinriki oscillator from my "zoo" at https://github.com/AHaumer/ChaoticCircuits.

Image

Image

AHaumer avatar May 13 '25 21:05 AHaumer

The Shinriki oscillator plots look nice, but I believe that what I see is matching the expectations expressed above; a long duration is needed cover all interesting parts, while less than 10% of that duration looks like a suitable duration for regression testing or correctness checking.

For models that challenge tool strategies for index reduction and state selection, I can also see the point of longer duration in order pass through more states where bad selection of states might result in singular equations, but if the dynamics are chaotic I would like a test without comparison of results, only a check that simulation completes without issues. Thus, if a model had something like TestCase(StopTime = 0.005), it would seem like a good testing strategy to perform simulation all the way to the standard stop time in order to verify that simulation completes without issues, but then to only perform comparison of simulation results up to the TestCase.StopTime.

henrikt-ma avatar May 14 '25 06:05 henrikt-ma

Testing should be a balance between finding likely errors and time spent (in setting up and running). To me the likely errors to find is just that some equations are incorrect (in model or due to tool-issues) and that should be detected fairly quickly.

I don't see the major benefit for running these models for a long time for testing - regardless of whether they are chaotic or periodic. Note that even if periodic signals don't change as dramatically as chaotic ones there might still be a time-shift if running too long that complicate comparison; so a few "periods" after settling should be good enough (I use "periods" to sort of handle chaotic signals).

If there are multiple regions in some way (conditions, dynamic state selection, etc) that are realistic to enter we should test all of them, and looking at Tony's Chaotic circuits they normally (except Shinriki) each have an opamp/diode-condition that either switch every oscillation or just a few times at the start (I haven't investigated why the other diodes don't switch in models with multiple ones; I assume it's because they have different functions); so just running the initial startup and then a few oscillations (or similarly for chaotic signals) should be good enough. (There are some without such changes as well.)

For Ideal Shinriki both an opamp- and a diode-condition switch after awhile for the periodic signal, but only one of them for the chaotic one. For improved Shinriki it's instead two opamps for the periodic and nothing for the chaotic one. (That seemed odd, but the explanation is that the chaotic signal has a lower amplitude.)

If we look at a chaotic example with dynamic state selection such as Modelica.Mechanics.MultiBody.Examples.Rotational3DEffects.GyroscopicEffects I think the current stop-time is fine for that as it goes through all dynamic states. I also noticed that it has two identical copies of the system, and in Dymola they start to drift apart after 11 s (indicating that comparisons are no longer valid), whereas the default stop time is 5 s.

Additionally I would say that periodic signals should (as default) have a stop-time matching that; looking at ChaoticCircuits.ShinrikiOscillator.IdealCircuit it's for me easier to see the result with a stop-time of 40 ms than with the default 100 ms, and for ChaoticCircuits.ColpittsCircuit.IdealCircuit 1 ms seems better than 10 ms (there are some transients up to about 0.5 ms as well; so a shorter interval wouldn't work).

But chaotic experiments might as experiment have a longer simulation time, so you can see the chaos.

HansOlsson avatar May 14 '25 08:05 HansOlsson