Datasets:

ArXiv:
License:
File size: 13,277 Bytes
25f269e
 
 
6f2e910
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
---
license: mit
---
# Training Graph Neural Networks for Mesh-based Physics Simulations

## Overview

This repository let's you train Graph Neural Networks on meshes (e.g. fluid dynamics, material simulations, etc).
It is based on the work from different papers:
- [Learning Mesh-Based Simulation with Graph Networks](https://arxiv.org/abs/2010.03409)
- [Multi-Grid Graph Neural Networks with Self-Attention for Computational Mechanics](https://arxiv.org/pdf/2409.11899)
- [MeshMask: Physics Simulations with Masked Graph Neural Networks](https://arxiv.org/pdf/2501.08738)
- [Training Transformers to Simulate Complex Physics](https://arxiv.org/abs/2508.18051)

We offer a simple training script to:
- Setup your model's architecture
- Define your dataset with different augmentation functions
- Follow the training live, including live vizualisations

The code is based on Pytorch, and a JAX extension might follow at some point.

At the moment, the repository supports the following:
- architecture:
  * [x] Mesh Graph Net
  * [x] Transformers
  * [ ] Multigrid
- dataset:
  * [x] matrix based, using .h5
  * [x] .xdmf based (if you have .vtu, .vtk etc, you can easily convert them to .xdmf)
- training methods and augmentations
  * [x] K-hop neighbours 
  * [x] Nodes Masking
  * [x] Augmented Adjacency Matrix
  * [ ] Sub-meshs

Feel free to open a PR if you want to implement a new feature, or an issue to request one.

## Datasets

You can access the 3D Coarse Aneurysm dataset here ! 

### Tutorials

We offer 2 Google colab to showcase training on:
- a Flow past a Cylinder Dataset with message passing
  - [Colab](https://colab.research.google.com/drive/1DVOLrfPPLsjrsC8oq1KaDTIMHxHl1rgH?usp=sharing)
  - dataset is from [Learning Mesh-Based Simulation with Graph Networks](https://arxiv.org/abs/2010.03409)
- a blood flow inside a 3D Aneurysm with Transformers
  - [Colab](https://colab.research.google.com/drive/1csjUx72GPcHzaaBC9z2b7wuxHVrAVsbO?usp=sharing)
  - dataset is from [AnXplore: a comprehensive fluid-structure interaction study of 101 intracranial aneurysms](https://www.frontiersin.org/journals/bioengineering-and-biotechnology/articles/10.3389/fbioe.2024.1433811/full?field&journalName=Frontiers_in_Bioengineering_and_Biotechnology&id=1433811)

## Vizualisations 

We use [Weights and Biases](https://wandb.ai/site) to log most information during training. This includes:
- training and validation loss
  - per step
  - per epoch 
- All Rollout RMSE on validation dataset

We also save:
- Images of ground truth and 1-step prediction for specific indices
  - `LogPyVistaPredictionsCallback(dataset=val_dataset, indices=[1, 2, 3])` in [train.py](https://github.com/DonsetPG/graph-physics/blob/main/graphphysics/train.py)
- Video of ground truth and auto regressive prediction between the first and the last index of the same `indices` list as above
- Meshes of auto regressive prediction as `.xdmf` file for the first trajectory of the validation dataset.

> [!WARNING]  
> If saving thoses meshes takes too much space, you can 1. monitor the disk usage using Weights and Biases, 2. Remove this functionnality in [lightning_module.py](https://github.com/DonsetPG/graph-physics/blob/0c9b6af20a25e7d08f2731efdfe4911f34fbc274/graphphysics/training/lightning_module.py#L154) (see the code below)

https://github.com/DonsetPG/graph-physics/blob/6687b0bafabdd575d2ace6c0e7c39796e1f1624c/graphphysics/training/lightning_module.py#L151-L165

## Setup

### Default requirements

```python
import torch

def format_pytorch_version(version):
  return version.split('+')[0]

TORCH_version = torch.__version__
TORCH = format_pytorch_version(TORCH_version)

def format_cuda_version(version):
  return 'cu' + version.replace('.', '')

CUDA_version = torch.version.cuda
CUDA = format_cuda_version(CUDA_version)
```

```
pip install torch-scatter     -f https://pytorch-geometric.com/whl/torch-{TORCH}+{CUDA}.html
pip install torch-sparse      -f https://pytorch-geometric.com/whl/torch-{TORCH}+{CUDA}.html
pip install torch-cluster     -f https://pytorch-geometric.com/whl/torch-{TORCH}+{CUDA}.html
pip install torch-spline-conv -f https://pytorch-geometric.com/whl/torch-{TORCH}+{CUDA}.html
pip install torch-geometric

pip install loguru==0.7.2
pip install autoflake==2.3.0
pip install pytest==8.0.1
pip install meshio==5.3.5
pip install h5py==3.10.0

!pip install pyvista lightning==2.5.0 wandb "wandb[media]"
!pip install pytorch-lightning==2.5.0 torchmetrics==1.6.3
```

### DGL

You will need to install DGL. You can find information on how to set it up for your environnement [here](https://www.dgl.ai/pages/start.html).

In the case of a google colab, you can use:
```
pip install  dgl -f https://data.dgl.ai/wheels/torch-2.4/cu124/repo.html
```

### WandB

We use Weights and Bias to log most of our metrics and vizualizations during the trainig. Make sure you create and account, and log in before you start training.

```python
import wandb
wandb.login()
```

### Vizualization in Colab

> [!WARNING]  
> Note that if you train inside a notebook, you will need a specific set-up to allow for Pyvista to work

```
apt-get install -qq xvfb
pip install pyvista panel -q
```

and run

```python
import os
os.system('/usr/bin/Xvfb :99 -screen 0 1024x768x24 &')
os.environ['DISPLAY'] = ':99'

import panel as pn
pn.extension('vtk')
```

in the same call as your training.

## Documentation

Most of setting up a new use case depends on two `.json` files: one to define the dataset details, and one for the training settings.

Let's start with the training settings. An exemple is available [here](https://github.com/DonsetPG/graph-physics/blob/main/training_config/cylinder.json).

### Dataset

```json 
"dataset": {
    "extension": "h5",
    "h5_path": "dataset/h5_dataset/cylinder_flow/train.h5",
    "meta_path": "dataset/h5_dataset/cylinder_flow/meta.json",
    "khop": 1
}
```

- `extension`: If the dataset used is h5 or xdmf.
- `h5_path` (`xdmf_folder` for an xdmf dataset): Path to the dataset.

> [!NOTE]  
> You will need a dataset at the same location with `test` instead of `train` in its name for the validation step to work. Otherwise, you can specify its name directly in `training.py`

- `meta_path`: Location to the .json file with the dataset details (see below)
- `khop`: K-hop neighbours size to use. You should start with 1.

You also need to define a few other parameters: 

```json
"index": {
    "feature_index_start": 0,
    "feature_index_end": 2,
    "output_index_start": 0,
    "output_index_end": 2,
    "node_type_index": 2
}
```
- `feature_index_`: This is to define where we should look for nodes features. The end is excluded. For example, if you have 2D velocities at index 0 and 1, and pressure at index 2. If you want to use the pressure you should set  `feature_index_start=0` and  `feature_index_end=3`, otherwise, `feature_index_end=2`.

- `output_index_`: We define our architectures to predict one of your feature for the enxt time steps. So you need to tell us where to look. For example, if you want to predict the velocity at the next step, since the velocity is at index 0 and 1, you will set  `output_index_start=0` and `output_index_end=2`.

- `node_type_index`: Finally, we use a node type classification for each node:

```python
NORMAL = 0
OBSTACLE = 1
AIRFOIL = 2
HANDLE = 3
INFLOW = 4
OUTFLOW = 5
WALL_BOUNDARY = 6
SIZE = 9
```

> [!WARNING]  
> You should modify this if this is not at all representative of your use case. Those are taken from [Meshgraphnet](https://github.com/google-deepmind/deepmind-research/tree/master/meshgraphnets) and we found them to be general enough for all of our use cases. 

This means that you either need to have such feature in your dataset, or to define a python function to build them (see below). After that, you need to tell us where to look. For example, if we only have velocity and node type, we will have  `node_type_index=2`. If we also had the pressure, we would set `node_type_index=3`

> [!WARNING]  
> H5-based dataloader does not support multiple workers. XDMF can.

### Custom Processing Functions

First, we allow you to add noise to your inputs to make the prediction of a trajectory more robust.

```json
"preprocessing": {
    "noise": 0.02,
    "noise_index_start": [0],
    "noise_index_end": [2],
    "masking": 0
},
```

> [!WARNING]  
> Masking is not implemented yet.

```python
def add_noise(
    graph: Data,
    noise_index_start: Union[int, List[int]],
    noise_index_end: Union[int, List[int]],
    noise_scale: Union[float, List[float]],
    node_type_index: int,
) -> Data:
    """
    Adds Gaussian noise to the specified features of the graph's nodes.

    Parameters:
        graph (Data): The graph to modify.
        noise_index_start (Union[int, List[int]]): The starting index or indices for noise addition.
        noise_index_end (Union[int, List[int]]): The ending index or indices for noise addition.
        noise_scale (Union[float, List[float]]): The standard deviation(s) of the Gaussian noise.
        node_type_index (int): The index of the node type feature.

    Returns:
        Data: The modified graph with noise added to node features.
    """
```

Second, in the case of dealing with multiple meshes, you can add extra edges based on closeness of those different meshes:

```json 
"world_pos_parameters": {
    "use": false,
    "world_pos_index_start": 0,
    "world_pos_index_end": 3
}
```

See the [description](https://arxiv.org/abs/2010.03409) regarding world edges.

Finally, in the case where: 
- you need to build the node type 
- you need to build extra features that were not in your dataset

In `train.py`:

```python
# Build preprocessing function
preprocessing = get_preprocessing(
    param=parameters,
    device=device,
    use_edge_feature=use_edge_feature,
    extra_node_features=None,
)
```

where: 

```python
extra_node_features: Optional[
        Union[Callable[[Data], Data], List[Callable[[Data], Data]]]
    ] = None
```

You can define one or several functions that takes a graph as an input, and returns another graph with the new features. 

> [!NOTE]  
> In the case where you might need the previous graph as well (to compute acceleration for example, you can pass `get_previous_data` in the `get_dataset` function, and you will be able to access it using the `previous_data` attribute: `graph.previous_data`)
> You can check [build_features](https://github.com/DonsetPG/graph-physics/blob/main/graphphysics/external/aneurysm.py) where we use `previous_velocity = torch.tensor(graph.previous_data["Vitesse"], device=device)`
> It's important to note to if you do so, those previous data also need to be updated autoregressively during the validation steps. To do so, we added 2 parameters in `train.py`: `previous_data_start` and `previous_data_end`. By default, they are set to 4 and 7. This works if for example, you set the acceleration (computed using the previous velocity) at indexes 4, 5 and 6.

For example, let's imagine we want to add the nodes position as a feature, one could define the following function: 

```python
def add_pos(graph: Data) -> Data:
    graph.x = torch.cat(
        (
            graph.pos,
            graph.x,
        ),
        dim=1,
    )
    return graph
```

<details>
  <summary>In that case, the settings would need to be updated.</summary>
  ```json
  "index": {
      "feature_index_start": 0,
      "feature_index_end": 4,
      "output_index_start": 2,
      "output_index_end": 4,
      "node_type_index": 4
  }
  ```
</details>

You can find more examples regarding adding features and building node type [here](https://github.com/DonsetPG/graph-physics/tree/main/graphphysics/external).

We simply then call the function `add_pos` in `get_preprocessing`:

```python
# Build preprocessing function
preprocessing = get_preprocessing(
    param=parameters,
    device=device,
    use_edge_feature=use_edge_feature,
    extra_node_features=add_pos,
)
```


### Architecture

```json
"model": {
    "type": "transformer",
    "message_passing_num": 5,
    "hidden_size": 32,
    "node_input_size": 2,
    "output_size": 2,
    "edge_input_size": 0,
    "num_heads": 4
}
```

- `type`: Type of the model, either `transformer` or `epd` (message passing)
- `message_passing_num`: Number of Layers
- `hidden_size`: Number of hidden neurons
- `node_input_size`: Number of node features

> [!WARNING]  
> This should not count the node type feature.

- `edge_input_size`: Size of the edge features. 3 in 2D and 4 in 3D. 0 for transformer based model.
- `output_size`: Size of the output
- `num_heads`: Number of heads for transformer based model.

### Dataset Settings

You will also need to design a .json to define the dataset details. Those `meta.json` files are inspired from [Meshgraphnet](https://github.com/google-deepmind/deepmind-research/tree/master/meshgraphnets).

You will need to define: 

- `dt`: the time step of your simulation
- `features`: a set of features used, including at least `cells` and `mesh_pos` for the .h5 dataset.
- `field_names`: the list of all features
- `trajectory_length`: the number of time steps per trajectory

Examples can be found [here](https://github.com/DonsetPG/graph-physics/tree/main/dataset_config).