Archived OpenModelica forums. Posting is disabled.

Alternative forums include GitHub discussions or StackOverflow (make sure to read the Stack Overflow rules; you need to have well-formed questions)


Forgot password? | Forgot username? | Register

How to count no.of cycles in Modelica?

How to count no.of cycles in Modelica?

The variable that is changing is called PNet. I want to count the number of cycles. how do I write this correctly using if or when or a combination of both? I am counting the number of cycles through a RealOutput called "cn". The start value of cn is 0.
I am very new to programming and Modelica in general.
I tried to write this:

when PNet=0 then cn=pre(cn)+1; end when PNet=pre(Pnet);

This was my logic: I want cn to increase by 1 whenever PNet touches 0 but I do not want this happening for every time PNet remains 0.
This is the main error I get: "Error: declaration window line 80 column 12, syntax error at "=" missing "then""
Please suggest what mistake I have made here or any alternative approaches.

Thanks,
Harish

Edited by: jayahoharish - May-20-19 19:09:02

Re: How to count no.of cycles in Modelica?

jayahoharish wrote:

when PNet=0 then cn=pre(cn)+1; end when PNet=pre(Pnet);

You are missing a semicolon the end of  "end when" and the conditon of when should be boolean comparison, i.e PNet == 0. so more or less you code should look like this

Code:


when PNet == 0 and PNet <> pre(PNet) then
  cn = pre(cn)+1;
end when;

hope it helps. Cheers

Edited by: Arinomo23 - May-21-19 07:02:25

Re: How to count no.of cycles in Modelica?

Thank you very much for this.
I was able to save the code without errors but I cannot run the simulation as there are 2 errors:
1. It is not allowed to use pre for a continuous time variable outside a when clause:
pre(PNet)
and
2. The when-condition must be a discrete expression:
when PNet == 0 and PNet <> pre(PNet) then
cn = 1+cn;
end when;

Any ideas on this?

Re: How to count no.of cycles in Modelica?

HI, is it possible to share you model?

Re: How to count no.of cycles in Modelica?

Here is the library:
https://github.com/UdK-VPT/BuildingSystems

I am working on Complex battery model which is located at Building Systems/Technologies/Electrical Storage.

I am trying to count the number of cycles the battery is going through and the depth of discharge of each cycle.

A simulation without these features can be run and this can be found at Building Systems/Technologies/Electrical Storage/Examples.

Re: How to count no.of cycles in Modelica?

Hi, so i copy the example and add a variable Counter

Code:


Integer Conter_1C(start=0,fixed=true);

and following equation

Code:


when battery_1C.PNet == 0 and battery_1C.PNet <> pre(battery_1C.PNet) then
    Counter_1C = pre(Counter_1C)+1;
  end when;

and after the simulation got Conter_1C = 43, which i think the right number of times when PNet equal to 0 before being cahrge. Hope it helps you.

Cheers

There are 0 guests and 0 other users also viewing this topic
You are here: