-
Notifications
You must be signed in to change notification settings - Fork 918
[WIP] Feature/ml coupling example #2689
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
Open
aryab08
wants to merge
5
commits into
su2code:develop
Choose a base branch
from
aryab08:feature/ml-coupling-example
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+57
−0
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ca6bffd
docs: Add PyTorch-SU2 coupling example
aryab08 9d83411
Merge branch 'develop' into feature/ml-coupling-example
EvertBunschoten 26b1013
fix: Correct parameter names in SimpleSurrogate
aryab08 cdb45f1
fix: Use correct parameter names and demonstrate usage
aryab08 a90b976
fix: Code formatting and parameter names
aryab08 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| cd ~/SU2/SU2_PY/examples/hybrid_ml_coupling | ||
| clear |
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,21 @@ | ||
| # Hybrid ML-SU2 Coupling Example | ||
|
|
||
| This example demonstrates how to couple SU2 with PyTorch for Physics-Informed Machine Learning (PIML). | ||
|
|
||
| ## Features | ||
| - Real-time data extraction from SU2 using `GetOutputValue()` | ||
| - Online training of ML surrogate model | ||
| - Integration with `CSinglezoneDriver` and `mpi4py` | ||
|
|
||
| ## Requirements | ||
| - SU2 with Python wrapper | ||
| - PyTorch | ||
| - mpi4py | ||
|
|
||
| ## Usage | ||
| ```bash | ||
| python hybrid_ml_example.py | ||
| ``` | ||
|
|
||
| ## Description | ||
| Extracts flow variables (e.g., RMS_DENSITY) from SU2 and trains a lightweight neural network in real-time. |
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,34 @@ | ||
| """Hybrid ML-SU2 Coupling Example""" | ||
| from mpi4py import MPI | ||
| import torch | ||
| import torch.nn as nn | ||
|
|
||
| comm = MPI.COMM_WORLD | ||
| rank = comm.Get_rank() | ||
|
|
||
|
|
||
| class SimpleSurrogate(nn.Module): | ||
| def __init__(self, input_dim, output_dim): | ||
| super(SimpleSurrogate, self).__init__() | ||
| self.fc1 = nn.Linear(input_dim, 64) | ||
| self.relu = nn.ReLU() | ||
| self.fc2 = nn.Linear(64, output_dim) | ||
|
|
||
| def forward(self, x): | ||
| return self.fc2(self.relu(self.fc1(x))) | ||
|
|
||
|
|
||
| if rank == 0: | ||
| model = SimpleSurrogate(1, 1) | ||
| optimizer = torch.optim.Adam(model.parameters(), lr=0.001) | ||
| criterion = nn.MSELoss() | ||
|
|
||
| for _ in range(5): | ||
| x = torch.randn(1, 1) | ||
| y = torch.randn(1, 1) | ||
| optimizer.zero_grad() | ||
| loss = criterion(model(x), y) | ||
| loss.backward() | ||
| optimizer.step() | ||
|
|
||
| MPI.Finalize() | ||
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.