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 new file mode 100644 index 0000000..f5f6d81 --- /dev/null +++ b/multiphysics/P1_radiation_python/run.py @@ -0,0 +1,125 @@ +#!/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 [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 +# +# 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 + +# 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) + + +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 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() + + 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, 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. Only the points owned by this rank are set. + 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) + + # 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"\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)") + + +if __name__ == "__main__": + main()