1 /* This code is part of the tng binary trajectory format.
3 * Written by Magnus Lundborg
4 * Copyright (c) 2012-2014, The GROMACS development team.
5 * Check out http://www.gromacs.org for more information.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the Revised BSD License.
12 #include "tng/tng_io.h"
14 #ifdef USE_STD_INTTYPES_H
21 #include "tng/version.h"
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"
36 static tng_function_status tng_test_setup_molecules(tng_trajectory_t traj)
38 tng_molecule_t molecule;
40 tng_residue_t residue;
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)
52 if(tng_residue_atom_add(traj, residue, "HO1", "H", &atom) == TNG_CRITICAL)
56 if(tng_residue_atom_add(traj, residue, "HO2", "H", &atom) == TNG_CRITICAL)
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);
69 /* printf("Created %"PRId64" %s molecules.\n", cnt, molecule->name); */
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;
80 // if(tng_add_atom_to_molecule(traj, &traj->molecules[0], "atom1", "type1") == TNG_CRITICAL)
82 // return(TNG_CRITICAL);
84 // if(tng_add_atom_to_molecule(traj, &traj->molecules[0], "atom2", "type1") == TNG_CRITICAL)
86 // return(TNG_CRITICAL);
88 // if(tng_add_atom_to_molecule(traj, &traj->molecules[0], "atom3", "type1") == TNG_CRITICAL)
90 // return(TNG_CRITICAL);
92 // if(tng_add_atom_to_molecule(traj, &traj->molecules[0], "atom4", "type2") == TNG_CRITICAL)
94 // return(TNG_CRITICAL);
96 // if(tng_add_atom_to_molecule(traj, &traj->molecules[0], "atom5", "type2") == TNG_CRITICAL)
98 // return(TNG_CRITICAL);
100 // if(tng_add_atom_to_molecule(traj, &traj->molecules[0], "atom6", "type2") == TNG_CRITICAL)
102 // return(TNG_CRITICAL);
104 // if(tng_add_atom_to_molecule(traj, &traj->molecules[0], "atom7", "type3") == TNG_CRITICAL)
106 // return(TNG_CRITICAL);
108 // if(tng_add_atom_to_molecule(traj, &traj->molecules[1], "C1", "C") == TNG_CRITICAL)
110 // return(TNG_CRITICAL);
112 // if(tng_add_atom_to_molecule(traj, &traj->molecules[1], "O1", "O") == TNG_CRITICAL)
114 // return(TNG_CRITICAL);
116 // if(tng_add_atom_to_molecule(traj, &traj->molecules[1], "H11", "H") == TNG_CRITICAL)
118 // return(TNG_CRITICAL);
120 // if(tng_add_atom_to_molecule(traj, &traj->molecules[1], "H12", "H") == TNG_CRITICAL)
122 // return(TNG_CRITICAL);
124 // if(tng_add_atom_to_molecule(traj, &traj->molecules[1], "H13", "H") == TNG_CRITICAL)
126 // return(TNG_CRITICAL);
132 static tng_function_status tng_test_molecules(tng_trajectory_t traj)
134 tng_molecule_t molecule, molecule_new;
136 tng_residue_t residue;
138 int64_t cnt, *bonds_to, *bonds_from;
139 char var_atoms, str[TNG_MAX_STR_LEN];
140 tng_function_status stat;
142 stat = tng_num_molecule_types_get(traj, &cnt);
143 if(stat != TNG_SUCCESS || cnt != 1)
145 printf("Molecule reading error. %s: %d\n",
150 stat = tng_num_molecules_get(traj, &cnt);
151 if(stat != TNG_SUCCESS || cnt != 200)
153 printf("Molecule reading error. %s: %d\n",
158 stat = tng_num_particles_variable_get(traj, &var_atoms);
159 if(stat != TNG_SUCCESS || var_atoms)
161 printf("Molecule reading error. %s: %d\n",
166 stat = tng_molecule_of_index_get(traj, 0, &molecule);
167 if(stat != TNG_SUCCESS)
169 printf("Cannot find molecule. %s: %d\n",
173 stat = tng_molecule_find(traj, "water", -1, &molecule);
174 if(stat != TNG_SUCCESS)
176 printf("Cannot find molecule. %s: %d\n",
180 stat =tng_molecule_name_get(traj, molecule, str, TNG_MAX_STR_LEN);
181 if(stat != TNG_SUCCESS)
183 printf("Cannot get molecule name. %s: %d\n",
187 stat = tng_molecule_num_chains_get(traj, molecule, &cnt);
188 if(stat != TNG_SUCCESS || cnt != 1)
190 printf("Cannot get number of chains in molecule. %s: %d\n",
194 stat = tng_molecule_chain_of_index_get(traj, molecule, 0, &chain);
195 if(stat != TNG_SUCCESS)
197 printf("Cannot get chain in molecule. %s: %d\n",
201 stat = tng_molecule_chain_find(traj, molecule, "W", -1, &chain);
202 if(stat != TNG_SUCCESS)
204 printf("Cannot get chain in molecule. %s: %d\n",
208 stat = tng_molecule_num_residues_get(traj, molecule, &cnt);
209 if(stat != TNG_SUCCESS || cnt != 1)
211 printf("Cannot get number of residues in molecule. %s: %d\n",
215 stat = tng_molecule_residue_of_index_get(traj, molecule, 0, &residue);
216 if(stat != TNG_SUCCESS)
218 printf("Cannot get residue in molecule. %s: %d\n",
222 stat = tng_molecule_num_atoms_get(traj, molecule, &cnt);
223 if(stat != TNG_SUCCESS || cnt != 3)
225 printf("Cannot get number of atoms in molecule. %s: %d\n",
229 stat = tng_molecule_atom_of_index_get(traj, molecule, 0, &atom);
230 if(stat != TNG_SUCCESS)
232 printf("Cannot get atom in molecule. %s: %d\n",
236 stat = tng_molecule_atom_find(traj, molecule, "O", -1, &atom);
237 if(stat != TNG_SUCCESS)
239 printf("Cannot get atom in molecule. %s: %d\n",
244 stat =tng_chain_name_get(traj, chain, str, TNG_MAX_STR_LEN);
245 if(stat != TNG_SUCCESS)
247 printf("Cannot get name of chain. %s: %d\n",
251 stat = tng_chain_num_residues_get(traj, chain, &cnt);
252 if(stat != TNG_SUCCESS || cnt != 1)
254 printf("Cannot get number of residues in chain. %s: %d\n",
258 stat = tng_chain_residue_of_index_get(traj, chain, 0, &residue);
259 if(stat != TNG_SUCCESS)
261 printf("Cannot get residue in chain. %s: %d\n",
265 stat = tng_chain_residue_find(traj, chain, "WAT", -1, &residue);
266 if(stat != TNG_SUCCESS)
268 printf("Cannot get residue in chain. %s: %d\n",
273 stat = tng_residue_name_get(traj, residue, str, TNG_MAX_STR_LEN);
274 if(stat != TNG_SUCCESS)
276 printf("Cannot get name of residue. %s: %d\n",
280 stat = tng_residue_num_atoms_get(traj, residue, &cnt);
281 if(stat != TNG_SUCCESS || cnt != 3)
283 printf("Cannot get number of atoms in residue. %s: %d\n",
287 stat = tng_residue_atom_of_index_get(traj, residue, 0, &atom);
288 if(stat != TNG_SUCCESS)
290 printf("Cannot get residue of atom. %s: %d\n",
295 stat = tng_atom_name_get(traj, atom, str, TNG_MAX_STR_LEN);
296 if(stat != TNG_SUCCESS)
298 printf("Cannot get name of atom. %s: %d\n",
302 stat = tng_atom_type_get(traj, atom, str, TNG_MAX_STR_LEN);
303 if(stat != TNG_SUCCESS)
305 printf("Cannot get atom type of atom. %s: %d\n",
310 stat = tng_molecule_id_of_particle_nr_get(traj, 0, &cnt);
311 if(stat != TNG_SUCCESS || cnt != 1)
313 printf("Cannot get molecule id of atom. %s: %d\n",
317 stat = tng_residue_id_of_particle_nr_get(traj, 0, &cnt);
318 if(stat != TNG_SUCCESS || cnt != 0)
320 printf("Cannot get residue id of atom. %s: %d\n",
324 stat = tng_global_residue_id_of_particle_nr_get(traj, 599, &cnt);
325 if(stat != TNG_SUCCESS || cnt != 199)
327 printf("Cannot get global residue id of atom. %s: %d\n",
331 stat = tng_molecule_name_of_particle_nr_get(traj, 0, str, TNG_MAX_STR_LEN);
332 if(stat != TNG_SUCCESS)
334 printf("Cannot get molecule name of atom. %s: %d\n",
338 stat = tng_chain_name_of_particle_nr_get(traj, 0, str, TNG_MAX_STR_LEN);
339 if(stat != TNG_SUCCESS)
341 printf("Cannot get chain name of atom. %s: %d\n",
345 stat = tng_residue_name_of_particle_nr_get(traj, 0, str, TNG_MAX_STR_LEN);
346 if(stat != TNG_SUCCESS)
348 printf("Cannot get residue name of atom. %s: %d\n",
352 stat = tng_atom_name_of_particle_nr_get(traj, 0, str, TNG_MAX_STR_LEN);
353 if(stat != TNG_SUCCESS)
355 printf("Cannot get atom name of atom. %s: %d\n",
360 stat = tng_molecule_alloc(traj, &molecule_new);
361 if(stat != TNG_SUCCESS)
363 printf("Cannot setup new molecule. %s: %d\n",
367 stat = tng_molecule_name_set(traj, molecule_new, "TEST");
368 if(stat != TNG_SUCCESS)
370 printf("Cannot set name of new molecule. %s: %d\n",
374 stat = tng_molecule_existing_add(traj, &molecule_new);
375 if(stat != TNG_SUCCESS)
377 printf("Cannot add new molecule to molecule system. %s: %d\n",
382 stat = tng_molsystem_bonds_get(traj, &cnt, &bonds_from, &bonds_to);
383 if(stat != TNG_SUCCESS || cnt != 400)
385 printf("Cannot get bonds in molecule system. %s: %d\n",
396 static tng_function_status tng_test_read_and_write_file
397 (tng_trajectory_t traj, const char hash_mode)
399 char file_name[TNG_MAX_STR_LEN];
400 tng_function_status stat;
402 stat = tng_input_file_get(traj, file_name, TNG_MAX_STR_LEN);
403 if(stat != TNG_SUCCESS)
405 printf("Could not get name of input file. %s: %d\n",
409 stat = tng_output_file_get(traj, file_name, TNG_MAX_STR_LEN);
410 if(stat != TNG_SUCCESS)
412 printf("Could not get name of output file. %s: %d\n",
417 stat = tng_file_headers_read(traj, hash_mode);
418 if(stat != TNG_SUCCESS)
420 printf("Could not read headers. %s: %d\n",
424 stat = tng_file_headers_write(traj, hash_mode);
425 if(stat != TNG_SUCCESS)
427 printf("Could not write headers. %s: %d\n",
432 while(stat == TNG_SUCCESS)
434 stat = tng_frame_set_read_next(traj, hash_mode);
435 if(stat == TNG_CRITICAL)
437 printf("Could not read frame set. %s: %d\n",
441 if(stat == TNG_FAILURE)
445 stat = tng_frame_set_write(traj, hash_mode);
451 static tng_function_status tng_test_write_and_read_traj(tng_trajectory_t *traj,
452 const char hash_mode)
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;
458 int64_t dist_exp = -9, temp_int, temp_int2;
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;
466 tng_medium_stride_length_set(*traj, MEDIUM_STRIDE_LEN);
467 tng_long_stride_length_set(*traj, LONG_STRIDE_LEN);
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);
474 tng_compression_precision_set(*traj, COMPRESSION_PRECISION);
476 tng_distance_unit_exponential_set(*traj, dist_exp);
478 tng_time_per_frame_set(*traj, TIME_PER_FRAME);
480 /* Create molecules */
481 if(tng_test_setup_molecules(*traj) == TNG_CRITICAL)
483 return(TNG_CRITICAL);
486 /* Set the box shape */
487 box_shape[1] = box_shape[2] = box_shape[3] = box_shape[5] = box_shape[6] =
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)
496 tng_trajectory_destroy(traj);
497 printf("Cannot write trajectory box shape.\n");
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++)
506 stat = tng_atom_type_of_particle_nr_get(*traj, i, atom_type,
508 if(stat == TNG_CRITICAL)
512 if(atom_type[0] == 'O')
516 else if(atom_type[0] == 'H')
521 if(stat == TNG_CRITICAL)
524 printf("Failed setting partial charges. %s: %d\n",
526 return(TNG_CRITICAL);
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);
534 if(stat != TNG_SUCCESS)
536 printf("Failed adding partial charges. %s: %d\n",
538 return(TNG_CRITICAL);
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)
549 printf("Failed adding details annotation data block. %s: %d\n",
551 return(TNG_CRITICAL);
554 /* Write file headers (includes non trajectory data blocks */
555 if(tng_file_headers_write(*traj, hash_mode) == TNG_CRITICAL)
557 printf("Cannot write file headers. %s: %d\n",
562 tng_num_frames_per_frame_set_get(*traj, &n_frames_per_frame_set);
564 data = malloc(sizeof(float) * n_particles *
565 n_frames_per_frame_set * 3);
568 printf("Cannot allocate memory. %s: %d\n", __FILE__, __LINE__);
569 return(TNG_CRITICAL);
572 tng_num_molecules_get(*traj, &tot_n_mols);
573 molpos = malloc(sizeof(float) * tot_n_mols * 3);
575 /* Set initial coordinates */
576 for(i = 0; i < tot_n_mols; i++)
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);
586 /* Generate frame sets - each with 100 frames (by default) */
587 for(i = 0; i < N_FRAME_SETS; i++)
590 if(i < N_FRAME_SETS/2)
592 codec_id = TNG_GZIP_COMPRESSION;
596 codec_id = TNG_TNG_COMPRESSION;
598 for(j = 0; j < n_frames_per_frame_set; j++)
600 for(k = 0; k < tot_n_mols; k++)
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;
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;
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)
622 printf("Error creating frame set %d. %s: %d\n",
623 i, __FILE__, __LINE__);
626 return(TNG_CRITICAL);
629 tng_frame_set_particle_mapping_free(*traj);
631 /* Setup particle mapping. Use 4 different mapping blocks with arbitrary
637 if(tng_particle_mapping_add(*traj, 0, 150, mapping) != TNG_SUCCESS)
639 printf("Error creating particle mapping. %s: %d\n",
643 return(TNG_CRITICAL);
649 if(tng_particle_mapping_add(*traj, 150, 150, mapping) != TNG_SUCCESS)
651 printf("Error creating particle mapping. %s: %d\n",
655 return(TNG_CRITICAL);
661 if(tng_particle_mapping_add(*traj, 300, 150, mapping) != TNG_SUCCESS)
663 printf("Error creating particle mapping. %s: %d\n",
667 return(TNG_CRITICAL);
673 if(tng_particle_mapping_add(*traj, 450, 150, mapping) != TNG_SUCCESS)
675 printf("Error creating particle mapping. %s: %d\n",
679 return(TNG_CRITICAL);
682 /* Add the positions in a data block */
683 if(tng_particle_data_block_add(*traj, TNG_TRAJ_POSITIONS,
686 TNG_TRAJECTORY_BLOCK,
687 n_frames_per_frame_set, 3,
689 /* TNG_UNCOMPRESSED, */
691 data) != TNG_SUCCESS)
693 printf("Error adding data. %s: %d\n", __FILE__, __LINE__);
696 return(TNG_CRITICAL);
698 /* Write the frame set */
699 if(tng_frame_set_write(*traj, hash_mode) != TNG_SUCCESS)
701 printf("Error writing frame set. %s: %d\n", __FILE__, __LINE__);
704 return(TNG_CRITICAL);
711 tng_trajectory_destroy(traj);
712 tng_trajectory_init(traj);
713 tng_input_file_set(*traj, TNG_EXAMPLE_FILES_DIR "tng_test.tng");
715 stat = tng_file_headers_read(*traj, hash_mode);
717 tng_first_user_name_get(*traj, temp_str, TNG_MAX_STR_LEN);
718 if(strcmp(USER_NAME, temp_str) != 0)
720 printf("User name does not match when reading written file. %s: %d\n",
725 tng_first_program_name_get(*traj, temp_str, TNG_MAX_STR_LEN);
726 if(strcmp(PROGRAM_NAME, temp_str) != 0)
728 printf("Program name does not match when reading written file. %s: %d\n",
733 tng_first_computer_name_get(*traj, temp_str, TNG_MAX_STR_LEN);
734 if(strcmp(COMPUTER_NAME, temp_str) != 0)
736 printf("Computer name does not match when reading written file. %s: %d\n",
741 tng_forcefield_name_get(*traj, temp_str, TNG_MAX_STR_LEN);
742 if(strcmp(FORCEFIELD_NAME, temp_str) != 0)
744 printf("Forcefield name does not match when reading written file. %s: %d\n",
749 tng_medium_stride_length_get(*traj, &temp_int);
750 if(temp_int != MEDIUM_STRIDE_LEN)
752 printf("Stride length does not match when reading written file. %s: %d\n",
757 tng_long_stride_length_get(*traj, &temp_int);
758 if(temp_int != LONG_STRIDE_LEN)
760 printf("Stride length does not match when reading written file. %s: %d\n",
765 tng_compression_precision_get(*traj, &temp_double);
766 if(temp_double != COMPRESSION_PRECISION)
768 printf("Compression precision does not match when reading written file. %s: %d\n",
773 tng_distance_unit_exponential_get(*traj, &temp_int);
774 if(temp_int != dist_exp)
776 printf("Distance unit exponential does not match when reading written file. %s: %d\n",
781 stat = tng_test_molecules(*traj);
782 if(stat != TNG_SUCCESS)
788 while(stat == TNG_SUCCESS)
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);
798 printf("File position of previous frame set not correct. %s: %d\n",
803 else if(temp_int != -1)
805 printf("File position of previous frame set not correct. %s: %d\n",
809 if(i < N_FRAME_SETS -1)
813 printf("File position of next frame set not correct. %s: %d\n",
818 else if(temp_int2 != -1)
820 printf("File position of previous next set not correct. %s: %d\n",
826 if(stat == TNG_CRITICAL)
831 tng_time_per_frame_get(*traj, &temp_double);
832 if(fabs(TIME_PER_FRAME - temp_double) > 0.000001)
834 printf("Time per frame does not match when reading written file. %s: %d\n",
836 printf("Value: %e, expected value: %e\n", temp_double, TIME_PER_FRAME);
840 stat = tng_frame_set_nr_find(*traj, (int64_t)(0.30*N_FRAME_SETS));
841 if(stat != TNG_SUCCESS)
843 printf("Could not find frame set %"PRId64". %s: %d\n", (int64_t)0.30*N_FRAME_SETS,
848 stat = tng_frame_set_nr_find(*traj, (int64_t)(0.75*N_FRAME_SETS));
849 if(stat != TNG_SUCCESS)
851 printf("Could not find frame set %"PRId64". %s: %d\n", (int64_t)0.75*N_FRAME_SETS,
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)
860 printf("Unexpected first frame in frame set. %s: %d\n",
865 stat = tng_frame_set_read_current_only_data_from_block_id(*traj, hash_mode, TNG_TRAJ_POSITIONS);
866 if(stat != TNG_SUCCESS)
868 printf("Cannot read positions in current frame set. %s: %d\n",
872 stat = tng_frame_set_read_next_only_data_from_block_id(*traj, hash_mode, TNG_TRAJ_POSITIONS);
873 if(stat != TNG_SUCCESS)
875 printf("Cannot read positions in next frame set. %s: %d\n",
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)
882 printf("Cannot get name of data block or unexpected name. %s: %d\n",
886 stat = tng_data_block_name_get(*traj, TNG_TRAJ_FORCES, temp_str, TNG_MAX_STR_LEN);
887 if(stat != TNG_FAILURE)
889 printf("Trying to retrieve name of non-existent data block did not return failure. %s: %d\n",
893 stat = tng_data_block_dependency_get(*traj, TNG_TRAJ_POSITIONS, &dependency);
894 if(stat != TNG_SUCCESS || dependency != TNG_FRAME_DEPENDENT + TNG_PARTICLE_DEPENDENT)
896 printf("Cannot get dependency of data block or unexpected dependency. %s: %d\n",
900 stat = tng_data_block_num_values_per_frame_get(*traj, TNG_TRAJ_POSITIONS, &temp_int);
901 if(stat != TNG_SUCCESS || temp_int != 3)
903 printf("Cannot get number of values per frame of data block or unexpected value. %s: %d\n",
907 stat = tng_data_get_stride_length(*traj, TNG_TRAJ_POSITIONS, 100, &temp_int);
908 if(stat != TNG_SUCCESS || temp_int != 1)
910 printf("Cannot get stride length of data block or unexpected value. %s: %d\n",
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)
921 int64_t n_frames, n_values_per_frame;
922 union data_values **values = 0;
925 if(tng_data_get(traj, TNG_TRAJ_BOX_SHAPE, &values, &n_frames,
926 &n_values_per_frame, &type) != TNG_SUCCESS)
928 printf("Failed getting box shape. %s: %d\n", __FILE__, __LINE__);
929 return(TNG_CRITICAL);
932 /* The X dimension in the example file is 50 */
933 if(fabs(values[0][0].d - 50) > 0.000001)
935 printf("Unexpected value in box shape. %s: %d\n", __FILE__, __LINE__);
939 tng_data_values_free(traj, values, n_frames, n_values_per_frame, type);
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)
950 int64_t i, j, k, n_frames, n_particles, n_values_per_frame;
951 union data_values ***values = 0;
954 if(tng_particle_data_get(traj, TNG_TRAJ_POSITIONS, &values, &n_frames,
955 &n_particles, &n_values_per_frame, &type) !=
958 printf("Failed getting particle positions. %s: %d\n", __FILE__, __LINE__);
959 return(TNG_CRITICAL);
962 if(n_values_per_frame != 3)
964 printf("Number of values per frame does not match expected value. %s: %d\n",
966 tng_particle_data_values_free(traj, values, n_frames, n_particles,
967 n_values_per_frame, type);
971 for(i = 0; i < n_frames; i++)
973 // printf("%"PRId64"\n", i);
974 for(j = 0; j < n_particles; j++)
976 for(k = 0; k < n_values_per_frame; k++)
978 // printf("%f ", values[i][j][k].f);
979 if(values[i][j][k].f < -500 || values[i][j][k].f > 500)
981 printf("Coordinates not in range. %s: %d\n",
983 tng_particle_data_values_free(traj, values, n_frames, n_particles,
984 n_values_per_frame, type);
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)
996 printf("Getting particle positions succeeded when it should have failed. %s: %d\n",
998 return(TNG_CRITICAL);
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)
1005 printf("Failed getting particle positions. %s: %d\n", __FILE__, __LINE__);
1006 return(TNG_CRITICAL);
1009 for(i = 0; i < 50; i++)
1011 // printf("%"PRId64"\n", i);
1012 for(j = 0; j < n_particles; j++)
1014 for(k = 0; k < n_values_per_frame; k++)
1016 // printf("%f ", values[i][j][k].f);
1017 if(values[i][j][k].f < -500 || values[i][j][k].f > 500)
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);
1030 tng_particle_data_values_free(traj, values, n_frames, n_particles,
1031 n_values_per_frame, type);
1033 return(TNG_SUCCESS);
1036 tng_function_status tng_test_utility_functions(tng_trajectory_t traj, const char hash_mode)
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;
1044 stat = tng_util_trajectory_open(TNG_EXAMPLE_FILES_DIR "tng_test.tng", 'r', &traj);
1045 if(stat != TNG_SUCCESS)
1050 stat = tng_util_time_of_frame_get(traj, 50, &time);
1051 if(stat != TNG_SUCCESS || fabs(time - 100e-13) > 0.000001)
1053 printf("Unexpected time at frame 50. %s: %d\n", __FILE__, __LINE__);
1054 printf("Value: %e, expected value: %e\n", time, 100e-13);
1057 stat = tng_util_time_of_frame_get(traj, 100, &time);
1058 if(stat != TNG_SUCCESS || fabs(time - 200e-13) > 0.000001)
1060 printf("Unexpected time at frame 100. %s: %d\n", __FILE__, __LINE__);
1061 printf("Value: %e, expected value: %e\n", time, 100e-13);
1065 tng_num_frames_per_frame_set_get(traj, &n_frames_per_frame_set);
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)
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);
1077 tng_num_frames_per_frame_set_get(traj, &n_frames_per_frame_set);
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)
1085 tng_num_particles_get(traj, &n_particles);
1087 stat = tng_util_pos_read_range(traj, 1, n_frames_to_read, &positions, &stride_len);
1088 if(stat != TNG_SUCCESS)
1097 for(i = 0; i < n_frames_to_read / stride_len; i++)
1099 for(j = 0; j < n_particles; j++)
1101 for(k = 0; k < 3; k++)
1103 if(positions[i*n_particles + j*3 + k] < -500 || positions[i*n_particles + j*3 + k] > 500)
1105 printf("Coordinates not in range. %s: %d\n",
1106 __FILE__, __LINE__);
1108 return(TNG_FAILURE);
1116 stat=tng_util_trajectory_next_frame_present_data_blocks_find(traj, n_frames_to_read,
1118 &n_blocks, &block_ids);
1123 if(stat != TNG_SUCCESS || n_blocks != 1 || next_frame != n_frames_to_read + stride_len)
1125 printf("Unexpected data blocks in next frame. %s: %d\n",
1126 __FILE__, __LINE__);
1127 return(TNG_FAILURE);
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)
1133 printf("Could not get compression. %s: %d\n",
1134 __FILE__, __LINE__);
1135 return(TNG_FAILURE);
1137 stat = tng_util_trajectory_close(&traj);
1138 if(stat != TNG_SUCCESS)
1143 return(TNG_SUCCESS);
1147 tng_function_status tng_test_append(tng_trajectory_t traj, const char hash_mode)
1149 char str[TNG_MAX_STR_LEN];
1150 int64_t n_frames, n_particles, i;
1151 double time, *velocities;
1152 tng_function_status stat;
1154 stat = tng_util_trajectory_open(TNG_EXAMPLE_FILES_DIR "tng_test.tng", 'a', &traj);
1155 if(stat != TNG_SUCCESS)
1157 printf("Cannot open trajectory. %s: %d\n",
1158 __FILE__, __LINE__);
1162 stat = tng_last_user_name_set(traj, USER_NAME);
1163 if(stat != TNG_SUCCESS)
1165 printf("Cannot set last user name. %s: %d\n",
1166 __FILE__, __LINE__);
1169 stat = tng_last_user_name_get(traj, str, TNG_MAX_STR_LEN);
1170 if(stat != TNG_SUCCESS)
1172 printf("Cannot get last user name. %s: %d\n",
1173 __FILE__, __LINE__);
1176 stat = tng_last_program_name_set(traj, PROGRAM_NAME);
1177 if(stat != TNG_SUCCESS)
1179 printf("Cannot set last program name. %s: %d\n",
1180 __FILE__, __LINE__);
1183 stat = tng_last_program_name_get(traj, str, TNG_MAX_STR_LEN);
1184 if(stat != TNG_SUCCESS)
1186 printf("Cannot get last program name. %s: %d\n",
1187 __FILE__, __LINE__);
1190 stat = tng_last_computer_name_set(traj, "Still " COMPUTER_NAME);
1191 if(stat != TNG_SUCCESS)
1193 printf("Cannot set last computer name. %s: %d\n",
1194 __FILE__, __LINE__);
1197 stat = tng_last_computer_name_get(traj, str, TNG_MAX_STR_LEN);
1198 if(stat != TNG_SUCCESS)
1200 printf("Cannot get last computer name. %s: %d\n",
1201 __FILE__, __LINE__);
1205 stat = tng_file_headers_write(traj, hash_mode);
1206 if(stat != TNG_SUCCESS)
1208 printf("Cannot write file headers. %s: %d\n",
1209 __FILE__, __LINE__);
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);
1219 velocities = malloc(sizeof(double) * n_particles * 3);
1222 printf("Cannot allocate memory. %s: %d\n",
1223 __FILE__, __LINE__);
1224 return(TNG_CRITICAL);
1227 for(i = 0; i < n_particles * 3; i++)
1232 stat = tng_util_vel_with_time_double_write(traj, n_frames, time, velocities);
1236 stat = tng_util_trajectory_close(&traj);
1241 tng_function_status tng_test_copy_container(tng_trajectory_t traj, const char hash_mode)
1243 tng_trajectory_t dest;
1244 tng_function_status stat;
1246 stat = tng_util_trajectory_open(TNG_EXAMPLE_FILES_DIR "tng_test.tng", 'r', &traj);
1247 if(stat != TNG_SUCCESS)
1249 printf("Cannot open trajectory. %s: %d\n",
1250 __FILE__, __LINE__);
1254 stat = tng_trajectory_init_from_src(traj, &dest);
1255 if(stat != TNG_SUCCESS)
1260 stat = tng_molecule_system_copy(traj, dest);
1261 if(stat != TNG_SUCCESS)
1266 stat = tng_util_trajectory_close(&traj);
1267 if(stat != TNG_SUCCESS)
1271 stat = tng_util_trajectory_close(&dest);
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;
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)
1287 printf("Succeeded.\n");
1291 printf("Failed.\n");
1294 printf("Test Init trajectory:\t\t\t\t");
1295 if(tng_trajectory_init(&traj) != TNG_SUCCESS)
1297 tng_trajectory_destroy(&traj);
1298 printf("Failed. %s: %d.\n", __FILE__, __LINE__);
1301 printf("Succeeded.\n");
1303 tng_time_get_str(traj, time_str);
1305 printf("Creation time: %s\n", time_str);
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");
1310 printf("Test Read and write file:\t\t\t");
1311 if(tng_test_read_and_write_file(traj, hash_mode) != TNG_SUCCESS)
1313 printf("Failed. %s: %d\n", __FILE__, __LINE__);
1317 printf("Succeeded.\n");
1320 printf("Test Get data:\t\t\t\t\t");
1321 if(tng_test_get_box_data(traj) != TNG_SUCCESS)
1323 printf("Failed. %s: %d\n", __FILE__, __LINE__);
1327 printf("Succeeded.\n");
1330 printf("Test Destroy and init trajectory:\t\t");
1331 if(tng_trajectory_destroy(&traj) != TNG_SUCCESS ||
1332 tng_trajectory_init(&traj) != TNG_SUCCESS)
1334 printf("Failed. %s: %d\n", __FILE__, __LINE__);
1338 printf("Succeeded.\n");
1342 tng_output_file_set(traj, TNG_EXAMPLE_FILES_DIR "tng_test.tng");
1344 printf("Test Write and read file:\t\t\t");
1345 if(tng_test_write_and_read_traj(&traj, hash_mode) != TNG_SUCCESS)
1347 printf("Failed. %s: %d\n", __FILE__, __LINE__);
1351 printf("Succeeded.\n");
1354 printf("Test Get particle data:\t\t\t\t");
1355 if(tng_test_get_positions_data(traj, hash_mode) != TNG_SUCCESS)
1357 printf("Failed. %s: %d\n",
1358 __FILE__, __LINE__);
1362 printf("Succeeded.\n");
1365 printf("Test Destroy trajectory:\t\t\t");
1366 if(tng_trajectory_destroy(&traj) != TNG_SUCCESS)
1368 printf("Failed. %s: %d.\n", __FILE__, __LINE__);
1373 printf("Succeeded.\n");
1376 printf("Test Utility functions:\t\t\t\t");
1377 if(tng_test_utility_functions(traj, hash_mode) != TNG_SUCCESS)
1379 printf("Failed. %s: %d.\n", __FILE__, __LINE__);
1384 printf("Succeeded.\n");
1387 printf("Test Append:\t\t\t\t\t");
1388 if(tng_test_append(traj, hash_mode) != TNG_SUCCESS)
1390 printf("Failed. %s: %d.\n", __FILE__, __LINE__);
1394 printf("Succeeded.\n");
1397 printf("Test Copy trajectory container:\t\t\t");
1398 if(tng_test_copy_container(traj, hash_mode) != TNG_SUCCESS)
1400 printf("Failed. %s: %d.\n", __FILE__, __LINE__);
1404 printf("Succeeded.\n");
1407 printf("Tests finished\n");