From a04a28a2622920a4037e4b097dc275dac21bb98d Mon Sep 17 00:00:00 2001
From: "Fabian.Jakub" <fabian@jakub.com>
Date: Wed, 29 Mar 2023 10:31:15 +0200
Subject: [PATCH] enhance spack icon-nwp package to allow a build with PETSc
 and 3D radiation

---
 spack_repo/packages/icon-nwp/package.py  | 24 ++++++++++-
 spack_repo/packages/tenstream/package.py | 51 ++++++++++++++++++++++++
 2 files changed, 73 insertions(+), 2 deletions(-)
 create mode 100644 spack_repo/packages/tenstream/package.py

diff --git a/spack_repo/packages/icon-nwp/package.py b/spack_repo/packages/icon-nwp/package.py
index e97a858..12eef11 100644
--- a/spack_repo/packages/icon-nwp/package.py
+++ b/spack_repo/packages/icon-nwp/package.py
@@ -27,6 +27,8 @@ class IconNwp(Package):
     version("psp", branch="icon-nwp/icon-nwp-psp")
 
     variant("lmu", default=False, description="if git.url and submodules should be patched to use the LMU mirrors")
+    variant("plexrt", default=False, description="add 3D radiation support with TenStream")
+    variant("petsc", default=False, description="add PETSc support")
 
     # Dependencies
     depends_on("mpi")
@@ -35,6 +37,10 @@ class IconNwp(Package):
     depends_on("eccodes+fortran")
     depends_on("libxml2")
 
+    depends_on("petsc", when="+petsc")
+    depends_on("petsc", when="+plexrt")
+    depends_on("tenstream", when="+plexrt")
+
     # Openblas? best way of doing it?
     depends_on("openblas", when="%gcc")
     depends_on("intel-mkl", when="%intel")
@@ -162,13 +168,27 @@ class IconNwp(Package):
             libs.append("-lyaml")
 
         mtune = "generic"
-        INCLUDES = f"-I{spec['libxml2'].prefix}/include/libxml2"
+        INCLUDES = [f"-I{spec['libxml2'].prefix}/include/libxml2", ]
+        CPPFLAGS = []
+
+        if "+plexrt" in self.spec:
+            CPPFLAGS.append("-DHAVE_PLEXRT")
+            INCLUDES.append(f"-I{spec['tenstream'].prefix}/include")
+            libs.append(f"-L{spec['tenstream'].prefix.lib} -ltenstream")
+
+        if "+petsc" in self.spec:
+            CPPFLAGS.append("-DHAVE_PETSC")
+            INCLUDES.append(f"-I{spec['petsc'].prefix}/include")
+            libs.append(f"-L{spec['petsc'].prefix.lib} -lpetsc")
+
+
         options = [
             f"CC={spec['mpi'].mpicc}",
             f"FC={spec['mpi'].mpifc}",
-            f"CFLAGS=-g -mpc64 {INCLUDES}",
+            f"CFLAGS=-g -mpc64 {' '.join(INCLUDES)}",
             f"ICON_CFLAGS=-O3 -g -mtune={mtune}",
             f"ICON_BUNDLED_CFLAGS=-O3 -mtune={mtune}",
+            f"CPPFLAGS={' '.join(CPPFLAGS)}",
             "FCFLAGS=-std=legacy -fmodule-private -fimplicit-none -fmax-identifier-length=63 -Wall -Wcharacter-truncation -Wconversion -Wunderflow -Wunused-parameter -Wno-surprising -fall-intrinsics -g -mpc64 -w",
             "ICON_FCFLAGS=-fbacktrace -fbounds-check -fstack-protector-all -finit-real=nan -finit-integer=-2147483648 -finit-character=127 -w -O2",
             f"ICON_OCEAN_FCFLAGS=-O3 -mtune={mtune}",
diff --git a/spack_repo/packages/tenstream/package.py b/spack_repo/packages/tenstream/package.py
new file mode 100644
index 0000000..56ad0f2
--- /dev/null
+++ b/spack_repo/packages/tenstream/package.py
@@ -0,0 +1,51 @@
+# Copyright 2013-2022 Lawrence Livermore National Security, LLC and other
+# Spack Project Developers. See the top-level COPYRIGHT file for details.
+#
+# SPDX-License-Identifier: (Apache-2.0 OR MIT)
+
+from spack.package import *
+
+class Tenstream(CMakePackage):
+    """
+    Recipe to build TenStream radiative transfer lib.
+    """
+
+    homepage = "https://gitlab.com/jakubfabian/tenstream.git"
+
+    git = "https://gitlab.com/jakubfabian/tenstream.git"
+
+    version("master", branch="master")
+    version("next", branch="next")
+
+    variant("debug", default=False, description="use debug flags to build")
+    variant("rayli", default=False, description="download and build with RayLI - MonteCarlo raytracer for unstructured meshes")
+
+    # Dependencies
+    depends_on("mpi")
+    depends_on("cmake")
+    depends_on("netcdf-fortran")
+    depends_on("petsc")
+
+    def setup_build_environment(self, env):
+        spec = self.spec
+
+        # Some environment variables to set
+        env_variables_to_set = {
+            "CC": spec["mpi"].mpicc,
+            "CXX": spec["mpi"].mpicxx,
+            "FC": spec["mpi"].mpifc,
+            "F77": spec["mpi"].mpif77,
+        }
+        for variable, value in env_variables_to_set.items():
+                env.set(variable, value)
+
+    def cmake_args(self):
+        args = []
+
+        if "+debug" in self.spec:
+            args.append('-DCMAKE_BUILD_TYPE=DEBUG')
+
+        if "+rayli" in self.spec:
+            args.append("-DBUILD_RAYLI=ON")
+
+        return args
-- 
GitLab