Skip to content

Commit 5a7c9df

Browse files
committed
fixed the behavior of impose_bc for 1.5 offset
1 parent 56c4118 commit 5a7c9df

File tree

8 files changed

+484
-99
lines changed

8 files changed

+484
-99
lines changed

torch_cfd/advection.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ def __init__(
225225
grid: Grid,
226226
bcs_c: Tuple[boundaries.BoundaryConditions, ...],
227227
bcs_v: Tuple[boundaries.BoundaryConditions, ...],
228-
offsets: Tuple[Tuple[float, ...], ...] = ((1.5, 0.5), (0.5, 1.5)),
228+
offsets: Tuple[Tuple[float, ...], ...] = ((1.0, 0.5), (0.5, 1.0)),
229229
**kwargs,
230230
):
231231
super().__init__()
@@ -281,10 +281,9 @@ def forward(self, cs: GridVariableVector, v: GridVariableVector) -> GridVariable
281281
# flux's bc will become None
282282
flux = GridVariableVector(tuple(c * u for c, u in zip(cs, v)))
283283

284-
# Apply boundary conditions to flux if not periodic
285-
flux = GridVariableVector(
286-
tuple(bc.impose_bc(f) for f, bc in zip(flux, self.flux_bcs))
287-
)
284+
# wrap flux with boundary conditions to flux if not periodic
285+
flux = GridVariableVector(tuple(GridVariable(f.data, offset, f.grid, bc) for f, offset, bc in zip(flux, self.offsets, self.flux_bcs)))
286+
288287

289288
# Return negative divergence of flux
290289
# after taking divergence the bc becomes None
@@ -642,13 +641,14 @@ def __init__(
642641
boundaries.periodic_boundary_conditions(ndim=2),
643642
boundaries.periodic_boundary_conditions(ndim=2),
644643
),
644+
advect: type[nn.Module] = AdvectionVanLeer,
645645
limiter: Callable = van_leer_limiter,
646646
**kwargs,
647647
):
648648
super().__init__()
649649

650650
self.advect = nn.ModuleList(
651-
AdvectionVanLeer(
651+
advect(
652652
grid=grid,
653653
offset=offset,
654654
bc_c=bc,

torch_cfd/fvm.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,11 @@ def forward(
186186
187187
Returns:
188188
Updated velocity field after one time step
189+
190+
Port note:
191+
- In Jax-CFD, dvdt is wrapped with the same bc with v,
192+
which does not work for inhomogeneous boundary condition.
193+
see explicit_terms_with_same_bcs in jax_cfd/base/equation.py
189194
"""
190195
alpha = self.params["a"]
191196
beta = self.params["b"]
@@ -206,6 +211,7 @@ def forward(
206211
if alpha[i - 1][j] != 0:
207212
u_star = u_star + dt * alpha[i - 1][j] * k[j]
208213

214+
u_star = wrap_field_same_bcs(u_star, u0)
209215
u[i], _ = equation.pressure_projection(u_star)
210216
k[i] = equation.explicit_terms(u[i], dt)
211217

@@ -214,8 +220,10 @@ def forward(
214220
if beta[j] != 0:
215221
u_star = u_star + dt * beta[j] * k[j]
216222

223+
u_star = wrap_field_same_bcs(u_star, u0)
217224
u_final, p = equation.pressure_projection(u_star)
218225

226+
u_final = wrap_field_same_bcs(u_final, u0)
219227
return u_final, p
220228

221229

@@ -309,9 +317,8 @@ def _explicit_terms(self, v, dt, **kwargs):
309317
dv_dt += self._diffusion(v)
310318
if forcing is not None:
311319
dv_dt += GridVariableVector(forcing(grid, v)) / density
312-
dv_dt = wrap_field_same_bcs(dv_dt, v)
313320
if self.drag > 0.0:
314-
dv_dt += -self.drag * v
321+
dv_dt += -self.drag * v.array
315322
return dv_dt
316323

317324
def explicit_terms(self, *args, **kwargs):

torch_cfd/grids.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -320,11 +320,7 @@ def trim_boundary(
320320
"trim_boundary() not implemented in BoundaryConditions base class."
321321
)
322322

323-
def impose_bc(
324-
self,
325-
u: GridVariable,
326-
mode: Optional[str] = ""
327-
) -> GridVariable:
323+
def impose_bc(self, u: GridVariable, mode: Optional[str] = "") -> GridVariable:
328324
"""Impose boundary conditions on the grid variable."""
329325
raise NotImplementedError(
330326
"impose_bc() not implemented in BoundaryConditions base class."
@@ -537,7 +533,7 @@ def __post_init__(self):
537533

538534
def __repr__(self) -> str:
539535
lines = [f"GridVariable:"]
540-
lines.append(f"data tensor: \n{self.data.detach().numpy()}")
536+
lines.append(f"data tensor: \n{self.data.cpu().detach().numpy()}")
541537
lines.append(f"data shape: {tuple(s for s in self.data.shape)}")
542538
lines.append(f"offset: {self.offset}")
543539
lines.append(f"grid shape: {self.grid.shape}")
@@ -780,7 +776,7 @@ def interior(self) -> GridVariable:
780776
interior_grid = self._interior_grid()
781777
return GridVariable(interior_array, self.offset, interior_grid)
782778

783-
def impose_bc(self, mode: str="") -> GridVariable:
779+
def impose_bc(self, mode: str = "") -> GridVariable:
784780
"""Returns the GridVariable with edge BC enforced, if applicable.
785781
786782
For GridVariables having nonperiodic BC and offset 0 or 1, there are values
@@ -1037,7 +1033,7 @@ def pad(
10371033
Padding.EXTEND,
10381034
Padding.SYMMETRIC,
10391035
], f"Padding mode must be one of ['{Padding.MIRROR}', '{Padding.EXTEND}', '{Padding.SYMMETRIC}'], got '{mode}'"
1040-
bc = bc if bc is not None else u.bc
1036+
bc = bc if bc is not None else u.bc # use bc in priority
10411037
bc_types = bc.types[dim] if bc_types is None else bc_types
10421038
values = bc.bc_values if values is None else values
10431039
if isinstance(width, int):
@@ -1086,11 +1082,16 @@ def pad(
10861082
if math.isclose(u.offset[dim] % 1, 0.5): # cell center
10871083
# make the linearly interpolated value equal to the boundary by setting
10881084
# the padded values to the negative symmetric values
1085+
# on the left side if u.offset is either 0.5 or 1.5, width = -1
1086+
# then (u - bc_val)/u.offset = (u_padded - bc_val)/(u.offset + width)
1087+
# here the implemenation assumes that the left pad and the right pad widths are the same when u.offset == 0.5 and only left needs to be padded when u.offset == 1.5
1088+
10891089
if any(p > 1 for p in padding):
10901090
mode = Padding.SYMMETRIC
1091-
data = 2 * expand_dims_pad(
1091+
_alpha = 1 / u.offset[dim]
1092+
data = _alpha * expand_dims_pad(
10921093
u.data, full_padding, mode="constant", constant_values=values
1093-
) - expand_dims_pad(u.data, full_padding, mode=mode)
1094+
) + (1 - _alpha) * expand_dims_pad(u.data, full_padding, mode=mode)
10941095
return GridVariable(data, tuple(new_offset), u.grid, bc)
10951096
elif math.isclose(u.offset[dim] % 1, 0): # cell edge
10961097
# First the value on

0 commit comments

Comments
 (0)