您的位置:

Flow++:深入理解流体仿真

流体仿真是计算机动画领域的一种重要技术,它涉及流体运动、压力、密度、温度等物理量的数值解算。而 Flow++ 则是一个开源的 Python 库,用于实现各种流体仿真算法,并包含了许多针对常见流体场情况的可配置示例。

一、概述

Flow++ 采用了 TensorFlow2 作为计算框架,由于 TensorFlow2 本身包含自动微分机制,在流体仿真中可以支持实时的反向传播求解,并减少了手动进行梯度计算的繁琐过程。而 Flow++ 采用的是非常具有代表性的格子 Boltzmann 方法进行流体模拟,该方法在分子动力学模拟、统计物理学领域有广泛应用,对于流场模拟的网格划分非常自然。

Flow++ 的核心组件包括 Boltzmann 传递算子、边界条件处理函数、宏观物理量计算以及反向传播求解等。其中,Boltzmann 传递算子由速度坐标和权重系数确定;边界条件处理函数涵盖了全反射、光滑墙面、强制流速等情况;宏观物理量计算则用于输出流体的压力、密度、温度等宏观物理量。

二、流场建模

流体模拟需要针对具体场景建立相应的数学模型。Flow++ 支持的建模方法包括数值行为模型和基于图形模型的方法。数值行为模型基于 Navier-Stokes 方程直接求解,因此适用于实际情况相对明确的场景;而基于图形模型的方法则可以通过快速建模手段实现复杂场景的模拟。

import tensorflow as tf
import flowpp as fp

# 建立单向盘流场模型
model = fp.models.SingleDirectionDisk(
    resolution=(256, 256), delta_t=0.01, viscosity=3e-4, radius=0.45)
tensor_shape = (1,) + model.resolution + (1,)

# 初始化传递算子
collision_operator = fp.operators.BGK(model.velocity_set, 1.0)
propagator = fp.operators.Propagator(model.velocity_set, model.compute_feq, collision_operator)

# 创建 tensorflow 占位符
density = tf.placeholder(tf.float32, shape=tensor_shape)
pressure = tf.placeholder(tf.float32, shape=tensor_shape)
velocity = tf.placeholder(tf.float32, shape=tensor_shape + (2,))

# 宏观量计算
momentum = model.compute_momentum(density, velocity)
tensor_pressure = model.compute_pressure(density, velocity, momentum)
tensor_velocity = model.compute_velocity(density, momentum)

# 定义边界条件
index_x = fp.grid.GridIndex(model.resolution, 0)
boundary = fp.conditions.BoundaryConditionSet(
    model.velocity_set, index_x, model.density_set, tensor_pressure)

三、模拟计算

建立好流场模型后,我们就可以进行模拟计算了。在 Flow++ 中,可以通过 execute 函数来进行多步迭代计算。在每一步迭代中,我们需要根据当前的流体参数,通过 Boltzmann 传递算子计算每个格子上的分布函数,之后应用边界条件和碰撞传播算子,并计算宏观物理量。

import numpy as np

# 初始化变量
density_value = np.ones(tensor_shape)
pressure_value = np.zeros(tensor_shape)
velocity_value = np.zeros(tensor_shape + (2,))

# 迭代计算
for i in range(10):
    f_eq = model.compute_feq(density_value, velocity_value)
    f_plus = propagator.apply(f_eq, velocity_value) + boundary.apply(f_eq, velocity_value, density_value, pressure_value)
    density_value, velocity_value = model.update_density_and_velocity(f_plus)
    pressure_value = model.compute_pressure(density_value, velocity_value)

四、示例应用

Flow++ 包含了多个示例应用,这些示例应用可以帮助用户更好地了解使用 Flow++ 进行流体模拟时的流程。例如,可以通过 Backward facing step 示例模拟不同速度的气体在突然变窄的通道中的流动以及纵横波(Kelvin-Helmholtz instability)的形成;也可以通过 Cylinder merging 示例模拟两圆柱体在不同的 Reynolds 数下相互合并的情况。

以下为 Cylinder merging 示例中的代码演示:

import numpy as np
import tensorflow as tf
import flowpp as fp

model = fp.models.CylinderMerging(
    resolution=(512, 512), delta_t=0.01, viscosity=3e-4,
    radius_a=0.13, radius_b=0.08, dx=0.15, dy=0.15, ra=2.0, rb=1.0)

density = tf.placeholder(tf.float32, shape=model.shape)
pressure = tf.placeholder(tf.float32, shape=model.shape)
velocity = tf.placeholder(tf.float32, shape=model.shape + (2,))

momentum = model.compute_momentum(density, velocity)
tensor_pressure = model.compute_pressure(density, velocity, momentum)
tensor_velocity = model.compute_velocity(density, momentum)

collision_operator = fp.operators.BGK(model.velocity_set, 1.0)
propagator = fp.operators.Propagator(
    model.velocity_set, model.compute_feq, collision_operator)

f_0 = model.compute_feq(density, velocity)
f_plus = propagator.apply(f_0, velocity)
density, velocity = model.update_density_and_velocity(f_plus)

f_b = boundary.apply(f_plus, velocity)
density, velocity = model.update_density_and_velocity(f_b)

五、总结

Flow++ 是一个优秀的开源 Python 库,可以用于实现各种流体仿真算法,支持当前流体仿真领域的多种数学建模方法。其优点在于使用 Tensorflow2 等先进的机器学习框架实现了自动微分、反向传播等计算机动画领域的常见技术,并通过多个示例应用让用户可以更好地了解库的使用方法。