summaryrefslogtreecommitdiff
path: root/src/tests/tng_io_read_pos.c
blob: 4ab27da26bbd6f511ffcb8f02f0fe4725b61d4e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <stdlib.h>
#include <stdio.h>
#include <tng_io.h>

int main(int argc, char **argv)
{
    tng_trajectory_t traj;
    union data_values ***positions = 0; // A 3-dimensional array to be populated
    int64_t n_particles, n_values_per_frame, n_frames;
    tng_data_type data_type;
    int i, j;
    int64_t particle = 0;
    // Set a default frame range
    int first_frame = 0, last_frame = 50;

    if(argc <= 1)
    {
        printf("No file specified\n");
        printf("Usage:\n");
        printf("tng_io_read_pos <tng_file> [particle number = %"PRId64"] "
               "[first_frame = %d] [last_frame = %d]\n",
               particle, first_frame, last_frame);
        exit(1);
    }

    // A reference must be passed to allocate memory
    if(tng_trajectory_init(&traj) != TNG_SUCCESS)
    {
        tng_trajectory_destroy(&traj);
        exit(1);
    }
    tng_input_file_set(traj, argv[1]);

    // Read the file headers
    tng_file_headers_read(traj, TNG_USE_HASH);

    if(argc >= 3)
    {
        particle = strtoll(argv[2], 0, 10);
        if(argc >= 4)
        {
            first_frame = strtoll(argv[3], 0, 10);
            if(argc >= 5)
            {
                last_frame = strtoll(argv[4], 0, 10);
            }
        }
    }

    n_frames = last_frame - first_frame + 1;

    // Get the positions of all particles in the requested frame range.
    // The positions are stored in the positions array.
    // N.B. No proper error checks.
    if(tng_particle_data_interval_get(traj, TNG_TRAJ_POSITIONS, first_frame,
       last_frame, TNG_USE_HASH, &positions, &n_particles, &n_values_per_frame,
       &data_type) != TNG_SUCCESS)
    {
        printf("Cannot read positions\n");
    }
    else
    {
        // Print the positions of the wanted particle (zero based)
        for(i=first_frame; i<=last_frame; i++)
        {
            printf("%d", i);
            for(j=0; j<n_values_per_frame; j++)
            {
                switch(data_type)
                {
                case TNG_INT_DATA:
                    printf("\t%"PRId64"", positions[i][particle][j].i);
                    break;
                case TNG_FLOAT_DATA:
                    printf("\t%f", positions[i][particle][j].f);
                    break;
                case TNG_DOUBLE_DATA:
                    printf("\t%f", positions[i][particle][j].d);
                    break;
                default:
                    break;
                }
                printf("\n");
            }
        }
    }

    // Free memory
    if(positions)
    {
        tng_particle_data_values_free(positions, n_frames, n_particles,
                                      n_values_per_frame, data_type);
    }
    tng_trajectory_destroy(&traj);

    return(0);
}
contact: Jan Huwald // Impressum