From 693070a8828ebf9463cdee00116f39c53eaf7d19 Mon Sep 17 00:00:00 2001
From: Rafael Ravedutti <rafaelravedutti@gmail.com>
Date: Mon, 6 Nov 2023 16:05:07 +0100
Subject: [PATCH] Do not write infinite particles into VTK file

Signed-off-by: Rafael Ravedutti <rafaelravedutti@gmail.com>
---
 runtime/vtk.hpp | 29 ++++++++++++++++++++++-------
 1 file changed, 22 insertions(+), 7 deletions(-)

diff --git a/runtime/vtk.hpp b/runtime/vtk.hpp
index 349b66c..3ae69e1 100644
--- a/runtime/vtk.hpp
+++ b/runtime/vtk.hpp
@@ -12,8 +12,9 @@ void vtk_write_data(PairsSimulation *ps, const char *filename, int start, int en
     std::string output_filename(filename);
     auto masses = ps->getAsFloatProperty(ps->getPropertyByName("mass"));
     auto positions = ps->getAsVectorProperty(ps->getPropertyByName("position"));
-    const int n = end - start;
+    auto flags = ps->getAsIntegerProperty(ps->getPropertyByName("flags"));
     const int prec = 8;
+    int n = end - start;
     std::ostringstream filename_oss;
 
     if(frequency != 0 && timestep % frequency != 0) {
@@ -31,6 +32,12 @@ void vtk_write_data(PairsSimulation *ps, const char *filename, int start, int en
     ps->copyPropertyToHost(masses);
     ps->copyPropertyToHost(positions);
 
+    for(int i = start; i < end; i++) {
+        if(!(flags(i) & FLAGS_INFINITE)) {
+            n--;
+        }
+    }
+
     if(out_file.is_open()) {
         out_file << "# vtk DataFile Version 2.0\n";
         out_file << "Particle data\n";
@@ -39,21 +46,27 @@ void vtk_write_data(PairsSimulation *ps, const char *filename, int start, int en
         out_file << "POINTS " << n << " double\n";
 
         for(int i = start; i < end; i++) {
-            out_file << std::fixed << std::setprecision(prec) << positions(i, 0) << " ";
-            out_file << std::fixed << std::setprecision(prec) << positions(i, 1) << " ";
-            out_file << std::fixed << std::setprecision(prec) << positions(i, 2) << "\n";
+            if(!(flags(i) & FLAGS_INFINITE)) {
+                out_file << std::fixed << std::setprecision(prec) << positions(i, 0) << " ";
+                out_file << std::fixed << std::setprecision(prec) << positions(i, 1) << " ";
+                out_file << std::fixed << std::setprecision(prec) << positions(i, 2) << "\n";
+            }
         }
 
         out_file << "\n\n";
         out_file << "CELLS " << n << " " << (n * 2) << "\n";
         for(int i = start; i < end; i++) {
-            out_file << "1 " << (i - start) << "\n";
+            if(!(flags(i) & FLAGS_INFINITE)) {
+                out_file << "1 " << (i - start) << "\n";
+            }
         }
 
         out_file << "\n\n";
         out_file << "CELL_TYPES " << n << "\n";
         for(int i = start; i < end; i++) {
-            out_file << "1\n";
+            if(!(flags(i) & FLAGS_INFINITE)) {
+                out_file << "1\n";
+            }
         }
 
         out_file << "\n\n";
@@ -61,7 +74,9 @@ void vtk_write_data(PairsSimulation *ps, const char *filename, int start, int en
         out_file << "SCALARS mass double\n";
         out_file << "LOOKUP_TABLE default\n";
         for(int i = start; i < end; i++) {
-            out_file << std::fixed << std::setprecision(prec) << masses(i) << "\n";
+            if(!(flags(i) & FLAGS_INFINITE)) {
+                out_file << std::fixed << std::setprecision(prec) << masses(i) << "\n";
+            }
         }
 
         out_file << "\n\n";
-- 
GitLab