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 impose a condition on a variable

How to impose a condition on a variable

Greetings,

I would like to solve a set of differential equations with OpenModelica and I would like to impose a condition on one of my variables: it is a water height so it should be always positive physically, however sometimes I get negative heights because of large gradients and I cannot afford to refine the time mesh.

I attempted to add a when directive in the equation part as follows:

Code:

 when h<0 then

reinit(h,0);
end when;

This seems to have no effect. Therefore I attempted the following:

Code:

 when pre(h)<0 then

reinit(h,0);
end when;

This has a weird behaviour as the variable is punctually set to 0 but its overall behaviour is unchanged.
Does anybody know how to impose a h>0 condition in the set of equations?

Thanks in advance

Re: How to impose a condition on a variable

Could you provide a minimal example from your code?

Have you look into the BouncingBall model from modelica? in there, the position of the ball is set to always positive i.e. when h<=0.0 the speed of the ball recalculated (reinit) and the ball "jumps" back in the air. How should your model reacts, if the water level is equal to zero? Does a pump start to fill the container so the level would rise or something else happen?

Re: How to impose a condition on a variable

Thank you for your answer. Yes I have seen the BouncingBall example, that's why I attempted to use the when and reinit directives.
The model is very complex (I did not develop it) as it contains many rooms and connexions between rooms, and the aim is to calculate the water height in each room.
The main equation in the room is :

Code:


rho * S * der(h) = flow

where rho and S are constants, h the calculated height and flow is the sum of incoming and outgoing water flows, that depend on the water heights in the other rooms.
In most cases the model works perfectly well, however sometimes the water drains from the room to other rooms so quickly that negative water heights can be obtained. This is why I try to impose the positivity of variable h.

Re: How to impose a condition on a variable

Its quiet hard without an example. But if i understand correctly, maybe the output flow should be set to zero when h equal to zero since in

Code:

rho * S * der(h) = flow 

the only variables are

Code:

 der(h) 

and

Code:

 flow 

so if you set

Code:

 flow = 0 

the derivative oo h should also be zero.

but again, without example model i could only guess.

Hope it helps you.

Edited by: Arinomo23 - Sep-30-21 07:59:22
There are 0 guests and 0 other users also viewing this topic
You are here: