zou_he inlet无法保障稳定,加入新的inlet模式

This commit is contained in:
Frank14f committed 2026-05-18 17:51:46 +08:00
1 parent ce492f2794
commit 50b2b6a7ca
26 files changed
+2466 -395

No files matched your search

+1
View File
@@ -85,6 +85,7 @@ def save_checkpoint(field, stepper, lbm_cfg, bodies, path=None):
"les_closed_form": cfg.les_closed_form,
"trt_magic_param": cfg.trt_magic_param,
"inlet_profile": cfg.inlet_profile,
"inlet_scheme": cfg.inlet_scheme,
"outlet_mode": cfg.outlet_mode,
"omega_min": cfg.omega_min, "omega_max": cfg.omega_max,
}
+21 -1
View File
@@ -26,6 +26,12 @@ COLLISION_MAP = {"SRT": 0, "TRT": 1, "MRT": 2}
STREAMING_MAP = {"double_buffer": 0, "esopull": 1}
PRECISION_MAP = {"FP32": 0, "FP16S": 1, "FP16C": 2}
INLET_MAP = {"uniform": 0, "parabolic": 1}
INLET_SCHEME_MAP = {
"zou_he_local": 0,
"channel_stabilized": 1,
"equilibrium": 2,
"regularized": 3,
}
OUTLET_MAP = {"neq_extrap": 0, "zero_gradient": 1, "blended": 2}
Y_WALL_BC_MAP = {"bounce_back": 0, "free_slip": 1}
DTYPE_MAP = {"FP32": "float", "FP64": "double"}
@@ -66,10 +72,12 @@ class LBMConfig:
les_closed_form: bool = True
trt_magic_param: float = 0.1875
inlet_profile: str = "parabolic"
inlet_scheme: str = "zou_he_local"
outlet_mode: str = "neq_extrap"
outlet_blend_alpha: float = 0.7
outlet_backflow_clamp: bool = True
inlet_trt_neq_damp: float = 0.5
inlet_regularized_neq_damp: float = 0.5
outlet_srt_neq_damp: float = 0.5
y_wall_bc: str = "bounce_back"
omega_min: float = 0.01
@@ -106,6 +114,8 @@ class LBMConfig:
assert self.data_type in DTYPE_MAP, f"Unknown data_type: {self.data_type}"
assert self.store_precision in PRECISION_MAP, (
f"Unknown store_precision: {self.store_precision}")
assert self.inlet_scheme in INLET_SCHEME_MAP, (
f"Unknown inlet_scheme: {self.inlet_scheme}")
if self.store_precision == "FP16C":
raise ValueError(
"store_precision='FP16C' is not supported in the current runtime path. "
@@ -119,6 +129,12 @@ class LBMConfig:
assert self.nx > 0 and self.ny > 0 and self.nz > 0
if self.dim == 2:
assert self.nz == 1, "nz must be 1 for 2D"
if not (0.0 <= self.inlet_trt_neq_damp <= 1.0):
raise ValueError("method.inlet.trt_neq_damp must lie in [0, 1].")
if not (0.0 <= self.inlet_regularized_neq_damp <= 1.0):
raise ValueError("method.inlet.regularized_neq_damp must lie in [0, 1].")
if not (0.0 <= self.outlet_srt_neq_damp <= 1.0):
raise ValueError("method.outlet.srt_neq_damp must lie in [0, 1].")
if self.dim == 3 and self.y_wall_bc == "free_slip":
raise ValueError("y_wall_bc='free_slip' is currently supported for D2Q9 only.")
if self.omega_max >= 2.0:
@@ -156,6 +172,7 @@ class LBMConfig:
"LES_CS": f"{self.les_cs:.6f}f",
"LES_CLOSED_FORM": int(self.les_closed_form),
"INLET_PROFILE": INLET_MAP[self.inlet_profile],
"INLET_SCHEME": INLET_SCHEME_MAP[self.inlet_scheme],
"OUTLET_MODE": OUTLET_MAP[self.outlet_mode],
"OUTLET_BLEND_ALPHA": f"{self.outlet_blend_alpha:.3f}f",
"OUTLET_BACKFLOW_CLAMP": int(self.outlet_backflow_clamp),
@@ -164,6 +181,7 @@ class LBMConfig:
"OMEGA_COLLISION_MAX": f"{self.omega_max:.3f}f",
"TRT_MAGIC_PARAM": f"{self.trt_magic_param:.6f}f",
"INLET_TRT_NEQ_DAMP": f"{self.inlet_trt_neq_damp:.4f}f",
"INLET_REGULARIZED_NEQ_DAMP": f"{self.inlet_regularized_neq_damp:.4f}f",
"OUTLET_SRT_NEQ_DAMP": f"{self.outlet_srt_neq_damp:.4f}f",
}
@@ -216,10 +234,12 @@ def load_lbm_config(path: Optional[str] = None) -> LBMConfig:
les_closed_form=m["les"].get("closed_form", True),
trt_magic_param=m["trt"]["magic_param"],
inlet_profile=m["inlet"]["profile"],
inlet_scheme=m["inlet"].get("scheme", "zou_he_local"),
outlet_mode=m["outlet"]["mode"],
outlet_blend_alpha=m["outlet"]["blend_alpha"],
outlet_backflow_clamp=m["outlet"]["backflow_clamp"],
inlet_trt_neq_damp=m["inlet"].get("trt_neq_damp", 0.5),
inlet_regularized_neq_damp=m["inlet"].get("regularized_neq_damp", 0.5),
outlet_srt_neq_damp=m["outlet"].get("srt_neq_damp", 0.5),
y_wall_bc=m.get("y_wall_bc", "bounce_back"),
omega_min=m["omega_guard"]["min"],
@@ -235,4 +255,4 @@ def load_body_config(path: Optional[str] = None) -> BodyConfig:
fpath = _find_config("config_body.json", path)
with open(fpath) as f:
d = json.load(f)
return BodyConfig(objects=d.get("objects", []))
return BodyConfig(objects=d.get("objects", []))
+4 -2
View File
@@ -35,8 +35,10 @@ Python `config.py` 只负责读取和校验,不是配置位置。
| `les.cs` | float | 0.16 | | Smagorinsky 常数 |
| `les.closed_form` | bool | true | | 闭合形式 τ_eff(vs 迭代) |
| `trt.magic_param` | float | 0.1875 | | TRT Λ 参数,高 Re 建议 0.001 |
| `inlet.profile` | string | `"parabolic"` | `uniform`, `parabolic` | 入口速度剖面 |
| `inlet.trt_neq_damp` | float | 0.5 | [0, 1] | TRT 入口 NEQ donor 阻尼;更小更平滑、精度略降 |
| `inlet.profile` | string | `"parabolic"` | `uniform`, `parabolic` | 入口速度剖面(物理目标速度,与 scheme 独立) |
| `inlet.scheme` | string | `"zou_he_local"` | `zou_he_local`, `channel_stabilized`, `equilibrium`, `regularized` | 入口数值闭合。`zou_he_local` 为本地 Zou-He,适合研究或 MRT 路径;`channel_stabilized` 为 donor NEQ 稳定化入口,适合高阻塞或更保守的量产路径;`equilibrium` 直接写入 `feq` 源态,适合 ghost-source 架构下的稳健 SRT 基线;`regularized` 使用本地宏量加 incoming donor NEQ 阻尼,是介于 `equilibrium` 与 `channel_stabilized` 之间的实验入口 |
| `inlet.trt_neq_damp` | float | 0.5 | [0, 1] | 仅 `channel_stabilized`:TRT 入口 donor NEQ 阻尼;更小更平滑、精度略降 |
| `inlet.regularized_neq_damp` | float | 0.5 | [0, 1] | 仅 `regularized`:incoming 方向 donor NEQ 阻尼;0 退化到 unknown 方向仅平衡态,1 为 unknown 方向全 donor NEQ |
| `outlet.mode` | string | `"neq_extrap"` | `neq_extrap`, `zero_gradient`, `blended` | 出口条件 |
| `outlet.backflow_clamp` | bool | true | | 出口回流钳位 |
| `outlet.blend_alpha` | float | 0.7 | | `blended` 下对未知入域方向的混合系数(**所有碰撞模型**共用同一路径) |
+6 -2
View File
@@ -33,9 +33,13 @@
},
"inlet": {
"profile": "parabolic",
"scheme": "zou_he_local",
"_profile": "uniform | parabolic",
"_scheme": "zou_he_local | channel_stabilized | equilibrium | regularized",
"trt_neq_damp": 0.5,
"_trt_neq_damp": "TRT inlet NEQ donor damping [0,1]. Lower = smoother inlet, less accurate."
"_trt_neq_damp": "channel_stabilized only. TRT donor NEQ damping [0,1]. Lower = smoother inlet, less accurate.",
"regularized_neq_damp": 0.5,
"_regularized_neq_damp": "regularized only. Incoming-direction donor NEQ damping [0,1]. 0 = equilibrium-only on unknowns, 1 = full donor NEQ on unknowns."
},
"outlet": {
"mode": "neq_extrap",
@@ -58,4 +62,4 @@
"compute_capability": "auto",
"_compute_capability": "auto | e.g. 7.0"
}
}
}
+7 -5
View File
@@ -115,6 +115,7 @@ def generate_config(cfg: LBMConfig, n_objects: int = 0):
#define LES_CLOSED_FORM {m['LES_CLOSED_FORM']}
#define INLET_PROFILE {m['INLET_PROFILE']}
#define INLET_SCHEME {m['INLET_SCHEME']}
#define OUTLET_MODE {m['OUTLET_MODE']}
#define OUTLET_BLEND_ALPHA {m['OUTLET_BLEND_ALPHA']}
#define OUTLET_BACKFLOW_CLAMP {m['OUTLET_BACKFLOW_CLAMP']}
@@ -125,10 +126,12 @@ def generate_config(cfg: LBMConfig, n_objects: int = 0):
#define TRT_MAGIC_PARAM {m['TRT_MAGIC_PARAM']}
// NEQ damping coefficients for inlet/outlet BC reconstruction.
// TRT inlet: damps donor non-equilibrium to reduce inlet noise at high Re.
// SRT outlet: damps donor non-equilibrium to suppress checkerboard noise.
#define INLET_TRT_NEQ_DAMP {m['INLET_TRT_NEQ_DAMP']}
#define OUTLET_SRT_NEQ_DAMP {m['OUTLET_SRT_NEQ_DAMP']}
// TRT inlet: donor damping used by the channel_stabilized inlet.
// Regularized inlet: damping on incoming-direction donor NEQ.
// SRT outlet: damped outlet reconstruction to suppress checkerboard noise.
#define INLET_TRT_NEQ_DAMP {m['INLET_TRT_NEQ_DAMP']}
#define INLET_REGULARIZED_NEQ_DAMP {m['INLET_REGULARIZED_NEQ_DAMP']}
#define OUTLET_SRT_NEQ_DAMP {m['OUTLET_SRT_NEQ_DAMP']}
#endif
""")
@@ -219,4 +222,3 @@ def load_module(ptx_path: Optional[str] = None) -> cuda.Module:
def compile_kernel_v2(arch: str = "sm_70") -> str:
"""Alias for compile_kernel() kept for test-script compatibility."""
return compile_kernel(arch=arch)
@@ -0,0 +1,110 @@
// CelerisLab – boundary/inlet/channel_stabilized.cuh
// Donor-based west velocity inlet closures designed for robust ghost-source use.
// ============================================================================
#ifndef CELERIS_BOUNDARY_INLET_CHANNEL_STABILIZED_CUH
#define CELERIS_BOUNDARY_INLET_CHANNEL_STABILIZED_CUH
#if LATTICE_MODEL == LATTICE_D2Q9
__device__ inline void apply_channel_stabilized_inlet_d2q9(
float* __restrict__ f,
const float* __restrict__ f_neb,
float y_coord)
{
float rho_neb, u_neb, v_neb;
compute_rho_u(f_neb, rho_neb, u_neb, v_neb);
const float u_target = inlet_target_u(y_coord);
const float v_target = 0.0f;
const float rho_in = west_velocity_rho_closure_d2q9(f, 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
const float beta_n = INLET_TRT_NEQ_DAMP;
#else
const float beta_n = 1.0f;
#endif
const float f1_try = feq_tar[1] + beta_n * (f_neb[1] - feq_neb[1]);
const float known_sum = f[0] + f[2] + f[3] + f[4] + f[6] + f[8];
float pair_diff = rho_in * v_target + (f[4] - f[3]) + (f[6] - f[8]);
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 (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);
}
#endif
#if LATTICE_MODEL == LATTICE_D3Q19
__device__ inline void apply_channel_stabilized_inlet_d3q19(
float* __restrict__ f,
const float* __restrict__ f_neb,
float y_coord)
{
float rho_neb, un, vn, wn;
compute_rho_u(f_neb, rho_neb, un, vn, wn);
const float u_tar = inlet_target_u(y_coord);
const float v_tar = 0.0f;
const float w_tar = 0.0f;
const float rho_in = west_velocity_rho_closure_d3q19(f, u_tar);
float feq_tar[19], feq_neb[19];
compute_feq(rho_in, u_tar, v_tar, w_tar, feq_tar);
compute_feq(rho_neb, un, vn, wn, feq_neb);
#if COLLISION_MODEL == 1
const float beta_n = INLET_TRT_NEQ_DAMP;
#else
const float beta_n = 1.0f;
#endif
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;
float y_diff = rho_in * v_tar
- (f[3] - f[4] - f[8] + f[11] - f[12] + f[14] + f[17] - f[18]);
float z_diff = rho_in * w_tar
- (f[5] - f[6] - f[10] + f[11] - f[12] + f[16] + f[18] - f[17]);
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);
}
#endif
#endif // CELERIS_BOUNDARY_INLET_CHANNEL_STABILIZED_CUH
@@ -0,0 +1,72 @@
// CelerisLab – boundary/inlet/common.cuh
// Shared helpers for west velocity inlet source-state generation.
//
// Important semantic contract in this solver:
// - x=0 inlet cells are SOLID|BC_INLET ghost source nodes
// - they generate the state later pulled by the first interior fluid column
// - inlet methods therefore construct a source state, not a textbook fluid-node
// boundary update in isolation
// ============================================================================
#ifndef CELERIS_BOUNDARY_INLET_COMMON_CUH
#define CELERIS_BOUNDARY_INLET_COMMON_CUH
#ifndef INLET_PROFILE
#define INLET_PROFILE 1
#endif
#ifndef INLET_SCHEME
#define INLET_SCHEME 0
#endif
#ifndef INLET_TRT_NEQ_DAMP
#define INLET_TRT_NEQ_DAMP 0.50f
#endif
#ifndef INLET_REGULARIZED_NEQ_DAMP
#define INLET_REGULARIZED_NEQ_DAMP 0.50f
#endif
__device__ __forceinline__ float inlet_target_u(float y_coord) {
#if INLET_PROFILE == 0
return U0;
#else
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;
const float shape = fmaxf(0.0f, 4.0f * eta * (1.0f - eta));
return U0 * 1.5f * shape;
#endif
}
#if LATTICE_MODEL == LATTICE_D2Q9
__device__ __forceinline__ float west_velocity_rho_closure_d2q9(
const float* __restrict__ f,
float ux_target)
{
return (f[0] + f[3] + f[4] + 2.0f * (f[2] + f[6] + f[8]))
/ (1.0f - ux_target);
}
#endif
#if LATTICE_MODEL == LATTICE_D3Q19
__device__ __forceinline__ float west_velocity_rho_closure_d3q19(
const float* __restrict__ f,
float ux_target)
{
return (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 - ux_target);
}
#endif
__device__ __forceinline__ bool inlet_scheme_uses_post_collision_ghost()
{
#if INLET_SCHEME == 0
return true;
#else
return false;
#endif
}
#endif // CELERIS_BOUNDARY_INLET_COMMON_CUH
@@ -0,0 +1,48 @@
// CelerisLab – boundary/inlet/equilibrium.cuh
// West velocity inlet source state built from full equilibrium.
//
// This method recovers rho from local west-boundary mass closure and then
// overwrites the full ghost-node state with feq(rho, u_target).
// It is robust for ghost-source architectures because it injects no boundary
// non-equilibrium content.
// ============================================================================
#ifndef CELERIS_BOUNDARY_INLET_EQUILIBRIUM_CUH
#define CELERIS_BOUNDARY_INLET_EQUILIBRIUM_CUH
#if LATTICE_MODEL == LATTICE_D2Q9
__device__ inline void apply_equilibrium_left_velocity_inlet_d2q9(
float* __restrict__ f,
float ux_target,
float uy_target)
{
const float rho = west_velocity_rho_closure_d2q9(f, ux_target);
float feq[9];
compute_feq(rho, ux_target, uy_target, feq);
#pragma unroll
for (int i = 0; i < 9; i++) {
f[i] = feq[i];
}
}
#endif
#if LATTICE_MODEL == LATTICE_D3Q19
__device__ inline void apply_equilibrium_left_velocity_inlet_d3q19(
float* __restrict__ f,
float ux_target,
float uy_target,
float uz_target)
{
const float rho = west_velocity_rho_closure_d3q19(f, ux_target);
float feq[19];
compute_feq(rho, ux_target, uy_target, uz_target, feq);
#pragma unroll
for (int i = 0; i < 19; i++) {
f[i] = feq[i];
}
}
#endif
#endif // CELERIS_BOUNDARY_INLET_EQUILIBRIUM_CUH
@@ -0,0 +1,59 @@
// CelerisLab – boundary/inlet/regularized.cuh
// West velocity inlet with local macro state and donor-damped incoming NEQ.
//
// This method keeps the target macro state from local west-boundary closure but
// avoids a full local algebraic source state by injecting only damped donor NEQ
// on incoming directions.
// ============================================================================
#ifndef CELERIS_BOUNDARY_INLET_REGULARIZED_CUH
#define CELERIS_BOUNDARY_INLET_REGULARIZED_CUH
#if LATTICE_MODEL == LATTICE_D2Q9
__device__ inline void apply_regularized_left_velocity_inlet_d2q9(
float* __restrict__ f,
const float* __restrict__ f_neb,
float ux_target,
float uy_target)
{
const float rho = west_velocity_rho_closure_d2q9(f, ux_target);
float rho_neb, u_neb, v_neb;
compute_rho_u(f_neb, rho_neb, u_neb, v_neb);
float feq_tar[9], feq_neb[9];
compute_feq(rho, ux_target, uy_target, feq_tar);
compute_feq(rho_neb, u_neb, v_neb, feq_neb);
const float beta = INLET_REGULARIZED_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]);
}
#endif
#if LATTICE_MODEL == LATTICE_D3Q19
__device__ inline void apply_regularized_left_velocity_inlet_d3q19(
float* __restrict__ f,
const float* __restrict__ f_neb,
float ux_target,
float uy_target,
float uz_target)
{
const float rho = west_velocity_rho_closure_d3q19(f, ux_target);
float rho_neb, u_neb, v_neb, w_neb;
compute_rho_u(f_neb, rho_neb, u_neb, v_neb, w_neb);
float feq_tar[19], feq_neb[19];
compute_feq(rho, ux_target, uy_target, uz_target, feq_tar);
compute_feq(rho_neb, u_neb, v_neb, w_neb, feq_neb);
const float beta = INLET_REGULARIZED_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]);
}
#endif
#endif // CELERIS_BOUNDARY_INLET_REGULARIZED_CUH
@@ -0,0 +1,90 @@
// CelerisLab – boundary/inlet/zou_he_local.cuh
// Local on-site west velocity inlet closures.
//
// D2Q9 and D3Q19 variants recover rho from local mass closure and reconstruct
// unknown incoming populations from local target velocity constraints.
//
// In the current ghost-source architecture these reconstructed states are later
// used as pull sources. Therefore this method requires post-BC ghost collision
// in the step kernel.
// ============================================================================
#ifndef CELERIS_BOUNDARY_INLET_ZOU_HE_LOCAL_CUH
#define CELERIS_BOUNDARY_INLET_ZOU_HE_LOCAL_CUH
#if LATTICE_MODEL == LATTICE_D2Q9
// Free-slip y-walls: at inlet rows y=1 and y=NY-2, pull can source wall nodes for
// some known directions. Copy those from stored DDF at (x=1, same y) only.
__device__ inline void repair_zou_he_west_knowns_d2q9(
float* __restrict__ f,
const fpxx* __restrict__ fi_in,
unsigned int x,
unsigned int y)
{
if (x != 0u) return;
const unsigned long k_int = linear_index(x + 1u, y);
if (y == 1u) {
f[3] = load_ddf(fi_in, index_f(k_int, 3u));
f[8] = load_ddf(fi_in, index_f(k_int, 8u));
} else if (y == (unsigned int)(NY - 2)) {
f[4] = load_ddf(fi_in, index_f(k_int, 4u));
f[6] = load_ddf(fi_in, index_f(k_int, 6u));
}
}
__device__ inline void apply_zou_he_left_velocity_inlet_d2q9(
float* __restrict__ f,
float ux_target,
float uy_target)
{
const float rho = west_velocity_rho_closure_d2q9(f, ux_target);
f[1] = f[2] + (2.0f / 3.0f) * rho * ux_target;
f[5] = f[6]
+ 0.5f * (f[4] - f[3])
+ (1.0f / 6.0f) * rho * ux_target
+ 0.5f * rho * uy_target;
f[7] = f[8]
+ 0.5f * (f[3] - f[4])
+ (1.0f / 6.0f) * rho * ux_target
- 0.5f * rho * uy_target;
}
#endif // LATTICE_MODEL == LATTICE_D2Q9
#if LATTICE_MODEL == LATTICE_D3Q19
// Hecht-Harting style D3Q19 on-site west velocity closure adapted to the
// codebase paired ordering:
// 7 = (+x,+y), 8 = (-x,-y)
// 13 = (+x,-y), 14 = (-x,+y)
// 9 = (+x,+z), 10 = (-x,-z)
// 15 = (+x,-z), 16 = (-x,+z)
__device__ inline void apply_zou_he_left_velocity_inlet_d3q19(
float* __restrict__ f,
float ux_target,
float uy_target,
float uz_target)
{
const float rho = west_velocity_rho_closure_d3q19(f, ux_target);
const float Nyx = 0.5f * (f[3] + f[11] + f[17] - (f[4] + f[12] + f[18]))
- (1.0f / 3.0f) * rho * uy_target;
const float Nzx = 0.5f * (f[5] + f[11] + f[18] - (f[6] + f[17] + f[12]))
- (1.0f / 3.0f) * rho * uz_target;
f[1] = f[2] + (1.0f / 3.0f) * rho * ux_target;
f[7] = f[8] + (1.0f / 6.0f) * rho * (ux_target + uy_target) - Nyx;
f[13] = f[14] + (1.0f / 6.0f) * rho * (ux_target - uy_target) + Nyx;
f[9] = f[10] + (1.0f / 6.0f) * rho * (ux_target + uz_target) - Nzx;
f[15] = f[16] + (1.0f / 6.0f) * rho * (ux_target - uz_target) + Nzx;
}
#endif // LATTICE_MODEL == LATTICE_D3Q19
#endif // CELERIS_BOUNDARY_INLET_ZOU_HE_LOCAL_CUH
@@ -1,359 +1,225 @@
// CelerisLab – boundary/inlet_outlet.cuh
// Inlet and outlet boundary conditions (D2Q9).
// Inlet and outlet dispatch layer.
//
// Parabolic inlet (non-equilibrium extrapolation, Zou-He style):
// Left wall (x=0): reconstruct cx>0 populations (i=1,5,7)
// This file contains only profile helpers, method includes, and compile-time
// switching. Each concrete inlet or outlet implementation lives in its own file
// under boundary/inlet/ or boundary/outlet/.
//
// Pressure outlet (non-equilibrium extrapolation):
// Right wall (x=NX-1): reconstruct cx<0 populations (i=2,6,8)
//
// New paired D2Q9 ordering:
// cx = {0, 1,-1, 0, 0, 1,-1, 1,-1}
// cy = {0, 0, 0, 1,-1, 1,-1,-1, 1}
// Inlet scheme IDs:
// 0 = zou_he_local
// 1 = channel_stabilized
// 2 = equilibrium
// 3 = regularized
// ============================================================================
#ifndef CELERIS_BOUNDARY_INLET_OUTLET_CUH
#define CELERIS_BOUNDARY_INLET_OUTLET_CUH
#ifndef INLET_PROFILE
#define INLET_PROFILE 1
#ifndef INLET_SCHEME
#define INLET_SCHEME 0
#endif
#ifndef OUTLET_MODE
#define OUTLET_MODE 0
#endif
#include "inlet/common.cuh"
#include "inlet/zou_he_local.cuh"
#include "inlet/channel_stabilized.cuh"
#include "inlet/equilibrium.cuh"
#include "inlet/regularized.cuh"
#include "outlet/pressure_neq.cuh"
#ifndef OUTLET_BACKFLOW_CLAMP
#define OUTLET_BACKFLOW_CLAMP 1
#endif
#ifndef OUTLET_BLEND_ALPHA
#define OUTLET_BLEND_ALPHA 0.70f
#endif
// OUTLET_SRT_NEQ_DAMP and INLET_TRT_NEQ_DAMP are injected by config_method.h.
// These fallback defaults are only active if building outside the normal
// Python compile pipeline (e.g. standalone nvcc tests).
#ifndef OUTLET_SRT_NEQ_DAMP
#define OUTLET_SRT_NEQ_DAMP 0.50f
#endif
#ifndef INLET_TRT_NEQ_DAMP
#define INLET_TRT_NEQ_DAMP 0.50f
#endif
__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
// 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; // 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
}
#if LATTICE_MODEL == LATTICE_D2Q9
// ---------------------------------------------------------------------------
// Parabolic inlet (x = 0, non-equilibrium extrapolation)
//
// f, f_neb are local DDF arrays:
// f = populations at the boundary node (x=0)
// 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).
//
// 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)
#if DIM == 2
__device__ __forceinline__ void apply_inlet_pull_d2q9(
float* __restrict__ f,
unsigned int x,
unsigned int y,
const fpxx* __restrict__ fi_in)
{
// 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 inlet velocity.
const float u_target = inlet_target_u(y_coord);
const float u_target = inlet_target_u((float)y);
const float v_target = 0.0f;
// 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
const float beta_n = INLET_TRT_NEQ_DAMP;
#else
const float beta_n = 1.0f;
#if INLET_SCHEME == 0
#if Y_WALL_BC == 1
repair_zou_he_west_knowns_d2q9(f, fi_in, x, y);
#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);
apply_zou_he_left_velocity_inlet_d2q9(f, u_target, v_target);
#elif INLET_SCHEME == 1 || INLET_SCHEME == 3
float f_neb[NQ];
const unsigned long k_neb = linear_index(x + 1u, y);
for (int i = 0; i < NQ; i++) {
f_neb[i] = load_ddf(fi_in, index_f(k_neb, (unsigned int)i));
}
f[1] = f1;
f[5] = 0.5f * (pair_sum + pair_diff);
f[7] = 0.5f * (pair_sum - pair_diff);
#if INLET_SCHEME == 1
apply_channel_stabilized_inlet_d2q9(f, f_neb, (float)y);
#else
apply_regularized_left_velocity_inlet_d2q9(f, f_neb, u_target, v_target);
#endif
#elif INLET_SCHEME == 2
apply_equilibrium_left_velocity_inlet_d2q9(f, u_target, v_target);
#else
#error "Unsupported INLET_SCHEME for D2Q9"
#endif
}
// ---------------------------------------------------------------------------
// Pressure outlet (x = NX-1, non-equilibrium extrapolation)
//
// Reconstructs f[2], f[6], f[8] (cx < 0 directions in new ordering)
// p_out = 0 (gauge pressure), uses velocity from neighbor.
// ---------------------------------------------------------------------------
__device__ inline void apply_pressure_outlet(float* __restrict__ f,
const float* __restrict__ f_neb,
float y_coord)
__device__ __forceinline__ void apply_outlet_pull_d2q9(
float* __restrict__ f,
unsigned int x,
unsigned int y,
const fpxx* __restrict__ fi_in)
{
(void)y_coord;
#if OUTLET_MODE == 1
// Simple zero-gradient copy for unknown incoming directions at outlet.
f[2] = f_neb[2];
f[8] = f_neb[8];
f[6] = f_neb[6];
#else
// Prescribed-pressure outlet: keep neighbor velocity and impose reference
// outlet density for NEQ reconstruction.
float rho_neb, u_neb, v_neb;
compute_rho_u(f_neb, rho_neb, u_neb, v_neb);
#if OUTLET_BACKFLOW_CLAMP
u_neb = fmaxf(u_neb, 0.0f);
#endif
float rho_out = RHO;
float feq_tar[9], feq_neb[9];
compute_feq(rho_out, u_neb, v_neb, feq_tar);
compute_feq(rho_neb, u_neb, v_neb, feq_neb);
#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++) {
const float fneq = f_neb[i] - feq_neb[i];
f[i] = feq_tar[i] + beta * fneq;
float f_neb[NQ];
const unsigned long k_neb = linear_index(x - 1u, y);
for (int i = 0; i < NQ; i++) {
f_neb[i] = load_ddf(fi_in, index_f(k_neb, (unsigned int)i));
}
#elif OUTLET_MODE == 2
const float a = OUTLET_BLEND_ALPHA;
f[2] = a * (f_neb[2] - feq_neb[2] + feq_tar[2]) + (1.0f - a) * f_neb[2];
f[8] = a * (f_neb[8] - feq_neb[8] + feq_tar[8]) + (1.0f - a) * f_neb[8];
f[6] = a * (f_neb[6] - feq_neb[6] + feq_tar[6]) + (1.0f - a) * f_neb[6];
#else
f[2] = f_neb[2] - feq_neb[2] + feq_tar[2];
f[8] = f_neb[8] - feq_neb[8] + feq_tar[8];
f[6] = f_neb[6] - feq_neb[6] + feq_tar[6];
#endif
#endif
apply_pressure_outlet_d2q9(f, f_neb);
}
#endif
#endif // LATTICE_D2Q9
// ============================================================================
// D3Q19 inlet / outlet (non-equilibrium extrapolation)
//
// Parabolic inlet (x=0): reconstruct cx>0 populations i=1,7,9,13,15
// Pressure outlet (x=NX-1): reconstruct cx<0 populations i=2,8,10,14,16
//
// Uses generic feq computation from macro.cuh to avoid hand-expanded formulas.
// ============================================================================
#if LATTICE_MODEL == LATTICE_D3Q19
__device__ inline void apply_parabolic_inlet_3d(float* __restrict__ f,
const float* __restrict__ f_neb,
float y_coord)
#if DIM == 3
__device__ __forceinline__ void apply_inlet_pull_d3q19(
float* __restrict__ f,
unsigned int x,
unsigned int y,
unsigned int z,
const fpxx* __restrict__ fi_in)
{
// Donor macros from the first interior fluid column.
float rho_neb, un, vn, wn;
compute_rho_u(f_neb, rho_neb, un, vn, wn);
const float u_target = inlet_target_u((float)y);
const float v_target = 0.0f;
const float w_target = 0.0f;
// 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 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);
float feq_tar[19], feq_neb[19];
compute_feq(rho_in, u_tar, v_tar, w_tar, feq_tar);
compute_feq(rho_neb, un, vn, wn, feq_neb);
#if COLLISION_MODEL == 1
const float beta_n = INLET_TRT_NEQ_DAMP;
#if INLET_SCHEME == 0
apply_zou_he_left_velocity_inlet_d3q19(f, u_target, v_target, w_target);
#elif INLET_SCHEME == 1 || INLET_SCHEME == 3
float f_neb[NQ];
const unsigned long k_neb = linear_index(x + 1u, y, z);
for (int i = 0; i < NQ; i++) {
f_neb[i] = load_ddf(fi_in, index_f(k_neb, (unsigned int)i));
}
#if INLET_SCHEME == 1
apply_channel_stabilized_inlet_d3q19(f, f_neb, (float)y);
#else
apply_regularized_left_velocity_inlet_d3q19(f, f_neb, u_target, v_target, w_target);
#endif
#elif INLET_SCHEME == 2
apply_equilibrium_left_velocity_inlet_d3q19(f, u_target, v_target, w_target);
#else
const float beta_n = 1.0f;
#error "Unsupported INLET_SCHEME for D3Q19"
#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,
const float* __restrict__ f_neb,
float y_coord)
__device__ __forceinline__ void apply_outlet_pull_d3q19(
float* __restrict__ f,
unsigned int x,
unsigned int y,
unsigned int z,
const fpxx* __restrict__ fi_in)
{
(void)y_coord;
#if OUTLET_MODE == 1
// Simple zero-gradient copy for unknown incoming directions at outlet.
f[2] = f_neb[2];
f[8] = f_neb[8];
f[10] = f_neb[10];
f[14] = f_neb[14];
f[16] = f_neb[16];
#else
// Neighbor macros
float rho_neb, un, vn, wn;
compute_rho_u(f_neb, rho_neb, un, vn, wn);
#if OUTLET_BACKFLOW_CLAMP
un = fmaxf(un, 0.0f);
#endif
// Prescribed-pressure outlet: keep neighbor velocity and impose reference
// outlet density for NEQ reconstruction.
float rho_out = RHO;
float feq_tar[19], feq_neb[19];
compute_feq(rho_out, un, vn, wn, feq_tar);
compute_feq(rho_neb, un, vn, wn, feq_neb);
// Reconstruct cx<0 directions: i = 2, 8, 10, 14, 16
#if COLLISION_MODEL == 0 || COLLISION_MODEL == 1
const float beta = OUTLET_SRT_NEQ_DAMP;
#pragma unroll
for (int i = 0; i < 19; i++) {
const float fneq = f_neb[i] - feq_neb[i];
f[i] = feq_tar[i] + beta * fneq;
float f_neb[NQ];
const unsigned long k_neb = linear_index(x - 1u, y, z);
for (int i = 0; i < NQ; i++) {
f_neb[i] = load_ddf(fi_in, index_f(k_neb, (unsigned int)i));
}
#elif OUTLET_MODE == 2
const float a = OUTLET_BLEND_ALPHA;
f[2] = a * (f_neb[2] - feq_neb[2] + feq_tar[2]) + (1.0f - a) * f_neb[2];
f[8] = a * (f_neb[8] - feq_neb[8] + feq_tar[8]) + (1.0f - a) * f_neb[8];
f[10] = a * (f_neb[10] - feq_neb[10] + feq_tar[10]) + (1.0f - a) * f_neb[10];
f[14] = a * (f_neb[14] - feq_neb[14] + feq_tar[14]) + (1.0f - a) * f_neb[14];
f[16] = a * (f_neb[16] - feq_neb[16] + feq_tar[16]) + (1.0f - a) * f_neb[16];
#else
f[2] = f_neb[2] - feq_neb[2] + feq_tar[2];
f[8] = f_neb[8] - feq_neb[8] + feq_tar[8];
f[10] = f_neb[10] - feq_neb[10] + feq_tar[10];
f[14] = f_neb[14] - feq_neb[14] + feq_tar[14];
f[16] = f_neb[16] - feq_neb[16] + feq_tar[16];
apply_pressure_outlet_d3q19(f, f_neb);
}
#endif
#if DIM == 2
__device__ __forceinline__ void apply_inlet_esopull_d2q9(
float* __restrict__ f,
unsigned int x,
unsigned int y,
const fpxx* __restrict__ fi,
unsigned long t)
{
const float u_target = inlet_target_u((float)y);
const float v_target = 0.0f;
#if INLET_SCHEME == 0
#if Y_WALL_BC == 1
repair_zou_he_west_knowns_d2q9(f, fi, x, y);
#endif
apply_zou_he_left_velocity_inlet_d2q9(f, u_target, v_target);
#elif INLET_SCHEME == 1 || INLET_SCHEME == 3
const unsigned long k_neb = linear_index(x + 1u, y);
unsigned long j_neb[NQ];
compute_neighbors(x + 1u, y, j_neb);
float f_neb[NQ];
load_f_esopull(k_neb, f_neb, fi, j_neb, t);
#if INLET_SCHEME == 1
apply_channel_stabilized_inlet_d2q9(f, f_neb, (float)y);
#else
apply_regularized_left_velocity_inlet_d2q9(f, f_neb, u_target, v_target);
#endif
#elif INLET_SCHEME == 2
apply_equilibrium_left_velocity_inlet_d2q9(f, u_target, v_target);
#else
#error "Unsupported INLET_SCHEME for D2Q9"
#endif
}
#endif // LATTICE_D3Q19
__device__ __forceinline__ void apply_outlet_esopull_d2q9(
float* __restrict__ f,
unsigned int x,
unsigned int y,
const fpxx* __restrict__ fi,
unsigned long t)
{
const unsigned long k_neb = linear_index(x - 1u, y);
unsigned long j_neb[NQ];
compute_neighbors(x - 1u, y, j_neb);
float f_neb[NQ];
load_f_esopull(k_neb, f_neb, fi, j_neb, t);
apply_pressure_outlet_d2q9(f, f_neb);
}
#endif
#endif // CELERIS_BOUNDARY_INLET_OUTLET_CUH
#if DIM == 3
__device__ __forceinline__ void apply_inlet_esopull_d3q19(
float* __restrict__ f,
unsigned int x,
unsigned int y,
unsigned int z,
const fpxx* __restrict__ fi,
unsigned long t)
{
const float u_target = inlet_target_u((float)y);
const float v_target = 0.0f;
const float w_target = 0.0f;
#if INLET_SCHEME == 0
apply_zou_he_left_velocity_inlet_d3q19(f, u_target, v_target, w_target);
#elif INLET_SCHEME == 1 || INLET_SCHEME == 3
const unsigned long k_neb = linear_index(x + 1u, y, z);
unsigned long j_neb[NQ];
compute_neighbors(k_neb, j_neb);
float f_neb[NQ];
load_f_esopull(k_neb, f_neb, fi, j_neb, t);
#if INLET_SCHEME == 1
apply_channel_stabilized_inlet_d3q19(f, f_neb, (float)y);
#else
apply_regularized_left_velocity_inlet_d3q19(f, f_neb, u_target, v_target, w_target);
#endif
#elif INLET_SCHEME == 2
apply_equilibrium_left_velocity_inlet_d3q19(f, u_target, v_target, w_target);
#else
#error "Unsupported INLET_SCHEME for D3Q19"
#endif
}
__device__ __forceinline__ void apply_outlet_esopull_d3q19(
float* __restrict__ f,
unsigned int x,
unsigned int y,
unsigned int z,
const fpxx* __restrict__ fi,
unsigned long t)
{
const unsigned long k_neb = linear_index(x - 1u, y, z);
unsigned long j_neb[NQ];
compute_neighbors(k_neb, j_neb);
float f_neb[NQ];
load_f_esopull(k_neb, f_neb, fi, j_neb, t);
apply_pressure_outlet_d3q19(f, f_neb);
}
#endif
#endif // CELERIS_BOUNDARY_INLET_OUTLET_CUH
@@ -0,0 +1,114 @@
// CelerisLab – boundary/outlet/pressure_neq.cuh
// Pressure outlet and zero-gradient outlet closures.
// ============================================================================
#ifndef CELERIS_BOUNDARY_OUTLET_PRESSURE_NEQ_CUH
#define CELERIS_BOUNDARY_OUTLET_PRESSURE_NEQ_CUH
#ifndef OUTLET_MODE
#define OUTLET_MODE 0
#endif
#ifndef OUTLET_BACKFLOW_CLAMP
#define OUTLET_BACKFLOW_CLAMP 1
#endif
#ifndef OUTLET_BLEND_ALPHA
#define OUTLET_BLEND_ALPHA 0.70f
#endif
#ifndef OUTLET_SRT_NEQ_DAMP
#define OUTLET_SRT_NEQ_DAMP 0.50f
#endif
#if LATTICE_MODEL == LATTICE_D2Q9
__device__ inline void apply_pressure_outlet_d2q9(
float* __restrict__ f,
const float* __restrict__ f_neb)
{
#if OUTLET_MODE == 1
f[2] = f_neb[2];
f[8] = f_neb[8];
f[6] = f_neb[6];
#else
float rho_neb, u_neb, v_neb;
compute_rho_u(f_neb, rho_neb, u_neb, v_neb);
#if OUTLET_BACKFLOW_CLAMP
u_neb = fmaxf(u_neb, 0.0f);
#endif
const float rho_out = RHO;
float feq_tar[9], feq_neb[9];
compute_feq(rho_out, u_neb, v_neb, feq_tar);
compute_feq(rho_neb, u_neb, v_neb, feq_neb);
#if COLLISION_MODEL == 0 || COLLISION_MODEL == 1
const float beta = OUTLET_SRT_NEQ_DAMP;
#pragma unroll
for (int i = 0; i < 9; i++) {
const float fneq = f_neb[i] - feq_neb[i];
f[i] = feq_tar[i] + beta * fneq;
}
#elif OUTLET_MODE == 2
const float a = OUTLET_BLEND_ALPHA;
f[2] = a * (f_neb[2] - feq_neb[2] + feq_tar[2]) + (1.0f - a) * f_neb[2];
f[8] = a * (f_neb[8] - feq_neb[8] + feq_tar[8]) + (1.0f - a) * f_neb[8];
f[6] = a * (f_neb[6] - feq_neb[6] + feq_tar[6]) + (1.0f - a) * f_neb[6];
#else
f[2] = f_neb[2] - feq_neb[2] + feq_tar[2];
f[8] = f_neb[8] - feq_neb[8] + feq_tar[8];
f[6] = f_neb[6] - feq_neb[6] + feq_tar[6];
#endif
#endif
}
#endif
#if LATTICE_MODEL == LATTICE_D3Q19
__device__ inline void apply_pressure_outlet_d3q19(
float* __restrict__ f,
const float* __restrict__ f_neb)
{
#if OUTLET_MODE == 1
f[2] = f_neb[2];
f[8] = f_neb[8];
f[10] = f_neb[10];
f[14] = f_neb[14];
f[16] = f_neb[16];
#else
float rho_neb, un, vn, wn;
compute_rho_u(f_neb, rho_neb, un, vn, wn);
#if OUTLET_BACKFLOW_CLAMP
un = fmaxf(un, 0.0f);
#endif
const float rho_out = RHO;
float feq_tar[19], feq_neb[19];
compute_feq(rho_out, un, vn, wn, feq_tar);
compute_feq(rho_neb, un, vn, wn, feq_neb);
#if COLLISION_MODEL == 0 || COLLISION_MODEL == 1
const float beta = OUTLET_SRT_NEQ_DAMP;
#pragma unroll
for (int i = 0; i < 19; i++) {
const float fneq = f_neb[i] - feq_neb[i];
f[i] = feq_tar[i] + beta * fneq;
}
#elif OUTLET_MODE == 2
const float a = OUTLET_BLEND_ALPHA;
f[2] = a * (f_neb[2] - feq_neb[2] + feq_tar[2]) + (1.0f - a) * f_neb[2];
f[8] = a * (f_neb[8] - feq_neb[8] + feq_tar[8]) + (1.0f - a) * f_neb[8];
f[10] = a * (f_neb[10] - feq_neb[10] + feq_tar[10]) + (1.0f - a) * f_neb[10];
f[14] = a * (f_neb[14] - feq_neb[14] + feq_tar[14]) + (1.0f - a) * f_neb[14];
f[16] = a * (f_neb[16] - feq_neb[16] + feq_tar[16]) + (1.0f - a) * f_neb[16];
#else
f[2] = f_neb[2] - feq_neb[2] + feq_tar[2];
f[8] = f_neb[8] - feq_neb[8] + feq_tar[8];
f[10] = f_neb[10] - feq_neb[10] + feq_tar[10];
f[14] = f_neb[14] - feq_neb[14] + feq_tar[14];
f[16] = f_neb[16] - feq_neb[16] + feq_tar[16];
#endif
#endif
}
#endif
#endif // CELERIS_BOUNDARY_OUTLET_PRESSURE_NEQ_CUH
@@ -6,8 +6,8 @@
#define NT 256
#define MULT_GPU 0
#define NX 1351
#define NY 601
#define NX 401
#define NY 201
#define NZ 1
// ---- Lattice model (single source of truth) ----
@@ -13,19 +13,22 @@
#define LES_CLOSED_FORM 1
#define INLET_PROFILE 0
#define INLET_SCHEME 3
#define OUTLET_MODE 0
#define OUTLET_BLEND_ALPHA 0.700f
#define OUTLET_BACKFLOW_CLAMP 1
#define Y_WALL_BC 1
#define Y_WALL_BC 0
#define OMEGA_COLLISION_MIN 0.01f
#define OMEGA_COLLISION_MAX 1.960f
#define TRT_MAGIC_PARAM 0.187500f
// NEQ damping coefficients for inlet/outlet BC reconstruction.
// TRT inlet: damps donor non-equilibrium to reduce inlet noise at high Re.
// SRT outlet: damps donor non-equilibrium to suppress checkerboard noise.
#define INLET_TRT_NEQ_DAMP 0.5000f
#define OUTLET_SRT_NEQ_DAMP 0.5000f
// TRT inlet: donor damping used by the channel_stabilized inlet.
// Regularized inlet: damping on incoming-direction donor NEQ.
// SRT outlet: damped outlet reconstruction to suppress checkerboard noise.
#define INLET_TRT_NEQ_DAMP 0.5000f
#define INLET_REGULARIZED_NEQ_DAMP 0.5000f
#define OUTLET_SRT_NEQ_DAMP 0.5000f
#endif
@@ -3,6 +3,6 @@
#ifndef CELERIS_CONFIG_OBJECTS_H
#define CELERIS_CONFIG_OBJECTS_H
#define N_OBJS 1
#define N_OBJS 0
#endif
@@ -4,7 +4,7 @@
#define CELERIS_CONFIG_PHYSICS_H
#define LBtype float
#define VIS 0.0056250000
#define VIS 0.0090000000
#define RHO 1.0
#define U0 0.03
@@ -22,18 +22,10 @@ __device__ __forceinline__ void apply_boundary_pull(
bool interior_y = (y > 0u) && (y < (unsigned int)(NY - 1));
if (is_inlet(fl) && interior_y) {
float f_neb[NQ];
unsigned long k_neb = linear_index(x + 1u, y);
for (int i = 0; i < NQ; i++)
f_neb[i] = load_ddf(fi_in, index_f(k_neb, (unsigned int)i));
apply_parabolic_inlet(f, f_neb, (float)y);
apply_inlet_pull_d2q9(f, x, y, fi_in);
}
else if (is_outlet(fl) && interior_y) {
float f_neb[NQ];
unsigned long k_neb = linear_index(x - 1u, y);
for (int i = 0; i < NQ; i++)
f_neb[i] = load_ddf(fi_in, index_f(k_neb, (unsigned int)i));
apply_pressure_outlet(f, f_neb, (float)y);
apply_outlet_pull_d2q9(f, x, y, fi_in);
}
else {
bounce_back_swap(f);
@@ -52,18 +44,10 @@ __device__ __forceinline__ void apply_boundary_pull_3d(
bool interior_y = (y > 0u) && (y < (unsigned int)(NY - 1));
if (is_inlet(fl) && interior_y) {
float f_neb[NQ];
unsigned long k_neb = linear_index(x + 1u, y, z);
for (int i = 0; i < NQ; i++)
f_neb[i] = load_ddf(fi_in, index_f(k_neb, (unsigned int)i));
apply_parabolic_inlet_3d(f, f_neb, (float)y);
apply_inlet_pull_d3q19(f, x, y, z, fi_in);
}
else if (is_outlet(fl) && interior_y) {
float f_neb[NQ];
unsigned long k_neb = linear_index(x - 1u, y, z);
for (int i = 0; i < NQ; i++)
f_neb[i] = load_ddf(fi_in, index_f(k_neb, (unsigned int)i));
apply_pressure_outlet_3d(f, f_neb, (float)y);
apply_outlet_pull_d3q19(f, x, y, z, fi_in);
}
else {
bounce_back_swap(f);
@@ -85,6 +69,7 @@ void OneStep(
uint16_t fl = flag[k];
unsigned long j[NQ];
const bool interior_y = (y > 0u) && (y < (unsigned int)(NY - 1));
compute_neighbors(x, y, j);
float f[NQ];
@@ -108,8 +93,15 @@ void OneStep(
#endif
}
// Collision (fluid only)
if (is_fluid(fl)) {
// Collision
// Normal path: fluid nodes collide.
// Path A repair: when using the local Zou-He inlet, the west inlet nodes are
// still tagged SOLID|BC_INLET ghost nodes, but their post-BC state is later
// used as a pull source for x=1. Leaving that ghost state uncollided was the
// main donor/ghost semantic bug behind the observed inlet blow-ups.
const bool collide_inlet_ghost = is_inlet(fl) && interior_y
&& inlet_scheme_uses_post_collision_ghost();
if (is_fluid(fl) || collide_inlet_ghost) {
float rho_n, ux, uy;
compute_rho_u(f, rho_n, ux, uy);
collide_dispatch(f, rho_n, ux, uy);
@@ -124,6 +116,7 @@ void OneStep(
uint16_t fl = flag[k];
unsigned long j[NQ];
const bool interior_y = (y > 0u) && (y < (unsigned int)(NY - 1));
compute_neighbors(k, j);
float f[NQ];
@@ -138,7 +131,9 @@ void OneStep(
if (is_fluid(fl) && (y == 1u || y == (unsigned int)(NY - 2)))
apply_wall_bb_d3q19_y_pull(y, f, fi_in, k);
if (is_fluid(fl)) {
const bool collide_inlet_ghost = is_inlet(fl) && interior_y
&& inlet_scheme_uses_post_collision_ghost();
if (is_fluid(fl) || collide_inlet_ghost) {
float rho_n, ux, uy, uz;
compute_rho_u(f, rho_n, ux, uy, uz);
collide_dispatch(f, rho_n, ux, uy, uz);
@@ -29,20 +29,10 @@ __device__ __forceinline__ void apply_boundary_esopull(
bool interior_y = (y > 0u) && (y < (unsigned int)(NY - 1));
if (is_inlet(fl) && interior_y) {
unsigned long k_neb = linear_index(x + 1u, y);
unsigned long j_neb[NQ];
compute_neighbors(x + 1u, y, j_neb);
float f_neb[NQ];
load_f_esopull(k_neb, f_neb, fi, j_neb, t);
apply_parabolic_inlet(f, f_neb, (float)y);
apply_inlet_esopull_d2q9(f, x, y, fi, t);
}
else if (is_outlet(fl) && interior_y) {
unsigned long k_neb = linear_index(x - 1u, y);
unsigned long j_neb[NQ];
compute_neighbors(x - 1u, y, j_neb);
float f_neb[NQ];
load_f_esopull(k_neb, f_neb, fi, j_neb, t);
apply_pressure_outlet(f, f_neb, (float)y);
apply_outlet_esopull_d2q9(f, x, y, fi, t);
}
else {
bounce_back_swap(f);
@@ -62,20 +52,10 @@ __device__ __forceinline__ void apply_boundary_esopull_3d(
bool interior_y = (y > 0u) && (y < (unsigned int)(NY - 1));
if (is_inlet(fl) && interior_y) {
unsigned long k_neb = linear_index(x + 1u, y, z);
unsigned long j_neb[NQ];
compute_neighbors(k_neb, j_neb);
float f_neb[NQ];
load_f_esopull(k_neb, f_neb, fi, j_neb, t);
apply_parabolic_inlet_3d(f, f_neb, (float)y);
apply_inlet_esopull_d3q19(f, x, y, z, fi, t);
}
else if (is_outlet(fl) && interior_y) {
unsigned long k_neb = linear_index(x - 1u, y, z);
unsigned long j_neb[NQ];
compute_neighbors(k_neb, j_neb);
float f_neb[NQ];
load_f_esopull(k_neb, f_neb, fi, j_neb, t);
apply_pressure_outlet_3d(f, f_neb, (float)y);
apply_outlet_esopull_d3q19(f, x, y, z, fi, t);
}
else {
bounce_back_swap(f);
@@ -99,6 +79,7 @@ void EsoPullStep(
uint16_t fl = flag[k];
unsigned long j[NQ];
const bool interior_y = (y > 0u) && (y < (unsigned int)(NY - 1));
compute_neighbors(x, y, j);
float f[NQ];
@@ -118,8 +99,13 @@ void EsoPullStep(
if (is_obstacle(fl) && !is_curved(fl))
bounce_back_swap(f);
// Collision (fluid only)
if (is_fluid(fl)) {
// Collision
// Same donor/ghost warning as the double-buffer path: for the local Zou-He
// inlet, the ghost-node state must be regularized before the next pull uses
// it as the source state for the first interior column.
const bool collide_inlet_ghost = is_inlet(fl) && interior_y
&& inlet_scheme_uses_post_collision_ghost();
if (is_fluid(fl) || collide_inlet_ghost) {
float rho_n, ux, uy;
compute_rho_u(f, rho_n, ux, uy);
collide_dispatch(f, rho_n, ux, uy);
@@ -134,6 +120,7 @@ void EsoPullStep(
uint16_t fl = flag[k];
unsigned long j[NQ];
const bool interior_y = (y > 0u) && (y < (unsigned int)(NY - 1));
compute_neighbors(k, j);
float f[NQ];
@@ -151,7 +138,9 @@ void EsoPullStep(
if (is_obstacle(fl) && !is_curved(fl))
bounce_back_swap(f);
if (is_fluid(fl)) {
const bool collide_inlet_ghost = is_inlet(fl) && interior_y
&& inlet_scheme_uses_post_collision_ghost();
if (is_fluid(fl) || collide_inlet_ghost) {
float rho_n, ux, uy, uz;
compute_rho_u(f, rho_n, ux, uy, uz);
collide_dispatch(f, rho_n, ux, uy, uz);