2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2013,2014,2015,2018, 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.
49 #include "tng/tng_io.h"
52 #include "gromacs/math/units.h"
53 #include "gromacs/math/utilities.h"
54 #include "gromacs/mdtypes/inputrec.h"
55 #include "gromacs/topology/ifunc.h"
56 #include "gromacs/topology/topology.h"
57 #include "gromacs/trajectory/trajectoryframe.h"
58 #include "gromacs/utility/basedefinitions.h"
59 #include "gromacs/utility/baseversion.h"
60 #include "gromacs/utility/fatalerror.h"
61 #include "gromacs/utility/futil.h"
62 #include "gromacs/utility/gmxassert.h"
63 #include "gromacs/utility/programcontext.h"
64 #include "gromacs/utility/smalloc.h"
65 #include "gromacs/utility/sysinfo.h"
66 #include "gromacs/utility/unique_cptr.h"
69 using tng_trajectory_t = void *;
72 /*! \brief Gromacs Wrapper around tng datatype
74 * This could in principle hold any GROMACS-specific requirements not yet
75 * implemented in or not relevant to the TNG library itself. However, for now
76 * we only use it to handle some shortcomings we have discovered, where the TNG
77 * API itself is a bit fragile and can end up overwriting data if called several
78 * times with the same frame number.
79 * The logic to determine the time per step was also a bit fragile. This is not
80 * critical, but since we anyway need a wrapper for ensuring unique frame
81 * numbers, we can also use it to store the time of the first step and use that
82 * to derive a slightly better/safer estimate of the time per step.
84 * At some future point where we have a second-generation TNG API we should
85 * consider removing this again.
87 struct gmx_tng_trajectory
89 tng_trajectory_t tng; //!< Actual TNG handle (pointer)
90 bool lastStepDataIsValid; //!< True if lastStep has been set
91 std::int64_t lastStep; //!< Index/step used for last frame
92 bool lastTimeDataIsValid; //!< True if lastTime has been set
93 double lastTime; //!< Time of last frame (TNG unit is seconds)
94 bool timePerFrameIsSet; //!< True if we have set the time per frame
95 int boxOutputInterval; //!< Number of steps between the output of box size
96 int lambdaOutputInterval; //!< Number of steps between the output of lambdas
100 static const char *modeToVerb(char mode)
115 gmx_fatal(FARGS, "Invalid file opening mode %c", mode);
121 void gmx_tng_open(const char *filename,
123 gmx_tng_trajectory_t *gmx_tng)
126 /* First check whether we have to make a backup,
127 * only for writing, not for read or append.
131 make_backup(filename);
134 *gmx_tng = new gmx_tng_trajectory;
135 (*gmx_tng)->lastStepDataIsValid = false;
136 (*gmx_tng)->lastTimeDataIsValid = false;
137 (*gmx_tng)->timePerFrameIsSet = false;
138 tng_trajectory_t * tng = &(*gmx_tng)->tng;
140 /* tng must not be pointing at already allocated memory.
141 * Memory will be allocated by tng_util_trajectory_open() and must
142 * later on be freed by tng_util_trajectory_close(). */
143 if (TNG_SUCCESS != tng_util_trajectory_open(filename, mode, tng))
145 /* TNG does return more than one degree of error, but there is
146 no use case for GROMACS handling the non-fatal errors
149 "File I/O error while opening %s for %s",
154 if (mode == 'w' || mode == 'a')
157 gmx_gethostname(hostname, 256);
160 tng_first_computer_name_set(*tng, hostname);
164 tng_last_computer_name_set(*tng, hostname);
167 char programInfo[256];
168 const char *precisionString = "";
170 precisionString = " (double precision)";
172 sprintf(programInfo, "%.100s %.128s%.24s",
173 gmx::getProgramContext().displayName(),
174 gmx_version(), precisionString);
177 tng_first_program_name_set(*tng, programInfo);
181 tng_last_program_name_set(*tng, programInfo);
185 if (!gmx_getusername(username, 256))
189 tng_first_user_name_set(*tng, username);
193 tng_last_user_name_set(*tng, username);
194 tng_file_headers_write(*tng, TNG_USE_HASH);
199 gmx_file("GROMACS was compiled without TNG support, cannot handle this file type");
200 GMX_UNUSED_VALUE(filename);
201 GMX_UNUSED_VALUE(mode);
202 GMX_UNUSED_VALUE(gmx_tng);
206 void gmx_tng_close(gmx_tng_trajectory_t *gmx_tng)
208 /* We have to check that tng is set because
209 * tng_util_trajectory_close wants to return a NULL in it, and
210 * gives a fatal error if it is NULL. */
212 if (gmx_tng == nullptr || *gmx_tng == nullptr)
216 tng_trajectory_t * tng = &(*gmx_tng)->tng;
220 tng_util_trajectory_close(tng);
226 GMX_UNUSED_VALUE(gmx_tng);
231 static void addTngMoleculeFromTopology(gmx_tng_trajectory_t gmx_tng,
232 const char *moleculeName,
233 const t_atoms *atoms,
234 int64_t numMolecules,
235 tng_molecule_t *tngMol)
237 tng_trajectory_t tng = gmx_tng->tng;
238 tng_chain_t tngChain = nullptr;
239 tng_residue_t tngRes = nullptr;
241 if (tng_molecule_add(tng, moleculeName, tngMol) != TNG_SUCCESS)
243 gmx_file("Cannot add molecule to TNG molecular system.");
246 for (int atomIndex = 0; atomIndex < atoms->nr; atomIndex++)
248 const t_atom *at = &atoms->atom[atomIndex];
249 /* FIXME: Currently the TNG API can only add atoms belonging to a
250 * residue and chain. Wait for TNG 2.0*/
253 const t_resinfo *resInfo = &atoms->resinfo[at->resind];
254 char chainName[2] = {resInfo->chainid, 0};
255 tng_atom_t tngAtom = nullptr;
260 prevAtom = &atoms->atom[atomIndex - 1];
267 /* If this is the first atom or if the residue changed add the
268 * residue to the TNG molecular system. */
269 if (!prevAtom || resInfo != &atoms->resinfo[prevAtom->resind])
271 /* If this is the first atom or if the chain changed add
272 * the chain to the TNG molecular system. */
273 if (!prevAtom || resInfo->chainid !=
274 atoms->resinfo[prevAtom->resind].chainid)
276 tng_molecule_chain_add(tng, *tngMol, chainName,
279 /* FIXME: When TNG supports both residue index and residue
280 * number the latter should be used. Wait for TNG 2.0*/
281 tng_chain_residue_add(tng, tngChain, *resInfo->name, &tngRes);
283 tng_residue_atom_add(tng, tngRes, *(atoms->atomname[atomIndex]), *(atoms->atomtype[atomIndex]), &tngAtom);
286 tng_molecule_cnt_set(tng, *tngMol, numMolecules);
289 void gmx_tng_add_mtop(gmx_tng_trajectory_t gmx_tng,
290 const gmx_mtop_t *mtop)
294 std::vector<real> atomCharges;
295 std::vector<real> atomMasses;
296 const t_ilist *ilist;
300 tng_trajectory_t tng = gmx_tng->tng;
304 /* No topology information available to add. */
309 datatype = TNG_DOUBLE_DATA;
311 datatype = TNG_FLOAT_DATA;
314 atomCharges.reserve(mtop->natoms);
315 atomMasses.reserve(mtop->natoms);
317 for (const gmx_molblock_t &molBlock : mtop->molblock)
319 tng_molecule_t tngMol = nullptr;
320 const gmx_moltype_t *molType = &mtop->moltype[molBlock.type];
322 /* Add a molecule to the TNG trajectory with the same name as the
323 * current molecule. */
324 addTngMoleculeFromTopology(gmx_tng,
330 /* Bonds have to be deduced from interactions (constraints etc). Different
331 * interactions have different sets of parameters. */
332 /* Constraints are specified using two atoms */
333 for (i = 0; i < F_NRE; i++)
337 ilist = &molType->ilist[i];
341 while (j < ilist->nr)
343 tng_molecule_bond_add(tng, tngMol, ilist->iatoms[j], ilist->iatoms[j+1], &tngBond);
349 /* Settle is described using three atoms */
350 ilist = &molType->ilist[F_SETTLE];
354 while (j < ilist->nr)
356 tng_molecule_bond_add(tng, tngMol, ilist->iatoms[j], ilist->iatoms[j+1], &tngBond);
357 tng_molecule_bond_add(tng, tngMol, ilist->iatoms[j], ilist->iatoms[j+2], &tngBond);
361 /* First copy atom charges and masses, first atom by atom and then copy the memory for the molecule instances.
362 * FIXME: Atom B state data should also be written to TNG (v 2.0?) */
363 for (int atomCounter = 0; atomCounter < molType->atoms.nr; atomCounter++)
365 atomCharges.push_back(molType->atoms.atom[atomCounter].q);
366 atomMasses.push_back(molType->atoms.atom[atomCounter].m);
368 for (int molCounter = 1; molCounter < molBlock.nmol; molCounter++)
370 std::copy_n(atomCharges.end() - molType->atoms.nr, molType->atoms.nr, std::back_inserter(atomCharges));
371 std::copy_n(atomMasses.end() - molType->atoms.nr, molType->atoms.nr, std::back_inserter(atomMasses));
374 /* Write the TNG data blocks. */
375 tng_particle_data_block_add(tng, TNG_TRAJ_PARTIAL_CHARGES, "PARTIAL CHARGES",
376 datatype, TNG_NON_TRAJECTORY_BLOCK,
377 1, 1, 1, 0, mtop->natoms,
378 TNG_GZIP_COMPRESSION, atomCharges.data());
379 tng_particle_data_block_add(tng, TNG_TRAJ_MASSES, "ATOM MASSES",
380 datatype, TNG_NON_TRAJECTORY_BLOCK,
381 1, 1, 1, 0, mtop->natoms,
382 TNG_GZIP_COMPRESSION, atomMasses.data());
385 /*! \libinternal \brief Compute greatest common divisor of n1 and n2
386 * if they are positive.
388 * If only one of n1 and n2 is positive, then return it.
389 * If neither n1 or n2 is positive, then return -1. */
391 greatest_common_divisor_if_positive(int n1, int n2)
395 return (0 >= n2) ? -1 : n2;
402 /* We have a non-trivial greatest common divisor to compute. */
403 return gmx_greatest_common_divisor(n1, n2);
406 /* By default try to write 100 frames (of actual output) in each frame set.
407 * This number is the number of outputs of the most frequently written data
408 * type per frame set.
409 * TODO for 5.1: Verify that 100 frames per frame set is efficient for most
410 * setups regarding compression efficiency and compression time. Make this
411 * a hidden command-line option? */
412 const int defaultFramesPerFrameSet = 100;
414 /*! \libinternal \brief Set the number of frames per frame
415 * set according to output intervals.
416 * The default is that 100 frames are written of the data
417 * that is written most often. */
418 static void tng_set_frames_per_frame_set(gmx_tng_trajectory_t gmx_tng,
419 const gmx_bool bUseLossyCompression,
420 const t_inputrec *ir)
423 tng_trajectory_t tng = gmx_tng->tng;
425 /* Set the number of frames per frame set to contain at least
426 * defaultFramesPerFrameSet of the lowest common denominator of
427 * the writing interval of positions and velocities. */
428 /* FIXME after 5.0: consider nstenergy also? */
429 if (bUseLossyCompression)
431 gcd = ir->nstxout_compressed;
435 gcd = greatest_common_divisor_if_positive(ir->nstxout, ir->nstvout);
436 gcd = greatest_common_divisor_if_positive(gcd, ir->nstfout);
443 tng_num_frames_per_frame_set_set(tng, gcd * defaultFramesPerFrameSet);
446 /*! \libinternal \brief Set the data-writing intervals, and number of
447 * frames per frame set */
448 static void set_writing_intervals(gmx_tng_trajectory_t gmx_tng,
449 const gmx_bool bUseLossyCompression,
450 const t_inputrec *ir)
452 tng_trajectory_t tng = gmx_tng->tng;
454 /* Define pointers to specific writing functions depending on if we
455 * write float or double data */
456 typedef tng_function_status (*set_writing_interval_func_pointer)(tng_trajectory_t,
464 set_writing_interval_func_pointer set_writing_interval = tng_util_generic_write_interval_double_set;
466 set_writing_interval_func_pointer set_writing_interval = tng_util_generic_write_interval_set;
468 int xout, vout, fout;
469 int gcd = -1, lowest = -1;
472 tng_set_frames_per_frame_set(gmx_tng, bUseLossyCompression, ir);
474 if (bUseLossyCompression)
476 xout = ir->nstxout_compressed;
478 /* If there is no uncompressed coordinate output write forces
479 and velocities to the compressed tng file. */
490 compression = TNG_TNG_COMPRESSION;
497 compression = TNG_GZIP_COMPRESSION;
501 set_writing_interval(tng, xout, 3, TNG_TRAJ_POSITIONS,
502 "POSITIONS", TNG_PARTICLE_BLOCK_DATA,
504 /* TODO: if/when we write energies to TNG also, reconsider how
505 * and when box information is written, because GROMACS
506 * behaviour pre-5.0 was to write the box with every
507 * trajectory frame and every energy frame, and probably
508 * people depend on this. */
510 gcd = greatest_common_divisor_if_positive(gcd, xout);
511 if (lowest < 0 || xout < lowest)
518 set_writing_interval(tng, vout, 3, TNG_TRAJ_VELOCITIES,
519 "VELOCITIES", TNG_PARTICLE_BLOCK_DATA,
522 gcd = greatest_common_divisor_if_positive(gcd, vout);
523 if (lowest < 0 || vout < lowest)
530 set_writing_interval(tng, fout, 3, TNG_TRAJ_FORCES,
531 "FORCES", TNG_PARTICLE_BLOCK_DATA,
532 TNG_GZIP_COMPRESSION);
534 gcd = greatest_common_divisor_if_positive(gcd, fout);
535 if (lowest < 0 || fout < lowest)
542 /* Lambdas and box shape written at an interval of the lowest common
543 denominator of other output */
544 set_writing_interval(tng, gcd, 1, TNG_GMX_LAMBDA,
545 "LAMBDAS", TNG_NON_PARTICLE_BLOCK_DATA,
546 TNG_GZIP_COMPRESSION);
548 set_writing_interval(tng, gcd, 9, TNG_TRAJ_BOX_SHAPE,
549 "BOX SHAPE", TNG_NON_PARTICLE_BLOCK_DATA,
550 TNG_GZIP_COMPRESSION);
551 gmx_tng->lambdaOutputInterval = gcd;
552 gmx_tng->boxOutputInterval = gcd;
553 if (gcd < lowest / 10)
555 gmx_warning("The lowest common denominator of trajectory output is "
556 "every %d step(s), whereas the shortest output interval "
557 "is every %d steps.", gcd, lowest);
563 void gmx_tng_prepare_md_writing(gmx_tng_trajectory_t gmx_tng,
564 const gmx_mtop_t *mtop,
565 const t_inputrec *ir)
568 gmx_tng_add_mtop(gmx_tng, mtop);
569 set_writing_intervals(gmx_tng, FALSE, ir);
570 tng_time_per_frame_set(gmx_tng->tng, ir->delta_t * PICO);
571 gmx_tng->timePerFrameIsSet = true;
573 GMX_UNUSED_VALUE(gmx_tng);
574 GMX_UNUSED_VALUE(mtop);
575 GMX_UNUSED_VALUE(ir);
580 /* Check if all atoms in the molecule system are selected
581 * by a selection group of type specified by gtype. */
582 static gmx_bool all_atoms_selected(const gmx_mtop_t *mtop,
585 /* Iterate through all atoms in the molecule system and
586 * check if they belong to a selection group of the
589 for (const gmx_molblock_t &molBlock : mtop->molblock)
591 const gmx_moltype_t &molType = mtop->moltype[molBlock.type];
592 const t_atoms &atoms = molType.atoms;
594 for (int j = 0; j < molBlock.nmol; j++)
596 for (int atomIndex = 0; atomIndex < atoms.nr; atomIndex++, i++)
598 if (ggrpnr(&mtop->groups, gtype, i) != 0)
608 /* Create TNG molecules which will represent each of the selection
609 * groups for writing. But do that only if there is actually a
610 * specified selection group and it is not the whole system.
611 * TODO: Currently the only selection that is taken into account
612 * is egcCompressedX, but other selections should be added when
613 * e.g. writing energies is implemented.
615 static void add_selection_groups(gmx_tng_trajectory_t gmx_tng,
616 const gmx_mtop_t *mtop)
618 const t_atoms *atoms;
620 const t_resinfo *resInfo;
621 const t_ilist *ilist;
624 tng_molecule_t mol, iterMol;
631 tng_trajectory_t tng = gmx_tng->tng;
633 /* TODO: When the TNG molecules block is more flexible TNG selection
634 * groups should not need all atoms specified. It should be possible
635 * just to specify what molecules are selected (and/or which atoms in
636 * the molecule) and how many (if applicable). */
638 /* If no atoms are selected we do not need to create a
639 * TNG selection group molecule. */
640 if (mtop->groups.ngrpnr[egcCompressedX] == 0)
645 /* If all atoms are selected we do not have to create a selection
646 * group molecule in the TNG molecule system. */
647 if (all_atoms_selected(mtop, egcCompressedX))
652 /* The name of the TNG molecule containing the selection group is the
653 * same as the name of the selection group. */
654 nameIndex = *mtop->groups.grps[egcCompressedX].nm_ind;
655 groupName = *mtop->groups.grpname[nameIndex];
657 tng_molecule_alloc(tng, &mol);
658 tng_molecule_name_set(tng, mol, groupName);
659 tng_molecule_chain_add(tng, mol, "", &chain);
661 for (const gmx_molblock_t &molBlock : mtop->molblock)
663 const gmx_moltype_t &molType = mtop->moltype[molBlock.type];
665 atoms = &molType.atoms;
667 for (int j = 0; j < molBlock.nmol; j++)
669 bool bAtomsAdded = FALSE;
670 for (int atomIndex = 0; atomIndex < atoms->nr; atomIndex++, i++)
675 if (ggrpnr(&mtop->groups, egcCompressedX, i) != 0)
679 at = &atoms->atom[atomIndex];
682 resInfo = &atoms->resinfo[at->resind];
683 /* FIXME: When TNG supports both residue index and residue
684 * number the latter should be used. */
685 res_name = *resInfo->name;
686 res_id = at->resind + 1;
690 res_name = const_cast<char*>("");
693 if (tng_chain_residue_find(tng, chain, res_name, res_id, &res)
696 /* Since there is ONE chain for selection groups do not keep the
697 * original residue IDs - otherwise there might be conflicts. */
698 tng_chain_residue_add(tng, chain, res_name, &res);
700 tng_residue_atom_w_id_add(tng, res, *(atoms->atomname[atomIndex]),
701 *(atoms->atomtype[atomIndex]),
702 atom_offset + atomIndex, &atom);
708 for (int k = 0; k < F_NRE; k++)
712 ilist = &molType.ilist[k];
716 while (l < ilist->nr)
719 atom1 = ilist->iatoms[l] + atom_offset;
720 atom2 = ilist->iatoms[l+1] + atom_offset;
721 if (ggrpnr(&mtop->groups, egcCompressedX, atom1) == 0 &&
722 ggrpnr(&mtop->groups, egcCompressedX, atom2) == 0)
724 tng_molecule_bond_add(tng, mol, ilist->iatoms[l],
725 ilist->iatoms[l+1], &tngBond);
732 /* Settle is described using three atoms */
733 ilist = &molType.ilist[F_SETTLE];
737 while (l < ilist->nr)
739 int atom1, atom2, atom3;
740 atom1 = ilist->iatoms[l] + atom_offset;
741 atom2 = ilist->iatoms[l+1] + atom_offset;
742 atom3 = ilist->iatoms[l+2] + atom_offset;
743 if (ggrpnr(&mtop->groups, egcCompressedX, atom1) == 0)
745 if (ggrpnr(&mtop->groups, egcCompressedX, atom2) == 0)
747 tng_molecule_bond_add(tng, mol, atom1,
750 if (ggrpnr(&mtop->groups, egcCompressedX, atom3) == 0)
752 tng_molecule_bond_add(tng, mol, atom1,
760 atom_offset += atoms->nr;
763 tng_molecule_existing_add(tng, &mol);
764 tng_molecule_cnt_set(tng, mol, 1);
765 tng_num_molecule_types_get(tng, &nMols);
766 for (int64_t k = 0; k < nMols; k++)
768 tng_molecule_of_index_get(tng, k, &iterMol);
773 tng_molecule_cnt_set(tng, iterMol, 0);
778 void gmx_tng_set_compression_precision(gmx_tng_trajectory_t gmx_tng,
782 tng_compression_precision_set(gmx_tng->tng, prec);
784 GMX_UNUSED_VALUE(gmx_tng);
785 GMX_UNUSED_VALUE(prec);
789 void gmx_tng_prepare_low_prec_writing(gmx_tng_trajectory_t gmx_tng,
790 const gmx_mtop_t *mtop,
791 const t_inputrec *ir)
794 gmx_tng_add_mtop(gmx_tng, mtop);
795 add_selection_groups(gmx_tng, mtop);
796 set_writing_intervals(gmx_tng, TRUE, ir);
797 tng_time_per_frame_set(gmx_tng->tng, ir->delta_t * PICO);
798 gmx_tng->timePerFrameIsSet = true;
799 gmx_tng_set_compression_precision(gmx_tng, ir->x_compression_precision);
801 GMX_UNUSED_VALUE(gmx_tng);
802 GMX_UNUSED_VALUE(mtop);
803 GMX_UNUSED_VALUE(ir);
807 void gmx_fwrite_tng(gmx_tng_trajectory_t gmx_tng,
808 const gmx_bool bUseLossyCompression,
810 real elapsedPicoSeconds,
819 typedef tng_function_status (*write_data_func_pointer)(tng_trajectory_t,
829 static write_data_func_pointer write_data = tng_util_generic_with_time_double_write;
831 static write_data_func_pointer write_data = tng_util_generic_with_time_write;
833 double elapsedSeconds = elapsedPicoSeconds * PICO;
840 /* This function might get called when the type of the
841 compressed trajectory is actually XTC. So we exit and move
845 tng_trajectory_t tng = gmx_tng->tng;
847 // While the GROMACS interface to this routine specifies 'step', TNG itself
848 // only uses 'frame index' internally, although it suggests that it's a good
849 // idea to let this represent the step rather than just frame numbers.
851 // However, the way the GROMACS interface works, there are cases where
852 // the step is simply not set, so all frames rather have step=0.
853 // If we call the lower-level TNG routines with the same frame index
854 // (which is set from the step) multiple times, things will go very
855 // wrong (overwritten data).
857 // To avoid this, we require the step value to always be larger than the
858 // previous value, and if this is not true we simply set it to a value
859 // one unit larger than the previous step.
861 // Note: We do allow the user to specify any crazy value the want for the
862 // first step. If it is "not set", GROMACS will have used the value 0.
863 if (gmx_tng->lastStepDataIsValid && step <= gmx_tng->lastStep)
865 step = gmx_tng->lastStep + 1;
868 // Now that we have fixed the potentially incorrect step, we can also set
869 // the time per frame if it was not already done, and we have
870 // step/time information from the last frame so it is possible to calculate it.
871 // Note that we do allow the time to be the same for all steps, or even
872 // decreasing. In that case we will simply say the time per step is 0
873 // or negative. A bit strange, but a correct representation of the data :-)
874 if (!gmx_tng->timePerFrameIsSet && gmx_tng->lastTimeDataIsValid && gmx_tng->lastStepDataIsValid)
876 double deltaTime = elapsedSeconds - gmx_tng->lastTime;
877 std::int64_t deltaStep = step - gmx_tng->lastStep;
878 tng_time_per_frame_set(tng, deltaTime / deltaStep );
879 gmx_tng->timePerFrameIsSet = true;
882 tng_num_particles_get(tng, &nParticles);
883 if (nAtoms != static_cast<int>(nParticles))
885 tng_implicit_num_particles_set(tng, nAtoms);
888 if (bUseLossyCompression)
890 compression = TNG_TNG_COMPRESSION;
894 compression = TNG_GZIP_COMPRESSION;
897 /* The writing is done using write_data, which writes float or double
898 * depending on the GROMACS compilation. */
901 GMX_ASSERT(box, "Need a non-NULL box if positions are written");
903 if (write_data(tng, step, elapsedSeconds,
904 reinterpret_cast<const real *>(x),
905 3, TNG_TRAJ_POSITIONS, "POSITIONS",
906 TNG_PARTICLE_BLOCK_DATA,
907 compression) != TNG_SUCCESS)
909 gmx_file("Cannot write TNG trajectory frame; maybe you are out of disk space?");
915 if (write_data(tng, step, elapsedSeconds,
916 reinterpret_cast<const real *>(v),
917 3, TNG_TRAJ_VELOCITIES, "VELOCITIES",
918 TNG_PARTICLE_BLOCK_DATA,
919 compression) != TNG_SUCCESS)
921 gmx_file("Cannot write TNG trajectory frame; maybe you are out of disk space?");
927 /* TNG-MF1 compression only compresses positions and velocities. Use lossless
928 * compression for forces regardless of output mode */
929 if (write_data(tng, step, elapsedSeconds,
930 reinterpret_cast<const real *>(f),
931 3, TNG_TRAJ_FORCES, "FORCES",
932 TNG_PARTICLE_BLOCK_DATA,
933 TNG_GZIP_COMPRESSION) != TNG_SUCCESS)
935 gmx_file("Cannot write TNG trajectory frame; maybe you are out of disk space?");
941 /* TNG-MF1 compression only compresses positions and velocities. Use lossless
942 * compression for box shape regardless of output mode */
943 if (write_data(tng, step, elapsedSeconds,
944 reinterpret_cast<const real *>(box),
945 9, TNG_TRAJ_BOX_SHAPE, "BOX SHAPE",
946 TNG_NON_PARTICLE_BLOCK_DATA,
947 TNG_GZIP_COMPRESSION) != TNG_SUCCESS)
949 gmx_file("Cannot write TNG trajectory frame; maybe you are out of disk space?");
955 /* TNG-MF1 compression only compresses positions and velocities. Use lossless
956 * compression for lambda regardless of output mode */
957 if (write_data(tng, step, elapsedSeconds,
958 reinterpret_cast<const real *>(&lambda),
959 1, TNG_GMX_LAMBDA, "LAMBDAS",
960 TNG_NON_PARTICLE_BLOCK_DATA,
961 TNG_GZIP_COMPRESSION) != TNG_SUCCESS)
963 gmx_file("Cannot write TNG trajectory frame; maybe you are out of disk space?");
967 // Update the data in the wrapper
969 gmx_tng->lastStepDataIsValid = true;
970 gmx_tng->lastStep = step;
971 gmx_tng->lastTimeDataIsValid = true;
972 gmx_tng->lastTime = elapsedSeconds;
974 GMX_UNUSED_VALUE(gmx_tng);
975 GMX_UNUSED_VALUE(bUseLossyCompression);
976 GMX_UNUSED_VALUE(step);
977 GMX_UNUSED_VALUE(elapsedPicoSeconds);
978 GMX_UNUSED_VALUE(lambda);
979 GMX_UNUSED_VALUE(box);
980 GMX_UNUSED_VALUE(nAtoms);
987 void fflush_tng(gmx_tng_trajectory_t gmx_tng)
994 tng_frame_set_premature_write(gmx_tng->tng, TNG_USE_HASH);
996 GMX_UNUSED_VALUE(gmx_tng);
1000 float gmx_tng_get_time_of_final_frame(gmx_tng_trajectory_t gmx_tng)
1006 tng_trajectory_t tng = gmx_tng->tng;
1008 tng_num_frames_get(tng, &nFrames);
1009 tng_util_time_of_frame_get(tng, nFrames - 1, &time);
1011 fTime = time / PICO;
1014 GMX_UNUSED_VALUE(gmx_tng);
1019 void gmx_prepare_tng_writing(const char *filename,
1021 gmx_tng_trajectory_t *gmx_tng_input,
1022 gmx_tng_trajectory_t *gmx_tng_output,
1024 const gmx_mtop_t *mtop,
1026 const char *indexGroupName)
1029 tng_trajectory_t *input = (gmx_tng_input && *gmx_tng_input) ? &(*gmx_tng_input)->tng : nullptr;
1030 /* FIXME after 5.0: Currently only standard block types are read */
1031 const int defaultNumIds = 5;
1032 static int64_t fallbackIds[defaultNumIds] =
1034 TNG_TRAJ_BOX_SHAPE, TNG_TRAJ_POSITIONS,
1035 TNG_TRAJ_VELOCITIES, TNG_TRAJ_FORCES,
1038 static char fallbackNames[defaultNumIds][32] =
1040 "BOX SHAPE", "POSITIONS", "VELOCITIES",
1044 typedef tng_function_status (*set_writing_interval_func_pointer)(tng_trajectory_t,
1052 set_writing_interval_func_pointer set_writing_interval = tng_util_generic_write_interval_double_set;
1054 set_writing_interval_func_pointer set_writing_interval = tng_util_generic_write_interval_set;
1057 gmx_tng_open(filename, mode, gmx_tng_output);
1058 tng_trajectory_t *output = &(*gmx_tng_output)->tng;
1060 /* Do we have an input file in TNG format? If so, then there's
1061 more data we can copy over, rather than having to improvise. */
1062 if (gmx_tng_input && *gmx_tng_input)
1064 /* Set parameters (compression, time per frame, molecule
1065 * information, number of frames per frame set and writing
1066 * intervals of positions, box shape and lambdas) of the
1067 * output tng container based on their respective values int
1068 * the input tng container */
1069 double time, compression_precision;
1070 int64_t n_frames_per_frame_set, interval = -1;
1072 tng_compression_precision_get(*input, &compression_precision);
1073 tng_compression_precision_set(*output, compression_precision);
1074 // TODO make this configurable in a future version
1075 char compression_type = TNG_TNG_COMPRESSION;
1077 tng_molecule_system_copy(*input, *output);
1079 tng_time_per_frame_get(*input, &time);
1080 tng_time_per_frame_set(*output, time);
1081 // Since we have copied the value from the input TNG we should not change it again
1082 (*gmx_tng_output)->timePerFrameIsSet = true;
1084 tng_num_frames_per_frame_set_get(*input, &n_frames_per_frame_set);
1085 tng_num_frames_per_frame_set_set(*output, n_frames_per_frame_set);
1087 for (int i = 0; i < defaultNumIds; i++)
1089 if (tng_data_get_stride_length(*input, fallbackIds[i], -1, &interval)
1092 switch (fallbackIds[i])
1094 case TNG_TRAJ_POSITIONS:
1095 case TNG_TRAJ_VELOCITIES:
1096 set_writing_interval(*output, interval, 3, fallbackIds[i],
1097 fallbackNames[i], TNG_PARTICLE_BLOCK_DATA,
1100 case TNG_TRAJ_FORCES:
1101 set_writing_interval(*output, interval, 3, fallbackIds[i],
1102 fallbackNames[i], TNG_PARTICLE_BLOCK_DATA,
1103 TNG_GZIP_COMPRESSION);
1105 case TNG_TRAJ_BOX_SHAPE:
1106 set_writing_interval(*output, interval, 9, fallbackIds[i],
1107 fallbackNames[i], TNG_NON_PARTICLE_BLOCK_DATA,
1108 TNG_GZIP_COMPRESSION);
1109 (*gmx_tng_output)->boxOutputInterval = interval;
1111 case TNG_GMX_LAMBDA:
1112 set_writing_interval(*output, interval, 1, fallbackIds[i],
1113 fallbackNames[i], TNG_NON_PARTICLE_BLOCK_DATA,
1114 TNG_GZIP_COMPRESSION);
1115 (*gmx_tng_output)->lambdaOutputInterval = interval;
1126 /* TODO after trjconv is modularized: fix this so the user can
1127 change precision when they are doing an operation where
1128 this makes sense, and not otherwise.
1130 char compression = bUseLossyCompression ? TNG_TNG_COMPRESSION : TNG_GZIP_COMPRESSION;
1131 gmx_tng_set_compression_precision(*output, ndec2prec(nDecimalsOfPrecision));
1133 gmx_tng_add_mtop(*gmx_tng_output, mtop);
1134 tng_num_frames_per_frame_set_set(*output, 1);
1137 if (index && nAtoms > 0)
1139 gmx_tng_setup_atom_subgroup(*gmx_tng_output, nAtoms, index, indexGroupName);
1142 /* If for some reason there are more requested atoms than there are atoms in the
1143 * molecular system create a number of implicit atoms (without atom data) to
1144 * compensate for that. */
1147 tng_implicit_num_particles_set(*output, nAtoms);
1150 GMX_UNUSED_VALUE(filename);
1151 GMX_UNUSED_VALUE(mode);
1152 GMX_UNUSED_VALUE(gmx_tng_input);
1153 GMX_UNUSED_VALUE(gmx_tng_output);
1154 GMX_UNUSED_VALUE(nAtoms);
1155 GMX_UNUSED_VALUE(mtop);
1156 GMX_UNUSED_VALUE(index);
1157 GMX_UNUSED_VALUE(indexGroupName);
1161 void gmx_write_tng_from_trxframe(gmx_tng_trajectory_t gmx_tng_output,
1162 const t_trxframe *frame,
1168 natoms = frame->natoms;
1170 gmx_fwrite_tng(gmx_tng_output,
1181 GMX_UNUSED_VALUE(gmx_tng_output);
1182 GMX_UNUSED_VALUE(frame);
1183 GMX_UNUSED_VALUE(natoms);
1192 convert_array_to_real_array(void *from,
1197 const char datatype)
1201 const bool useDouble = GMX_DOUBLE;
1204 case TNG_FLOAT_DATA:
1209 memcpy(to, from, nValues * sizeof(real) * nAtoms);
1213 for (i = 0; i < nAtoms; i++)
1215 for (j = 0; j < nValues; j++)
1217 to[i*nValues+j] = reinterpret_cast<float *>(from)[i*nValues+j] * fact;
1224 for (i = 0; i < nAtoms; i++)
1226 for (j = 0; j < nValues; j++)
1228 to[i*nValues+j] = reinterpret_cast<float *>(from)[i*nValues+j] * fact;
1234 for (i = 0; i < nAtoms; i++)
1236 for (j = 0; j < nValues; j++)
1238 to[i*nValues+j] = reinterpret_cast<int64_t *>(from)[i*nValues+j] * fact;
1242 case TNG_DOUBLE_DATA:
1243 if (sizeof(real) == sizeof(double))
1247 memcpy(to, from, nValues * sizeof(real) * nAtoms);
1251 for (i = 0; i < nAtoms; i++)
1253 for (j = 0; j < nValues; j++)
1255 to[i*nValues+j] = reinterpret_cast<double *>(from)[i*nValues+j] * fact;
1262 for (i = 0; i < nAtoms; i++)
1264 for (j = 0; j < nValues; j++)
1266 to[i*nValues+j] = reinterpret_cast<double *>(from)[i*nValues+j] * fact;
1272 gmx_incons("Illegal datatype when converting values to a real array!");
1276 real getDistanceScaleFactor(gmx_tng_trajectory_t in)
1279 real distanceScaleFactor;
1281 // TODO Hopefully, TNG 2.0 will do this kind of thing for us
1282 tng_distance_unit_exponential_get(in->tng, &exp);
1284 // GROMACS expects distances in nm
1288 distanceScaleFactor = NANO/NANO;
1291 distanceScaleFactor = NANO/ANGSTROM;
1294 distanceScaleFactor = pow(10.0, exp + 9.0);
1297 return distanceScaleFactor;
1303 void gmx_tng_setup_atom_subgroup(gmx_tng_trajectory_t gmx_tng,
1309 int64_t nAtoms, cnt, nMols;
1310 tng_molecule_t mol, iterMol;
1314 tng_function_status stat;
1315 tng_trajectory_t tng = gmx_tng->tng;
1317 tng_num_particles_get(tng, &nAtoms);
1324 stat = tng_molecule_find(tng, name, -1, &mol);
1325 if (stat == TNG_SUCCESS)
1327 tng_molecule_num_atoms_get(tng, mol, &nAtoms);
1328 tng_molecule_cnt_get(tng, mol, &cnt);
1338 if (stat == TNG_FAILURE)
1340 /* The indexed atoms are added to one separate molecule. */
1341 tng_molecule_alloc(tng, &mol);
1342 tng_molecule_name_set(tng, mol, name);
1343 tng_molecule_chain_add(tng, mol, "", &chain);
1345 for (int i = 0; i < nind; i++)
1347 char temp_name[256], temp_type[256];
1349 /* Try to retrieve the residue name of the atom */
1350 stat = tng_residue_name_of_particle_nr_get(tng, ind[i], temp_name, 256);
1351 if (stat != TNG_SUCCESS)
1353 temp_name[0] = '\0';
1355 /* Check if the molecule of the selection already contains this residue */
1356 if (tng_chain_residue_find(tng, chain, temp_name, -1, &res)
1359 tng_chain_residue_add(tng, chain, temp_name, &res);
1361 /* Try to find the original name and type of the atom */
1362 stat = tng_atom_name_of_particle_nr_get(tng, ind[i], temp_name, 256);
1363 if (stat != TNG_SUCCESS)
1365 temp_name[0] = '\0';
1367 stat = tng_atom_type_of_particle_nr_get(tng, ind[i], temp_type, 256);
1368 if (stat != TNG_SUCCESS)
1370 temp_type[0] = '\0';
1372 tng_residue_atom_w_id_add(tng, res, temp_name, temp_type, ind[i], &atom);
1374 tng_molecule_existing_add(tng, &mol);
1376 /* Set the count of the molecule containing the selected atoms to 1 and all
1377 * other molecules to 0 */
1378 tng_molecule_cnt_set(tng, mol, 1);
1379 tng_num_molecule_types_get(tng, &nMols);
1380 for (int64_t k = 0; k < nMols; k++)
1382 tng_molecule_of_index_get(tng, k, &iterMol);
1387 tng_molecule_cnt_set(tng, iterMol, 0);
1390 GMX_UNUSED_VALUE(gmx_tng);
1391 GMX_UNUSED_VALUE(nind);
1392 GMX_UNUSED_VALUE(ind);
1393 GMX_UNUSED_VALUE(name);
1397 /* TODO: If/when TNG acquires the ability to copy data blocks without
1398 * uncompressing them, then this implemenation should be reconsidered.
1399 * Ideally, gmx trjconv -f a.tng -o b.tng -b 10 -e 20 would be fast
1400 * and lose no information. */
1401 gmx_bool gmx_read_next_tng_frame(gmx_tng_trajectory_t gmx_tng_input,
1403 int64_t *requestedIds,
1404 int numRequestedIds)
1407 tng_trajectory_t input = gmx_tng_input->tng;
1408 gmx_bool bOK = TRUE;
1409 tng_function_status stat;
1410 int64_t numberOfAtoms = -1, frameNumber = -1;
1411 int64_t nBlocks, blockId, *blockIds = nullptr, codecId;
1413 void *values = nullptr;
1414 double frameTime = -1.0;
1415 int size, blockDependency;
1417 const int defaultNumIds = 5;
1418 static int64_t fallbackRequestedIds[defaultNumIds] =
1420 TNG_TRAJ_BOX_SHAPE, TNG_TRAJ_POSITIONS,
1421 TNG_TRAJ_VELOCITIES, TNG_TRAJ_FORCES,
1428 fr->bLambda = FALSE;
1436 /* If no specific IDs were requested read all block types that can
1437 * currently be interpreted */
1438 if (!requestedIds || numRequestedIds == 0)
1440 numRequestedIds = defaultNumIds;
1441 requestedIds = fallbackRequestedIds;
1444 stat = tng_num_particles_get(input, &numberOfAtoms);
1445 if (stat != TNG_SUCCESS)
1447 gmx_file("Cannot determine number of atoms from TNG file.");
1449 fr->natoms = numberOfAtoms;
1451 bool nextFrameExists = gmx_get_tng_data_block_types_of_next_frame(gmx_tng_input,
1458 gmx::unique_cptr<int64_t, gmx::free_wrapper> blockIdsGuard(blockIds);
1459 if (!nextFrameExists)
1469 for (int64_t i = 0; i < nBlocks; i++)
1471 blockId = blockIds[i];
1472 tng_data_block_dependency_get(input, blockId, &blockDependency);
1473 if (blockDependency & TNG_PARTICLE_DEPENDENT)
1475 stat = tng_util_particle_data_next_frame_read(input,
1484 stat = tng_util_non_particle_data_next_frame_read(input,
1491 if (stat == TNG_CRITICAL)
1493 gmx_file("Cannot read positions from TNG file.");
1496 else if (stat == TNG_FAILURE)
1502 case TNG_TRAJ_BOX_SHAPE:
1506 size = sizeof(int64_t);
1508 case TNG_FLOAT_DATA:
1509 size = sizeof(float);
1511 case TNG_DOUBLE_DATA:
1512 size = sizeof(double);
1515 gmx_incons("Illegal datatype of box shape values!");
1517 for (int i = 0; i < DIM; i++)
1519 convert_array_to_real_array(reinterpret_cast<char *>(values) + size * i * DIM,
1520 reinterpret_cast<real *>(fr->box[i]),
1521 getDistanceScaleFactor(gmx_tng_input),
1528 case TNG_TRAJ_POSITIONS:
1529 srenew(fr->x, fr->natoms);
1530 convert_array_to_real_array(values,
1531 reinterpret_cast<real *>(fr->x),
1532 getDistanceScaleFactor(gmx_tng_input),
1537 tng_util_frame_current_compression_get(input, blockId, &codecId, &prec);
1538 /* This must be updated if/when more lossy compression methods are added */
1539 if (codecId == TNG_TNG_COMPRESSION)
1545 case TNG_TRAJ_VELOCITIES:
1546 srenew(fr->v, fr->natoms);
1547 convert_array_to_real_array(values,
1548 reinterpret_cast<real *>(fr->v),
1549 getDistanceScaleFactor(gmx_tng_input),
1554 tng_util_frame_current_compression_get(input, blockId, &codecId, &prec);
1555 /* This must be updated if/when more lossy compression methods are added */
1556 if (codecId == TNG_TNG_COMPRESSION)
1562 case TNG_TRAJ_FORCES:
1563 srenew(fr->f, fr->natoms);
1564 convert_array_to_real_array(values,
1565 reinterpret_cast<real *>(fr->f),
1566 getDistanceScaleFactor(gmx_tng_input),
1572 case TNG_GMX_LAMBDA:
1575 case TNG_FLOAT_DATA:
1576 fr->lambda = *(reinterpret_cast<float *>(values));
1578 case TNG_DOUBLE_DATA:
1579 fr->lambda = *(reinterpret_cast<double *>(values));
1582 gmx_incons("Illegal datatype lambda value!");
1587 gmx_warning("Illegal block type! Currently GROMACS tools can only handle certain data types. Skipping block.");
1589 /* values does not have to be freed before reading next frame. It will
1590 * be reallocated if it is not NULL. */
1593 fr->step = frameNumber;
1596 // Convert the time to ps
1597 fr->time = frameTime / PICO;
1598 fr->bTime = (frameTime > 0);
1600 // TODO This does not leak, but is not exception safe.
1601 /* values must be freed before leaving this function */
1606 GMX_UNUSED_VALUE(gmx_tng_input);
1607 GMX_UNUSED_VALUE(fr);
1608 GMX_UNUSED_VALUE(requestedIds);
1609 GMX_UNUSED_VALUE(numRequestedIds);
1614 void gmx_print_tng_molecule_system(gmx_tng_trajectory_t gmx_tng_input,
1618 int64_t nMolecules, nChains, nResidues, nAtoms, nFramesRead;
1619 int64_t strideLength, nParticlesRead, nValuesPerFrameRead, *molCntList;
1620 tng_molecule_t molecule;
1622 tng_residue_t residue;
1624 tng_function_status stat;
1628 void *data = nullptr;
1629 std::vector<real> atomCharges;
1630 std::vector<real> atomMasses;
1631 tng_trajectory_t input = gmx_tng_input->tng;
1633 tng_num_molecule_types_get(input, &nMolecules);
1634 tng_molecule_cnt_list_get(input, &molCntList);
1635 /* Can the number of particles change in the trajectory or is it constant? */
1636 tng_num_particles_variable_get(input, &varNAtoms);
1638 for (int64_t i = 0; i < nMolecules; i++)
1640 tng_molecule_of_index_get(input, i, &molecule);
1641 tng_molecule_name_get(input, molecule, str, 256);
1642 if (varNAtoms == TNG_CONSTANT_N_ATOMS)
1644 if (static_cast<int>(molCntList[i]) == 0)
1648 fprintf(stream, "Molecule: %s, count: %d\n", str, static_cast<int>(molCntList[i]));
1652 fprintf(stream, "Molecule: %s\n", str);
1654 tng_molecule_num_chains_get(input, molecule, &nChains);
1657 for (int64_t j = 0; j < nChains; j++)
1659 tng_molecule_chain_of_index_get(input, molecule, j, &chain);
1660 tng_chain_name_get(input, chain, str, 256);
1661 fprintf(stream, "\tChain: %s\n", str);
1662 tng_chain_num_residues_get(input, chain, &nResidues);
1663 for (int64_t k = 0; k < nResidues; k++)
1665 tng_chain_residue_of_index_get(input, chain, k, &residue);
1666 tng_residue_name_get(input, residue, str, 256);
1667 fprintf(stream, "\t\tResidue: %s\n", str);
1668 tng_residue_num_atoms_get(input, residue, &nAtoms);
1669 for (int64_t l = 0; l < nAtoms; l++)
1671 tng_residue_atom_of_index_get(input, residue, l, &atom);
1672 tng_atom_name_get(input, atom, str, 256);
1673 fprintf(stream, "\t\t\tAtom: %s", str);
1674 tng_atom_type_get(input, atom, str, 256);
1675 fprintf(stream, " (%s)\n", str);
1680 /* It is possible to have a molecule without chains, in which case
1681 * residues in the molecule can be iterated through without going
1682 * through chains. */
1685 tng_molecule_num_residues_get(input, molecule, &nResidues);
1688 for (int64_t k = 0; k < nResidues; k++)
1690 tng_molecule_residue_of_index_get(input, molecule, k, &residue);
1691 tng_residue_name_get(input, residue, str, 256);
1692 fprintf(stream, "\t\tResidue: %s\n", str);
1693 tng_residue_num_atoms_get(input, residue, &nAtoms);
1694 for (int64_t l = 0; l < nAtoms; l++)
1696 tng_residue_atom_of_index_get(input, residue, l, &atom);
1697 tng_atom_name_get(input, atom, str, 256);
1698 fprintf(stream, "\t\t\tAtom: %s", str);
1699 tng_atom_type_get(input, atom, str, 256);
1700 fprintf(stream, " (%s)\n", str);
1706 tng_molecule_num_atoms_get(input, molecule, &nAtoms);
1707 for (int64_t l = 0; l < nAtoms; l++)
1709 tng_molecule_atom_of_index_get(input, molecule, l, &atom);
1710 tng_atom_name_get(input, atom, str, 256);
1711 fprintf(stream, "\t\t\tAtom: %s", str);
1712 tng_atom_type_get(input, atom, str, 256);
1713 fprintf(stream, " (%s)\n", str);
1719 tng_num_particles_get(input, &nAtoms);
1720 stat = tng_particle_data_vector_get(input, TNG_TRAJ_PARTIAL_CHARGES, &data, &nFramesRead,
1721 &strideLength, &nParticlesRead,
1722 &nValuesPerFrameRead, &datatype);
1723 if (stat == TNG_SUCCESS)
1725 atomCharges.resize(nAtoms);
1726 convert_array_to_real_array(data,
1733 fprintf(stream, "Atom Charges (%d):\n", int(nAtoms));
1734 for (int64_t i = 0; i < nAtoms; i += 10)
1736 fprintf(stream, "Atom Charges [%8d-]=[", int(i));
1737 for (int64_t j = 0; (j < 10 && i + j < nAtoms); j++)
1739 fprintf(stream, " %12.5e", atomCharges[i + j]);
1741 fprintf(stream, "]\n");
1745 stat = tng_particle_data_vector_get(input, TNG_TRAJ_MASSES, &data, &nFramesRead,
1746 &strideLength, &nParticlesRead,
1747 &nValuesPerFrameRead, &datatype);
1748 if (stat == TNG_SUCCESS)
1750 atomMasses.resize(nAtoms);
1751 convert_array_to_real_array(data,
1758 fprintf(stream, "Atom Masses (%d):\n", int(nAtoms));
1759 for (int64_t i = 0; i < nAtoms; i += 10)
1761 fprintf(stream, "Atom Masses [%8d-]=[", int(i));
1762 for (int64_t j = 0; (j < 10 && i + j < nAtoms); j++)
1764 fprintf(stream, " %12.5e", atomMasses[i + j]);
1766 fprintf(stream, "]\n");
1772 GMX_UNUSED_VALUE(gmx_tng_input);
1773 GMX_UNUSED_VALUE(stream);
1777 gmx_bool gmx_get_tng_data_block_types_of_next_frame(gmx_tng_trajectory_t gmx_tng_input,
1780 int64_t *requestedIds,
1786 tng_function_status stat;
1787 tng_trajectory_t input = gmx_tng_input->tng;
1789 stat = tng_util_trajectory_next_frame_present_data_blocks_find(input, frame,
1790 nRequestedIds, requestedIds,
1794 if (stat == TNG_CRITICAL)
1796 gmx_file("Cannot read TNG file. Cannot find data blocks of next frame.");
1798 else if (stat == TNG_FAILURE)
1804 GMX_UNUSED_VALUE(gmx_tng_input);
1805 GMX_UNUSED_VALUE(frame);
1806 GMX_UNUSED_VALUE(nRequestedIds);
1807 GMX_UNUSED_VALUE(requestedIds);
1808 GMX_UNUSED_VALUE(nextFrame);
1809 GMX_UNUSED_VALUE(nBlocks);
1810 GMX_UNUSED_VALUE(blockIds);
1815 gmx_bool gmx_get_tng_data_next_frame_of_block_type(gmx_tng_trajectory_t gmx_tng_input,
1818 int64_t *frameNumber,
1820 int64_t *nValuesPerFrame,
1828 tng_function_status stat;
1831 int blockDependency;
1832 void *data = nullptr;
1834 tng_trajectory_t input = gmx_tng_input->tng;
1836 stat = tng_data_block_name_get(input, blockId, name, maxLen);
1837 if (stat != TNG_SUCCESS)
1839 gmx_file("Cannot read next frame of TNG file");
1841 stat = tng_data_block_dependency_get(input, blockId, &blockDependency);
1842 if (stat != TNG_SUCCESS)
1844 gmx_file("Cannot read next frame of TNG file");
1846 if (blockDependency & TNG_PARTICLE_DEPENDENT)
1848 tng_num_particles_get(input, nAtoms);
1849 stat = tng_util_particle_data_next_frame_read(input,
1858 *nAtoms = 1; /* There are not actually any atoms, but it is used for
1859 allocating memory */
1860 stat = tng_util_non_particle_data_next_frame_read(input,
1867 if (stat == TNG_CRITICAL)
1869 gmx_file("Cannot read next frame of TNG file");
1871 if (stat == TNG_FAILURE)
1877 stat = tng_data_block_num_values_per_frame_get(input, blockId, nValuesPerFrame);
1878 if (stat != TNG_SUCCESS)
1880 gmx_file("Cannot read next frame of TNG file");
1882 srenew(*values, sizeof(real) * *nValuesPerFrame * *nAtoms);
1883 convert_array_to_real_array(data,
1885 getDistanceScaleFactor(gmx_tng_input),
1890 tng_util_frame_current_compression_get(input, blockId, &codecId, &localPrec);
1892 /* This must be updated if/when more lossy compression methods are added */
1893 if (codecId != TNG_TNG_COMPRESSION)
1907 GMX_UNUSED_VALUE(gmx_tng_input);
1908 GMX_UNUSED_VALUE(blockId);
1909 GMX_UNUSED_VALUE(values);
1910 GMX_UNUSED_VALUE(frameNumber);
1911 GMX_UNUSED_VALUE(frameTime);
1912 GMX_UNUSED_VALUE(nValuesPerFrame);
1913 GMX_UNUSED_VALUE(nAtoms);
1914 GMX_UNUSED_VALUE(prec);
1915 GMX_UNUSED_VALUE(name);
1916 GMX_UNUSED_VALUE(maxLen);
1917 GMX_UNUSED_VALUE(bOK);
1922 int gmx_tng_get_box_output_interval(gmx_tng_trajectory_t gmx_tng)
1925 return gmx_tng->boxOutputInterval;
1927 GMX_UNUSED_VALUE(gmx_tng);
1931 int gmx_tng_get_lambda_output_interval(gmx_tng_trajectory_t gmx_tng)
1934 return gmx_tng->lambdaOutputInterval;
1936 GMX_UNUSED_VALUE(gmx_tng);