TNG version 1.7.3
[alexxy/gromacs.git] / src / external / tng_io / src / tests / tng_io_read_pos_util.c
1 /* This code is part of the tng binary trajectory format.
2  *
3  *  The high-level API of the TNG API is used where appropriate.
4  *
5  * Written by Magnus Lundborg
6  * Copyright (c) 2012-2013, The GROMACS development team.
7  * Check out http://www.gromacs.org for more information.
8  *
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the Revised BSD License.
12  */
13
14 #include "tng/tng_io.h"
15
16 #ifdef USE_STD_INTTYPES_H
17 #include <inttypes.h>
18 #endif
19
20 #include <stdlib.h>
21 #include <stdio.h>
22
23 int main(int argc, char **argv)
24 {
25     tng_trajectory_t traj;
26     /* Assume that the data is stored as floats. The data is placed in 1-D
27      * arrays */
28     float *positions = 0, *box_shape = 0;
29     int64_t n_particles, n_frames, tot_n_frames, stride_length, i, j;
30     /* Set a default frame range */
31     int first_frame = 0, last_frame = 5000, n_strides;
32
33     if(argc <= 1)
34     {
35         printf("No file specified\n");
36         printf("Usage:\n");
37         printf("tng_io_read_pos_util <tng_file> "
38                "[first_frame = %d] [last_frame = %d]\n",
39                first_frame, last_frame);
40         exit(1);
41     }
42
43     /* A reference must be passed to allocate memory */
44     tng_util_trajectory_open(argv[1], 'r', &traj);
45
46     if(argc >= 3)
47     {
48         first_frame = strtol(argv[2], 0, 10);
49         if(argc >= 4)
50         {
51             last_frame = strtol(argv[3], 0, 10);
52         }
53     }
54
55     if(tng_num_frames_get(traj, &tot_n_frames) != TNG_SUCCESS)
56     {
57         printf("Cannot determine the number of frames in the file\n");
58         tng_util_trajectory_close(&traj);
59         exit(1);
60     }
61
62     if(tng_num_particles_get(traj, &n_particles) != TNG_SUCCESS)
63     {
64         printf("Cannot determine the number of particles in the file\n");
65         tng_util_trajectory_close(&traj);
66         exit(1);
67     }
68
69     printf("%"PRId64" frames in file\n", tot_n_frames);
70
71     if(last_frame > tot_n_frames - 1)
72     {
73         last_frame = tot_n_frames - 1;
74     }
75
76
77     n_frames = last_frame - first_frame + 1;
78
79     if(tng_util_box_shape_read(traj, &box_shape, &stride_length) ==
80        TNG_SUCCESS)
81     {
82         printf("Simulation box shape: ");
83         for(i=0; i < 9; i++)
84         {
85             printf("%f ", box_shape[i]);
86         }
87         printf("\n");
88     }
89     else
90     {
91         printf("Simulation box shape not set in the file (or could not be read)\n");
92     }
93
94     n_strides = (n_frames % stride_length) ? n_frames / stride_length + 1:
95                 n_frames / stride_length;
96
97     /* Get the positions of all particles in the requested frame range.
98      * The positions are stored in the positions array.
99      * N.B. No proper error checks. */
100     if(tng_util_pos_read_range(traj, 0, last_frame, &positions, &stride_length)
101        == TNG_SUCCESS)
102     {
103         /* Print the positions of the wanted particle (zero based) */
104         for(i=0; i < n_strides; i++)
105         {
106             printf("\nFrame %"PRId64":\n", first_frame + i*stride_length);
107             for(j=0; j < n_particles; j++)
108             {
109                 printf("Atom nr: %"PRId64"", j);
110                 printf("\t%f", positions[i*n_particles*3+j*3]);
111                 printf("\t%f", positions[i*n_particles*3+j*3+1]);
112                 printf("\t%f", positions[i*n_particles*3+j*3+2]);
113                 printf("\n");
114             }
115         }
116     }
117     else
118     {
119         printf("Cannot read positions\n");
120     }
121
122     /* Free memory */
123     if(positions)
124     {
125         free(positions);
126     }
127     if(box_shape)
128     {
129         free(box_shape);
130     }
131     tng_util_trajectory_close(&traj);
132
133     return(0);
134 }