Fortran OOP 学习笔记

学习 Fortran 面向对象编程方法

参考 Fortran 官方站点 及 B站左老师的视频 20210607-现代Fortran编程:拥抱开源Fortran环境,高效使用现代Fortran-

Fortran 语言

  1. Fortran 66/77
    • Fixed-Form
    • go to
    • do … continue
  2. Fortran 90
    • Free-Form
    • module
    • interface
  3. Fortran 95
  4. Fortran 03
    • OOP
    • class
  5. Fortran 08
    • coarray
    • submodule
    • interface to C
    • newunit
  6. Fortran 18
    • interface to C
  7. Fortran 23

style

采用一致的风格可以提高代码的可读性,以下风格源于 Fortran stdlib

  1. 使用 Modern Fortran 标准
    1. 不要使用过时或删除的语言特征,例如 common, pause, entry, goto
    2. 不要使用非标准语法或供应商提供的 intrinsic procedures, 如 real*8, etime()
  2. 文件命名协议
    1. 每个文件只包含一个 program, module, submodule
    2. 文件名与 program, module, submodule 匹配
    3. 如果接口或实现分割成了 submodule,文件名应添加后缀 _implementation
    4. 测试应该添加到 test 路径下并且文件名前添加 test_ 前缀
  3. 缩进和空格
    1. 缩进 4 个空格
    2. 每行长度限制在 80 个字符并且不超过 132 个
    3. 不使用 tab 字符进行缩进
    4. 移除行末的空格
  4. 变量和程序命名
    1. 变量、程序和关键字均采用小写
    2. 优先使用蛇形命名法 snake_case 而非驼峰命名法 CamelCase
    3. 恰当的缩写可以忽略下划线,例如 lin_space
  5. 属性
    1. 始终为形参指定 intent 属性
    2. 不要使用 Dimension 声明数组,因为过于冗长,使用 real, allocatable :: a(:), b(:,:)
    3. optional 属性放在 intent 属性后面
  6. 块命名
    1. end 块时连接 procedure, module, program 名,除非程序在25行以内

其他参考风格

  1. 注释风格 (https://fortran-lang.org/packages/programming/)
  2. Best practices (https://fortran-lang.org/learn/best_practices/)

编译

  1. makefile
  2. cmake
  3. fpm (https://github.com/fortran-lang/fpm)
  4. xmake

代码要求

  1. 代码整洁度,复用性
  2. 资源调用
  3. 具备传参功能
  4. 离散计算功能: 多exe,多dll
  5. 有能力解析配置文件: xml yaml json toml
  6. 具备日志: stdlib_logger
  7. 使用数据存储器: hdf5 netcdf
  8. 数据结构: 数组 链表 二叉树
  9. 面向对象,简化代码架构

开发思路

  1. 测试代码
    • 语法 => 逻辑 => 业务 => 集成 => 测试
  2. 开发效率
    • 科学计算主要时间花费在计算上,多态接口代价相对较小
  3. IO效率
    • IO堵塞
  4. 编译效率
    • 使用动态库、静态库

语法

  • program
    • implicit none
  • subroutine
    • intent
  • function
    • pure, elemental
  • interface
    • to C
    • to lib or dll
    • polymorphic
    • Overloading Operator
  • module
    • container
    • submodule

编程范式

Fortran三种编程范式:结构化、函数式、面向对象编程
Modern Fortran的23种设计模式–面向对象编程与模板示例

  • 结构化编程 Structural Programming
  • 函数式编程 Functional Programming
  • 面向对象编程 Object-Oriented Programming
0%