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.
35 #include "tngio_for_tools.h"
47 #include "tng/tng_io.h"
50 #include "gromacs/legacyheaders/types/atoms.h"
51 #include "gromacs/legacyheaders/physics.h"
52 #include "gromacs/legacyheaders/gmx_fatal.h"
54 #include "gromacs/utility/common.h"
55 #include "gromacs/utility/smalloc.h"
57 void gmx_prepare_tng_writing(const char *filename,
59 tng_trajectory_t *input,
60 tng_trajectory_t *output,
62 const gmx_mtop_t *mtop,
64 const char *indexGroupName)
67 /* FIXME after 5.0: Currently only standard block types are read */
68 const int defaultNumIds = 5;
69 static gmx_int64_t fallbackIds[defaultNumIds] =
71 TNG_TRAJ_BOX_SHAPE, TNG_TRAJ_POSITIONS,
72 TNG_TRAJ_VELOCITIES, TNG_TRAJ_FORCES,
75 static char fallbackNames[defaultNumIds][32] =
77 "BOX SHAPE", "POSITIONS", "VELOCITIES",
82 gmx_tng_open(filename, mode, output);
84 /* Do we have an input file in TNG format? If so, then there's
85 more data we can copy over, rather than having to improvise. */
88 /* Set parameters (compression, time per frame, molecule
89 * information, number of frames per frame set and writing
90 * intervals of positions, box shape and lambdas) of the
91 * output tng container based on their respective values int
92 * the input tng container */
93 double time, compression_precision;
94 gmx_int64_t n_frames_per_frame_set, interval = -1;
96 tng_compression_precision_get(*input, &compression_precision);
97 tng_compression_precision_set(*output, compression_precision);
98 // TODO make this configurable in a future version
99 char compression_type = TNG_TNG_COMPRESSION;
101 tng_molecule_system_copy(*input, *output);
103 tng_time_per_frame_get(*input, &time);
104 tng_time_per_frame_set(*output, time);
106 tng_num_frames_per_frame_set_get(*input, &n_frames_per_frame_set);
107 tng_num_frames_per_frame_set_set(*output, n_frames_per_frame_set);
109 for (int i = 0; i < defaultNumIds; i++)
111 if (tng_data_get_stride_length(*input, fallbackIds[i], -1, &interval)
114 switch (fallbackIds[i])
116 case TNG_TRAJ_POSITIONS:
117 case TNG_TRAJ_VELOCITIES:
118 tng_util_generic_write_interval_set(*output, interval, 3, fallbackIds[i],
119 fallbackNames[i], TNG_PARTICLE_BLOCK_DATA,
122 case TNG_TRAJ_FORCES:
123 tng_util_generic_write_interval_set(*output, interval, 3, fallbackIds[i],
124 fallbackNames[i], TNG_PARTICLE_BLOCK_DATA,
125 TNG_GZIP_COMPRESSION);
127 case TNG_TRAJ_BOX_SHAPE:
128 tng_util_generic_write_interval_set(*output, interval, 9, fallbackIds[i],
129 fallbackNames[i], TNG_NON_PARTICLE_BLOCK_DATA,
130 TNG_GZIP_COMPRESSION);
133 tng_util_generic_write_interval_set(*output, interval, 1, fallbackIds[i],
134 fallbackNames[i], TNG_NON_PARTICLE_BLOCK_DATA,
135 TNG_GZIP_COMPRESSION);
145 /* TODO after trjconv is modularized: fix this so the user can
146 change precision when they are doing an operation where
147 this makes sense, and not otherwise.
149 char compression = bUseLossyCompression ? TNG_TNG_COMPRESSION : TNG_GZIP_COMPRESSION;
150 gmx_tng_set_compression_precision(*output, ndec2prec(nDecimalsOfPrecision));
152 gmx_tng_add_mtop(*output, mtop);
153 tng_num_frames_per_frame_set_set(*output, 1);
156 if (index && nAtoms > 0)
158 gmx_tng_setup_atom_subgroup(*output, nAtoms, index, indexGroupName);
161 /* If for some reason there are more requested atoms than there are atoms in the
162 * molecular system create a number of implicit atoms (without atom data) to
163 * compensate for that. */
166 tng_implicit_num_particles_set(*output, nAtoms);
169 GMX_UNUSED_VALUE(filename);
170 GMX_UNUSED_VALUE(mode);
171 GMX_UNUSED_VALUE(input);
172 GMX_UNUSED_VALUE(output);
173 GMX_UNUSED_VALUE(nAtoms);
174 GMX_UNUSED_VALUE(mtop);
175 GMX_UNUSED_VALUE(index);
176 GMX_UNUSED_VALUE(indexGroupName);
180 void gmx_write_tng_from_trxframe(tng_trajectory_t output,
187 double timePerFrame = frame->time * PICO / frame->step;
188 tng_time_per_frame_set(output, timePerFrame);
192 natoms = frame->natoms;
194 gmx_fwrite_tng(output,
199 (const rvec *) frame->box,
201 (const rvec *) frame->x,
202 (const rvec *) frame->v,
203 (const rvec *) frame->f);
205 GMX_UNUSED_VALUE(output);
206 GMX_UNUSED_VALUE(frame);
207 GMX_UNUSED_VALUE(natoms);
213 convert_array_to_real_array(void *from,
225 if (sizeof(real) == sizeof(float))
229 memcpy(to, from, nValues * sizeof(real) * nAtoms);
233 for (i = 0; i < nAtoms; i++)
235 for (j = 0; j < nValues; j++)
237 to[i*nValues+j] = (real)((float *)from)[i*nValues+j] * fact;
244 for (i = 0; i < nAtoms; i++)
246 for (j = 0; j < nValues; j++)
248 to[i*nValues+j] = (real)((float *)from)[i*nValues+j] * fact;
254 for (i = 0; i < nAtoms; i++)
256 for (j = 0; j < nValues; j++)
258 to[i*nValues+j] = (real)((gmx_int64_t *)from)[i*nValues+j] * fact;
262 case TNG_DOUBLE_DATA:
263 if (sizeof(real) == sizeof(double))
267 memcpy(to, from, nValues * sizeof(real) * nAtoms);
271 for (i = 0; i < nAtoms; i++)
273 for (j = 0; j < nValues; j++)
275 to[i*nValues+j] = (real)((double *)from)[i*nValues+j] * fact;
282 for (i = 0; i < nAtoms; i++)
284 for (j = 0; j < nValues; j++)
286 to[i*nValues+j] = (real)((double *)from)[i*nValues+j] * fact;
292 gmx_incons("Illegal datatype when converting values to a real array!");
298 static real getDistanceScaleFactor(tng_trajectory_t in)
300 gmx_int64_t exp = -1;
301 real distanceScaleFactor;
303 // TODO Hopefully, TNG 2.0 will do this kind of thing for us
304 tng_distance_unit_exponential_get(in, &exp);
306 // GROMACS expects distances in nm
310 distanceScaleFactor = NANO/NANO;
313 distanceScaleFactor = NANO/ANGSTROM;
316 distanceScaleFactor = pow(10.0, exp + 9.0);
319 return distanceScaleFactor;
323 void gmx_tng_setup_atom_subgroup(tng_trajectory_t tng,
329 gmx_int64_t nAtoms, cnt, nMols;
330 tng_molecule_t mol, iterMol;
334 tng_function_status stat;
336 tng_num_particles_get(tng, &nAtoms);
343 stat = tng_molecule_find(tng, name, -1, &mol);
344 if (stat == TNG_SUCCESS)
346 tng_molecule_num_atoms_get(tng, mol, &nAtoms);
347 tng_molecule_cnt_get(tng, mol, &cnt);
357 if (stat == TNG_FAILURE)
359 /* The indexed atoms are added to one separate molecule. */
360 tng_molecule_alloc(tng, &mol);
361 tng_molecule_name_set(tng, mol, name);
362 tng_molecule_chain_add(tng, mol, "", &chain);
364 for (int i = 0; i < nind; i++)
366 char temp_name[256], temp_type[256];
368 /* Try to retrieve the residue name of the atom */
369 stat = tng_residue_name_of_particle_nr_get(tng, ind[i], temp_name, 256);
370 if (stat != TNG_SUCCESS)
374 /* Check if the molecule of the selection already contains this residue */
375 if (tng_chain_residue_find(tng, chain, temp_name, -1, &res)
378 tng_chain_residue_add(tng, chain, temp_name, &res);
380 /* Try to find the original name and type of the atom */
381 stat = tng_atom_name_of_particle_nr_get(tng, ind[i], temp_name, 256);
382 if (stat != TNG_SUCCESS)
386 stat = tng_atom_type_of_particle_nr_get(tng, ind[i], temp_type, 256);
387 if (stat != TNG_SUCCESS)
391 tng_residue_atom_w_id_add(tng, res, temp_name, temp_type, ind[i], &atom);
393 tng_molecule_existing_add(tng, &mol);
395 /* Set the count of the molecule containing the selected atoms to 1 and all
396 * other molecules to 0 */
397 tng_molecule_cnt_set(tng, mol, 1);
398 tng_num_molecule_types_get(tng, &nMols);
399 for (gmx_int64_t k = 0; k < nMols; k++)
401 tng_molecule_of_index_get(tng, k, &iterMol);
406 tng_molecule_cnt_set(tng, iterMol, 0);
409 GMX_UNUSED_VALUE(tng);
410 GMX_UNUSED_VALUE(nind);
411 GMX_UNUSED_VALUE(ind);
412 GMX_UNUSED_VALUE(name);
416 /* TODO: If/when TNG acquires the ability to copy data blocks without
417 * uncompressing them, then this implemenation should be reconsidered.
418 * Ideally, gmx trjconv -f a.tng -o b.tng -b 10 -e 20 would be fast
419 * and lose no information. */
420 gmx_bool gmx_read_next_tng_frame(tng_trajectory_t input,
422 gmx_int64_t *requestedIds,
427 tng_function_status stat;
428 gmx_int64_t numberOfAtoms = -1, frameNumber = -1;
429 gmx_int64_t nBlocks, blockId, *blockIds = NULL, codecId;
432 double frameTime = -1.0;
433 int size, blockDependency;
435 const int defaultNumIds = 5;
436 static gmx_int64_t fallbackRequestedIds[defaultNumIds] =
438 TNG_TRAJ_BOX_SHAPE, TNG_TRAJ_POSITIONS,
439 TNG_TRAJ_VELOCITIES, TNG_TRAJ_FORCES,
454 /* If no specific IDs were requested read all block types that can
455 * currently be interpreted */
456 if (!requestedIds || numRequestedIds == 0)
458 numRequestedIds = defaultNumIds;
459 requestedIds = fallbackRequestedIds;
462 stat = tng_num_particles_get(input, &numberOfAtoms);
463 if (stat != TNG_SUCCESS)
465 gmx_file("Cannot determine number of atoms from TNG file.");
467 fr->natoms = numberOfAtoms;
469 if (!gmx_get_tng_data_block_types_of_next_frame(input,
485 for (gmx_int64_t i = 0; i < nBlocks; i++)
487 blockId = blockIds[i];
488 tng_data_block_dependency_get(input, blockId, &blockDependency);
489 if (blockDependency & TNG_PARTICLE_DEPENDENT)
491 stat = tng_util_particle_data_next_frame_read(input,
500 stat = tng_util_non_particle_data_next_frame_read(input,
507 if (stat == TNG_CRITICAL)
509 gmx_file("Cannot read positions from TNG file.");
512 else if (stat == TNG_FAILURE)
518 case TNG_TRAJ_BOX_SHAPE:
522 size = sizeof(gmx_int64_t);
525 size = sizeof(float);
527 case TNG_DOUBLE_DATA:
528 size = sizeof(double);
531 size = 0; /* Just to make the compiler happy. */
532 gmx_incons("Illegal datatype of box shape values!");
534 for (int i = 0; i < DIM; i++)
536 convert_array_to_real_array((char *)(values) + size * i * DIM,
538 getDistanceScaleFactor(input),
545 case TNG_TRAJ_POSITIONS:
546 srenew(fr->x, fr->natoms);
547 convert_array_to_real_array(values,
549 getDistanceScaleFactor(input),
554 tng_util_frame_current_compression_get(input, blockId, &codecId, &prec);
555 /* This must be updated if/when more lossy compression methods are added */
556 if (codecId == TNG_TNG_COMPRESSION)
562 case TNG_TRAJ_VELOCITIES:
563 srenew(fr->v, fr->natoms);
564 convert_array_to_real_array(values,
566 getDistanceScaleFactor(input),
571 tng_util_frame_current_compression_get(input, blockId, &codecId, &prec);
572 /* This must be updated if/when more lossy compression methods are added */
573 if (codecId == TNG_TNG_COMPRESSION)
579 case TNG_TRAJ_FORCES:
580 srenew(fr->f, fr->natoms);
581 convert_array_to_real_array(values,
583 getDistanceScaleFactor(input),
593 fr->lambda = (*(float *)values);
595 case TNG_DOUBLE_DATA:
596 fr->lambda = (*(double *)values);
599 gmx_incons("Illegal datatype lambda value!");
604 gmx_warning("Illegal block type! Currently GROMACS tools can only handle certain data types. Skipping block.");
606 /* values does not have to be freed before reading next frame. It will
607 * be reallocated if it is not NULL. */
610 fr->step = (int) frameNumber;
612 // Convert the time to ps
613 fr->time = frameTime / PICO;
616 /* values must be freed before leaving this function */
621 GMX_UNUSED_VALUE(input);
622 GMX_UNUSED_VALUE(fr);
623 GMX_UNUSED_VALUE(requestedIds);
624 GMX_UNUSED_VALUE(numRequestedIds);
629 void gmx_print_tng_molecule_system(tng_trajectory_t input,
633 gmx_int64_t nMolecules, nChains, nResidues, nAtoms, *molCntList;
634 tng_molecule_t molecule;
636 tng_residue_t residue;
638 char str[256], varNAtoms;
640 tng_num_molecule_types_get(input, &nMolecules);
641 tng_molecule_cnt_list_get(input, &molCntList);
642 /* Can the number of particles change in the trajectory or is it constant? */
643 tng_num_particles_variable_get(input, &varNAtoms);
645 for (gmx_int64_t i = 0; i < nMolecules; i++)
647 tng_molecule_of_index_get(input, i, &molecule);
648 tng_molecule_name_get(input, molecule, str, 256);
649 if (varNAtoms == TNG_CONSTANT_N_ATOMS)
651 if ((int)molCntList[i] == 0)
655 fprintf(stream, "Molecule: %s, count: %d\n", str, (int)molCntList[i]);
659 fprintf(stream, "Molecule: %s\n", str);
661 tng_molecule_num_chains_get(input, molecule, &nChains);
664 for (gmx_int64_t j = 0; j < nChains; j++)
666 tng_molecule_chain_of_index_get(input, molecule, j, &chain);
667 tng_chain_name_get(input, chain, str, 256);
668 fprintf(stream, "\tChain: %s\n", str);
669 tng_chain_num_residues_get(input, chain, &nResidues);
670 for (gmx_int64_t k = 0; k < nResidues; k++)
672 tng_chain_residue_of_index_get(input, chain, k, &residue);
673 tng_residue_name_get(input, residue, str, 256);
674 fprintf(stream, "\t\tResidue: %s\n", str);
675 tng_residue_num_atoms_get(input, residue, &nAtoms);
676 for (gmx_int64_t l = 0; l < nAtoms; l++)
678 tng_residue_atom_of_index_get(input, residue, l, &atom);
679 tng_atom_name_get(input, atom, str, 256);
680 fprintf(stream, "\t\t\tAtom: %s", str);
681 tng_atom_type_get(input, atom, str, 256);
682 fprintf(stream, " (%s)\n", str);
687 /* It is possible to have a molecule without chains, in which case
688 * residues in the molecule can be iterated through without going
692 tng_molecule_num_residues_get(input, molecule, &nResidues);
695 for (gmx_int64_t k = 0; k < nResidues; k++)
697 tng_molecule_residue_of_index_get(input, molecule, k, &residue);
698 tng_residue_name_get(input, residue, str, 256);
699 fprintf(stream, "\t\tResidue: %s\n", str);
700 tng_residue_num_atoms_get(input, residue, &nAtoms);
701 for (gmx_int64_t l = 0; l < nAtoms; l++)
703 tng_residue_atom_of_index_get(input, residue, l, &atom);
704 tng_atom_name_get(input, atom, str, 256);
705 fprintf(stream, "\t\t\tAtom: %s", str);
706 tng_atom_type_get(input, atom, str, 256);
707 fprintf(stream, " (%s)\n", str);
713 tng_molecule_num_atoms_get(input, molecule, &nAtoms);
714 for (gmx_int64_t l = 0; l < nAtoms; l++)
716 tng_molecule_atom_of_index_get(input, molecule, l, &atom);
717 tng_atom_name_get(input, atom, str, 256);
718 fprintf(stream, "\t\t\tAtom: %s", str);
719 tng_atom_type_get(input, atom, str, 256);
720 fprintf(stream, " (%s)\n", str);
726 GMX_UNUSED_VALUE(input);
727 GMX_UNUSED_VALUE(stream);
731 gmx_bool gmx_get_tng_data_block_types_of_next_frame(tng_trajectory_t input,
734 gmx_int64_t *requestedIds,
735 gmx_int64_t *nextFrame,
736 gmx_int64_t *nBlocks,
737 gmx_int64_t **blockIds)
740 tng_function_status stat;
742 stat = tng_util_trajectory_next_frame_present_data_blocks_find(input, frame,
743 nRequestedIds, requestedIds,
747 if (stat == TNG_CRITICAL)
749 gmx_file("Cannot read TNG file. Cannot find data blocks of next frame.");
751 else if (stat == TNG_FAILURE)
757 GMX_UNUSED_VALUE(input);
758 GMX_UNUSED_VALUE(frame);
759 GMX_UNUSED_VALUE(nRequestedIds);
760 GMX_UNUSED_VALUE(requestedIds);
761 GMX_UNUSED_VALUE(nextFrame);
762 GMX_UNUSED_VALUE(nBlocks);
763 GMX_UNUSED_VALUE(blockIds);
768 gmx_bool gmx_get_tng_data_next_frame_of_block_type(tng_trajectory_t input,
771 gmx_int64_t *frameNumber,
773 gmx_int64_t *nValuesPerFrame,
781 tng_function_status stat;
788 stat = tng_data_block_name_get(input, blockId, name, maxLen);
789 if (stat != TNG_SUCCESS)
791 gmx_file("Cannot read next frame of TNG file");
793 stat = tng_data_block_dependency_get(input, blockId, &blockDependency);
794 if (stat != TNG_SUCCESS)
796 gmx_file("Cannot read next frame of TNG file");
798 if (blockDependency & TNG_PARTICLE_DEPENDENT)
800 tng_num_particles_get(input, nAtoms);
801 stat = tng_util_particle_data_next_frame_read(input,
810 *nAtoms = 1; /* There are not actually any atoms, but it is used for
812 stat = tng_util_non_particle_data_next_frame_read(input,
819 if (stat == TNG_CRITICAL)
821 gmx_file("Cannot read next frame of TNG file");
823 if (stat == TNG_FAILURE)
829 stat = tng_data_block_num_values_per_frame_get(input, blockId, nValuesPerFrame);
830 if (stat != TNG_SUCCESS)
832 gmx_file("Cannot read next frame of TNG file");
834 snew(*values, sizeof(real) * *nValuesPerFrame * *nAtoms);
835 convert_array_to_real_array(data,
837 getDistanceScaleFactor(input),
842 tng_util_frame_current_compression_get(input, blockId, &codecId, &localPrec);
844 /* This must be updated if/when more lossy compression methods are added */
845 if (codecId != TNG_TNG_COMPRESSION)
857 GMX_UNUSED_VALUE(input);
858 GMX_UNUSED_VALUE(blockId);
859 GMX_UNUSED_VALUE(values);
860 GMX_UNUSED_VALUE(frameNumber);
861 GMX_UNUSED_VALUE(frameTime);
862 GMX_UNUSED_VALUE(nValuesPerFrame);
863 GMX_UNUSED_VALUE(nAtoms);
864 GMX_UNUSED_VALUE(prec);
865 GMX_UNUSED_VALUE(name);
866 GMX_UNUSED_VALUE(maxLen);
867 GMX_UNUSED_VALUE(bOK);