2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2013,2014, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
46 #include "tng/tng_io.h"
49 #include "gromacs/legacyheaders/copyrite.h"
50 #include "gromacs/legacyheaders/gmx_fatal.h"
51 #include "gromacs/legacyheaders/main.h"
52 #include "gromacs/legacyheaders/physics.h"
53 #include "gromacs/math/utilities.h"
54 #include "gromacs/utility/common.h"
55 #include "gromacs/utility/gmxassert.h"
56 #include "gromacs/utility/programcontext.h"
59 static const char *modeToVerb(char mode)
73 gmx_fatal(FARGS, "Invalid file opening mode %c", mode);
78 void gmx_tng_open(const char *filename,
80 tng_trajectory_t *tng)
83 /* First check whether we have to make a backup,
84 * only for writing, not for read or append.
89 /* only make backups for normal gromacs */
90 make_backup(filename);
94 /* tng must not be pointing at already allocated memory.
95 * Memory will be allocated by tng_util_trajectory_open() and must
96 * later on be freed by tng_util_trajectory_close(). */
97 if (TNG_SUCCESS != tng_util_trajectory_open(filename, mode, tng))
99 /* TNG does return more than one degree of error, but there is
100 no use case for GROMACS handling the non-fatal errors
103 "%s while opening %s for %s",
104 gmx_strerror("file"),
109 if (mode == 'w' || mode == 'a')
111 /* FIXME in TNG: When adding data to the header, subsequent blocks might get
112 * overwritten. This could be solved by moving the first trajectory
113 * frame set(s) to the end of the file. Could that cause other problems,
114 * e.g. when continuing a simulation? */
116 gmx_gethostname(hostname, 256);
119 tng_first_computer_name_set(*tng, hostname);
121 /* TODO: This should be implemented when the above fixme is done (adding data to
125 // tng_last_computer_name_set(*tng, hostname);
128 char programInfo[256];
129 const char *precisionString = "";
131 precisionString = " (double precision)";
133 sprintf(programInfo, "%.100s, %.128s%.24s",
134 gmx::getProgramContext().displayName(),
135 GromacsVersion(), precisionString);
138 tng_first_program_name_set(*tng, programInfo);
140 /* TODO: This should be implemented when the above fixme is done (adding data to
144 // tng_last_program_name_set(*tng, programInfo);
149 getlogin_r(username, 256);
152 tng_first_user_name_set(*tng, username);
154 /* TODO: This should be implemented when the above fixme is done (adding data to
158 // tng_last_user_name_set(*tng, username);
163 gmx_file("GROMACS was compiled without TNG support, cannot handle this file type");
164 GMX_UNUSED_VALUE(filename);
165 GMX_UNUSED_VALUE(mode);
166 GMX_UNUSED_VALUE(tng);
170 void gmx_tng_close(tng_trajectory_t *tng)
172 /* We have to check that tng is set because
173 * tng_util_trajectory_close wants to return a NULL in it, and
174 * gives a fatal error if it is NULL. */
178 tng_util_trajectory_close(tng);
181 GMX_UNUSED_VALUE(tng);
186 static void addTngMoleculeFromTopology(tng_trajectory_t tng,
187 const char *moleculeName,
188 const t_atoms *atoms,
189 gmx_int64_t numMolecules,
190 tng_molecule_t *tngMol)
192 tng_chain_t tngChain = NULL;
193 tng_residue_t tngRes = NULL;
195 if (tng_molecule_add(tng, moleculeName, tngMol) != TNG_SUCCESS)
197 gmx_file("Cannot add molecule to TNG molecular system.");
200 /* FIXME: The TNG atoms should contain mass and atomB info (for free
201 * energy calculations), i.e. in when it's available in TNG (2.0). */
202 for (int atomIndex = 0; atomIndex < atoms->nr; atomIndex++)
204 const t_atom *at = &atoms->atom[atomIndex];
205 /* FIXME: Currently the TNG API can only add atoms belonging to a
206 * residue and chain. Wait for TNG 2.0*/
209 const t_resinfo *resInfo = &atoms->resinfo[at->resind];
210 char chainName[2] = {resInfo->chainid, 0};
211 tng_atom_t tngAtom = NULL;
216 prevAtom = &atoms->atom[atomIndex - 1];
223 /* If this is the first atom or if the residue changed add the
224 * residue to the TNG molecular system. */
225 if (!prevAtom || resInfo != &atoms->resinfo[prevAtom->resind])
227 /* If this is the first atom or if the chain changed add
228 * the chain to the TNG molecular system. */
229 if (!prevAtom || resInfo->chainid !=
230 atoms->resinfo[prevAtom->resind].chainid)
232 tng_molecule_chain_add(tng, *tngMol, chainName,
235 /* FIXME: When TNG supports both residue index and residue
236 * number the latter should be used. Wait for TNG 2.0*/
237 tng_chain_residue_add(tng, tngChain, *resInfo->name, &tngRes);
239 tng_residue_atom_add(tng, tngRes, *(atoms->atomname[atomIndex]), *(atoms->atomtype[atomIndex]), &tngAtom);
242 tng_molecule_cnt_set(tng, *tngMol, numMolecules);
245 void gmx_tng_add_mtop(tng_trajectory_t tng,
246 const gmx_mtop_t *mtop)
249 const t_ilist *ilist;
254 /* No topology information available to add. */
258 for (int molIndex = 0; molIndex < mtop->nmolblock; molIndex++)
260 tng_molecule_t tngMol = NULL;
261 const gmx_moltype_t *molType =
262 &mtop->moltype[mtop->molblock[molIndex].type];
264 /* Add a molecule to the TNG trajectory with the same name as the
265 * current molecule. */
266 addTngMoleculeFromTopology(tng,
269 mtop->molblock[molIndex].nmol,
272 /* Bonds have to be deduced from interactions (constraints etc). Different
273 * interactions have different sets of parameters. */
274 /* Constraints are specified using two atoms */
275 for (i = 0; i < F_NRE; i++)
279 ilist = &molType->ilist[i];
283 while (j < ilist->nr)
285 tng_molecule_bond_add(tng, tngMol, ilist->iatoms[j], ilist->iatoms[j+1], &tngBond);
291 /* Settle is described using three atoms */
292 ilist = &molType->ilist[F_SETTLE];
296 while (j < ilist->nr)
298 tng_molecule_bond_add(tng, tngMol, ilist->iatoms[j], ilist->iatoms[j+1], &tngBond);
299 tng_molecule_bond_add(tng, tngMol, ilist->iatoms[j], ilist->iatoms[j+2], &tngBond);
306 /*! \libinternal \brief Compute greatest common divisor of n1 and n2
307 * if they are positive.
309 * If only one of n1 and n2 is positive, then return it.
310 * If neither n1 or n2 is positive, then return -1. */
312 greatest_common_divisor_if_positive(int n1, int n2)
316 return (0 >= n2) ? -1 : n2;
323 /* We have a non-trivial greatest common divisor to compute. */
324 return gmx_greatest_common_divisor(n1, n2);
327 /* By default try to write 100 frames (of actual output) in each frame set.
328 * This number is the number of outputs of the most frequently written data
329 * type per frame set.
330 * TODO for 5.1: Verify that 100 frames per frame set is efficient for most
331 * setups regarding compression efficiency and compression time. Make this
332 * a hidden command-line option? */
333 const int defaultFramesPerFrameSet = 100;
335 /*! \libinternal \brief Set the number of frames per frame
336 * set according to output intervals.
337 * The default is that 100 frames are written of the data
338 * that is written most often. */
339 static void tng_set_frames_per_frame_set(tng_trajectory_t tng,
340 const gmx_bool bUseLossyCompression,
341 const t_inputrec *ir)
345 /* Set the number of frames per frame set to contain at least
346 * defaultFramesPerFrameSet of the lowest common denominator of
347 * the writing interval of positions and velocities. */
348 /* FIXME after 5.0: consider nstenergy also? */
349 if (bUseLossyCompression)
351 gcd = ir->nstxout_compressed;
355 gcd = greatest_common_divisor_if_positive(ir->nstxout, ir->nstvout);
356 gcd = greatest_common_divisor_if_positive(gcd, ir->nstfout);
363 tng_num_frames_per_frame_set_set(tng, gcd * defaultFramesPerFrameSet);
366 /*! \libinternal \brief Set the data-writing intervals, and number of
367 * frames per frame set */
368 static void set_writing_intervals(tng_trajectory_t tng,
369 const gmx_bool bUseLossyCompression,
370 const t_inputrec *ir)
372 /* Define pointers to specific writing functions depending on if we
373 * write float or double data */
374 typedef tng_function_status (*set_writing_interval_func_pointer)(tng_trajectory_t,
382 set_writing_interval_func_pointer set_writing_interval = tng_util_generic_write_interval_double_set;
384 set_writing_interval_func_pointer set_writing_interval = tng_util_generic_write_interval_set;
386 int xout, vout, fout;
387 // int gcd = -1, lowest = -1;
390 tng_set_frames_per_frame_set(tng, bUseLossyCompression, ir);
392 if (bUseLossyCompression)
394 xout = ir->nstxout_compressed;
397 compression = TNG_TNG_COMPRESSION;
404 compression = TNG_GZIP_COMPRESSION;
408 set_writing_interval(tng, xout, 3, TNG_TRAJ_POSITIONS,
409 "POSITIONS", TNG_PARTICLE_BLOCK_DATA,
411 /* The design of TNG makes it awkward to try to write a box
412 * with multiple periodicities, which might be co-prime. Since
413 * the use cases for the box with a frame consisting only of
414 * velocities seem low, for now we associate box writing with
415 * position writing. */
416 set_writing_interval(tng, xout, 9, TNG_TRAJ_BOX_SHAPE,
417 "BOX SHAPE", TNG_NON_PARTICLE_BLOCK_DATA,
418 TNG_GZIP_COMPRESSION);
419 /* TODO: if/when we write energies to TNG also, reconsider how
420 * and when box information is written, because GROMACS
421 * behaviour pre-5.0 was to write the box with every
422 * trajectory frame and every energy frame, and probably
423 * people depend on this. */
425 /* TODO: If we need to write lambda values at steps when
426 * positions (or other data) are not also being written, then
427 * code in mdoutf.c will need to match however that is
429 set_writing_interval(tng, xout, 1, TNG_GMX_LAMBDA,
430 "LAMBDAS", TNG_NON_PARTICLE_BLOCK_DATA,
431 TNG_GZIP_COMPRESSION);
433 /* FIXME: gcd and lowest currently not used. */
434 // gcd = greatest_common_divisor_if_positive(gcd, xout);
435 // if (lowest < 0 || xout < lowest)
442 set_writing_interval(tng, ir->nstvout, 3, TNG_TRAJ_VELOCITIES,
443 "VELOCITIES", TNG_PARTICLE_BLOCK_DATA,
446 /* FIXME: gcd and lowest currently not used. */
447 // gcd = greatest_common_divisor_if_positive(gcd, vout);
448 // if (lowest < 0 || vout < lowest)
455 set_writing_interval(tng, ir->nstfout, 3, TNG_TRAJ_FORCES,
456 "FORCES", TNG_PARTICLE_BLOCK_DATA,
457 TNG_GZIP_COMPRESSION);
459 /* FIXME: gcd and lowest currently not used. */
460 // gcd = greatest_common_divisor_if_positive(gcd, fout);
461 // if (lowest < 0 || fout < lowest)
466 /* FIXME: See above. gcd interval for lambdas is disabled. */
469 // /* Lambdas written at an interval of the lowest common denominator
470 // * of other output */
471 // set_writing_interval(tng, gcd, 1, TNG_GMX_LAMBDA,
472 // "LAMBDAS", TNG_NON_PARTICLE_BLOCK_DATA,
473 // TNG_GZIP_COMPRESSION);
475 // if (gcd < lowest / 10)
477 // gmx_warning("The lowest common denominator of trajectory output is "
478 // "every %d step(s), whereas the shortest output interval "
479 // "is every %d steps.", gcd, lowest);
485 void gmx_tng_prepare_md_writing(tng_trajectory_t tng,
486 const gmx_mtop_t *mtop,
487 const t_inputrec *ir)
490 gmx_tng_add_mtop(tng, mtop);
491 set_writing_intervals(tng, FALSE, ir);
492 tng_time_per_frame_set(tng, ir->delta_t * PICO);
494 GMX_UNUSED_VALUE(tng);
495 GMX_UNUSED_VALUE(mtop);
496 GMX_UNUSED_VALUE(ir);
501 /* Check if all atoms in the molecule system are selected
502 * by a selection group of type specified by gtype. */
503 static gmx_bool all_atoms_selected(const gmx_mtop_t *mtop,
506 const gmx_moltype_t *molType;
507 const t_atoms *atoms;
509 /* Iterate through all atoms in the molecule system and
510 * check if they belong to a selection group of the
512 for (int molIndex = 0, i = 0; molIndex < mtop->nmoltype; molIndex++)
514 molType = &mtop->moltype[mtop->molblock[molIndex].type];
516 atoms = &molType->atoms;
518 for (int j = 0; j < mtop->molblock[molIndex].nmol; j++)
520 for (int atomIndex = 0; atomIndex < atoms->nr; atomIndex++, i++)
522 if (ggrpnr(&mtop->groups, gtype, i) != 0)
532 /* Create TNG molecules which will represent each of the selection
533 * groups for writing. But do that only if there is actually a
534 * specified selection group and it is not the whole system.
535 * TODO: Currently the only selection that is taken into account
536 * is egcCompressedX, but other selections should be added when
537 * e.g. writing energies is implemented.
539 static void add_selection_groups(tng_trajectory_t tng,
540 const gmx_mtop_t *mtop)
542 const gmx_moltype_t *molType;
543 const t_atoms *atoms;
545 const t_resinfo *resInfo;
546 const t_ilist *ilist;
549 tng_molecule_t mol, iterMol;
557 /* TODO: When the TNG molecules block is more flexible TNG selection
558 * groups should not need all atoms specified. It should be possible
559 * just to specify what molecules are selected (and/or which atoms in
560 * the molecule) and how many (if applicable). */
562 /* If no atoms are selected we do not need to create a
563 * TNG selection group molecule. */
564 if (mtop->groups.ngrpnr[egcCompressedX] == 0)
569 /* If all atoms are selected we do not have to create a selection
570 * group molecule in the TNG molecule system. */
571 if (all_atoms_selected(mtop, egcCompressedX))
576 /* The name of the TNG molecule containing the selection group is the
577 * same as the name of the selection group. */
578 nameIndex = *mtop->groups.grps[egcCompressedX].nm_ind;
579 groupName = *mtop->groups.grpname[nameIndex];
581 tng_molecule_alloc(tng, &mol);
582 tng_molecule_name_set(tng, mol, groupName);
583 tng_molecule_chain_add(tng, mol, "", &chain);
584 for (int molIndex = 0, i = 0; molIndex < mtop->nmoltype; molIndex++)
586 molType = &mtop->moltype[mtop->molblock[molIndex].type];
588 atoms = &molType->atoms;
590 for (int j = 0; j < mtop->molblock[molIndex].nmol; j++)
592 bool bAtomsAdded = FALSE;
593 for (int atomIndex = 0; atomIndex < atoms->nr; atomIndex++, i++)
598 if (ggrpnr(&mtop->groups, egcCompressedX, i) != 0)
602 at = &atoms->atom[atomIndex];
605 resInfo = &atoms->resinfo[at->resind];
606 /* FIXME: When TNG supports both residue index and residue
607 * number the latter should be used. */
608 res_name = *resInfo->name;
609 res_id = at->resind + 1;
613 res_name = (char *)"";
616 if (tng_chain_residue_find(tng, chain, res_name, res_id, &res)
619 /* Since there is ONE chain for selection groups do not keep the
620 * original residue IDs - otherwise there might be conflicts. */
621 tng_chain_residue_add(tng, chain, res_name, &res);
623 tng_residue_atom_w_id_add(tng, res, *(atoms->atomname[atomIndex]),
624 *(atoms->atomtype[atomIndex]),
625 atom_offset + atomIndex, &atom);
631 for (int k = 0; k < F_NRE; k++)
635 ilist = &molType->ilist[k];
639 while (l < ilist->nr)
642 atom1 = ilist->iatoms[l] + atom_offset;
643 atom2 = ilist->iatoms[l+1] + atom_offset;
644 if (ggrpnr(&mtop->groups, egcCompressedX, atom1) == 0 &&
645 ggrpnr(&mtop->groups, egcCompressedX, atom2) == 0)
647 tng_molecule_bond_add(tng, mol, ilist->iatoms[l],
648 ilist->iatoms[l+1], &tngBond);
655 /* Settle is described using three atoms */
656 ilist = &molType->ilist[F_SETTLE];
660 while (l < ilist->nr)
662 int atom1, atom2, atom3;
663 atom1 = ilist->iatoms[l] + atom_offset;
664 atom2 = ilist->iatoms[l+1] + atom_offset;
665 atom3 = ilist->iatoms[l+2] + atom_offset;
666 if (ggrpnr(&mtop->groups, egcCompressedX, atom1) == 0)
668 if (ggrpnr(&mtop->groups, egcCompressedX, atom2) == 0)
670 tng_molecule_bond_add(tng, mol, atom1,
673 if (ggrpnr(&mtop->groups, egcCompressedX, atom3) == 0)
675 tng_molecule_bond_add(tng, mol, atom1,
683 atom_offset += atoms->nr;
686 tng_molecule_existing_add(tng, &mol);
687 tng_molecule_cnt_set(tng, mol, 1);
688 tng_num_molecule_types_get(tng, &nMols);
689 for (gmx_int64_t k = 0; k < nMols; k++)
691 tng_molecule_of_index_get(tng, k, &iterMol);
696 tng_molecule_cnt_set(tng, iterMol, 0);
701 void gmx_tng_set_compression_precision(tng_trajectory_t tng,
705 tng_compression_precision_set(tng, prec);
707 GMX_UNUSED_VALUE(tng);
708 GMX_UNUSED_VALUE(prec);
712 void gmx_tng_prepare_low_prec_writing(tng_trajectory_t tng,
713 const gmx_mtop_t *mtop,
714 const t_inputrec *ir)
717 gmx_tng_add_mtop(tng, mtop);
718 add_selection_groups(tng, mtop);
719 set_writing_intervals(tng, TRUE, ir);
720 tng_time_per_frame_set(tng, ir->delta_t * PICO);
721 gmx_tng_set_compression_precision(tng, ir->x_compression_precision);
723 GMX_UNUSED_VALUE(tng);
724 GMX_UNUSED_VALUE(mtop);
725 GMX_UNUSED_VALUE(ir);
729 void gmx_fwrite_tng(tng_trajectory_t tng,
730 const gmx_bool bUseLossyCompression,
732 real elapsedPicoSeconds,
741 typedef tng_function_status (*write_data_func_pointer)(tng_trajectory_t,
751 static write_data_func_pointer write_data = tng_util_generic_with_time_double_write;
753 static write_data_func_pointer write_data = tng_util_generic_with_time_write;
755 double elapsedSeconds = elapsedPicoSeconds * PICO;
756 gmx_int64_t nParticles;
762 /* This function might get called when the type of the
763 compressed trajectory is actually XTC. So we exit and move
768 tng_num_particles_get(tng, &nParticles);
769 if (nAtoms != (int)nParticles)
771 tng_implicit_num_particles_set(tng, nAtoms);
774 if (bUseLossyCompression)
776 compression = TNG_TNG_COMPRESSION;
780 compression = TNG_GZIP_COMPRESSION;
783 /* The writing is done using write_data, which writes float or double
784 * depending on the GROMACS compilation. */
787 GMX_ASSERT(box, "Need a non-NULL box if positions are written");
789 if (write_data(tng, step, elapsedSeconds,
790 reinterpret_cast<const real *>(x),
791 3, TNG_TRAJ_POSITIONS, "POSITIONS",
792 TNG_PARTICLE_BLOCK_DATA,
793 compression) != TNG_SUCCESS)
795 gmx_file("Cannot write TNG trajectory frame; maybe you are out of disk space?");
797 /* TNG-MF1 compression only compresses positions and velocities. Use lossless
798 * compression for box shape regardless of output mode */
799 if (write_data(tng, step, elapsedSeconds,
800 reinterpret_cast<const real *>(box),
801 9, TNG_TRAJ_BOX_SHAPE, "BOX SHAPE",
802 TNG_NON_PARTICLE_BLOCK_DATA,
803 TNG_GZIP_COMPRESSION) != TNG_SUCCESS)
805 gmx_file("Cannot write TNG trajectory frame; maybe you are out of disk space?");
811 if (write_data(tng, step, elapsedSeconds,
812 reinterpret_cast<const real *>(v),
813 3, TNG_TRAJ_VELOCITIES, "VELOCITIES",
814 TNG_PARTICLE_BLOCK_DATA,
815 compression) != TNG_SUCCESS)
817 gmx_file("Cannot write TNG trajectory frame; maybe you are out of disk space?");
823 /* TNG-MF1 compression only compresses positions and velocities. Use lossless
824 * compression for forces regardless of output mode */
825 if (write_data(tng, step, elapsedSeconds,
826 reinterpret_cast<const real *>(f),
827 3, TNG_TRAJ_FORCES, "FORCES",
828 TNG_PARTICLE_BLOCK_DATA,
829 TNG_GZIP_COMPRESSION) != TNG_SUCCESS)
831 gmx_file("Cannot write TNG trajectory frame; maybe you are out of disk space?");
835 /* TNG-MF1 compression only compresses positions and velocities. Use lossless
836 * compression for lambdas regardless of output mode */
837 if (write_data(tng, step, elapsedSeconds,
838 reinterpret_cast<const real *>(&lambda),
839 1, TNG_GMX_LAMBDA, "LAMBDAS",
840 TNG_NON_PARTICLE_BLOCK_DATA,
841 TNG_GZIP_COMPRESSION) != TNG_SUCCESS)
843 gmx_file("Cannot write TNG trajectory frame; maybe you are out of disk space?");
846 GMX_UNUSED_VALUE(tng);
847 GMX_UNUSED_VALUE(bUseLossyCompression);
848 GMX_UNUSED_VALUE(step);
849 GMX_UNUSED_VALUE(elapsedPicoSeconds);
850 GMX_UNUSED_VALUE(lambda);
851 GMX_UNUSED_VALUE(box);
852 GMX_UNUSED_VALUE(nAtoms);
859 void fflush_tng(tng_trajectory_t tng)
866 tng_frame_set_premature_write(tng, TNG_USE_HASH);
868 GMX_UNUSED_VALUE(tng);
872 float gmx_tng_get_time_of_final_frame(tng_trajectory_t tng)
879 tng_num_frames_get(tng, &nFrames);
880 tng_util_time_of_frame_get(tng, nFrames - 1, &time);
885 GMX_UNUSED_VALUE(tng);