-
Notifications
You must be signed in to change notification settings - Fork 393
PyTorch Geometric support in MeshGraphNet #995
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Alexey-Kamenev
merged 20 commits into
NVIDIA:main
from
Alexey-Kamenev:pyg-vortex-shedding
Jul 11, 2025
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
0b0799c
Add PyG version of VortexShedding example and VortexSheddingDataset
Alexey-Kamenev dccba37
Replace Union type hints with an alias. Add MeshNodeBlock tests.
Alexey-Kamenev 870cc90
Merge branch 'main' into pyg-vortex-shedding
Alexey-Kamenev b594b98
Add distributed sampler to the example. Add MeshEdgeBlock test. Fix D…
Alexey-Kamenev f75d347
Fix VortexShedding PyG inference script
Alexey-Kamenev c6dbaf3
Add MGN DGL2PYG tests.
Alexey-Kamenev 9fd4e97
Update inference notebooks
Alexey-Kamenev b9877dd
Merge branch 'main' into pyg-vortex-shedding
Alexey-Kamenev e47ef86
Make linter happy.
Alexey-Kamenev 7fa66cd
Fix test.
Alexey-Kamenev 3c7f24f
Merge branch 'main' into pyg-vortex-shedding
Alexey-Kamenev fa51735
Update req.txt. Clean up TODO
Alexey-Kamenev 5b986f4
Merge branch 'main' into pyg-vortex-shedding
Alexey-Kamenev 37d7524
Merge branch 'main' into pyg-vortex-shedding
Alexey-Kamenev f2a89f8
Address review feedback.
Alexey-Kamenev 9645112
Update README
Alexey-Kamenev 3688998
Add proper epoch loss reporting
Alexey-Kamenev 54cdb11
Address review feedback.
Alexey-Kamenev 2b424d7
Require DGL or PyG only when necessary
Alexey-Kamenev b92b52d
Merge branch 'main' into pyg-vortex-shedding
Alexey-Kamenev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 38 additions & 38 deletions
76
examples/cfd/vortex_shedding_mgn/inference_analysis/inference_analysis.ipynb
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
# MeshGraphNet for transient vortex shedding | ||
mnabian marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
> [!IMPORTANT] | ||
> Deprecation Notice | ||
> | ||
> Over the next 2-3 releases, DGL-based functionality will be phased out and replaced | ||
> by equivalent or improved implementations using PyTorch Geometric (PyG). | ||
> PyG will become the default and only supported graph backend. | ||
|
||
This example is a re-implementation of the DeepMind's vortex shedding example | ||
<https://github.com/deepmind/deepmind-research/tree/master/meshgraphnets> in PyTorch. | ||
It demonstrates how to train a Graph Neural Network (GNN) for evaluation of the | ||
transient vortex shedding on parameterized geometries. | ||
|
||
## Problem overview | ||
|
||
Mesh-based simulations play a central role in modeling complex physical systems across | ||
various scientific and engineering disciplines. They offer robust numerical integration | ||
methods and allow for adaptable resolution to strike a balance between accuracy and | ||
efficiency. Machine learning surrogate models have emerged as powerful tools to reduce | ||
the cost of tasks like design optimization, design space exploration, and what-if | ||
analysis, which involve repetitive high-dimensional scientific simulations. | ||
|
||
However, some existing machine learning surrogate models, such as CNN-type models, | ||
are constrained by structured grids, | ||
making them less suitable for complex geometries or shells. The homogeneous fidelity of | ||
CNNs is a significant limitation for many complex physical systems that require an | ||
adaptive mesh representation to resolve multi-scale physics. | ||
|
||
Graph Neural Networks (GNNs) present a viable approach for surrogate modeling in science | ||
and engineering. They are data-driven and capable of handling complex physics. Being | ||
mesh-based, GNNs can handle geometry irregularities and multi-scale physics, | ||
making them well-suited for a wide range of applications. | ||
|
||
## Dataset | ||
|
||
We rely on DeepMind's vortex shedding dataset for this example. The dataset includes | ||
1000 training, 100 validation, and 100 test samples that are simulated using COMSOL | ||
with irregular triangle 2D meshes, each for 600 time steps with a time step size of | ||
0.01s. These samples vary in the size and the position of the cylinder. Each sample | ||
has a unique mesh due to geometry variations across samples, and the meshes have 1885 | ||
nodes on average. Note that the model can handle different meshes with different number | ||
of nodes and edges as the input. | ||
|
||
## Model overview and architecture | ||
|
||
The model is free-running and auto-regressive. It takes the initial condition as the | ||
input and predicts the solution at the first time step. It then takes the prediction at | ||
the first time step to predict the solution at the next time step. The model continues | ||
to use the prediction at time step $t$ to predict the solution at time step $t+1$, until | ||
the rollout is complete. Note that the model is also able to predict beyond the | ||
simulation time span and extrapolate in time. However, the accuracy of the prediction | ||
might degrade over time and if possible, extrapolation should be avoided unless | ||
the underlying data patterns remain stationary and consistent. | ||
|
||
The model uses the input mesh to construct a bi-directional DGL graph for each sample. | ||
The node features include (6 in total): | ||
|
||
- Velocity components at time step $t$, i.e., $u_t$, $v_t$ | ||
- One-hot encoded node type (interior node, no-slip node, inlet node, outlet node) | ||
|
||
The edge features for each sample are time-independent and include (3 in total): | ||
|
||
- Relative $x$ and $y$ distance between the two end nodes of an edge | ||
- L2 norm of the relative distance vector | ||
|
||
The output of the model is the velocity components at time step t+1, i.e., | ||
$u_{t+1}$, $v_{t+1}$, as well as the pressure $p_{t+1}$. | ||
|
||
 | ||
|
||
A hidden dimensionality of 128 is used in the encoder, | ||
processor, and decoder. The encoder and decoder consist of two hidden layers, and | ||
the processor includes 15 message passing layers. Batch size per GPU is set to 1. | ||
Summation aggregation is used in the | ||
processor for message aggregation. A learning rate of 0.0001 is used, decaying | ||
exponentially with a rate of 0.9999991. Training is performed on 8 NVIDIA A100 | ||
GPUs, leveraging data parallelism for 25 epochs. | ||
|
||
## Prerequisites | ||
|
||
This example requires the `tensorflow` library to load the data in the `.tfrecord` | ||
format. | ||
|
||
Note: If installing tensorflow inside the PhysicsNeMo docker container, it's recommended | ||
to use `pip install "tensorflow<=2.17.1"` | ||
|
||
Install the requirements using: | ||
|
||
```bash | ||
pip install -r requirements.txt | ||
pip install dgl -f https://data.dgl.ai/wheels/torch-2.4/cu124/repo.html --no-deps | ||
``` | ||
|
||
## Getting Started | ||
|
||
To download the data from DeepMind's repo, run | ||
|
||
```bash | ||
cd raw_dataset | ||
sh download_dataset.sh cylinder_flow | ||
``` | ||
|
||
To train the model, run | ||
|
||
```bash | ||
python train.py | ||
``` | ||
|
||
Data parallelism is also supported with multi-GPU runs. To launch a multi-GPU training, | ||
run | ||
|
||
```bash | ||
mpirun -np <num_GPUs> python train.py | ||
``` | ||
|
||
If running in a docker container, you may need to include the `--allow-run-as-root` in | ||
the multi-GPU run command. | ||
|
||
Progress and loss logs can be monitored using Weights & Biases. To activate that, | ||
set `wandb_mode` to `online` in the `constants.py`. This requires to have an active | ||
Weights & Biases account. You also need to provide your API key. There are multiple ways | ||
for providing the API key but you can simply export it as an environment variable | ||
|
||
```bash | ||
export WANDB_API_KEY=<your_api_key> | ||
``` | ||
|
||
The URL to the dashboard will be displayed in the terminal after the run is launched. | ||
Alternatively, the logging utility in `train.py` can be switched to MLFlow. | ||
|
||
Once the model is trained, run | ||
|
||
```bash | ||
python inference.py | ||
``` | ||
|
||
This will save the predictions for the test dataset in `.gif` format in the `animations` | ||
directory. | ||
|
||
## References | ||
|
||
- [Learning Mesh-Based Simulation with Graph Networks](https://arxiv.org/abs/2010.03409) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.