吾生有涯 学海无涯
析模有界 知识无界

OpenFOAM|创建新的边界条件

内容纲要
OpenFOAM中包含了一些标准边界类型及边界条件,但是如果遇到一些特殊的边界条件,如与时间、空间或物理量相关的边界条件,则需要另外处理。

如要定义实现以下边界条件:

式中,x0及t0为指定的参数,x范围为0时刻0,t0时刻x0。

常用的处理方式有两种:

  • codedFixedValue

  • uniformFixedValue

1 利用codedFixedValue边界条件实现

利用codeFixedValue是较为方便的一种。

  • codeFixedValue允许使用c++代码片段计算边界值

  • 所有代码片段在运行时加载

  • 在代码片段中,用户可以使用Foam::fvMesh及Foam::Time类实现相应的功能

如测试案例:

cp -r $FOAM_TUTORIALS /incompressible/pisoFoam/RAS/cavity $FOAM_RUN
其文件0/U中边界定义:
movingWall
{
    type            fixedValue;
    value           uniform (1 0 0);
}

换成上面的自定义边界:

movingWall
{
   type codedFixedValue;
   value           uniform (0 0 0);

   redirectType increaseToFixedValue;

   code
   #{
       const scalar t = this->db().time().value();
       const scalar t0 = 5.0;
       const vector x0(1, 0, 0);
       operator==( x0 * ( 1 / (1.0 + exp(-10.0*t/t0 + 5.0) ) ) );
   #};
}

注:采用这种方式的话,只要边界代码发生改变,在计算时都会进行重新编译。

2 uniformFixedValue实现

  • uniformFixedValue边界是fixedValue边界的扩展

  • 其可以设置边界条件为时间及其它物理量的函数

利用命令查看文件uniformFixedValueFvPatchField.H:

cat $FOAM_SRC/finiteVolume/fields/fvPatchFields/derived/uniformFixedValue/uniformFixedValueFvPatchField.H

其文件部分内容如下:

Description
   This boundary condition provides a uniform fixed value condition.

Usage
   table
       Property | Description | Required | Default value
       uniformValue | uniform value| yes |
   endtable

   Example of the boundary condition specification:
   verbatim
   
  {
       type uniformFixedValue;
       uniformValue constant 0.2;
  }
   endverbatim

Note
   The uniformValue entry is a Function1 type, able to describe time
   varying functions. The example above gives the usage for supplying a
   constant value.
关于该文件的说明:
  • uniformValue是一个Function1类型,其允许定义随时间变化的函数

在OpenFOAM的源文件中寻找Function1,可以利用以下命令:

find $FOAM_SRC -name "Function1"

如下图所示,返回了两条路径。

在Function1路径下存放了大量写函数相关的文件夹,这里可以挑选一个与目标函数比较接近的来用,根据前面函数形式,可以挑选Sine。

  • 运行以下命令,将文件拷贝到相应的路径下

cd $FOAM_RUN
mkdir newFunction1
cp -r $FOAM_SRC/OpenFOAM/primitives/functions/Function1/Sine/ $FOAM_RUN/newFunction1/
cd $FOAM_RUN/newFunction1
mv Sine TrainingExp
cd TrainingExp
  • 将TrainingExp路径下的所有Sine文件名替换成TrainingExp

修改后文件夹如下图所示。

注:这里不改文件名其实也可以。

  • 将所有文件中的Sine替换成TrainingExp,可以利用sed命令,也可以手工替换
sed -i "s/Sine/TrainingExp/g" TrainingExp*
下一步开始考虑植入边界条件。

  • 查看文件TrainingExp.h
#ifndef TrainingExp_H
#define TrainingExp_H

#include "Function1.H"
// * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
namespace Function1Types
{

/*------------------------------*
  Class TrainingExp Declaration
*------------------------------*/

template<class Type>
class TrainingExp
:

public Function1
{
   // Private data

       //- Start-time for the sin function
       scalar t0_;

       //- Scalar amplitude of the sin function
       autoPtr> amplitude_;

       //- Frequency of the sin function
       autoPtr> frequency_;

       //- Scaling factor of the sin function
       autoPtr> scale_;

       //- Level to which the sin function is added
       autoPtr> level_;


   // Private Member Functions

       //- Read the coefficients from the given dictionary
       void read(const dictionary& coeffs);

       //- Disallow default bitwise assignment
       void operator=(const TrainingExp&);


public:

   // Runtime type information
   TypeName("trainingExp");


   // Constructors

       //- Construct from entry name and dictionary
       TrainingExp
      (
           const word& entryName,
           const dictionary& dict
      );
       //- Copy constructor
       TrainingExp(const TrainingExp& se);
   //- Destructor
   virtual ~TrainingExp();
   // Member Functions
       //- Return value for time t
       virtual inline Type value(const scalar t) const;
       //- Write in dictionary format
       virtual void writeData(Ostream& os) const;
};

} // End namespace Function1Types
} // End namespace Foam

#include "TrainingExpI.H"
#ifdef NoRepository
   #include "TrainingExp.C"
#endif
#endif
该文件定义了一个类TrainingExp,其包含5个成员变量:
  • t0:起始时间,保留不变

  • amplitude:函数幅值,将其替换成A

  • frequency:数频率,将其替换成B

  • scale:缩放系数,此处没什么用,可以将其去掉

  • level:截距,将其替换成x0

可以利用以下命令来实现文本替换:

sed -i "s/amplitude/A/g" TrainingExp*
sed -i "s/frequency/B/g" TrainingExp*
sed -i "s/level/x0/g" TrainingExp*

注意:

1、建议注释掉TrainingExp.H以及TrainingExp.C文件中所有与变量scale有关的行,否则在后面运行的过程中会报错。若不注释的话,后面在应用过程中需要指定变量scale的值。2、注意语句TypeName("trainingExp");括号中的trainingExp为后面应用时的边界条件类型。

下面需要在TrainingExpI.H文件中实现表达式。

  • 打开文件TrainingExpI.H文件,修改其内容
#include "TrainingExp.H"
#include "mathematicalConstants.H"

// * * * * Member Functions * //

template<class Type>
inline Type Foam:
:Function1Types::TrainingExp::value(const scalar t) const
{
   return
  (t < t0_) ?
       x0_->value(t) * ( 1.0 /
            ( 1.0 + exp(A_->value(t) * t/t0_ + B_->value(t)) ) )
                      : x0_->value(t);
}

注意包含头文件#include "TrainingExp.H"

修改完毕后如下图所示。

文件修改后,需要重新编译。

  • 拷贝文件makeFunction1s.C到当前路径

cp $FOAM_SRC/OpenFOAM/primitives/functions/Function1/makeFunction1s.C $FOAM_RUN/newFunction1/

打开文件makeFunction1s.C,修改代码如下:

#include "TrainingExp.H"
#include "fieldTypes.H"

// * * * * * * * * * * * * * * //

#define makeFunction1s(Type)
   makeFunction1Type(TrainingExp, Type); 

namespace Foam
{
   makeFunction1s(scalar);
   makeFunction1s(vector);
}

注:注意包含头文件#include "TrainingExp.H"

修改完毕后如下图所示。

  • 创建make路径及option和files文件

mkdir $FOAM_RUN/newFunction1/Makecd Maketouch optionstouch files

对于本例来说,option文件中无需任何内容。

在files文件中写入内容:

makeFunction1s.CLIB = $(FOAM_USER_LIBBIN)/libUserFunction1

如下图所示。

  • 进行编译

cd $FOAM_RUN/newFunction1wmake

若编译过程中无任何报错,则可以在新的案例中使用边界条件TrainingExp了。

若一切顺利,会如下图所示出现libUserFunction1.dll字样。

注:在linux下,为libUserFunction1.so

  • 利用前面的测试案例,修改0/U文件
movingWall
{
   type            uniformFixedValue;
   uniformValue
  {
       type        trainingExp;
       t0          5;
       A           -10;
       B           5;
       x0         (1 0 0);
  }
}
  • 修改system/controlDict文件

FoamFile
{
  version     2.0;
  format     ascii;
  class       dictionary;
  location    "system";
  object     controlDict;
}


libs (
 "libUserFunction1.dll"
);


application     pisoFoam;

startFrom       startTime;

startTime       0;

stopAt         endTime;

endTime         2;

deltaT          0.005;

writeControl   timeStep;

writeInterval   100;

purgeWrite      0;

writeFormat     ascii;

writePrecision  6;

writeCompression off;

timeFormat     general;

timePrecision   6;

runTimeModifiable true;
后面就可以进行计算了。
blockMeshpisoFoam

计算2 s时刻压力分布如图所示。


本文在CFDSupport OpenFOAM for windows 1910下运行通过。相关文件:

链接:

https://pan.baidu.com/s/1gvzL42deMzeyJFB9ckt4mw

提取码:xysr

本篇文章来源于微信公众号: CFD之道

赞(1) 打赏
版权声明:未经允许,请勿随意用于商业用途。
文章名称:《OpenFOAM|创建新的边界条件》
文章链接:https://www.topcfd.cn/12655/
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。
分享到

说两句 抢沙发

评论前必须登录!

 

觉得文章有用就打赏一下文章作者吧

非常感谢你的打赏,我们将继续给力更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫

微信扫一扫

登录

找回密码

注册