From 6c329b1046b8827a56ac248a6cef9e6fa46c006c Mon Sep 17 00:00:00 2001
From: renchao_lu <renchao.lu@gmail.com>
Date: Mon, 29 Jun 2020 00:19:28 +0200
Subject: [PATCH] [PL] Pass chemical solver interface.

---
 Applications/ApplicationsLib/ProjectData.cpp  | 41 +++++++++++--------
 Applications/ApplicationsLib/ProjectData.h    | 18 ++++----
 .../CreateComponentTransportProcess.cpp       |  3 +-
 .../CreateComponentTransportProcess.h         |  3 +-
 ProcessLib/CreateTimeLoop.cpp                 |  2 +-
 ProcessLib/CreateTimeLoop.h                   |  2 +-
 6 files changed, 39 insertions(+), 30 deletions(-)

diff --git a/Applications/ApplicationsLib/ProjectData.cpp b/Applications/ApplicationsLib/ProjectData.cpp
index 01e60a54a38..3b574218bfd 100644
--- a/Applications/ApplicationsLib/ProjectData.cpp
+++ b/Applications/ApplicationsLib/ProjectData.cpp
@@ -328,14 +328,15 @@ ProjectData::ProjectData(BaseLib::ConfigTree const& project_config,
     //! \ogs_file_param{prj__media}
     parseMedia(project_config.getConfigSubtreeOptional("media"));
 
-    parseChemicalSolverInterface(
+    auto chemical_solver_interface = parseChemicalSolverInterface(
         //! \ogs_file_param{prj__chemical_system}
         project_config.getConfigSubtreeOptional("chemical_system"),
         output_directory);
 
     //! \ogs_file_param{prj__processes}
     parseProcesses(project_config.getConfigSubtree("processes"),
-                   project_directory, output_directory);
+                   project_directory, output_directory,
+                   chemical_solver_interface.get());
 
     //! \ogs_file_param{prj__linear_solvers}
     parseLinearSolvers(project_config.getConfigSubtree("linear_solvers"));
@@ -345,7 +346,7 @@ ProjectData::ProjectData(BaseLib::ConfigTree const& project_config,
 
     //! \ogs_file_param{prj__time_loop}
     parseTimeLoop(project_config.getConfigSubtree("time_loop"),
-                  output_directory);
+                  output_directory, std::move(chemical_solver_interface));
 }
 
 void ProjectData::parseProcessVariables(
@@ -501,15 +502,18 @@ void ProjectData::parseMedia(
     }
 }
 
-void ProjectData::parseChemicalSolverInterface(
+std::unique_ptr<ChemistryLib::ChemicalSolverInterface>
+ProjectData::parseChemicalSolverInterface(
     boost::optional<BaseLib::ConfigTree> const& config,
     std::string const& output_directory)
 {
     if (!config)
     {
-        return;
+        return nullptr;
     }
 
+    std::unique_ptr<ChemistryLib::ChemicalSolverInterface>
+        chemical_solver_interface;
 #ifdef OGS_BUILD_PROCESS_COMPONENTTRANSPORT
     INFO(
         "Ready for initializing interface to a chemical solver for water "
@@ -525,10 +529,9 @@ void ProjectData::parseChemicalSolverInterface(
             "Configuring phreeqc interface for water chemistry "
             "calculation using file-based approach.");
 
-        _chemical_solver_interface =
-            ChemistryLib::createChemicalSolverInterface<
-                ChemistryLib::ChemicalSolver::Phreeqc>(_mesh_vec, *config,
-                                                       output_directory);
+        chemical_solver_interface = ChemistryLib::createChemicalSolverInterface<
+            ChemistryLib::ChemicalSolver::Phreeqc>(_mesh_vec, *config,
+                                                   output_directory);
     }
     else if (boost::iequals(chemical_solver, "PhreeqcKernel"))
     {
@@ -553,11 +556,14 @@ void ProjectData::parseChemicalSolverInterface(
         "the present, water chemistry calculation is only available for "
         "component transport process.");
 #endif
+    return chemical_solver_interface;
 }
 
-void ProjectData::parseProcesses(BaseLib::ConfigTree const& processes_config,
-                                 std::string const& project_directory,
-                                 std::string const& output_directory)
+void ProjectData::parseProcesses(
+    BaseLib::ConfigTree const& processes_config,
+    std::string const& project_directory,
+    std::string const& output_directory,
+    ChemistryLib::ChemicalSolverInterface* const chemical_solver_interface)
 {
     (void)project_directory;  // to avoid compilation warning
     (void)output_directory;   // to avoid compilation warning
@@ -728,7 +734,7 @@ void ProjectData::parseProcesses(BaseLib::ConfigTree const& processes_config,
                     name, *_mesh_vec[0], std::move(jacobian_assembler),
                     _process_variables, _parameters, integration_order,
                     process_config, _mesh_vec, output_directory, _media,
-                    _chemical_solver_interface);
+                    chemical_solver_interface);
         }
         else
 #endif
@@ -1022,14 +1028,17 @@ void ProjectData::parseProcesses(BaseLib::ConfigTree const& processes_config,
     }
 }
 
-void ProjectData::parseTimeLoop(BaseLib::ConfigTree const& config,
-                                std::string const& output_directory)
+void ProjectData::parseTimeLoop(
+    BaseLib::ConfigTree const& config,
+    std::string const& output_directory,
+    std::unique_ptr<ChemistryLib::ChemicalSolverInterface>&&
+        chemical_solver_interface)
 {
     DBUG("Reading time loop configuration.");
 
     _time_loop = ProcessLib::createTimeLoop(
         config, output_directory, _processes, _nonlinear_solvers, _mesh_vec,
-        _chemical_solver_interface);
+        std::move(chemical_solver_interface));
 
     if (!_time_loop)
     {
diff --git a/Applications/ApplicationsLib/ProjectData.h b/Applications/ApplicationsLib/ProjectData.h
index 19b94b239f1..a12704bee55 100644
--- a/Applications/ApplicationsLib/ProjectData.h
+++ b/Applications/ApplicationsLib/ProjectData.h
@@ -99,13 +99,17 @@ private:
     /// Parses the processes configuration and creates new processes for each
     /// process entry passing the corresponding subtree to the process
     /// constructor.
-    void parseProcesses(BaseLib::ConfigTree const& processes_config,
-                        std::string const& project_directory,
-                        std::string const& output_directory);
+    void parseProcesses(
+        BaseLib::ConfigTree const& processes_config,
+        std::string const& project_directory,
+        std::string const& output_directory,
+        ChemistryLib::ChemicalSolverInterface* chemical_solver_interface);
 
     /// Parses the time loop configuration.
     void parseTimeLoop(BaseLib::ConfigTree const& config,
-                       const std::string& output_directory);
+                       const std::string& output_directory,
+                       std::unique_ptr<ChemistryLib::ChemicalSolverInterface>&&
+                           chemical_solver_interface);
 
     void parseLinearSolvers(BaseLib::ConfigTree const& config);
 
@@ -113,7 +117,8 @@ private:
 
     void parseCurves(boost::optional<BaseLib::ConfigTree> const& config);
 
-    void parseChemicalSolverInterface(
+    std::unique_ptr<ChemistryLib::ChemicalSolverInterface>
+    parseChemicalSolverInterface(
         boost::optional<BaseLib::ConfigTree> const& config,
         const std::string& output_directory);
 
@@ -138,7 +143,4 @@ private:
     std::map<std::string,
              std::unique_ptr<MathLib::PiecewiseLinearInterpolation>>
         _curves;
-
-    std::unique_ptr<ChemistryLib::ChemicalSolverInterface>
-        _chemical_solver_interface;
 };
diff --git a/ProcessLib/ComponentTransport/CreateComponentTransportProcess.cpp b/ProcessLib/ComponentTransport/CreateComponentTransportProcess.cpp
index 9f17e7ee508..2c549d84290 100644
--- a/ProcessLib/ComponentTransport/CreateComponentTransportProcess.cpp
+++ b/ProcessLib/ComponentTransport/CreateComponentTransportProcess.cpp
@@ -85,8 +85,7 @@ std::unique_ptr<Process> createComponentTransportProcess(
     std::vector<std::unique_ptr<MeshLib::Mesh>> const& meshes,
     std::string const& output_directory,
     std::map<int, std::shared_ptr<MaterialPropertyLib::Medium>> const& media,
-    std::unique_ptr<ChemistryLib::ChemicalSolverInterface> const&
-        chemical_solver_interface)
+    ChemistryLib::ChemicalSolverInterface* const chemical_solver_interface)
 {
     //! \ogs_file_param{prj__processes__process__type}
     config.checkConfigParameter("type", "ComponentTransport");
diff --git a/ProcessLib/ComponentTransport/CreateComponentTransportProcess.h b/ProcessLib/ComponentTransport/CreateComponentTransportProcess.h
index c52a8d3e33a..c95b5f2562b 100644
--- a/ProcessLib/ComponentTransport/CreateComponentTransportProcess.h
+++ b/ProcessLib/ComponentTransport/CreateComponentTransportProcess.h
@@ -38,7 +38,6 @@ std::unique_ptr<Process> createComponentTransportProcess(
     std::vector<std::unique_ptr<MeshLib::Mesh>> const& meshes,
     std::string const& output_directory,
     std::map<int, std::shared_ptr<MaterialPropertyLib::Medium>> const& media,
-    std::unique_ptr<ChemistryLib::ChemicalSolverInterface> const&
-        chemical_solver_interface);
+    ChemistryLib::ChemicalSolverInterface* chemical_solver_interface);
 }  // namespace ComponentTransport
 }  // namespace ProcessLib
diff --git a/ProcessLib/CreateTimeLoop.cpp b/ProcessLib/CreateTimeLoop.cpp
index 19cb21a6399..6a422d6e86f 100644
--- a/ProcessLib/CreateTimeLoop.cpp
+++ b/ProcessLib/CreateTimeLoop.cpp
@@ -25,7 +25,7 @@ std::unique_ptr<TimeLoop> createTimeLoop(
     const std::map<std::string, std::unique_ptr<NumLib::NonlinearSolverBase>>&
         nonlinear_solvers,
     std::vector<std::unique_ptr<MeshLib::Mesh>> const& meshes,
-    std::unique_ptr<ChemistryLib::ChemicalSolverInterface>&
+    std::unique_ptr<ChemistryLib::ChemicalSolverInterface>&&
         chemical_solver_interface)
 {
     auto const& coupling_config
diff --git a/ProcessLib/CreateTimeLoop.h b/ProcessLib/CreateTimeLoop.h
index a6000261acc..c005c61c32e 100644
--- a/ProcessLib/CreateTimeLoop.h
+++ b/ProcessLib/CreateTimeLoop.h
@@ -50,7 +50,7 @@ std::unique_ptr<TimeLoop> createTimeLoop(
     std::map<std::string, std::unique_ptr<NumLib::NonlinearSolverBase>> const&
         nonlinear_solvers,
     std::vector<std::unique_ptr<MeshLib::Mesh>> const& meshes,
-    std::unique_ptr<ChemistryLib::ChemicalSolverInterface>&
+    std::unique_ptr<ChemistryLib::ChemicalSolverInterface>&&
         chemical_solver_interface);
 
 }  // namespace ProcessLib
-- 
GitLab