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

Please login with a confirmed email address before reporting spam

Hi,

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

22 Replies Last Post Jul 21, 2017, 3:46 a.m. EDT
Ivar KJELBERG COMSOL Multiphysics(r) fan, retired, former "Senior Expert" at CSEM SA (CH)

Please login with a confirmed email address before reporting spam

Posted: 2 decades ago Aug 18, 2009, 3:19 a.m. EDT
Hi

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
Hi an expression V = V0+V1*(x>1) works well for many cases meaning V=V0 for X1) 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

Magnus Ringh COMSOL Employee

Please login with a confirmed email address before reporting spam

Posted: 2 decades ago Aug 18, 2009, 4:51 a.m. EDT
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
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: http://www.comsol.com/support/knowledgebase/905/ Best regards, Magnus Ringh, COMSOL

Maurits Vandegehuchte

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Nov 24, 2010, 5:09 a.m. EST
How can a funtion be applied in a boundary expression?

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?
How can a funtion be applied in a boundary expression? I want to apply following step function in my temperature boundary condition: T= 293 for t

Maurits Vandegehuchte

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Nov 24, 2010, 5:58 a.m. EST
If I use following function in the boundary expression:
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?
If I use following function in the boundary expression: 293-10*flc2hs(t,0)*(600


Ivar KJELBERG COMSOL Multiphysics(r) fan, retired, former "Senior Expert" at CSEM SA (CH)

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Nov 24, 2010, 8:40 a.m. EST
Hi

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
Hi 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

Posted: 1 decade ago Mar 3, 2011, 1:20 p.m. EST
<<V = V0+V1*(x>1) works well for many cases meaning V=V0 for X<= 1 and V=V0+V1 above>>

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

Regarding the above statement, I don't see how this syntax communicates that V=V0 for X 100, then i = -1, if not i = 0

Ivar KJELBERG COMSOL Multiphysics(r) fan, retired, former "Senior Expert" at CSEM SA (CH)

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Mar 3, 2011, 2:37 p.m. EST
Hi

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
Hi 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

Posted: 1 decade ago May 13, 2011, 3:06 p.m. EDT
Hi,

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
Hi, 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

Ivar KJELBERG COMSOL Multiphysics(r) fan, retired, former "Senior Expert" at CSEM SA (CH)

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago May 13, 2011, 4:26 p.m. EDT
Hi

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
Hi 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

Posted: 1 decade ago May 13, 2011, 9:46 p.m. EDT
hi Ivar,

Thanks.

Regards,
Susant
hi Ivar, Thanks. Regards, Susant

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Apr 23, 2013, 4:58 a.m. EDT
I have a similar problem in Darcy flow simulation where I want to change the field variable p only when a another field variable phils is below a certain value, the first error is:
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


I have a similar problem in Darcy flow simulation where I want to change the field variable p only when a another field variable phils is below a certain value, the first error is: 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

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Sep 4, 2014, 1:00 p.m. EDT
Susant,

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
Susant, I was working on a similar problem and had similar issues. What worked for me is V0(v

Alexandre Martins Alves

Please login with a confirmed email address before reporting spam

Posted: 8 years ago Jan 26, 2017, 10:23 a.m. EST
Hi,

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.
Hi, 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.

Jeff Hiller COMSOL Employee

Please login with a confirmed email address before reporting spam

Posted: 8 years ago Jan 26, 2017, 10:36 a.m. EST
Hi Alexandre,
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
Hi Alexandre, 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

Magnus Ringh COMSOL Employee

Please login with a confirmed email address before reporting spam

Posted: 8 years ago Jan 26, 2017, 11:21 a.m. EST
To add to Jeff's reply, COMSOL Multiphysics includes units for hour, day, and year, so you can use, for example,

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
To add to Jeff's reply, COMSOL Multiphysics includes units for hour, day, and year, so you can use, for example, 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

Posted: 8 years ago Feb 11, 2017, 1:03 a.m. EST
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.
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

Please login with a confirmed email address before reporting spam

Posted: 8 years ago Feb 11, 2017, 1:16 a.m. EST

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.
[QUOTE] 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

Gunnar Andersson COMSOL Employee

Please login with a confirmed email address before reporting spam

Posted: 8 years ago Feb 13, 2017, 2:38 a.m. EST

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)


[QUOTE] 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

Alexandre Martins Alves

Please login with a confirmed email address before reporting spam

Posted: 8 years ago Feb 23, 2017, 10:26 a.m. EST
Hi Jeff,

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
Hi Jeff, 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

Gunnar Andersson COMSOL Employee

Please login with a confirmed email address before reporting spam

Posted: 8 years ago Feb 23, 2017, 10:32 a.m. EST

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).

[QUOTE] 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? [/QUOTE] This works. You can also use max(f(t), 0).

Karunakaran Chellappa

Please login with a confirmed email address before reporting spam

Posted: 8 years ago Apr 9, 2017, 2:50 a.m. EDT
Thank you. It worked for me
Thank you. It worked for me

Please login with a confirmed email address before reporting spam

Posted: 7 years ago Jul 21, 2017, 3:46 a.m. EDT
Updated: 7 years ago Jul 21, 2017, 3:47 a.m. EDT
hi, i wonder could i set a condition that means "the time when temperature reaches to a certain level"?
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
hi, i wonder could i set a condition that means "the time when temperature reaches to a certain level"? 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.