From e70fef9625572ccbeb1d9a0e5863b4ffebc10118 Mon Sep 17 00:00:00 2001
From: teddy <teddy.chantrait@cea.fr>
Date: Mon, 27 Feb 2023 16:24:23 +0100
Subject: [PATCH] Modifs to make pugs compile when costo is not found

---
 CMakeLists.txt                            | 47 ++++++++++++++---------
 cmake/FindParMETIS.cmake                  |  8 +++-
 src/language/modules/CouplageModule.cpp   |  2 +
 src/language/modules/CouplageModule.hpp   |  2 +
 src/language/modules/ModuleRepository.cpp |  6 ++-
 src/language/modules/SchemeModule.cpp     |  4 ++
 src/scheme/HyperelasticSolver.cpp         |  9 ++++-
 src/scheme/VectorDiamondScheme.cpp        | 26 +++----------
 src/utils/Messenger.cpp                   |  2 +
 src/utils/Messenger.hpp                   |  6 ++-
 src/utils/Serializer.cpp                  |  2 +
 src/utils/Serializer.hpp                  |  4 +-
 src/utils/pugs_config.hpp.in              |  1 +
 13 files changed, 73 insertions(+), 46 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 3f15fc266..97130345f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -157,23 +157,6 @@ else()
   endif()
 endif()
 
-#------------------------------------------------------
-# Search for Costo
-
-find_package(Costo 0.0.1 COMPONENTS CPO)
-
-if(NOT Costo_FOUND)
-  MESSAGE( "Cant find Costo")
-else()
-  MESSAGE("COSTO ${Costo_LIBRARIES}")
-  MESSAGE("COSTO ${Costo_LIBRARY_DIR}")
-  MESSAGE("COSTO ${Costo_INCLUDE_DIR}")
-  include_directories(SYSTEM ${Costo_INCLUDE_DIR})
-  link_directories(${Costo_LIBRARY_DIR})
-endif()
-
-
-
 #------------------------------------------------------
 # Check for PETSc
 # defaults use PETSc
@@ -241,6 +224,34 @@ endif()
 
 set(PUGS_HAS_MPI ${MPI_FOUND})
 
+#------------------------------------------------------
+# Search for Costo
+set(PUGS_ENABLE_COSTO OFF CACHE STRING
+  "Choose one of: AUTO ON OFF")
+
+if (PUGS_ENABLE_COSTO MATCHES "^(AUTO|ON)$")
+  find_package(Costo 0.0.1 COMPONENTS CPO)
+
+  if(Costo_FOUND)
+  else()
+    message(FATAL_ERROR "Could not find Costo!")
+    set(Costo_FOUND FALSE)
+    unset(PUGS_HAS_COSTO)
+  endif()
+  set(PUGS_HAS_COSTO ${Costo_FOUND})
+else()
+    unset(PUGS_HAS_COSTO)
+endif()
+
+if (${Costo_FOUND})
+  include_directories(SYSTEM ${Costo_INCLUDE_DIR})
+  link_directories(${Costo_LIBRARY_DIR})
+else()
+  if (PUGS_ENABLE_COSTO MATCHES "^ON$")
+    message(FATAL_ERROR "Could not find Costo!")
+  endif()
+endif()
+
 #------------------------------------------------------
 # search for clang-format
 
@@ -580,8 +591,6 @@ endif()
 link_libraries("-rdynamic")
 
 # ------------------- Source files --------------------
-
-
 # Pugs binary
 add_executable(
   pugs
diff --git a/cmake/FindParMETIS.cmake b/cmake/FindParMETIS.cmake
index 71e47b3c0..340fed22c 100644
--- a/cmake/FindParMETIS.cmake
+++ b/cmake/FindParMETIS.cmake
@@ -5,14 +5,18 @@ find_path(PARMETIS_INCLUDE_DIR parmetis.h
 
 if (EXISTS "${PARMETIS_INCLUDE_DIR}/parmetis.h")
   message(STATUS "Found parmetis.h in ${PARMETIS_INCLUDE_DIR}")
-  find_library(LIB_PARMETIS parmetis)
+  find_library(LIB_PARMETIS parmetis ${PARMETIS_LIBDIR})
   if("${LIB_PARMETIS}" STREQUAL "LIB_PARMETIS-NOTFOUND")
     message("** Could not find parmetis library")
   endif()
-  find_library(LIB_METIS metis)
+  find_library(LIB_METIS metis ${METIS_LIBDIR})
   if("${LIB_PARMETIS}" STREQUAL "LIB_METIS-NOTFOUND")
     message("** Could not find metis library")
   endif()
+  include_directories(SYSTEM ${PARMETIS_INCLUDE_DIR})
+  include_directories(SYSTEM ${METIS_INCDIR})
+  link_directories(${PARMETIS_LIBDIR})
+  link_directories(${METIS_LIBDIR})
   set(PARMETIS_LIBRARIES ${LIB_PARMETIS} ${LIB_METIS})
   message(STATUS "Found parmetis/metis libraries ${PARMETIS_LIBRARIES}")
 else()
diff --git a/src/language/modules/CouplageModule.cpp b/src/language/modules/CouplageModule.cpp
index c845dbe93..475a93774 100644
--- a/src/language/modules/CouplageModule.cpp
+++ b/src/language/modules/CouplageModule.cpp
@@ -1,3 +1,4 @@
+#ifdef PUGS_HAS_COSTO
 #include <language/modules/CouplageModule.hpp>
 
 #include <language/modules/MeshModule.hpp>
@@ -84,3 +85,4 @@ CouplageModule::test()
 {
   std::cout << "in test" << std::endl;
 }
+#endif   // PUGS_HAS_COSTO
diff --git a/src/language/modules/CouplageModule.hpp b/src/language/modules/CouplageModule.hpp
index ac25ec65c..58ed49664 100644
--- a/src/language/modules/CouplageModule.hpp
+++ b/src/language/modules/CouplageModule.hpp
@@ -1,5 +1,6 @@
 #ifndef COUPLAGE_MODULE_HPP
 #define COUPLAGE_MODULE_HPP
+#ifdef PUGS_HAS_COSTO
 
 #include <language/modules/BuiltinModule.hpp>
 #include <language/utils/ASTNodeDataTypeTraits.hpp>
@@ -45,4 +46,5 @@ class CouplageModule : public BuiltinModule
   void registerOperators() const final;
 };
 
+#endif   // PUGS_HAS_COSTO
 #endif   // COUPLAGE_MODULE_HPP
diff --git a/src/language/modules/ModuleRepository.cpp b/src/language/modules/ModuleRepository.cpp
index 45c3e5c2a..31d9df220 100644
--- a/src/language/modules/ModuleRepository.cpp
+++ b/src/language/modules/ModuleRepository.cpp
@@ -2,7 +2,9 @@
 
 #include <language/ast/ASTNode.hpp>
 #include <language/modules/CoreModule.hpp>
+#ifdef PUGS_HAS_COSTO
 #include <language/modules/CouplageModule.hpp>
+#endif   // PUGS_HAS_COSTO
 #include <language/modules/DevUtilsModule.hpp>
 #include <language/modules/LinearSolverModule.hpp>
 #include <language/modules/MathModule.hpp>
@@ -55,7 +57,6 @@ ModuleRepository::_subscribe(std::unique_ptr<IModule> m)
 ModuleRepository::ModuleRepository()
 {
   this->_subscribe(std::make_unique<CoreModule>());
-  this->_subscribe(std::make_unique<CouplageModule>());
   this->_subscribe(std::make_unique<LinearSolverModule>());
   this->_subscribe(std::make_unique<MathModule>());
   this->_subscribe(std::make_unique<MeshModule>());
@@ -63,6 +64,9 @@ ModuleRepository::ModuleRepository()
   this->_subscribe(std::make_unique<SocketModule>());
   this->_subscribe(std::make_unique<DevUtilsModule>());
   this->_subscribe(std::make_unique<WriterModule>());
+#ifdef PUGS_HAS_COSTO
+  this->_subscribe(std::make_unique<CouplageModule>());
+#endif   // PUGS_HAS_COSTO
 }
 
 template <typename NameEmbedderMapT, typename EmbedderTableT>
diff --git a/src/language/modules/SchemeModule.cpp b/src/language/modules/SchemeModule.cpp
index bb2fd4488..2f2ed2795 100644
--- a/src/language/modules/SchemeModule.cpp
+++ b/src/language/modules/SchemeModule.cpp
@@ -280,6 +280,7 @@ SchemeModule::SchemeModule()
 
                                           ));
 
+#ifdef PUGS_HAS_COSTO
   this->_addBuiltinFunction("cpl_pressure",
                             std::function(
 
@@ -291,6 +292,7 @@ SchemeModule::SchemeModule()
                               }
 
                               ));
+#endif()   // PUGS_HAS_COSTO
 
   this->_addBuiltinFunction("normalstress",
                             std::function(
@@ -337,6 +339,7 @@ SchemeModule::SchemeModule()
                               }
 
                               ));
+#ifdef PUGS_HAS_COSTO
 
   this->_addBuiltinFunction("cpl_normal_strain",
                             std::function(
@@ -348,6 +351,7 @@ SchemeModule::SchemeModule()
                               }
 
                               ));
+#endif()   // PUGS_HAS_COSTO
 
   this->_addBuiltinFunction("fourier",
                             std::function(
diff --git a/src/scheme/HyperelasticSolver.cpp b/src/scheme/HyperelasticSolver.cpp
index e4ceca7e1..c755a4b1d 100644
--- a/src/scheme/HyperelasticSolver.cpp
+++ b/src/scheme/HyperelasticSolver.cpp
@@ -298,7 +298,10 @@ class HyperelasticSolverHandler::HyperelasticSolver final : public HyperelasticS
                                                                                      mesh_face_boundary.faceList());
             bc_list.emplace_back(PressureBoundaryCondition{mesh_face_boundary, face_values});
           }
-        } else if (dirichlet_bc_descriptor.name() == "cpl_pressure") {
+        }
+#ifdef PUGS_HAS_COSTO
+        else if (dirichlet_bc_descriptor.name() == "cpl_pressure") {
+
           const int src = 1;
           const int tag = 200;
 
@@ -328,7 +331,9 @@ class HyperelasticSolverHandler::HyperelasticSolver final : public HyperelasticS
             }
             bc_list.emplace_back(PressureBoundaryCondition{mesh_face_boundary, face_values});
           }
-        } else if (dirichlet_bc_descriptor.name() == "normal-stress") {
+        }
+#endif()   // PUGS_HAS_COSTO
+        else if (dirichlet_bc_descriptor.name() == "normal-stress") {
           const FunctionSymbolId normal_stress_id = dirichlet_bc_descriptor.rhsSymbolId();
 
           if constexpr (Dimension == 1) {
diff --git a/src/scheme/VectorDiamondScheme.cpp b/src/scheme/VectorDiamondScheme.cpp
index 57cfbdb3a..91344a087 100644
--- a/src/scheme/VectorDiamondScheme.cpp
+++ b/src/scheme/VectorDiamondScheme.cpp
@@ -369,7 +369,9 @@ class VectorDiamondSchemeHandler::VectorDiamondScheme : public VectorDiamondSche
           } else {
             throw NotImplementedError("Normal strain conditions are not supported in 1d");
           }
-        } else if (dirichlet_bc_descriptor.name() == "cpl_normal_strain") {
+        }
+#ifdef PUGS_HAS_COSTO
+        else if (dirichlet_bc_descriptor.name() == "cpl_normal_strain") {
           if constexpr (Dimension > 1) {
             MeshFaceBoundary<Dimension> mesh_face_boundary =
               getMeshFaceBoundary(*mesh, dirichlet_bc_descriptor.boundaryDescriptor());
@@ -402,7 +404,9 @@ class VectorDiamondSchemeHandler::VectorDiamondScheme : public VectorDiamondSche
           } else {
             throw NotImplementedError("Normal strain conditions are not supported in 1d");
           }
-        } else {
+        }
+#endif()   // PUGS_HAS_COSTO
+        else {
           is_valid_boundary_condition = false;
         }
         break;
@@ -995,7 +999,6 @@ class VectorDiamondSchemeHandler::VectorDiamondScheme : public VectorDiamondSche
         }
 
         m_dual_solution.fill(zero);
-
         const auto& face_to_cell_matrix = mesh->connectivity().faceToCellMatrix();
         for (CellId cell_id = 0; cell_id < diamond_mesh->numberOfCells(); ++cell_id) {
           const FaceId face_id = dual_cell_face_id[cell_id];
@@ -1355,23 +1358,6 @@ class EnergyComputerHandler::EnergyComputer : public EnergyComputerHandler::IEne
           } else {
             throw NotImplementedError("Neumann conditions are not supported in 1d");
           }
-        } else if (dirichlet_bc_descriptor.name() == "cpl_normal_strain") {
-          if constexpr (Dimension > 1) {
-            MeshFaceBoundary<Dimension> mesh_face_boundary =
-              getMeshFaceBoundary(*mesh, dirichlet_bc_descriptor.boundaryDescriptor());
-
-            MeshDataType& mesh_data = MeshDataManager::instance().getMeshData(*mesh);
-
-            const FunctionSymbolId g_id = dirichlet_bc_descriptor.rhsSymbolId();
-
-            Array<const TinyVector<Dimension>> value_list = InterpolateItemValue<TinyVector<Dimension>(
-              TinyVector<Dimension>)>::template interpolate<ItemType::face>(g_id, mesh_data.xl(),
-                                                                            mesh_face_boundary.faceList());
-            boundary_condition_list.push_back(NormalStrainBoundaryCondition{mesh_face_boundary.faceList(), value_list});
-
-          } else {
-            throw NotImplementedError("Normal strain conditions are not supported in 1d");
-          }
         } else if (dirichlet_bc_descriptor.name() == "normal_strain") {
           if constexpr (Dimension > 1) {
             MeshFaceBoundary<Dimension> mesh_face_boundary =
diff --git a/src/utils/Messenger.cpp b/src/utils/Messenger.cpp
index 92f8c5f51..1a8f84365 100644
--- a/src/utils/Messenger.cpp
+++ b/src/utils/Messenger.cpp
@@ -33,6 +33,7 @@ Messenger::Messenger([[maybe_unused]] int& argc, [[maybe_unused]] char* argv[])
 #ifdef PUGS_HAS_MPI
   MPI_Init(&argc, &argv);
 
+#ifdef PUGS_HAS_COSTO
   if (CommunicatorManager::hasSplitColor()) {
     myCoupling = new costo::coupling(CommunicatorManager::splitColor());
 
@@ -43,6 +44,7 @@ Messenger::Messenger([[maybe_unused]] int& argc, [[maybe_unused]] char* argv[])
     /*   MPI_Abort(MPI_COMM_WORLD, res); */
     /* } */
   }
+#endif()   // PUGS_HAS_COSTO
 
   m_rank = [&]() {
     int rank;
diff --git a/src/utils/Messenger.hpp b/src/utils/Messenger.hpp
index 414911405..ee5be2b4e 100644
--- a/src/utils/Messenger.hpp
+++ b/src/utils/Messenger.hpp
@@ -16,7 +16,10 @@
 #include <mpi.h>
 #endif   // PUGS_HAS_MPI
 
+#ifdef PUGS_HAS_COSTO
 #include <CPO/couplingObjects.h>
+#endif   // PUGS_HAS_COSTO
+
 #include <utils/CommunicatorManager.hpp>
 #include <utils/Exceptions.hpp>
 #include <utils/PugsTraits.hpp>
@@ -408,8 +411,9 @@ class Messenger
   }
 
  public:
+#ifdef PUGS_HAS_COSTO
   costo::coupling* myCoupling = nullptr;
-
+#endif()   // PUGS_HAS_COSTO
   static void create(int& argc, char* argv[]);
   static void destroy();
 
diff --git a/src/utils/Serializer.cpp b/src/utils/Serializer.cpp
index a2a8fd23a..00415d2ae 100644
--- a/src/utils/Serializer.cpp
+++ b/src/utils/Serializer.cpp
@@ -1,3 +1,4 @@
+#ifdef PUGS_HAS_COSTO
 #include <mesh/MeshNodeBoundary.hpp>
 #include <utils/FPEManager.hpp>
 #include <utils/Messenger.hpp>
@@ -322,3 +323,4 @@ Serializer::apply(const std::shared_ptr<const IBoundaryDescriptor>& boundary,
   }
   }
 }
+#endif()   // PUGS_HAS_COSTO
diff --git a/src/utils/Serializer.hpp b/src/utils/Serializer.hpp
index 6c47225f7..175894a57 100644
--- a/src/utils/Serializer.hpp
+++ b/src/utils/Serializer.hpp
@@ -1,5 +1,6 @@
 #ifndef SERIALIZER_HPP
 #define SERIALIZER_HPP
+#ifdef PUGS_HAS_COSTO
 
 #include <language/utils/InterpolateItemValue.hpp>
 #include <mesh/Connectivity.hpp>
@@ -43,4 +44,5 @@ class Serializer
              const int tag);
 };
 
-#endif   // SERIALIZER
+#endif()   // PUGS_HAS_COSTO
+#endif     // SERIALIZER
diff --git a/src/utils/pugs_config.hpp.in b/src/utils/pugs_config.hpp.in
index 0736003b9..233d49b1b 100644
--- a/src/utils/pugs_config.hpp.in
+++ b/src/utils/pugs_config.hpp.in
@@ -5,6 +5,7 @@
 #cmakedefine PUGS_HAS_MPI
 #cmakedefine PUGS_HAS_PETSC
 #cmakedefine PUGS_HAS_SLEPC
+#cmakedefine PUGS_HAS_COSTO
 
 #cmakedefine SYSTEM_IS_LINUX
 #cmakedefine SYSTEM_IS_DARWIN
-- 
GitLab