# Discussion Forum

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.

## How to use user defined functions as physics's input? Thanks!

Hello, every one!
I am learnging to use COMSOL4.2 and want to add AC signal as my device's input.

Just as Eric told me to write an equation as the input.

I want to try another way.

Because there is a way to define a function( Model bulider--> Global Definition-->Function),

I can define a funciton (name wv1) as wv1= 5 *sin( 50*pi* t ).

My question is, how can I use this function as an AC input to a capacitor's input?

Thanks!

14 Replies Last Post Jan 23, 2013, 10:08 PM EST
Posted: 5 years ago
Hi

you can define a global function or a model function (I usually use the later), the you can use the transient solver, bu defining your current/voltage at your port/terminal node as A0*funct(t) where A0 is you amplitude and funct(t) is the name of your function. If you have not define any specific units you might need to call it with a funct(t[1/s]) to make the argument "unitless"

But be aware that with a transient solver you have all the ringing at the start as usually the system is at rest (all "0") and you give it a kick start by asking at t>0 to have large values (also for the gradients). Therefore for true sustained AC values it's far easier and simpler to use the harmonic development with a frequency defined in the "frequency domain" solver node, and only the amplitude A0 defined in the port/terminal node

--
Good luck
Ivar

Posted: 5 years ago
Thanks! Ivar.

Sorry , I cann't understand your explanation.

My question is after my defining the function "wv1", how can I use this funciton
elsewhere by using "wv1", or "root.wv1", or "model.wv1" or the like , just want
to replace some parameter.

As in the the 'Global section' , I define a parameter
Vac = 5 *sin( 50*pi* t ), and I can use Vac in my capacitor terminal .

I also want to define a function, and use it's name as the input of the
capacitor terminal.

thanks.

Posted: 5 years ago
Hi

you need the argument "t" try Wv1(t) or even vw1(t[1/s]) if you have not defined any units for the argument "t"
Then you need to run a time serie / transients solver , else "t" is not defined internally by COMSOL

--
Good luck
Ivar

Posted: 5 years ago
Hi,

I have a matrix A containing 3 columns, x, y and C. 'x' and 'y' are coordinates and C is the concentration. I want to initialize the domain with these values via matlab. How can I do this? I do not want to write the data to a file and then read it in via the interpolation function. I want this to be done directly from the memory. Can you kindly help me with this?

Thanks,

Hamsa

Posted: 5 years ago

Hi

you need the argument "t" try Wv1(t) or even vw1(t[1/s]) if you have not defined any units for the argument "t"
Then you need to run a time serie / transients solver , else "t" is not defined internally by COMSOL

--
Good luck
Ivar

Thanks Ivar!

I tried both : wv1(t) and wv1(t[1/s]), and COMSOL seems can accept both expressions.

But I find that when I use them in time-dependent solver, the magnitude of the voltage

One thing to mention is that when I define the sin function amplitude, I can only input
5( not 5V) as the amplitude.

I am keeping trying ......

Wang Jiujiang

Posted: 5 years ago
To Ivar:

I cannot find time serie / transients solver in my COMSOL4.2 by now.

Let me try .

Thanks !

Wang Jiujiang

Posted: 5 years ago
To Hamsa Dhwani:
I am not familiar with this by now.

Good luck!

Posted: 5 years ago
Hi

probably you must use Matlab for that, as I do not know how to populate a matrix without reading it in froma file into an interpolation function.

One of the issues is that you need to map the C values onto the surface, with the (x,y) grid from your matrix mapping, but then the mesh might have a different mapping, so COMSOL must interpolate anyhow and transfer the correct values where the mesh requires it.

Only other way, you have tha same area and you use COMSOL to define your matrix of C, then you can map that over to your model using it as initial conditions

--
Good luck
Ivar

Posted: 5 years ago

Hi

you need the argument "t" try Wv1(t) or even vw1(t[1/s]) if you have not defined any units for the argument "t"
Then you need to run a time serie / transients solver , else "t" is not defined internally by COMSOL

--
Good luck
Ivar

I defined a waveform funciton as wv1
( amplitude 5 (no unit), angular frequency 50*pi; phase 0 )
and I can draw it's waveform with amplitude 5( no unit, does that mean 5V).

And I define a global parameter as Vdc = 5* sin( 50 *pi ) [V], I can plot it's waveform
in the Result section using 1-D global, and the result is a discrete sin waveform.

When I use wv1(t) or wv1(t[1/s]) as a parameter to replace the global parameter Vdc,
the Result is a non-uniform waveform , especially the amplitude is very small.

Also, the runtime of COMSOL is much much longer when using wv1.

Thanks !

Wang Jiujiang

Posted: 5 years ago
Hi

I do not understand your "without argument" a function must have a argument otherwise its simply a parameter
COMSOL does not propose any "argument" but expects you to provide one

You should read the wv1 node as

Amplitude*sin(Angular_frequency*dummy_argument+Phase)

and you call it i.e. with a wv1(t[1/s])*1[m]

note the units wv1 expect an argument that is unit-less and provide no units so when used you must normalise the argument to a number and multiply by the amplitude units

You should plot your function to check it, then you will notice that COMSOL uses a default argument "t" time for the horizontal axis. but
(COMSOL developers you are floppy here !!)
for some unbelievable reason COMSOL does not propose any names nor units for the default plotting of functions !

this is rather poorly done, I find,as they have everything, and it would be far clearer to write out fully: "wv1(t)" for the plot title and "t" for the horizontal axis and "Amplitude" for the vertical axis titles

--
Good luck
Ivar

Posted: 5 years ago
Thanks , Ivar!

It seems that I used the wv1 in the wrong format.

I am not sure I understand what you said, but according to your example of using wv1
wv1(t[1/s])*1[m], I use it as a AC voltage, so I write it as wv1(10*t)*5 [V] , but they are
not work!

So if there is any problem of my using wv1, please tell me.
Or I must quit using the funciton, because it wasted us a lot of time.

Thanks!

Wang Jiujiang

Posted: 5 years ago
Hi

yes your formula should be correct, so there must be something else that is wrong, in ACDC indeed for a voltage you would write either 5[V]*wv1(f*t) with wv1 defined as Amplitude =1, angular frequency 2*pi and phase whatever you need, probably "0", and f is the frequency in Hz and t the time in S (their units cancel out as required

or you could write wv1(t[1/s])*1[V] and define wv1 as Amplitude =5 frequency 2*pi*f and phase whatever

this should work. You should define this under model Definitions and use it in the model. if you define it in Global Definitions you might need to refer to it as "root.wv1(t)" and formally the Model Definition is accessed as "mod1.wv1(t)" but inside the "model1" the "mod1." is implicit and does not really need to be written out. This is required if you have several models and want to link them across

--
Good luck
Ivar

Posted: 5 years ago

Hi

probably you must use Matlab for that, as I do not know how to populate a matrix without reading it in froma file into an interpolation function.

One of the issues is that you need to map the C values onto the surface, with the (x,y) grid from your matrix mapping, but then the mesh might have a different mapping, so COMSOL must interpolate anyhow and transfer the correct values where the mesh requires it.

Only other way, you have tha same area and you use COMSOL to define your matrix of C, then you can map that over to your model using it as initial conditions

--
Good luck
Ivar

Hi Ivar,

Thanks for your reply. I am using matlab. I am not sure of the sequence of commands to access a particulat coordinate and then set C at that coordinate. I know to set a value for the entire domain. But not sure how to do it for individual points in the domain.

I suspect something like

model.selection(<domain>).set('posx', <x0>);
model.selection(<domain>).set('posy', <y0>);
model.selection(<domain>).set('C', <C0>);

Not sure if this is the right way to do though.

Thanks,

Posted: 5 years ago

Hi

yes your formula should be correct, so there must be something else that is wrong, in ACDC indeed for a voltage you would write either 5[V]*wv1(f*t) with wv1 defined as Amplitude =1, angular frequency 2*pi and phase whatever you need, probably "0", and f is the frequency in Hz and t the time in S (their units cancel out as required

or you could write wv1(t[1/s])*1[V] and define wv1 as Amplitude =5 frequency 2*pi*f and phase whatever

this should work. You should define this under model Definitions and use it in the model. if you define it in Global Definitions you might need to refer to it as "root.wv1(t)" and formally the Model Definition is accessed as "mod1.wv1(t)" but inside the "model1" the "mod1." is implicit and does not really need to be written out. This is required if you have several models and want to link them across

--
Good luck
Ivar

Dear Ivar:
It works!!!
According to my usage, root.wv1(t)*1[V] and 1[V]*root.wv1(t) and root.wv1(r[1/s]) *1[V] are all OK,
but root.wv1(f*t) doesn't work.

And if I define the funciton inside the model, the same rule is appliable.

Thanks you very much for your instrction and time and patience!

Wang Jiujiang

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.