Sah04验证部分通过,临时修复部分NEQ inlet解决高阻塞流动。kan99b验证初期。
This commit is contained in:
1 parent
925417abcb
commit
ce492f2794
32 files changed
+3939
-846
No files matched your search
@@ -22,7 +22,13 @@ from typing import Dict, List, Optional
|
||||
from .objects import SimObject
|
||||
from ..cuda.compiler_v2 import obs_layout
|
||||
from ..cuda.obs_layout import ObsLayout
|
||||
from ..lbm.descriptors import FLUID, D2Q9_EX, D2Q9_EY, D3Q19_EX, D3Q19_EY, D3Q19_EZ
|
||||
from ..lbm.descriptors import (
|
||||
D2Q9_EX,
|
||||
D2Q9_EY,
|
||||
D3Q19_EX,
|
||||
D3Q19_EY,
|
||||
D3Q19_EZ,
|
||||
)
|
||||
|
||||
|
||||
class ObjectManager:
|
||||
@@ -34,8 +40,6 @@ class ObjectManager:
|
||||
Device macros ``OBS_*`` in ``config_obs.h`` must match ``obs_layout(dim, count)``.
|
||||
"""
|
||||
|
||||
# -- Construction and registration ---------------------------------------
|
||||
|
||||
def __init__(self, nx: int, ny: int, nz: int, nq: int, cfg=None):
|
||||
self.nx = nx
|
||||
self.ny = ny
|
||||
@@ -51,11 +55,11 @@ class ObjectManager:
|
||||
self._obs_alloc_nbytes: int = 0
|
||||
|
||||
self.action = np.zeros(0, dtype=np.float32)
|
||||
|
||||
self.obs_pinned: Optional[np.ndarray] = None
|
||||
self.obs_force_view: Optional[np.ndarray] = None
|
||||
self.obs_torque_view: Optional[np.ndarray] = None
|
||||
self.obs_sensor_view: Optional[np.ndarray] = None
|
||||
self.sensor_cell_counts = np.zeros(0, dtype=np.int32)
|
||||
self.slot_stride_floats: int = 0
|
||||
self.torque_stride_floats: int = 0
|
||||
self.torque_components: int = 0
|
||||
@@ -65,6 +69,9 @@ class ObjectManager:
|
||||
self.obs_nbytes: int = 0
|
||||
|
||||
self._telemetry_field: Optional[object] = None
|
||||
# Ensure host action buffer is non-empty so CUDA alloc never sees 0 bytes
|
||||
# when bodies.count == 0 (matches _resize_buffers sizing for n=0).
|
||||
self._resize_buffers()
|
||||
|
||||
def add(self, obj: SimObject) -> int:
|
||||
"""Register an object and return its id."""
|
||||
@@ -99,31 +106,27 @@ class ObjectManager:
|
||||
new_action[:copy_n] = self.action[:copy_n]
|
||||
self.action = new_action
|
||||
|
||||
# -- Geometry and flag rasterisation -------------------------------------
|
||||
new_sensor_counts = np.zeros(max(n, 1), dtype=np.int32)
|
||||
if self.sensor_cell_counts.size > 0:
|
||||
copy_n = min(self.sensor_cell_counts.size, new_sensor_counts.size)
|
||||
new_sensor_counts[:copy_n] = self.sensor_cell_counts[:copy_n]
|
||||
self.sensor_cell_counts = new_sensor_counts
|
||||
|
||||
def build_flags(self, base_flags: np.ndarray) -> np.ndarray:
|
||||
"""Merge all object flag masks onto *base_flags* (modified in-place)."""
|
||||
"""Merge object flag masks onto a clean domain base.
|
||||
|
||||
Callers should pass a fresh channel-layout flag array. This keeps object
|
||||
overlays stateless and prevents stale obstacle bits from surviving after
|
||||
geometry edits or future body motion.
|
||||
"""
|
||||
merged = np.array(base_flags, copy=True)
|
||||
for obj in self._objects.values():
|
||||
mask = obj.get_flag_mask(self.nx, self.ny, self.nz)
|
||||
nonzero = mask != 0
|
||||
base_flags[nonzero] = mask[nonzero]
|
||||
return base_flags
|
||||
merged[nonzero] = mask[nonzero]
|
||||
return merged
|
||||
|
||||
def _rest_nonfluid(self, field):
|
||||
"""Set DDF of non-fluid nodes to rest equilibrium w_i * rho0."""
|
||||
field.download_ddf()
|
||||
nq = field.nq
|
||||
w = field._read_lattice_weights()
|
||||
f = field.ddf.reshape(nq, -1)
|
||||
nonfluid = (field.flag & FLUID) == 0
|
||||
for i in range(nq):
|
||||
f[i, nonfluid] = w[i] * field.cfg.rho
|
||||
field.ddf = f.reshape(-1)
|
||||
field.upload_ddf()
|
||||
|
||||
# -- Compact list building -----------------------------------------------
|
||||
|
||||
def build_compact_lists(self):
|
||||
def build_compact_lists(self, domain_flags: np.ndarray | None = None):
|
||||
"""Build cut-link SoA columns and sensor lists.
|
||||
|
||||
Returns:
|
||||
@@ -134,6 +137,7 @@ class ObjectManager:
|
||||
cl_idx, cl_dir, cl_q, cl_bid = [], [], [], []
|
||||
cl_rx, cl_ry, cl_rz, cl_fallback = [], [], [], []
|
||||
s_cells, s_ids = [], []
|
||||
self.sensor_cell_counts.fill(0)
|
||||
|
||||
ez = None
|
||||
if self.cfg and self.cfg.is_d3q19:
|
||||
@@ -148,7 +152,9 @@ class ObjectManager:
|
||||
rx_vals, ry_vals, rz_vals, fallback_vals
|
||||
) = obj.get_curved_list(
|
||||
self.nx, self.ny, self.nq,
|
||||
nz=self.nz, ex=ex, ey=ey, ez=ez)
|
||||
nz=self.nz, ex=ex, ey=ey, ez=ez,
|
||||
domain_flags=domain_flags,
|
||||
)
|
||||
if len(fluid_idx) > 0:
|
||||
cl_idx.append(fluid_idx)
|
||||
cl_dir.append(dirs)
|
||||
@@ -163,6 +169,8 @@ class ObjectManager:
|
||||
if len(cells) > 0:
|
||||
s_cells.append(cells)
|
||||
s_ids.append(ids)
|
||||
if 0 <= obj.obj_id < self.sensor_cell_counts.size:
|
||||
self.sensor_cell_counts[obj.obj_id] = int(len(cells))
|
||||
|
||||
r_idx = np.concatenate(cl_idx) if cl_idx else np.zeros(0, dtype=np.uint32)
|
||||
r_dir = np.concatenate(cl_dir) if cl_dir else np.zeros(0, dtype=np.uint8)
|
||||
@@ -184,20 +192,26 @@ class ObjectManager:
|
||||
sensor_cells, sensor_obj_id,
|
||||
)
|
||||
|
||||
# -- GPU buffers (action + obs telemetry) --------------------------------
|
||||
def sync_to_gpu(self, field, *, rebuild_flags: bool = True):
|
||||
"""Upload compact lists, action buffer, and packed ``obs`` GPU buffer.
|
||||
|
||||
def sync_to_gpu(self, field):
|
||||
"""Upload merged flags, compact lists, action buffer, and packed ``obs`` GPU buffer."""
|
||||
Args:
|
||||
rebuild_flags: Rebuild ``field.flag`` from the clean channel base and
|
||||
upload it before constructing compact lists. Pass ``False`` when
|
||||
the caller has already prepared and uploaded the final flag field
|
||||
for this geometry, such as during initialization.
|
||||
"""
|
||||
self._telemetry_field = field
|
||||
|
||||
field.flag = self.build_flags(field.flag)
|
||||
field.upload_flags()
|
||||
if rebuild_flags:
|
||||
field.flag = self.build_flags(field.build_channel_flags())
|
||||
field.upload_flags()
|
||||
|
||||
(
|
||||
cl_fluid_idx, cl_dir, cl_q, cl_body_id,
|
||||
cl_rx, cl_ry, cl_rz, cl_fallback_class,
|
||||
sensor_cells, sensor_obj_id,
|
||||
) = self.build_compact_lists()
|
||||
) = self.build_compact_lists(domain_flags=field.flag)
|
||||
|
||||
field.curved.assign_host(
|
||||
cl_fluid_idx, cl_dir, cl_q, cl_body_id,
|
||||
@@ -214,8 +228,6 @@ class ObjectManager:
|
||||
self.obs_pinned.fill(0)
|
||||
cuda.memcpy_htod(self.obs_gpu, self.obs_pinned)
|
||||
|
||||
self._rest_nonfluid(field)
|
||||
|
||||
def _allocate_packed_telemetry(self) -> None:
|
||||
"""Resize ``action_gpu`` / ``obs_gpu`` / ``obs_pinned`` when layout changes."""
|
||||
dim = self.cfg.dim if self.cfg else 2
|
||||
@@ -237,7 +249,6 @@ class ObjectManager:
|
||||
self.obs_gpu = None
|
||||
self.obs_gpu = cuda.mem_alloc(self.obs_nbytes)
|
||||
self._obs_alloc_nbytes = self.obs_nbytes
|
||||
# Pagelocked mirror (zeros); PyCUDA exposes pagelocked_empty, not pagelocked_zeros.
|
||||
self.obs_pinned = cuda.pagelocked_empty(lay.total_floats, np.float32)
|
||||
self.obs_pinned.fill(0)
|
||||
n_slots = max(self.count, 1)
|
||||
@@ -274,8 +285,6 @@ class ObjectManager:
|
||||
self.obs_pinned.fill(0)
|
||||
cuda.memcpy_htod(self.obs_gpu, self.obs_pinned)
|
||||
|
||||
# -- B3 async telemetry rhythm -------------------------------------------
|
||||
|
||||
def zero_force_segment_async(self, stream: cuda.Stream):
|
||||
"""Zero body telemetry (force + torque) of ``obs_gpu``."""
|
||||
n_floats = self.sensor0_floats
|
||||
@@ -296,8 +305,6 @@ class ObjectManager:
|
||||
assert self.obs_pinned is not None
|
||||
cuda.memcpy_dtoh_async(self.obs_pinned, self.obs_gpu, stream)
|
||||
|
||||
# -- Public introspection -------------------------------------------------
|
||||
|
||||
@property
|
||||
def obs_n_slots(self) -> int:
|
||||
"""At-least-one slot count ``max(count, 1)`` matching ``config_obs.h``."""
|
||||
@@ -322,18 +329,11 @@ class ObjectManager:
|
||||
"""Return the DIM-vector force on body ``body_id`` from ``obs_pinned``.
|
||||
|
||||
Caller must have synchronised the CUDA stream before reading.
|
||||
|
||||
Args:
|
||||
body_id: Object id assigned by :meth:`add`.
|
||||
|
||||
Returns:
|
||||
Copy of length-``DIM`` float32 values from the force segment.
|
||||
"""
|
||||
self._validate_body_id(body_id)
|
||||
assert self.cfg is not None
|
||||
assert self.obs_pinned is not None
|
||||
d = self.cfg.dim
|
||||
# Force segment starts at float index 0 (``OBS_FORCE0_FLOATS``).
|
||||
i0 = body_id * d
|
||||
return np.array(self.obs_pinned[i0:i0 + d], dtype=np.float32)
|
||||
|
||||
@@ -345,7 +345,24 @@ class ObjectManager:
|
||||
return np.array(
|
||||
self.obs_pinned[i0:i0 + self.torque_components], dtype=np.float32)
|
||||
|
||||
# -- Runtime action state -------------------------------------------------
|
||||
def read_sensor(self, body_id: int, *, normalize: bool = True) -> np.ndarray:
|
||||
"""Return sensor accumulation for ``body_id``.
|
||||
|
||||
By default this returns the area-averaged value over the sensor footprint.
|
||||
Set ``normalize=False`` to get the raw sum accumulated by ``SensorKernel``.
|
||||
"""
|
||||
self._validate_body_id(body_id)
|
||||
assert self.obs_pinned is not None
|
||||
assert self.cfg is not None
|
||||
d = self.cfg.dim
|
||||
i0 = self.sensor0_floats + body_id * d
|
||||
values = np.array(self.obs_pinned[i0:i0 + d], dtype=np.float32)
|
||||
if not normalize:
|
||||
return values
|
||||
count = int(self.sensor_cell_counts[body_id]) if body_id < self.sensor_cell_counts.size else 0
|
||||
if count <= 0:
|
||||
return values
|
||||
return values / np.float32(count)
|
||||
|
||||
def set_body_state(self, body_id: int, omega: float = 0.0) -> None:
|
||||
"""Set runtime body state used by kernels (currently only omega)."""
|
||||
@@ -392,8 +409,6 @@ class ObjectManager:
|
||||
return 0
|
||||
return int(np.count_nonzero(curved.q[:curved.count] < np.float32(threshold)))
|
||||
|
||||
# -- Rigid body kinematics (future) --------------------------------------
|
||||
|
||||
def update_states(self, dt: float):
|
||||
"""Integrate object motion (placeholder)."""
|
||||
pass
|
||||
|
||||
@@ -108,7 +108,8 @@ class Cylinder(SimObject):
|
||||
return mask
|
||||
|
||||
def get_curved_list(self, nx: int, ny: int, nq: int,
|
||||
nz: int = 1, ex=None, ey=None, ez=None):
|
||||
nz: int = 1, ex=None, ey=None, ez=None,
|
||||
domain_flags=None):
|
||||
"""Return per-link curved boundary data (fluid-centric).
|
||||
|
||||
Each record represents one cut link: a fluid node with a lattice
|
||||
@@ -159,15 +160,24 @@ class Cylinder(SimObject):
|
||||
r_sq = r * r
|
||||
use_3d = ez is not None
|
||||
|
||||
def _donor_is_fluid(xd: int, yd: int, zd: int = 0) -> bool:
|
||||
if domain_flags is None:
|
||||
return True
|
||||
if nz > 1:
|
||||
idx = xd + yd * nx + zd * nx * ny
|
||||
else:
|
||||
idx = xd + yd * nx
|
||||
return (int(domain_flags[idx]) & FLUID) != 0
|
||||
|
||||
def _fallback_class(
|
||||
q_val: float, donor_inside: bool, donor_in_domain: bool
|
||||
q_val: float, donor_inside: bool, donor_in_domain: bool, donor_is_fluid: bool
|
||||
) -> np.uint8:
|
||||
"""Classify donor legality for Bouzidi interpolation fallback."""
|
||||
if not (0.0 < q_val <= 1.0):
|
||||
raise ValueError(f"q must satisfy 0 < q <= 1, got {q_val}")
|
||||
if q_val >= 0.5:
|
||||
return FALLBACK_CLASS_BOUZIDI
|
||||
if not donor_in_domain or donor_inside:
|
||||
if (not donor_in_domain) or donor_inside or (not donor_is_fluid):
|
||||
return FALLBACK_CLASS_HALFWAY_BOUNCEBACK
|
||||
return FALLBACK_CLASS_BOUZIDI
|
||||
|
||||
@@ -207,10 +217,12 @@ class Cylinder(SimObject):
|
||||
zff = z - ez[i]
|
||||
donor_in_domain = (0 <= xff < nx and 0 <= yff < ny and 0 <= zff < nz)
|
||||
donor_inside = False
|
||||
donor_is_fluid = False
|
||||
if donor_in_domain:
|
||||
donor_inside = (
|
||||
(xff - xc) ** 2 + (yff - yc) ** 2 + (zff - zc) ** 2
|
||||
) < r_sq
|
||||
donor_is_fluid = _donor_is_fluid(xff, yff, zff)
|
||||
idx_list.append(
|
||||
np.uint32(x + y * nx + z * nx * ny))
|
||||
dir_list.append(np.uint8(i))
|
||||
@@ -219,7 +231,7 @@ class Cylinder(SimObject):
|
||||
ry_list.append(np.float32(hit[1] - yc))
|
||||
rz_list.append(np.float32(hit[2] - zc))
|
||||
fallback_list.append(
|
||||
_fallback_class(q, donor_inside, donor_in_domain))
|
||||
_fallback_class(q, donor_inside, donor_in_domain, donor_is_fluid))
|
||||
else:
|
||||
for x in range(x0, x1 + 1):
|
||||
for y in range(y0, y1 + 1):
|
||||
@@ -246,8 +258,10 @@ class Cylinder(SimObject):
|
||||
yff = y - ey[i]
|
||||
donor_in_domain = (0 <= xff < nx and 0 <= yff < ny)
|
||||
donor_inside = False
|
||||
donor_is_fluid = False
|
||||
if donor_in_domain:
|
||||
donor_inside = ((xff - xc) ** 2 + (yff - yc) ** 2) < r_sq
|
||||
donor_is_fluid = _donor_is_fluid(xff, yff)
|
||||
idx_list.append(np.uint32(x + y * nx))
|
||||
dir_list.append(np.uint8(i))
|
||||
q_list.append(np.float32(q))
|
||||
@@ -255,7 +269,7 @@ class Cylinder(SimObject):
|
||||
ry_list.append(np.float32(hit[1] - yc))
|
||||
rz_list.append(np.float32(0.0))
|
||||
fallback_list.append(
|
||||
_fallback_class(q, donor_inside, donor_in_domain))
|
||||
_fallback_class(q, donor_inside, donor_in_domain, donor_is_fluid))
|
||||
|
||||
n = len(idx_list)
|
||||
if n > 0:
|
||||
@@ -339,4 +353,4 @@ class Sensor(SimObject):
|
||||
cells = np.array(cell_list, dtype=np.uint32)
|
||||
obj_ids = np.full(len(cell_list), self.obj_id, dtype=np.int32)
|
||||
return cells, obj_ids
|
||||
return np.zeros(0, dtype=np.uint32), np.zeros(0, dtype=np.int32)
|
||||
return np.zeros(0, dtype=np.uint32), np.zeros(0, dtype=np.int32)
|
||||
@@ -10,7 +10,7 @@ Python `config.py` 只负责读取和校验,不是配置位置。
|
||||
| 字段 | 类型 | 默认 | 允许值 | 说明 |
|
||||
|------|------|------|--------|------|
|
||||
| `lattice_model` | string | `"D2Q9"` | `D2Q9`, `D3Q19` | 格子模型,决定维度和速度数量 |
|
||||
| `nx` | int | 512 | >0 | x 方向格点数(流向),必须整除 `threads_per_block` |
|
||||
| `nx` | int | 512 | >0 | x 方向格点数(流向),建议整除 `threads_per_block` 以减少尾块浪费 |
|
||||
| `ny` | int | 256 | >0 | y 方向格点数 |
|
||||
| `nz` | int | 1 | >0 | z 方向格点数,D2Q9 时必须为 1 |
|
||||
|
||||
@@ -29,7 +29,7 @@ Python `config.py` 只负责读取和校验,不是配置位置。
|
||||
|------|------|------|--------|------|
|
||||
| `collision` | string | `"SRT"` | `SRT`, `TRT`, `MRT` | 碰撞算子 |
|
||||
| `streaming` | string | `"double_buffer"` | `double_buffer`, `esopull` | 流传输方式 |
|
||||
| `store_precision` | string | `"FP32"` | `FP32`, `FP16S`, `FP16C` | GPU 存储精度,FP16S 可 1.87× 加速 |
|
||||
| `store_precision` | string | `"FP32"` | `FP32`, `FP16S`, `FP16C` | GPU 存储精度。当前运行时已实现 `FP32` 与 `FP16S`,`FP16C` 仍为保留选项 |
|
||||
| `ddf_shifting` | bool | false | | 存储 f−w 而非 f,提升 FP16 精度 |
|
||||
| `les.enabled` | bool | false | | LES Smagorinsky 子格模型 |
|
||||
| `les.cs` | float | 0.16 | | Smagorinsky 常数 |
|
||||
@@ -39,8 +39,8 @@ Python `config.py` 只负责读取和校验,不是配置位置。
|
||||
| `inlet.trt_neq_damp` | float | 0.5 | [0, 1] | TRT 入口 NEQ donor 阻尼;更小更平滑、精度略降 |
|
||||
| `outlet.mode` | string | `"neq_extrap"` | `neq_extrap`, `zero_gradient`, `blended` | 出口条件 |
|
||||
| `outlet.backflow_clamp` | bool | true | | 出口回流钳位 |
|
||||
| `outlet.blend_alpha` | float | 0.7 | | blended 模式混合系数 |
|
||||
| `outlet.srt_neq_damp` | float | 0.5 | [0, 1] | SRT 出口 NEQ donor 阻尼;更小可减轻棋盘噪声 |
|
||||
| `outlet.blend_alpha` | float | 0.7 | | `blended` 下对未知入域方向的混合系数(**所有碰撞模型**共用同一路径) |
|
||||
| `outlet.srt_neq_damp` | float | 0.5 | [0, 1] | 仅当 `outlet.mode` 为 **`neq_extrap`** 且碰撞为 **SRT/TRT** 时:出口全分布 damped NEQ 的阻尼系数。`outlet.mode` 为 **`blended`** 时 SRT/TRT 走混合未知方向分支,不使用本键。MRT + `neq_extrap` 仍为少量方向的 NEQ 外推 |
|
||||
| `y_wall_bc` | string | `"bounce_back"` | `bounce_back`, `free_slip` | y 向上下壁面边界条件(仅壁面相邻流体行生效) |
|
||||
| `omega_guard.min` | float | 0.01 | | ω 下界(LES τ_eff 保护) |
|
||||
| `omega_guard.max` | float | 1.96 | | ω 上界,高 Re 建议 1.90-1.96 |
|
||||
@@ -87,4 +87,4 @@ step/one_step_*.cu → kernel 编排
|
||||
### 稳定性默认窗口(Stability defaults)
|
||||
|
||||
- 默认 `method.omega_guard = {min: 0.01, max: 1.96}`,用于约束碰撞频率与 LES 有效粘度路径。
|
||||
- 高 Re 调参建议将 `omega_guard.max` 保持在 `1.90-1.96` 区间;过高上界会增大接近 `omega -> 2` 的不稳定风险。
|
||||
- 高 Re 调参建议将 `omega_guard.max` 保持在 `1.90-1.96` 区间;过高上界会增大接近 `omega -> 2` 的不稳定风险。
|
||||
@@ -1,16 +1,16 @@
|
||||
# CelerisLab/lbm/__init__.py
|
||||
"""
|
||||
Lattice Boltzmann Method (LBM) module for fluid simulation.
|
||||
Lattice Boltzmann Method module for fluid simulation.
|
||||
"""
|
||||
|
||||
try:
|
||||
from .descriptors import * # noqa: flags, lattice vectors
|
||||
from .field import LBMField
|
||||
from .stepper import LBMStepper
|
||||
from .initializers import add_lamb_oseen, add_taylor_green
|
||||
__all__ = ["LBMField", "LBMStepper", "add_lamb_oseen", "add_taylor_green"]
|
||||
from .initializers import add_vortex
|
||||
|
||||
__all__ = ["LBMField", "LBMStepper", "add_vortex"]
|
||||
except ImportError as e:
|
||||
import warnings
|
||||
warnings.warn(f"LBM module not fully available: {e}", ImportWarning)
|
||||
__all__ = []
|
||||
|
||||
__all__ = []
|
||||
+63
-26
@@ -22,7 +22,7 @@ import pycuda.driver as cuda
|
||||
|
||||
from ..config import LBMConfig
|
||||
from .curved_links import CurvedLinkSoA, SensorSoA
|
||||
from .descriptors import FLUID
|
||||
from .descriptors import FLUID, SOLID, BC_WALL, BC_INLET, BC_OUTLET
|
||||
|
||||
_SUPPORTED_STORE_PRECISIONS = ("FP32", "FP16S")
|
||||
|
||||
@@ -55,7 +55,7 @@ class LBMField:
|
||||
|
||||
# Host arrays (always float32 for convenience)
|
||||
self.ddf = np.zeros(self.n * self.nq, dtype=self.dtype)
|
||||
self.flag = np.ones(self.n, dtype=np.uint16) * FLUID
|
||||
self.flag = self.build_channel_flags()
|
||||
|
||||
# Compact lists (filled by ObjectManager.sync_to_gpu)
|
||||
self.curved = CurvedLinkSoA()
|
||||
@@ -70,9 +70,50 @@ class LBMField:
|
||||
# Snapshot
|
||||
self._ddf_snap: np.ndarray | None = None
|
||||
|
||||
# Host mirror of runtime parameters so partial updates preserve state.
|
||||
self._runtime_params = {
|
||||
"omega": float(cfg.omega),
|
||||
"fx": 0.0,
|
||||
"fy": 0.0,
|
||||
"fz": 0.0,
|
||||
"n_objects": 0,
|
||||
}
|
||||
|
||||
# Upload d_params immediately
|
||||
self._upload_params()
|
||||
|
||||
def build_channel_flags(self) -> np.ndarray:
|
||||
"""Return a fresh host flag array for the base channel domain.
|
||||
|
||||
The returned array contains only the static channel topology:
|
||||
inlet, outlet, y-walls, and interior fluid. Object overlays are applied
|
||||
later by ObjectManager on top of this clean base, which avoids stale
|
||||
obstacle bits when geometry is rebuilt or moved.
|
||||
"""
|
||||
flag = np.ones(self.n, dtype=np.uint16) * FLUID
|
||||
|
||||
if self.cfg.is_d2q9:
|
||||
flag[0:self.nx] = SOLID | BC_WALL
|
||||
flag[(self.ny - 1) * self.nx:self.ny * self.nx] = SOLID | BC_WALL
|
||||
flag[::self.nx] = SOLID | BC_INLET
|
||||
flag[self.nx - 1::self.nx] = SOLID | BC_OUTLET
|
||||
flag[0] = SOLID | BC_WALL
|
||||
flag[self.nx - 1] = SOLID | BC_WALL
|
||||
flag[(self.ny - 1) * self.nx] = SOLID | BC_WALL
|
||||
flag[self.ny * self.nx - 1] = SOLID | BC_WALL
|
||||
return flag
|
||||
|
||||
flag = flag.reshape(self.nz, self.ny, self.nx)
|
||||
flag[:, 0, :] = SOLID | BC_WALL
|
||||
flag[:, self.ny - 1, :] = SOLID | BC_WALL
|
||||
flag[:, :, 0] = SOLID | BC_INLET
|
||||
flag[:, :, self.nx - 1] = SOLID | BC_OUTLET
|
||||
flag[:, 0, 0] = SOLID | BC_WALL
|
||||
flag[:, 0, self.nx - 1] = SOLID | BC_WALL
|
||||
flag[:, self.ny - 1, 0] = SOLID | BC_WALL
|
||||
flag[:, self.ny - 1, self.nx - 1] = SOLID | BC_WALL
|
||||
return flag.reshape(-1)
|
||||
|
||||
@property
|
||||
def n_curved(self) -> int:
|
||||
return self.curved.count
|
||||
@@ -115,20 +156,18 @@ class LBMField:
|
||||
"""Pack LBMParams struct and upload to __constant__ d_params.
|
||||
|
||||
Must match struct LBMParams in core/params.cuh layout:
|
||||
float omega, omega_bulk;
|
||||
float omega;
|
||||
float fx, fy, fz;
|
||||
float rho_ref, u_inlet;
|
||||
uint n_objects;
|
||||
Total: 8 floats + 1 uint = 36 bytes (no alignment padding needed).
|
||||
Total: 5 floats/uint words = 20 bytes.
|
||||
"""
|
||||
cfg = self.cfg
|
||||
fmt = "=ff fff ff I"
|
||||
p = self._runtime_params
|
||||
fmt = "=f fff I"
|
||||
data = struct.pack(
|
||||
fmt,
|
||||
cfg.omega, 0.0, # omega, omega_bulk
|
||||
0.0, 0.0, 0.0, # fx, fy, fz
|
||||
cfg.rho, cfg.velocity, # rho_ref, u_inlet
|
||||
0, # n_objects (updated later)
|
||||
p["omega"],
|
||||
p["fx"], p["fy"], p["fz"],
|
||||
p["n_objects"],
|
||||
)
|
||||
ptr, size = self.module.get_global("d_params")
|
||||
cuda.memcpy_htod(ptr, data)
|
||||
@@ -136,25 +175,23 @@ class LBMField:
|
||||
def update_params(self, **kwargs):
|
||||
"""Re-upload d_params after changing runtime-adjustable values.
|
||||
|
||||
Accepted keys: omega, omega_bulk, fx, fy, fz, rho_ref, u_inlet, n_objects.
|
||||
Accepted keys: omega, fx, fy, fz, n_objects.
|
||||
"""
|
||||
cfg = self.cfg
|
||||
omega = kwargs.get("omega", cfg.omega)
|
||||
omega_bulk = kwargs.get("omega_bulk", 0.0)
|
||||
fx = kwargs.get("fx", 0.0)
|
||||
fy = kwargs.get("fy", 0.0)
|
||||
fz = kwargs.get("fz", 0.0)
|
||||
rho_ref = kwargs.get("rho_ref", cfg.rho)
|
||||
u_inlet = kwargs.get("u_inlet", cfg.velocity)
|
||||
n_objects = kwargs.get("n_objects", 0)
|
||||
self._runtime_params.update({
|
||||
"omega": float(kwargs.get("omega", self._runtime_params["omega"])),
|
||||
"fx": float(kwargs.get("fx", self._runtime_params["fx"])),
|
||||
"fy": float(kwargs.get("fy", self._runtime_params["fy"])),
|
||||
"fz": float(kwargs.get("fz", self._runtime_params["fz"])),
|
||||
"n_objects": int(kwargs.get("n_objects", self._runtime_params["n_objects"])),
|
||||
})
|
||||
|
||||
fmt = "=ff fff ff I"
|
||||
p = self._runtime_params
|
||||
fmt = "=f fff I"
|
||||
data = struct.pack(
|
||||
fmt,
|
||||
omega, omega_bulk,
|
||||
fx, fy, fz,
|
||||
rho_ref, u_inlet,
|
||||
n_objects,
|
||||
p["omega"],
|
||||
p["fx"], p["fy"], p["fz"],
|
||||
p["n_objects"],
|
||||
)
|
||||
ptr, _ = self.module.get_global("d_params")
|
||||
cuda.memcpy_htod(ptr, data)
|
||||
|
||||
@@ -53,8 +53,9 @@ def add_vortex(field, center: Tuple[float, float],
|
||||
|
||||
# Compute current macroscopic from DDF
|
||||
rho_old = np.sum(f, axis=0)
|
||||
ux_old = sum(f[i] * cx[i] for i in range(nq))
|
||||
uy_old = sum(f[i] * cy[i] for i in range(nq))
|
||||
rho_safe = np.where(np.abs(rho_old) > 1e-12, rho_old, 1.0)
|
||||
ux_old = sum(f[i] * cx[i] for i in range(nq)) / rho_safe
|
||||
uy_old = sum(f[i] * cy[i] for i in range(nq)) / rho_safe
|
||||
p_old = rho_old / 3.0
|
||||
|
||||
# Superimpose
|
||||
@@ -126,4 +127,4 @@ def _taylor_vortex(r, theta, radius, strength, nu):
|
||||
* np.exp(-2 * r**2 / radius**2)
|
||||
/ (np.pi**2 * radius**6)
|
||||
)
|
||||
return u_vor, v_vor, p_vor
|
||||
return u_vor, v_vor, p_vor
|
||||
@@ -1,16 +1,24 @@
|
||||
// CelerisLab – boundary/curved_boundary.cuh
|
||||
// Bouzidi linear interpolated bounce-back for curved surfaces.
|
||||
//
|
||||
// Per-link, fluid-centric formulation [Bouzidi 2001, Mei 1999].
|
||||
// Each thread processes one cut link: a fluid node + direction that
|
||||
// crosses the solid boundary. The kernel reads post-collision DDFs
|
||||
// from fi_out (current step) and fi_in (previous step), computes the
|
||||
// reflected population via Bouzidi's scheme, and stores it to fi_out.
|
||||
// Pull-streaming contract used here:
|
||||
// before the main pull step, each cut-link thread computes the reflected
|
||||
// population and writes it into the adjacent solid source node. The pull load
|
||||
// from the neighboring solid node then receives the corrected value in the
|
||||
// same time step. This avoids colliding fluid nodes with garbage populations
|
||||
// pulled from obstacle interiors.
|
||||
//
|
||||
// References:
|
||||
// [Bou01] Bouzidi, Firdaouss & Lallemand, Phys. Fluids 13(11), 2001
|
||||
// [Mei99] Mei, Luo & Shyy, J. Comput. Phys. 155(2), 1999
|
||||
// [Gin08b] Ginzburg, Verhaeghe & d'Humieres, Commun. Comput. Phys. 2008
|
||||
// [Wen15] Wen, Zhang & Fang, Entropy 17(12), 2015 (MEA force)
|
||||
//
|
||||
// Note on TRT:
|
||||
// Plain linear Bouzidi interpolation is kept here because it is simple and
|
||||
// useful for SRT and MRT baselines and for moving simple geometries.
|
||||
// However, it is not a TRT-parametrized curved-wall family. Expect TRT to be
|
||||
// more sensitive here than with CLI, MGLI, or other TRT-consistent schemes.
|
||||
// ============================================================================
|
||||
|
||||
#ifndef CELERIS_BOUNDARY_CURVED_BOUNDARY_CUH
|
||||
@@ -19,78 +27,72 @@
|
||||
constexpr unsigned char CURVED_FALLBACK_BOUZIDI = 0u;
|
||||
constexpr unsigned char CURVED_FALLBACK_HALFWAY = 1u;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// apply_bouzidi_link – single cut link, dimension-agnostic core
|
||||
//
|
||||
// dir : direction FROM fluid TOWARD wall (α)
|
||||
// q : fractional distance from fluid to wall intersection ∈ (0,1]
|
||||
// k_f : linear index of the fluid node
|
||||
// fi_out: post-collision output buffer (current step) — valid for dir α
|
||||
// fi_in : post-collision input buffer (previous step) — used for dir ᾱ
|
||||
// where fi_out[x_f, ᾱ] is garbage (pulled from solid)
|
||||
// Uw,Vw : wall velocity components (Ww added for 3-D overload)
|
||||
// rx,ry : link-arm components from body center to wall-intersection point
|
||||
// fallback_class: 0=Bouzidi interpolation, nonzero=half-way bounce-back fallback
|
||||
// obs : packed telemetry base pointer; force segment starts at OBS_FORCE0_FLOATS (0).
|
||||
// body_id: ObjectManager object index; writes use obs_force_index/obs_torque_index.
|
||||
// ---------------------------------------------------------------------------
|
||||
__device__ __forceinline__ float bouzidi_linear_moving_correction(
|
||||
float q, unsigned char fallback_class, float alpha_ci_dot_uw)
|
||||
{
|
||||
if (fallback_class != CURVED_FALLBACK_BOUZIDI) {
|
||||
// Fallback replaces the q < 1/2 branch when its donor is illegal.
|
||||
// For D2Q9, alpha_i = 3 w_i, so the moving-wall addend is
|
||||
// +2 * alpha_i * (c_i · u_w) in the q < 1/2 branch.
|
||||
return 2.0f * alpha_ci_dot_uw;
|
||||
}
|
||||
if (q < 0.5f) {
|
||||
return 2.0f * alpha_ci_dot_uw;
|
||||
}
|
||||
return (alpha_ci_dot_uw / q);
|
||||
}
|
||||
|
||||
#if DIM == 2
|
||||
__device__ inline void apply_bouzidi_link(
|
||||
unsigned int dir, float q, unsigned long k_f,
|
||||
fpxx* __restrict__ fi_out,
|
||||
const fpxx* __restrict__ fi_in,
|
||||
fpxx* __restrict__ fi,
|
||||
float Uw, float Vw,
|
||||
float rx, float ry,
|
||||
unsigned char fallback_class,
|
||||
float* __restrict__ obs, int body_id)
|
||||
{
|
||||
int dir_opp = opp_dir(dir);
|
||||
const unsigned int dir_opp = (unsigned int)opp_dir((int)dir);
|
||||
|
||||
// Post-collision population heading toward wall (valid in fi_out)
|
||||
float f_toward = load_ddf(fi_out, index_f(k_f, (unsigned int)dir));
|
||||
unsigned int xf, yf;
|
||||
coordinates(k_f, xf, yf);
|
||||
const int xs = (int)xf + d_cx[dir];
|
||||
const int ys = (int)yf + d_cy[dir];
|
||||
const unsigned long k_s = linear_index((unsigned int)xs, (unsigned int)ys);
|
||||
|
||||
const float f_toward = load_ddf(fi, index_f(k_f, dir));
|
||||
|
||||
float f_reflected;
|
||||
if (fallback_class == CURVED_FALLBACK_BOUZIDI) {
|
||||
if (q < 0.5f) {
|
||||
// Fluid-dominant: interpolate with farther fluid neighbor.
|
||||
// Donor validity is guaranteed by host-side fallback_class tagging.
|
||||
unsigned int xf, yf;
|
||||
coordinates(k_f, xf, yf);
|
||||
int xff = (int)xf - d_cx[dir];
|
||||
int yff = (int)yf - d_cy[dir];
|
||||
unsigned long k_ff = linear_index((unsigned int)xff, (unsigned int)yff);
|
||||
float f_toward_ff = load_ddf(fi_out, index_f(k_ff, (unsigned int)dir));
|
||||
const int xff = (int)xf - d_cx[dir];
|
||||
const int yff = (int)yf - d_cy[dir];
|
||||
const unsigned long k_ff = linear_index((unsigned int)xff, (unsigned int)yff);
|
||||
const float f_toward_ff = load_ddf(fi, index_f(k_ff, dir));
|
||||
f_reflected = 2.0f * q * f_toward + (1.0f - 2.0f * q) * f_toward_ff;
|
||||
} else {
|
||||
// Wall-dominant: interpolate with same node's opposite direction
|
||||
// fi_out[k_f, dir_opp] is garbage; use fi_in (previous step)
|
||||
float f_opp_prev = load_ddf(fi_in, index_f(k_f, (unsigned int)dir_opp));
|
||||
const float f_opp_same = load_ddf(fi, index_f(k_f, dir_opp));
|
||||
f_reflected = (1.0f / (2.0f * q)) * f_toward
|
||||
+ (1.0f - 1.0f / (2.0f * q)) * f_opp_prev;
|
||||
+ (1.0f - 1.0f / (2.0f * q)) * f_opp_same;
|
||||
}
|
||||
} else if (fallback_class == CURVED_FALLBACK_HALFWAY) {
|
||||
// Explicit fallback path: half-way moving bounce-back.
|
||||
f_reflected = f_toward;
|
||||
} else {
|
||||
// Host contract guarantees classes {0,1}; fallback to conservative path.
|
||||
f_reflected = f_toward;
|
||||
}
|
||||
|
||||
// Wall velocity correction: +6 w_α (c_α · u_w) [Lallemand & Luo 2003]
|
||||
float ci_dot_uw = (float)d_cx[dir] * Uw + (float)d_cy[dir] * Vw;
|
||||
f_reflected += 6.0f * d_w[dir] * ci_dot_uw;
|
||||
const float ci_dot_uw = (float)d_cx[dir] * Uw + (float)d_cy[dir] * Vw;
|
||||
const float alpha_ci_dot_uw = 3.0f * d_w[dir] * ci_dot_uw;
|
||||
f_reflected += bouzidi_linear_moving_correction(
|
||||
q, fallback_class, alpha_ci_dot_uw);
|
||||
|
||||
// Store corrected population in the opposite direction at the fluid node
|
||||
store_ddf(fi_out, index_f(k_f, (unsigned int)dir_opp), f_reflected);
|
||||
// Write to the solid source node so the subsequent pull step loads the
|
||||
// corrected incoming population into the adjacent fluid node.
|
||||
store_ddf(fi, index_f(k_s, dir_opp), f_reflected);
|
||||
|
||||
// Momentum-exchange force [Mei 1999, Wen 2015]
|
||||
// F_link = c_α · (f_toward + f_reflected)
|
||||
if (obs != nullptr) {
|
||||
float fx = (float)d_cx[dir] * (f_toward + f_reflected);
|
||||
float fy = (float)d_cy[dir] * (f_toward + f_reflected);
|
||||
const float fx = (float)d_cx[dir] * (f_toward + f_reflected);
|
||||
const float fy = (float)d_cy[dir] * (f_toward + f_reflected);
|
||||
atomicAdd(&obs[obs_force_index(body_id, 0)], fx);
|
||||
atomicAdd(&obs[obs_force_index(body_id, 1)], fy);
|
||||
float tz = rx * fy - ry * fx;
|
||||
const float tz = rx * fy - ry * fx;
|
||||
atomicAdd(&obs[obs_torque_index(body_id, 0)], tz);
|
||||
}
|
||||
}
|
||||
@@ -99,56 +101,59 @@ __device__ inline void apply_bouzidi_link(
|
||||
#if DIM == 3
|
||||
__device__ inline void apply_bouzidi_link(
|
||||
unsigned int dir, float q, unsigned long k_f,
|
||||
fpxx* __restrict__ fi_out,
|
||||
const fpxx* __restrict__ fi_in,
|
||||
fpxx* __restrict__ fi,
|
||||
float Uw, float Vw, float Ww,
|
||||
float rx, float ry, float rz,
|
||||
unsigned char fallback_class,
|
||||
float* __restrict__ obs, int body_id)
|
||||
{
|
||||
int dir_opp = opp_dir(dir);
|
||||
const unsigned int dir_opp = (unsigned int)opp_dir((int)dir);
|
||||
|
||||
float f_toward = load_ddf(fi_out, index_f(k_f, (unsigned int)dir));
|
||||
unsigned int xf, yf, zf;
|
||||
coordinates(k_f, xf, yf, zf);
|
||||
const int xs = (int)xf + d_cx[dir];
|
||||
const int ys = (int)yf + d_cy[dir];
|
||||
const int zs = (int)zf + d_cz[dir];
|
||||
const unsigned long k_s = linear_index((unsigned int)xs, (unsigned int)ys, (unsigned int)zs);
|
||||
|
||||
const float f_toward = load_ddf(fi, index_f(k_f, dir));
|
||||
|
||||
float f_reflected;
|
||||
if (fallback_class == CURVED_FALLBACK_BOUZIDI) {
|
||||
if (q < 0.5f) {
|
||||
unsigned int xf, yf, zf;
|
||||
coordinates(k_f, xf, yf, zf);
|
||||
int xff = (int)xf - d_cx[dir];
|
||||
int yff = (int)yf - d_cy[dir];
|
||||
int zff = (int)zf - d_cz[dir];
|
||||
unsigned long k_ff = linear_index((unsigned int)xff, (unsigned int)yff, (unsigned int)zff);
|
||||
float f_toward_ff = load_ddf(fi_out, index_f(k_ff, (unsigned int)dir));
|
||||
const int xff = (int)xf - d_cx[dir];
|
||||
const int yff = (int)yf - d_cy[dir];
|
||||
const int zff = (int)zf - d_cz[dir];
|
||||
const unsigned long k_ff = linear_index((unsigned int)xff, (unsigned int)yff, (unsigned int)zff);
|
||||
const float f_toward_ff = load_ddf(fi, index_f(k_ff, dir));
|
||||
f_reflected = 2.0f * q * f_toward + (1.0f - 2.0f * q) * f_toward_ff;
|
||||
} else {
|
||||
float f_opp_prev = load_ddf(fi_in, index_f(k_f, (unsigned int)dir_opp));
|
||||
const float f_opp_same = load_ddf(fi, index_f(k_f, dir_opp));
|
||||
f_reflected = (1.0f / (2.0f * q)) * f_toward
|
||||
+ (1.0f - 1.0f / (2.0f * q)) * f_opp_prev;
|
||||
+ (1.0f - 1.0f / (2.0f * q)) * f_opp_same;
|
||||
}
|
||||
} else if (fallback_class == CURVED_FALLBACK_HALFWAY) {
|
||||
f_reflected = f_toward;
|
||||
} else {
|
||||
// Host contract guarantees classes {0,1}; fallback to conservative path.
|
||||
f_reflected = f_toward;
|
||||
}
|
||||
|
||||
float ci_dot_uw = (float)d_cx[dir] * Uw + (float)d_cy[dir] * Vw
|
||||
+ (float)d_cz[dir] * Ww;
|
||||
f_reflected += 6.0f * d_w[dir] * ci_dot_uw;
|
||||
const float ci_dot_uw = (float)d_cx[dir] * Uw + (float)d_cy[dir] * Vw
|
||||
+ (float)d_cz[dir] * Ww;
|
||||
const float alpha_ci_dot_uw = 3.0f * d_w[dir] * ci_dot_uw;
|
||||
f_reflected += bouzidi_linear_moving_correction(
|
||||
q, fallback_class, alpha_ci_dot_uw);
|
||||
|
||||
store_ddf(fi_out, index_f(k_f, (unsigned int)dir_opp), f_reflected);
|
||||
store_ddf(fi, index_f(k_s, dir_opp), f_reflected);
|
||||
|
||||
if (obs != nullptr) {
|
||||
float fx = (float)d_cx[dir] * (f_toward + f_reflected);
|
||||
float fy = (float)d_cy[dir] * (f_toward + f_reflected);
|
||||
float fz = (float)d_cz[dir] * (f_toward + f_reflected);
|
||||
const float fx = (float)d_cx[dir] * (f_toward + f_reflected);
|
||||
const float fy = (float)d_cy[dir] * (f_toward + f_reflected);
|
||||
const float fz = (float)d_cz[dir] * (f_toward + f_reflected);
|
||||
atomicAdd(&obs[obs_force_index(body_id, 0)], fx);
|
||||
atomicAdd(&obs[obs_force_index(body_id, 1)], fy);
|
||||
atomicAdd(&obs[obs_force_index(body_id, 2)], fz);
|
||||
float tx = ry * fz - rz * fy;
|
||||
float ty = rz * fx - rx * fz;
|
||||
float tz = rx * fy - ry * fx;
|
||||
const float tx = ry * fz - rz * fy;
|
||||
const float ty = rz * fx - rx * fz;
|
||||
const float tz = rx * fy - ry * fx;
|
||||
atomicAdd(&obs[obs_torque_index(body_id, 0)], tx);
|
||||
atomicAdd(&obs[obs_torque_index(body_id, 1)], ty);
|
||||
atomicAdd(&obs[obs_torque_index(body_id, 2)], tz);
|
||||
|
||||
@@ -44,13 +44,19 @@
|
||||
|
||||
__device__ __forceinline__ float inlet_target_u(float y_coord) {
|
||||
#if INLET_PROFILE == 0
|
||||
// Uniform profile: U0 is the imposed streamwise velocity everywhere on the
|
||||
// inlet fluid band.
|
||||
return U0;
|
||||
#else
|
||||
// Define profile on fluid-node band y in [1, NY-2] and clamp to non-negative
|
||||
// to avoid near-corner backflow injection.
|
||||
// Parabolic profile on the fluid-node band y in [1, NY-2].
|
||||
//
|
||||
// U0 is treated here as the mean streamwise inlet velocity. The returned
|
||||
// peak centerline velocity is 1.5 * U0, matching the discrete Poiseuille
|
||||
// profile used throughout initialization and boundary reconstruction.
|
||||
// Keep this convention aligned with case setup and validation scripts.
|
||||
const float y_clamped = fminf((float)(NY - 2), fmaxf(1.0f, y_coord));
|
||||
const float H = fmaxf((float)(NY - 2), 1.0f);
|
||||
const float eta = (y_clamped - 0.5f) / H; // maps first/last fluid rows near 0/1
|
||||
const float eta = (y_clamped - 0.5f) / H; // first and last fluid rows map near 0 and 1
|
||||
const float shape = fmaxf(0.0f, 4.0f * eta * (1.0f - eta));
|
||||
return U0 * 1.5f * shape;
|
||||
#endif
|
||||
@@ -66,44 +72,88 @@ __device__ __forceinline__ float inlet_target_u(float y_coord) {
|
||||
// f_neb = populations at the interior neighbor (x=1)
|
||||
// y = y-coordinate of the boundary node
|
||||
//
|
||||
// Reconstructs f[1], f[5], f[7] (cx > 0 directions in new ordering)
|
||||
// using: f_bc[i] = f_neb[i] - feq(rho_neb, u_neb)[i] + feq(rho_neb, u_target)[i]
|
||||
// Reconstructs f[1], f[5], f[7] (cx > 0 directions in new ordering).
|
||||
//
|
||||
// Velocity convention:
|
||||
// uniform -> U0 is the imposed inlet velocity
|
||||
// parabolic -> U0 is the mean inlet velocity, so inlet_target_u() returns a
|
||||
// centerline peak of 1.5 * U0
|
||||
//
|
||||
// Reconstruction keeps the Zou-He mass closure but does not copy all three
|
||||
// unknown-direction NEQ parts from the donor.
|
||||
//
|
||||
// Why this split form:
|
||||
// - In narrow high-blockage channels, the donor diagonals f_neb[5], f_neb[7]
|
||||
// are strongly contaminated by near-wall shear and tend to inject a spurious
|
||||
// negative shift into the first interior column. Empirically this is not just
|
||||
// a corner-node artifact: the whole x=1 profile can be biased low when the
|
||||
// channel becomes very narrow.
|
||||
// - Removing NEQ entirely hurts stability, so the streamwise unknown f[1] keeps
|
||||
// donor NEQ information.
|
||||
// - The diagonal unknowns are instead reconstructed from local density and
|
||||
// transverse-velocity constraints. A positivity limiter is applied only if
|
||||
// the local constraints are mutually inconsistent, preferring exact rho and
|
||||
// streamwise flux over exact v_target at that node.
|
||||
// ---------------------------------------------------------------------------
|
||||
__device__ inline void apply_parabolic_inlet(float* __restrict__ f,
|
||||
const float* __restrict__ f_neb,
|
||||
float y_coord)
|
||||
{
|
||||
// Neighbor macros
|
||||
// Donor macros from the first interior fluid column.
|
||||
float rho_neb, u_neb, v_neb;
|
||||
compute_rho_u(f_neb, rho_neb, u_neb, v_neb);
|
||||
|
||||
// Target velocity (parabolic profile)
|
||||
float u_target = inlet_target_u(y_coord);
|
||||
float v_target = 0.0f;
|
||||
// Target inlet velocity.
|
||||
const float u_target = inlet_target_u(y_coord);
|
||||
const float v_target = 0.0f;
|
||||
|
||||
// Zou-He mass balance: compute rho from known populations at x=0.
|
||||
// Known (correctly streamed): f[0],f[2],f[3],f[4],f[6],f[8] (cx<=0)
|
||||
// Unknown (being set): f[1],f[5],f[7] (cx>0)
|
||||
// rho*(1 - u_target) = f[0]+f[3]+f[4] + 2*(f[2]+f[6]+f[8])
|
||||
float rho_in = (f[0] + f[3] + f[4] + 2.0f*(f[2] + f[6] + f[8]))
|
||||
/ (1.0f - u_target);
|
||||
// Zou-He mass closure at the west boundary.
|
||||
// Known (after pull and any wall pre-repair): f[0],f[2],f[3],f[4],f[6],f[8]
|
||||
// Unknown to reconstruct: f[1],f[5],f[7]
|
||||
const float rho_in = (f[0] + f[3] + f[4] + 2.0f * (f[2] + f[6] + f[8]))
|
||||
/ (1.0f - u_target);
|
||||
|
||||
float feq_tar[9], feq_neb[9];
|
||||
compute_feq(rho_in, u_target, v_target, feq_tar);
|
||||
compute_feq(rho_neb, u_neb, v_neb, feq_neb);
|
||||
|
||||
#if COLLISION_MODEL == 1
|
||||
// TRT path: reconstruct unknown incoming populations only (cx>0 at west inlet).
|
||||
const float beta = INLET_TRT_NEQ_DAMP;
|
||||
f[1] = feq_tar[1] + beta * (f_neb[1] - feq_neb[1]);
|
||||
f[5] = feq_tar[5] + beta * (f_neb[5] - feq_neb[5]);
|
||||
f[7] = feq_tar[7] + beta * (f_neb[7] - feq_neb[7]);
|
||||
const float beta_n = INLET_TRT_NEQ_DAMP;
|
||||
#else
|
||||
const float beta = 1.0f;
|
||||
f[1] = feq_tar[1] + beta * (f_neb[1] - feq_neb[1]);
|
||||
f[5] = feq_tar[5] + beta * (f_neb[5] - feq_neb[5]);
|
||||
f[7] = feq_tar[7] + beta * (f_neb[7] - feq_neb[7]);
|
||||
const float beta_n = 1.0f;
|
||||
#endif
|
||||
|
||||
// Keep donor NEQ only for the streamwise incoming population. This retains
|
||||
// the stabilizing normal-flux information without feeding both diagonal
|
||||
// donor modes back into the inlet every step.
|
||||
const float f1_try = feq_tar[1] + beta_n * (f_neb[1] - feq_neb[1]);
|
||||
|
||||
// Known-part density contribution.
|
||||
const float known_sum = f[0] + f[2] + f[3] + f[4] + f[6] + f[8];
|
||||
|
||||
// From uy = (f3 - f4 + f5 - f6 - f7 + f8) / rho, so with v_target = 0:
|
||||
// f5 - f7 = rho*v_target + (f4 - f3) + (f6 - f8)
|
||||
float pair_diff = rho_in * v_target + (f[4] - f[3]) + (f[6] - f[8]);
|
||||
|
||||
// Density fixes f5 + f7 once f1 is chosen:
|
||||
// f5 + f7 = rho - known_sum - f1
|
||||
// To keep both diagonals non-negative we need pair_sum >= |pair_diff|,
|
||||
// hence f1 <= rho - known_sum - |pair_diff|.
|
||||
const float f1_hi = fmaxf(0.0f, rho_in - known_sum - fabsf(pair_diff));
|
||||
const float f1 = fminf(fmaxf(f1_try, 0.0f), f1_hi);
|
||||
|
||||
float pair_sum = rho_in - known_sum - f1;
|
||||
|
||||
// If the local constraints are still inconsistent because of roundoff or an
|
||||
// extremely distorted incoming state, clip the transverse difference rather
|
||||
// than emitting negative diagonal populations.
|
||||
if (fabsf(pair_diff) > pair_sum) {
|
||||
pair_diff = copysignf(pair_sum, pair_diff);
|
||||
}
|
||||
|
||||
f[1] = f1;
|
||||
f[5] = 0.5f * (pair_sum + pair_diff);
|
||||
f[7] = 0.5f * (pair_sum - pair_diff);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -138,10 +188,9 @@ __device__ inline void apply_pressure_outlet(float* __restrict__ f,
|
||||
compute_feq(rho_out, u_neb, v_neb, feq_tar);
|
||||
compute_feq(rho_neb, u_neb, v_neb, feq_neb);
|
||||
|
||||
#if COLLISION_MODEL == 0
|
||||
// SRT path: use full-population damped NEQ reconstruction at outlet to
|
||||
// suppress checkerboard/grid noise from high-frequency donor content.
|
||||
// Reference: high-Re outlet regularization rationale and NEQ decomposition.
|
||||
#if COLLISION_MODEL == 0 || COLLISION_MODEL == 1
|
||||
// SRT and TRT path: use full-population damped NEQ reconstruction at
|
||||
// outlet to suppress checkerboard and boundary-source noise.
|
||||
const float beta = OUTLET_SRT_NEQ_DAMP;
|
||||
#pragma unroll
|
||||
for (int i = 0; i < 9; i++) {
|
||||
@@ -177,41 +226,78 @@ __device__ inline void apply_parabolic_inlet_3d(float* __restrict__ f,
|
||||
const float* __restrict__ f_neb,
|
||||
float y_coord)
|
||||
{
|
||||
// Neighbor macros
|
||||
// Donor macros from the first interior fluid column.
|
||||
float rho_neb, un, vn, wn;
|
||||
compute_rho_u(f_neb, rho_neb, un, vn, wn);
|
||||
|
||||
// Target velocity (parabolic in y, uniform in z)
|
||||
float u_tar = inlet_target_u(y_coord);
|
||||
// Target velocity: parabolic in y, uniform in z.
|
||||
const float u_tar = inlet_target_u(y_coord);
|
||||
const float v_tar = 0.0f;
|
||||
const float w_tar = 0.0f;
|
||||
|
||||
// Zou-He mass balance: compute rho from known populations at x=0.
|
||||
// cx>0 (unknown): i=1,7,9,13,15 cx<0 (known): i=2,8,10,14,16 cx=0 (known): rest
|
||||
// rho*(1 - u_tar) = sum(cx=0 pops) + 2*sum(cx<0 pops)
|
||||
float rho_in = (f[0] + f[3] + f[4] + f[5] + f[6] + f[11] + f[12] + f[17] + f[18]
|
||||
+ 2.0f*(f[2] + f[8] + f[10] + f[14] + f[16]))
|
||||
/ (1.0f - u_tar);
|
||||
// Zou-He mass balance at the west boundary.
|
||||
// Unknown cx>0 populations are i = 1, 7, 9, 13, 15.
|
||||
const float rho_in = (f[0] + f[3] + f[4] + f[5] + f[6] + f[11] + f[12] + f[17] + f[18]
|
||||
+ 2.0f * (f[2] + f[8] + f[10] + f[14] + f[16]))
|
||||
/ (1.0f - u_tar);
|
||||
|
||||
// feq arrays
|
||||
float feq_tar[19], feq_neb[19];
|
||||
compute_feq(rho_in, u_tar, 0.0f, 0.0f, feq_tar);
|
||||
compute_feq(rho_in, u_tar, v_tar, w_tar, feq_tar);
|
||||
compute_feq(rho_neb, un, vn, wn, feq_neb);
|
||||
|
||||
// Reconstruct cx>0 directions: i = 1, 7, 9, 13, 15
|
||||
#if COLLISION_MODEL == 1
|
||||
// TRT path: damped NEQ reconstruction to suppress inlet noise
|
||||
const float beta = INLET_TRT_NEQ_DAMP;
|
||||
f[1] = feq_tar[1] + beta * (f_neb[1] - feq_neb[1]);
|
||||
f[7] = feq_tar[7] + beta * (f_neb[7] - feq_neb[7]);
|
||||
f[9] = feq_tar[9] + beta * (f_neb[9] - feq_neb[9]);
|
||||
f[13] = feq_tar[13] + beta * (f_neb[13] - feq_neb[13]);
|
||||
f[15] = feq_tar[15] + beta * (f_neb[15] - feq_neb[15]);
|
||||
const float beta_n = INLET_TRT_NEQ_DAMP;
|
||||
#else
|
||||
f[1] = f_neb[1] - feq_neb[1] + feq_tar[1];
|
||||
f[7] = f_neb[7] - feq_neb[7] + feq_tar[7];
|
||||
f[9] = f_neb[9] - feq_neb[9] + feq_tar[9];
|
||||
f[13] = f_neb[13] - feq_neb[13] + feq_tar[13];
|
||||
f[15] = f_neb[15] - feq_neb[15] + feq_tar[15];
|
||||
const float beta_n = 1.0f;
|
||||
#endif
|
||||
|
||||
// D3Q19 counterpart of the D2Q9 split strategy:
|
||||
// - keep donor NEQ on the pure streamwise incoming population f[1]
|
||||
// - retain the x-z pair only through its total incoming mass, because the
|
||||
// present channel setup has y-walls but no z-wall BC path
|
||||
// - reconstruct the y-coupled diagonals from local rho/uy constraints to
|
||||
// avoid feeding wall-shear contamination back into the inlet every step
|
||||
const float f1_try = feq_tar[1] + beta_n * (f_neb[1] - feq_neb[1]);
|
||||
const float zsum_try = (feq_tar[9] + feq_tar[15])
|
||||
+ beta_n * ((f_neb[9] - feq_neb[9])
|
||||
+ (f_neb[15] - feq_neb[15]));
|
||||
|
||||
const float known_sum = f[0] + f[2] + f[3] + f[4] + f[5] + f[6]
|
||||
+ f[8] + f[10] + f[11] + f[12] + f[14]
|
||||
+ f[16] + f[17] + f[18];
|
||||
const float rem_total = rho_in - known_sum;
|
||||
|
||||
// uy constraint:
|
||||
// uy = (f3 - f4 + f7 - f8 + f11 - f12 + f14 - f13 + f17 - f18) / rho
|
||||
float y_diff = rho_in * v_tar
|
||||
- (f[3] - f[4] - f[8] + f[11] - f[12] + f[14] + f[17] - f[18]);
|
||||
|
||||
// uz constraint:
|
||||
// uz = (f5 - f6 + f9 - f10 + f11 - f12 + f16 - f15 + f18 - f17) / rho
|
||||
float z_diff = rho_in * w_tar
|
||||
- (f[5] - f[6] - f[10] + f[11] - f[12] + f[16] + f[18] - f[17]);
|
||||
|
||||
// Reserve enough total mass for the y-diagonal pair to satisfy positivity.
|
||||
const float f1_hi = fmaxf(0.0f, rem_total - fabsf(y_diff));
|
||||
const float f1 = fminf(fmaxf(f1_try, 0.0f), f1_hi);
|
||||
|
||||
const float zsum_hi = fmaxf(0.0f, rem_total - f1 - fabsf(y_diff));
|
||||
const float z_sum = fminf(fmaxf(zsum_try, 0.0f), zsum_hi);
|
||||
if (fabsf(z_diff) > z_sum) {
|
||||
z_diff = copysignf(z_sum, z_diff);
|
||||
}
|
||||
|
||||
float y_sum = rem_total - f1 - z_sum;
|
||||
y_sum = fmaxf(y_sum, 0.0f);
|
||||
if (fabsf(y_diff) > y_sum) {
|
||||
y_diff = copysignf(y_sum, y_diff);
|
||||
}
|
||||
|
||||
f[1] = f1;
|
||||
f[9] = 0.5f * (z_sum + z_diff);
|
||||
f[15] = 0.5f * (z_sum - z_diff);
|
||||
f[7] = 0.5f * (y_sum + y_diff);
|
||||
f[13] = 0.5f * (y_sum - y_diff);
|
||||
}
|
||||
|
||||
__device__ inline void apply_pressure_outlet_3d(float* __restrict__ f,
|
||||
@@ -244,7 +330,7 @@ __device__ inline void apply_pressure_outlet_3d(float* __restrict__ f,
|
||||
compute_feq(rho_neb, un, vn, wn, feq_neb);
|
||||
|
||||
// Reconstruct cx<0 directions: i = 2, 8, 10, 14, 16
|
||||
#if COLLISION_MODEL == 0
|
||||
#if COLLISION_MODEL == 0 || COLLISION_MODEL == 1
|
||||
const float beta = OUTLET_SRT_NEQ_DAMP;
|
||||
#pragma unroll
|
||||
for (int i = 0; i < 19; i++) {
|
||||
|
||||
@@ -6,8 +6,8 @@
|
||||
#define NT 256
|
||||
#define MULT_GPU 0
|
||||
|
||||
#define NX 2402
|
||||
#define NY 102
|
||||
#define NX 1351
|
||||
#define NY 601
|
||||
#define NZ 1
|
||||
|
||||
// ---- Lattice model (single source of truth) ----
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
#ifndef CELERIS_CONFIG_METHOD_H
|
||||
#define CELERIS_CONFIG_METHOD_H
|
||||
|
||||
#define COLLISION_MODEL 0
|
||||
#define COLLISION_MODEL 2
|
||||
#define STREAMING_MODEL 0
|
||||
#define STORE_PRECISION 0
|
||||
#define USE_DDF_SHIFTING 0
|
||||
@@ -12,11 +12,11 @@
|
||||
#define LES_CS 0.160000f
|
||||
#define LES_CLOSED_FORM 1
|
||||
|
||||
#define INLET_PROFILE 1
|
||||
#define INLET_PROFILE 0
|
||||
#define OUTLET_MODE 0
|
||||
#define OUTLET_BLEND_ALPHA 0.700f
|
||||
#define OUTLET_BACKFLOW_CLAMP 1
|
||||
#define Y_WALL_BC 0
|
||||
#define Y_WALL_BC 1
|
||||
|
||||
#define OMEGA_COLLISION_MIN 0.01f
|
||||
#define OMEGA_COLLISION_MAX 1.960f
|
||||
|
||||
@@ -4,9 +4,9 @@
|
||||
#define CELERIS_CONFIG_PHYSICS_H
|
||||
|
||||
#define LBtype float
|
||||
#define VIS 0.0125000000
|
||||
#define VIS 0.0056250000
|
||||
#define RHO 1.0
|
||||
#define U0 0.0277777778
|
||||
#define U0 0.03
|
||||
|
||||
#define PI 3.141592653589793238
|
||||
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
// CelerisLab – core/params.cuh
|
||||
// Runtime parameter structures transported via __constant__ memory.
|
||||
// Includes: LBMParams, RigidBodyState2D, RigidBodyControl2D.
|
||||
//
|
||||
// Design note:
|
||||
// Only parameters that are truly consumed by current kernels should live in
|
||||
// LBMParams. Future-facing interface placeholders belong in host-side API
|
||||
// comments or TODOs, not in the active device contract.
|
||||
// ============================================================================
|
||||
|
||||
#ifndef CELERIS_CORE_PARAMS_CUH
|
||||
@@ -9,26 +14,18 @@
|
||||
#include <stdint.h>
|
||||
|
||||
// ============================================================================
|
||||
// LBM runtime parameters (uploaded once per run or on parameter change)
|
||||
// LBM runtime parameters
|
||||
//
|
||||
// Grid topology (NX, NY, NZ, DIM, NQ) is compile-time via config_grid.h.
|
||||
// Collision method & LES params are compile-time via config_method.h.
|
||||
// Only runtime-mutable values live here.
|
||||
// Grid topology and collision family are compile-time via generated headers.
|
||||
// Runtime state is intentionally kept minimal:
|
||||
// - omega : effective relaxation rate used by SRT/TRT/MRT
|
||||
// - fx,fy,fz : external body force density for Guo forcing
|
||||
// - n_objects : packed telemetry contract
|
||||
// ============================================================================
|
||||
struct LBMParams {
|
||||
//--- relaxation (runtime-mutable for LES / adaptive omega) ---
|
||||
float omega; // SRT/TRT主松弛率 w = 1/(3ν + 0.5)
|
||||
float omega_bulk; // MRT bulk 松弛率 (s_e / s_eps)
|
||||
|
||||
//--- external body force (runtime-mutable for forcing protocols) ---
|
||||
float omega;
|
||||
float fx, fy, fz;
|
||||
|
||||
//--- reference quantities (runtime-mutable for ramping) ---
|
||||
float rho_ref; // 参考密度 (通常 1.0)
|
||||
float u_inlet; // 入口参考速度
|
||||
|
||||
//--- diagnostics ---
|
||||
unsigned int n_objects; // 观测对象数量
|
||||
unsigned int n_objects;
|
||||
};
|
||||
|
||||
__constant__ LBMParams d_params;
|
||||
@@ -37,14 +34,14 @@ __constant__ LBMParams d_params;
|
||||
// Rigid-body state / control (2D, per architecture §17.3)
|
||||
// ============================================================================
|
||||
struct RigidBodyState2D {
|
||||
float x, y, theta; // 位姿 (position + orientation)
|
||||
float vx, vy, omega; // 速度 (translational + rotational)
|
||||
float x, y, theta;
|
||||
float vx, vy, omega;
|
||||
};
|
||||
|
||||
struct RigidBodyControl2D {
|
||||
float ax_cmd, ay_cmd; // 目标加速度(或目标速度/位移, 取决于 mode)
|
||||
float alpha_cmd; // 目标角加速度 / 角速度
|
||||
int control_mode; // 0=力控, 1=速度控, 2=位移控
|
||||
float ax_cmd, ay_cmd;
|
||||
float alpha_cmd;
|
||||
int control_mode; // 0=force, 1=velocity, 2=displacement
|
||||
};
|
||||
|
||||
// ============================================================================
|
||||
@@ -53,7 +50,7 @@ struct RigidBodyControl2D {
|
||||
struct HaloPlan {
|
||||
int face_id;
|
||||
int peer_gpu;
|
||||
int level; // AMR level (0 = base)
|
||||
int level;
|
||||
size_t count;
|
||||
int* send_idx;
|
||||
int* recv_idx;
|
||||
|
||||
@@ -15,6 +15,10 @@
|
||||
// m[6] = qy (energy flux y, s₆ = s_q)
|
||||
// m[7] = pxx (stress, s₇ = s_nu = ω = 1/(3ν + 0.5))
|
||||
// m[8] = pxy (stress, s₈ = s_nu)
|
||||
//
|
||||
// Forcing contract:
|
||||
// Fin[i] is the raw Guo source term without the (1 - ω/2) prefactor.
|
||||
// This operator applies the prefactor internally, matching collide_srt().
|
||||
// ============================================================================
|
||||
|
||||
#ifndef CELERIS_OPERATORS_COLLISION_MRT_CUH
|
||||
@@ -22,30 +26,22 @@
|
||||
|
||||
#if LATTICE_MODEL == LATTICE_D2Q9
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// D2Q9 MRT (fully expanded, no loops, no matrix storage)
|
||||
// ---------------------------------------------------------------------------
|
||||
__device__ __forceinline__ void collide_mrt(float* __restrict__ g,
|
||||
float rho, float ux, float uy,
|
||||
const float* __restrict__ Fin,
|
||||
float omega)
|
||||
{
|
||||
// ----- Relaxation rates -----
|
||||
// s_rho = s_jx = s_jy = 0.0 (strictly conserved in collision)
|
||||
// s_e = s_eps = s_q = 1.2 (kinetic transport)
|
||||
// s_nu = omega (viscosity-related)
|
||||
const float s_rho = 0.0f; // conserved moments
|
||||
const float s_rho = 0.0f;
|
||||
const float s_e = 1.2f;
|
||||
const float s_eps = 1.2f;
|
||||
const float s_jx = 0.0f;
|
||||
const float s_q = 1.2f;
|
||||
const float s_jy = 0.0f;
|
||||
const float s_nu = omega;
|
||||
const float c_tau = 1.0f - 0.5f * omega;
|
||||
|
||||
// ----- Pressure from local density -----
|
||||
const float p = rho * (1.0f / 3.0f);
|
||||
|
||||
// ----- Forward transform: m = M * g (new paired ordering) -----
|
||||
float m[9];
|
||||
m[0] = g[0] + g[1] + g[2] + g[3] + g[4] + g[5] + g[6] + g[7] + g[8];
|
||||
m[1] = -4*g[0] - g[1] - g[2] - g[3] - g[4] + 2*g[5] + 2*g[6] + 2*g[7] + 2*g[8];
|
||||
@@ -57,20 +53,18 @@ __device__ __forceinline__ void collide_mrt(float* __restrict__ g,
|
||||
m[7] = g[1] + g[2] - g[3] - g[4];
|
||||
m[8] = g[5] + g[6] - g[7] - g[8];
|
||||
|
||||
// ----- Equilibrium moments -----
|
||||
const float u2 = ux * ux + uy * uy;
|
||||
float meq[9];
|
||||
meq[0] = 3.0f * p; // ρ
|
||||
meq[1] = -6.0f * p + 3.0f * rho * u2; // e
|
||||
meq[2] = 3.0f * p - 3.0f * rho * u2; // ε
|
||||
meq[3] = rho * ux; // jx
|
||||
meq[4] = -rho * ux; // qx
|
||||
meq[5] = rho * uy; // jy
|
||||
meq[6] = -rho * uy; // qy
|
||||
meq[7] = rho * (ux * ux - uy * uy); // pxx
|
||||
meq[8] = rho * ux * uy; // pxy
|
||||
meq[0] = 3.0f * p;
|
||||
meq[1] = -6.0f * p + 3.0f * rho * u2;
|
||||
meq[2] = 3.0f * p - 3.0f * rho * u2;
|
||||
meq[3] = rho * ux;
|
||||
meq[4] = -rho * ux;
|
||||
meq[5] = rho * uy;
|
||||
meq[6] = -rho * uy;
|
||||
meq[7] = rho * (ux * ux - uy * uy);
|
||||
meq[8] = rho * ux * uy;
|
||||
|
||||
// ----- Relaxation: delta_m[i] = s_i * (meq[i] - m[i]) -----
|
||||
float dm[9];
|
||||
dm[0] = s_rho * (meq[0] - m[0]);
|
||||
dm[1] = s_e * (meq[1] - m[1]);
|
||||
@@ -82,7 +76,6 @@ __device__ __forceinline__ void collide_mrt(float* __restrict__ g,
|
||||
dm[7] = s_nu * (meq[7] - m[7]);
|
||||
dm[8] = s_nu * (meq[8] - m[8]);
|
||||
|
||||
// ----- Inverse transform: g += M⁻¹ * dm (new paired ordering) -----
|
||||
g[0] += ( dm[0] - dm[1] + dm[2] ) / 9.0f;
|
||||
g[1] += (4 * dm[0] - dm[1] - 2* dm[2] + 6* dm[3] - 6* dm[4] + 9*dm[7]) / 36.0f;
|
||||
g[2] += (4 * dm[0] - dm[1] - 2* dm[2] - 6* dm[3] + 6* dm[4] + 9*dm[7]) / 36.0f;
|
||||
@@ -93,14 +86,12 @@ __device__ __forceinline__ void collide_mrt(float* __restrict__ g,
|
||||
g[7] += (4 * dm[0] + 2* dm[1] + dm[2] + 6* dm[3] + 3* dm[4] - 6*dm[5] - 3*dm[6] - 9*dm[8]) / 36.0f;
|
||||
g[8] += (4 * dm[0] + 2* dm[1] + dm[2] - 6* dm[3] - 3* dm[4] + 6*dm[5] + 3*dm[6] - 9*dm[8]) / 36.0f;
|
||||
|
||||
// ----- Add forcing (if present) -----
|
||||
#pragma unroll
|
||||
for (int i = 0; i < 9; i++) {
|
||||
g[i] += Fin[i];
|
||||
g[i] += c_tau * Fin[i];
|
||||
}
|
||||
}
|
||||
|
||||
// Convenience wrapper: no external forcing
|
||||
__device__ __forceinline__ void collide_mrt_no_force(float* __restrict__ g,
|
||||
float rho, float ux, float uy,
|
||||
float omega)
|
||||
@@ -111,18 +102,6 @@ __device__ __forceinline__ void collide_mrt_no_force(float* __restrict__ g,
|
||||
|
||||
#elif LATTICE_MODEL == LATTICE_D3Q19
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// D3Q19 MRT (tensor-projected multi-mode relaxation)
|
||||
//
|
||||
// This implementation performs a true multi-relaxation update by splitting
|
||||
// non-equilibrium content into three subspaces:
|
||||
// 1) deviatoric 2nd-order stress modes -> s_nu (shear, controlled by omega)
|
||||
// 2) isotropic 2nd-order stress mode -> s_bulk (bulk, controlled by omega_bulk)
|
||||
// 3) higher-order residual modes -> s_high (kinetic damping)
|
||||
//
|
||||
// It is formulated in Hermite/tensor space, avoiding explicit 19x19 matrices
|
||||
// while still relaxing different mode families at different rates.
|
||||
// ---------------------------------------------------------------------------
|
||||
__device__ __forceinline__ void collide_mrt(float* __restrict__ g,
|
||||
float rho, float ux, float uy, float uz,
|
||||
const float* __restrict__ Fin,
|
||||
@@ -131,23 +110,21 @@ __device__ __forceinline__ void collide_mrt(float* __restrict__ g,
|
||||
float feq[19];
|
||||
compute_feq(rho, ux, uy, uz, feq);
|
||||
|
||||
// Relaxation rates
|
||||
const float s_nu = omega;
|
||||
const float s_bulk = (d_params.omega_bulk > 0.0f) ? d_params.omega_bulk : 1.2f;
|
||||
const float s_bulk = 1.2f;
|
||||
const float s_high = 1.4f;
|
||||
const float c_tau = 1.0f - 0.5f * omega;
|
||||
|
||||
const float one_minus_s_nu = 1.0f - s_nu;
|
||||
const float one_minus_s_bulk = 1.0f - s_bulk;
|
||||
const float one_minus_s_high = 1.0f - s_high;
|
||||
|
||||
// Non-equilibrium populations
|
||||
float neq[19];
|
||||
#pragma unroll
|
||||
for (int i = 0; i < 19; i++) {
|
||||
neq[i] = g[i] - feq[i];
|
||||
}
|
||||
|
||||
// Build non-equilibrium 2nd-order tensor Πneq = Σ (ci ci) (fi-feqi)
|
||||
float pixx = 0.0f, piyy = 0.0f, pizz = 0.0f;
|
||||
float pixy = 0.0f, pixz = 0.0f, piyz = 0.0f;
|
||||
|
||||
@@ -165,7 +142,6 @@ __device__ __forceinline__ void collide_mrt(float* __restrict__ g,
|
||||
piyz += fneq * ci_y * ci_z;
|
||||
}
|
||||
|
||||
// Isotropic / deviatoric split of Πneq
|
||||
const float tr_pi = pixx + piyy + pizz;
|
||||
const float tr_pi_thrd = tr_pi * (1.0f / 3.0f);
|
||||
|
||||
@@ -176,10 +152,7 @@ __device__ __forceinline__ void collide_mrt(float* __restrict__ g,
|
||||
const float dev_xz = pixz;
|
||||
const float dev_yz = piyz;
|
||||
|
||||
// cs^2 = 1/3 => 2*cs^4 = 2/9, inverse = 4.5
|
||||
// Project neq onto second-order Hermite basis and relax mode families.
|
||||
const float proj_pref = 4.5f;
|
||||
const float c_tau = 1.0f - 0.5f * omega;
|
||||
|
||||
#pragma unroll
|
||||
for (int i = 0; i < 19; i++) {
|
||||
@@ -187,7 +160,6 @@ __device__ __forceinline__ void collide_mrt(float* __restrict__ g,
|
||||
const float ci_y = (float)d_cy[i];
|
||||
const float ci_z = (float)d_cz[i];
|
||||
|
||||
// Q = ci ci - cs^2 I
|
||||
const float q_xx = ci_x * ci_x - CS2;
|
||||
const float q_yy = ci_y * ci_y - CS2;
|
||||
const float q_zz = ci_z * ci_z - CS2;
|
||||
@@ -195,12 +167,11 @@ __device__ __forceinline__ void collide_mrt(float* __restrict__ g,
|
||||
const float q_xz = ci_x * ci_z;
|
||||
const float q_yz = ci_y * ci_z;
|
||||
|
||||
// Contractions Q:Πdev and Q:Πiso
|
||||
const float q_dot_dev =
|
||||
q_xx * dev_xx + q_yy * dev_yy + q_zz * dev_zz
|
||||
+ 2.0f * (q_xy * dev_xy + q_xz * dev_xz + q_yz * dev_yz);
|
||||
|
||||
const float q_trace = q_xx + q_yy + q_zz; // = |ci|^2 - 1
|
||||
const float q_trace = q_xx + q_yy + q_zz;
|
||||
const float q_dot_iso = q_trace * tr_pi_thrd;
|
||||
|
||||
const float neq_dev = d_w[i] * proj_pref * q_dot_dev;
|
||||
|
||||
@@ -8,13 +8,15 @@
|
||||
// Paired direction ordering makes TRT natural:
|
||||
// f_s[i] = 0.5*(f[i] + f[opp(i)]) symmetric
|
||||
// f_a[i] = 0.5*(f[i] - f[opp(i)]) antisymmetric
|
||||
//
|
||||
// Forcing contract:
|
||||
// Fin[i] is the raw Guo source term without the (1 - ω/2) prefactor.
|
||||
// This operator applies the prefactor internally, matching collide_srt().
|
||||
// ============================================================================
|
||||
|
||||
#ifndef CELERIS_OPERATORS_COLLISION_TRT_CUH
|
||||
#define CELERIS_OPERATORS_COLLISION_TRT_CUH
|
||||
|
||||
// Magic parameter Λ for TRT wall/collision coupling.
|
||||
// Default keeps previous behavior; can be overridden in macros.h / tests.
|
||||
#ifndef TRT_MAGIC_PARAM
|
||||
#define TRT_MAGIC_PARAM (0.1875f)
|
||||
#endif
|
||||
@@ -30,34 +32,28 @@ __device__ __forceinline__ void collide_trt(float* __restrict__ f,
|
||||
{
|
||||
const float wp = omega;
|
||||
const float wm = compute_omega_minus(wp);
|
||||
const float c_tau = 1.0f - 0.5f * wp;
|
||||
|
||||
// Direction 0: rest particle – treat as symmetric-only
|
||||
f[0] = f[0] - wp * (f[0] - feq[0]) + Fin[0];
|
||||
f[0] = f[0] - wp * (f[0] - feq[0]) + c_tau * Fin[0];
|
||||
|
||||
// Direction pairs (i, i+1) for i = 1, 3, 5, …
|
||||
#pragma unroll
|
||||
for (int i = 1; i < NQ; i += 2) {
|
||||
const int ib = i + 1; // opposite (paired layout)
|
||||
const int ib = i + 1;
|
||||
|
||||
// Current and opposite
|
||||
const float fi = f[i];
|
||||
const float fb = f[ib];
|
||||
|
||||
// Equilibrium
|
||||
const float feqi = feq[i];
|
||||
const float feqb = feq[ib];
|
||||
|
||||
// Symmetric / antisymmetric non-equilibrium
|
||||
const float delta_s = 0.5f * ((fi - feqi) + (fb - feqb));
|
||||
const float delta_a = 0.5f * ((fi - feqi) - (fb - feqb));
|
||||
|
||||
// Relax + forcing
|
||||
f[i] = fi - wp * delta_s - wm * delta_a + Fin[i];
|
||||
f[ib] = fb - wp * delta_s + wm * delta_a + Fin[ib];
|
||||
f[i] = fi - wp * delta_s - wm * delta_a + c_tau * Fin[i];
|
||||
f[ib] = fb - wp * delta_s + wm * delta_a + c_tau * Fin[ib];
|
||||
}
|
||||
}
|
||||
|
||||
// Variant without forcing (Fin = 0)
|
||||
__device__ __forceinline__ void collide_trt_no_force(float* __restrict__ f,
|
||||
const float* __restrict__ feq,
|
||||
float omega)
|
||||
|
||||
@@ -5,7 +5,6 @@
|
||||
#ifndef CELERIS_OPERATORS_HELPERS_CUH
|
||||
#define CELERIS_OPERATORS_HELPERS_CUH
|
||||
|
||||
// ---- Bounce-back: swap paired directions in-register ----
|
||||
__device__ __forceinline__ void bounce_back_swap(float* f) {
|
||||
#pragma unroll
|
||||
for (int i = 1; i < NQ; i += 2) {
|
||||
@@ -13,7 +12,6 @@ __device__ __forceinline__ void bounce_back_swap(float* f) {
|
||||
}
|
||||
}
|
||||
|
||||
// ---- Collision dispatch (selects SRT/TRT/MRT + optional LES) ----
|
||||
__device__ __forceinline__ void collide_dispatch(
|
||||
float* f, float rho_n,
|
||||
#if DIM == 2
|
||||
@@ -23,13 +21,35 @@ __device__ __forceinline__ void collide_dispatch(
|
||||
#endif
|
||||
) {
|
||||
float feq[NQ], Fin[NQ];
|
||||
#if DIM == 2
|
||||
compute_feq(rho_n, ux, uy, feq);
|
||||
#elif DIM == 3
|
||||
compute_feq(rho_n, ux, uy, uz, feq);
|
||||
#endif
|
||||
zero_forcing(Fin);
|
||||
float omega_col = d_params.omega;
|
||||
|
||||
#if DIM == 2
|
||||
const float fx = d_params.fx;
|
||||
const float fy = d_params.fy;
|
||||
if (fx != 0.0f || fy != 0.0f) {
|
||||
apply_guo_velocity_correction(ux, uy, fx, fy, rho_n);
|
||||
}
|
||||
compute_feq(rho_n, ux, uy, feq);
|
||||
if (fx != 0.0f || fy != 0.0f) {
|
||||
compute_guo_forcing(ux, uy, fx, fy, Fin);
|
||||
} else {
|
||||
zero_forcing(Fin);
|
||||
}
|
||||
#elif DIM == 3
|
||||
const float fx = d_params.fx;
|
||||
const float fy = d_params.fy;
|
||||
const float fz = d_params.fz;
|
||||
if (fx != 0.0f || fy != 0.0f || fz != 0.0f) {
|
||||
apply_guo_velocity_correction(ux, uy, uz, fx, fy, fz, rho_n);
|
||||
}
|
||||
compute_feq(rho_n, ux, uy, uz, feq);
|
||||
if (fx != 0.0f || fy != 0.0f || fz != 0.0f) {
|
||||
compute_guo_forcing(ux, uy, uz, fx, fy, fz, Fin);
|
||||
} else {
|
||||
zero_forcing(Fin);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if USE_LES
|
||||
omega_col = compute_omega_smag(f, feq, rho_n, omega_col);
|
||||
#endif
|
||||
|
||||
@@ -6,17 +6,16 @@
|
||||
// CurvedBoundaryKernel: per-link geometry is passed as cl_rx/y/z and runtime
|
||||
// angular state is read from action[body_id * OBS_BODY_SLOT_FLOATS + ...].
|
||||
// For DIM==2, wall velocity uses rigid-body rotation: Uw=-omega*ry, Vw=omega*rx.
|
||||
//
|
||||
// Important timing:
|
||||
// CurvedBoundaryKernel runs BEFORE the main pull step. It writes corrected
|
||||
// source populations into obstacle nodes so the pull loader receives valid
|
||||
// incoming distributions on the same step.
|
||||
// ============================================================================
|
||||
|
||||
#ifndef CELERIS_STEP_AUX_KERNELS_CU
|
||||
#define CELERIS_STEP_AUX_KERNELS_CU
|
||||
|
||||
// ============================================================================
|
||||
// CurvedBoundaryKernel — per-link Bouzidi curved BC (n_curved threads)
|
||||
//
|
||||
// One thread per cut link. Reads from fi_out (current step, post-collision)
|
||||
// and fi_in (previous step) to compute Bouzidi linear interpolation.
|
||||
// ============================================================================
|
||||
__device__ __forceinline__ float action_omega(const float* action, int body_id)
|
||||
{
|
||||
if (action == nullptr || body_id < 0) return 0.0f;
|
||||
@@ -25,8 +24,7 @@ __device__ __forceinline__ float action_omega(const float* action, int body_id)
|
||||
}
|
||||
|
||||
__global__ void CurvedBoundaryKernel(
|
||||
fpxx* fi_out,
|
||||
const fpxx* fi_in,
|
||||
fpxx* fi,
|
||||
const unsigned int* cl_fluid_idx,
|
||||
const unsigned char* cl_dir,
|
||||
const float* cl_q,
|
||||
@@ -42,34 +40,30 @@ __global__ void CurvedBoundaryKernel(
|
||||
unsigned int tid = threadIdx.x + blockIdx.x * blockDim.x;
|
||||
if (tid >= n_curved) return;
|
||||
|
||||
unsigned long k_f = (unsigned long)cl_fluid_idx[tid];
|
||||
unsigned int dir = (unsigned int)cl_dir[tid];
|
||||
float q = cl_q[tid];
|
||||
int bid = cl_body_id[tid];
|
||||
float rx = cl_rx[tid];
|
||||
float ry = cl_ry[tid];
|
||||
unsigned char fallback_class = cl_fallback_class[tid];
|
||||
float omega = action_omega(action, bid);
|
||||
const unsigned long k_f = (unsigned long)cl_fluid_idx[tid];
|
||||
const unsigned int dir = (unsigned int)cl_dir[tid];
|
||||
const float q = cl_q[tid];
|
||||
const int bid = cl_body_id[tid];
|
||||
const float rx = cl_rx[tid];
|
||||
const float ry = cl_ry[tid];
|
||||
const unsigned char fallback_class = cl_fallback_class[tid];
|
||||
const float omega = action_omega(action, bid);
|
||||
|
||||
#if DIM == 2
|
||||
float Uw = -omega * ry;
|
||||
float Vw = omega * rx;
|
||||
const float Uw = -omega * ry;
|
||||
const float Vw = omega * rx;
|
||||
(void)cl_rz;
|
||||
apply_bouzidi_link(dir, q, k_f, fi_out, fi_in, Uw, Vw, rx, ry, fallback_class, obs, bid);
|
||||
apply_bouzidi_link(dir, q, k_f, fi, Uw, Vw, rx, ry, fallback_class, obs, bid);
|
||||
#elif DIM == 3
|
||||
float rz = cl_rz[tid];
|
||||
// Placeholder 3D angular contract: read scalar omega_z from action tail.
|
||||
float Uw = -omega * ry;
|
||||
float Vw = omega * rx;
|
||||
float Ww = 0.0f;
|
||||
apply_bouzidi_link(dir, q, k_f, fi_out, fi_in, Uw, Vw, Ww,
|
||||
const float rz = cl_rz[tid];
|
||||
const float Uw = -omega * ry;
|
||||
const float Vw = omega * rx;
|
||||
const float Ww = 0.0f;
|
||||
apply_bouzidi_link(dir, q, k_f, fi, Uw, Vw, Ww,
|
||||
rx, ry, rz, fallback_class, obs, bid);
|
||||
#endif
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// SensorKernel — compact-list sensor readout (n_sensor threads)
|
||||
// ============================================================================
|
||||
__global__ void SensorKernel(
|
||||
const fpxx* fi,
|
||||
const unsigned short* flag,
|
||||
|
||||
@@ -33,14 +33,28 @@ __device__ __forceinline__ void write_rest_equilibrium(
|
||||
}
|
||||
}
|
||||
|
||||
__device__ __forceinline__ uint16_t classify_boundary(
|
||||
__device__ __forceinline__ uint16_t channel_flag_from_coords(
|
||||
unsigned int x, unsigned int y)
|
||||
{
|
||||
uint16_t fl = FLAG_SOLID;
|
||||
if (x == 0) fl |= FLAG_BC_INLET;
|
||||
else if (x == (unsigned int)(NX - 1)) fl |= FLAG_BC_OUTLET;
|
||||
else fl |= FLAG_BC_WALL;
|
||||
return fl;
|
||||
if (y == 0u || y == (unsigned int)(NY - 1))
|
||||
return (uint16_t)(FLAG_SOLID | FLAG_BC_WALL);
|
||||
if (x == 0u)
|
||||
return (uint16_t)(FLAG_SOLID | FLAG_BC_INLET);
|
||||
if (x == (unsigned int)(NX - 1))
|
||||
return (uint16_t)(FLAG_SOLID | FLAG_BC_OUTLET);
|
||||
return FLAG_FLUID;
|
||||
}
|
||||
|
||||
__device__ __forceinline__ uint16_t finalize_domain_flag(
|
||||
uint16_t fl, unsigned int x, unsigned int y)
|
||||
{
|
||||
if (is_obstacle(fl))
|
||||
return fl;
|
||||
|
||||
const uint16_t base = channel_flag_from_coords(x, y);
|
||||
if (is_sensor(fl) && base == FLAG_FLUID)
|
||||
return (uint16_t)(base | FLAG_SENSOR);
|
||||
return base;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
@@ -53,12 +67,11 @@ __global__ void InitTubeFlow_v2(uint16_t* flag, fpxx* fi)
|
||||
index_from_thread(x, y, k);
|
||||
if (x >= (unsigned int)NX || y >= (unsigned int)NY) return;
|
||||
|
||||
if (y == 0 || y == NY - 1 || x == 0 || x == NX - 1) {
|
||||
flag[k] = classify_boundary(x, y);
|
||||
for (int i = 0; i < NQ; i++)
|
||||
store_ddf(fi, index_f(k, (unsigned int)i), 0.0f);
|
||||
const uint16_t fl = finalize_domain_flag(flag[k], x, y);
|
||||
flag[k] = fl;
|
||||
if (is_solid(fl)) {
|
||||
write_rest_equilibrium(fi, k);
|
||||
} else {
|
||||
flag[k] = FLAG_FLUID;
|
||||
write_equilibrium(fi, k, inlet_target_u((float)y));
|
||||
}
|
||||
#elif DIM == 3
|
||||
@@ -66,12 +79,11 @@ __global__ void InitTubeFlow_v2(uint16_t* flag, fpxx* fi)
|
||||
index_from_thread(x, y, z, k);
|
||||
if (x >= (unsigned int)NX || y >= (unsigned int)NY || z >= (unsigned int)NZ) return;
|
||||
|
||||
if (y == 0 || y == NY - 1 || x == 0 || x == NX - 1) {
|
||||
flag[k] = classify_boundary(x, y);
|
||||
for (int i = 0; i < NQ; i++)
|
||||
store_ddf(fi, index_f(k, (unsigned int)i), 0.0f);
|
||||
const uint16_t fl = finalize_domain_flag(flag[k], x, y);
|
||||
flag[k] = fl;
|
||||
if (is_solid(fl)) {
|
||||
write_rest_equilibrium(fi, k);
|
||||
} else {
|
||||
flag[k] = FLAG_FLUID;
|
||||
write_equilibrium(fi, k, inlet_target_u((float)y));
|
||||
}
|
||||
#endif
|
||||
@@ -87,11 +99,11 @@ __global__ void InitEsoPull(uint16_t* flag, fpxx* fi)
|
||||
index_from_thread(x, y, k);
|
||||
if (x >= (unsigned int)NX || y >= (unsigned int)NY) return;
|
||||
|
||||
if (y == 0 || y == NY - 1 || x == 0 || x == NX - 1) {
|
||||
flag[k] = classify_boundary(x, y);
|
||||
const uint16_t fl = finalize_domain_flag(flag[k], x, y);
|
||||
flag[k] = fl;
|
||||
if (is_solid(fl)) {
|
||||
write_rest_equilibrium(fi, k);
|
||||
} else {
|
||||
flag[k] = FLAG_FLUID;
|
||||
write_equilibrium(fi, k, inlet_target_u((float)y));
|
||||
}
|
||||
#elif DIM == 3
|
||||
@@ -99,14 +111,14 @@ __global__ void InitEsoPull(uint16_t* flag, fpxx* fi)
|
||||
index_from_thread(x, y, z, k);
|
||||
if (x >= (unsigned int)NX || y >= (unsigned int)NY || z >= (unsigned int)NZ) return;
|
||||
|
||||
if (y == 0 || y == NY - 1 || x == 0 || x == NX - 1) {
|
||||
flag[k] = classify_boundary(x, y);
|
||||
const uint16_t fl = finalize_domain_flag(flag[k], x, y);
|
||||
flag[k] = fl;
|
||||
if (is_solid(fl)) {
|
||||
write_rest_equilibrium(fi, k);
|
||||
} else {
|
||||
flag[k] = FLAG_FLUID;
|
||||
write_equilibrium(fi, k, inlet_target_u((float)y));
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif // CELERIS_STEP_INIT_FLOW_CU
|
||||
#endif // CELERIS_STEP_INIT_FLOW_CU
|
||||
@@ -16,7 +16,7 @@ __device__ __forceinline__ void apply_boundary_pull(
|
||||
float* f, uint16_t fl, unsigned int x, unsigned int y,
|
||||
const fpxx* fi_in, unsigned long k)
|
||||
{
|
||||
// Curved-BC nodes are handled by CurvedBoundaryKernel after the step.
|
||||
// Curved-BC nodes are handled by CurvedBoundaryKernel before OneStep (see stepper).
|
||||
if (is_curved(fl)) return;
|
||||
|
||||
bool interior_y = (y > 0u) && (y < (unsigned int)(NY - 1));
|
||||
@@ -46,7 +46,7 @@ __device__ __forceinline__ void apply_boundary_pull_3d(
|
||||
float* f, uint16_t fl, unsigned int x, unsigned int y, unsigned int z,
|
||||
const fpxx* fi_in, unsigned long k)
|
||||
{
|
||||
// Curved-BC nodes are handled by CurvedBoundaryKernel after the step.
|
||||
// Curved-BC nodes are handled by CurvedBoundaryKernel before OneStep (see stepper).
|
||||
if (is_curved(fl)) return;
|
||||
|
||||
bool interior_y = (y > 0u) && (y < (unsigned int)(NY - 1));
|
||||
|
||||
@@ -22,7 +22,8 @@ __device__ __forceinline__ void apply_boundary_esopull(
|
||||
float* f, uint16_t fl, unsigned int x, unsigned int y,
|
||||
const fpxx* fi, unsigned long t)
|
||||
{
|
||||
// Curved-BC nodes are handled by CurvedBoundaryKernel after the step.
|
||||
// Curved-BC nodes are handled by CurvedBoundaryKernel before the main step when
|
||||
// streaming is double_buffer; EsoPull skips curved (see stepper).
|
||||
if (is_curved(fl)) return;
|
||||
|
||||
bool interior_y = (y > 0u) && (y < (unsigned int)(NY - 1));
|
||||
@@ -54,7 +55,8 @@ __device__ __forceinline__ void apply_boundary_esopull_3d(
|
||||
float* f, uint16_t fl, unsigned int x, unsigned int y, unsigned int z,
|
||||
const fpxx* fi, unsigned long t)
|
||||
{
|
||||
// Curved-BC nodes are handled by CurvedBoundaryKernel after the step.
|
||||
// Curved-BC nodes are handled by CurvedBoundaryKernel before the main step when
|
||||
// streaming is double_buffer; EsoPull skips curved (see stepper).
|
||||
if (is_curved(fl)) return;
|
||||
|
||||
bool interior_y = (y > 0u) && (y < (unsigned int)(NY - 1));
|
||||
|
||||
@@ -19,10 +19,8 @@ class LBMStepper:
|
||||
self.module = module
|
||||
self.cfg = cfg
|
||||
|
||||
# Streaming mode
|
||||
self._esopull = (cfg.streaming == "esopull")
|
||||
|
||||
# Kernel handles
|
||||
if self._esopull:
|
||||
self.step_fn = module.get_function("EsoPullStep")
|
||||
self.init_fn = module.get_function("InitEsoPull")
|
||||
@@ -30,11 +28,9 @@ class LBMStepper:
|
||||
self.step_fn = module.get_function("OneStep")
|
||||
self.init_fn = module.get_function("InitTubeFlow_v2")
|
||||
|
||||
# Aux kernels (always present; launched only when compact lists are non-empty)
|
||||
self.curved_fn = module.get_function("CurvedBoundaryKernel")
|
||||
self.sensor_fn = module.get_function("SensorKernel")
|
||||
|
||||
# Launch geometry — ceiling division to cover all cells
|
||||
tpb = cfg.threads_per_block
|
||||
self.block = (tpb, 1, 1)
|
||||
if cfg.is_d2q9:
|
||||
@@ -44,7 +40,6 @@ class LBMStepper:
|
||||
|
||||
self._step_count = 0
|
||||
|
||||
# -- Initialization ------------------------------------------------------
|
||||
def initialize(self):
|
||||
"""Run the init kernel to set up channel flow + flags."""
|
||||
f = self.field
|
||||
@@ -53,14 +48,11 @@ class LBMStepper:
|
||||
block=self.block, grid=self.grid,
|
||||
)
|
||||
if not self._esopull:
|
||||
# Double-buffer: copy init state to both buffers
|
||||
_gpu_bytes = f.n * f.nq * f.store_bytes
|
||||
cuda.memcpy_dtod(f.temp_gpu, f.ddf_gpu, _gpu_bytes)
|
||||
# Sync host
|
||||
cuda.memcpy_dtoh(f.flag, f.flag_gpu)
|
||||
f.download_ddf()
|
||||
|
||||
# -- Stepping ------------------------------------------------------------
|
||||
def step(
|
||||
self,
|
||||
n: int = 1,
|
||||
@@ -69,22 +61,15 @@ class LBMStepper:
|
||||
obs_gpu: cuda.DeviceAllocation,
|
||||
stream: cuda.Stream | None = None,
|
||||
):
|
||||
"""Advance *n* time steps.
|
||||
|
||||
Args:
|
||||
n: Number of substeps.
|
||||
action_gpu: Device buffer for per-object runtime control (packed floats).
|
||||
obs_gpu: Packed telemetry buffer (force + torque + sensor segments); aux kernels
|
||||
index via compile-time ``OBS_*`` macros — host does not pass offsets.
|
||||
stream: Optional CUDA stream for kernel launches (required by runners).
|
||||
|
||||
"""
|
||||
"""Advance *n* time steps."""
|
||||
f = self.field
|
||||
launch_kw = {}
|
||||
if stream is not None:
|
||||
launch_kw["stream"] = stream
|
||||
|
||||
for _ in range(n):
|
||||
self._launch_curved(action_gpu, obs_gpu, **launch_kw)
|
||||
|
||||
if self._esopull:
|
||||
self.step_fn(
|
||||
f.ddf_gpu, f.flag_gpu,
|
||||
@@ -99,29 +84,17 @@ class LBMStepper:
|
||||
block=self.block, grid=self.grid,
|
||||
**launch_kw,
|
||||
)
|
||||
# Swap buffers
|
||||
f.ddf_gpu, f.temp_gpu = f.temp_gpu, f.ddf_gpu
|
||||
|
||||
self._launch_curved(action_gpu, obs_gpu, **launch_kw)
|
||||
self._launch_sensor(obs_gpu, **launch_kw)
|
||||
|
||||
self._step_count += 1
|
||||
|
||||
# -- Aux kernel launchers ------------------------------------------------
|
||||
def _launch_curved(self, action_gpu, obs_gpu, **launch_kw):
|
||||
f = self.field
|
||||
if f.n_curved == 0:
|
||||
return
|
||||
if self._esopull:
|
||||
# Curved BC (Bouzidi) requires a previous-step fi_in buffer, which
|
||||
# EsoPull does not maintain (single-buffer). Curved BC is therefore
|
||||
# unsupported with EsoPull until a shadow-buffer strategy is added.
|
||||
# See one_step_esopull.cu for the implementation plan.
|
||||
if f.n_curved == 0 or self._esopull:
|
||||
return
|
||||
tpb = self.cfg.threads_per_block
|
||||
grid_c = ((f.n_curved + tpb - 1) // tpb, 1, 1)
|
||||
fi_out = f.ddf_gpu
|
||||
fi_in = f.temp_gpu
|
||||
cl = f.curved
|
||||
assert cl.fluid_idx_gpu is not None
|
||||
assert cl.dir_gpu is not None
|
||||
@@ -132,7 +105,7 @@ class LBMStepper:
|
||||
assert cl.fallback_class_gpu is not None
|
||||
assert cl.body_id_gpu is not None
|
||||
self.curved_fn(
|
||||
fi_out, fi_in,
|
||||
f.ddf_gpu,
|
||||
cl.fluid_idx_gpu, cl.dir_gpu, cl.q_gpu,
|
||||
cl.rx_gpu, cl.ry_gpu, cl.rz_gpu, cl.fallback_class_gpu, cl.body_id_gpu,
|
||||
action_gpu, obs_gpu,
|
||||
|
||||
@@ -63,7 +63,10 @@ class Simulation:
|
||||
)
|
||||
|
||||
self._initialized = False
|
||||
self._assert_object_count_contract(expected_count=0)
|
||||
self._load_objects_from_body_config()
|
||||
if self.bodies.count > 0:
|
||||
self.recompile()
|
||||
self._assert_object_count_contract(expected_count=self.bodies.count)
|
||||
|
||||
# -- Object management ---------------------------------------------------
|
||||
def add_cylinder(self, center: Tuple[float, ...],
|
||||
@@ -79,6 +82,33 @@ class Simulation:
|
||||
def add_object(self, obj: SimObject) -> int:
|
||||
return self.bodies.add(obj)
|
||||
|
||||
def _load_objects_from_body_config(self) -> None:
|
||||
"""Instantiate simple objects declared in ``config_body.json``."""
|
||||
for spec in self.body_cfg.objects:
|
||||
obj_type = str(spec.get("type", "")).strip().lower()
|
||||
center = tuple(spec.get("center", ()))
|
||||
radius = float(spec.get("radius", 0.0))
|
||||
omega = spec.get("omega", None)
|
||||
|
||||
if obj_type == "":
|
||||
raise ValueError("Body config object is missing required key 'type'.")
|
||||
if len(center) not in (2, 3):
|
||||
raise ValueError(
|
||||
f"Body config object '{obj_type}' requires center with 2 or 3 entries."
|
||||
)
|
||||
|
||||
if obj_type == "cylinder":
|
||||
body_id = self.add_cylinder(center=center, radius=radius)
|
||||
if omega is not None:
|
||||
self.bodies.get(body_id).state.omega = float(omega)
|
||||
elif obj_type == "sensor":
|
||||
self.add_sensor(center=center, radius=radius)
|
||||
else:
|
||||
raise ValueError(
|
||||
f"Unsupported body config object type '{obj_type}'. "
|
||||
"Supported types: cylinder, sensor."
|
||||
)
|
||||
|
||||
# -- Compilation ---------------------------------------------------------
|
||||
def recompile(self):
|
||||
"""Re-generate config headers and recompile kernel.
|
||||
@@ -106,12 +136,18 @@ class Simulation:
|
||||
# -- Initialization ------------------------------------------------------
|
||||
def initialize(self):
|
||||
"""Initialize flow field and sync objects to GPU."""
|
||||
# Recompile if objects were added after construction
|
||||
if self.bodies.count > 0:
|
||||
# Recompile if objects were added after construction.
|
||||
if self.bodies.count > 0 and self._read_generated_n_objects() != self.bodies.count:
|
||||
self.recompile()
|
||||
|
||||
self.field.flag = self.field.build_channel_flags()
|
||||
if self.bodies.count > 0:
|
||||
self.field.flag = self.bodies.build_flags(self.field.flag)
|
||||
self.field.upload_flags()
|
||||
|
||||
self.stepper.initialize()
|
||||
if self.bodies.count > 0:
|
||||
self.bodies.sync_to_gpu(self.field)
|
||||
self.bodies.sync_to_gpu(self.field, rebuild_flags=False)
|
||||
else:
|
||||
self.bodies.ensure_packed_buffers()
|
||||
self._assert_runtime_contracts()
|
||||
@@ -171,7 +207,7 @@ class Simulation:
|
||||
def update_runtime_params(self, **kwargs):
|
||||
"""Update __constant__ d_params without recompiling.
|
||||
|
||||
Accepted: omega, omega_bulk, fx, fy, fz, rho_ref, u_inlet, n_objects.
|
||||
Accepted: omega, fx, fy, fz, n_objects.
|
||||
"""
|
||||
self.field.update_params(**kwargs)
|
||||
|
||||
@@ -260,4 +296,4 @@ class Simulation:
|
||||
def _assert_runtime_contracts(self) -> None:
|
||||
"""Validate object-count and packed-observation contracts before stepping."""
|
||||
self._assert_object_count_contract()
|
||||
self._assert_obs_layout_contract()
|
||||
self._assert_obs_layout_contract()
|
||||
Reference in new issue
Block a user