CUDA × OpenGL:格子ボルツマン法(LBM)によるリアルタイム流体シミュレーターの構築

GPUの演算能力を、物理シミュレーションとグラフィックスの融合に活用してみました。 本プロジェクトでは、流体解析の手法である「格子ボルツマン法(LBM: Lattice Boltzmann Method)」をCUDAで実装し、OpenGLを用いてその動態をリアルタイムに可視化するシミュレーターを作成しました。

中心には複雑な干渉を生む「星型」の障害物を配置。マウスドラッグによる滑らかな波紋の生成や、障害物による波の回折・干渉を、512×512のグリッド上で高フレームレートにシミュレートします。物理演算と描画の両方をGPU内で完結させることで、CPUでは到底不可能なリアルタイム性を実現しています。


技術詳細解説

本プログラムは、大きく分けて**「物理演算(LBM)」「パルス注入(入力)」「可視化」**の3つのフェーズで構成されています。

1. 格子ボルツマン法 (D2Q9モデル)

流体を微視的な粒子の集まりとして捉え、その速度分布関数の変化を計算します。

  • D2Q9モデル: 各格子点は9つの速度方向(静止、上下左右、斜め)を持ちます。

  • ストリーミング(移動): 隣接する格子から粒子が移動してくるステップです。本コードでは、境界条件(星型の壁)に衝突した粒子を跳ね返す「Bounce-back法」を実装しています。

  • 衝突(緩和): 粒子同士の衝突により、局所的な平衡状態へ近づくステップです。定数 omega(緩和時間)が流体の粘性を決定します。

2. 滑らかなマウス入力(線形補間パルス)

マウスドラッグの軌跡を滑らかに表現するため、単なる点ではなく「線分」としてエネルギーを注入しています。

  • 線分最短距離計算: 前フレームと現フレームの座標を結ぶ線分から、各格子点への最短距離を計算します。

  • ガウス分布による注入: 距離に応じた重み付け(expf)を行い、密度を加算することで、航跡のような自然な波紋を生成します。

3. GPUダイレクト可視化

演算結果を一度もCPUに戻すことなく、GPU内で直接映像へ変換しています。

  • 物理量のカラーマッピング: 格子点ごとの流速(ux, uy)と密度(rho)の変化を色相に変換。tanhf 関数を用いることで、激しい変化もサチュレーションを起こさず美しく描写します。

  • テクスチャ共有: CUDAで計算した画像バッファをOpenGLのテクスチャとしてバインドし、1枚の板ポリゴンに貼り付けて表示しています。

    This project is a real-time fluid dynamics simulator that implements the Lattice Boltzmann Method (LBM) by leveraging the parallel processing power of the GPU. By tightly integrating CUDA for physical computations and OpenGL for rendering, it achieves exceptionally smooth visualization of fluid behavior on a high-resolution grid.

    Technical Highlights

    • Star-Shaped Obstacle: A gray, star-shaped obstacle is positioned at the center of the domain. Utilizing a mathematical Signed Distance Function (SDF) approach based on polar coordinates, the simulator accurately handles wave diffraction and interference against complex geometries.

    • Linearly Interpolated Drag Input: To ensure seamless wave generation regardless of mouse movement speed, a custom kernel interpolates between previous and current mouse coordinates, injecting energy along the continuous path.

    • Zero-Copy Visualization: Physical quantities are converted directly into pixel colors within GPU memory. These are then rendered as OpenGL textures without being transferred back to the CPU, eliminating data bottlenecks and ensuring high frame rates.

    How It Works

    1. Collision & Streaming: The particle distribution functions at each grid point move to neighboring nodes and collide to approach a local equilibrium state.

    2. Boundary Conditions: Particles hitting the star-shaped wall undergo a “bounce-back” process, creating intricate wave patterns around the object.

    3. Visualization: Changes in local velocity and density are mapped to a vibrant color palette using a tanh activation function to prevent color saturation.

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です