Discussion Closed This discussion was created more than 6 months ago and has been closed. To start a new discussion with a link back to this one, click here.
Conditional Statements within an Expression
Posted 17 ago 2009, 14:35 GMT-4 Version 5.2a 22 Replies
Please login with a confirmed email address before reporting spam
I was wondering whether it is possible to incorporate conditional statements within an expression?
For example,
if ( t > 6 ) then Zeta = 5
In this example I test the simulation time and if it is greater than 6 then I change a boundary condition.
This is possible in MATLAB, but can it be incorporated into an Expression within COMSOL?
Regards,
Tom
Please login with a confirmed email address before reporting spam
an expression V = V0+V1*(x>1) works well for many cases meaning V=V0 for X<= 1 and V=V0+V1 above.
now if you are in the time domain a F = F0*(t>1) to turn on the force F after 1 second is a "hard" way to go for your solver, try the heaviside functions to smoothen the turn on/off (search for heaviside in the doc
good luck
Ivar
Please login with a confirmed email address before reporting spam
Yes you can. Simply type, for example, 5*(t>6), which evaluates to 0 for simulation times up to 6 seconds and to 5 after 6 seconds.
Discontinuous functions like the step function above might cause numerical difficulties. COMSOL Multiphysics provides smoothing functions that you can use to make, for example, smooth step functions that are continuous. See the COMSOL Multiphysics User's Guide and the following Knowledge Base entry for details:
www.comsol.com/support/knowledgebase/905/
Best regards,
Magnus Ringh, COMSOL
Please login with a confirmed email address before reporting spam
I want to apply following step function in my temperature boundary condition:
T= 293 for t<600
T=283 for 600<t<3600
T=293 for 3600<t
How can this be applied?
Please login with a confirmed email address before reporting spam
293-10*flc2hs(t,0)*(600<t)+10*flc2hs(t,0)*(3600<t)
the resulting temperature at this boundary makes a nice step between 600 and 3600 s, but afterwards it starts to oscillate (while it should remain constant at 293) (see figure)
Any tips?
Attachments:
Please login with a confirmed email address before reporting spam
I can only think of two reasons for "oscillations":
i) your BC is oscillating, but you should be able to plot that function (or define it as a function first and then plot it from the user function menu)
ii) you have some numerical or strange model issues going on, ad one coul expect that to happen also if you sart with just one step
cannot suggest more like taht ;)
--
Good luck
Ivar
Please login with a confirmed email address before reporting spam
Regarding the above statement, I don't see how this syntax communicates that V=V0 for X<=1
This expression (V=V0) is not in the above syntax.
Maybe it would help if I explained what I am trying to do. I want a conditional statement that says:
if h > 100, then i = -1, if not i = 0
Please login with a confirmed email address before reporting spam
then what about:
i = -(h>100)
enter it in as a function and plot it, that is the easiest way I know to check equations in COMSOL
--
Good luck
Ivar
Please login with a confirmed email address before reporting spam
I have a similar problem. I have a simple bi-metal assembly. For a given temperature, it is either bend upward or not at all. i.e. like having an imaginary wall that prevents it to move downward. I tried to implement as a prescribed displacement : V0*(v<0) where V0=0.
It seems to create some sort of conflict. Is my appled BC is consistant with the problem statement?
Thanks for your advise.
Regards,
Susant
Please login with a confirmed email address before reporting spam
I believe its not balck and white, it depends ;) but what is certain, and I'm sure this reminds you something from your math courses one should keep driving equations (for the solver) continuous, and derivative at least once, if nottwice. So I would advice to use a step function or a heaviside function with a smooth (but short) transition width
In this way you ensure that the jacobian of your system can be evaluated without too much noise, hence the solver wil step through your discontinuity without failure
--
Good luck
Ivar
Please login with a confirmed email address before reporting spam
Thanks.
Regards,
Susant
Please login with a confirmed email address before reporting spam
Duplicate variable name.
- Variable: mod1.p
- Variable: mod1.p
- Variable: mod1.p
Error in multiphysics compilation.
I had an expression as "max(p,-capillarypressure*(phils <= .1))" defining p in Darcy's law; thus leaving p unchanged for phils > 0.1 and -capillarypressure elsewhere
How shall I obtain what i want?
Best regards
Christian
Hi,
Yes you can. Simply type, for example, 5*(t>6), which evaluates to 0 for simulation times up to 6 seconds and to 5 after 6 seconds.
Discontinuous functions like the step function above might cause numerical difficulties. COMSOL Multiphysics provides smoothing functions that you can use to make, for example, smooth step functions that are continuous. See the COMSOL Multiphysics User's Guide and the following Knowledge Base entry for details:
www.comsol.com/support/knowledgebase/905/
Best regards,
Magnus Ringh, COMSOL
Please login with a confirmed email address before reporting spam
I was working on a similar problem and had similar issues.
What worked for me is V0(v<=0) instead of V0(v<0). Not sure if this helps.
-Sagnik
Please login with a confirmed email address before reporting spam
And if I want a whole expression to be above 0, for example?
In my case, I'm trying to type a cosine function to simulate solar radiation throghout a year, with daily and annual variation. But I want that the function to be true only to values above 0.
I tried that:
((a+b*cos(2*pi*t/8760)+c*cos(2*pi*t/24))>0) or simply, ((f(t))>0)
It does not work.
Thanks for any help.
Alexandre.
Please login with a confirmed email address before reporting spam
The numbers 24 and 8760 in your expression suggest to me that perhaps you think t is in hours (8760 is the number of hours in a year, 24 the number of hours in a day), when it's in seconds, the SI unit for time.
Best,
Jeff
Please login with a confirmed email address before reporting spam
1[d]
1[a]
8760[h]
where the last two expressions both evaluate to the number of seconds in an astronomical year (31557600 s).
Best,
Magnus
Please login with a confirmed email address before reporting spam
For instance, suppose that you need to assign 5 to a variable on a rectangular surface with the dimensions
of 2 [mm] * 3 [mm], but not along the two edges at x=2 and y=3.
I tried the following expression with no success:
if ( (x<2[mm]) * (y<3[mm]) , 5 , 0 )
The issue is that only one of the conditions (first one) works. I understood it by plotting the results.
Please login with a confirmed email address before reporting spam
Can anyone give an advice on how to implement a conditional statement with two arguments?
For instance, suppose that you need to assign 5 to a variable on a rectangular surface with the dimensions
of 2 [mm] * 3 [mm], but not along the two edges at x=2 and y=3.
I tried the following expression with no success:
if ( (x<2[mm]) * (y<3[mm]) , 5 , 0 )
The issue is that only one of the conditions (first one) works. I understood it by plotting the results.
I found the solution and I'm going to share it with you.
you need to separate each condition and multiply them, i.e.
if ((x<2[mm]),5,0) * if ((y<3[mm]),5,0)
Hope this helps someone.
Please login with a confirmed email address before reporting spam
I found the solution and I'm going to share it with you.
you need to separate each condition and multiply them, i.e.
if ((x<2[mm]),5,0) * if ((y<3[mm]),5,0)
This way you get the value 25 if both conditions are satisfied, which sounds wrong to me. I suggest that you use the && operator: if ((x<2[mm]) && (y<3[mm]),5,0)
Please login with a confirmed email address before reporting spam
Thank you for your answer. In fact, I'm not having problems with units of time, since I can define that the argument t is in hours, then COMSOL makes the convertion automaticaly I believe.
I think I've found a solution to my problem, although I'm not using it anymore in my simulation.
I would create an analytic equation with the expression:
f(t) = a+b*cos(2*pi*t/8760)+c*cos(2*pi*t/24)
After I would created another one with a conditional statement:
if(f(t)>0, f(t), 0)
I didn't tested it, but it seems that it would work. Am I right?
Best Regards,
Alexandre
Please login with a confirmed email address before reporting spam
After I would created another one with a conditional statement:
if(f(t)>0, f(t), 0)
I didn't tested it, but it seems that it would work. Am I right?
This works. You can also use max(f(t), 0).
Please login with a confirmed email address before reporting spam
Please login with a confirmed email address before reporting spam
For instance,
I took the minimum temperature of a domain and i want to imply that the time when the minimum temperature in a system reaches 50 degC ?
regards
huseyin
Note that while COMSOL employees may participate in the discussion forum, COMSOL® software users who are on-subscription should submit their questions via the Support Center for a more comprehensive response from the Technical Support team.