Opened 11 years ago

Closed 10 years ago

#2737 closed defect (fixed)

Handle inherited replaceable functions called in functions

Reported by: Martin Sjölund Owned by: Martin Sjölund
Priority: high Milestone: 1.9.1
Component: Frontend Version: trunk
Keywords: Cc: Per Östlund

Description

Inherited replaceable functions called in functions are used in the Noise library:

package Noise
  package RNG
    package SampleBased
      function RNG_MRG
        extends Noise.Utilities.Interfaces.SampleBasedRNG;
        input Integer[:] a = {1071064, 0, 0, 0, 0, 0, 2113664};
        input Integer c = 0;
        input Integer m = 2147483629;
      algorithm
        assert(size(states_in, 1) >= size(a, 1), "State must have at least as many elements as a!");
        states_out := states_in;
        states_out[1] := 0;
        for i in 1:size(a, 1) loop
          states_out[1] := states_out[1] + a[i] * states_in[i];
        end for;
        states_out[1] := integer(mod(states_out[1] + c, m));
        for i in 1:size(a, 1) - 1 loop
          states_out[i + 1] := states_in[i];
        end for;
        rand := abs(states_out[1] / (m - 1));
      end RNG_MRG;

      function RNG_LCG
        extends Noise.Utilities.Interfaces.SampleBasedRNG;
        input Integer a = 69069;
        input Integer c = 1;
        input Integer m = 2147483647;
      algorithm
        (rand, states_out) := RNG_MRG(instance, states_in, a = {a}, c = c, m = m);
      end RNG_LCG;
    end SampleBased;
  end RNG;

  package PDF
    function PDF_Uniform
      extends Noise.Utilities.Interfaces.PDF;
      input Real[2] interval = {0, 1};
    algorithm
      (rand, states_out) := RNG(instance = instance, states_in = states_in);
      rand := rand * (interval[2] - interval[1]) + interval[1];
    end PDF_Uniform;
  end PDF;

  package Utilities
    package Interfaces
      partial function InputOutput
        input Real instance;
        input Integer[:] states_in;
        output Real rand;
        output Integer[size(states_in, 1)] states_out;
      end InputOutput;

      partial function RNG
        extends Interfaces.InputOutput;
      end RNG;

      partial function SampleBasedRNG
        extends RNG;
      end SampleBasedRNG;

      partial function PDF
        extends Interfaces.InputOutput;
        replaceable function RNG = Noise.RNG.SampleBased.RNG_LCG constrainedby Interfaces.RNG;
      end PDF;
    end Interfaces;
  end Utilities;
end Noise;

model M
  Real r = Noise.PDF.PDF_Uniform(1.0,{1,2,3});
end M;

Change History (3)

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

Owner: changed from somebody to Martin Sjölund
Status: newaccepted

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

Cc: Per Östlund added

I changed this to extends Interfaces.RNG; in the Noise library, which makes things work. But I think there is still work needed in OpenModelica to handle this case.

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

Resolution: fixed
Status: acceptedclosed

Working after Adrian's changes.

Note: See TracTickets for help on using tickets.