I'm recently working on finite element method with MATLAB
I tried to optimize my code in MATLAB.
While searching, I found that matrix assembling could be accelerated by using mex function.
While porting my "PoissonAssembler.m" to mex function, I stuck into some problems.
PoissonAssembler.m has basically this kind of structure.
for every element{
loc_stiff_assmbl();
loc_rhs_assmbl(); // this one involves with function evaluation @(x,y)
for every edges{
loc_edge_assmbl();
if boundary{
loc_rhs_edge_assmbl(); // this one also have function eval
}
}
}
In matlab, I have
f = @(x,y) some_math_function;
Since this function will be changed for other numerical simulation,
I wanted to use function handle as input for mex file
I found that there is a way to do this by using mexCallMatlab() and feval.
However, I think it will slow down my code because of overhead caused by calling matlab.
Is there any way to avoid it without compiling mex file every time I change the function handle?
More precise code is like this
for (k=0;k<nE;k++){ // nE : number of elements (about 10^5 to 10^6)
// other assembling procedure
blabla
// function evaluation for rhs assemble
for (i=0; i<nP; i++){ // nP : number of points in element
fxy[i] = f(x[i],y[i])};
}
// rhs assemble
for (j=0; j<nP; j++){
for (i=0; i<nP; i++){ // matrix vector multiplication
rhs[k*nE + j] += Mass[ i*nP + j]*fxy[i];
}
}
// face loop
for (f1 = 0; f1 < nF4E; f1++){ // number of face for each elements
switch(BCType[k1*nF4E + f1]){ // if boundary
case 1 : // Dirichlet
// inner product with evaluation of gD = @(x,y) function
CODE OMITTED
case 2 : // Neumann
// inner product with evaluation of gN = @(x,y) function
CODE OMITTED
}
}
}