Bumped the TNG library to the latest version.
[gromacs.git] / src / external / tng_io / src / tests / tng_io_read_pos_util.c
blob1667bae2d8e3501bf99d6272ffdf86d7158495d1
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
15 #include <inttypes.h>
16 #endif
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include "../../include/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
26 * arrays */
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;
32 if(argc <= 1)
34 printf("No file specified\n");
35 printf("Usage:\n");
36 printf("tng_io_read_pos_util <tng_file> "
37 "[first_frame = %d] [last_frame = %d]\n",
38 first_frame, last_frame);
39 exit(1);
42 /* A reference must be passed to allocate memory */
43 tng_util_trajectory_open(argv[1], 'r', &traj);
45 if(argc >= 3)
47 first_frame = strtol(argv[2], 0, 10);
48 if(argc >= 4)
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);
58 exit(1);
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);
65 exit(1);
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) ==
79 TNG_SUCCESS)
81 printf("Simulation box shape: ");
82 for(i=0; i < 9; i++)
84 printf("%f ", box_shape[i]);
86 printf("\n");
88 else
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)
100 == TNG_SUCCESS)
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]);
112 printf("\n");
116 else
118 printf("Cannot read positions\n");
121 /* Free memory */
122 if(positions)
124 free(positions);
126 if(box_shape)
128 free(box_shape);
130 tng_util_trajectory_close(&traj);
132 return(0);