2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2013, 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.
35 #include "tngio_for_tools.h"
45 #include "../../external/tng_io/include/tng_io.h"
48 #include "gromacs/utility/common.h"
49 #include "gromacs/legacyheaders/types/atoms.h"
50 #include "gromacs/legacyheaders/smalloc.h"
51 #include "gromacs/legacyheaders/physics.h"
52 #include "gromacs/legacyheaders/gmx_fatal.h"
54 void gmx_prepare_tng_writing(const char *filename,
56 tng_trajectory_t *input,
57 tng_trajectory_t *output,
59 const gmx_mtop_t *mtop,
61 const char *indexGroupName)
64 /* FIXME after 5.0: Currently only standard block types are read */
65 const int defaultNumIds = 5;
66 static gmx_int64_t fallbackIds[defaultNumIds] =
68 TNG_TRAJ_BOX_SHAPE, TNG_TRAJ_POSITIONS,
69 TNG_TRAJ_VELOCITIES, TNG_TRAJ_FORCES,
72 static char fallbackNames[defaultNumIds][32] =
74 "BOX SHAPE", "POSITIONS", "VELOCITIES",
79 gmx_tng_open(filename, mode, output);
81 /* Do we have an input file in TNG format? If so, then there's
82 more data we can copy over, rather than having to improvise. */
85 /* Set parameters (compression, time per frame, molecule
86 * information, number of frames per frame set and writing
87 * intervals of positions, box shape and lambdas) of the
88 * output tng container based on their respective values int
89 * the input tng container */
90 double time, compression_precision;
91 gmx_int64_t n_frames_per_frame_set, interval = -1;
93 tng_compression_precision_get(*input, &compression_precision);
94 tng_compression_precision_set(*output, compression_precision);
95 // TODO make this configurable in a future version
96 char compression_type = TNG_TNG_COMPRESSION;
98 tng_molecule_system_copy(*input, *output);
100 tng_time_per_frame_get(*input, &time);
101 tng_time_per_frame_set(*output, time);
103 tng_num_frames_per_frame_set_get(*input, &n_frames_per_frame_set);
104 tng_num_frames_per_frame_set_set(*output, n_frames_per_frame_set);
106 for (int i = 0; i < defaultNumIds; i++)
108 if (tng_data_get_stride_length(*input, fallbackIds[i], -1, &interval)
111 switch (fallbackIds[i])
113 case TNG_TRAJ_POSITIONS:
114 case TNG_TRAJ_VELOCITIES:
115 tng_util_generic_write_interval_set(*output, interval, 3, fallbackIds[i],
116 fallbackNames[i], TNG_PARTICLE_BLOCK_DATA,
119 case TNG_TRAJ_FORCES:
120 tng_util_generic_write_interval_set(*output, interval, 3, fallbackIds[i],
121 fallbackNames[i], TNG_PARTICLE_BLOCK_DATA,
122 TNG_GZIP_COMPRESSION);
124 case TNG_TRAJ_BOX_SHAPE:
125 tng_util_generic_write_interval_set(*output, interval, 9, fallbackIds[i],
126 fallbackNames[i], TNG_NON_PARTICLE_BLOCK_DATA,
127 TNG_GZIP_COMPRESSION);
130 tng_util_generic_write_interval_set(*output, interval, 1, fallbackIds[i],
131 fallbackNames[i], TNG_NON_PARTICLE_BLOCK_DATA,
132 TNG_GZIP_COMPRESSION);
142 /* TODO after trjconv is modularized: fix this so the user can
143 change precision when they are doing an operation where
144 this makes sense, and not otherwise.
146 char compression = bUseLossyCompression ? TNG_TNG_COMPRESSION : TNG_GZIP_COMPRESSION;
147 gmx_tng_set_compression_precision(*output, ndec2prec(nDecimalsOfPrecision));
149 gmx_tng_add_mtop(*output, mtop);
150 tng_num_frames_per_frame_set_set(*output, 1);
153 if (index && nAtoms > 0)
155 gmx_tng_setup_atom_subgroup(*output, nAtoms, index, indexGroupName);
158 /* If for some reason there are more requested atoms than there are atoms in the
159 * molecular system create a number of implicit atoms (without atom data) to
160 * compensate for that. */
163 tng_implicit_num_particles_set(*output, nAtoms);
166 GMX_UNUSED_VALUE(filename);
167 GMX_UNUSED_VALUE(mode);
168 GMX_UNUSED_VALUE(input);
169 GMX_UNUSED_VALUE(output);
170 GMX_UNUSED_VALUE(nAtoms);
174 void gmx_write_tng_from_trxframe(tng_trajectory_t output,
181 double timePerFrame = frame->time * PICO / frame->step;
182 tng_time_per_frame_set(output, timePerFrame);
186 natoms = frame->natoms;
188 gmx_fwrite_tng(output,
193 (const rvec *) frame->box,
195 (const rvec *) frame->x,
196 (const rvec *) frame->v,
197 (const rvec *) frame->f);
199 GMX_UNUSED_VALUE(output);
200 GMX_UNUSED_VALUE(frame);
206 convert_array_to_real_array(void *from,
218 if (sizeof(real) == sizeof(float))
222 memcpy(to, from, nValues * sizeof(real) * nAtoms);
226 for (i = 0; i < nAtoms; i++)
228 for (j = 0; j < nValues; j++)
230 to[i*nValues+j] = (real)((float *)from)[i*nValues+j] * fact;
237 for (i = 0; i < nAtoms; i++)
239 for (j = 0; j < nValues; j++)
241 to[i*nValues+j] = (real)((float *)from)[i*nValues+j] * fact;
247 for (i = 0; i < nAtoms; i++)
249 for (j = 0; j < nValues; j++)
251 to[i*nValues+j] = (real)((gmx_int64_t *)from)[i*nValues+j] * fact;
255 case TNG_DOUBLE_DATA:
256 if (sizeof(real) == sizeof(double))
260 memcpy(to, from, nValues * sizeof(real) * nAtoms);
264 for (i = 0; i < nAtoms; i++)
266 for (j = 0; j < nValues; j++)
268 to[i*nValues+j] = (real)((double *)from)[i*nValues+j] * fact;
275 for (i = 0; i < nAtoms; i++)
277 for (j = 0; j < nValues; j++)
279 to[i*nValues+j] = (real)((double *)from)[i*nValues+j] * fact;
285 gmx_incons("Illegal datatype when converting values to a real array!");
291 static real getDistanceScaleFactor(tng_trajectory_t in)
293 gmx_int64_t exp = -1;
294 real distanceScaleFactor;
296 // TODO Hopefully, TNG 2.0 will do this kind of thing for us
297 tng_distance_unit_exponential_get(in, &exp);
299 // GROMACS expects distances in nm
303 distanceScaleFactor = NANO/NANO;
306 distanceScaleFactor = NANO/ANGSTROM;
309 distanceScaleFactor = pow(10.0, exp + 9.0);
312 return distanceScaleFactor;
316 void gmx_tng_setup_atom_subgroup(tng_trajectory_t tng,
322 gmx_int64_t nAtoms, cnt, nMols;
323 tng_molecule_t mol, iterMol;
327 tng_function_status stat;
329 tng_num_particles_get(tng, &nAtoms);
336 stat = tng_molecule_find(tng, name, -1, &mol);
337 if (stat == TNG_SUCCESS)
339 tng_molecule_num_atoms_get(tng, mol, &nAtoms);
340 tng_molecule_cnt_get(tng, mol, &cnt);
350 if (stat == TNG_FAILURE)
352 /* The indexed atoms are added to one separate molecule. */
353 tng_molecule_alloc(tng, &mol);
354 tng_molecule_name_set(tng, mol, name);
355 tng_molecule_chain_add(tng, mol, "", &chain);
357 for (int i = 0; i < nind; i++)
359 char temp_name[256], temp_type[256];
361 /* Try to retrieve the residue name of the atom */
362 stat = tng_residue_name_of_particle_nr_get(tng, ind[i], temp_name, 256);
363 if (stat != TNG_SUCCESS)
367 /* Check if the molecule of the selection already contains this residue */
368 if (tng_chain_residue_find(tng, chain, temp_name, -1, &res)
371 tng_chain_residue_add(tng, chain, temp_name, &res);
373 /* Try to find the original name and type of the atom */
374 stat = tng_atom_name_of_particle_nr_get(tng, ind[i], temp_name, 256);
375 if (stat != TNG_SUCCESS)
379 stat = tng_atom_type_of_particle_nr_get(tng, ind[i], temp_type, 256);
380 if (stat != TNG_SUCCESS)
384 tng_residue_atom_w_id_add(tng, res, temp_name, temp_type, ind[i], &atom);
386 tng_molecule_existing_add(tng, &mol);
388 /* Set the count of the molecule containing the selected atoms to 1 and all
389 * other molecules to 0 */
390 tng_molecule_cnt_set(tng, mol, 1);
391 tng_num_molecule_types_get(tng, &nMols);
392 for (gmx_int64_t k = 0; k < nMols; k++)
394 tng_molecule_of_index_get(tng, k, &iterMol);
399 tng_molecule_cnt_set(tng, iterMol, 0);
402 GMX_UNUSED_VALUE(tng);
403 GMX_UNUSED_VALUE(nind);
404 GMX_UNUSED_VALUE(ind);
405 GMX_UNUSED_VALUE(name);
409 /* TODO: If/when TNG acquires the ability to copy data blocks without
410 * uncompressing them, then this implemenation should be reconsidered.
411 * Ideally, gmx trjconv -f a.tng -o b.tng -b 10 -e 20 would be fast
412 * and lose no information. */
413 gmx_bool gmx_read_next_tng_frame(tng_trajectory_t input,
415 gmx_int64_t *requestedIds,
420 tng_function_status stat;
421 gmx_int64_t numberOfAtoms = -1, frameNumber = -1;
422 gmx_int64_t nBlocks, blockId, *blockIds = NULL;
423 char datatype = -1, codecId;
425 double frameTime = -1.0;
426 int size, blockDependency;
428 const int defaultNumIds = 5;
429 static gmx_int64_t fallbackRequestedIds[defaultNumIds] =
431 TNG_TRAJ_BOX_SHAPE, TNG_TRAJ_POSITIONS,
432 TNG_TRAJ_VELOCITIES, TNG_TRAJ_FORCES,
447 /* If no specific IDs were requested read all block types that can
448 * currently be interpreted */
449 if (!requestedIds || numRequestedIds == 0)
451 numRequestedIds = defaultNumIds;
452 requestedIds = fallbackRequestedIds;
455 stat = tng_num_particles_get(input, &numberOfAtoms);
456 if (stat != TNG_SUCCESS)
458 gmx_file("Cannot determine number of atoms from TNG file.");
460 fr->natoms = numberOfAtoms;
462 if (!gmx_get_tng_data_block_types_of_next_frame(input,
478 for (gmx_int64_t i = 0; i < nBlocks; i++)
480 blockId = blockIds[i];
481 tng_data_block_dependency_get(input, blockId, &blockDependency);
482 if (blockDependency & TNG_PARTICLE_DEPENDENT)
484 stat = tng_util_particle_data_next_frame_read(input,
493 stat = tng_util_non_particle_data_next_frame_read(input,
500 if (stat == TNG_CRITICAL)
502 gmx_file("Cannot read positions from TNG file.");
505 else if (stat == TNG_FAILURE)
511 case TNG_TRAJ_BOX_SHAPE:
515 size = sizeof(gmx_int64_t);
518 size = sizeof(float);
520 case TNG_DOUBLE_DATA:
521 size = sizeof(double);
524 size = 0; /* Just to make the compiler happy. */
525 gmx_incons("Illegal datatype of box shape values!");
527 for (int i = 0; i < DIM; i++)
529 convert_array_to_real_array((char *)(values) + size * i * DIM,
531 getDistanceScaleFactor(input),
538 case TNG_TRAJ_POSITIONS:
539 srenew(fr->x, fr->natoms);
540 convert_array_to_real_array(values,
542 getDistanceScaleFactor(input),
547 tng_util_frame_current_compression_get(input, blockId, &codecId, &prec);
548 /* This must be updated if/when more lossy compression methods are added */
549 if (codecId == TNG_TNG_COMPRESSION)
555 case TNG_TRAJ_VELOCITIES:
556 srenew(fr->v, fr->natoms);
557 convert_array_to_real_array(values,
559 getDistanceScaleFactor(input),
564 tng_util_frame_current_compression_get(input, blockId, &codecId, &prec);
565 /* This must be updated if/when more lossy compression methods are added */
566 if (codecId == TNG_TNG_COMPRESSION)
572 case TNG_TRAJ_FORCES:
573 srenew(fr->f, fr->natoms);
574 convert_array_to_real_array(values,
576 getDistanceScaleFactor(input),
586 fr->lambda = (*(float *)values);
588 case TNG_DOUBLE_DATA:
589 fr->lambda = (*(double *)values);
592 gmx_incons("Illegal datatype lambda value!");
597 gmx_warning("Illegal block type! Currently GROMACS tools can only handle certain data types. Skipping block.");
599 /* values does not have to be freed before reading next frame. It will
600 * be reallocated if it is not NULL. */
603 fr->step = (int) frameNumber;
605 // Convert the time to ps
606 fr->time = frameTime / PICO;
609 /* values must be freed before leaving this function */
614 GMX_UNUSED_VALUE(input);
615 GMX_UNUSED_VALUE(fr);
616 GMX_UNUSED_VALUE(requestedIds);
621 void gmx_print_tng_molecule_system(tng_trajectory_t input,
625 gmx_int64_t nMolecules, nChains, nResidues, nAtoms, *molCntList;
626 tng_molecule_t molecule;
628 tng_residue_t residue;
630 char str[256], varNAtoms;
632 tng_num_molecule_types_get(input, &nMolecules);
633 tng_molecule_cnt_list_get(input, &molCntList);
634 /* Can the number of particles change in the trajectory or is it constant? */
635 tng_num_particles_variable_get(input, &varNAtoms);
637 for (gmx_int64_t i = 0; i < nMolecules; i++)
639 tng_molecule_of_index_get(input, i, &molecule);
640 tng_molecule_name_get(input, molecule, str, 256);
641 if (varNAtoms == TNG_CONSTANT_N_ATOMS)
643 if ((int)molCntList[i] == 0)
647 fprintf(stream, "Molecule: %s, count: %d\n", str, (int)molCntList[i]);
651 fprintf(stream, "Molecule: %s\n", str);
653 tng_molecule_num_chains_get(input, molecule, &nChains);
656 for (gmx_int64_t j = 0; j < nChains; j++)
658 tng_molecule_chain_of_index_get(input, molecule, j, &chain);
659 tng_chain_name_get(input, chain, str, 256);
660 fprintf(stream, "\tChain: %s\n", str);
661 tng_chain_num_residues_get(input, chain, &nResidues);
662 for (gmx_int64_t k = 0; k < nResidues; k++)
664 tng_chain_residue_of_index_get(input, chain, k, &residue);
665 tng_residue_name_get(input, residue, str, 256);
666 fprintf(stream, "\t\tResidue: %s\n", str);
667 tng_residue_num_atoms_get(input, residue, &nAtoms);
668 for (gmx_int64_t l = 0; l < nAtoms; l++)
670 tng_residue_atom_of_index_get(input, residue, l, &atom);
671 tng_atom_name_get(input, atom, str, 256);
672 fprintf(stream, "\t\t\tAtom: %s", str);
673 tng_atom_type_get(input, atom, str, 256);
674 fprintf(stream, " (%s)\n", str);
679 /* It is possible to have a molecule without chains, in which case
680 * residues in the molecule can be iterated through without going
684 tng_molecule_num_residues_get(input, molecule, &nResidues);
687 for (gmx_int64_t k = 0; k < nResidues; k++)
689 tng_molecule_residue_of_index_get(input, molecule, k, &residue);
690 tng_residue_name_get(input, residue, str, 256);
691 fprintf(stream, "\t\tResidue: %s\n", str);
692 tng_residue_num_atoms_get(input, residue, &nAtoms);
693 for (gmx_int64_t l = 0; l < nAtoms; l++)
695 tng_residue_atom_of_index_get(input, residue, l, &atom);
696 tng_atom_name_get(input, atom, str, 256);
697 fprintf(stream, "\t\t\tAtom: %s", str);
698 tng_atom_type_get(input, atom, str, 256);
699 fprintf(stream, " (%s)\n", str);
705 tng_molecule_num_atoms_get(input, molecule, &nAtoms);
706 for (gmx_int64_t l = 0; l < nAtoms; l++)
708 tng_molecule_atom_of_index_get(input, molecule, l, &atom);
709 tng_atom_name_get(input, atom, str, 256);
710 fprintf(stream, "\t\t\tAtom: %s", str);
711 tng_atom_type_get(input, atom, str, 256);
712 fprintf(stream, " (%s)\n", str);
718 GMX_UNUSED_VALUE(input);
719 GMX_UNUSED_VALUE(stream);
723 gmx_bool gmx_get_tng_data_block_types_of_next_frame(tng_trajectory_t input,
726 gmx_int64_t *requestedIds,
727 gmx_int64_t *nextFrame,
728 gmx_int64_t *nBlocks,
729 gmx_int64_t **blockIds)
732 tng_function_status stat;
734 stat = tng_util_trajectory_next_frame_present_data_blocks_find(input, frame,
735 nRequestedIds, requestedIds,
739 if (stat == TNG_CRITICAL)
741 gmx_file("Cannot read TNG file. Cannot find data blocks of next frame.");
743 else if (stat == TNG_FAILURE)
749 GMX_UNUSED_VALUE(input);
750 GMX_UNUSED_VALUE(frame);
751 GMX_UNUSED_VALUE(nRequestedIds);
752 GMX_UNUSED_VALUE(requestedIds);
753 GMX_UNUSED_VALUE(nextFrame);
754 GMX_UNUSED_VALUE(nBlocks);
755 GMX_UNUSED_VALUE(blockIds);
760 gmx_bool gmx_get_tng_data_next_frame_of_block_type(tng_trajectory_t input,
763 gmx_int64_t *frameNumber,
765 gmx_int64_t *nValuesPerFrame,
773 tng_function_status stat;
774 char datatype = -1, codecId;
779 stat = tng_data_block_name_get(input, blockId, name, maxLen);
780 if (stat != TNG_SUCCESS)
782 gmx_file("Cannot read next frame of TNG file");
784 stat = tng_data_block_dependency_get(input, blockId, &blockDependency);
785 if (stat != TNG_SUCCESS)
787 gmx_file("Cannot read next frame of TNG file");
789 if (blockDependency & TNG_PARTICLE_DEPENDENT)
791 tng_num_particles_get(input, nAtoms);
792 stat = tng_util_particle_data_next_frame_read(input,
801 *nAtoms = 1; /* There are not actually any atoms, but it is used for
803 stat = tng_util_non_particle_data_next_frame_read(input,
810 if (stat == TNG_CRITICAL)
812 gmx_file("Cannot read next frame of TNG file");
814 if (stat == TNG_FAILURE)
820 stat = tng_data_block_num_values_per_frame_get(input, blockId, nValuesPerFrame);
821 if (stat != TNG_SUCCESS)
823 gmx_file("Cannot read next frame of TNG file");
825 snew(*values, sizeof(real) * *nValuesPerFrame * *nAtoms);
826 convert_array_to_real_array(data,
828 getDistanceScaleFactor(input),
833 tng_util_frame_current_compression_get(input, blockId, &codecId, &localPrec);
835 /* This must be updated if/when more lossy compression methods are added */
836 if (codecId != TNG_TNG_COMPRESSION)
848 GMX_UNUSED_VALUE(input);
849 GMX_UNUSED_VALUE(blockId);
850 GMX_UNUSED_VALUE(values);
851 GMX_UNUSED_VALUE(frameNumber);
852 GMX_UNUSED_VALUE(frameTime);
853 GMX_UNUSED_VALUE(nValuesPerFrame);
854 GMX_UNUSED_VALUE(nAtoms);
855 GMX_UNUSED_VALUE(prec);
856 GMX_UNUSED_VALUE(name);
857 GMX_UNUSED_VALUE(maxLen);
858 GMX_UNUSED_VALUE(bOK);