external function

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

external function

James
my external function is in the form of

function [a, b] = myfun(c)

May I set a and b as output from myfun in fea.expr?

fea.expr = { 'a,b', 'myfun(c)'};

By this way both a and b values can be obtained by just running myfun once.

Thank you
Reply | Threaded
Open this post in threaded view
|

Re: external function

Precise Simulation
Administrator
This post was updated on .
James wrote
my external function is in the form of

function [a, b] = myfun(c)

May I set a and b as output from myfun in fea.expr?

fea.expr = { 'a,b', 'myfun(c)'};

By this way both a and b values can be obtained by just running myfun once.
No, basically the output must be a *single* array of the same size as the input. For example if you have 'x' as a coefficient but switch to 'myfun(x)', the output must have the same size as input x (all inputs will be arrays of the same size or scalars).

See the example here: https://www.featool.com/doc/physics.html#phys_coef_user

You can however cache results in your custom function for example using the Matlab "persistent" variable syntax:

https://www.mathworks.com/help/matlab/ref/persistent.html
Reply | Threaded
Open this post in threaded view
|

Re: external function

James

Thank you for your quick response.

Do you mean in Featool, I have to call external function as

fea.expr = { 'a', 'myfun(c)'; 'b', 'myfun(c)'}

to get a and b? 

The limitation of this call is that, if it takes long time to run myfun, I still need to run myfun two times to obtain a and b.

Do you have any better solutions?

Thanks.

On 2020-07-29 11:25 p.m., Precise Simulation [via FEATool Multiphysics Forum] wrote:
James wrote
my external function is in the form of

function [a, b] = myfun(c)

May I set a and b as output from myfun in fea.expr?

fea.expr = { 'a,b', 'myfun(c)'};

By this way both a and b values can be obtained by just running myfun once.
No, basically the output must be a *single* array of the same size as the input. For example if you have 'x' as a coefficient but switch to 'myfun(x)', the output must have the same size as input x (all inputs will be arrays of the same size or scalars).

See the example here: https://www.featool.com/doc/physics.html#phys_coef_user


If you reply to this email, your message will be added to the discussion below:
http://forum.featool.com/external-function-tp644p645.html
To unsubscribe from external function, click here.
NAML
Reply | Threaded
Open this post in threaded view
|

Re: external function

Precise Simulation
Administrator
James wrote
The limitation of this call is that, if it takes long time to run myfun,
I still need to run myfun two times to obtain a and b.
Yes, you have to run it multiple times for multiple outputs as you only have a *single* output is allowed for coeffcients.

You can however pre-compute/cache results in your custom function for example using the Matlab "persistent" variable syntax.