在OpenFOAM中,边界条件的实现采用了面向对象的编程思想,通过继承和多态机制来实现不同类型的边界条件。以下是其继承关系和实现机制的详细说明:
1. 基类体系结构
OpenFOAM的边界条件继承关系主要围绕以下几个核心基类:
(1) fvPatchField<Type>
- 位置:
src/finiteVolume/fields/fvPatchFields/fvPatchField
- 作用: 所有边界条件的抽象基类模板,定义了边界条件的通用接口(如
updateCoeffs
,evaluate
等)。 - 关键派生类:
fixedValueFvPatchField
(固定值)fixedGradientFvPatchField
(固定梯度)mixedFvPatchField
(混合类型)zeroGradientFvPatchField
(零梯度)
(2) basicBoundaryMesh
和 fvBoundaryMesh
- 负责管理边界条件的几何信息(如面单元、邻接关系等)。
2. 继承关系示例
以标量场(scalar
)的固定值边界为例,其继承链如下:
fvPatchField<scalar> // 基类
← fixedValueFvPatchField<scalar> // 固定值边界
← 具体边界类(如`inletOutletFvPatchField`)
典型派生类:
- 固定值:
template<class Type> class fixedValueFvPatchField : public fvPatchField<Type>
- 零梯度:
template<class Type> class zeroGradientFvPatchField : public fvPatchField<Type>
- 自定义边界:
用户可通过继承fvPatchField
或现有派生类实现新边界类型。
3. 关键实现机制
(1) 多态行为
- 通过虚函数(如
updateCoeffs()
、evaluate()
)实现不同边界条件的特定行为。 - 例如:
void fixedValueFvPatchField<Type>::updateCoeffs() { if (updated()) return; fvPatchField<Type>::operator==(fixedValue_); fvPatchField<Type>::updateCoeffs(); }
(2) 工厂模式
- 边界条件的创建通过
dictionary
动态解析,利用宏makePatchField
注册:#define makePatchField(fvPatchTypeField) \ defineTemplateRunTimeSelectionTable(fvPatchTypeField, dictionary);
- 在运行时根据
boundaryField
字典中的type
字段选择具体类。
(3) 模板化设计
- 边界条件支持任意场类型(
scalar
,vector
,tensor
等),通过模板类fvPatchField<Type>
实现。
4. 用户自定义边界条件
步骤:
- 创建派生类:
class myCustomBoundary : public fixedValueFvPatchField<scalar> { // 实现虚函数(如updateCoeffs) };
- 注册宏:
makePatchTypeField(fvPatchScalarField, myCustomBoundary);
- 字典中使用:
boundaryField { inlet { type myCustomBoundary; customParam 42; } }
5. 重要文件路径
- 基类定义:
src/finiteVolume/fields/fvPatchFields/fvPatchField
- 常用边界条件:
src/finiteVolume/fields/fvPatchFields/basic/
(如fixedValue
,zeroGradient
)src/finiteVolume/fields/fvPatchFields/derived/
(如pressureInlet
,turbulentInlet
)
6. 总结
OpenFOAM通过以下方式实现边界条件:
- 继承:从基类
fvPatchField
派生出具体边界类型。 - 多态:虚函数实现不同边界的独特逻辑。
- 工厂模式:运行时动态创建边界对象。
- 模板化:支持多种场类型。
这种设计允许用户灵活扩展新边界条件,同时保持代码的统一性。