| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
Embedding neural networks into dynamic power system simulators
RamsesNN integrates Physics-Informed Neural Networks (PINNs) into the RAMSES time-domain simulator, part of the STEPSS power system simulation platform. Neural networks trained in PyTorch are exported to ONNX, converted to native Fortran with the roseNNa inference library, and embedded in RAMSES as custom injector models, replacing or augmenting traditional power system component models.
Requirements: Python 3.10 with numpy<2, onnx, onnxruntime, CPU torch, and fypp (for ONNX-to-Fortran conversion); Visual Studio with Intel Fortran (oneAPI) to build the included URAMSES and Evaluate_PINN solutions on Windows; the RAMSES library and module files from stepss-uramses (proprietary, not included; see License).
git clone https://github.com/SPS-L/stepss-RamsesNN.gitCreate the Python environment used by the conversion pipeline:
conda create -n roparse python=3.10 -y
conda activate roparse
pip install "numpy<2" onnx==1.15.0 onnxruntime==1.16.3
pip install torch --index-url https://download.pytorch.org/whl/cpu
pip install fyppA full copy of roseNNa is vendored at Evaluate_PINN/roseNNa-master/; you can use it directly or a separate roseNNa checkout.
A ready-made reduced-order machine model is included: URAMSES/rosenna/ ships modelCreator.f90, onnxModel_redv2.txt, onnxWeights_redv2.txt, and the source PINN_redv2.onnx. The full-model files (full_frzd) must be generated from your own trained network.
Train your neural network in PyTorch and export it to ONNX format:
import torch
# Train your model
model = YourNeuralNetwork()
# ... training code ...
# Export to ONNX
init_cond = torch.randn(1, 10) # Example input matching your network's input shape
torch.onnx.export(model, init_cond, "NN.onnx")Work in roseNNa's fLibrary/ directory (e.g. Evaluate_PINN/roseNNa-master/fLibrary/), with the roparse environment active.
ONNX models need shape inference for proper parsing. Create and run a helper script:
Bash (Linux/Mac):
cat > infer_shapes.py << 'EOF'
import onnx, onnx.shape_inference
m = onnx.load('./NN.onnx')
m_inf = onnx.shape_inference.infer_shapes(m)
onnx.save(m_inf, './NN_inferred.onnx')
print('Wrote NN_inferred.onnx')
EOF
python infer_shapes.pyPowerShell (Windows):
$code = @"
import onnx, onnx.shape_inference
m = onnx.load(r'.\NN.onnx')
m_inf = onnx.shape_inference.infer_shapes(m)
onnx.save(m_inf, r'.\NN_inferred.onnx')
print('Wrote NN_inferred.onnx')
"@
Set-Content -Encoding ASCII infer_shapes.py $code
python .\infer_shapes.py# Clean previous files
rm -f onnxModel.txt onnxWeights.txt variables.fpp modelCreator.f90
# Parse ONNX model
python modelParserONNX.py -f "./NN.onnx" -w "./NN.onnx" -i "./NN_inferred.onnx"
# Verify output files were created
ls onnxModel.txt onnxWeights.txt variables.fpp
# Generate Fortran code via fypp preprocessor
python -m fypp ./modelCreator.fpp ./modelCreator.f90
# Fix C++ style namespace syntax for Fortran
sed -i 's/onnx:://g' modelCreator.f90 # Linux/Mac
# OR for Windows PowerShell:
# (Get-Content .\modelCreator.f90) -replace 'onnx::','' | Set-Content .\modelCreator.f90make libraryThis generates libcorelib.a, the core roseNNa library file.
These roseNNa source files must be part of the RAMSES user-model project. They are already included in URAMSES/rosenna/:
Place the generated model files in the RAMSES executable directory:
Note: The reader.f90 in this repository is modified relative to upstream roseNNa: initialize_nnx(model_name) takes a model name and loads the matching onnxModel_<name>.txt / onnxWeights_<name>.txt pair, so multiple networks can coexist.
URAMSES/my_models/inj_norton.f90 demonstrates a complete integration. Key components:
module inj_norton_mod
use iso_fortran_env, only: real64, int32
use MODELING
use rosenna ! Neural network library
implicit none
! Neural network I/O arrays
real(real64), allocatable :: pinn_input(:,:) ! (1, n_in)
real(real64), allocatable :: pinn_output(:,:) ! (1, n_out)case (initialize)
! Define model dimensions (full or reduced)
if (prm(5) < 1.5) then
n_nn_input = 10
n_nn_output = 9
else
n_nn_input = 8
n_nn_output = 7
end if
! Allocate arrays
allocate(pinn_input(1, n_nn_input))
allocate(pinn_output(1, n_nn_output))
! Initialize input with steady-state values
pinn_input(1,1) = 0.01_real64 ! timestep
! ... additional states ...
! Initialize roseNNa with model name
nn_model_name = "full_frzd" ! or "redv2" for reduced model
call initialize_nnx(nn_model_name)case (evaluate_eqs)
! Update inputs based on current system state
call inf_bus_equations(vx, vy, old_ix, old_iy, Xline, Rline, &
new_v_infty, new_v_infty_ang)
pinn_input(1,8) = new_v_infty
pinn_input(1,9) = new_v_infty_ang
! Neural network forward pass
call use_model(pinn_input, pinn_output)
! Convert NN output to physical quantities
call machine_solver(pinn_output(1,1:9), machine_output(1,1:6))
! Transform to xy coordinates
call park_transform_dq_xy(machine_output(1,3), machine_output(1,4), &
pinn_output(1,1), ixnorton, iynorton)Add source files to your project:
Configure module path:
Link library:
See URAMSES/README.rst for the general procedure to build user models against RAMSES with Visual Studio and Intel Fortran.
RamsesNN/ ├── Evaluate_PINN/ # Standalone Fortran console app: test PINN inference outside RAMSES │ └── roseNNa-master/ # Vendored copy of the roseNNa library (MIT) ├── URAMSES/ # RAMSES user-model solution (Visual Studio / Intel Fortran) │ ├── src/ # Model registration files (usr_inj_models.f90, ...) │ ├── my_models/ # User models, incl. inj_norton.f90 (NN-driven Norton injector) │ └── rosenna/ # roseNNa sources + generated model code and weights (redv2) ├── LICENSE └── README.md
roseNNa currently supports:
For small networks typical in physics applications:
When converting LSTMs to ONNX, you need two exports (with and without constant folding):
# Model structure (with optimization)
torch.onnx.export(model, (inp, hidden),
"model_structure.onnx",
export_params=True,
opset_version=12,
do_constant_folding=True,
input_names=['input', 'hidden_state', 'cell_state'],
output_names=['output'])
# Model weights (without optimization)
torch.onnx.export(model, (inp, hidden),
"model_weights.onnx",
export_params=True,
opset_version=12,
do_constant_folding=False,
input_names=['input', 'hidden_state', 'cell_state'],
output_names=['output'])| Document | Description |
|---|---|
| STEPSS platform | Documentation site for the STEPSS simulation platform |
| stepss | Using the compiled RAMSES library from Python |
| URAMSES/README.rst | Building user models with Visual Studio and Intel Fortran |
| roseNNa | Upstream neural network inference library for Fortran/C |
RamsesNN is distributed under the MIT License. See LICENSE. Copyright (c) 2025 Bruno Gelfort.
The MIT grant covers only the code in this repository. It does not extend to its dependencies, which carry their own terms:
The compiled RAMSES library (libramses.lib / libramses.a) and its Fortran module files are not included in this repository: they are proprietary and not redistributable under MIT. To build the URAMSES models here, get them from stepss-uramses and place them in URAMSES/modules/ (Windows/Intel) or URAMSES/modules_lin/ (Linux/gfortran).
RamsesNN was originally developed by Bruno Gelfort (MSc thesis, ETH Zurich, 2025). It is maintained by the Sustainable Power Systems Laboratory (SPS-L) at the Cyprus University of Technology, under the direction of Dr. Petros Aristidou.
Contact: info@sps-lab.org, https://sps-lab.org
| Back | FazBrowse Home | New Git URL |