Change TNG file opening
authorPaul Bauer <paul.bauer.q@gmail.com>
Fri, 23 Feb 2018 14:33:33 +0000 (15:33 +0100)
committerMark Abraham <mark.j.abraham@gmail.com>
Mon, 26 Feb 2018 15:08:46 +0000 (16:08 +0100)
Change to TNG file opening to return a pointer the same way as open_trx
does. This is needed in a follow-up change so that no memory is lost
there.

Also made sure it is possible to open TNG files if no input file is
specified, or if the input file is not of TNG type.

Change-Id: I47cf1625815e640b399ed5a0ed5bbc9a3ead6e1b

src/gromacs/fileio/trxio.cpp
src/gromacs/fileio/trxio.h
src/gromacs/gmxana/gmx_trjcat.cpp
src/gromacs/gmxana/gmx_trjconv.cpp

index a9932699c4a49fbc5e2bb5df409408b34a3a02bb..86ac53d4e8881ef0d20d0aad2cf313d327148cee 100644 (file)
@@ -463,39 +463,36 @@ int write_trxframe_indexed(t_trxstatus *status, const t_trxframe *fr, int nind,
     return 0;
 }
 
-void trjtools_gmx_prepare_tng_writing(const char       *filename,
-                                      char              filemode,
-                                      t_trxstatus      *in,
-                                      t_trxstatus     **out,
-                                      const char       *infile,
-                                      const int         natoms,
-                                      const gmx_mtop_t *mtop,
-                                      const int        *index,
-                                      const char       *index_group_name)
+t_trxstatus *
+trjtools_gmx_prepare_tng_writing(const char       *filename,
+                                 char              filemode,
+                                 t_trxstatus      *in,
+                                 const char       *infile,
+                                 const int         natoms,
+                                 const gmx_mtop_t *mtop,
+                                 const int        *index,
+                                 const char       *index_group_name)
 {
     if (filemode != 'w' && filemode != 'a')
     {
         gmx_incons("Sorry, can only prepare for TNG output.");
     }
-
-    if (*out == nullptr)
-    {
-        snew((*out), 1);
-    }
-    status_init(*out);
+    t_trxstatus *out;
+    snew(out, 1);
+    status_init(out);
 
     if (in != nullptr)
     {
         gmx_prepare_tng_writing(filename,
                                 filemode,
                                 &in->tng,
-                                &(*out)->tng,
+                                &out->tng,
                                 natoms,
                                 mtop,
                                 index,
                                 index_group_name);
     }
-    else if (efTNG == fn2ftp(infile))
+    else if ((infile) && (efTNG == fn2ftp(infile)))
     {
         gmx_tng_trajectory_t tng_in;
         gmx_tng_open(infile, 'r', &tng_in);
@@ -503,12 +500,26 @@ void trjtools_gmx_prepare_tng_writing(const char       *filename,
         gmx_prepare_tng_writing(filename,
                                 filemode,
                                 &tng_in,
-                                &(*out)->tng,
+                                &out->tng,
+                                natoms,
+                                mtop,
+                                index,
+                                index_group_name);
+    }
+    else
+    {
+        // we start from a file that is not a tng file or have been unable to load the
+        // input file, so we need to populate the fields independently of it
+        gmx_prepare_tng_writing(filename,
+                                filemode,
+                                nullptr,
+                                &out->tng,
                                 natoms,
                                 mtop,
                                 index,
                                 index_group_name);
     }
+    return out;
 }
 
 void write_tng_frame(t_trxstatus *status,
index f338eccc2a7d667f43f847e9deeae45741ea6cbb..ee035a0fd8a95c1624be08e598f4235d9d1bbc8e 100644 (file)
@@ -100,20 +100,19 @@ int write_trx(t_trxstatus *status, int nind, const int *ind, const t_atoms *atom
  * v can be NULL.
  * atoms can be NULL for file types which don't need atom names.
  */
-
-void trjtools_gmx_prepare_tng_writing(const char               *filename,
-                                      char                      filemode,
-                                      t_trxstatus              *in,
-                                      t_trxstatus             **out,
-                                      const char               *infile,
-                                      const int                 natoms,
-                                      const struct gmx_mtop_t  *mtop,
-                                      const int                *index,
-                                      const char               *index_group_name);
+t_trxstatus *
+trjtools_gmx_prepare_tng_writing(const char               *filename,
+                                 char                      filemode,
+                                 t_trxstatus              *in,
+                                 const char               *infile,
+                                 const int                 natoms,
+                                 const struct gmx_mtop_t  *mtop,
+                                 const int                *index,
+                                 const char               *index_group_name);
 /* Sets up *out for writing TNG. If *in != NULL and contains a TNG trajectory
- * some data, e.g. molecule system, will be copied over from *in to *out.
+ * some data, e.g. molecule system, will be copied over from *in to the return value.
  * If *in == NULL a file name (infile) of a TNG file can be provided instead
- * and used for copying data to *out.
+ * and used for copying data to the return value.
  * If there is no TNG input natoms is used to create "implicit atoms" (no atom
  * or molecular data present). If natoms == -1 the number of atoms are
  * not known (or there is already a TNG molecule system to copy, in which case
index 6a97e3932b4134fc3719f298f1dd0910d588c8f3..144764792eaa4f3120b8e80a1d1727aa411d29cb 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
  * Copyright (c) 2001-2004, The GROMACS development team.
- * Copyright (c) 2013,2014,2015,2016,2017, by the GROMACS development team, led by
+ * Copyright (c) 2013,2014,2015,2016,2017,2018, by the GROMACS development team, led by
  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
  * and including many others, as listed in the AUTHORS file in the
  * top-level source directory and at http://www.gromacs.org.
@@ -656,13 +656,13 @@ int gmx_trjcat(int argc, char *argv[])
                 }
                 if (bIndex)
                 {
-                    trjtools_gmx_prepare_tng_writing(out_file, 'w', nullptr, &trxout,
-                                                     fnms[0], isize, nullptr, index, grpname);
+                    trxout = trjtools_gmx_prepare_tng_writing(out_file, 'w', nullptr,
+                                                              fnms[0], isize, nullptr, index, grpname);
                 }
                 else
                 {
-                    trjtools_gmx_prepare_tng_writing(out_file, 'w', nullptr, &trxout,
-                                                     fnms[0], -1, nullptr, nullptr, nullptr);
+                    trxout = trjtools_gmx_prepare_tng_writing(out_file, 'w', nullptr,
+                                                              fnms[0], -1, nullptr, nullptr, nullptr);
                 }
             }
             else
index fd6ed925759b211fb1d418c208f5d8fdccc00d4d..e2ec4eedb864889fd5515b0d3602ef93f9553b3e 100644 (file)
@@ -1360,15 +1360,14 @@ int gmx_trjconv(int argc, char *argv[])
             switch (ftp)
             {
                 case efTNG:
-                    trjtools_gmx_prepare_tng_writing(out_file,
-                                                     filemode[0],
-                                                     trxin,
-                                                     &trxout,
-                                                     nullptr,
-                                                     nout,
-                                                     mtop,
-                                                     index,
-                                                     grpnm);
+                    trxout = trjtools_gmx_prepare_tng_writing(out_file,
+                                                              filemode[0],
+                                                              trxin,
+                                                              nullptr,
+                                                              nout,
+                                                              mtop,
+                                                              index,
+                                                              grpnm);
                     break;
                 case efXTC:
                 case efTRR: