8f932059228afd13026e1f6b014baa57183ccb36
[alexxy/gromacs.git] / src / external / tng_io / src / tests / tng_io_testing.c
1 /* This code is part of the tng binary trajectory format.
2  *
3  * Written by Magnus Lundborg
4  * Copyright (c) 2012-2014, 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 <string.h>
20 #include <math.h>
21 #include "tng/version.h"
22
23 #define BOX_SHAPE_X 150.0
24 #define BOX_SHAPE_Y 145.5
25 #define BOX_SHAPE_Z 155.5
26 #define N_FRAME_SETS 100
27 #define MEDIUM_STRIDE_LEN 5
28 #define LONG_STRIDE_LEN 25
29 #define TIME_PER_FRAME 2e-15
30 #define COMPRESSION_PRECISION 1000
31 #define USER_NAME "USER 1"
32 #define PROGRAM_NAME "tng_testing"
33 #define COMPUTER_NAME "Unknown computer"
34 #define FORCEFIELD_NAME "No forcefield"
35
36 static tng_function_status tng_test_setup_molecules(tng_trajectory_t traj)
37 {
38     tng_molecule_t molecule;
39     tng_chain_t chain;
40     tng_residue_t residue;
41     tng_atom_t atom;
42     tng_bond_t bond;
43     int64_t cnt;
44
45     tng_molecule_add(traj, "water", &molecule);
46     tng_molecule_chain_add(traj, molecule, "W", &chain);
47     tng_chain_residue_add(traj, chain, "WAT", &residue);
48     if(tng_residue_atom_add(traj, residue, "O", "O", &atom) == TNG_CRITICAL)
49     {
50         return(TNG_CRITICAL);
51     }
52     if(tng_residue_atom_add(traj, residue, "HO1", "H", &atom) == TNG_CRITICAL)
53     {
54         return(TNG_CRITICAL);
55     }
56     if(tng_residue_atom_add(traj, residue, "HO2", "H", &atom) == TNG_CRITICAL)
57     {
58         return(TNG_CRITICAL);
59     }
60     tng_molecule_bond_add(traj, molecule, 0, 1, &bond);
61     tng_molecule_bond_add(traj, molecule, 0, 2, &bond);
62     tng_molecule_cnt_set(traj, molecule, 200);
63     tng_molecule_cnt_get(traj, molecule, &cnt);
64
65     if(cnt != 200)
66     {
67         return(TNG_CRITICAL);
68     }
69 /*     printf("Created %"PRId64" %s molecules.\n", cnt, molecule->name); */
70
71 /*     traj->molecule_cnt_list[traj->n_molecules-1] = 5;
72 //     tng_molecule_name_set(traj, &traj->molecules[1], "ligand");
73 //     tng_molecule_name_set(traj, &traj->molecules[2], "water");
74 //     tng_molecule_name_set(traj, &traj->molecules[3], "dummy");
75 //     traj->molecules[0].id = 0;
76 //     traj->molecules[1].id = 1;
77 //     traj->molecules[2].id = 2;
78 //     traj->molecules[3].id = 3;
79
80 //     if(tng_add_atom_to_molecule(traj, &traj->molecules[0], "atom1", "type1") == TNG_CRITICAL)
81 //     {
82 //         return(TNG_CRITICAL);
83 //     }
84 //     if(tng_add_atom_to_molecule(traj, &traj->molecules[0], "atom2", "type1") == TNG_CRITICAL)
85 //     {
86 //         return(TNG_CRITICAL);
87 //     }
88 //     if(tng_add_atom_to_molecule(traj, &traj->molecules[0], "atom3", "type1") == TNG_CRITICAL)
89 //     {
90 //         return(TNG_CRITICAL);
91 //     }
92 //     if(tng_add_atom_to_molecule(traj, &traj->molecules[0], "atom4", "type2") == TNG_CRITICAL)
93 //     {
94 //         return(TNG_CRITICAL);
95 //     }
96 //     if(tng_add_atom_to_molecule(traj, &traj->molecules[0], "atom5", "type2") == TNG_CRITICAL)
97 //     {
98 //         return(TNG_CRITICAL);
99 //     }
100 //     if(tng_add_atom_to_molecule(traj, &traj->molecules[0], "atom6", "type2") == TNG_CRITICAL)
101 //     {
102 //         return(TNG_CRITICAL);
103 //     }
104 //     if(tng_add_atom_to_molecule(traj, &traj->molecules[0], "atom7", "type3") == TNG_CRITICAL)
105 //     {
106 //         return(TNG_CRITICAL);
107 //     }
108 //     if(tng_add_atom_to_molecule(traj, &traj->molecules[1], "C1", "C") == TNG_CRITICAL)
109 //     {
110 //         return(TNG_CRITICAL);
111 //     }
112 //     if(tng_add_atom_to_molecule(traj, &traj->molecules[1], "O1", "O") == TNG_CRITICAL)
113 //     {
114 //         return(TNG_CRITICAL);
115 //     }
116 //     if(tng_add_atom_to_molecule(traj, &traj->molecules[1], "H11", "H") == TNG_CRITICAL)
117 //     {
118 //         return(TNG_CRITICAL);
119 //     }
120 //     if(tng_add_atom_to_molecule(traj, &traj->molecules[1], "H12", "H") == TNG_CRITICAL)
121 //     {
122 //         return(TNG_CRITICAL);
123 //     }
124 //     if(tng_add_atom_to_molecule(traj, &traj->molecules[1], "H13", "H") == TNG_CRITICAL)
125 //     {
126 //         return(TNG_CRITICAL);
127 //     }
128 */
129     return(TNG_SUCCESS);
130 }
131
132 static tng_function_status tng_test_molecules(tng_trajectory_t traj)
133 {
134     tng_molecule_t molecule, molecule_new;
135     tng_chain_t chain;
136     tng_residue_t residue;
137     tng_atom_t atom;
138     int64_t cnt, *bonds_to, *bonds_from;
139     char var_atoms, str[TNG_MAX_STR_LEN];
140     tng_function_status stat;
141
142     stat = tng_num_molecule_types_get(traj, &cnt);
143     if(stat != TNG_SUCCESS || cnt != 1)
144     {
145         printf("Molecule reading error. %s: %d\n",
146                __FILE__, __LINE__);
147         return(TNG_FAILURE);
148     }
149
150     stat = tng_num_molecules_get(traj, &cnt);
151     if(stat != TNG_SUCCESS || cnt != 200)
152     {
153         printf("Molecule reading error. %s: %d\n",
154                __FILE__, __LINE__);
155         return(TNG_FAILURE);
156     }
157
158     stat = tng_num_particles_variable_get(traj, &var_atoms);
159     if(stat != TNG_SUCCESS || var_atoms)
160     {
161         printf("Molecule reading error. %s: %d\n",
162                __FILE__, __LINE__);
163         return(TNG_FAILURE);
164     }
165
166     stat = tng_molecule_of_index_get(traj, 0, &molecule);
167     if(stat != TNG_SUCCESS)
168     {
169         printf("Cannot find molecule. %s: %d\n",
170                __FILE__, __LINE__);
171         return(stat);
172     }
173     stat = tng_molecule_find(traj, "water", -1, &molecule);
174     if(stat != TNG_SUCCESS)
175     {
176         printf("Cannot find molecule. %s: %d\n",
177                __FILE__, __LINE__);
178         return(stat);
179     }
180     stat =tng_molecule_name_get(traj, molecule, str, TNG_MAX_STR_LEN);
181     if(stat != TNG_SUCCESS)
182     {
183         printf("Cannot get molecule name. %s: %d\n",
184                __FILE__, __LINE__);
185         return(stat);
186     }
187     stat = tng_molecule_num_chains_get(traj, molecule, &cnt);
188     if(stat != TNG_SUCCESS || cnt != 1)
189     {
190         printf("Cannot get number of chains in molecule. %s: %d\n",
191                __FILE__, __LINE__);
192         return(TNG_FAILURE);
193     }
194     stat = tng_molecule_chain_of_index_get(traj, molecule, 0, &chain);
195     if(stat != TNG_SUCCESS)
196     {
197         printf("Cannot get chain in molecule. %s: %d\n",
198                __FILE__, __LINE__);
199         return(stat);
200     }
201     stat = tng_molecule_chain_find(traj, molecule, "W", -1, &chain);
202     if(stat != TNG_SUCCESS)
203     {
204         printf("Cannot get chain in molecule. %s: %d\n",
205                __FILE__, __LINE__);
206         return(stat);
207     }
208     stat = tng_molecule_num_residues_get(traj, molecule, &cnt);
209     if(stat != TNG_SUCCESS || cnt != 1)
210     {
211         printf("Cannot get number of residues in molecule. %s: %d\n",
212                __FILE__, __LINE__);
213         return(TNG_FAILURE);
214     }
215     stat = tng_molecule_residue_of_index_get(traj, molecule, 0, &residue);
216     if(stat != TNG_SUCCESS)
217     {
218         printf("Cannot get residue in molecule. %s: %d\n",
219                __FILE__, __LINE__);
220         return(stat);
221     }
222     stat = tng_molecule_num_atoms_get(traj, molecule, &cnt);
223     if(stat != TNG_SUCCESS || cnt != 3)
224     {
225         printf("Cannot get number of atoms in molecule. %s: %d\n",
226                __FILE__, __LINE__);
227         return(TNG_FAILURE);
228     }
229     stat = tng_molecule_atom_of_index_get(traj, molecule, 0, &atom);
230     if(stat != TNG_SUCCESS)
231     {
232         printf("Cannot get atom in molecule. %s: %d\n",
233                __FILE__, __LINE__);
234         return(stat);
235     }
236     stat = tng_molecule_atom_find(traj, molecule, "O", -1, &atom);
237     if(stat != TNG_SUCCESS)
238     {
239         printf("Cannot get atom in molecule. %s: %d\n",
240                __FILE__, __LINE__);
241         return(stat);
242     }
243
244     stat =tng_chain_name_get(traj, chain, str, TNG_MAX_STR_LEN);
245     if(stat != TNG_SUCCESS)
246     {
247         printf("Cannot get name of chain. %s: %d\n",
248                __FILE__, __LINE__);
249         return(stat);
250     }
251     stat = tng_chain_num_residues_get(traj, chain, &cnt);
252     if(stat != TNG_SUCCESS || cnt != 1)
253     {
254         printf("Cannot get number of residues in chain. %s: %d\n",
255                __FILE__, __LINE__);
256         return(TNG_FAILURE);
257     }
258     stat = tng_chain_residue_of_index_get(traj, chain, 0, &residue);
259     if(stat != TNG_SUCCESS)
260     {
261         printf("Cannot get residue in chain. %s: %d\n",
262                __FILE__, __LINE__);
263         return(stat);
264     }
265     stat = tng_chain_residue_find(traj, chain, "WAT", -1, &residue);
266     if(stat != TNG_SUCCESS)
267     {
268         printf("Cannot get residue in chain. %s: %d\n",
269                __FILE__, __LINE__);
270         return(stat);
271     }
272
273     stat = tng_residue_name_get(traj, residue, str, TNG_MAX_STR_LEN);
274     if(stat != TNG_SUCCESS)
275     {
276         printf("Cannot get name of residue. %s: %d\n",
277                __FILE__, __LINE__);
278         return(stat);
279     }
280     stat = tng_residue_num_atoms_get(traj, residue, &cnt);
281     if(stat != TNG_SUCCESS || cnt != 3)
282     {
283         printf("Cannot get number of atoms in residue. %s: %d\n",
284                __FILE__, __LINE__);
285         return(TNG_FAILURE);
286     }
287     stat = tng_residue_atom_of_index_get(traj, residue, 0, &atom);
288     if(stat != TNG_SUCCESS)
289     {
290         printf("Cannot get residue of atom. %s: %d\n",
291                __FILE__, __LINE__);
292         return(stat);
293     }
294
295     stat = tng_atom_name_get(traj, atom, str, TNG_MAX_STR_LEN);
296     if(stat != TNG_SUCCESS)
297     {
298         printf("Cannot get name of atom. %s: %d\n",
299                __FILE__, __LINE__);
300         return(stat);
301     }
302     stat = tng_atom_type_get(traj, atom, str, TNG_MAX_STR_LEN);
303     if(stat != TNG_SUCCESS)
304     {
305         printf("Cannot get atom type of atom. %s: %d\n",
306                __FILE__, __LINE__);
307         return(stat);
308     }
309
310     stat = tng_molecule_id_of_particle_nr_get(traj, 0, &cnt);
311     if(stat != TNG_SUCCESS || cnt != 1)
312     {
313         printf("Cannot get molecule id of atom. %s: %d\n",
314                __FILE__, __LINE__);
315         return(TNG_FAILURE);
316     }
317     stat = tng_residue_id_of_particle_nr_get(traj, 0, &cnt);
318     if(stat != TNG_SUCCESS || cnt != 0)
319     {
320         printf("Cannot get residue id of atom. %s: %d\n",
321                __FILE__, __LINE__);
322         return(TNG_FAILURE);
323     }
324     stat = tng_global_residue_id_of_particle_nr_get(traj, 599, &cnt);
325     if(stat != TNG_SUCCESS || cnt != 199)
326     {
327         printf("Cannot get global residue id of atom. %s: %d\n",
328                __FILE__, __LINE__);
329         return(TNG_FAILURE);
330     }
331     stat = tng_molecule_name_of_particle_nr_get(traj, 0, str, TNG_MAX_STR_LEN);
332     if(stat != TNG_SUCCESS)
333     {
334         printf("Cannot get molecule name of atom. %s: %d\n",
335                __FILE__, __LINE__);
336         return(stat);
337     }
338     stat = tng_chain_name_of_particle_nr_get(traj, 0, str, TNG_MAX_STR_LEN);
339     if(stat != TNG_SUCCESS)
340     {
341         printf("Cannot get chain name of atom. %s: %d\n",
342                __FILE__, __LINE__);
343         return(stat);
344     }
345     stat = tng_residue_name_of_particle_nr_get(traj, 0, str, TNG_MAX_STR_LEN);
346     if(stat != TNG_SUCCESS)
347     {
348         printf("Cannot get residue name of atom. %s: %d\n",
349                __FILE__, __LINE__);
350         return(stat);
351     }
352     stat = tng_atom_name_of_particle_nr_get(traj, 0, str, TNG_MAX_STR_LEN);
353     if(stat != TNG_SUCCESS)
354     {
355         printf("Cannot get atom name of atom. %s: %d\n",
356                __FILE__, __LINE__);
357         return(stat);
358     }
359
360     stat = tng_molecule_alloc(traj, &molecule_new);
361     if(stat != TNG_SUCCESS)
362     {
363         printf("Cannot setup new molecule. %s: %d\n",
364                __FILE__, __LINE__);
365         return(stat);
366     }
367     stat = tng_molecule_name_set(traj, molecule_new, "TEST");
368     if(stat != TNG_SUCCESS)
369     {
370         printf("Cannot set name of new molecule. %s: %d\n",
371                __FILE__, __LINE__);
372         return(stat);
373     }
374     stat = tng_molecule_existing_add(traj, &molecule_new);
375     if(stat != TNG_SUCCESS)
376     {
377         printf("Cannot add new molecule to molecule system. %s: %d\n",
378                __FILE__, __LINE__);
379         return(stat);
380     }
381
382     stat = tng_molsystem_bonds_get(traj, &cnt, &bonds_from, &bonds_to);
383     if(stat != TNG_SUCCESS || cnt != 400)
384     {
385         printf("Cannot get bonds in molecule system. %s: %d\n",
386                __FILE__, __LINE__);
387         return(stat);
388     }
389
390     free(bonds_from);
391     free(bonds_to);
392
393     return(TNG_SUCCESS);
394 }
395
396 static tng_function_status tng_test_read_and_write_file
397                 (tng_trajectory_t traj, const char hash_mode)
398 {
399     char file_name[TNG_MAX_STR_LEN];
400     tng_function_status stat;
401
402     stat = tng_input_file_get(traj, file_name, TNG_MAX_STR_LEN);
403     if(stat != TNG_SUCCESS)
404     {
405         printf("Could not get name of input file. %s: %d\n",
406                __FILE__, __LINE__);
407         return(stat);
408     }
409     stat = tng_output_file_get(traj, file_name, TNG_MAX_STR_LEN);
410     if(stat != TNG_SUCCESS)
411     {
412         printf("Could not get name of output file. %s: %d\n",
413                __FILE__, __LINE__);
414         return(stat);
415     }
416
417     stat = tng_file_headers_read(traj, hash_mode);
418     if(stat != TNG_SUCCESS)
419     {
420         printf("Could not read headers. %s: %d\n",
421                __FILE__, __LINE__);
422         return(stat);
423     }
424     stat = tng_file_headers_write(traj, hash_mode);
425     if(stat != TNG_SUCCESS)
426     {
427         printf("Could not write headers. %s: %d\n",
428                __FILE__, __LINE__);
429         return(stat);
430     }
431
432     while(stat == TNG_SUCCESS)
433     {
434         stat = tng_frame_set_read_next(traj, hash_mode);
435         if(stat == TNG_CRITICAL)
436         {
437             printf("Could not read frame set. %s: %d\n",
438                    __FILE__, __LINE__);
439             return(stat);
440         }
441         if(stat == TNG_FAILURE)
442         {
443             return(TNG_SUCCESS);
444         }
445         stat = tng_frame_set_write(traj, hash_mode);
446     }
447
448     return(stat);
449 }
450
451 static tng_function_status tng_test_write_and_read_traj(tng_trajectory_t *traj,
452                                                         const char hash_mode)
453 {
454     int i, j, k, nr, cnt, dependency;
455     float *data, *molpos, *charges;
456     int64_t mapping[300], n_particles, n_frames_per_frame_set, tot_n_mols;
457     int64_t codec_id;
458     int64_t dist_exp = -9, temp_int, temp_int2;
459 //     int64_t frame_nr;
460     double box_shape[9], temp_double;
461     char atom_type[16], annotation[128];
462     char temp_str[TNG_MAX_STR_LEN];
463     tng_trajectory_frame_set_t frame_set;
464     tng_function_status stat = TNG_SUCCESS;
465
466     tng_medium_stride_length_set(*traj, MEDIUM_STRIDE_LEN);
467     tng_long_stride_length_set(*traj, LONG_STRIDE_LEN);
468
469     tng_first_user_name_set(*traj, USER_NAME);
470     tng_first_program_name_set(*traj, PROGRAM_NAME);
471     tng_first_computer_name_set(*traj, COMPUTER_NAME);
472     tng_forcefield_name_set(*traj, FORCEFIELD_NAME);
473
474     tng_compression_precision_set(*traj, COMPRESSION_PRECISION);
475
476     tng_distance_unit_exponential_set(*traj, dist_exp);
477
478     tng_time_per_frame_set(*traj, TIME_PER_FRAME);
479
480     /* Create molecules */
481     if(tng_test_setup_molecules(*traj) == TNG_CRITICAL)
482     {
483         return(TNG_CRITICAL);
484     }
485
486     /* Set the box shape */
487     box_shape[1] = box_shape[2] = box_shape[3] = box_shape[5] = box_shape[6] =
488     box_shape[7] = 0;
489     box_shape[0] = BOX_SHAPE_X;
490     box_shape[4] = BOX_SHAPE_Y;
491     box_shape[8] = BOX_SHAPE_Z;
492     if(tng_data_block_add(*traj, TNG_TRAJ_BOX_SHAPE, "BOX SHAPE", TNG_DOUBLE_DATA,
493                        TNG_NON_TRAJECTORY_BLOCK, 1, 9, 1, TNG_UNCOMPRESSED,
494                        box_shape) == TNG_CRITICAL)
495     {
496         tng_trajectory_destroy(traj);
497         printf("Cannot write trajectory box shape.\n");
498         exit(1);
499     }
500
501     /* Set partial charges (treat the water as TIP3P. */
502     tng_num_particles_get(*traj, &n_particles);
503     charges = malloc(sizeof(float) * n_particles);
504     for(i = 0; i < n_particles; i++)
505     {
506         stat = tng_atom_type_of_particle_nr_get(*traj, i, atom_type,
507                                                 sizeof(atom_type));
508         if(stat == TNG_CRITICAL)
509         {
510             break;
511         }
512         if(atom_type[0] == 'O')
513         {
514             charges[i] = -0.834;
515         }
516         else if(atom_type[0] == 'H')
517         {
518             charges[i] = 0.417;
519         }
520     }
521     if(stat == TNG_CRITICAL)
522     {
523         free(charges);
524         printf("Failed setting partial charges. %s: %d\n",
525                __FILE__, __LINE__);
526         return(TNG_CRITICAL);
527     }
528
529     stat = tng_particle_data_block_add(*traj, TNG_TRAJ_PARTIAL_CHARGES, "PARTIAL CHARGES",
530                                        TNG_FLOAT_DATA, TNG_NON_TRAJECTORY_BLOCK,
531                                        1, 1, 1, 0, n_particles,
532                                        TNG_UNCOMPRESSED, charges);
533     free(charges);
534     if(stat != TNG_SUCCESS)
535     {
536         printf("Failed adding partial charges. %s: %d\n",
537                __FILE__, __LINE__);
538         return(TNG_CRITICAL);
539     }
540
541
542     /* Generate a custom annotation data block */
543     strcpy(annotation, "This trajectory was generated from tng_io_testing. "
544                        "It is not a real MD trajectory.");
545     if(tng_data_block_add(*traj, TNG_TRAJ_GENERAL_COMMENTS, "COMMENTS", TNG_CHAR_DATA,
546                           TNG_NON_TRAJECTORY_BLOCK, 1, 1, 1, TNG_UNCOMPRESSED,
547                           annotation) != TNG_SUCCESS)
548     {
549         printf("Failed adding details annotation data block. %s: %d\n",
550                __FILE__, __LINE__);
551         return(TNG_CRITICAL);
552     }
553
554     /* Write file headers (includes non trajectory data blocks */
555     if(tng_file_headers_write(*traj, hash_mode) == TNG_CRITICAL)
556     {
557         printf("Cannot write file headers. %s: %d\n",
558                __FILE__, __LINE__);
559     }
560
561
562     tng_num_frames_per_frame_set_get(*traj, &n_frames_per_frame_set);
563
564     data = malloc(sizeof(float) * n_particles *
565                   n_frames_per_frame_set * 3);
566     if(!data)
567     {
568         printf("Cannot allocate memory. %s: %d\n", __FILE__, __LINE__);
569         return(TNG_CRITICAL);
570     }
571
572     tng_num_molecules_get(*traj, &tot_n_mols);
573     molpos = malloc(sizeof(float) * tot_n_mols * 3);
574
575     /* Set initial coordinates */
576     for(i = 0; i < tot_n_mols; i++)
577     {
578         nr = i * 3;
579         /* Somewhat random coordinates (between 0 and 100),
580          * but not specifying a random seed */
581         molpos[nr] = 100.0 * rand() / (RAND_MAX + 1.0);
582         molpos[nr+1] = 100.0 * rand() / (RAND_MAX + 1.0);
583         molpos[nr+2] = 100.0 * rand() / (RAND_MAX + 1.0);
584     }
585
586     /* Generate frame sets - each with 100 frames (by default) */
587     for(i = 0; i < N_FRAME_SETS; i++)
588     {
589         cnt = 0;
590         if(i < N_FRAME_SETS/2)
591         {
592             codec_id = TNG_GZIP_COMPRESSION;
593         }
594         else
595         {
596             codec_id = TNG_TNG_COMPRESSION;
597         }
598         for(j = 0; j < n_frames_per_frame_set; j++)
599         {
600             for(k = 0; k < tot_n_mols; k++)
601             {
602                 nr = k * 3;
603                 /* Move -1 to 1 */
604                 molpos[nr] += 2 * (rand() / (RAND_MAX + 1.0)) - 1;
605                 molpos[nr+1] += 2 * (rand() / (RAND_MAX + 1.0)) - 1;
606                 molpos[nr+2] += 2 * (rand() / (RAND_MAX + 1.0)) - 1;
607
608                 data[cnt++] = molpos[nr];
609                 data[cnt++] = molpos[nr + 1];
610                 data[cnt++] = molpos[nr + 2];
611                 data[cnt++] = molpos[nr] + 1;
612                 data[cnt++] = molpos[nr + 1] + 1;
613                 data[cnt++] = molpos[nr + 2] + 1;
614                 data[cnt++] = molpos[nr] - 1;
615                 data[cnt++] = molpos[nr + 1] - 1;
616                 data[cnt++] = molpos[nr + 2] - 1;
617             }
618         }
619         if(tng_frame_set_with_time_new(*traj, i * n_frames_per_frame_set,
620                                        n_frames_per_frame_set, 2e-15 * (i*n_frames_per_frame_set)) != TNG_SUCCESS)
621         {
622             printf("Error creating frame set %d. %s: %d\n",
623                    i, __FILE__, __LINE__);
624             free(molpos);
625             free(data);
626             return(TNG_CRITICAL);
627         }
628
629         tng_frame_set_particle_mapping_free(*traj);
630
631         /* Setup particle mapping. Use 4 different mapping blocks with arbitrary
632          * mappings. */
633         for(k=0; k<150; k++)
634         {
635             mapping[k]=k;
636         }
637         if(tng_particle_mapping_add(*traj, 0, 150, mapping) != TNG_SUCCESS)
638         {
639             printf("Error creating particle mapping. %s: %d\n",
640                    __FILE__, __LINE__);
641             free(molpos);
642             free(data);
643             return(TNG_CRITICAL);
644         }
645         for(k=0; k<150; k++)
646         {
647             mapping[k]=599-k;
648         }
649         if(tng_particle_mapping_add(*traj, 150, 150, mapping) != TNG_SUCCESS)
650         {
651             printf("Error creating particle mapping. %s: %d\n",
652                    __FILE__, __LINE__);
653             free(molpos);
654             free(data);
655             return(TNG_CRITICAL);
656         }
657         for(k=0; k<150; k++)
658         {
659             mapping[k]=k+150;
660         }
661         if(tng_particle_mapping_add(*traj, 300, 150, mapping) != TNG_SUCCESS)
662         {
663             printf("Error creating particle mapping. %s: %d\n",
664                    __FILE__, __LINE__);
665             free(molpos);
666             free(data);
667             return(TNG_CRITICAL);
668         }
669         for(k=0; k<150; k++)
670         {
671             mapping[k]=449-k;
672         }
673         if(tng_particle_mapping_add(*traj, 450, 150, mapping) != TNG_SUCCESS)
674         {
675             printf("Error creating particle mapping. %s: %d\n",
676                    __FILE__, __LINE__);
677             free(molpos);
678             free(data);
679             return(TNG_CRITICAL);
680         }
681
682         /* Add the positions in a data block */
683         if(tng_particle_data_block_add(*traj, TNG_TRAJ_POSITIONS,
684                                        "POSITIONS",
685                                        TNG_FLOAT_DATA,
686                                        TNG_TRAJECTORY_BLOCK,
687                                        n_frames_per_frame_set, 3,
688                                        1, 0, n_particles,
689 /*                                        TNG_UNCOMPRESSED, */
690                                        codec_id,
691                                        data) != TNG_SUCCESS)
692         {
693             printf("Error adding data. %s: %d\n", __FILE__, __LINE__);
694             free(molpos);
695             free(data);
696             return(TNG_CRITICAL);
697         }
698         /* Write the frame set */
699         if(tng_frame_set_write(*traj, hash_mode) != TNG_SUCCESS)
700         {
701             printf("Error writing frame set. %s: %d\n", __FILE__, __LINE__);
702             free(molpos);
703             free(data);
704             return(TNG_CRITICAL);
705         }
706     }
707
708     free(molpos);
709     free(data);
710
711     tng_trajectory_destroy(traj);
712     tng_trajectory_init(traj);
713     tng_input_file_set(*traj, TNG_EXAMPLE_FILES_DIR "tng_test.tng");
714
715     stat = tng_file_headers_read(*traj, hash_mode);
716
717     tng_first_user_name_get(*traj, temp_str, TNG_MAX_STR_LEN);
718     if(strcmp(USER_NAME, temp_str) != 0)
719     {
720         printf("User name does not match when reading written file. %s: %d\n",
721                __FILE__, __LINE__);
722         return(TNG_FAILURE);
723     }
724
725     tng_first_program_name_get(*traj, temp_str, TNG_MAX_STR_LEN);
726     if(strcmp(PROGRAM_NAME, temp_str) != 0)
727     {
728         printf("Program name does not match when reading written file. %s: %d\n",
729                __FILE__, __LINE__);
730         return(TNG_FAILURE);
731     }
732
733     tng_first_computer_name_get(*traj, temp_str, TNG_MAX_STR_LEN);
734     if(strcmp(COMPUTER_NAME, temp_str) != 0)
735     {
736         printf("Computer name does not match when reading written file. %s: %d\n",
737                __FILE__, __LINE__);
738         return(TNG_FAILURE);
739     }
740
741     tng_forcefield_name_get(*traj, temp_str, TNG_MAX_STR_LEN);
742     if(strcmp(FORCEFIELD_NAME, temp_str) != 0)
743     {
744         printf("Forcefield name does not match when reading written file. %s: %d\n",
745                __FILE__, __LINE__);
746         return(TNG_FAILURE);
747     }
748
749     tng_medium_stride_length_get(*traj, &temp_int);
750     if(temp_int != MEDIUM_STRIDE_LEN)
751     {
752         printf("Stride length does not match when reading written file. %s: %d\n",
753                __FILE__, __LINE__);
754         return(TNG_FAILURE);
755     }
756
757     tng_long_stride_length_get(*traj, &temp_int);
758     if(temp_int != LONG_STRIDE_LEN)
759     {
760         printf("Stride length does not match when reading written file. %s: %d\n",
761                __FILE__, __LINE__);
762         return(TNG_FAILURE);
763     }
764
765     tng_compression_precision_get(*traj, &temp_double);
766     if(temp_double != COMPRESSION_PRECISION)
767     {
768         printf("Compression precision does not match when reading written file. %s: %d\n",
769                __FILE__, __LINE__);
770         return(TNG_FAILURE);
771     }
772
773     tng_distance_unit_exponential_get(*traj, &temp_int);
774     if(temp_int != dist_exp)
775     {
776         printf("Distance unit exponential does not match when reading written file. %s: %d\n",
777                __FILE__, __LINE__);
778         return(TNG_FAILURE);
779     }
780
781     stat = tng_test_molecules(*traj);
782     if(stat != TNG_SUCCESS)
783     {
784         return(stat);
785     }
786
787     i = 0;
788     while(stat == TNG_SUCCESS)
789     {
790         stat = tng_frame_set_read_next(*traj, hash_mode);
791         tng_current_frame_set_get(*traj, &frame_set);
792         tng_frame_set_prev_frame_set_file_pos_get(*traj, frame_set, &temp_int);
793         tng_frame_set_next_frame_set_file_pos_get(*traj, frame_set, &temp_int2);
794         if(i > 0)
795         {
796             if(temp_int == -1)
797             {
798                 printf("File position of previous frame set not correct. %s: %d\n",
799                        __FILE__, __LINE__);
800                 return(TNG_FAILURE);
801             }
802         }
803         else if(temp_int != -1)
804         {
805             printf("File position of previous frame set not correct. %s: %d\n",
806                     __FILE__, __LINE__);
807             return(TNG_FAILURE);
808         }
809         if(i < N_FRAME_SETS -1)
810         {
811             if(temp_int2 == -1)
812             {
813                 printf("File position of next frame set not correct. %s: %d\n",
814                        __FILE__, __LINE__);
815                 return(TNG_FAILURE);
816             }
817         }
818         else if(temp_int2 != -1)
819         {
820             printf("File position of previous next set not correct. %s: %d\n",
821                     __FILE__, __LINE__);
822             return(TNG_FAILURE);
823         }
824         i++;
825     }
826     if(stat == TNG_CRITICAL)
827     {
828         return(stat);
829     }
830
831     tng_time_per_frame_get(*traj, &temp_double);
832     if(fabs(TIME_PER_FRAME - temp_double) > 0.000001)
833     {
834         printf("Time per frame does not match when reading written file. %s: %d\n",
835                __FILE__, __LINE__);
836         printf("Value: %e, expected value: %e\n", temp_double, TIME_PER_FRAME);
837         return(TNG_FAILURE);
838     }
839
840     stat = tng_frame_set_nr_find(*traj, (int64_t)(0.30*N_FRAME_SETS));
841     if(stat != TNG_SUCCESS)
842     {
843         printf("Could not find frame set %"PRId64". %s: %d\n", (int64_t)0.30*N_FRAME_SETS,
844                __FILE__, __LINE__);
845         return(stat);
846     }
847
848     stat = tng_frame_set_nr_find(*traj, (int64_t)(0.75*N_FRAME_SETS));
849     if(stat != TNG_SUCCESS)
850     {
851         printf("Could not find frame set %"PRId64". %s: %d\n", (int64_t)0.75*N_FRAME_SETS,
852                __FILE__, __LINE__);
853         return(stat);
854     }
855
856     tng_current_frame_set_get(*traj, &frame_set);
857     tng_frame_set_frame_range_get(*traj, frame_set, &temp_int, &temp_int2);
858     if(temp_int !=  75 * n_frames_per_frame_set)
859     {
860         printf("Unexpected first frame in frame set. %s: %d\n",
861                __FILE__, __LINE__);
862         return(TNG_FAILURE);
863     }
864
865     stat = tng_frame_set_read_current_only_data_from_block_id(*traj, hash_mode, TNG_TRAJ_POSITIONS);
866     if(stat != TNG_SUCCESS)
867     {
868         printf("Cannot read positions in current frame set. %s: %d\n",
869                __FILE__, __LINE__);
870         return(TNG_FAILURE);
871     }
872     stat = tng_frame_set_read_next_only_data_from_block_id(*traj, hash_mode, TNG_TRAJ_POSITIONS);
873     if(stat != TNG_SUCCESS)
874     {
875         printf("Cannot read positions in next frame set. %s: %d\n",
876                __FILE__, __LINE__);
877         return(TNG_FAILURE);
878     }
879     stat = tng_data_block_name_get(*traj, TNG_TRAJ_POSITIONS, temp_str, TNG_MAX_STR_LEN);
880     if(stat != TNG_SUCCESS || strcmp("POSITIONS", temp_str) != 0)
881     {
882         printf("Cannot get name of data block or unexpected name. %s: %d\n",
883                __FILE__, __LINE__);
884         return(TNG_FAILURE);
885     }
886     stat = tng_data_block_name_get(*traj, TNG_TRAJ_FORCES, temp_str, TNG_MAX_STR_LEN);
887     if(stat != TNG_FAILURE)
888     {
889         printf("Trying to retrieve name of non-existent data block did not return failure. %s: %d\n",
890                __FILE__, __LINE__);
891         return(TNG_FAILURE);
892     }
893     stat = tng_data_block_dependency_get(*traj, TNG_TRAJ_POSITIONS, &dependency);
894     if(stat != TNG_SUCCESS || dependency != TNG_FRAME_DEPENDENT + TNG_PARTICLE_DEPENDENT)
895     {
896         printf("Cannot get dependency of data block or unexpected dependency. %s: %d\n",
897                __FILE__, __LINE__);
898         return(TNG_FAILURE);
899     }
900     stat = tng_data_block_num_values_per_frame_get(*traj, TNG_TRAJ_POSITIONS, &temp_int);
901     if(stat != TNG_SUCCESS || temp_int != 3)
902     {
903         printf("Cannot get number of values per frame of data block or unexpected value. %s: %d\n",
904                __FILE__, __LINE__);
905         return(TNG_FAILURE);
906     }
907     stat = tng_data_get_stride_length(*traj, TNG_TRAJ_POSITIONS, 100, &temp_int);
908     if(stat != TNG_SUCCESS || temp_int != 1)
909     {
910         printf("Cannot get stride length of data block or unexpected value. %s: %d\n",
911                __FILE__, __LINE__);
912         return(TNG_FAILURE);
913     }
914
915     return(TNG_SUCCESS);
916 }
917
918 /* This test relies on knowing that the box shape is stored as double */
919 tng_function_status tng_test_get_box_data(tng_trajectory_t traj)
920 {
921     int64_t n_frames, n_values_per_frame;
922     union data_values **values = 0;
923     char type;
924
925     if(tng_data_get(traj, TNG_TRAJ_BOX_SHAPE, &values, &n_frames,
926                     &n_values_per_frame, &type) != TNG_SUCCESS)
927     {
928         printf("Failed getting box shape. %s: %d\n", __FILE__, __LINE__);
929         return(TNG_CRITICAL);
930     }
931
932     /* The X dimension in the example file is 50 */
933     if(fabs(values[0][0].d - 50) > 0.000001)
934     {
935         printf("Unexpected value in box shape. %s: %d\n", __FILE__, __LINE__);
936         return(TNG_FAILURE);
937     }
938
939     tng_data_values_free(traj, values, n_frames, n_values_per_frame, type);
940
941     return(TNG_SUCCESS);
942 }
943
944 /* This test relies on knowing that the positions are stored as float
945  * and that the data is not sparse (i.e. as many frames in the data
946  * as in the frame set */
947 tng_function_status tng_test_get_positions_data(tng_trajectory_t traj,
948                                                 const char hash_mode)
949 {
950     int64_t i, j, k, n_frames, n_particles, n_values_per_frame;
951     union data_values ***values = 0;
952     char type;
953
954     if(tng_particle_data_get(traj, TNG_TRAJ_POSITIONS, &values, &n_frames,
955                              &n_particles, &n_values_per_frame, &type) !=
956        TNG_SUCCESS)
957     {
958         printf("Failed getting particle positions. %s: %d\n", __FILE__, __LINE__);
959         return(TNG_CRITICAL);
960     }
961
962     if(n_values_per_frame != 3)
963     {
964         printf("Number of values per frame does not match expected value. %s: %d\n",
965                __FILE__, __LINE__);
966         tng_particle_data_values_free(traj, values, n_frames, n_particles,
967                                       n_values_per_frame, type);
968         return(TNG_FAILURE);
969     }
970
971     for(i = 0; i < n_frames; i++)
972     {
973 //         printf("%"PRId64"\n", i);
974         for(j = 0; j < n_particles; j++)
975         {
976             for(k = 0; k < n_values_per_frame; k++)
977             {
978 //                 printf("%f ", values[i][j][k].f);
979                 if(values[i][j][k].f < -500 || values[i][j][k].f > 500)
980                 {
981                     printf("Coordinates not in range. %s: %d\n",
982                            __FILE__, __LINE__);
983                     tng_particle_data_values_free(traj, values, n_frames, n_particles,
984                                                   n_values_per_frame, type);
985                     return(TNG_FAILURE);
986                 }
987             }
988 //             printf("\n");
989         }
990     }
991
992     if(tng_particle_data_interval_get(traj, TNG_TRAJ_POSITIONS, 111000, 111499,
993                                       hash_mode, &values, &n_particles,
994                                       &n_values_per_frame, &type) == TNG_SUCCESS)
995     {
996         printf("Getting particle positions succeeded when it should have failed. %s: %d\n",
997                __FILE__, __LINE__);
998         return(TNG_CRITICAL);
999     }
1000
1001     if(tng_particle_data_interval_get(traj, TNG_TRAJ_POSITIONS, 1000, 1050,
1002                                       hash_mode, &values, &n_particles,
1003                                       &n_values_per_frame, &type) != TNG_SUCCESS)
1004     {
1005         printf("Failed getting particle positions. %s: %d\n", __FILE__, __LINE__);
1006         return(TNG_CRITICAL);
1007     }
1008
1009     for(i = 0; i < 50; i++)
1010     {
1011 //         printf("%"PRId64"\n", i);
1012         for(j = 0; j < n_particles; j++)
1013         {
1014             for(k = 0; k < n_values_per_frame; k++)
1015             {
1016 //                 printf("%f ", values[i][j][k].f);
1017                 if(values[i][j][k].f < -500 || values[i][j][k].f > 500)
1018                 {
1019                     printf("Coordinates not in range. %s: %d\n",
1020                            __FILE__, __LINE__);
1021                     tng_particle_data_values_free(traj, values, n_frames, n_particles,
1022                                                   n_values_per_frame, type);
1023                     return(TNG_FAILURE);
1024                 }
1025             }
1026 //             printf("\n");
1027         }
1028     }
1029
1030     tng_particle_data_values_free(traj, values, n_frames, n_particles,
1031                                   n_values_per_frame, type);
1032
1033     return(TNG_SUCCESS);
1034 }
1035
1036 tng_function_status tng_test_utility_functions(tng_trajectory_t traj, const char hash_mode)
1037 {
1038     tng_function_status stat;
1039     int64_t n_particles, i, j, k, codec_id, n_frames, n_frames_per_frame_set;
1040     int64_t n_frames_to_read=30, stride_len, next_frame, n_blocks, *block_ids = 0;
1041     double time, multiplier;
1042     float *positions = 0;
1043
1044     stat = tng_util_trajectory_open(TNG_EXAMPLE_FILES_DIR "tng_test.tng", 'r', &traj);
1045     if(stat != TNG_SUCCESS)
1046     {
1047         return(stat);
1048     }
1049
1050     stat = tng_util_time_of_frame_get(traj, 50, &time);
1051     if(stat != TNG_SUCCESS || fabs(time - 100e-13) > 0.000001)
1052     {
1053         printf("Unexpected time at frame 50. %s: %d\n", __FILE__, __LINE__);
1054         printf("Value: %e, expected value: %e\n", time, 100e-13);
1055         return(stat);
1056     }
1057     stat = tng_util_time_of_frame_get(traj, 100, &time);
1058     if(stat != TNG_SUCCESS || fabs(time - 200e-13) > 0.000001)
1059     {
1060         printf("Unexpected time at frame 100. %s: %d\n", __FILE__, __LINE__);
1061         printf("Value: %e, expected value: %e\n", time, 100e-13);
1062         return(stat);
1063     }
1064
1065     tng_num_frames_per_frame_set_get(traj, &n_frames_per_frame_set);
1066
1067     stat = tng_util_num_frames_with_data_of_block_id_get(traj, TNG_TRAJ_POSITIONS, &n_frames);
1068     if(stat != TNG_SUCCESS || n_frames != n_frames_per_frame_set * N_FRAME_SETS)
1069     {
1070         printf("Unexpected number of frames with positions data. %s: %d\n",
1071                __FILE__, __LINE__);
1072         printf("Value: %"PRId64", expected value: %"PRId64"\n", n_frames,
1073                n_frames_per_frame_set * N_FRAME_SETS);
1074         return(stat);
1075     }
1076
1077     tng_num_frames_per_frame_set_get(traj, &n_frames_per_frame_set);
1078
1079     stat = tng_util_num_frames_with_data_of_block_id_get(traj, TNG_TRAJ_POSITIONS, &n_frames);
1080     if(stat != TNG_SUCCESS || n_frames != n_frames_per_frame_set * N_FRAME_SETS)
1081     {
1082         return(stat);
1083     }
1084
1085     tng_num_particles_get(traj, &n_particles);
1086
1087     stat = tng_util_pos_read_range(traj, 1, n_frames_to_read, &positions, &stride_len);
1088     if(stat != TNG_SUCCESS)
1089     {
1090         if(positions)
1091         {
1092             free(positions);
1093         }
1094         return(stat);
1095     }
1096
1097     for(i = 0; i < n_frames_to_read / stride_len; i++)
1098     {
1099         for(j = 0; j < n_particles; j++)
1100         {
1101             for(k = 0; k < 3; k++)
1102             {
1103                 if(positions[i*n_particles + j*3 + k] < -500 || positions[i*n_particles + j*3 + k] > 500)
1104                 {
1105                     printf("Coordinates not in range. %s: %d\n",
1106                            __FILE__, __LINE__);
1107                     free(positions);
1108                     return(TNG_FAILURE);
1109                 }
1110             }
1111         }
1112     }
1113
1114     free(positions);
1115
1116     stat=tng_util_trajectory_next_frame_present_data_blocks_find(traj, n_frames_to_read,
1117                                                                  0, 0, &next_frame,
1118                                                                  &n_blocks, &block_ids);
1119     if(block_ids)
1120     {
1121         free(block_ids);
1122     }
1123     if(stat != TNG_SUCCESS || n_blocks != 1 || next_frame != n_frames_to_read + stride_len)
1124     {
1125         printf("Unexpected data blocks in next frame. %s: %d\n",
1126                __FILE__, __LINE__);
1127         return(TNG_FAILURE);
1128     }
1129
1130     stat = tng_util_frame_current_compression_get(traj, TNG_TRAJ_POSITIONS, &codec_id, &multiplier);
1131     if(stat != TNG_SUCCESS || codec_id != TNG_GZIP_COMPRESSION)
1132     {
1133         printf("Could not get compression. %s: %d\n",
1134                __FILE__, __LINE__);
1135         return(TNG_FAILURE);
1136     }
1137     stat = tng_util_trajectory_close(&traj);
1138     if(stat != TNG_SUCCESS)
1139     {
1140         return(stat);
1141     }
1142
1143     return(TNG_SUCCESS);
1144 }
1145
1146
1147 tng_function_status tng_test_append(tng_trajectory_t traj, const char hash_mode)
1148 {
1149     char str[TNG_MAX_STR_LEN];
1150     int64_t n_frames, n_particles, i;
1151     double time, *velocities;
1152     tng_function_status stat;
1153
1154     stat = tng_util_trajectory_open(TNG_EXAMPLE_FILES_DIR "tng_test.tng", 'a', &traj);
1155     if(stat != TNG_SUCCESS)
1156     {
1157         printf("Cannot open trajectory. %s: %d\n",
1158                __FILE__, __LINE__);
1159         return(stat);
1160     }
1161
1162     stat = tng_last_user_name_set(traj, USER_NAME);
1163     if(stat != TNG_SUCCESS)
1164     {
1165         printf("Cannot set last user name. %s: %d\n",
1166                __FILE__, __LINE__);
1167         return(stat);
1168     }
1169     stat = tng_last_user_name_get(traj, str, TNG_MAX_STR_LEN);
1170     if(stat != TNG_SUCCESS)
1171     {
1172         printf("Cannot get last user name. %s: %d\n",
1173                __FILE__, __LINE__);
1174         return(stat);
1175     }
1176     stat = tng_last_program_name_set(traj, PROGRAM_NAME);
1177     if(stat != TNG_SUCCESS)
1178     {
1179         printf("Cannot set last program name. %s: %d\n",
1180                __FILE__, __LINE__);
1181         return(stat);
1182     }
1183     stat = tng_last_program_name_get(traj, str, TNG_MAX_STR_LEN);
1184     if(stat != TNG_SUCCESS)
1185     {
1186         printf("Cannot get last program name. %s: %d\n",
1187                __FILE__, __LINE__);
1188         return(stat);
1189     }
1190     stat = tng_last_computer_name_set(traj, "Still " COMPUTER_NAME);
1191     if(stat != TNG_SUCCESS)
1192     {
1193         printf("Cannot set last computer name. %s: %d\n",
1194                __FILE__, __LINE__);
1195         return(stat);
1196     }
1197     stat = tng_last_computer_name_get(traj, str, TNG_MAX_STR_LEN);
1198     if(stat != TNG_SUCCESS)
1199     {
1200         printf("Cannot get last computer name. %s: %d\n",
1201                __FILE__, __LINE__);
1202         return(stat);
1203     }
1204
1205     stat = tng_file_headers_write(traj, hash_mode);
1206     if(stat != TNG_SUCCESS)
1207     {
1208         printf("Cannot write file headers. %s: %d\n",
1209                __FILE__, __LINE__);
1210         return(stat);
1211     }
1212
1213     tng_num_frames_get(traj, &n_frames);
1214     tng_frame_set_of_frame_find(traj, n_frames - 1);
1215     tng_util_time_of_frame_get(traj, n_frames - 1, &time);
1216     time += TIME_PER_FRAME;
1217     tng_num_particles_get(traj, &n_particles);
1218
1219     velocities = malloc(sizeof(double) * n_particles * 3);
1220     if(!velocities)
1221     {
1222         printf("Cannot allocate memory. %s: %d\n",
1223                __FILE__, __LINE__);
1224         return(TNG_CRITICAL);
1225     }
1226
1227     for(i = 0; i < n_particles * 3; i++)
1228     {
1229         velocities[i] = i;
1230     }
1231
1232     stat = tng_util_vel_with_time_double_write(traj, n_frames, time, velocities);
1233
1234     free(velocities);
1235
1236     stat = tng_util_trajectory_close(&traj);
1237
1238     return(stat);
1239 }
1240
1241 tng_function_status tng_test_copy_container(tng_trajectory_t traj, const char hash_mode)
1242 {
1243     tng_trajectory_t dest;
1244     tng_function_status stat;
1245
1246     stat = tng_util_trajectory_open(TNG_EXAMPLE_FILES_DIR "tng_test.tng", 'r', &traj);
1247     if(stat != TNG_SUCCESS)
1248     {
1249         printf("Cannot open trajectory. %s: %d\n",
1250                __FILE__, __LINE__);
1251         return(stat);
1252     }
1253
1254     stat = tng_trajectory_init_from_src(traj, &dest);
1255     if(stat != TNG_SUCCESS)
1256     {
1257         return(stat);
1258     }
1259
1260     stat = tng_molecule_system_copy(traj, dest);
1261     if(stat != TNG_SUCCESS)
1262     {
1263         return(stat);
1264     }
1265
1266     stat = tng_util_trajectory_close(&traj);
1267     if(stat != TNG_SUCCESS)
1268     {
1269         return(stat);
1270     }
1271     stat = tng_util_trajectory_close(&dest);
1272
1273     return(stat);
1274 }
1275
1276 int main()
1277 {
1278     tng_trajectory_t traj;
1279     char time_str[TNG_MAX_DATE_STR_LEN];
1280     char version_str[TNG_MAX_STR_LEN];
1281     char hash_mode = TNG_USE_HASH;
1282
1283     tng_version(traj, version_str, TNG_MAX_STR_LEN);
1284     printf("Test version control:\t\t\t\t");
1285     if(strncmp(TNG_VERSION, version_str, TNG_MAX_STR_LEN) == 0)
1286     {
1287         printf("Succeeded.\n");
1288     }
1289     else
1290     {
1291         printf("Failed.\n");
1292     }
1293
1294     printf("Test Init trajectory:\t\t\t\t");
1295     if(tng_trajectory_init(&traj) != TNG_SUCCESS)
1296     {
1297         tng_trajectory_destroy(&traj);
1298         printf("Failed. %s: %d.\n", __FILE__, __LINE__);
1299         exit(1);
1300     }
1301     printf("Succeeded.\n");
1302
1303     tng_time_get_str(traj, time_str);
1304
1305     printf("Creation time: %s\n", time_str);
1306
1307     tng_input_file_set(traj, TNG_EXAMPLE_FILES_DIR "tng_example.tng");
1308     tng_output_file_set(traj, TNG_EXAMPLE_FILES_DIR "tng_example_out.tng");
1309
1310     printf("Test Read and write file:\t\t\t");
1311     if(tng_test_read_and_write_file(traj, hash_mode) != TNG_SUCCESS)
1312     {
1313         printf("Failed. %s: %d\n", __FILE__, __LINE__);
1314     }
1315     else
1316     {
1317         printf("Succeeded.\n");
1318     }
1319
1320     printf("Test Get data:\t\t\t\t\t");
1321     if(tng_test_get_box_data(traj) != TNG_SUCCESS)
1322     {
1323         printf("Failed. %s: %d\n", __FILE__, __LINE__);
1324     }
1325     else
1326     {
1327         printf("Succeeded.\n");
1328     }
1329
1330     printf("Test Destroy and init trajectory:\t\t");
1331     if(tng_trajectory_destroy(&traj) != TNG_SUCCESS ||
1332        tng_trajectory_init(&traj) != TNG_SUCCESS)
1333     {
1334         printf("Failed. %s: %d\n", __FILE__, __LINE__);
1335     }
1336     else
1337     {
1338         printf("Succeeded.\n");
1339     }
1340
1341
1342     tng_output_file_set(traj, TNG_EXAMPLE_FILES_DIR "tng_test.tng");
1343
1344     printf("Test Write and read file:\t\t\t");
1345     if(tng_test_write_and_read_traj(&traj, hash_mode) != TNG_SUCCESS)
1346     {
1347         printf("Failed. %s: %d\n", __FILE__, __LINE__);
1348     }
1349     else
1350     {
1351         printf("Succeeded.\n");
1352     }
1353
1354     printf("Test Get particle data:\t\t\t\t");
1355     if(tng_test_get_positions_data(traj, hash_mode) != TNG_SUCCESS)
1356     {
1357         printf("Failed. %s: %d\n",
1358                __FILE__, __LINE__);
1359     }
1360     else
1361     {
1362         printf("Succeeded.\n");
1363     }
1364
1365     printf("Test Destroy trajectory:\t\t\t");
1366     if(tng_trajectory_destroy(&traj) != TNG_SUCCESS)
1367     {
1368         printf("Failed. %s: %d.\n", __FILE__, __LINE__);
1369         exit(1);
1370     }
1371     else
1372     {
1373         printf("Succeeded.\n");
1374     }
1375
1376     printf("Test Utility functions:\t\t\t\t");
1377     if(tng_test_utility_functions(traj, hash_mode) != TNG_SUCCESS)
1378     {
1379         printf("Failed. %s: %d.\n", __FILE__, __LINE__);
1380         exit(1);
1381     }
1382     else
1383     {
1384         printf("Succeeded.\n");
1385     }
1386
1387     printf("Test Append:\t\t\t\t\t");
1388     if(tng_test_append(traj, hash_mode) != TNG_SUCCESS)
1389     {
1390         printf("Failed. %s: %d.\n", __FILE__, __LINE__);
1391     }
1392     else
1393     {
1394         printf("Succeeded.\n");
1395     }
1396
1397     printf("Test Copy trajectory container:\t\t\t");
1398     if(tng_test_copy_container(traj, hash_mode) != TNG_SUCCESS)
1399     {
1400         printf("Failed. %s: %d.\n", __FILE__, __LINE__);
1401     }
1402     else
1403     {
1404         printf("Succeeded.\n");
1405     }
1406
1407     printf("Tests finished\n");
1408
1409     exit(0);
1410 }