From 7e92d79ab1697c3714ff38e5904bfe5dd01ea6e5 Mon Sep 17 00:00:00 2001
From: Tsang Keung Chan <tsang.k.chan@durham.ac.uk>
Date: Fri, 17 Feb 2023 15:10:51 +0000
Subject: [PATCH] Resolve "SPHM1RT doesn't compile with 2 or 3 photon groups"

---
 configure.ac                                                 | 3 +++
 doc/RTD/source/RadiativeTransfer/SPHM1_RT.rst                | 3 ++-
 .../AdvectionDifferentTimeStepSizes_1D/README                | 2 +-
 examples/RadiativeTransferTests/Advection_1D/README          | 2 +-
 examples/RadiativeTransferTests/Advection_1D/plotSolution.py | 5 +++++
 .../RadiativeTransferTests/Advection_1D/rt_advection1D.yml   | 4 ++--
 examples/RadiativeTransferTests/StromgrenSphere_2D/README    | 2 +-
 examples/RadiativeTransferTests/StromgrenSphere_3D/README    | 2 +-
 .../StromgrenSphere_3D/propagationTest-3D.yml                | 4 ++--
 9 files changed, 18 insertions(+), 9 deletions(-)

diff --git a/configure.ac b/configure.ac
index 2420d9accf..c561ce3359 100644
--- a/configure.ac
+++ b/configure.ac
@@ -2836,6 +2836,9 @@ case "$with_rt" in
       if test "$number_group" -eq "0"; then
           AC_MSG_ERROR([SPHM1RT: Cannot work with zero photon groups])
       fi
+      if test $number_group -lt 4; then
+          AC_MSG_ERROR([SPHM1RT: Cannot work with < four photon groups for now])
+      fi
       if ! test $number_group -eq $number_group; then
           # abuse -eq to check whether $number_group is an integer. -eq
           # only works with those.
diff --git a/doc/RTD/source/RadiativeTransfer/SPHM1_RT.rst b/doc/RTD/source/RadiativeTransfer/SPHM1_RT.rst
index 6b13a4b3af..9a663dcf34 100644
--- a/doc/RTD/source/RadiativeTransfer/SPHM1_RT.rst
+++ b/doc/RTD/source/RadiativeTransfer/SPHM1_RT.rst
@@ -20,7 +20,8 @@ Compiling for SPHM1-RT
 
 -   To compile swift to be able to run with SPHM1-RT, you need to configure with
     ``--with-rt=SPHM1RT_N`` where ``N`` is the integer number of photon groups that 
-    you intend to use in your simulation.
+    you intend to use in your simulation. The number of photon groups for SPHM1RT has
+     to be four for now (since it is hard-coded in thermo-chemistry solver).
 
 -   SPHM1-RT is compatible with any SPH scheme. You'll
     need to compile using ``--with-hydro=sphenix`` or other SPH schemes, e.g. we have tested gadget2, minimal, and sphenix.
diff --git a/examples/RadiativeTransferTests/AdvectionDifferentTimeStepSizes_1D/README b/examples/RadiativeTransferTests/AdvectionDifferentTimeStepSizes_1D/README
index 3cf3226b92..686f50d9e9 100644
--- a/examples/RadiativeTransferTests/AdvectionDifferentTimeStepSizes_1D/README
+++ b/examples/RadiativeTransferTests/AdvectionDifferentTimeStepSizes_1D/README
@@ -32,7 +32,7 @@ GEAR_RT:
      --with-riemann-solver=hllc --with-stars=GEAR --with-feedback=none --with-grackle=$GRACKLE_ROOT
 
 SPHM1RT:
-    --with-rt=SPHM1RT_3 --with-hydro-dimension=1 --with-stars=basic  --with-sundials=$SUNDIALS_ROOT
+    --with-rt=SPHM1RT_4 --with-hydro-dimension=1 --with-stars=basic  --with-sundials=$SUNDIALS_ROOT
     
 IMPORTANT: Need SUNDIALS version  = 5 . 
 SUNDIALS_ROOT is the root directory that contains the lib and include directories, e.g. on cosma:
diff --git a/examples/RadiativeTransferTests/Advection_1D/README b/examples/RadiativeTransferTests/Advection_1D/README
index 731c528cfc..13dd7ce4ca 100644
--- a/examples/RadiativeTransferTests/Advection_1D/README
+++ b/examples/RadiativeTransferTests/Advection_1D/README
@@ -19,7 +19,7 @@ GEAR_RT:
      --with-riemann-solver=hllc --with-stars=GEAR --with-feedback=none --with-grackle=$GRACKLE_ROOT
 
 SPHM1RT:
-    --with-rt=SPHM1RT_3 --with-hydro-dimension=1 --with-stars=basic  --with-sundials=$SUNDIALS_ROOT
+    --with-rt=SPHM1RT_4 --with-hydro-dimension=1 --with-stars=basic  --with-sundials=$SUNDIALS_ROOT
     
 IMPORTANT: Need SUNDIALS version  = 5 . 
 SUNDIALS_ROOT is the root directory that contains the lib and include directories, e.g. on cosma:
diff --git a/examples/RadiativeTransferTests/Advection_1D/plotSolution.py b/examples/RadiativeTransferTests/Advection_1D/plotSolution.py
index ed2ac18857..4feadac2a7 100755
--- a/examples/RadiativeTransferTests/Advection_1D/plotSolution.py
+++ b/examples/RadiativeTransferTests/Advection_1D/plotSolution.py
@@ -115,6 +115,11 @@ def plot_photons(filename, energy_boundaries=None, flux_boundaries=None):
     boxsize = meta.boxsize[0]
 
     ngroups = int(meta.subgrid_scheme["PhotonGroupNumber"])
+    # Currently, SPHM1RT only works for frequency group = 4 in the code
+    # However, we only plot 3 frequency groups here, so 
+    # we set ngroups = 3: 
+    if scheme.startswith("SPH M1closure"):
+       ngroups = 3 
 
     for g in range(ngroups):
         # workaround to access named columns data with swiftsimio visualisaiton
diff --git a/examples/RadiativeTransferTests/Advection_1D/rt_advection1D.yml b/examples/RadiativeTransferTests/Advection_1D/rt_advection1D.yml
index 76fb70f54b..a528dbeb16 100644
--- a/examples/RadiativeTransferTests/Advection_1D/rt_advection1D.yml
+++ b/examples/RadiativeTransferTests/Advection_1D/rt_advection1D.yml
@@ -63,9 +63,9 @@ GEARRT:
 SPHM1RT:
   cred: 2.99792458e10              # reduce the speed of light in the code unit
   CFL_condition: 0.1               # CFL condition for RT, independent of hydro 
-  photon_groups_Hz: [1., 2.]  # Photon frequency group bin edges in Hz. Needs to be 1 less than the number of groups (N) requested during the configuration (--with-RT=SPHM1RT_N).
+  photon_groups_Hz: [0., 1., 2.]  # Photon frequency group bin edges in Hz. Needs to be 1 less than the number of groups (N) requested during the configuration (--with-RT=SPHM1RT_N).
   use_const_emission_rates: 1     # (Optional) use constant emission rates for stars as defined with star_emission_rates parameter
-  star_emission_rates: [1e-32, 1e-32, 1e-32]   # (Optional) constant star emission rates for each photon frequency group to use if use_constant_emission_rates is set.
+  star_emission_rates: [1e-32,1e-32, 1e-32, 1e-32]   # (Optional) constant star emission rates for each photon frequency group to use if use_constant_emission_rates is set.
   stellar_spectrum_type: 0                          # Which radiation spectrum to use. 0: constant from 0 until some max frequency set by stellar_spectrum_const_max_frequency_Hz. 1: blackbody spectrum.
   stellar_spectrum_const_max_frequency_Hz: 1.e17    # (Conditional) if stellar_spectrum_type=0, use this maximal frequency for the constant spectrum. 
   skip_thermochemistry: 1                             # (Optional) skip the thermochemistry. This is intended only for debugging and testing the radiation transport, as it breaks the purpose of RT.
diff --git a/examples/RadiativeTransferTests/StromgrenSphere_2D/README b/examples/RadiativeTransferTests/StromgrenSphere_2D/README
index c1269bdaac..d291adc607 100644
--- a/examples/RadiativeTransferTests/StromgrenSphere_2D/README
+++ b/examples/RadiativeTransferTests/StromgrenSphere_2D/README
@@ -25,7 +25,7 @@ To use the GEAR RT model, compile with :
     --with-rt=GEAR_1 --with-rt-riemann-solver=GLF --with-hydro-dimension=2 --with-hydro=gizmo-mfv --with-riemann-solver=hllc --with-stars=GEAR --with-feedback=none --with-grackle=$GRACKLE_ROOT
 
 To use the SPHM1 RT model, compile with :
-    --with-rt=SPHM1RT_1 --with-hydro-dimension=2  --with-stars=basic --with-feedback=none   --with-sundials=$SUNDIALS_ROOT
+    --with-rt=SPHM1RT_4 --with-hydro-dimension=2  --with-stars=basic --with-feedback=none   --with-sundials=$SUNDIALS_ROOT
     
 IMPORTANT: Need SUNDIALS version  = 5 . 
 SUNDIALS_ROOT is the root directory that contains the lib and include directories, e.g. on cosma:
diff --git a/examples/RadiativeTransferTests/StromgrenSphere_3D/README b/examples/RadiativeTransferTests/StromgrenSphere_3D/README
index f4cf4fc660..69bbb63c5e 100644
--- a/examples/RadiativeTransferTests/StromgrenSphere_3D/README
+++ b/examples/RadiativeTransferTests/StromgrenSphere_3D/README
@@ -61,7 +61,7 @@ https://github.com/SWIFTSIM/swiftsim-rt-tools/blob/main/additionalTests/instruct
 
 To use the SPHM1 RT model, compile with :
 for propagation test:
-    --with-rt=SPHM1RT_1 --with-stars=basic --with-feedback=none   --with-sundials=$SUNDIALS_ROOT
+    --with-rt=SPHM1RT_4 --with-stars=basic --with-feedback=none   --with-sundials=$SUNDIALS_ROOT
 
 for Stromgren3D test:
     --with-rt=SPHM1RT_4 --with-stars=basic --with-feedback=none   --with-sundials=$SUNDIALS_ROOT
diff --git a/examples/RadiativeTransferTests/StromgrenSphere_3D/propagationTest-3D.yml b/examples/RadiativeTransferTests/StromgrenSphere_3D/propagationTest-3D.yml
index db02aad603..3fa7414add 100644
--- a/examples/RadiativeTransferTests/StromgrenSphere_3D/propagationTest-3D.yml
+++ b/examples/RadiativeTransferTests/StromgrenSphere_3D/propagationTest-3D.yml
@@ -61,9 +61,9 @@ GEARRT:
 SPHM1RT:
   cred: 2.99792458e10              # reduce the speed of light in the code unit
   CFL_condition: 0.1               # CFL condition for RT, independent of hydro 
-  photon_groups_Hz: []  # Photon frequency group bin edges in Hz. Needs to be 1 less than the number of groups (N) requested during the configuration (--with-RT=SPHM1RT_N).
+  photon_groups_Hz: [0.,1.,2.]  # Photon frequency group bin edges in Hz. Needs to be 1 less than the number of groups (N) requested during the configuration (--with-RT=SPHM1RT_N).
   use_const_emission_rates: 1         # (Optional) use constant emission rates for stars as defined with star_emission_rates_erg_LSol parameter
-  star_emission_rates: [382600.0]   # (Optional) constant star emission rates (1e-28 Lsun) for each photon frequency group to use if use_constant_emission_rates is set.
+  star_emission_rates: [382600.0, 1e-32, 1e-32, 1e-32]   # (Optional) constant star emission rates (1e-28 Lsun) for each photon frequency group to use if use_constant_emission_rates is set.
   stellar_spectrum_type: 0                          # Which radiation spectrum to use. 0: constant from 0 until some max frequency set by stellar_spectrum_const_max_frequency_Hz. 1: blackbody spectrum.
   stellar_spectrum_const_max_frequency_Hz: 1.e17    # (Conditional) if stellar_spectrum_type=0, use this maximal frequency for the constant spectrum. 
   stars_max_timestep: 1.953125e-06                  # update stars every step!
-- 
GitLab