openfoam学习笔记-6
学习笔记-6
添加自己的边界条件
在openfoam中,多态应用很广,例如一下模型都用到了多态:
- Turbulence models
- Boundary conditions
- functionObjects
如果我们想创建一个新的湍流模型,只需要从基类继承,openfoam甚至允许程序运行的过程中通过controlDict修改模型; 边界条件类文件位于文件夹src/finiteVolume/fields/fvPatchFields; 在该文件夹中有如下几个子文件夹:basic, constraint, derived, fvPatchField
- fvPatchField is the (virtual) base class
- basic contains intermediate classes; in particular fixedValue, fixedGradient, zeroGradient, mixed
- derived contains the actual useable classes. cylindricalInletVelocity (derived from fixedValue) looks suitable! 拷贝cylindricalInletVelocity文件到用户文件夹,并重命名为parabolicInletVelocityFvPatchVectorField.C/.H文件
打开上述的.C和.H文件,使用ctrl+H快捷键替换cylindrical 为parabolic; 建立Make文件夹,并新建files及options文件,在files文件中指定源代码以及编译文件的类型为链接库;
parabolicInletVelocityFvPatchVectorField.C
LIB = $(FOAM_USER_LIBBIN)/libnewBC
然后在options文件中指定头文件等路径
EXE_INC = \
-I$(LIB_SRC)/triSurface/lnInclude \
-I$(LIB_SRC)/meshTools/lnInclude \
-I$(LIB_SRC)/finiteVolume/lnInclude
LIB_LIBS = \
-lOpenFOAM \
-ltriSurface \
-lmeshTools \
-lfiniteVolume
在.H文件中,新增private数据成员,如最大速度,中心轴等,注释掉不要的代码,最终部分代码如下:
#ifndef parabolicInletVelocityFvPatchVectorField_H
#define parabolicInletVelocityFvPatchVectorField_H
#include "fixedValueFvPatchFields.H"
#include "Function1.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
/*---------------------------------------------------------------------------*\
Class parabolicInletVelocityFvPatchVectorField Declaration
\*---------------------------------------------------------------------------*/
class parabolicInletVelocityFvPatchVectorField
:
public fixedValueFvPatchVectorField
{
// Private data
//- Origin of the rotation
const scalar maxVelocity_;//added by zhou
const vector centre_;//added by zhou
//- Axis of the rotation
const vector axis_;
const scalar R_;//added by zhou
//- Axial velocity
//autoPtr<Function1<scalar>> axialVelocity_;//by zhou
//- Radial velocity
//autoPtr<Function1<scalar>> radialVelocity_;//by zhou
//- RPM
// autoPtr<Function1<scalar>> rpm_;//by zhou
public:
//- Runtime type information
TypeName("parabolicInletVelocity");
修改.C中的空构造函数(null constructor)
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::parabolicInletVelocityFvPatchVectorField::
parabolicInletVelocityFvPatchVectorField
(
const fvPatch& p,
const DimensionedField<vector, volMesh>& iF
)
:
fixedValueFvPatchField<vector>(p, iF),
maxVelocity_(0),//zhou
centre_(pTraits<vector>::zero),//zhou
axis_(pTraits<vector>::zero),
R_(0)
{}
pTraits是什么类?待解答;修改拷贝构造函数(copy constructor)
Foam::parabolicInletVelocityFvPatchVectorField::
parabolicInletVelocityFvPatchVectorField
(
const parabolicInletVelocityFvPatchVectorField& ptf,
const fvPatch& p,
const DimensionedField<vector, volMesh>& iF,
const fvPatchFieldMapper& mapper
)
:
fixedValueFvPatchField<vector>(ptf, p, iF, mapper),
maxVelocity_(ptf.maxVelocity_),//zhou
centre_(ptf.centre_),//zhou
axis_(ptf.axis_),//zhou
R_(ptf.R_)//zhou
{}
(ptf.centre…等是什么?待解答;)从速度字典文件中读取数据的构造函数
Foam::parabolicInletVelocityFvPatchVectorField::
parabolicInletVelocityFvPatchVectorField
(
const fvPatch& p,
const DimensionedField<vector, volMesh>& iF,
const dictionary& dict
)
:
fixedValueFvPatchField<vector>(p, iF, dict),//zhou
maxVelocity_(readScalar(dict.lookup("maxVelocity"))),//zhou
centre_(dict.lookup("centre")),//zhou
axis_(dict.lookup("axis")),//zhou
R_(readScalar(dict.lookup("radius")))//zhou
{}
修改write out成员函数
void Foam::parabolicInletVelocityFvPatchVectorField::write(Ostream& os) const
{
fvPatchField<vector>::write(os);//zhou
os.writeKeyword("maxVelocity") << maxVelocity_ <<//zhou
token::END_STATEMENT << nl;//zhou
os.writeKeyword("centre") << centre_ << token::END_STATEMENT << nl;//zhou
os.writeKeyword("axis") << axis_ << token::END_STATEMENT << nl;//zhou
os.writeKeyword("radius") << R_ <<//zhou
token::END_STATEMENT << nl;//zhou
writeEntry("value", os);//zhou
}
修改updateCoeffs()成员函数,在这里真正定义边界条件;
void Foam::parabolicInletVelocityFvPatchVectorField::updateCoeffs()
{
if (updated())
{
return;
}
vector hatAxis = axis_/mag(axis_);//zhou
const scalarField r(mag(patch().Cf() - centre_));//zhou
operator==(hatAxis*maxVelocity_*(1.0 - (r*r)/(R_*R_)));//zhou
fixedValueFvPatchField<vector>::updateCoeffs();//zhou
}
wmake libso编译成功后,还需要告诉Openfoam动态链接文件的存在,这可以通过在system/controlDict增加libs (“libnewBC.so”);语句实现,注意libs后面有空格。
FoamFile
{
version 2.0;
format ascii;
class dictionary;
object controlDict;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
libs ("libnewBC.so");
application simpleFoam;
startFrom latestTime;
//…
在Orifice案例(打包文件已上传于百度网盘 提取码: b78s)中应用该边界条件,修改INLET边界条件如下:
INLET
{
type parabolicInletVelocity;
axis (0 0 1);
centre (0 0 0.0215);
maxVelocity 0.02;
radius 0.05;
value (0 0 0);
}
其中边界条件的最终相关文件,已打包上传至百度网盘百度网盘 ,提取码: iqi7 最终运行结果如下图所示