Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8534605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:14:02+00:00 2026-06-11T10:14:02+00:00

model try discrete Integer x(start = 1); algorithm when time >= 3 then x:=

  • 0
model try
discrete Integer x(start = 1);
algorithm
when time >= 3 then
  x:= x + 5;
end when;
x:= 5;
end try;

model try1
discrete Integer x(start = 1);
algorithm
x:= 5;
when time >= 3 then
  x:= x + 5;
end when;
end try;

Both of the simulation result on OpenModelica is that x is always equal to 5. Why?

It seems that x in model try should be 5. But why x in model try1 is not 10 when time is equal to 3?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T10:14:03+00:00Added an answer on June 11, 2026 at 10:14 am

    I have two comments. First, keep in mind that when dealing with when clauses, it is often prudent to utilize the pre(...) operator to make explicitly clear which value you are referring to, the new one or the previous. I don’t know if it is strictly necessary in this case (and even if I did know, I’m not sure if all developers of Modelica tools use consistent semantics), but it is a good idea if only to make it clear to readers of the code.

    The other issue is with the handling of algorithms. Normally, the statements are executed in the order they appear in the algorithm section. But when clauses are a bit tricky since they have an asynchronous nature to them. Again, I’m not clear what the exact semantics are with regards to the interleaving of when clauses with other assignment statements in the model, but writing an algorithm section like you have is a bit ambiguous. Let’s assume the when statement is evaluated and x is given a new value, how long do you expect it to keep that value? The compiler has latitude in repeatedly evaluating the algorithm section might be evaluated immediately after the when clause is invoked in which case x will be given a new value of 5 (perhaps even without time advancing at all).

    I’m not sure what your intention really is with this model. But if you want it to start with one value and then, after time>3, take on a new value (based on the old value), I see two ways of achieving this in Modelica and both of them involve how you set the initial value. Consider the following model:

      model InitAndEvent
        discrete Integer x(start=1);
      algorithm
        when initial() then
          x := 5;
        end when;
        when time >= 3 then
          x := pre(x) + 5;
        end when;
      end InitAndEvent;
    

    This model uses the pre operator but, more importantly, it also uses the initial() event to provide the initial value for x. In this way, this avoids the problem you were probably experiencing with the previous model of constantly overwriting the value of x to 5. Another way to do this could be:

      model InitAndEvent
        discrete Integer x(start=1);
      initial algorithm
        x := 5;
      algorithm
        when time >= 3 then
          x := pre(x) + 5;
        end when;
      end InitAndEvent;
    

    Although I confess that I suspect different tools might have different semantics for each of these versions. I’d say the when initial() version is probably more universally consistent.

    Update:

    I think your misunderstanding comes from not considering the implications of subsequent evaluations. In the case of try1, at time==3 the when clause is evaluated and x is given a new value.

    But what happens on the next model evaluation? The algorithm section is evaluated again and x is set back to 5. The when clause does not get evaluated again until the condition expression turns false and then true again. So in this case, it triggers exactly once! If you want it to be evaluated for all times greater than 3, then you need to use an if statement.

    And keep this question in mind…how much simulation time elapses between the execution of the when clause and the next model evaluation (when x is reset)? Quite possibly none. You have no way of knowing when the algorithm section is executed (that depends on events, the integrator used, etc.). So if you want x to be given a value once at the start of the simulation and once at time==3, then you need to state that in your model (as I did in my examples).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

model try Real x(start = 1); algorithm when x >= 7 then reinit(x, 5);
I try to validate the presence of attributes that belong to another model, to
Every time try to set the value of any variable in my model object,
I'm getting an error when I try to save a model that has a
i try to build a app that is in landsscape mode all the time.
Helo, I'm building a relatively complex Discrete Event Simulation Model in SimPy. When I
I have a model with a unique integer that needs to increment with regards
I have two models: The model NetworkObject try to describe hosts. I want to
I'm try something from this comment idea Code Igniter Controller/Model name conflicts find class
When i try baking with console .. cake bake model After choosing which table

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.