Use of Functions as Boundary Conditions

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

Use of Functions as Boundary Conditions

StoneFrench
Hello,

I am attempting to use a function to define my boundary inlet flow. The function I am attempting to use is shown below, however when I run this function it appears to only use the else statement and only runs the input as if it is a constant 27.9258. Can you explain why this is and what I need to do to correct this? My total height is 30000 so it should be using the log function for everything below 10000 but this doesn't appear to be the case.

function output=myfun_BoundaryInletFlow(y)
if y<10000
    output=2.5*log(y+1)+4.9;
else
    output=27.9258;
end
end
Reply | Threaded
Open this post in threaded view
|

Re: Use of Functions as Boundary Conditions

Precise Simulation
Administrator
This post was updated on .
For efficiency reasons custom functions are called in a vectorized manner, that is y is not a scalar but an array of several y vales to be evaluated in each call. You then either have to write a for loop for each y value (potentially slow in Matlab)

output = zeros(size(y));
for i=1:numel(y)
  if y(i)<10000
    output(i)=2.5*log(y(i)+1)+4.9;
  else
    output(i)=27.9258;
  end
end

or used vectorized evaluation, something like:

output = 27.9258*ones(size(y));
output(y<10000) = 2.5*log(y(y<10000)+1)+4.9;

edit:

That said, you should be able to implement this BC expression using logical switch expressions directly in the GUI without using a custom Matlab function, something like:

    (2.5*log(y+1)+4.9)*(y<10000)+27.9258*(y>=100000)

StoneFrench wrote
I am attempting to use a function to define my boundary inlet flow. The function I am attempting to use is shown below, however when I run this function it appears to only use the else statement and only runs the input as if it is a constant 27.9258. Can you explain why this is and what I need to do to correct this? My total height is 30000 so it should be using the log function for everything below 10000 but this doesn't appear to be the case.

function output=myfun_BoundaryInletFlow(y)
if y<10000
    output=2.5*log(y+1)+4.9;
else
    output=27.9258;
end
end