Harish Pavan Kumar
Harish Pavan Kumar

Reputation: 41

How to implement Cycle counting in Modelica?

I have a battery model in Modelica. PNet is the value of power flowing through battery (PNet is positive for charging and negative for discharging). This oscillates based on a load. I want to calculate the number of cycles that the battery is put through and also the depth of discharge comin in from each of these cycles.

Upvotes: 4

Views: 481

Answers (1)

Markus A.
Markus A.

Reputation: 7525

This is a pretty generic question so my answer will be rather generic as well. Also it is not clear to me, what you are referring to as a cycle. Wikipedia mentions deep and shallow discharge and there are some others as well.

Some general note: In Modelica the when statement is useful for counting. You can read through Section 8.3.5 of the Modelica Language Specification to get full information on this.

The below examples computes how often the variable PNet turns positive, which should respond to the number of shallow cycles above. Some description for the model:

  • The model noiseSource computes a random number which is then filtered by a first order (PT1) element to compute PNet. The filter should likely be skipped in the original example, it is only there to smooth the trajectory a bit.
  • The code in the when statement is executed once at the time when the condition turns true, which enables the counting.
  • The pre statement accesses the value of cycles right before the when statement got active, which enables counting how often the condition occurred.
  • The start=0 in cycles(start=0) sets the starting value for the variable cycles, which is necessary as you cannot use cycles = 0 as this would generate an equation for cycles, which is not what you want.
  • The inner model globalSeed is necessary for the noiseSource to work.

Here is the actual code:

model CycleCounter
  inner Modelica.Blocks.Noise.GlobalSeed globalSeed;
  Modelica.Blocks.Noise.NormalNoise noiseSource;

  parameter Modelica.SIunits.Time T = 1e-3 "Time constant of PT1 element to filter random signal to compute PNet";

  Integer cycles(start=0) "Counts the number of ";
  Real PNet "Random value";

equation 
  der(PNet) = (noiseSource.y - PNet)/T;

  when PNet > 0 then
    cycles = pre(cycles)+1;
  end when;

  annotation (uses(Modelica(version="3.2.3")));
end CycleCounter;

And the result from simulating in Dymola:

Result of the code above

Upvotes: 5

Related Questions