Opened 9 years ago

Last modified 7 years ago

#3383 assigned defect

Issues with Modelica_Noise Beta 1 Backend

Reported by: anonymous Owned by: Per Östlund
Priority: high Milestone: Future
Component: Backend Version: trunk
Keywords: Cc: martin.otter@…

Description

Please get https://github.com/DLR-SR/Noise/tree/MSL (https://github.com/DLR-SR/Noise/archive/MSL.zip) and load package Modelica Modelica_Noise Beta 1.

With OpenModelica 1.9.3NightlyBuild from today, the following problems occur:

The following models give an error when translating the generated C-Code:

simulate(Modelica_Noise.Blocks.Examples.NoiseExamples.GenericNoise)
simulate(Modelica_Noise.Blocks.Examples.NoiseExamples.AutomaticSeed)
simulate(Modelica_Noise.Blocks.Examples.NoiseExamples.Distributions)
simulate(Modelica_Noise.Blocks.Examples.NoiseExamples.UniformNoiseProperties)
simulate(Modelica_Noise.Blocks.Examples.NoiseExamples.ImpureGenerator)
simulate(Modelica_Noise.Blocks.Examples.NoiseExamples.ActuatorWithNoise)

All errors seem to be the same source due to Modelica_Noise.Blocks.Noise.GlobalSeed

  function random = Modelica_Noise.Math.Random.Utilities.impureRandom(final id=id_impure) 
    protected 
  parameter Integer id_impure = ...;

the "id_impure variable is claimed to be not defined when defining function random

Attachments (3)

Modelica_Noise_1.0_Beta.1_simplified.zip (493.2 KB ) - added by martin.otter@… 9 years ago.
TestNoise.mo (32.2 KB ) - added by martin.otter@… 9 years ago.
Modelica_Noise 1.0 Beta.1_more_simplified.zip (493.1 KB ) - added by martin.otter@… 9 years ago.

Download all attachments as: .zip

Change History (18)

comment:1 by Martin Sjölund, 9 years ago

Milestone: 1.9.31.9.4

Moved to new milestone 1.9.4

by martin.otter@…, 9 years ago

by martin.otter@…, 9 years ago

Attachment: TestNoise.mo added

comment:2 by martin.otter@…, 9 years ago

I am trying to run Modelica_Noise 1.0 Beta 1 (git@…:DLR-SR/Noise.git) in OpenModelica and downloaded the nightly build of OpenModelica yesterday.
Simulating Modelica_Noise.Blocks.Examples.NoiseExamples.UniformNoiseProperties gives translation errors

I have made a local branch on my computer and removed the replaceable distribution function in Modelica_Noise.Blocks.Noise.GenericNoise (this means there are no longer replaceable elements used in GenericNoise). Find attached this Modelica_Noise library (Modelica_Noise_1.0_Beta.1_simplified.zip).
Simulating Modelica_Noise.Blocks.Examples.NoiseExamples.UniformNoiseProperties gives translation errors

I have made a test library (TestNoise) that only contains the essential models, "GlobalSeed" and "GenericNoise", together with an example model "TestNoise.ExampleModel". This library is attached. The only change that I did is to replace the external C-functions by Modelica functions (having a dummy implementation).
Simulating TestNoise.ExampleModel is successful.

So, this means that the source of the error is "somehow" related to the external C-code. I would like to include the Modelica_Noise library this week into MSL trunk. Please, can you fix this bug in OpenModelica soon.

comment:3 by Martin Sjölund, 9 years ago

I guess this is due to id_impure actually being globalSeed.id_impure. But after flattening, this is not true. The thing is, the function is defined to have a binding referencing a parameter outside its definition (which is not something you can do directly in a Modelica function; only functions can be looked up the other way). Functions in OpenModelica thus assume that the binding is referencing a local variable. I still maintain the position that having functions refer to parameters is wrong.

The error has nothing at all to do with the external C-code (although the zip-file uses include-annotations for ModelicaRandom.c, which could use Library annotations for ModelicaExternalC instead).

comment:4 by Martin Sjölund, 9 years ago

Owner: set to Per Östlund
Status: newassigned

in reply to:  3 ; comment:5 by martin.otter@…, 9 years ago

Replying to sjoelund.se:

I guess this is due to id_impure actually being globalSeed.id_impure. But after flattening, this is not true. The thing is, the function is defined to have a binding referencing a parameter outside its definition (which is not something you can do directly in a Modelica function; only functions can be looked up the other way). Functions in OpenModelica thus assume that the binding is referencing a local variable. I still maintain the position that having functions refer to parameters is wrong.

The construct is according to section 4.5.1 Short Class Definition (Modelica 3.2 specification) that defines:

class IDENT1 = IDENT2 class_modification;

there is no restriction in the specification that states that "class" cannot be a function.

The error has nothing at all to do with the external C-code (although the zip-file uses include-annotations for ModelicaRandom.c, which could use Library annotations for ModelicaExternalC instead).

Hm. But why is TestNoise.ExampleModel correctly simulating in OpenModelica? This model uses Modelica functions with the above "short class definition" and providing the first argument as a parameter. If you would be right, then the OpenModelica compiler should report an error here, but it accepts the construct and simulates correctly. Since the only change is to replace a Modelica function with an external C function, this issue must be related to the external C function.

comment:6 by martin.otter@…, 9 years ago

Note, the basic issue is that the impure random generator needs to be initialized, which is done with:

initializeImpureRandom(seed)

and then then random function can be used in a model,

impureRandom()

and it must be guaranteed that initializeImpureRandom is called before any call to impureRandom. The construction with "id_impure" is only used to force such a calling sequence. If someone knows an alternative to force this sequence, then id_impure can be removed.

by martin.otter@…, 9 years ago

comment:7 by martin.otter@…, 9 years ago

I have just attached a new file (Modelica_Noise 1.0 Beta.1_more_simplified.zip) which is further simplified:

  • The functions in GlobalSeed are removed. Instead only the "id_impure" is declared and passed via inner/outer to the models where the impure functions are called with this id.
  • The "#include" directive is removed and replaced by library "ModelicaExternalC"
  • The newest OpenModelica (from 13:33 today) is used.

Translation of Modelica_Noise.Blocks.Examples.NoiseExamples.UniformNoiseProperties still gives an error(!) in the backend. Since the functions are removed, no replaceable stuff whatsoever is present, there must be another reason.

Since the C-compiler reports an issue with id_impure, it seems that no correct C-code is generated if parameter id_impure is passed from globalSeed to the model.

in reply to:  5 ; comment:8 by Martin Sjölund, 9 years ago

Replying to martin.otter@…:

The construct is according to section 4.5.1 Short Class Definition (Modelica 3.2 specification) that defines:

class IDENT1 = IDENT2 class_modification;

there is no restriction in the specification that states that "class" cannot be a function.

That's not what I am arguing against. Try defining a function like this (which is what you are trying to do through the modifier):

model M
  parameter Real p = 1.5;
  function f
    input Real r = p;
    output Real o = r;
  end f;
  Real x = f();
end M;

rather than the following (which only sometimes works in OpenModelica):

model M
  parameter Real p = 1.5;
  function f1
    input Real r;
    output Real o = r;
  end f1;
  function f = f1(r=p);
  Real x = f();
end M;

comment:9 by anonymous, 9 years ago

Why not use external objects for one-time initialization/termination of the RNG?

in reply to:  8 comment:10 by martin.otter@…, 9 years ago

Replying to sjoelund.se:

That's not what I am arguing against. Try defining a function like this (which is what you are trying to do through the modifier): ...

Maybe, we need not discuss this now: I have provided a library without such functions (see comment/attachment above; the functions are no longer in GlobalSeed but are directly called in the model and the id_impure is explicitly passed at the place where the function is called). Still the backend of OpenModelica gives an error. I hope you are able to figure out the reason for this behavior.

comment:11 by Martin Sjölund, 9 years ago

Milestone: 1.9.41.9.5

Milestone pushed to 1.9.5

comment:12 by Martin Sjölund, 9 years ago

Milestone: 1.9.51.10.0

Milestone renamed

comment:13 by Martin Sjölund, 8 years ago

Milestone: 1.10.01.11.0

Ticket retargeted after milestone closed

comment:14 by Martin Sjölund, 8 years ago

Milestone: 1.11.01.12.0

Milestone moved to 1.12.0 due to 1.11.0 already being released.

comment:15 by Francesco Casella, 7 years ago

Milestone: 1.12.0Future

The milestone of this ticket has been reassigned to "Future".

If you think the issue is still valid and relevant for you, please select milestone 1.13.0 for back-end, code generation and run-time issues, or 2.0.0 for front-end issues.

If you are aware that the problem is no longer present, please select the milestone corresponding to the version of OMC you used to check that, and set the status to "worksforme".

In both cases, a short informative comment would be welcome.

Note: See TracTickets for help on using tickets.