From dbf808fef9b586ea0efa528d4b54fbf4d27814c1 Mon Sep 17 00:00:00 2001
From: Lars Bilke <lars.bilke@ufz.de>
Date: Tue, 28 Feb 2017 14:45:54 +0100
Subject: [PATCH] [insitu] WIP implementation of insitu vis.

---
 Applications/CLI/CMakeLists.txt       |  4 ++
 Applications/CLI/ogs.cpp              | 22 ++++++--
 Applications/CMakeLists.txt           |  4 ++
 Applications/InSituLib/Adaptor.cpp    | 77 +++++++++++++++++++++++++++
 Applications/InSituLib/Adaptor.h      | 24 +++++++++
 Applications/InSituLib/CMakeLists.txt |  7 +++
 CMakeLists.txt                        |  1 +
 ProcessLib/CMakeLists.txt             |  3 ++
 ProcessLib/Output.cpp                 |  6 +++
 9 files changed, 143 insertions(+), 5 deletions(-)
 create mode 100644 Applications/InSituLib/Adaptor.cpp
 create mode 100644 Applications/InSituLib/Adaptor.h
 create mode 100644 Applications/InSituLib/CMakeLists.txt

diff --git a/Applications/CLI/CMakeLists.txt b/Applications/CLI/CMakeLists.txt
index 4f65bdb9446..5081da088fa 100644
--- a/Applications/CLI/CMakeLists.txt
+++ b/Applications/CLI/CMakeLists.txt
@@ -16,6 +16,10 @@ if(OGS_USE_PETSC)
     target_link_libraries(ogs ${PETSC_LIBRARIES})
 endif()
 
+if(OGS_INSITU)
+    target_link_libraries(ogs InSituLib)
+endif()
+
 ####################
 ### Tests ##########
 ####################
diff --git a/Applications/CLI/ogs.cpp b/Applications/CLI/ogs.cpp
index 0c503b81db0..fc36dbfb217 100644
--- a/Applications/CLI/ogs.cpp
+++ b/Applications/CLI/ogs.cpp
@@ -23,6 +23,7 @@
 #include "Applications/ApplicationsLib/LinearSolverLibrarySetup.h"
 #include "Applications/ApplicationsLib/LogogSetup.h"
 #include "Applications/ApplicationsLib/ProjectData.h"
+#include "Applications/InSituLib/Adaptor.h"
 #include "ProcessLib/UncoupledProcessesTimeLoop.h"
 
 #include "NumLib/NumericsConfig.h"
@@ -117,11 +118,11 @@ int main(int argc, char *argv[])
                                 BaseLib::extractPath(project_arg.getValue()),
                                 outdir_arg.getValue());
 
-            // Check intermediately that config parsing went fine.
-            project_config.checkAndInvalidate();
-            BaseLib::ConfigTree::assertNoSwallowedErrors();
-
-            BaseLib::ConfigTree::assertNoSwallowedErrors();
+#ifdef USE_INSITU
+            INFO("Initialize insitu scripts.");
+            if (auto t = project_config->getConfigSubtreeOptional("insitu"))
+                InSituLib::Initialize(t->getConfigSubtree("scripts"), BaseLib::extractPath(project_arg.getValue()));
+#endif
 
             INFO("Initialize processes.");
             for (auto& p : project.getProcesses())
@@ -129,12 +130,23 @@ int main(int argc, char *argv[])
                 p.second->initialize();
             }
 
+            // Check intermediately that config parsing went fine.
+            project_config.checkAndInvalidate();
+            BaseLib::ConfigTree::assertNoSwallowedErrors();
+
+            BaseLib::ConfigTree::assertNoSwallowedErrors();
+
             BaseLib::ConfigTree::assertNoSwallowedErrors();
 
             INFO("Solve processes.");
 
             auto& time_loop = project.getTimeLoop();
             solver_succeeded = time_loop.loop();
+
+#ifdef USE_INSITU
+            if (project_config->getConfigSubtreeOptional("insitu"))
+                InSituLib::Finalize();
+ #endif
         }  // This nested scope ensures that everything that could possibly
            // possess a ConfigTree is destructed before the final check below is
            // done.
diff --git a/Applications/CMakeLists.txt b/Applications/CMakeLists.txt
index 9db2c81a5af..35289fb8dd5 100644
--- a/Applications/CMakeLists.txt
+++ b/Applications/CMakeLists.txt
@@ -17,3 +17,7 @@ endif() # OGS_BUILD_GUI
 if(OGS_BUILD_CLI)
     add_subdirectory(CLI)
 endif() # OGS_BUILD_CLI
+
+if(OGS_INSITU)
+    add_subdirectory(InSituLib)
+endif()
diff --git a/Applications/InSituLib/Adaptor.cpp b/Applications/InSituLib/Adaptor.cpp
new file mode 100644
index 00000000000..76f1a0f8994
--- /dev/null
+++ b/Applications/InSituLib/Adaptor.cpp
@@ -0,0 +1,77 @@
+/**
+ * \copyright
+ * Copyright (c) 2012-2017, OpenGeoSys Community (http://www.opengeosys.org)
+ *            Distributed under a Modified BSD License.
+ *              See accompanying file LICENSE.txt or
+ *              http://www.opengeosys.org/project/license
+ *
+ */
+
+#include "Adaptor.h"
+
+#include <vtkCPDataDescription.h>
+#include <vtkCPInputDataDescription.h>
+#include <vtkCPProcessor.h>
+#include <vtkCPPythonScriptPipeline.h>
+#include <vtkNew.h>
+#include <vtkUnstructuredGrid.h>
+
+#include "MeshLib/Mesh.h"
+#include "MeshLib/Vtk/VtkMappedMeshSource.h"
+
+namespace InSituLib
+{
+vtkCPProcessor* Processor = NULL;
+
+void Initialize(BaseLib::ConfigTree const & scripts_config, std::string const & path)
+{
+    if (Processor == NULL)
+    {
+        Processor = vtkCPProcessor::New();
+        Processor->Initialize();
+    }
+    else
+    {
+        Processor->RemoveAllPipelines();
+    }
+    for (auto script_config : scripts_config.getConfigSubtreeList("script"))
+    {
+        std::stringstream ss;
+        ss << path << script_config.getConfigParameter<std::string>("name");
+        vtkNew<vtkCPPythonScriptPipeline> pipeline;
+        pipeline->Initialize(ss.str().c_str());
+        Processor->AddPipeline(pipeline.GetPointer());
+    }
+}
+void Finalize()
+{
+    if (Processor)
+    {
+        Processor->Delete();
+        Processor = NULL;
+    }
+}
+void CoProcess(MeshLib::Mesh const& mesh, double const time,
+               unsigned int const timeStep, bool const lastTimeStep)
+{
+    vtkNew<vtkCPDataDescription> dataDescription;
+    dataDescription->AddInput("input");
+    dataDescription->SetTimeData(time, timeStep);
+    if (lastTimeStep == true)
+    {
+        // assume that we want to all the pipelines to execute if it
+        // is the last time step.
+        dataDescription->ForceOutputOn();
+    }
+    if (Processor->RequestDataDescription(dataDescription.GetPointer()) != 0)
+    {
+        INFO("Start InSitu process.");
+        vtkNew<MeshLib::VtkMappedMeshSource> vtkSource;
+        vtkSource->SetMesh(&mesh);
+        vtkSource->Update();
+        dataDescription->GetInputDescriptionByName("input")->SetGrid(vtkSource->GetOutput());
+        Processor->CoProcess(dataDescription.GetPointer());
+        INFO("End InSitu process.");
+    }
+}
+}  // namespace
diff --git a/Applications/InSituLib/Adaptor.h b/Applications/InSituLib/Adaptor.h
new file mode 100644
index 00000000000..5d3ec0b4e45
--- /dev/null
+++ b/Applications/InSituLib/Adaptor.h
@@ -0,0 +1,24 @@
+/**
+ * \copyright
+ * Copyright (c) 2012-2017, OpenGeoSys Community (http://www.opengeosys.org)
+ *            Distributed under a Modified BSD License.
+ *              See accompanying file LICENSE.txt or
+ *              http://www.opengeosys.org/project/license
+ *
+ */
+
+#pragma once
+
+#include "BaseLib/ConfigTree.h"
+
+namespace MeshLib {
+    class Mesh;
+}
+
+namespace InSituLib {
+
+void Initialize(BaseLib::ConfigTree const & scripts_config, std::string const & path);
+void Finalize();
+void CoProcess( MeshLib::Mesh const & mesh, double const time, unsigned int const timeStep, bool const lastTimeStep);
+
+}
diff --git a/Applications/InSituLib/CMakeLists.txt b/Applications/InSituLib/CMakeLists.txt
new file mode 100644
index 00000000000..a8b55101ce4
--- /dev/null
+++ b/Applications/InSituLib/CMakeLists.txt
@@ -0,0 +1,7 @@
+# Source files
+GET_SOURCE_FILES(SOURCES)
+
+# Library
+add_library(InSituLib ${SOURCES})
+
+# target_link_libraries(InSituLib  )
diff --git a/CMakeLists.txt b/CMakeLists.txt
index c344513177d..f7e5ada0b24 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -213,6 +213,7 @@ include(VtkModules)
 if(OGS_INSITU)
     include(ExternalProjectCatalyst)
     include("${PARAVIEW_USE_FILE}")
+    add_definitions(-DUSE_INSITU)
 else()
     include(ExternalProjectVtk)
 endif()
diff --git a/ProcessLib/CMakeLists.txt b/ProcessLib/CMakeLists.txt
index 029d9bc4c3c..3295d09f505 100644
--- a/ProcessLib/CMakeLists.txt
+++ b/ProcessLib/CMakeLists.txt
@@ -70,3 +70,6 @@ endif()
 if(TARGET Boost)
     add_dependencies(ProcessLib Boost)
 endif()
+if(OGS_INSITU)
+    target_link_libraries(ProcessLib InSituLib)
+endif()
diff --git a/ProcessLib/Output.cpp b/ProcessLib/Output.cpp
index adb2b2ebd41..3036cea5566 100644
--- a/ProcessLib/Output.cpp
+++ b/ProcessLib/Output.cpp
@@ -17,6 +17,7 @@
 
 #include "BaseLib/FileTools.h"
 #include "BaseLib/RunTime.h"
+#include "Applications/InSituLib/Adaptor.h"
 
 namespace
 {
@@ -124,6 +125,11 @@ void Output::doOutputAlways(Process const& process,
 
     INFO("[time] Output of timestep %d took %g s.", timestep,
          time_output.elapsed());
+
+#ifdef USE_INSITU
+    // TODO: get number of timesteps
+    InSituLib::CoProcess(process.getMesh(), t, timestep, false);
+#endif
 }
 
 void Output::doOutput(Process const& process,
-- 
GitLab