From 363608c79db28c62e2c5dd708cc9b509037eb340 Mon Sep 17 00:00:00 2001
From: Christoph Lehmann <christoph.lehmann@ufz.de>
Date: Tue, 21 Nov 2023 14:50:55 +0100
Subject: [PATCH] [PL/ST] Make component id mandatory for source terms

---
 .../CreateSourceTerm.cpp                       | 16 ++++++++--------
 .../SourceTermConfig.h                         |  4 ++--
 ProcessLib/ProcessVariable.cpp                 | 18 ++++++++++++++----
 3 files changed, 24 insertions(+), 14 deletions(-)

diff --git a/ProcessLib/BoundaryConditionAndSourceTerm/CreateSourceTerm.cpp b/ProcessLib/BoundaryConditionAndSourceTerm/CreateSourceTerm.cpp
index 7850fa9e4cb..2ae9b5628f3 100644
--- a/ProcessLib/BoundaryConditionAndSourceTerm/CreateSourceTerm.cpp
+++ b/ProcessLib/BoundaryConditionAndSourceTerm/CreateSourceTerm.cpp
@@ -33,13 +33,13 @@ std::unique_ptr<SourceTerm> createSourceTerm(
     // check basic data consistency
     if (variable_id >=
             static_cast<int>(dof_table_bulk.getNumberOfVariables()) ||
-        *config.component_id >=
+        config.component_id >=
             dof_table_bulk.getNumberOfVariableComponents(variable_id))
     {
         OGS_FATAL(
             "Variable id or component id too high. Actual values: ({:d}, "
             "{:d}), maximum values: ({:d}, {:d}).",
-            variable_id, *config.component_id,
+            variable_id, config.component_id,
             dof_table_bulk.getNumberOfVariables(),
             dof_table_bulk.getNumberOfVariableComponents(variable_id));
     }
@@ -59,7 +59,7 @@ std::unique_ptr<SourceTerm> createSourceTerm(
         "Found {:d} nodes for source term at mesh '{:s}' for the variable {:d} "
         "and component {:d}",
         source_term_nodes.size(), source_term_mesh.getName(), variable_id,
-        *config.component_id);
+        config.component_id);
 
     MeshLib::MeshSubset source_term_mesh_subset(source_term_mesh,
                                                 source_term_nodes);
@@ -68,11 +68,11 @@ std::unique_ptr<SourceTerm> createSourceTerm(
     {
         auto dof_table_source_term =
             dof_table_bulk.deriveBoundaryConstrainedMap(
-                variable_id, {*config.component_id},
+                variable_id, {config.component_id},
                 std::move(source_term_mesh_subset));
         return ProcessLib::createNodalSourceTerm(
             config.config, config.mesh, std::move(dof_table_source_term),
-            source_term_mesh.getID(), variable_id, *config.component_id,
+            source_term_mesh.getID(), variable_id, config.component_id,
             parameters);
     }
 
@@ -80,10 +80,10 @@ std::unique_ptr<SourceTerm> createSourceTerm(
     {
         auto dof_table_source_term =
             dof_table_bulk.deriveBoundaryConstrainedMap(
-                variable_id, {*config.component_id},
+                variable_id, {config.component_id},
                 std::move(source_term_mesh_subset));
         auto const& bulk_mesh_dimension =
-            dof_table_bulk.getMeshSubset(variable_id, *config.component_id)
+            dof_table_bulk.getMeshSubset(variable_id, config.component_id)
                 .getMesh()
                 .getDimension();
         return ProcessLib::createVolumetricSourceTerm(
@@ -100,7 +100,7 @@ std::unique_ptr<SourceTerm> createSourceTerm(
 
         return ProcessLib::createPythonSourceTerm(
             config.config, config.mesh, std::move(dof_table_source_term),
-            variable_id, *config.component_id, integration_order,
+            variable_id, config.component_id, integration_order,
             shapefunction_order, source_term_mesh.getDimension(),
             all_process_variables_for_this_process);
     }
diff --git a/ProcessLib/BoundaryConditionAndSourceTerm/SourceTermConfig.h b/ProcessLib/BoundaryConditionAndSourceTerm/SourceTermConfig.h
index 60f7cb28549..170ee89a602 100644
--- a/ProcessLib/BoundaryConditionAndSourceTerm/SourceTermConfig.h
+++ b/ProcessLib/BoundaryConditionAndSourceTerm/SourceTermConfig.h
@@ -19,7 +19,7 @@ struct SourceTermConfig final
 {
     SourceTermConfig(BaseLib::ConfigTree&& config_,
                      MeshLib::Mesh const& mesh_,
-                     std::optional<int> const component_id_)
+                     int component_id_)
         : config(std::move(config_)), mesh(mesh_), component_id(component_id_)
     {
     }
@@ -33,7 +33,7 @@ struct SourceTermConfig final
 
     BaseLib::ConfigTree config;
     MeshLib::Mesh const& mesh;
-    std::optional<int> const component_id;
+    int component_id;
 };
 
 }  // namespace ProcessLib
diff --git a/ProcessLib/ProcessVariable.cpp b/ProcessLib/ProcessVariable.cpp
index b7d0d58c9c5..2ae57236f49 100644
--- a/ProcessLib/ProcessVariable.cpp
+++ b/ProcessLib/ProcessVariable.cpp
@@ -172,14 +172,24 @@ ProcessVariable::ProcessVariable(
                 //! \ogs_file_param{prj__process_variables__process_variable__source_terms__source_term__component}
                 st_config.getConfigParameterOptional<int>("component");
 
-            if (!component_id && _n_components == 1)
+            if (!component_id)
             {
-                // default value for single component vars.
-                component_id = 0;
+                if (_n_components == 1)
+                {
+                    // default value for single component vars.
+                    component_id = 0;
+                }
+                else
+                {
+                    OGS_FATAL(
+                        "Specifying the component id (<component>) for a "
+                        "source term for a non-scalar process variable is "
+                        "mandatory.");
+                }
             }
 
             _source_term_configs.emplace_back(std::move(st_config), st_mesh,
-                                              component_id);
+                                              *component_id);
         }
     }
     else
-- 
GitLab