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