OpenFOAM with GSL

using GSL (GNU Scientific Library) in OpenFOAM

编译安装

在源码目录下,执行下列命令:

./configure --prefix=/THL6/home/yzg/OpenFOAM/gsl-2.4/build 2>&1 | tee c.txt
make
make install

系统环境变量

~/.bashrc 文件中添加:

# GSL
export LD_LIBRARY_PATH=/THL6/home/yzg/OpenFOAM/gsl-2.4/build/lib:$LD_LIBRARY_PATH

在 OF solver 中使用 GSL

options 文件中添加:

EXE_INC = \
...
-I/THL6/home/yzg/OpenFOAM/gsl-2.4/build/include \


EXE_LIBS = \
...
-L/THL6/home/yzg/OpenFOAM/gsl-2.4/build/lib \
-lgsl \
-lgslcblas \

然后按照需要包含其头文件:

#include <gsl/gsl_spline2d.h>
#include <gsl/gsl_spline.h>
#include <gsl/gsl_interp2d.h>
#include <gsl/gsl_interp.h>
#include <gsl/gsl_sf_gamma.h>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_linalg.h>

使用举例

一维数组插值

gsl_interp_accel *acc = gsl_interp_accel_alloc();
const gsl_interp_type *t = gsl_interp_linear;
gsl_spline *ZY_spline
= gsl_spline_alloc (t, index_old.size());
gsl_spline_init
(ZY_spline, index_old, data_old, index_old.size());
forAll(index_new, i)
{
scalar getValue =
gsl_spline_eval (ZY_spline, index_new[i], acc);
data_new[i] = getValue;
}

beta 积分

scalar beta_ab_gsl = gsl_sf_beta(a,b);
scalar beta_inc_abz_gsl = gsl_sf_beta_inc(a,b,z)

矩阵求逆

gsl_matrix* Foam::ZYclass::GetInverse(gsl_matrix *A)
{
label n = A->size1; label sign = 0;
gsl_matrix *inverse;

gsl_matrix *tmpA = gsl_matrix_alloc(n, n);
gsl_matrix_memcpy(tmpA, A);
gsl_permutation *p = gsl_permutation_alloc(n);

gsl_linalg_LU_decomp(tmpA, p, &sign);
inverse = gsl_matrix_alloc(n, n);
gsl_linalg_LU_invert(tmpA, p, inverse);
gsl_permutation_free(p);
gsl_matrix_free(tmpA);
return inverse;
}

矩阵相乘

gsl_matrix* Foam::ZYclass::gsl_matrix_mul(gsl_matrix *a,gsl_matrix *b)
{
gsl_matrix *c=gsl_matrix_alloc(a->size1, b->size2);
for (size_t i=0;i<a->size1;i++)
{
for (size_t j=0;j<b->size2;j++)
{
double sum=0.0;
for (size_t k=0;k<b->size1;k++)
{
sum+=gsl_matrix_get(a,i,k)*gsl_matrix_get(b,k,j);
}
gsl_matrix_set(c,i,j,sum);
}
}
return c;
}
文章作者: Yan Zhang
文章链接: https://openfoam.top/gsl/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 OpenFOAM 成长之路
您的肯定会给我更大动力~