From fb9deb4bef7e323b709568d214205a15dd78359d Mon Sep 17 00:00:00 2001
From: acrovato <a.crovato@uliege.be>
Date: Fri, 6 Dec 2024 11:14:40 +0100
Subject: [PATCH] Add capability to save mesh and results as list in Gmsh for
 adaptive mesh refinement

---
 tbox/src/wGmshExport.cpp | 67 ++++++++++++++++++++++++++++++++++++++++
 tbox/src/wGmshExport.h   |  1 +
 tbox/src/wMshExport.cpp  |  4 +++
 tbox/src/wMshExport.h    |  1 +
 4 files changed, 73 insertions(+)

diff --git a/tbox/src/wGmshExport.cpp b/tbox/src/wGmshExport.cpp
index 6487d0d..e9df489 100644
--- a/tbox/src/wGmshExport.cpp
+++ b/tbox/src/wGmshExport.cpp
@@ -298,6 +298,73 @@ void GmshExport::save(std::string const &fname, Results const &r) const
     std::cout << "done" << std::endl;
 }
 
+/**
+ * @brief Save results as a list view, i.e. with the underlying mesh, on disk
+ * @todo Check if useful with newer Gmsh versions
+ * @todo Remove if remeshing is not done through Gmsh
+ */
+void GmshExport::saveList(std::string const &fname, Results const &r) const
+{
+    std::string wname = fname + ".pos";
+    std::cout << "writing file: " << wname << "... " << std::flush;
+
+    FILE *file;
+    file = fopen(wname.c_str(), "wt");
+    if (!file)
+        throw std::runtime_error("tbox::GmshExport::saveList Error opening file!\n");
+
+    if (!r.scalars_at_elems.empty() || !r.vectors_at_nodes.empty() || !r.vectors_at_elems.empty() || !r.tensors_at_nodes.empty() || !r.tensors_at_elems.empty())
+        throw std::runtime_error("tbox::GmshExport::saveList Only scalars_at_nodes Result type is supported!\n");
+
+    for (auto &p : r.scalars_at_nodes)
+    {
+        // name of view
+        fprintf(file, "View \"%s\" {\n", p.first.c_str());
+        // element and data
+        for (auto e : msh->elems)
+        {
+            std::string str;
+            if (e->type() == ElType::POINT1)
+            {
+                str = "SP(";
+            }
+            else if (e->type() == ElType::LINE2)
+            {
+                str = "SL(";
+            }
+            else if (e->type() == ElType::TRI3)
+            {
+                str = "ST(";
+            }
+            else if (e->type() == ElType::TETRA4)
+            {
+                str = "SS(";
+            }
+            else
+                throw std::runtime_error("tbox::GmshExport::saveList Element type not supported!\n");
+            // node coordinates and values
+            for (auto n : e->nodes)
+                str += std::to_string(n->pos(0)) + "," + std::to_string(n->pos(1)) + "," + std::to_string(n->pos(2)) + ",";
+            str.erase(str.length() - 1);
+            str += "){";
+            for (auto n : e->nodes)
+            {
+                if (std::isnan((*(p.second))[n->row]))
+                    str += "-nan,"; // find a nicer way to handle nan
+                else
+                    str += std::to_string((*(p.second))[n->row]) + ",";
+            }
+            str.erase(str.length() - 1);
+            str += "};\n";
+            fprintf(file, "%s", str.c_str());
+        }
+        fprintf(file, "};\n");
+    }
+    // close file
+    fclose(file);
+    std::cout << "done" << std::endl;
+}
+
 void GmshExport::write(std::ostream &out) const
 {
     out << "GmshExport on " << *msh << std::endl;
diff --git a/tbox/src/wGmshExport.h b/tbox/src/wGmshExport.h
index dc74bf3..0d15cbf 100644
--- a/tbox/src/wGmshExport.h
+++ b/tbox/src/wGmshExport.h
@@ -39,6 +39,7 @@ public:
     virtual void save(std::string const &fname) const override;
 #ifndef SWIG
     virtual void save(std::string const &fname, Results const &r) const override;
+    virtual void saveList(std::string const &fname, Results const &r) const override;
 #endif
 
     virtual void write(std::ostream &out) const override;
diff --git a/tbox/src/wMshExport.cpp b/tbox/src/wMshExport.cpp
index 3eca104..6327e30 100644
--- a/tbox/src/wMshExport.cpp
+++ b/tbox/src/wMshExport.cpp
@@ -32,3 +32,7 @@ void MshExport::save(std::string const &fname, Results const &r) const
 {
     throw std::runtime_error("tbox::MshExport::save not implemented!\n");
 }
+void MshExport::saveList(std::string const &fname, Results const &r) const
+{
+    throw std::runtime_error("tbox::MshExport::save_ not implemented!\n");
+}
diff --git a/tbox/src/wMshExport.h b/tbox/src/wMshExport.h
index 19d6e3a..ec732a2 100644
--- a/tbox/src/wMshExport.h
+++ b/tbox/src/wMshExport.h
@@ -54,6 +54,7 @@ public:
     virtual void save(std::string const &fname) const;
 #ifndef SWIG
     virtual void save(std::string const &fname, Results const &r) const;
+    virtual void saveList(std::string const &fname, Results const &r) const;
 #endif
 };
 
-- 
GitLab