Cloud中植入用户自定义的入射模型
原文地址:https://zhuanlan.zhihu.com/p/75104508
这一节将以KinematicCloud为例子植入用户自定义的入射模型,以供大家作参考。
1. 在lagrangian/intermediate/submodels/Kinematic中植入新的入射模型
a.创建文件夹,注意这里的PrimaryInjectionModel对标InjectionModel,绝大多数的部分不会修改,用户可以自定义自己需要的功能。
mkdir PrimaryInjectionModel
cd PrimaryInjectionModel
b.在PrimaryInjectionModel下仿照InjectionModel创建5个文件:
PrimaryInjectionModel.C
PrimaryInjectionModel.H
PrimaryInjectionModelI.H
PrimaryInjectionModelList.C
PrimaryInjectionModelList.H
具体操作为:
cp ../InjectionModel/InjectionModel/InjectionModel* .
mv InjectionModel.C PrimaryInjectionModel.C
..
c.去除代码中关于runtimeSelect的部分它们包括:
- InjectionModel.H
#include "runTimeSelectionTables.H"
//- Declare runtime constructor selection table
declareRunTimeSelectionTable
(
autoPtr,
InjectionModel,
dictionary,
(
const dictionary& dict,
CloudType& owner,
const word& modelType
),
(dict, owner, modelType)
);
// Selectors
//- Selector with lookup from dictionary
static autoPtr<InjectionModel<CloudType>> New
(
const dictionary& dict,
CloudType& owner
);
//- Selector with name and type
static autoPtr<InjectionModel<CloudType>> New
(
const dictionary& dict,
const word& modelName,
const word& modelType,
CloudType& owner
);
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#define makeInjectionModel(CloudType) \
\
typedef Foam::CloudType::kinematicCloudType kinematicCloudType; \
defineNamedTemplateTypeNameAndDebug \
( \
Foam::InjectionModel<kinematicCloudType>, \
0 \
); \
\
namespace Foam \
{ \
defineTemplateRunTimeSelectionTable \
( \
InjectionModel<kinematicCloudType>, \
dictionary \
); \
}
#define makeInjectionModelType(SS, CloudType) \
\
typedef Foam::CloudType::kinematicCloudType kinematicCloudType; \
defineNamedTemplateTypeNameAndDebug(Foam::SS<kinematicCloudType>, 0); \
\
Foam::InjectionModel<kinematicCloudType>:: \
adddictionaryConstructorToTable<Foam::SS<kinematicCloudType>> \
add##SS##CloudType##kinematicCloudType##ConstructorToTable_;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#ifdef NoRepository
#include "InjectionModel.C"
#endif
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
- InjectionModelList.H
#ifdef NoRepository
#include "InjectionModelList.C"
#endif
d.使用包含模式书写模版,并在.C文件的头文件中去除相应的.H文件
- InjectionModel.H
添加到末尾#end if 前面
#include "InjectionModel.C"
- InjectionModelList.H
添加到末尾#end if 前面
#include "InjectionModelList.C"
- InjectionModel.C
去除 #include "InjectionModel.H"
- InjectionModelList.C
去除 #include "InjectionModelList.H"
e.修改PrimaryInjectionModel文件:
sed -i s/'InjectionModel'/'PrimaryInjectionModel'/g PrimaryInjectionModel.C
...
自此一个不需要runtimeSelect的入射模型已经完成。
f.修改PrimaryInjectionModel和PrimaryInjectionModelList的构造器,不再从dict中读取信息,允许通过外界传入的信息进行初始化
2. 在lagrangian/intermediate/clouds/Templates/KinematicCloud
中使用新植入的模型
- KinematicCloud.H的namespace中使用forward declare
template<class CloudType>
class PrimaryInjectionModelList;
- KinematicCloud.H添加新模型的数据
PrimaryInjectionModelList<KinematicCloud<CloudType>> atomizers_;
- KinematicCloud.H添加新的函数
void setAtomizersSize(const label newSize);
void updateatomizers
(
label order,
scalar SOI,
label nParticle,
vector position,
vector U0,
scalar diameter,
scalar rhoLiq,
scalar duration,
label parceltoinject
);
- KinematicCloud.C中添加头文件(注意为包含模式)
#include "PrimaryInjectionModelList.H"
- KinematicCloud.C中修改构造器
构造器中需要增加新数据的初始化,这里采用PrimaryInjectionModelList默认构造器
atomizers_(*this)
copy constructor中为:
atomizers_(c.atomizers_)
- KinematicCloud.C中添加函数表达式,采用List的方法
template<class CloudType>
void Foam::KinematicCloud<CloudType>::setAtomizersSize(const label newSize)
{
//more delete less extend
atomizers_.setSize(newSize);
}
template<class CloudType>
void Foam::KinematicCloud<CloudType>::updateatomizers
(
label order,
scalar SOI,
label nParticle,
vector position,
vector U0,
scalar diameter,
scalar rhoLiq,
scalar duration,
label parceltoinject
)
{
atomizers_.set
(
order,
new PrimaryInjectionModel<KinematicCloud<CloudType>>
(
*this, //owner use *this to construct
SOI,
nParticle,
position,
U0,
diameter,
rhoLiq,
duration,
parceltoinject
)
);
}
- KinematicCloud.C中EvolveCloud中入射自定义入射模型
injectors_.inject(td);
//inject new model and move its position to current time
atomizers_.inject(td);
至此Cloud中添加新模型完成,其具体思路为:
(1)在求解器中初始化Cloud, atomizers_被初始化为空列表
(2)在求解器中调用setAtomizersSize设置用于自定义的入射器的个数
(3)在求解器中调用updateatomizers,将该时间步内需要入射的入射器的信息传入Cloud中去,并且添加到atomizers_列表中
(4)通过inject方法入射atomizers_
该方法与OpenFOAM自带的到dict中读取入射器的方法最大的不同为,可以在运行过程中自由的入射自己想要的效果,并且可以运行过程中修改,OpenFOAM自带的入射器只初始化一次,读取入射器信息后就无法再修改。

浙公网安备 33010602011771号