From 909c2d69b1526f41ad058cec2a775218e870fc1c Mon Sep 17 00:00:00 2001 From: Pragyaan Gaur Date: Sun, 29 Nov 2026 10:12:43 +0530 Subject: [PATCH 1/2] Add the P1 radiation with a python heat source tutorial case This adds run.py for the tutorial on the P1 radiation model and PYTHON_CUSTOM_SOURCE. The script heats a square air cavity with an elliptical source set from python and runs with or without the P1 model, depending on the absorption coefficient given as its argument. --- multiphysics/P1_radiation_python/run.py | 155 ++++++++++++++++++++++++ 1 file changed, 155 insertions(+) create mode 100644 multiphysics/P1_radiation_python/run.py diff --git a/multiphysics/P1_radiation_python/run.py b/multiphysics/P1_radiation_python/run.py new file mode 100644 index 0000000..a9c3150 --- /dev/null +++ b/multiphysics/P1_radiation_python/run.py @@ -0,0 +1,155 @@ +#!/usr/bin/env python + +## \file run.py +# \brief Natural convection and P1 radiation in a square air cavity, heated by an +# elliptical volumetric source that is set point by point from python. +# Usage: python run.py ABSORPTION_COEFF (0 switches radiation off). +# \version 8.5.0 "Harrier" +# +# SU2 Project Website: https://su2code.github.io +# +# The SU2 Project is maintained by the SU2 Foundation +# (http://su2foundation.org) +# +# Copyright 2012-2026, SU2 Contributors (cf. AUTHORS.md) +# +# SU2 is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# SU2 is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with SU2. If not, see . + +from mpi4py import MPI +import math +import sys +import pysu2 + +# Cavity and source geometry. +L = 0.05 # cavity side (m) +N = 65 # grid points per side +T_WALL = 300.0 # wall temperature (K) +SOURCE_VAL = 1.0e5 # volumetric heat source (W/m^3) +SOURCE_CENTER = (0.025, 0.02) # center of the ellipse (m) +SOURCE_AXES = (0.01, 0.005) # semi-axes of the ellipse (m) +SOURCE_ROTATION = 30.0 # rotation about z (degrees) +ABSORPTION = float(sys.argv[1]) if len(sys.argv) > 1 else 1.0 # absorption coefficient (1/m) + +settings = f""" +SOLVER= INC_NAVIER_STOKES +INC_NONDIM= DIMENSIONAL +INC_DENSITY_MODEL= VARIABLE +INC_ENERGY_EQUATION= YES +INC_DENSITY_INIT= 1.1766 +INC_VELOCITY_INIT= ( 0.0, 0.0, 0.0 ) +INC_TEMPERATURE_INIT= {T_WALL} +FLUID_MODEL= INC_IDEAL_GAS +SPECIFIC_HEAT_CP= 1004.703 +MOLECULAR_WEIGHT= 28.96 +VISCOSITY_MODEL= CONSTANT_VISCOSITY +MU_CONSTANT= 1.716e-5 +CONDUCTIVITY_MODEL= CONSTANT_CONDUCTIVITY +THERMAL_CONDUCTIVITY_CONSTANT= 0.0257 +BODY_FORCE= YES +BODY_FORCE_VECTOR= ( 0.0, -9.81, 0.0 ) + +RADIATION_MODEL= {"P1" if ABSORPTION > 0 else "NONE"} +ABSORPTION_COEFF= {ABSORPTION} +SCATTERING_COEFF= 0.0 +P1_INITIALIZATION= TEMPERATURE_INIT +MARKER_EMISSIVITY= ( x_minus, 1.0, x_plus, 1.0, y_minus, 1.0, y_plus, 1.0 ) +CFL_NUMBER_RAD= 1e4 + +PYTHON_CUSTOM_SOURCE= YES + +MARKER_ISOTHERMAL= ( x_minus, {T_WALL}, x_plus, {T_WALL}, y_minus, {T_WALL}, y_plus, {T_WALL} ) +MARKER_MONITORING= ( x_minus, x_plus, y_minus, y_plus ) + +MESH_FORMAT= RECTANGLE +MESH_BOX_SIZE= ( {N}, {N}, 0 ) +MESH_BOX_LENGTH= ( {L}, {L}, 0 ) + +NUM_METHOD_GRAD= WEIGHTED_LEAST_SQUARES +CONV_NUM_METHOD_FLOW= FDS +MUSCL_FLOW= YES +SLOPE_LIMITER_FLOW= NONE +TIME_DISCRE_FLOW= EULER_IMPLICIT +CFL_NUMBER= 10 +CFL_ADAPT= YES +CFL_ADAPT_PARAM= ( 0.9, 1.1, 50.0, 1000.0 ) +LINEAR_SOLVER= FGMRES +LINEAR_SOLVER_PREC= ILU +LINEAR_SOLVER_ERROR= 1e-6 +LINEAR_SOLVER_ITER= 20 + +ITER= 8000 +CONV_FIELD= RMS_ENTHALPY +CONV_RESIDUAL_MINVAL= -9 + +SCREEN_OUTPUT= INNER_ITER, RMS_PRESSURE, RMS_ENTHALPY, RMS_RAD_ENERGY, TOTAL_HEATFLUX +SCREEN_WRT_FREQ_INNER= 100 +HISTORY_OUTPUT= ITER, RMS_RES, HEAT +OUTPUT_FILES= RESTART, PARAVIEW +VOLUME_FILENAME= cavity_k{ABSORPTION:g} +""" + + +def HeatSource(x, y): + """SOURCE_VAL inside the rotated ellipse, zero outside (W/m^3).""" + a = math.radians(SOURCE_ROTATION) + dx, dy = x - SOURCE_CENTER[0], y - SOURCE_CENTER[1] + xr = dx * math.cos(a) + dy * math.sin(a) + yr = -dx * math.sin(a) + dy * math.cos(a) + inside = (xr / SOURCE_AXES[0])**2 + (yr / SOURCE_AXES[1])**2 <= 1.0 + return SOURCE_VAL if inside else 0.0 + + +def main(): + comm = MPI.COMM_WORLD + rank = comm.Get_rank() + if rank == 0: + with open("cavity.cfg", "w") as f: + f.write(settings) + comm.Barrier() + + driver = pysu2.CSinglezoneDriver("cavity.cfg", 1, comm) + + iFLOW = driver.GetSolverIndices()["INC.FLOW"] + # The variables are (pressure, velocity, temperature), so the energy equation is last. + iENERGY = driver.GetNumberDimensions() + 1 + nNode = driver.GetNumberNodes() - driver.GetNumberHaloNodes() + coords = driver.Coordinates() + + # The energy equation of the incompressible solver is in W/m^3, so the + # source can be given directly. + source = driver.UserDefinedSource(iFLOW) + for iPoint in range(nNode): + x, y = coords.Get(iPoint)[:2] + source.Set(iPoint, iENERGY, HeatSource(x, y)) + + driver.StartSolver() + + # The solution holds enthalpy, so read the temperature from the primitives. + iTEMP = driver.GetPrimitiveIndices()["TEMPERATURE"] + primitives = driver.Primitives() + t_max = max(primitives.Get(i, iTEMP) for i in range(nNode)) + t_max = comm.allreduce(t_max, op=MPI.MAX) + heat_flux = driver.GetOutputValue("TOTAL_HEATFLUX") + driver.Finalize() + + if rank == 0: + exact = SOURCE_VAL * math.pi * SOURCE_AXES[0] * SOURCE_AXES[1] + print(f"\nAbsorption coefficient: {ABSORPTION} 1/m") + print(f"Maximum temperature: {t_max:.2f} K") + print(f"Heat into the walls: {heat_flux:.3f} W/m") + print(f"Heat from the source: {exact:.3f} W/m (exact ellipse area)") + + +if __name__ == "__main__": + main() From 4dc6dbc9a395fb96a80a66b8eec99ec7d6bdff4b Mon Sep 17 00:00:00 2001 From: Pragyaan Gaur Date: Sun, 29 Nov 2026 20:37:35 +0530 Subject: [PATCH 2/2] Ship cavity.cfg and make the P1 tutorial runnable from tutorials.py The script now reads the config file from its first argument, as TestCase passes it, and takes an optional absorption coefficient as a second argument, for which it writes a copy of the config. The case converges on both the enthalpy and the P1 residual, and the run with an absorption coefficient of 10 1/m is dropped because the segregated coupling makes it converge very slowly. --- multiphysics/P1_radiation_python/cavity.cfg | 108 ++++++++++++++++++ multiphysics/P1_radiation_python/run.py | 118 ++++++++------------ 2 files changed, 152 insertions(+), 74 deletions(-) create mode 100644 multiphysics/P1_radiation_python/cavity.cfg diff --git a/multiphysics/P1_radiation_python/cavity.cfg b/multiphysics/P1_radiation_python/cavity.cfg new file mode 100644 index 0000000..5d2db78 --- /dev/null +++ b/multiphysics/P1_radiation_python/cavity.cfg @@ -0,0 +1,108 @@ +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% % +% SU2 configuration file % +% Case description: Natural convection and P1 radiation in a square air % +% cavity, heated by a volumetric source set from python % +% (see run.py) % +% Author: Pragyaan Gaur % +% File Version 8.5.0 "Harrier" % +% % +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +% ------------- DIRECT, ADJOINT, AND LINEARIZED PROBLEM DEFINITION ------------% +% +SOLVER= INC_NAVIER_STOKES +% +% ---------------- INCOMPRESSIBLE FLOW CONDITION DEFINITION -------------------% +% +INC_NONDIM= DIMENSIONAL +INC_DENSITY_MODEL= VARIABLE +INC_ENERGY_EQUATION= YES +INC_DENSITY_INIT= 1.1766 +INC_VELOCITY_INIT= ( 0.0, 0.0, 0.0 ) +INC_TEMPERATURE_INIT= 300.0 +% +% ---- IDEAL GAS, POLYTROPIC, VAN DER WAALS AND PENG ROBINSON CONSTANTS -------% +% +FLUID_MODEL= INC_IDEAL_GAS +SPECIFIC_HEAT_CP= 1004.703 +MOLECULAR_WEIGHT= 28.96 +% +% --------------------------- VISCOSITY MODEL ---------------------------------% +% +VISCOSITY_MODEL= CONSTANT_VISCOSITY +MU_CONSTANT= 1.716e-5 +% +% --------------------------- THERMAL CONDUCTIVITY MODEL ----------------------% +% +CONDUCTIVITY_MODEL= CONSTANT_CONDUCTIVITY +THERMAL_CONDUCTIVITY_CONSTANT= 0.0257 +% +% ----------------------- BODY FORCE DEFINITION -------------------------------% +% +BODY_FORCE= YES +BODY_FORCE_VECTOR= ( 0.0, -9.81, 0.0 ) +% +% ------------------------- RADIATION MODEL -----------------------------------% +% +% The absorption coefficient gives an optical thickness kL = 0.05 for this +% cavity. Real air is almost transparent, so this value is for demonstration. +RADIATION_MODEL= P1 +ABSORPTION_COEFF= 1.0 +SCATTERING_COEFF= 0.0 +P1_INITIALIZATION= TEMPERATURE_INIT +MARKER_EMISSIVITY= ( x_minus, 1.0, x_plus, 1.0, y_minus, 1.0, y_plus, 1.0 ) +CFL_NUMBER_RAD= 1e4 +% +% ----------------------- PYTHON CUSTOM SOURCE --------------------------------% +% +% The heat source is set point by point in run.py. +PYTHON_CUSTOM_SOURCE= YES +% +% -------------------- BOUNDARY CONDITION DEFINITION --------------------------% +% +MARKER_ISOTHERMAL= ( x_minus, 300.0, x_plus, 300.0, y_minus, 300.0, y_plus, 300.0 ) +MARKER_MONITORING= ( x_minus, x_plus, y_minus, y_plus ) +% +% ------------------------- INPUT/OUTPUT INFORMATION --------------------------% +% +% Structured 65 x 65 mesh of a 0.05 m square, generated by SU2. +MESH_FORMAT= RECTANGLE +MESH_BOX_SIZE= ( 65, 65, 0 ) +MESH_BOX_LENGTH= ( 0.05, 0.05, 0 ) +% +% ------------- COMMON PARAMETERS DEFINING THE NUMERICAL METHOD ---------------% +% +NUM_METHOD_GRAD= WEIGHTED_LEAST_SQUARES +CFL_NUMBER= 10 +CFL_ADAPT= YES +CFL_ADAPT_PARAM= ( 0.9, 1.1, 50.0, 1000.0 ) +% +% ------------------------ LINEAR SOLVER DEFINITION ---------------------------% +% +LINEAR_SOLVER= FGMRES +LINEAR_SOLVER_PREC= ILU +LINEAR_SOLVER_ERROR= 1e-6 +LINEAR_SOLVER_ITER= 20 +% +% -------------------- FLOW NUMERICAL METHOD DEFINITION -----------------------% +% +CONV_NUM_METHOD_FLOW= FDS +MUSCL_FLOW= YES +SLOPE_LIMITER_FLOW= NONE +TIME_DISCRE_FLOW= EULER_IMPLICIT +% +% --------------------------- CONVERGENCE PARAMETERS --------------------------% +% +% The P1 residual stays about four orders above the enthalpy residual. When it +% reaches -6, the wall heat flux has settled to about 0.01 percent. +ITER= 10000 +CONV_FIELD= RMS_ENTHALPY, RMS_RAD_ENERGY +CONV_RESIDUAL_MINVAL= -6 +% +% ------------------------- SCREEN/HISTORY OUTPUT -----------------------------% +% +SCREEN_OUTPUT= INNER_ITER, RMS_PRESSURE, RMS_VELOCITY-X, RMS_VELOCITY-Y, RMS_ENTHALPY, RMS_RAD_ENERGY, TOTAL_HEATFLUX +HISTORY_OUTPUT= ITER, RMS_RES, HEAT +OUTPUT_FILES= RESTART, PARAVIEW +VOLUME_FILENAME= cavity_k1 diff --git a/multiphysics/P1_radiation_python/run.py b/multiphysics/P1_radiation_python/run.py index a9c3150..f5f6d81 100644 --- a/multiphysics/P1_radiation_python/run.py +++ b/multiphysics/P1_radiation_python/run.py @@ -3,7 +3,9 @@ ## \file run.py # \brief Natural convection and P1 radiation in a square air cavity, heated by an # elliptical volumetric source that is set point by point from python. -# Usage: python run.py ABSORPTION_COEFF (0 switches radiation off). +# Usage: python run.py [CONFIG_FILE] [ABSORPTION_COEFF] +# Without arguments it runs cavity.cfg. An absorption coefficient overrides the one +# in the config file, and 0 switches the radiation model off. # \version 8.5.0 "Harrier" # # SU2 Project Website: https://su2code.github.io @@ -31,73 +33,11 @@ import sys import pysu2 -# Cavity and source geometry. -L = 0.05 # cavity side (m) -N = 65 # grid points per side -T_WALL = 300.0 # wall temperature (K) -SOURCE_VAL = 1.0e5 # volumetric heat source (W/m^3) +# Heat source. The cavity itself (0.05 m square, walls at 300 K) is set in cavity.cfg. +SOURCE_VAL = 1.0e5 # volumetric heat source (W/m^3) SOURCE_CENTER = (0.025, 0.02) # center of the ellipse (m) SOURCE_AXES = (0.01, 0.005) # semi-axes of the ellipse (m) -SOURCE_ROTATION = 30.0 # rotation about z (degrees) -ABSORPTION = float(sys.argv[1]) if len(sys.argv) > 1 else 1.0 # absorption coefficient (1/m) - -settings = f""" -SOLVER= INC_NAVIER_STOKES -INC_NONDIM= DIMENSIONAL -INC_DENSITY_MODEL= VARIABLE -INC_ENERGY_EQUATION= YES -INC_DENSITY_INIT= 1.1766 -INC_VELOCITY_INIT= ( 0.0, 0.0, 0.0 ) -INC_TEMPERATURE_INIT= {T_WALL} -FLUID_MODEL= INC_IDEAL_GAS -SPECIFIC_HEAT_CP= 1004.703 -MOLECULAR_WEIGHT= 28.96 -VISCOSITY_MODEL= CONSTANT_VISCOSITY -MU_CONSTANT= 1.716e-5 -CONDUCTIVITY_MODEL= CONSTANT_CONDUCTIVITY -THERMAL_CONDUCTIVITY_CONSTANT= 0.0257 -BODY_FORCE= YES -BODY_FORCE_VECTOR= ( 0.0, -9.81, 0.0 ) - -RADIATION_MODEL= {"P1" if ABSORPTION > 0 else "NONE"} -ABSORPTION_COEFF= {ABSORPTION} -SCATTERING_COEFF= 0.0 -P1_INITIALIZATION= TEMPERATURE_INIT -MARKER_EMISSIVITY= ( x_minus, 1.0, x_plus, 1.0, y_minus, 1.0, y_plus, 1.0 ) -CFL_NUMBER_RAD= 1e4 - -PYTHON_CUSTOM_SOURCE= YES - -MARKER_ISOTHERMAL= ( x_minus, {T_WALL}, x_plus, {T_WALL}, y_minus, {T_WALL}, y_plus, {T_WALL} ) -MARKER_MONITORING= ( x_minus, x_plus, y_minus, y_plus ) - -MESH_FORMAT= RECTANGLE -MESH_BOX_SIZE= ( {N}, {N}, 0 ) -MESH_BOX_LENGTH= ( {L}, {L}, 0 ) - -NUM_METHOD_GRAD= WEIGHTED_LEAST_SQUARES -CONV_NUM_METHOD_FLOW= FDS -MUSCL_FLOW= YES -SLOPE_LIMITER_FLOW= NONE -TIME_DISCRE_FLOW= EULER_IMPLICIT -CFL_NUMBER= 10 -CFL_ADAPT= YES -CFL_ADAPT_PARAM= ( 0.9, 1.1, 50.0, 1000.0 ) -LINEAR_SOLVER= FGMRES -LINEAR_SOLVER_PREC= ILU -LINEAR_SOLVER_ERROR= 1e-6 -LINEAR_SOLVER_ITER= 20 - -ITER= 8000 -CONV_FIELD= RMS_ENTHALPY -CONV_RESIDUAL_MINVAL= -9 - -SCREEN_OUTPUT= INNER_ITER, RMS_PRESSURE, RMS_ENTHALPY, RMS_RAD_ENERGY, TOTAL_HEATFLUX -SCREEN_WRT_FREQ_INNER= 100 -HISTORY_OUTPUT= ITER, RMS_RES, HEAT -OUTPUT_FILES= RESTART, PARAVIEW -VOLUME_FILENAME= cavity_k{ABSORPTION:g} -""" +SOURCE_ROTATION = 30.0 # rotation about z (degrees) def HeatSource(x, y): @@ -110,24 +50,52 @@ def HeatSource(x, y): return SOURCE_VAL if inside else 0.0 +def WriteConfig(cfg_file, absorption): + """Writes a copy of cfg_file with another absorption coefficient and returns its name.""" + new_file = f"cavity_k{absorption:g}.cfg" + overrides = { + "RADIATION_MODEL": "P1" if absorption > 0 else "NONE", + "ABSORPTION_COEFF": f"{absorption:g}", + "CONV_FIELD": "RMS_ENTHALPY, RMS_RAD_ENERGY" if absorption > 0 else "RMS_ENTHALPY", + # Without radiation only the enthalpy residual is checked, so it can go lower. + "CONV_RESIDUAL_MINVAL": "-6" if absorption > 0 else "-9", + "SCREEN_OUTPUT": "INNER_ITER, RMS_PRESSURE, RMS_VELOCITY-X, RMS_VELOCITY-Y, RMS_ENTHALPY" + + (", RMS_RAD_ENERGY" if absorption > 0 else "") + ", TOTAL_HEATFLUX", + "VOLUME_FILENAME": f"cavity_k{absorption:g}", + } + lines = [] + with open(cfg_file) as f: + for line in f: + key = line.split("=")[0].strip() + lines.append(f"{key}= {overrides[key]}\n" if key in overrides and not line.lstrip().startswith("%") else line) + with open(new_file, "w") as f: + f.writelines(lines) + return new_file + + def main(): comm = MPI.COMM_WORLD rank = comm.Get_rank() - if rank == 0: - with open("cavity.cfg", "w") as f: - f.write(settings) - comm.Barrier() - driver = pysu2.CSinglezoneDriver("cavity.cfg", 1, comm) + cfg_file = sys.argv[1] if len(sys.argv) > 1 else "cavity.cfg" + if len(sys.argv) > 2: + new_file = f"cavity_k{float(sys.argv[2]):g}.cfg" + if rank == 0: + WriteConfig(cfg_file, float(sys.argv[2])) + comm.Barrier() + cfg_file = new_file + + # The constructor reads the config file and builds the mesh and the solvers. + driver = pysu2.CSinglezoneDriver(cfg_file, 1, comm) iFLOW = driver.GetSolverIndices()["INC.FLOW"] - # The variables are (pressure, velocity, temperature), so the energy equation is last. + # The variables are (pressure, velocity, enthalpy), so the energy equation is last. iENERGY = driver.GetNumberDimensions() + 1 nNode = driver.GetNumberNodes() - driver.GetNumberHaloNodes() coords = driver.Coordinates() # The energy equation of the incompressible solver is in W/m^3, so the - # source can be given directly. + # source can be given directly. Only the points owned by this rank are set. source = driver.UserDefinedSource(iFLOW) for iPoint in range(nNode): x, y = coords.Get(iPoint)[:2] @@ -140,12 +108,14 @@ def main(): primitives = driver.Primitives() t_max = max(primitives.Get(i, iTEMP) for i in range(nNode)) t_max = comm.allreduce(t_max, op=MPI.MAX) + + # Conductive plus radiative heat flux into the walls in MARKER_MONITORING. heat_flux = driver.GetOutputValue("TOTAL_HEATFLUX") driver.Finalize() if rank == 0: exact = SOURCE_VAL * math.pi * SOURCE_AXES[0] * SOURCE_AXES[1] - print(f"\nAbsorption coefficient: {ABSORPTION} 1/m") + print(f"\nConfig file: {cfg_file}") print(f"Maximum temperature: {t_max:.2f} K") print(f"Heat into the walls: {heat_flux:.3f} W/m") print(f"Heat from the source: {exact:.3f} W/m (exact ellipse area)")