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