用EXTENDS实现CFD求解器抽象网格类与结构化网格子类
解读
国内头部研究所/超算中心在招聘CFD方向Fortran研发时,**“面向对象+现代并行”**是必考项。本题表面让写两段类型定义,实则考察四点:
- 是否熟悉Fortran 2003+的EXTENDS TYPE语法与ABSTRACT INTERFACE机制;
- 能否把CFD里“网格”这一核心数据结构的**共性(维度、边界条件、邻居拓扑)与个性(结构化可直接i±1寻址,非结构化需间接列表)**正确剥离;
- 在类型绑定过程(TBP)里是否显式加DEFERRED以形成抽象接口,保证上层算法(通量计算、梯度重构)只依赖抽象类,符合开闭原则;
- 是否记得给子类写FINAL析构函数并处理Coarray/OMP并行的内存可见性,体现超算场景下的工程素养。
面试官常追问:若以后再加非结构网格子类,要不要改抽象类?答案必须“不改”,否则设计失败。
知识点
- EXTENDS(父类型):Fortran 2003引入,单继承,公有/保护成员直接可见,私有成员需通过TBP访问。
- ABSTRACT TYPE & DEFERRED:抽象类型不能实例化,含未实现的过程接口;子类必须OVERRIDE所有延迟过程,否则编译报错。
- PASS与NOPASS:默认PASS把对象自身作为第一实参;若接口需要全局MPI通讯器,可设NOPASS并显式传参。
- GENERIC & OPERATOR:抽象类可定义泛型接口,子类只需实现具体过程,上层代码用**网格%flux(...)**即可多态调用。
- 内存布局与SIMD:结构化网格子类用CONTIGUOUS指针保证x(i,j,k)连续,方便编译器向量化;抽象类不能假设存储顺序,因此不暴露裸指针,只提供下标到线性索引的映射接口。
- 并行一致性:抽象类里声明CODIMENSION[*]的幽灵单元缓冲区,子类负责SYNC IMAGES;面试时要提到EVENT_TYPE做轻量同步,展示对Fortran 2018的掌握。
答案
module grid_abstract
use, intrinsic :: iso_fortran_env, only: real64
implicit none
type, abstract :: AbstractGrid
integer :: ndim = 3 ! 1D/2D/3D
integer :: n_cells(3) = 0 ! 实际活跃单元
integer :: n_ghost = 2 ! 幽灵层厚度
contains
procedure(iface_volume), deferred :: volume
procedure(iface_flux), deferred :: flux
procedure :: total_cells => total_cells_impl ! 非延迟,可共享
generic :: get => volume, flux
end type AbstractGrid
abstract interface
pure function iface_volume(this, i, j, k) result(vol)
import :: AbstractGrid, real64
class(AbstractGrid), intent(in) :: this
integer, intent(in) :: i,j,k
real(real64) :: vol
end function
pure function iface_flux(this, i, j, k, dir) result(f)
import :: AbstractGrid, real64
class(AbstractGrid), intent(in) :: this
integer, intent(in) :: i,j,k,dir
real(real64) :: f(5) ! 假设5方程Euler
end function
end interface
contains
pure integer function total_cells_impl(this)
class(AbstractGrid), intent(in) :: this
total_cells_impl = product(this%n_cells) + &
2*this%n_ghost * sum(this%n_cells(1:this%ndim))
end function
end module grid_abstract
module grid_structured
use grid_abstract, only: AbstractGrid, real64
implicit none
type, extends(AbstractGrid) :: StructuredGrid
real(real64), allocatable :: x(:,:,:), y(:,:,:), z(:,:,:)
real(real64), allocatable :: dx(:,:,:), dy(:,:,:), dz(:,:,:)
contains
procedure :: volume => struct_volume
procedure :: flux => struct_flux
final :: struct_final
end type StructuredGrid
interface StructuredGrid
module procedure new_structured
end interface
contains
function new_structured(ni,nj,nk,ghost) result(grid)
integer, intent(in) :: ni,nj,nk,ghost
type(StructuredGrid) :: grid
grid%ndim = 3; grid%n_cells = [ni,nj,nk]; grid%n_ghost = ghost
allocate(grid%x (ni+2*ghost, nj+2*ghost, nk+2*ghost), &
grid%y (ni+2*ghost, nj+2*ghost, nk+2*ghost), &
grid%z (ni+2*ghost, nj+2*ghost, nk+2*ghost))
! 初始化坐标略
end function new_structured
pure function struct_volume(this, i, j, k) result(vol)
class(StructuredGrid), intent(in) :: this
integer, intent(in) :: i,j,k
vol = this%dx(i,j,k)*this%dy(i,j,k)*this%dz(i,j,k)
end function
pure function struct_flux(this, i, j, k, dir) result(f)
class(StructuredGrid), intent(in) :: this
integer, intent(in) :: i,j,k,dir
real(real64) :: f(5)
! Roe近似Riemann求解器实现略
end function
subroutine struct_final(this)
type(StructuredGrid) :: this
if (allocated(this%x)) deallocate(this%x, this%y, this%z, this%dx, this%dy, this%dz)
end subroutine
end module grid_structured
program demo
use grid_structured
implicit none
class(AbstractGrid), allocatable :: grid
integer :: i,j,k
grid = StructuredGrid(64,64,64,2) ! 多态构造
print *, 'Total cells=', grid%total_cells()
! 上层算法只依赖AbstractGrid接口,无需知道结构化细节
end program demo
关键语句已用注释标出:EXTENDS、DEFERRED、OVERRIDE(隐式)、FINAL、ALLOCATABLE CLASS。编译器若发现StructuredGrid未实现volume或flux,会立刻报错,确保接口契约。
拓展思考
- 非结构网格子类如何设计?
在抽象类里再加一延迟过程neighbor_indices(this, cell, nbList),返回邻居编号列表;结构化子类直接计算i±1,非结构子类查CSR拓扑表,上层通量循环仍用同一接口。 - GPU移植时,抽象类是否保留?
保留。把volume/flux标**#pragma acc routine seq**,结构化子类数据区域用present(x)统一托管;抽象类负责声明device_ptr属性,保证OpenACC与MPIdevice-aware对接。 - 与C++联编:抽象类绑定到ISO_C_BINDING的void*句柄,C++端通过函数指针表调用延迟过程,实现Fortran核心+C++ GUI的跨语言插件架构,国内多款自研CFD平台已采用此方案。