|
| 1 | +--- |
| 2 | +title: P1 Radiation with a Python Heat Source |
| 3 | +permalink: /tutorials/P1_radiation_python/ |
| 4 | +written_by: Pragyaan Gaur |
| 5 | +for_version: 8.5.0 |
| 6 | +revised_by: |
| 7 | +revision_date: |
| 8 | +revised_version: |
| 9 | +solver: INC_NAVIER_STOKES |
| 10 | +requires: SU2_CFD, python |
| 11 | +complexity: intermediate |
| 12 | +--- |
| 13 | + |
| 14 | + |
| 15 | +Figure (1): Temperature and velocity direction in an air cavity heated by an elliptical source, without radiation (left) and with the P1 model at an absorption coefficient of 10 1/m (right). |
| 16 | + |
| 17 | +## Goals |
| 18 | + |
| 19 | +In this tutorial we heat a closed air cavity with a volumetric heat source that has the shape of a rotated ellipse, and we let the heat leave by natural convection, conduction and thermal radiation. The heat source is applied from python, and the radiation is modelled with the P1 model of SU2. We will cover the following topics. |
| 20 | + |
| 21 | +- Running SU2 from python with the python wrapper. |
| 22 | +- Applying a volumetric heat source of any shape to the energy equation with `PYTHON_CUSTOM_SOURCE`. |
| 23 | +- Setting up the P1 radiation model for the incompressible solver. |
| 24 | +- Reading the solution back in python and checking the energy balance. |
| 25 | + |
| 26 | +## Resources |
| 27 | + |
| 28 | +The resources for this tutorial can be found in the [P1_radiation_python](https://github.com/su2code/Tutorials/tree/develop/multiphysics/P1_radiation_python) directory in the [tutorial repository](https://github.com/su2code/Tutorials). You only need the python script [run.py](https://github.com/su2code/Tutorials/tree/develop/multiphysics/P1_radiation_python/run.py). The script writes its own configuration file, and the mesh is generated by SU2 at run time, so there is no mesh file to download. |
| 29 | + |
| 30 | +## Background |
| 31 | + |
| 32 | +### The P1 radiation model |
| 33 | + |
| 34 | +The P1 model is the lowest order spherical harmonics approximation of the radiative transfer equation. It replaces the full angular intensity field with a single scalar, the incident radiation $$G$$, which obeys a diffusion equation, |
| 35 | + |
| 36 | +$$ \nabla \cdot \left( \frac{1}{3(\kappa + \sigma_s)} \nabla G \right) = \kappa \left( G - 4 \sigma T^4 \right), $$ |
| 37 | + |
| 38 | +where $$\kappa$$ is the absorption coefficient, $$\sigma_s$$ is the scattering coefficient and $$\sigma$$ is the Stefan-Boltzmann constant. The right hand side is the net radiative exchange with the gas. It enters the energy equation of the flow as a source term, so a hot region of gas loses heat by emission and a cold region gains heat by absorption. At walls, SU2 uses the Marshak boundary condition, which depends on the wall emissivity. |
| 39 | + |
| 40 | +The P1 model works best when the medium is optically thick, which means that the optical thickness $$\kappa L$$ is of order one or larger. For a very thin medium it overpredicts the radiative exchange, so it should be used with care there. |
| 41 | + |
| 42 | +### Custom sources from python |
| 43 | + |
| 44 | +With `PYTHON_CUSTOM_SOURCE= YES`, every solver keeps a field of user defined sources with one value per point and per equation. The python wrapper gives direct access to this field. SU2 multiplies each value by the volume of the control volume and adds it to the residual, so the value has the units of the equation per unit volume. For the energy equation of the incompressible solver this is W/m^3, so a heat source can be given directly in W/m^3. |
| 45 | + |
| 46 | +This makes it possible to add a heat source of any shape, or one that changes in time or depends on the solution, without changing the C++ code. Here we use an ellipse rotated about the z axis, but the same approach works for a source read from a file or computed by another code. |
| 47 | + |
| 48 | +## Problem Setup |
| 49 | + |
| 50 | +The domain is a square cavity of side 0.05 m filled with air at 300 K. All four walls are held at 300 K and have an emissivity of 1. Gravity acts in the negative y direction. Inside the cavity there is a heat source of 1e5 W/m^3 in an ellipse with semi-axes of 10 mm and 5 mm, centered at (25, 20) mm and rotated by 30 degrees. Its total strength is $$q \pi a b = 15.71$$ W per metre of depth. |
| 51 | + |
| 52 | +We run the case three times, with no radiation and with the P1 model at absorption coefficients of 1 and 10 1/m. These correspond to optical thicknesses of 0.05 and 0.5 based on the cavity side. |
| 53 | + |
| 54 | +### Compiling SU2 with the python wrapper |
| 55 | + |
| 56 | +SU2 has to be compiled with python support. Add the option `-Denable-pywrapper=true` to the meson setup. With MPI support this becomes |
| 57 | + |
| 58 | +```bash |
| 59 | +./meson.py setup build -Denable-pywrapper=true -Dwith-mpi=enabled --prefix=/path/to/install |
| 60 | +./ninja -C build install |
| 61 | +``` |
| 62 | + |
| 63 | +The python wrapper needs `swig` and, for MPI, the `mpi4py` package built against the same MPI library as SU2. After installing, add the `bin` folder of the install to both `PATH` and `PYTHONPATH`, so that `import pysu2` works. |
| 64 | + |
| 65 | +## Configuration File Options |
| 66 | + |
| 67 | +The script writes the configuration file `cavity.cfg` from a string at the top of `run.py`. The options for the flow are the same as in other incompressible natural convection cases. The energy equation is switched on and the density follows the ideal gas law, so that the buoyancy is driven by the temperature differences. |
| 68 | + |
| 69 | +``` |
| 70 | +SOLVER= INC_NAVIER_STOKES |
| 71 | +INC_NONDIM= DIMENSIONAL |
| 72 | +INC_DENSITY_MODEL= VARIABLE |
| 73 | +INC_ENERGY_EQUATION= YES |
| 74 | +FLUID_MODEL= INC_IDEAL_GAS |
| 75 | +BODY_FORCE= YES |
| 76 | +BODY_FORCE_VECTOR= ( 0.0, -9.81, 0.0 ) |
| 77 | +``` |
| 78 | + |
| 79 | +The radiation model is set up with the following options. |
| 80 | + |
| 81 | +``` |
| 82 | +RADIATION_MODEL= P1 |
| 83 | +ABSORPTION_COEFF= 1.0 |
| 84 | +SCATTERING_COEFF= 0.0 |
| 85 | +P1_INITIALIZATION= TEMPERATURE_INIT |
| 86 | +MARKER_EMISSIVITY= ( x_minus, 1.0, x_plus, 1.0, y_minus, 1.0, y_plus, 1.0 ) |
| 87 | +CFL_NUMBER_RAD= 1e4 |
| 88 | +``` |
| 89 | + |
| 90 | +`RADIATION_MODEL= P1` adds the P1 solver to the incompressible solver. `ABSORPTION_COEFF` and `SCATTERING_COEFF` are the coefficients in the equation above, in 1/m. `P1_INITIALIZATION= TEMPERATURE_INIT` starts the incident radiation from its equilibrium value $$4 \sigma T^4$$ at the initial temperature, which is a better start than zero. Every wall needs an emissivity in `MARKER_EMISSIVITY`. `CFL_NUMBER_RAD` sets the pseudo time step of the radiation equation. The P1 equation is a diffusion equation, so a high value is fine. |
| 91 | + |
| 92 | +The custom source is switched on with a single option. |
| 93 | + |
| 94 | +``` |
| 95 | +PYTHON_CUSTOM_SOURCE= YES |
| 96 | +``` |
| 97 | + |
| 98 | +The mesh is a structured grid of 65 by 65 points generated by SU2 itself. Its boundaries are called `x_minus`, `x_plus`, `y_minus` and `y_plus`. |
| 99 | + |
| 100 | +``` |
| 101 | +MESH_FORMAT= RECTANGLE |
| 102 | +MESH_BOX_SIZE= ( 65, 65, 0 ) |
| 103 | +MESH_BOX_LENGTH= ( 0.05, 0.05, 0 ) |
| 104 | +``` |
| 105 | + |
| 106 | +`TOTAL_HEATFLUX` is the conductive heat flux into the walls listed in `MARKER_MONITORING`. We print it on screen to check the energy balance. |
| 107 | + |
| 108 | +## The Python Script |
| 109 | + |
| 110 | +After the configuration file is written, the script creates the driver. The constructor reads the configuration and builds the mesh and the solvers, but it does not iterate yet. |
| 111 | + |
| 112 | +```python |
| 113 | +driver = pysu2.CSinglezoneDriver("cavity.cfg", 1, comm) |
| 114 | +``` |
| 115 | + |
| 116 | +Next we find the flow solver and the index of the energy equation. The incompressible solver has the variables (pressure, velocity, enthalpy), so in 2D the energy equation has index 3. |
| 117 | + |
| 118 | +```python |
| 119 | +iFLOW = driver.GetSolverIndices()["INC.FLOW"] |
| 120 | +iENERGY = driver.GetNumberDimensions() + 1 |
| 121 | +nNode = driver.GetNumberNodes() - driver.GetNumberHaloNodes() |
| 122 | +coords = driver.Coordinates() |
| 123 | +``` |
| 124 | + |
| 125 | +The source is set point by point. The function `HeatSource` returns the source strength at a point, which is the source value inside the rotated ellipse and zero outside. Only the points owned by this rank are set, so the script also runs in parallel. |
| 126 | + |
| 127 | +```python |
| 128 | +source = driver.UserDefinedSource(iFLOW) |
| 129 | +for iPoint in range(nNode): |
| 130 | + x, y = coords.Get(iPoint)[:2] |
| 131 | + source.Set(iPoint, iENERGY, HeatSource(x, y)) |
| 132 | +``` |
| 133 | + |
| 134 | +The source does not change in this case, so it is set once before the solver starts. For a source that depends on time or on the solution, the same loop can be put inside a time loop, between calls to `driver.Preprocess`, `driver.Run` and `driver.Postprocess`. |
| 135 | + |
| 136 | +```python |
| 137 | +driver.StartSolver() |
| 138 | +``` |
| 139 | + |
| 140 | +After the run, the script reads the temperature from the primitive variables and the wall heat flux from the screen output. Note that the solution vector holds the enthalpy, so the temperature has to be read from the primitives. |
| 141 | + |
| 142 | +```python |
| 143 | +iTEMP = driver.GetPrimitiveIndices()["TEMPERATURE"] |
| 144 | +primitives = driver.Primitives() |
| 145 | +t_max = max(primitives.Get(i, iTEMP) for i in range(nNode)) |
| 146 | +heat_flux = driver.GetOutputValue("TOTAL_HEATFLUX") |
| 147 | +``` |
| 148 | + |
| 149 | +## Running SU2 |
| 150 | + |
| 151 | +The absorption coefficient is given as the argument of the script, and 0 switches the radiation model off. |
| 152 | + |
| 153 | +```bash |
| 154 | +python run.py 0 |
| 155 | +python run.py 1 |
| 156 | +python run.py 10 |
| 157 | +``` |
| 158 | + |
| 159 | +The script can also run in parallel with MPI. |
| 160 | + |
| 161 | +```bash |
| 162 | +mpirun -n 4 python run.py 10 |
| 163 | +``` |
| 164 | + |
| 165 | +Each run takes a minute or two on a single core of a laptop. |
| 166 | + |
| 167 | +## Results |
| 168 | + |
| 169 | +The table below shows the results of the three runs. |
| 170 | + |
| 171 | +| Absorption coefficient (1/m) | Maximum temperature (K) | Conductive heat into the walls (W/m) | Share of the source | |
| 172 | +|---|---|---|---| |
| 173 | +| 0 (no radiation) | 325.7 | 15.81 | 100.6 % | |
| 174 | +| 1 | 324.6 | 14.96 | 95.2 % | |
| 175 | +| 10 | 319.6 | 10.91 | 69.5 % | |
| 176 | + |
| 177 | +Without radiation, all the heat from the source has to leave by conduction through the walls. The conductive heat flux is 15.81 W/m, against 15.71 W/m for the exact ellipse. The small difference comes from sampling the source at the mesh points. A point either lies inside the ellipse or it does not, so the discrete ellipse has a slightly different area from the exact one. It becomes smaller on a finer mesh. |
| 178 | + |
| 179 | +With radiation, the conductive heat flux into the walls drops, because part of the heat now leaves the gas as radiation and is absorbed by the walls. `TOTAL_HEATFLUX` only contains the conductive part, so the difference with the source strength is the radiative part. At an absorption coefficient of 1 1/m, about 5 percent of the heat leaves by radiation. At 10 1/m this rises to about 30 percent, and the hot plume above the source is clearly cooler, as figure (1) shows. The flow pattern itself, with a plume rising above the source and two counter-rotating cells, hardly changes. |
| 180 | + |
| 181 | +## Going further |
| 182 | + |
| 183 | +- Change the shape, position or strength of the source in `HeatSource`. Any function of the coordinates works. |
| 184 | +- Make the source depend on time, by setting it inside a time loop in an unsteady run. |
| 185 | +- Make the source depend on the solution, for example to model a heater that switches off above a set temperature, by reading `driver.Primitives()` before setting the source. |
| 186 | +- The same `PYTHON_CUSTOM_SOURCE` mechanism works for the heat equation solver (`SOLVER= HEAT_EQUATION`). There the energy equation is divided by $$\rho c_p$$, so the source has to be divided by $$\rho c_p$$ as well. The test case `TestCases/py_wrapper/custom_heat_source_steady` in the SU2 repository shows how. |
0 commit comments