1
model test
  import Modelica.Constants.pi;
  Real f;
  discrete Real g;
  Clock clk=Clock(0.1);
equation 
  f = sin(pi*time);
  when Clock(0.1) then
    if f >= 0 then
      g = (sin(pi*time)) - 0.1;
    else
      g = (sin(pi*time)) + 0.1;
    end if;
  end when;
end test;

enter image description here

f is assigned as a continuous function. I want to sample the value of g depended on f, but f also be changed to a discrete value. Is there anything wrong ?

marco
  • 5,944
  • 10
  • 22
Jack Hsueh
  • 115
  • 8

1 Answers1

1

The clock partitioning sees f as being used directly inside the when Clock and thus f is also seen as a clocked variable.

Use sample(f) if that is not desired:

model test
  import Modelica.Constants.pi;
  Real f;
  discrete Real g;
  Clock clk=Clock(0.1);
equation 
  f = sin(pi*time);
  when Clock(0.1) then
    if sample(f) >= 0 then
      g = (sin(pi*time)) - 0.1;
    else
      g = (sin(pi*time)) + 0.1;
    end if;
  end when;
end test;

See also: Failure to handle clock inference in Dymola

Hans Olsson
  • 11,123
  • 15
  • 38
  • Thank you! Is it to say, all the variables or expression inside "when-clock" block must be sampled? like: when Clock(.1) then if sample(f >= 0) then g = sample(f - 0.1); else g = sample(f + 0.1); end if; end when; – Jack Hsueh Oct 17 '22 at 01:45
  • 1
    They must either sampled or be on the clock, as g. If you intend to use that many expressions involving f I would introduce sampled_f=sample(f); (possibly with a shorter name) and then use that instead of repeating sample(f). – Hans Olsson Oct 17 '22 at 06:50