Discussion Locked This discussion was was locked by a forum moderator.

Variables & Functions in Comsol 4.1

Alois Pfenniger
Hi,

I am trying to do something very simple that worked perfectly in 3.5a, but that I cannot reproduce in 4.1. I have the feeling that I am totally missing something, but I do not know what.

I define a global variable, myVar, and want to use it in a global function, myFun. The function myFun does not recognize the variable myVar (when plotting the function). Why???
Next, I want to use this function (which is a function of time) to define a boundary load (Model 1>Solid Mechanics>Boundary load 1). But the function is not recognized (it stays orange). Why???

I have put the example as attachment.

Thanks for any help.

Alois Pfenniger


14 Replies Last Post Aug 4, 2015, 10:35 AM EDT
Posted: 7 years ago Mar 23, 2011, 2:16 AM EDT
Hi

there are the color code that Mostly works OK, but sometimes need a solve initialisation, or a refresch such as exciting and reentering a node to get updated.

The colors are orange for unit errors, and red for typos (missing (), undefined variables ...)

Then there are a few things to learn:

operators and functions require unitless entries (and return unitless)

hence if you wanf to use myFunc(myVar) as a force input in Newtons, you should write:

1[N]*myfunc(myVar[1/myVar_Units])

or just

myfunc(myVar[1/myVar_Units])[N]

if myVar are in microns, and you write myVar[1/m] then the microns are scaled to meters and entered into the function as meter value, but without units

Then another point: if you have solved a model, and you add some operators, these are not understood by the Results section before you have "updated the solver" or done a full new "Solve". Therefore they are not accessible and flagged as unknown in a postprocessing sequence.

Finally, if you leave the mouse pointer over a variable field with an error, normally COMSOL will tell you the error it detected, in a small popup

--
Good luck
Ivar
Hi there are the color code that Mostly works OK, but sometimes need a solve initialisation, or a refresch such as exciting and reentering a node to get updated. The colors are orange for unit errors, and red for typos (missing (), undefined variables ...) Then there are a few things to learn: operators and functions require unitless entries (and return unitless) hence if you wanf to use myFunc(myVar) as a force input in Newtons, you should write: 1[N]*myfunc(myVar[1/myVar_Units]) or just myfunc(myVar[1/myVar_Units])[N] if myVar are in microns, and you write myVar[1/m] then the microns are scaled to meters and entered into the function as meter value, but without units Then another point: if you have solved a model, and you add some operators, these are not understood by the Results section before you have "updated the solver" or done a full new "Solve". Therefore they are not accessible and flagged as unknown in a postprocessing sequence. Finally, if you leave the mouse pointer over a variable field with an error, normally COMSOL will tell you the error it detected, in a small popup -- Good luck Ivar

Alois Pfenniger
Posted: 7 years ago Mar 23, 2011, 5:55 AM EDT
Hi,

Thanks for your answer. I get the point with the use of functions that require unitless entries :-)

The point concerning the use of a variable in a function was maybe not explained correctly. Here is what I want to do:

In global variables:
myVar=100

In global functions:
myFun(t)=myVar*t

Now when I want to plot the function myFun over the interval [0 1], I get the following error message:

Undefined variable
-Variable: myVar
-Function: myFun
-Plot: previewplot1 (Line graph)
-Function: myFun

If you open the file attached to my first post, you should be able to reproduce this behaviour. It seems that it is not possible to define a variable and to use it as a constant in a function?

Thanks.

Alois
Hi, Thanks for your answer. I get the point with the use of functions that require unitless entries :-) The point concerning the use of a variable in a function was maybe not explained correctly. Here is what I want to do: In global variables: myVar=100 In global functions: myFun(t)=myVar*t Now when I want to plot the function myFun over the interval [0 1], I get the following error message: Undefined variable -Variable: myVar -Function: myFun -Plot: previewplot1 (Line graph) -Function: myFun If you open the file attached to my first post, you should be able to reproduce this behaviour. It seems that it is not possible to define a variable and to use it as a constant in a function? Thanks. Alois

Magnus Ringh COMSOL Employee
Posted: 7 years ago Mar 23, 2011, 8:01 AM EDT
Hi,

The problem is the use of a Global Variable in the Analytic function. Analytic functions take one or more argument that you define directly in the Settings window for the Analytic function. That is, you can define it without specifying the actual names of the arguments that you will use when calling it. For defining analytic functions you can use any predefined mathematical functions and also Global Parameters but not Global Variables. That is because a Global Variable can have an explicit dependence on the time t, for example. So, in your model, define myVar as a Global Parameter instead.

If the definition of myVar*t means "a constant times the time t" you can define it directly as Global Variable, where "t" is a predefined variable for time. If you define it as analytic function, you can call it with "t" for time or any other input argument.

Best regards,
Magnus Ringh, COMSOL
Hi, The problem is the use of a Global Variable in the Analytic function. Analytic functions take one or more argument that you define directly in the Settings window for the Analytic function. That is, you can define it without specifying the actual names of the arguments that you will use when calling it. For defining analytic functions you can use any predefined mathematical functions and also Global Parameters but not Global Variables. That is because a Global Variable can have an explicit dependence on the time t, for example. So, in your model, define myVar as a Global Parameter instead. If the definition of myVar*t means "a constant times the time t" you can define it directly as Global Variable, where "t" is a predefined variable for time. If you define it as analytic function, you can call it with "t" for time or any other input argument. Best regards, Magnus Ringh, COMSOL

Alois Pfenniger
Posted: 7 years ago Mar 24, 2011, 9:39 AM EDT
Hi,

Thanks for the explanation. Now I understand better how Comsol 4.1 deals with variables and functions :-)

Regards,

Alois Pfenniger
Hi, Thanks for the explanation. Now I understand better how Comsol 4.1 deals with variables and functions :-) Regards, Alois Pfenniger

Tijana Timotijevic
Posted: 6 years ago Nov 28, 2011, 2:56 PM EST
Hello,
May I add to the question with my query: how can I define a function of a function (in 4.2)?

I defined functions:

epc = e1 - j*s1/(2*pi*freq)
emc = e2 - j*s2/(2*pi*freq)
Arguments: freq [Hz}

When I try to define and then plot the third function, F=(epc(freq)-emc(freq))/(epc(freq)+2*emc(freq)), I get the error message

"Unknown function: epc".

I tried using synthax epc(freq[1/Hz]) - I still get the "unknown function" error, or just simply epc-emc/(epc+2*emc) without arguments but then COMSOL interprets it as a variable.

Any ideas how I could do this?
Many thanks!
Hello, May I add to the question with my query: how can I define a function of a function (in 4.2)? I defined functions: epc = e1 - j*s1/(2*pi*freq) emc = e2 - j*s2/(2*pi*freq) Arguments: freq [Hz} When I try to define and then plot the third function, F=(epc(freq)-emc(freq))/(epc(freq)+2*emc(freq)), I get the error message "Unknown function: epc". I tried using synthax epc(freq[1/Hz]) - I still get the "unknown function" error, or just simply epc-emc/(epc+2*emc) without arguments but then COMSOL interprets it as a variable. Any ideas how I could do this? Many thanks!

Posted: 6 years ago Nov 29, 2011, 1:46 AM EST
Hi

I'm not sure what it could be, but I expect you are aware that many of the short names are already used by COMSOL such as s1 and s2, perhaps some of the others too. This is not necessarily flagged by COMSOL, as certain redefinitions are allowed, but when done by error strange things comes out of your solver ;)

--
Good luck
Ivar
Hi I'm not sure what it could be, but I expect you are aware that many of the short names are already used by COMSOL such as s1 and s2, perhaps some of the others too. This is not necessarily flagged by COMSOL, as certain redefinitions are allowed, but when done by error strange things comes out of your solver ;) -- Good luck Ivar

Posted: 5 years ago Feb 20, 2013, 11:50 AM EST
Hi Mr Var,
My name is Arsene. I would need your help to solve a COMSOL problem.

I have a data table of specific heat (Cp) of Gadolinium. Those data variate with temperature (T) and magnetic field (H) (see attached file).
I would like to import this data into my comsol model and define a two-dimensional interpolation function, so that, for any value of T and H calculated by the model I could find a value of Cp.
How could i do it

Thankyou in advance for you precious help

Hi Mr Var, My name is Arsene. I would need your help to solve a COMSOL problem. I have a data table of specific heat (Cp) of Gadolinium. Those data variate with temperature (T) and magnetic field (H) (see attached file). I would like to import this data into my comsol model and define a two-dimensional interpolation function, so that, for any value of T and H calculated by the model I could find a value of Cp. How could i do it Thankyou in advance for you precious help


Posted: 5 years ago Feb 20, 2013, 2:40 PM EST
Hi Arsene

I believe its the format that is not correct, you need to order it in columns and not in a matrix (check user 4.3a guide p 194++)

But then also, look at the material from the material data base that is using T and or Pa dependence. For materials you need to define a blanc material, add the interpolation function, read from file, and check that its the correct format "T, H, data" in columns (there is an example to in the User guide, and more in the materials doc, if you have that one)

--
Good luck
Ivar
Hi Arsene I believe its the format that is not correct, you need to order it in columns and not in a matrix (check user 4.3a guide p 194++) But then also, look at the material from the material data base that is using T and or Pa dependence. For materials you need to define a blanc material, add the interpolation function, read from file, and check that its the correct format "T, H, data" in columns (there is an example to in the User guide, and more in the materials doc, if you have that one) -- Good luck Ivar

Posted: 4 years ago Feb 12, 2014, 5:40 AM EST
Hello,Mr Ivar Kjelberg! I have a question. I define a Gaussian Pulse m and want to put it in the Added mass condition. My expression is m(x[1/x_Units])[kg]. x is a variable. But it seems wrong because the color is orange. Where am I wrong? Thanks!
Ben
Hello,Mr Ivar Kjelberg! I have a question. I define a Gaussian Pulse m and want to put it in the Added mass condition. My expression is m(x[1/x_Units])[kg]. x is a variable. But it seems wrong because the color is orange. Where am I wrong? Thanks! Ben

Posted: 3 years ago Oct 20, 2014, 1:54 AM EDT
hi Alois Pfenniger,

would you mind to share your model in 3,5 version? i believe i had the same problem as yours.
Tq
hi Alois Pfenniger, would you mind to share your model in 3,5 version? i believe i had the same problem as yours. Tq

Alois Pfenniger
Posted: 3 years ago Oct 21, 2014, 3:39 AM EDT
Hi,
I regret but I do not have the 3.5 file anymore.
Best,
Alois
Hi, I regret but I do not have the 3.5 file anymore. Best, Alois

Posted: 2 years ago Jun 2, 2015, 4:18 PM EDT
i have a doubt..
the study is stationary, so how can it solve for myVar*t
i have a doubt.. the study is stationary, so how can it solve for myVar*t

Posted: 2 years ago Aug 4, 2015, 9:51 AM EDT
Can you tell me the predefined function for tangential electric field, x component? I want my function to be dependent on the tangential Electric field and use it to define a property of a material.
Can you tell me the predefined function for tangential electric field, x component? I want my function to be dependent on the tangential Electric field and use it to define a property of a material.

Walter Frei COMSOL Employee
Posted: 2 years ago Aug 4, 2015, 10:35 AM EDT
Dear All,

This thread seems to be spanning several different topics and is loosing focus. In the interests of keeping each thread focused around a single subject, we are closing this thread for further comments. If there are any other related questions, please open a new thread, and reference this one, if it is relevant.

With respect to the latest question, about the tangential derivatives and components: please see the section on "Predefined and built-in variables" in the COMSOL Multiphysics Reference Manual.

Best Regards,
Dear All, This thread seems to be spanning several different topics and is loosing focus. In the interests of keeping each thread focused around a single subject, we are closing this thread for further comments. If there are any other related questions, please open a new thread, and reference this one, if it is relevant. With respect to the latest question, about the tangential derivatives and components: please see the section on "Predefined and built-in variables" in the COMSOL Multiphysics Reference Manual. Best Regards,

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.