Skip to content

Commit 5f19c26

Browse files
committed
Update examples and changelog
1 parent 3724f9f commit 5f19c26

File tree

4 files changed

+51
-69
lines changed

4 files changed

+51
-69
lines changed

README.md

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,16 @@
33

44
![A decaying turbulence (McWilliams 1984)](examples/McWilliams2d.svg)
55

6-
## Summary
7-
8-
This repository contains mainly two parts:
9-
10-
### Part I: a native PyTorch port of [Google's Computational Fluid Dynamics package in Jax](https://github.com/google/jax-cfd)
11-
The main changes are documented in the `README.md` under the [`torch_cfd` directory](./torch_cfd/). The most significant changes in all routines include:
6+
## A native PyTorch port of [Google's Computational Fluid Dynamics package in Jax](https://github.com/google/jax-cfd)
7+
This port is a good pedagogical tool to learn how to implement traditional numerical solvers using modern deep learning software interfaces. The main changes are documented in the [`torch_cfd` directory](./torch_cfd/). The most significant changes in all routines include:
128
- Supports for nonhomogenous boundary conditions, many routines in Jax-CFD only work with only periodic boundary.
139
- Routines that rely on the functional programming of Jax have been rewritten to be the PyTorch's tensor-in-tensor-out style, which is arguably more user-friendly to debugging as one can view intermediate values in tensors in VS Code debugger, opposed to Jax's `JaxprTrace`.
1410
- All operations take into consideration the batch dimension of tensors `(b, *, n, m)` regardless of `*` dimension, for example, `(b, T, C, n, m)`, which is similar to PyTorch behavior. In the original Jax-CFD package, only a single trajectory is implemented. The stencil operators are changed to generally operate from the last dimension using negative indexing, following `torch.nn.functional.pad`'s behavior.
1511
- Functions and operators are in general implemented as `nn.Module` like a factory template.
16-
- Jax-CFD's `funcutils.trajectory` function supports tracking only one field variable (vorticity or velocity). in Torch-CFD, extra fields computation and tracking are more accessible and easier for user to add, such as time derivatives $\partial_t\mathbf{u}_h$ and PDE residual $R(\mathbf{u}_h):=\mathbf{f}-\partial_t \mathbf{u}_h-(\mathbf{u}_h\cdot\nabla)\mathbf{u}_h + \nu \Delta \mathbf{u}_h$.
12+
- Jax-CFD's `funcutils.trajectory` function supports tracking only one field variable (vorticity or velocity). In Torch-CFD, extra fields computation and tracking are more accessible and easier for user to add, such as time derivatives $\partial_t\mathbf{u}_h$ and PDE residual $R(\mathbf{u}_h):=\mathbf{f}-\partial_t \mathbf{u}_h-(\mathbf{u}_h\cdot\nabla)\mathbf{u}_h + \nu \Delta \mathbf{u}_h$.
1713

1814

19-
### Part II: Spectral-Refiner: Neural Operator-Assisted Navier-Stokes Equations simulator.
15+
## Neural Operator-Assisted Navier-Stokes Equations simulator.
2016
- The **Spatiotemporal Fourier Neural Operator** (SFNO) is a spacetime tensor-to-tensor learner (or trajectory-to-trajectory), available in the [`fno` directory](./fno). Different components of FNO have been re-implemented keeping the conciseness of the original implementation while allowing modern expansions. We draw inspiration from the [3D FNO in Nvidia's Neural Operator repo](https://github.com/neuraloperator/neuraloperator), [Transformers-based neural operators](https://github.com/thuml/Neural-Solver-Library), as well as Temam's book on functional analysis for the NSE.
2117
- Major architectural changes: learnable spatiotemporal positional encodings, layernorm to replace a hard-coded global Gaussian normalizer, and many others. For more details please see [the documentation of the `SFNO` class](./fno/sfno.py#L485).
2218
- Data generation for the meta-example of the isotropic turbulence in [McWilliams1984]. After the warmup phase, the energy spectra match the inverse cascade of Kolmogorov flow in a periodic box.
@@ -62,9 +58,10 @@ The Apache 2.0 License in the root folder applies to the `torch-cfd` folder of t
6258
## Contributions
6359
PR welcome. Currently, the port of `torch-cfd` currently includes:
6460
- The pseudospectral method for vorticity uses anti-aliasing filtering techniques for nonlinear terms to maintain stability.
65-
- The finite volume method on a MAC grid for velocity, and using the projection scheme to impose the divergence free condition.
66-
- Temporal discretization: Currently only RK4-family of marching schemes uses explicit time-stepping for advection, either implicit or explicit time-stepping for diffusion.
67-
- Boundary conditions: only periodic and Dirichlet boundary conditions.
61+
- The finite volume method on a MAC grids for velocity, and using the projection scheme to impose the divergence free condition.
62+
- Temporal discretization: Currently it has only single-step RK4-family schemes uses explicit time-stepping for advection, either implicit or explicit time-stepping for diffusion.
63+
- Boundary conditions: only periodic and Dirichlet boundary conditions for velocity, Neumann boundary for pressure.
64+
- Solvers: pseudoinverse (either FFT-based or SVD based), Jacobi- or Multigrid V-cycle-preconditioned Conjugate gradient.
6865

6966
## Reference
7067

examples/Kolmogrov2d_rk4_fvm_forced_turbulence.ipynb

Lines changed: 12 additions & 13 deletions
Large diffs are not rendered by default.

examples/Lid-driven_cavity_rk4_fvm.ipynb

Lines changed: 19 additions & 39 deletions
Large diffs are not rendered by default.

torch_cfd/README.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,31 @@
55
- [x] add native FFT-based vorticity computation, instead of taking finite differences for pseudo-spectral (added in 0.0.4).
66
- [x] add native PyTorch implementation for applying `torch.linalg` and `torch.fft` function directly on `GridArray` and `GridVariable` (added 0.1.0).
77
- [x] add no-slip boundary (added in 0.2.2).
8+
- [ ] rewrite `shift` and `pad` using `torch.roll`.
89
- [ ] support for function-valued boundary conditions.
10+
- [ ] change the multigrid implementation using convolution kernel.
911

1012
# Changelog
1113

14+
### 0.2.4
15+
- Added native implementation of Conjugate gradient, Gauss-Seidel smoothers, and a hard-coded implementation of multigrid in `solvers.py`. Reference: [Long Chen's notes on finite difference methods](https://www.math.uci.edu/~chenlong/226/FDMcode.pdf) and [programming multigrid method on MAC grids](https://www.math.uci.edu/~chenlong/226/MACcode.pdf).
16+
- Added `test_solvers.py`.
17+
1218
### 0.2.3
13-
- Major fixes: Jax-CFD routines that does not work for non-homogeneous boundary conditions are rewritten:
19+
- Major fixes: Jax-CFD routines that do not work for non-homogeneous boundary conditions are rewritten:
1420
- removed wrapping $\partial v/\partial t$ with $v$'s boundary condition (`explicit_terms_with_same_bcs` routine, which is wrong for nonhomogeneous bcs).
1521
- changed `pad` function to work with tuple padding inputs.
1622
- fixed `pad` behavior on `offset==1.5` functions.
17-
- fixed `bc.pad_all` behavior.`
18-
- added a [`_symmetric_pad_tensor`](./grids.py#1348) function to match the behavior of `np.pad` with mode `symmetric` (symmetric padding across the boundary, not just mirror).
19-
- changd the behavior of `pad_and_impose_bc` in `BoundaryCondition` class to correctly impose bc when ghost cells have to be presented, and added some tests.
20-
- advection module is completely refactored to as `nn.Module`, tests added for advection.
23+
- fixed `bc.pad_all` behavior.
24+
- added a [`_symmetric_pad_tensor`](./grids.py#1348) function in PyTorch to match the behavior of `np.pad` with mode `symmetric` (symmetric padding across the boundary, not just mirror).
25+
- changed the behavior of `pad_and_impose_bc` in `BoundaryCondition` class to correctly impose bc when ghost cells have to be presented, and added some tests.
26+
- `torch_cfd.advection` module is completely refactored to as `nn.Module`, tests added for advection.
2127
- added `.norm` property and `__getitem__` for a `GridVariable`.
2228
- added `__repr__` for `Grid` and `GridVariable` for neater format when being printed.
2329

2430
### 0.2.0
2531

26-
After version `0.1.0`, I began prompt with existing codes in VSCode Copilot (using the OpenAI Enterprise API kindedly provided by UM), which arguably significantly improve the "porting->debugging->refactoring" cycle. I recorded some several good refactoring suggestions by GPT o4-mini and some by ***Claude Sonnet 3.7*** here. There were definitely over-complicated "poor" refactoring suggestions, which have been stashed after benchmarking. I found that Sonnet 3.7 is exceptionally good at providing templates for me to filling the details, when it is properly prompted with details of the functionality of current codes. Another highlight is that, based on the error or exception raised in the unittests, Sonnet 3.7 directly added configurations in `.vscode/launch.json`, saving me quite some time of copy-paste boilerplates then change by hand.
32+
After version `0.1.0`, I began prompt with existing codes in VSCode Copilot (using the OpenAI Enterprise API kindly provided by UM), which arguably significantly improve the "porting->debugging->refactoring" cycle. I recorded some several good refactoring suggestions by GPT o4-mini and some by ***Claude Sonnet 3.7*** here. There were definitely over-complicated "poor" refactoring suggestions, which have been stashed after benchmarking. I found that Sonnet 3.7 is exceptionally good at providing templates for me to filling the details, when it is properly prompted with details of the functionality of current codes. Another highlight is that, based on the error or exception raised in the unittests, Sonnet 3.7 directly added configurations in `.vscode/launch.json`, saving me quite some time of copy-paste boilerplates then change by hand.
2733

2834
#### Major change: batch dimension for FVM
2935
The finite volume solver now accepts the batch dimension, some key updates include

0 commit comments

Comments
 (0)