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 "../../external/tng_io/include/tng_io.h"
50 #include "gromacs/math/units.h"
51 #include "gromacs/utility/common.h"
52 #include "gromacs/utility/fatalerror.h"
53 #include "gromacs/utility/smalloc.h"
55 void gmx_prepare_tng_writing(const char *filename,
57 tng_trajectory_t *input,
58 tng_trajectory_t *output,
60 const gmx_mtop_t *mtop,
62 const char *indexGroupName)
65 /* FIXME after 5.0: Currently only standard block types are read */
66 const int defaultNumIds = 5;
67 static gmx_int64_t fallbackIds[defaultNumIds] =
69 TNG_TRAJ_BOX_SHAPE, TNG_TRAJ_POSITIONS,
70 TNG_TRAJ_VELOCITIES, TNG_TRAJ_FORCES,
73 static char fallbackNames[defaultNumIds][32] =
75 "BOX SHAPE", "POSITIONS", "VELOCITIES",
80 gmx_tng_open(filename, mode, output);
82 /* Do we have an input file in TNG format? If so, then there's
83 more data we can copy over, rather than having to improvise. */
86 /* Set parameters (compression, time per frame, molecule
87 * information, number of frames per frame set and writing
88 * intervals of positions, box shape and lambdas) of the
89 * output tng container based on their respective values int
90 * the input tng container */
91 double time, compression_precision;
92 gmx_int64_t n_frames_per_frame_set, interval = -1;
94 tng_compression_precision_get(*input, &compression_precision);
95 tng_compression_precision_set(*output, compression_precision);
96 // TODO make this configurable in a future version
97 char compression_type = TNG_TNG_COMPRESSION;
99 tng_molecule_system_copy(*input, *output);
101 tng_time_per_frame_get(*input, &time);
102 tng_time_per_frame_set(*output, time);
104 tng_num_frames_per_frame_set_get(*input, &n_frames_per_frame_set);
105 tng_num_frames_per_frame_set_set(*output, n_frames_per_frame_set);
107 for (int i = 0; i < defaultNumIds; i++)
109 if (tng_data_get_stride_length(*input, fallbackIds[i], -1, &interval)
112 switch (fallbackIds[i])
114 case TNG_TRAJ_POSITIONS:
115 case TNG_TRAJ_VELOCITIES:
116 tng_util_generic_write_interval_set(*output, interval, 3, fallbackIds[i],
117 fallbackNames[i], TNG_PARTICLE_BLOCK_DATA,
120 case TNG_TRAJ_FORCES:
121 tng_util_generic_write_interval_set(*output, interval, 3, fallbackIds[i],
122 fallbackNames[i], TNG_PARTICLE_BLOCK_DATA,
123 TNG_GZIP_COMPRESSION);
125 case TNG_TRAJ_BOX_SHAPE:
126 tng_util_generic_write_interval_set(*output, interval, 9, fallbackIds[i],
127 fallbackNames[i], TNG_NON_PARTICLE_BLOCK_DATA,
128 TNG_GZIP_COMPRESSION);
131 tng_util_generic_write_interval_set(*output, interval, 1, fallbackIds[i],
132 fallbackNames[i], TNG_NON_PARTICLE_BLOCK_DATA,
133 TNG_GZIP_COMPRESSION);
143 /* TODO after trjconv is modularized: fix this so the user can
144 change precision when they are doing an operation where
145 this makes sense, and not otherwise.
147 char compression = bUseLossyCompression ? TNG_TNG_COMPRESSION : TNG_GZIP_COMPRESSION;
148 gmx_tng_set_compression_precision(*output, ndec2prec(nDecimalsOfPrecision));
150 gmx_tng_add_mtop(*output, mtop);
151 tng_num_frames_per_frame_set_set(*output, 1);
154 if (index && nAtoms > 0)
156 gmx_tng_setup_atom_subgroup(*output, nAtoms, index, indexGroupName);
159 /* If for some reason there are more requested atoms than there are atoms in the
160 * molecular system create a number of implicit atoms (without atom data) to
161 * compensate for that. */
164 tng_implicit_num_particles_set(*output, nAtoms);
167 GMX_UNUSED_VALUE(filename);
168 GMX_UNUSED_VALUE(mode);
169 GMX_UNUSED_VALUE(input);
170 GMX_UNUSED_VALUE(output);
171 GMX_UNUSED_VALUE(nAtoms);
175 void gmx_write_tng_from_trxframe(tng_trajectory_t output,
182 double timePerFrame = frame->time * PICO / frame->step;
183 tng_time_per_frame_set(output, timePerFrame);
187 natoms = frame->natoms;
189 gmx_fwrite_tng(output,
194 (const rvec *) frame->box,
196 (const rvec *) frame->x,
197 (const rvec *) frame->v,
198 (const rvec *) frame->f);
200 GMX_UNUSED_VALUE(output);
201 GMX_UNUSED_VALUE(frame);
207 convert_array_to_real_array(void *from,
219 if (sizeof(real) == sizeof(float))
223 memcpy(to, from, nValues * sizeof(real) * nAtoms);
227 for (i = 0; i < nAtoms; i++)
229 for (j = 0; j < nValues; j++)
231 to[i*nValues+j] = (real)((float *)from)[i*nValues+j] * fact;
238 for (i = 0; i < nAtoms; i++)
240 for (j = 0; j < nValues; j++)
242 to[i*nValues+j] = (real)((float *)from)[i*nValues+j] * fact;
248 for (i = 0; i < nAtoms; i++)
250 for (j = 0; j < nValues; j++)
252 to[i*nValues+j] = (real)((gmx_int64_t *)from)[i*nValues+j] * fact;
256 case TNG_DOUBLE_DATA:
257 if (sizeof(real) == sizeof(double))
261 memcpy(to, from, nValues * sizeof(real) * nAtoms);
265 for (i = 0; i < nAtoms; i++)
267 for (j = 0; j < nValues; j++)
269 to[i*nValues+j] = (real)((double *)from)[i*nValues+j] * fact;
276 for (i = 0; i < nAtoms; i++)
278 for (j = 0; j < nValues; j++)
280 to[i*nValues+j] = (real)((double *)from)[i*nValues+j] * fact;
286 gmx_incons("Illegal datatype when converting values to a real array!");
292 static real getDistanceScaleFactor(tng_trajectory_t in)
294 gmx_int64_t exp = -1;
295 real distanceScaleFactor;
297 // TODO Hopefully, TNG 2.0 will do this kind of thing for us
298 tng_distance_unit_exponential_get(in, &exp);
300 // GROMACS expects distances in nm
304 distanceScaleFactor = NANO/NANO;
307 distanceScaleFactor = NANO/ANGSTROM;
310 distanceScaleFactor = pow(10.0, exp + 9.0);
313 return distanceScaleFactor;
317 void gmx_tng_setup_atom_subgroup(tng_trajectory_t tng,
323 gmx_int64_t nAtoms, cnt, nMols;
324 tng_molecule_t mol, iterMol;
328 tng_function_status stat;
330 tng_num_particles_get(tng, &nAtoms);
337 stat = tng_molecule_find(tng, name, -1, &mol);
338 if (stat == TNG_SUCCESS)
340 tng_molecule_num_atoms_get(tng, mol, &nAtoms);
341 tng_molecule_cnt_get(tng, mol, &cnt);
351 if (stat == TNG_FAILURE)
353 /* The indexed atoms are added to one separate molecule. */
354 tng_molecule_alloc(tng, &mol);
355 tng_molecule_name_set(tng, mol, name);
356 tng_molecule_chain_add(tng, mol, "", &chain);
358 for (int i = 0; i < nind; i++)
360 char temp_name[256], temp_type[256];
362 /* Try to retrieve the residue name of the atom */
363 stat = tng_residue_name_of_particle_nr_get(tng, ind[i], temp_name, 256);
364 if (stat != TNG_SUCCESS)
368 /* Check if the molecule of the selection already contains this residue */
369 if (tng_chain_residue_find(tng, chain, temp_name, -1, &res)
372 tng_chain_residue_add(tng, chain, temp_name, &res);
374 /* Try to find the original name and type of the atom */
375 stat = tng_atom_name_of_particle_nr_get(tng, ind[i], temp_name, 256);
376 if (stat != TNG_SUCCESS)
380 stat = tng_atom_type_of_particle_nr_get(tng, ind[i], temp_type, 256);
381 if (stat != TNG_SUCCESS)
385 tng_residue_atom_w_id_add(tng, res, temp_name, temp_type, ind[i], &atom);
387 tng_molecule_existing_add(tng, &mol);
389 /* Set the count of the molecule containing the selected atoms to 1 and all
390 * other molecules to 0 */
391 tng_molecule_cnt_set(tng, mol, 1);
392 tng_num_molecule_types_get(tng, &nMols);
393 for (gmx_int64_t k = 0; k < nMols; k++)
395 tng_molecule_of_index_get(tng, k, &iterMol);
400 tng_molecule_cnt_set(tng, iterMol, 0);
403 GMX_UNUSED_VALUE(tng);
404 GMX_UNUSED_VALUE(nind);
405 GMX_UNUSED_VALUE(ind);
406 GMX_UNUSED_VALUE(name);
410 /* TODO: If/when TNG acquires the ability to copy data blocks without
411 * uncompressing them, then this implemenation should be reconsidered.
412 * Ideally, gmx trjconv -f a.tng -o b.tng -b 10 -e 20 would be fast
413 * and lose no information. */
414 gmx_bool gmx_read_next_tng_frame(tng_trajectory_t input,
416 gmx_int64_t *requestedIds,
421 tng_function_status stat;
422 gmx_int64_t numberOfAtoms = -1, frameNumber = -1;
423 gmx_int64_t nBlocks, blockId, *blockIds = NULL, codecId;
426 double frameTime = -1.0;
427 int size, blockDependency;
429 const int defaultNumIds = 5;
430 static gmx_int64_t fallbackRequestedIds[defaultNumIds] =
432 TNG_TRAJ_BOX_SHAPE, TNG_TRAJ_POSITIONS,
433 TNG_TRAJ_VELOCITIES, TNG_TRAJ_FORCES,
448 /* If no specific IDs were requested read all block types that can
449 * currently be interpreted */
450 if (!requestedIds || numRequestedIds == 0)
452 numRequestedIds = defaultNumIds;
453 requestedIds = fallbackRequestedIds;
456 stat = tng_num_particles_get(input, &numberOfAtoms);
457 if (stat != TNG_SUCCESS)
459 gmx_file("Cannot determine number of atoms from TNG file.");
461 fr->natoms = numberOfAtoms;
463 if (!gmx_get_tng_data_block_types_of_next_frame(input,
479 for (gmx_int64_t i = 0; i < nBlocks; i++)
481 blockId = blockIds[i];
482 tng_data_block_dependency_get(input, blockId, &blockDependency);
483 if (blockDependency & TNG_PARTICLE_DEPENDENT)
485 stat = tng_util_particle_data_next_frame_read(input,
494 stat = tng_util_non_particle_data_next_frame_read(input,
501 if (stat == TNG_CRITICAL)
503 gmx_file("Cannot read positions from TNG file.");
506 else if (stat == TNG_FAILURE)
512 case TNG_TRAJ_BOX_SHAPE:
516 size = sizeof(gmx_int64_t);
519 size = sizeof(float);
521 case TNG_DOUBLE_DATA:
522 size = sizeof(double);
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;
780 stat = tng_data_block_name_get(input, blockId, name, maxLen);
781 if (stat != TNG_SUCCESS)
783 gmx_file("Cannot read next frame of TNG file");
785 stat = tng_data_block_dependency_get(input, blockId, &blockDependency);
786 if (stat != TNG_SUCCESS)
788 gmx_file("Cannot read next frame of TNG file");
790 if (blockDependency & TNG_PARTICLE_DEPENDENT)
792 tng_num_particles_get(input, nAtoms);
793 stat = tng_util_particle_data_next_frame_read(input,
802 *nAtoms = 1; /* There are not actually any atoms, but it is used for
804 stat = tng_util_non_particle_data_next_frame_read(input,
811 if (stat == TNG_CRITICAL)
813 gmx_file("Cannot read next frame of TNG file");
815 if (stat == TNG_FAILURE)
821 stat = tng_data_block_num_values_per_frame_get(input, blockId, nValuesPerFrame);
822 if (stat != TNG_SUCCESS)
824 gmx_file("Cannot read next frame of TNG file");
826 snew(*values, sizeof(real) * *nValuesPerFrame * *nAtoms);
827 convert_array_to_real_array(data,
829 getDistanceScaleFactor(input),
834 tng_util_frame_current_compression_get(input, blockId, &codecId, &localPrec);
836 /* This must be updated if/when more lossy compression methods are added */
837 if (codecId != TNG_TNG_COMPRESSION)
849 GMX_UNUSED_VALUE(input);
850 GMX_UNUSED_VALUE(blockId);
851 GMX_UNUSED_VALUE(values);
852 GMX_UNUSED_VALUE(frameNumber);
853 GMX_UNUSED_VALUE(frameTime);
854 GMX_UNUSED_VALUE(nValuesPerFrame);
855 GMX_UNUSED_VALUE(nAtoms);
856 GMX_UNUSED_VALUE(prec);
857 GMX_UNUSED_VALUE(name);
858 GMX_UNUSED_VALUE(maxLen);
859 GMX_UNUSED_VALUE(bOK);