diff --git a/_data/tutorials.yml b/_data/tutorials.yml index 31a2fc1a..e0bc5163 100644 --- a/_data/tutorials.yml +++ b/_data/tutorials.yml @@ -54,6 +54,7 @@ - Inc_Laminar_Diffusion_Flame - Inc_Combustion - TFC_python + - P1_radiation_python - title: Design Features tutorials: diff --git a/_tutorials/multiphysics/P1_radiation_python/P1_radiation_python.md b/_tutorials/multiphysics/P1_radiation_python/P1_radiation_python.md new file mode 100644 index 00000000..f796ef84 --- /dev/null +++ b/_tutorials/multiphysics/P1_radiation_python/P1_radiation_python.md @@ -0,0 +1,201 @@ +--- +title: P1 Radiation with a Python Heat Source +permalink: /tutorials/P1_radiation_python/ +written_by: Pragyaan Gaur +for_version: 8.5.0 +revised_by: +revision_date: +revised_version: +solver: INC_NAVIER_STOKES +requires: SU2_CFD, python +complexity: intermediate +--- + +![P1 cavity temperature](../../tutorials_files/multiphysics/P1_radiation_python/images/cavity_temperature.png) +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 1 1/m (middle). The right panel shows how much the radiation cools the gas. + +## Goals + +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. + +- Running SU2 from python with the python wrapper. +- Applying a volumetric heat source of any shape to the energy equation with `PYTHON_CUSTOM_SOURCE`. +- Setting up the P1 radiation model for the incompressible solver. +- Reading the solution back in python and checking the energy balance. + +## Resources + +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 need the python script [run.py](https://github.com/su2code/Tutorials/tree/develop/multiphysics/P1_radiation_python/run.py) and the configuration file [cavity.cfg](https://github.com/su2code/Tutorials/tree/develop/multiphysics/P1_radiation_python/cavity.cfg). The mesh is generated by SU2 at run time, so there is no mesh file to download. + +## Background + +### The P1 radiation model + +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, + +$$ \nabla \cdot \left( \frac{1}{3(\kappa + \sigma_s)} \nabla G \right) = \kappa \left( G - 4 \sigma T^4 \right), $$ + +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. + +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. + +### Custom sources from python + +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. + +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. + +## Problem Setup + +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. + +We run the case twice, without radiation and with the P1 model at an absorption coefficient of 1 1/m, which gives an optical thickness $$\kappa L = 0.05$$ based on the cavity side. This value is for demonstration only. Real air is almost transparent to thermal radiation, and the absorption comes from gases such as water vapour and carbon dioxide, or from soot and particles. The P1 model is also at its least accurate in such a thin medium, as noted above. For a real application, take the absorption coefficient from a model of the gas mixture. + +### Compiling SU2 with the python wrapper + +SU2 has to be compiled with python support. Add the option `-Denable-pywrapper=true` to the meson setup. With MPI support this becomes + +```bash +./meson.py setup build -Denable-pywrapper=true -Dwith-mpi=enabled --prefix=/path/to/install +./ninja -C build install +``` + +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. + +## Configuration File Options + +The configuration file is `cavity.cfg`. 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. + +``` +SOLVER= INC_NAVIER_STOKES +INC_NONDIM= DIMENSIONAL +INC_DENSITY_MODEL= VARIABLE +INC_ENERGY_EQUATION= YES +FLUID_MODEL= INC_IDEAL_GAS +BODY_FORCE= YES +BODY_FORCE_VECTOR= ( 0.0, -9.81, 0.0 ) +``` + +The radiation model is set up with the following options. + +``` +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 +``` + +`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. + +The custom source is switched on with a single option. + +``` +PYTHON_CUSTOM_SOURCE= YES +``` + +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`. + +``` +MESH_FORMAT= RECTANGLE +MESH_BOX_SIZE= ( 65, 65, 0 ) +MESH_BOX_LENGTH= ( 0.05, 0.05, 0 ) +``` + +`TOTAL_HEATFLUX` is the heat flux into the walls listed in `MARKER_MONITORING`. With a radiation model it contains both the conductive and the radiative heat flux into the walls, so it can be used to check the energy balance. The radiative part is the Marshak wall flux $$\Theta (G - 4 \sigma T_w^4)$$ with $$\Theta = \varepsilon / (2(2 - \varepsilon))$$. + +SU2 solves the energy equation and the P1 equation one after the other, so the P1 residual falls more slowly than the enthalpy residual and stays about four orders above it. Both residuals are used to decide when the run has converged. + +``` +ITER= 10000 +CONV_FIELD= RMS_ENTHALPY, RMS_RAD_ENERGY +CONV_RESIDUAL_MINVAL= -6 +``` + +When the P1 residual reaches -6, the wall heat flux has settled to about 0.01 percent. + +## The Python Script + +The script takes the configuration file as its first argument, and `cavity.cfg` when none is given. It then creates the driver. The constructor reads the configuration and builds the mesh and the solvers, but it does not iterate yet. + +```python +driver = pysu2.CSinglezoneDriver(cfg_file, 1, comm) +``` + +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. + +```python +iFLOW = driver.GetSolverIndices()["INC.FLOW"] +iENERGY = driver.GetNumberDimensions() + 1 +nNode = driver.GetNumberNodes() - driver.GetNumberHaloNodes() +coords = driver.Coordinates() +``` + +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. + +```python +source = driver.UserDefinedSource(iFLOW) +for iPoint in range(nNode): + x, y = coords.Get(iPoint)[:2] + source.Set(iPoint, iENERGY, HeatSource(x, y)) +``` + +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`. + +```python +driver.StartSolver() +``` + +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. + +```python +iTEMP = driver.GetPrimitiveIndices()["TEMPERATURE"] +primitives = driver.Primitives() +t_max = max(primitives.Get(i, iTEMP) for i in range(nNode)) +heat_flux = driver.GetOutputValue("TOTAL_HEATFLUX") +``` + +## Running SU2 + +Run the case with the absorption coefficient in `cavity.cfg` as follows. + +```bash +python run.py cavity.cfg +``` + +A second argument overrides the absorption coefficient. The script then writes a copy of the configuration file, such as `cavity_k0.cfg`, and runs that. An absorption coefficient of 0 switches the radiation model off. + +```bash +python run.py cavity.cfg 0 +``` + +The script can also run in parallel with MPI. + +```bash +mpirun -n 4 python run.py cavity.cfg +``` + +With radiation the run takes about 8800 iterations, which is about a minute and a half on 4 cores of a laptop. Without radiation it converges in about 900 iterations. + +## Results + +The table below shows the results of the two runs. + +| Absorption coefficient (1/m) | Maximum temperature (K) | Heat into the walls, `TOTAL_HEATFLUX` (W/m) | Conductive part (W/m) | Radiative part (W/m) | +|---|---|---|---|---| +| 0 (no radiation) | 325.7 | 15.808 | 15.808 | 0 | +| 1 | 324.6 | 15.807 | 14.96 | 0.85 | + +The exact strength of the source is 15.708 W/m, but both runs give 15.808 W/m, which is 0.6 percent more. This gap is not an error in the energy balance. SU2 stores the solution at the mesh points, and each point owns a small control volume around it. The script sets the source at a point to the full value when the point lies inside the ellipse and to zero when it does not. The heat added is therefore the source value times the total volume of the control volumes whose points lie inside the ellipse. That area is slightly larger than the exact area of the ellipse on this mesh. The gap shrinks as the mesh is refined, and it would vanish if the source were set to the fraction of each control volume that lies inside the ellipse. Since the heat into the walls matches the heat that was actually added, the energy balance closes in both runs. + +With radiation, the total heat into the walls stays the same, but about 5 percent of it now arrives as radiation instead of conduction. The gas emits radiation where it is hot and absorbs it where it is cool, so the plume above the source cools by up to 1.2 K and the rest of the cavity cools a little less, as the right panel of figure (1) shows. The flow pattern, with a plume rising above the source and two counter-rotating cells, hardly changes. + +With a larger absorption coefficient, more of the heat leaves by radiation and the effect on the temperature grows. The coupling between the energy equation and the P1 equation also becomes stronger, and since SU2 solves them one after the other, the run then needs many more iterations to converge. + +## Going further + +- Change the shape, position or strength of the source in `HeatSource`. Any function of the coordinates works. +- Make the source depend on time, by setting it inside a time loop in an unsteady run. +- 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. +- 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. diff --git a/tutorials_files/multiphysics/P1_radiation_python/images/cavity_temperature.png b/tutorials_files/multiphysics/P1_radiation_python/images/cavity_temperature.png new file mode 100644 index 00000000..cc2ff235 Binary files /dev/null and b/tutorials_files/multiphysics/P1_radiation_python/images/cavity_temperature.png differ