From 93bfe5a8ac500b470bf53ab56d647138889602e5 Mon Sep 17 00:00:00 2001
From: Cristian C Lalescu <Cristian.Lalescu@ds.mpg.de>
Date: Wed, 22 Jan 2020 15:46:08 +0100
Subject: [PATCH] particle output creates file if it doesn't exist

there was an implicit assumption that the file exists before.
---
 cpp/particles/particles_output_hdf5.hpp | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/cpp/particles/particles_output_hdf5.hpp b/cpp/particles/particles_output_hdf5.hpp
index 6978a45c..4fbd16c9 100644
--- a/cpp/particles/particles_output_hdf5.hpp
+++ b/cpp/particles/particles_output_hdf5.hpp
@@ -29,6 +29,7 @@
 #include <memory>
 #include <vector>
 #include <hdf5.h>
+#include <sys/stat.h>
 
 #include "abstract_particles_output.hpp"
 #include "scope_timer.hpp"
@@ -140,10 +141,23 @@ public:
         if(Parent::isInvolved()){
             if (Parent::getMyRank() == 0)
             {
-                hid_t file_id = H5Fopen(
+                bool file_exists = false;
+                {
+                    struct stat file_buffer;
+                    file_exists = (stat(filename.c_str(), &file_buffer) == 0);
+                }
+                hid_t file_id;
+                if (file_exists)
+                    file_id = H5Fopen(
                         filename.c_str(),
                         H5F_ACC_RDWR | H5F_ACC_DEBUG,
                         H5P_DEFAULT);
+                else
+                    file_id = H5Fcreate(
+                        filename.c_str(),
+                        H5F_ACC_EXCL | H5F_ACC_DEBUG,
+                        H5P_DEFAULT,
+                        H5P_DEFAULT);
                 assert(file_id >= 0);
                 bool group_exists = H5Lexists(
                         file_id,
-- 
GitLab