diff --git a/SU2_PY/examples/hy b/SU2_PY/examples/hy new file mode 100644 index 00000000000..87de0fa6f38 --- /dev/null +++ b/SU2_PY/examples/hy @@ -0,0 +1,2 @@ +cd ~/SU2/SU2_PY/examples/hybrid_ml_coupling +clear diff --git a/SU2_PY/examples/hybrid_ml_coupling/README.md b/SU2_PY/examples/hybrid_ml_coupling/README.md new file mode 100644 index 00000000000..e99977241c4 --- /dev/null +++ b/SU2_PY/examples/hybrid_ml_coupling/README.md @@ -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. diff --git a/SU2_PY/examples/hybrid_ml_coupling/hybrid_ml_example.py b/SU2_PY/examples/hybrid_ml_coupling/hybrid_ml_example.py new file mode 100644 index 00000000000..4fc27c684f5 --- /dev/null +++ b/SU2_PY/examples/hybrid_ml_coupling/hybrid_ml_example.py @@ -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()