TNG version 1.7.3
[alexxy/gromacs.git] / src / external / tng_io / src / tests / tng_io_read_pos.c
1 /* This code is part of the tng binary trajectory format.
2  *
3  * Written by Magnus Lundborg
4  * Copyright (c) 2012-2013, The GROMACS development team.
5  * check out http://www.gromacs.org for more information.
6  *
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the Revised BSD License.
10  */
11
12 #include "tng/tng_io.h"
13
14 #ifdef USE_STD_INTTYPES_H
15 #include <inttypes.h>
16 #endif
17
18 #include <stdlib.h>
19 #include <stdio.h>
20
21 int main(int argc, char **argv)
22 {
23     tng_trajectory_t traj;
24     union data_values ***positions = 0; /* A 3-dimensional array to be populated */
25     int64_t n_particles, n_values_per_frame, n_frames, tot_n_frames;
26     char data_type;
27     int i, j;
28     int particle = 0;
29     /* Set a default frame range */
30     int first_frame = 0, last_frame = 50;
31     char atom_name[64], res_name[64], chain_name[64];
32
33     if(argc <= 1)
34     {
35         printf("No file specified\n");
36         printf("Usage:\n");
37         printf("tng_io_read_pos <tng_file> [particle number = %d] "
38                "[first_frame = %d] [last_frame = %d]\n",
39                particle, first_frame, last_frame);
40         exit(1);
41     }
42
43     /* A reference must be passed to allocate memory */
44     if(tng_trajectory_init(&traj) != TNG_SUCCESS)
45     {
46         tng_trajectory_destroy(&traj);
47         exit(1);
48     }
49     tng_input_file_set(traj, argv[1]);
50
51     /* Read the file headers */
52     tng_file_headers_read(traj, TNG_USE_HASH);
53
54     if(argc >= 3)
55     {
56         particle = strtol(argv[2], 0, 10);
57         if(argc >= 4)
58         {
59             first_frame = strtol(argv[3], 0, 10);
60             if(argc >= 5)
61             {
62                 last_frame = strtol(argv[4], 0, 10);
63             }
64         }
65     }
66
67     if(tng_num_frames_get(traj, &tot_n_frames) != TNG_SUCCESS)
68     {
69         printf("Cannot determine the number of frames in the file\n");
70         tng_trajectory_destroy(&traj);
71         exit(1);
72     }
73
74     printf("%"PRId64" frames in file\n", tot_n_frames);
75
76     if(last_frame > tot_n_frames - 1)
77     {
78         last_frame = tot_n_frames - 1;
79     }
80
81     n_frames = last_frame - first_frame + 1;
82
83     if(tng_atom_name_of_particle_nr_get(traj, particle, atom_name,
84                                         sizeof(atom_name)) ==
85        TNG_SUCCESS &&
86        tng_residue_name_of_particle_nr_get(traj, particle, res_name,
87                                            sizeof(res_name)) ==
88        TNG_SUCCESS &&
89        tng_chain_name_of_particle_nr_get(traj, particle, chain_name,
90                                          sizeof(chain_name)) ==
91        TNG_SUCCESS)
92     {
93         printf("Particle: %s (%s: %s)\n", atom_name, chain_name, res_name);
94     }
95     else
96     {
97         printf("Particle name not found\n");
98     }
99
100     /* Get the positions of all particles in the requested frame range.
101        The positions are stored in the positions array.
102        N.B. No proper error checks. */
103     if(tng_particle_data_interval_get(traj, TNG_TRAJ_POSITIONS, first_frame,
104        last_frame, TNG_USE_HASH, &positions, &n_particles, &n_values_per_frame,
105        &data_type) == TNG_SUCCESS)
106     {
107         if(particle >= n_particles)
108         {
109             printf("Chosen particle out of range. Only %"PRId64" particles in trajectory.\n", n_particles);
110         }
111         else
112         {
113             /* Print the positions of the wanted particle (zero based) */
114             for(i=0; i<n_frames; i++)
115             {
116                 printf("%d", first_frame + i);
117                 for(j=0; j<n_values_per_frame; j++)
118                 {
119                     switch(data_type)
120                     {
121                     case TNG_INT_DATA:
122                         printf("\t%"PRId64"", positions[i][particle][j].i);
123                         break;
124                     case TNG_FLOAT_DATA:
125                         printf("\t%f", positions[i][particle][j].f);
126                         break;
127                     case TNG_DOUBLE_DATA:
128                         printf("\t%f", positions[i][particle][j].d);
129                         break;
130                     default:
131                         break;
132                     }
133                     printf("\n");
134                 }
135             }
136         }
137     }
138     else
139     {
140         printf("Cannot read positions\n");
141     }
142
143     /* Free memory */
144     if(positions)
145     {
146         tng_particle_data_values_free(traj, positions, n_frames, n_particles,
147                                       n_values_per_frame, data_type);
148     }
149     tng_trajectory_destroy(&traj);
150
151     return(0);
152 }