summaryrefslogtreecommitdiff
path: root/src/tests/trg_io_testing.c
blob: caa74dd9103df69eea75d7671764fa93cca75966 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#include <inttypes.h>
#include <stdlib.h>
#include <string.h>
#include "trg_io.h"




static trg_function_status trg_setup_test_molecules(struct trg_trajectory *traj)
{
    struct trg_molecule *molecule;
    struct trg_chain *chain;
    struct trg_residue *residue;
    struct trg_atom *atom;
    uint64_t cnt;
//     int i;

    trg_add_molecule(traj, "water", &molecule);
    trg_add_chain_to_molecule(traj, molecule, "W", &chain);
    trg_add_residue_to_chain(traj, chain, "WAT", &residue);
    if(trg_add_atom_to_residue(traj, residue, "O", "O", &atom) == TRG_CRITICAL)
    {
        return(TRG_CRITICAL);
    }
    if(trg_add_atom_to_residue(traj, residue, "HO1", "H", &atom) == TRG_CRITICAL)
    {
        return(TRG_CRITICAL);
    }
    if(trg_add_atom_to_residue(traj, residue, "HO2", "H", &atom) == TRG_CRITICAL)
    {
        return(TRG_CRITICAL);
    }
    trg_set_molecule_cnt(traj, molecule, 200);
    trg_get_molecule_cnt(traj, molecule, &cnt);
    printf("Created %"PRId64" %s molecules.\n", cnt, molecule->name);
//     traj->molecule_cnt_list[traj->n_molecules-1] = 5;
//     trg_set_molecule_name(traj, &traj->molecules[1], "ligand");
//     trg_set_molecule_name(traj, &traj->molecules[2], "water");
//     trg_set_molecule_name(traj, &traj->molecules[3], "dummy");
//     traj->molecules[0].id = 0;
//     traj->molecules[1].id = 1;
//     traj->molecules[2].id = 2;
//     traj->molecules[3].id = 3;

//     if(trg_add_atom_to_molecule(traj, &traj->molecules[0], "atom1", "type1") == TRG_CRITICAL)
//     {
//         return(TRG_CRITICAL);
//     }
//     if(trg_add_atom_to_molecule(traj, &traj->molecules[0], "atom2", "type1") == TRG_CRITICAL)
//     {
//         return(TRG_CRITICAL);
//     }
//     if(trg_add_atom_to_molecule(traj, &traj->molecules[0], "atom3", "type1") == TRG_CRITICAL)
//     {
//         return(TRG_CRITICAL);
//     }
//     if(trg_add_atom_to_molecule(traj, &traj->molecules[0], "atom4", "type2") == TRG_CRITICAL)
//     {
//         return(TRG_CRITICAL);
//     }
//     if(trg_add_atom_to_molecule(traj, &traj->molecules[0], "atom5", "type2") == TRG_CRITICAL)
//     {
//         return(TRG_CRITICAL);
//     }
//     if(trg_add_atom_to_molecule(traj, &traj->molecules[0], "atom6", "type2") == TRG_CRITICAL)
//     {
//         return(TRG_CRITICAL);
//     }
//     if(trg_add_atom_to_molecule(traj, &traj->molecules[0], "atom7", "type3") == TRG_CRITICAL)
//     {
//         return(TRG_CRITICAL);
//     }
//     if(trg_add_atom_to_molecule(traj, &traj->molecules[1], "C1", "C") == TRG_CRITICAL)
//     {
//         return(TRG_CRITICAL);
//     }
//     if(trg_add_atom_to_molecule(traj, &traj->molecules[1], "O1", "O") == TRG_CRITICAL)
//     {
//         return(TRG_CRITICAL);
//     }
//     if(trg_add_atom_to_molecule(traj, &traj->molecules[1], "H11", "H") == TRG_CRITICAL)
//     {
//         return(TRG_CRITICAL);
//     }
//     if(trg_add_atom_to_molecule(traj, &traj->molecules[1], "H12", "H") == TRG_CRITICAL)
//     {
//         return(TRG_CRITICAL);
//     }
//     if(trg_add_atom_to_molecule(traj, &traj->molecules[1], "H13", "H") == TRG_CRITICAL)
//     {
//         return(TRG_CRITICAL);
//     }

    return(TRG_SUCCESS);
}

static trg_function_status trg_test_read_and_write_file
                (struct trg_trajectory *traj)
{
    trg_function_status stat;

    stat = trg_read_file_headers(traj, TRG_KEEP_FILE_OPEN);
    if(stat == TRG_CRITICAL)
    {
        return(stat);
    }
    stat = trg_write_file_headers(traj, TRG_KEEP_FILE_OPEN);
    if(stat == TRG_CRITICAL)
    {
        return(stat);
    }

    while(stat != TRG_CRITICAL && traj->input_file_pos < traj->input_file_len &&
          traj->current_trajectory_frame_set.next_frame_set_file_pos != -1UL)
    {
        stat = trg_read_next_frame_set(traj, TRG_KEEP_FILE_OPEN);
        if(stat == TRG_CRITICAL)
        {
            return(stat);
        }
        stat = trg_write_frame_set(traj, TRG_KEEP_FILE_OPEN);
    }
    
    return(stat);
}

static trg_function_status trg_test_write_and_read_traj(struct trg_trajectory *traj)
{
    int i, j, k, nr, tot_n_mols, cnt;
    float *data, *molpos;
    trg_function_status stat;
    
    /* Create molecules */
    if(trg_setup_test_molecules(traj) == TRG_CRITICAL)
    {
        return(TRG_CRITICAL);
    }

    if(trg_init_block(&traj->non_trajectory_blocks[traj->n_non_trajectory_blocks]) == TRG_CRITICAL)
    {
        return(TRG_CRITICAL);
    }
    traj->non_trajectory_blocks[traj->n_non_trajectory_blocks].id = TRG_MOLECULES;
    if(trg_set_block_name(traj,
                          &traj->non_trajectory_blocks[traj->n_non_trajectory_blocks++],
                          "MOLECULES") == TRG_CRITICAL)
    {
        return(TRG_CRITICAL);
    }

    if(trg_write_file_headers(traj, TRG_KEEP_FILE_OPEN) == TRG_CRITICAL)
    {
        return(TRG_CRITICAL);
    }
    
    data = malloc(sizeof(float) * traj->n_particles *
                  traj->frame_set_n_frames * 3);
    if(!data)
    {
        printf("Cannot allocate memory. %s: %d\n", __FILE__, __LINE__);
        return(TRG_CRITICAL);
    }

    tot_n_mols = 0;
    for(i = 0; i < traj->n_molecules; i++)
    {
        tot_n_mols += traj->molecule_cnt_list[i];
    }
    molpos = malloc(sizeof(float) * tot_n_mols * 3);

    /* Set initial coordinates */
    for(i = 0; i < tot_n_mols; i++)
    {
        nr = i * 3;
        /* Somewhat random coordinates (between 0 and 100),
        * but not specifying a random seed */
        molpos[nr] = 100.0 * rand() / (RAND_MAX + 1.0);
        molpos[nr+1] = 100.0 * rand() / (RAND_MAX + 1.0);
        molpos[nr+2] = 100.0 * rand() / (RAND_MAX + 1.0);
    }

    /* Generate 200 frame sets - each with 100 frames (by default) */
    for(i = 0; i < 200; i++)
    {
        cnt = 0;
        for(j = 0; j < traj->frame_set_n_frames; j++)
        {
            for(k = 0; k < tot_n_mols; k++)
            {
                nr = k * 3;
                /* Move -1 to 1 */
                molpos[nr] += 2 * (rand() / (RAND_MAX + 1.0)) - 1;
                molpos[nr+1] += 2 * (rand() / (RAND_MAX + 1.0)) - 1;
                molpos[nr+2] += 2 * (rand() / (RAND_MAX + 1.0)) - 1;

                data[cnt++] = molpos[nr];
                data[cnt++] = molpos[nr + 1];
                data[cnt++] = molpos[nr + 2];
                data[cnt++] = molpos[nr] + 1;
                data[cnt++] = molpos[nr + 1] + 1;
                data[cnt++] = molpos[nr + 2] + 1;
                data[cnt++] = molpos[nr] - 1;
                data[cnt++] = molpos[nr + 1] - 1;
                data[cnt++] = molpos[nr + 2] - 1;
            }
        }
        if(trg_new_frame_set(traj, i * traj->frame_set_n_frames,
            traj->frame_set_n_frames) != TRG_SUCCESS)
        {
            printf("Error creating frame set %d. %s: %d\n",
                   i, __FILE__, __LINE__);
            free(molpos);
            free(data);
            return(TRG_CRITICAL);
        }

        if(trg_add_particle_data_block(traj, TRG_TRAJ_POSITIONS,
                                       "POSITIONS",
                                       TRG_FLOAT_DATA,
                                       traj->frame_set_n_frames, 3,
                                       1, 0, traj->n_particles,
                                       TRG_UNCOMPRESSED,
                                       data) != TRG_SUCCESS)
        {
            printf("Error adding data. %s: %d\n", __FILE__, __LINE__);
            free(molpos);
            free(data);
            return(TRG_CRITICAL);
        }
        if(trg_write_frame_set(traj, TRG_KEEP_FILE_OPEN) != TRG_SUCCESS)
        {
            printf("Error writing frame set. %s: %d\n", __FILE__, __LINE__);
            free(molpos);
            free(data);
            return(TRG_CRITICAL);
        }
    }

//     trg_add_ids_names_pair(traj, TRG_TRAJ_VELOCITIES, "BOX SHAPE");
//     trg_add_ids_names_pair(traj, TRG_TRAJ_POSITIONS, "TRAJECTORY POSITIONS");
//     trg_add_ids_names_pair(traj, TRG_TRAJ_VELOCITIES, "TRAJECTORY VELOCITIES");
//     trg_add_ids_names_pair(traj, TRG_TRAJ_FORCES, "TRAJECTORY FORCES");
//     trg_add_ids_names_pair(traj, 11000, "TEST DATA");
        
    free(molpos);
    free(data);

    trg_destroy_trajectory(traj);
    trg_set_input_file(traj, "/tmp/trg_test.trg");
    
    stat = trg_read_file_headers(traj, TRG_KEEP_FILE_OPEN);
    
    while(stat != TRG_CRITICAL && traj->input_file_pos < traj->input_file_len &&
          traj->current_trajectory_frame_set.next_frame_set_file_pos != -1ULL)
    {
        stat = trg_read_next_frame_set(traj, TRG_KEEP_FILE_OPEN);
        if(stat == TRG_CRITICAL)
        {
            return(stat);
        }
    }

    return(stat);
}

int main()
{
    struct trg_trajectory traj;
    char time_str[TRG_MAX_DATE_STR_LEN];
    
    if(trg_init_trajectory(&traj) != TRG_SUCCESS)
    {
        trg_destroy_trajectory(&traj);
        printf("Test Init trajectory:\t\t\t\tFailed. %s: %d.\n",
               __FILE__, __LINE__);
        exit(1);
    }
    printf("Test Init trajectory:\t\t\t\tSucceeded.\n");

    trg_get_time_str(&traj, time_str);

    printf("Creation time: %s\n", time_str);
    
    trg_set_input_file(&traj, "trg_example.trg");
    trg_set_output_file(&traj, "/tmp/trg_example_test.trg");

//     if(trg_test_endianness(&traj) != TRG_SUCCESS)
//     {
//         printf("Test failed: Endianness. %s: %d\n", __FILE__, __LINE__);
//     }

    if(trg_test_read_and_write_file(&traj) == TRG_CRITICAL)
    {
        printf("Test Read and write file:\t\t\tFailed. %s: %d\n",
               __FILE__, __LINE__);
    }
    else
    {
        printf("Test Read and write file:\t\t\tSucceeded.\n");
    }
    
    if(trg_destroy_trajectory(&traj) == TRG_CRITICAL ||
       trg_init_trajectory(&traj) == TRG_CRITICAL)
    {
        printf("Test Destroy and init trajectory:\t\tFailed. %s: %d\n",
               __FILE__, __LINE__);
    }
    else
    {
        printf("Test Destroy and init trajectory:\t\tSucceeded.\n");
    }
    

    trg_set_output_file(&traj, "/tmp/trg_test.trg");
    
    if(trg_test_write_and_read_traj(&traj) == TRG_CRITICAL)
    {
        printf("Test Write and read file:\t\t\tFailed. %s: %d\n",
               __FILE__, __LINE__);
    }
    else
    {
        printf("Test Write and read file:\t\t\tSucceeded.\n");
    }
    
    if(trg_destroy_trajectory(&traj) == TRG_CRITICAL)
    {
        printf("Test Destroy trajectory:\t\t\tFailed. %s: %d.\n",
               __FILE__, __LINE__);
        exit(1);
    }
    else
    {
        printf("Test Destroy trajectory:\t\t\tSucceeded.\n");
    }
    

    printf("Tests finished\n");
    
    exit(0);
}
contact: Jan Huwald // Impressum