Note: This discussion is about an older version of the COMSOL Multiphysics® software. The information provided may be out of date.

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.

Parametric sweep and fuction definition

Please login with a confirmed email address before reporting spam

Hi all,

I'm having some problems in using parameter sweep for parameters that are inside a definition of a function.

I built a simple example in which the temperature of a boundary is defined to be tempInput(z). The function tempInput is defined to be sin(nodesIn*z*2*pi), and nodesIn is a global parameter. I then defined a study with a parametric sweep on the parameter nodesIn on, let's say, 4 different values. When I solve the model, the solver generates a solution for all the 4 parameters, but the solution is exactly the same (corresponding to the default value of nodesIn).

To be sure I didn't made stupid errors in the model, I then redefined the boundary temperature to be tempInput(nodesOut*z) (thus nodesOut plays an equivalent role as nodesIn, except that is used outside the function definition). I then defined another study with a parametric sweep over nodesOut, and this time the solutions generated for different values of nodeOut are indeed different (as expect).

So far, it seem to me that sweeping on parameters used inside a function definitions simply doesn't work. Is that true? Does anyone knows why? Possible workarounds?

However, that's not the end of the story. I do have a real model (that I think is too complicated to be posted here, but if you want...) in which I defined 4 different functions for 4 temperature profiles, let's say temp1... temp4. Then, I created another function defined as:
(n==1)*temp1+(n==2)*temp2+(n==3)*temp3+(n==4)*temp4
and assigned that as the temperature function in the boundary condition. I did that so I could easily switch between the 4 parameters by simply changing the value of the parameter n. Now, I DID USE that in a parametric sweep in that model, and it worked! I also set up different studies and added a "parametric" node to the solver to just select a particular value for n different from the default, and again it worked. That is strange, because it seems to contradict the above conclusion that parameters used inside a function definition can't be swept (well, they can, but the solution doesn't change!). Unfortunately, again, I tried to implement the same thing in the example model and... it doesn't work!

I'm really, really confused...

The model I'm attaching summarizes all these cases:
- in the study InnerParameter, I'm sweeping on the parameter used inside the function definition: no difference between the solutions
- in the study OuterParameter, I'm sweeping on the parameter used in the argument of the function (thus, outside the function definition): all the solutions are different, as expected
- in the study BooleanParameter, i tried the (n==?) trick described above, but it didn't work (although I repeat I have a model that is currently working with that trick... don't ask me how!)
- in the study ManualParametric, I try to add a parametric node directly to the solver (instead of using a parametric sweep to the study node) to simply select a value for the parameter n different from the default (the same technique I used in the model that works!), but it doesn't work.

I'll be grateful for any help...

Giacomo


6 Replies Last Post 25 ago 2011, 00:49 GMT-4

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 15 ago 2011, 17:42 GMT-4
It looks like no one is answering to this post (meaning that either it's not interesting, or no one knows the solution), so I figured out I could do it. :-)

First of all, I have a correction (with apologies) to my previous post. The following part is actually wrong:


However, that's not the end of the story. I do have a real model (that I think is too complicated to be posted here, but if you want...) in which I defined 4 different functions for 4 temperature profiles
...[CUT]...
Now, I DID USE that in a parametric sweep in that model, and it worked! I also set up different studies and added a "parametric" node to the solver to just select a particular value for n different from the default, and again it worked. That is strange, because it seems to contradict the above conclusion that parameters used inside a function definition can't be swept (well, they can, but the solution doesn't change!). Unfortunately, again, I tried to implement the same thing in the example model and... it doesn't work!


Apparently I did found the problem, and a solution to it, when I first implemented the complicated model... then I forgot! :-(
My workaround was to define the parameter n (to be used inside the function) as an argument to the function itself. At this point, I was able to use the parametric sweep, that apparently changes the value of n in the call to the function, instead of in the definition of the function itself (that I discovered wouldn't work).

Summarizing and concluding, for the benefits of other having the same issue, it seems to me that:
- parameters can be used in function definitions, but their values cannot be changed during a parametric sweep (they can, but this does not affect the function definition, that takes either the default value or the first of the values set in the sweep). Why this, is still a mystery to me.
- an easy and viable workaround is to define parameters that you may want to vary during solving as arguments to the function, and the passing the parameter as a value of the argument.

Example:

- defining f(x)=n*x^2, using f(x) somewhere and sweeping n=2, 3, 4 will not work (f will always be interpreted as 2*x^2)
- defining f(x,k)=k*x^2, using f(x,n) somewhere and sweeping n=2, 3, 4 will work as expected (producing 2*x^2, 4*x^2, 6*x^2)

Giacomo
It looks like no one is answering to this post (meaning that either it's not interesting, or no one knows the solution), so I figured out I could do it. :-) First of all, I have a correction (with apologies) to my previous post. The following part is actually wrong: [QUOTE] However, that's not the end of the story. I do have a real model (that I think is too complicated to be posted here, but if you want...) in which I defined 4 different functions for 4 temperature profiles ...[CUT]... Now, I DID USE that in a parametric sweep in that model, and it worked! I also set up different studies and added a "parametric" node to the solver to just select a particular value for n different from the default, and again it worked. That is strange, because it seems to contradict the above conclusion that parameters used inside a function definition can't be swept (well, they can, but the solution doesn't change!). Unfortunately, again, I tried to implement the same thing in the example model and... it doesn't work! [/QUOTE] Apparently I did found the problem, and a solution to it, when I first implemented the complicated model... then I forgot! :-( My workaround was to define the parameter n (to be used inside the function) as an argument to the function itself. At this point, I was able to use the parametric sweep, that apparently changes the value of n in the call to the function, instead of in the definition of the function itself (that I discovered wouldn't work). Summarizing and concluding, for the benefits of other having the same issue, it seems to me that: - parameters can be used in function definitions, but their values cannot be changed during a parametric sweep (they can, but this does not affect the function definition, that takes either the default value or the first of the values set in the sweep). Why this, is still a mystery to me. - an easy and viable workaround is to define parameters that you may want to vary during solving as arguments to the function, and the passing the parameter as a value of the argument. Example: - defining f(x)=n*x^2, using f(x) somewhere and sweeping n=2, 3, 4 will not work (f will always be interpreted as 2*x^2) - defining f(x,k)=k*x^2, using f(x,n) somewhere and sweeping n=2, 3, 4 will work as expected (producing 2*x^2, 4*x^2, 6*x^2) Giacomo

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 18 ago 2011, 10:11 GMT-4
Thanks pal you saved my day. I really think this is a bug or the parametric sweep help entry should be updated by a comment concerning parameters in functions
Thanks pal you saved my day. I really think this is a bug or the parametric sweep help entry should be updated by a comment concerning parameters in functions

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 18 ago 2011, 12:01 GMT-4
Glad to know that the the time spent writing the solution was worth something! :-)

Have a good one

Giacomo
Glad to know that the the time spent writing the solution was worth something! :-) Have a good one Giacomo

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 18 ago 2011, 16:05 GMT-4
Hi

indeed this are important effects to know, thanks for the analysis.

Now, for me, I cannot call it a bug though, as I interpret it as an issue of "global" and "local" variables, and the order of which functions are interpreted.

this means that Parameters are not Variables

perhaps, to be tested, if you write in a Variable Nvar = Npar, where Npar is a Parameter updated by the Parametric Sweep, and then refer to Nvar in your function it might work, I will have to check once back in the office ;)

--
Good luck
Ivar
Hi indeed this are important effects to know, thanks for the analysis. Now, for me, I cannot call it a bug though, as I interpret it as an issue of "global" and "local" variables, and the order of which functions are interpreted. this means that Parameters are not Variables perhaps, to be tested, if you write in a Variable Nvar = Npar, where Npar is a Parameter updated by the Parametric Sweep, and then refer to Nvar in your function it might work, I will have to check once back in the office ;) -- Good luck Ivar

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 24 ago 2011, 21:32 GMT-4
I wish to come up with a way to scan the wavelength for a given waveguide
structure and obtain the calculated mode field shape (eigenfunctions)
and modal constants (eigenvalues), saving into files in some way. I believe
that I can achieve this either using Matlab code which runs
COMSOL, or perhaps just COMSOL alone.

Your input is greatly appreciated. Thanks in advance.

Cheers,
-Ed
eiskander@hotmail.com
I wish to come up with a way to scan the wavelength for a given waveguide structure and obtain the calculated mode field shape (eigenfunctions) and modal constants (eigenvalues), saving into files in some way. I believe that I can achieve this either using Matlab code which runs COMSOL, or perhaps just COMSOL alone. Your input is greatly appreciated. Thanks in advance. Cheers, -Ed eiskander@hotmail.com

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 25 ago 2011, 00:49 GMT-4
Not sure which kind of input you are looking for. Parametric sweeps (more in the help) are probably a way youo can do it. Not sure how you can save the different files, just because I never tried.

BTW, it's good habit to start a new thread if you are submitting a different problem/help request (as in this case)

Giacomo
Not sure which kind of input you are looking for. Parametric sweeps (more in the help) are probably a way youo can do it. Not sure how you can save the different files, just because I never tried. BTW, it's good habit to start a new thread if you are submitting a different problem/help request (as in this case) Giacomo

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.