1 /* This code is part of the tng binary trajectory format.
3 * The high-level API of the TNG API is used where appropriate.
5 * Written by Magnus Lundborg
6 * Copyright (c) 2012-2013, The GROMACS development team.
7 * Check out http://www.gromacs.org for more information.
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the Revised BSD License.
14 #ifdef USE_STD_INTTYPES_H
20 #include "tng/tng_io.h"
22 int main(int argc, char **argv)
24 tng_trajectory_t traj;
25 /* Assume that the data is stored as floats. The data is placed in 1-D
27 float *positions = 0, *box_shape = 0;
28 int64_t n_particles, n_frames, tot_n_frames, stride_length, i, j;
29 /* Set a default frame range */
30 int first_frame = 0, last_frame = 5000, n_strides;
34 printf("No file specified\n");
36 printf("tng_io_read_pos_util <tng_file> "
37 "[first_frame = %d] [last_frame = %d]\n",
38 first_frame, last_frame);
42 /* A reference must be passed to allocate memory */
43 tng_util_trajectory_open(argv[1], 'r', &traj);
47 first_frame = strtol(argv[2], 0, 10);
50 last_frame = strtol(argv[3], 0, 10);
54 if(tng_num_frames_get(traj, &tot_n_frames) != TNG_SUCCESS)
56 printf("Cannot determine the number of frames in the file\n");
57 tng_util_trajectory_close(&traj);
61 if(tng_num_particles_get(traj, &n_particles) != TNG_SUCCESS)
63 printf("Cannot determine the number of particles in the file\n");
64 tng_util_trajectory_close(&traj);
68 printf("%"PRId64" frames in file\n", tot_n_frames);
70 if(last_frame > tot_n_frames - 1)
72 last_frame = tot_n_frames - 1;
76 n_frames = last_frame - first_frame + 1;
78 if(tng_util_box_shape_read(traj, &box_shape, &stride_length) ==
81 printf("Simulation box shape: ");
84 printf("%f ", box_shape[i]);
90 printf("Simulation box shape not set in the file (or could not be read)\n");
93 n_strides = (n_frames % stride_length) ? n_frames / stride_length + 1:
94 n_frames / stride_length;
96 /* Get the positions of all particles in the requested frame range.
97 * The positions are stored in the positions array.
98 * N.B. No proper error checks. */
99 if(tng_util_pos_read_range(traj, 0, last_frame, &positions, &stride_length)
102 /* Print the positions of the wanted particle (zero based) */
103 for(i=0; i < n_strides; i++)
105 printf("\nFrame %"PRId64":\n", first_frame + i*stride_length);
106 for(j=0; j < n_particles; j++)
108 printf("Atom nr: %"PRId64"", j);
109 printf("\t%f", positions[i*n_particles*3+j*3]);
110 printf("\t%f", positions[i*n_particles*3+j*3+1]);
111 printf("\t%f", positions[i*n_particles*3+j*3+2]);
118 printf("Cannot read positions\n");
130 tng_util_trajectory_close(&traj);