我最近正在使用 MATLAB 研究有限元方法
我试图在 MATLAB 中优化我的代码。
在搜索时,我发现使用 mex 函数可以加速矩阵组装。
在将我的“PoissonAssembler.m”移植到 mex 函数时,我遇到了一些问题。
PoissonAssembler.m 基本上就是这种结构。
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
}
}
}
在matlab中,我有
f = @(x,y) some_math_function;
由于此函数将更改为其他数值模拟,
我想使用函数句柄作为 mex 文件的输入
我发现有一种方法可以通过使用 mexCallMatlab() 和 feval 来做到这一点。
但是,我认为由于调用 matlab 引起的开销,它会减慢我的代码。
每次更改函数句柄时,有什么方法可以避免它而不编译 mex 文件?
更精确的代码是这样的
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
}
}
}