diff options
-rw-r--r-- | src/lib/tng_io.c | 258 | ||||
-rw-r--r-- | src/lib/tng_io.h | 48 | ||||
-rw-r--r-- | src/tests/tng_io_read_pos.c | 52 |
3 files changed, 309 insertions, 49 deletions
diff --git a/src/lib/tng_io.c b/src/lib/tng_io.c index 4b37297..708fc3d 100644 --- a/src/lib/tng_io.c +++ b/src/lib/tng_io.c @@ -556,7 +556,7 @@ static tng_function_status tng_block_init(struct tng_gen_block **block_p) if(!*block_p) { - printf("Cannot allocate memory (%"PRId64" bytes). %s: %d\n", + printf("Cannot allocate memory (%lu bytes). %s: %d\n", sizeof(struct tng_gen_block), __FILE__, __LINE__); return(TNG_CRITICAL); } @@ -5425,6 +5425,100 @@ tng_function_status tng_molecule_destroy(const tng_trajectory_t tng_data, return(TNG_SUCCESS); } +tng_function_status tng_molecule_name_of_particle_nr_get + (const tng_trajectory_t tng_data, + const int64_t nr, + char *name, + int max_len) +{ + int64_t cnt = 0, i, *molecule_cnt_list; + tng_molecule_t mol; + tng_bool found = FALSE; + + if(tng_data->var_num_atoms_flag) + { + molecule_cnt_list = tng_data->current_trajectory_frame_set. + molecule_cnt_list; + } + else + { + molecule_cnt_list = tng_data->molecule_cnt_list; + } + + for(i = 0; i < tng_data->n_molecules; i++) + { + mol = &tng_data->molecules[i]; + if(cnt + mol->n_atoms * molecule_cnt_list[i] - 1 < nr) + { + cnt += mol->n_atoms * molecule_cnt_list[i]; + continue; + } + found = TRUE; + break; + } + if(!found) + { + return(TNG_FAILURE); + } + + strncpy(name, mol->name, max_len - 1); + name[max_len - 1] = 0; + + if(strlen(mol->name) > max_len - 1) + { + return(TNG_FAILURE); + } + return(TNG_SUCCESS); +} + +tng_function_status tng_atom_name_of_particle_nr_get + (const tng_trajectory_t tng_data, + const int64_t nr, + char *name, + int max_len) +{ + int64_t cnt = 0, i, *molecule_cnt_list; + tng_molecule_t mol; + tng_atom_t atom; + tng_bool found = FALSE; + + if(tng_data->var_num_atoms_flag) + { + molecule_cnt_list = tng_data->current_trajectory_frame_set. + molecule_cnt_list; + } + else + { + molecule_cnt_list = tng_data->molecule_cnt_list; + } + + for(i = 0; i < tng_data->n_molecules; i++) + { + mol = &tng_data->molecules[i]; + if(cnt + mol->n_atoms * molecule_cnt_list[i] - 1 < nr) + { + cnt += mol->n_atoms * molecule_cnt_list[i]; + continue; + } + atom = &mol->atoms[nr % mol->n_atoms]; + found = TRUE; + break; + } + if(!found) + { + return(TNG_FAILURE); + } + + strncpy(name, atom->name, max_len - 1); + name[max_len - 1] = 0; + + if(strlen(atom->name) > max_len - 1) + { + return(TNG_FAILURE); + } + return(TNG_SUCCESS); +} + tng_function_status tng_particle_mapping_add (tng_trajectory_t tng_data, const int64_t num_first_particle, @@ -5438,7 +5532,7 @@ tng_function_status tng_particle_mapping_add /* Sanity check of the particle ranges. Split into multiple if * statements for improved readability */ - for(i=0; i<frame_set->n_mapping_blocks; i++) + for(i = 0; i < frame_set->n_mapping_blocks; i++) { mapping = &frame_set->mappings[i]; if(num_first_particle >= mapping->num_first_particle && @@ -5650,6 +5744,7 @@ tng_function_status tng_trajectory_init(struct tng_trajectory **tng_data_p) tng_data->current_trajectory_frame_set.next_frame_set_file_pos = -1; tng_data->current_trajectory_frame_set.prev_frame_set_file_pos = -1; + tng_data->current_trajectory_frame_set.n_frames = 0; return(TNG_SUCCESS); } @@ -7895,8 +7990,18 @@ tng_function_status tng_frame_data_write(tng_trajectory_t tng_data, header_pos = file_pos; frame_set = &tng_data->current_trajectory_frame_set; - fread(&datatype, sizeof(datatype), 1, tng_data->input_file); - fread(&dependency, sizeof(dependency), 1, tng_data->input_file); + if(fread(&datatype, sizeof(datatype), 1, tng_data->input_file) == 0) + { + printf("Error reading file. %s: %d\n", __FILE__, __LINE__); + tng_block_destroy(&block); + return(TNG_CRITICAL); + } + if(fread(&dependency, sizeof(dependency), 1, tng_data->input_file) == 0) + { + printf("Error reading file. %s: %d\n", __FILE__, __LINE__); + tng_block_destroy(&block); + return(TNG_CRITICAL); + } data.datatype = datatype; if(!(dependency & TNG_FRAME_DEPENDENT) || @@ -7911,18 +8016,33 @@ tng_function_status tng_frame_data_write(tng_trajectory_t tng_data, return(TNG_FAILURE); } - fread(&sparse_data, sizeof(sparse_data), 1, tng_data->input_file); - - fread(&data.n_values_per_frame, sizeof(data.n_values_per_frame), 1, - tng_data->input_file); + if(fread(&sparse_data, sizeof(sparse_data), 1, tng_data->input_file) == 0) + { + printf("Error reading file. %s: %d\n", __FILE__, __LINE__); + tng_block_destroy(&block); + return(TNG_CRITICAL); + } + + if(fread(&data.n_values_per_frame, sizeof(data.n_values_per_frame), 1, + tng_data->input_file) == 0) + { + printf("Error reading file. %s: %d\n", __FILE__, __LINE__); + tng_block_destroy(&block); + return(TNG_CRITICAL); + } if(tng_swap_byte_order_64(tng_data, &data.n_values_per_frame) != TNG_SUCCESS) { printf("Cannot swap byte order to get big endian. %s: %d\n", __FILE__, __LINE__); } - fread(&data.codec_id, sizeof(data.codec_id), 1, - tng_data->input_file); + if(fread(&data.codec_id, sizeof(data.codec_id), 1, + tng_data->input_file) == 0) + { + printf("Error reading file. %s: %d\n", __FILE__, __LINE__); + tng_block_destroy(&block); + return(TNG_CRITICAL); + } if(tng_swap_byte_order_64(tng_data, &data.codec_id) != TNG_SUCCESS) { printf("Cannot swap byte order to get big endian. %s: %d\n", @@ -7931,8 +8051,14 @@ tng_function_status tng_frame_data_write(tng_trajectory_t tng_data, if(data.codec_id != TNG_UNCOMPRESSED) { - fread(&data.compression_multiplier, - sizeof(data.compression_multiplier), 1, tng_data->input_file); + if(fread(&data.compression_multiplier, + sizeof(data.compression_multiplier), 1, tng_data->input_file) + == 0) + { + printf("Error reading file. %s: %d\n", __FILE__, __LINE__); + tng_block_destroy(&block); + return(TNG_CRITICAL); + } if(tng_swap_byte_order_64(tng_data, (int64_t *) &data.compression_multiplier) != TNG_SUCCESS) @@ -7948,8 +8074,13 @@ tng_function_status tng_frame_data_write(tng_trajectory_t tng_data, if(sparse_data) { - fread(&data.first_frame_with_data, sizeof(data.first_frame_with_data), - 1, tng_data->input_file); + if(fread(&data.first_frame_with_data, sizeof(data.first_frame_with_data), + 1, tng_data->input_file) == 0) + { + printf("Error reading file. %s: %d\n", __FILE__, __LINE__); + tng_block_destroy(&block); + return(TNG_CRITICAL); + } if(tng_swap_byte_order_64(tng_data, &data.first_frame_with_data) != TNG_SUCCESS) { @@ -7957,8 +8088,13 @@ tng_function_status tng_frame_data_write(tng_trajectory_t tng_data, __FILE__, __LINE__); } - fread(&data.stride_length, sizeof(data.stride_length), - 1, tng_data->input_file); + if(fread(&data.stride_length, sizeof(data.stride_length), + 1, tng_data->input_file) == 0) + { + printf("Error reading file. %s: %d\n", __FILE__, __LINE__); + tng_block_destroy(&block); + return(TNG_CRITICAL); + } if(tng_swap_byte_order_64(tng_data, &data.stride_length) != TNG_SUCCESS) { @@ -8298,9 +8434,21 @@ tng_function_status tng_frame_particle_data_write(tng_trajectory_t tng_data, header_pos = ftell(tng_data->output_file) - header_size; frame_set = &tng_data->current_trajectory_frame_set; - fread(&datatype, sizeof(datatype), 1, tng_data->input_file); - fread(&dependency, sizeof(dependency), 1, tng_data->input_file); + if(fread(&datatype, sizeof(datatype), 1, tng_data->input_file) == 0) + { + printf("Error reading file. %s: %d\n", __FILE__, __LINE__); + tng_block_destroy(&block); + return(TNG_CRITICAL); + } + data.datatype = datatype; + + if(fread(&dependency, sizeof(dependency), 1, tng_data->input_file) == 0) + { + printf("Error reading file. %s: %d\n", __FILE__, __LINE__); + tng_block_destroy(&block); + return(TNG_CRITICAL); + } if(!(dependency & TNG_FRAME_DEPENDENT) || !(dependency & TNG_PARTICLE_DEPENDENT)) @@ -8314,18 +8462,34 @@ tng_function_status tng_frame_particle_data_write(tng_trajectory_t tng_data, return(TNG_FAILURE); } - fread(&sparse_data, sizeof(sparse_data), 1, tng_data->input_file); + if(fread(&sparse_data, sizeof(sparse_data), 1, tng_data->input_file) == 0) + { + printf("Error reading file. %s: %d\n", __FILE__, __LINE__); + tng_block_destroy(&block); + return(TNG_CRITICAL); + } - fread(&data.n_values_per_frame, sizeof(data.n_values_per_frame), 1, - tng_data->input_file); - if(tng_swap_byte_order_64(tng_data, &data.n_values_per_frame) != TNG_SUCCESS) + if(fread(&data.n_values_per_frame, sizeof(data.n_values_per_frame), 1, + tng_data->input_file) == 0) + { + printf("Error reading file. %s: %d\n", __FILE__, __LINE__); + tng_block_destroy(&block); + return(TNG_CRITICAL); + } + if(tng_swap_byte_order_64(tng_data, &data.n_values_per_frame) + != TNG_SUCCESS) { printf("Cannot swap byte order to get big endian. %s: %d\n", __FILE__, __LINE__); } - fread(&data.codec_id, sizeof(data.codec_id), 1, - tng_data->input_file); + if(fread(&data.codec_id, sizeof(data.codec_id), 1, + tng_data->input_file) == 0) + { + printf("Error reading file. %s: %d\n", __FILE__, __LINE__); + tng_block_destroy(&block); + return(TNG_CRITICAL); + } if(tng_swap_byte_order_64(tng_data, &data.codec_id) != TNG_SUCCESS) { printf("Cannot swap byte order to get big endian. %s: %d\n", @@ -8334,8 +8498,15 @@ tng_function_status tng_frame_particle_data_write(tng_trajectory_t tng_data, if(data.codec_id != TNG_UNCOMPRESSED) { - fread(&data.compression_multiplier, - sizeof(data.compression_multiplier), 1, tng_data->input_file); + if(fread(&data.compression_multiplier, + sizeof(data.compression_multiplier), 1, tng_data->input_file) + == 0) + { + printf("Error reading file. %s: %d\n", __FILE__, __LINE__); + tng_block_destroy(&block); + return(TNG_CRITICAL); + } + if(tng_swap_byte_order_64(tng_data, (int64_t *) &data.compression_multiplier) != TNG_SUCCESS) @@ -8351,8 +8522,14 @@ tng_function_status tng_frame_particle_data_write(tng_trajectory_t tng_data, if(sparse_data) { - fread(&data.first_frame_with_data, sizeof(data.first_frame_with_data), - 1, tng_data->input_file); + if(fread(&data.first_frame_with_data, + sizeof(data.first_frame_with_data), + 1, tng_data->input_file) == 0) + { + printf("Error reading file. %s: %d\n", __FILE__, __LINE__); + tng_block_destroy(&block); + return(TNG_CRITICAL); + } if(tng_swap_byte_order_64(tng_data, &data.first_frame_with_data) != TNG_SUCCESS) { @@ -8360,8 +8537,13 @@ tng_function_status tng_frame_particle_data_write(tng_trajectory_t tng_data, __FILE__, __LINE__); } - fread(&data.stride_length, sizeof(data.stride_length), - 1, tng_data->input_file); + if(fread(&data.stride_length, sizeof(data.stride_length), + 1, tng_data->input_file) == 0) + { + printf("Error reading file. %s: %d\n", __FILE__, __LINE__); + tng_block_destroy(&block); + return(TNG_CRITICAL); + } if(tng_swap_byte_order_64(tng_data, &data.stride_length) != TNG_SUCCESS) { @@ -8376,7 +8558,13 @@ tng_function_status tng_frame_particle_data_write(tng_trajectory_t tng_data, } data.n_frames = tng_data->current_trajectory_frame_set.n_frames; - fread(&num_first_particle, sizeof(num_first_particle), 1, tng_data->input_file); + if(fread(&num_first_particle, sizeof(num_first_particle), 1, + tng_data->input_file) == 0) + { + printf("Error reading file. %s: %d\n", __FILE__, __LINE__); + tng_block_destroy(&block); + return(TNG_CRITICAL); + } if(tng_swap_byte_order_64(tng_data, &num_first_particle) != TNG_SUCCESS) { @@ -8384,7 +8572,13 @@ tng_function_status tng_frame_particle_data_write(tng_trajectory_t tng_data, __FILE__, __LINE__); } - fread(&block_n_particles, sizeof(block_n_particles), 1, tng_data->input_file); + if(fread(&block_n_particles, sizeof(block_n_particles), 1, + tng_data->input_file) == 0) + { + printf("Error reading file. %s: %d\n", __FILE__, __LINE__); + tng_block_destroy(&block); + return(TNG_CRITICAL); + } if(tng_swap_byte_order_64(tng_data, &block_n_particles) != TNG_SUCCESS) { diff --git a/src/lib/tng_io.h b/src/lib/tng_io.h index 8bda250..c00abc0 100644 --- a/src/lib/tng_io.h +++ b/src/lib/tng_io.h @@ -1317,6 +1317,54 @@ tng_function_status tng_atom_type_set_(tng_trajectory_t tng_data, } /** + * @brief Get the molecume name of real particle number (number in mol system). + * @param tng_data is the trajectory data container containing the atom. + * @param nr is the real number of the particle in the molecular system. + * @param name is a string, which is set to the name of the molecule. Memory + * must be reserved beforehand. + * @param max_len is the maximum length of name. + * @return TNG_SUCCESS (0) if successful or TNG_FAILURE (!) if a minor error + * has occured. + */ +tng_function_status tng_molecule_name_of_particle_nr_get + (const tng_trajectory_t tng_data, + const int64_t nr, + char *name, + int max_len); +tng_function_status tng_molecule_name_of_particle_nr_get_ + (const tng_trajectory_t tng_data, + const int64_t nr, + char *name, + int max_len) +{ + return(tng_molecule_name_of_particle_nr_get(tng_data, nr, name, max_len)); +} + +/** + * @brief Get the atom name of real particle number (number in mol system). + * @param tng_data is the trajectory data container containing the atom. + * @param nr is the real number of the particle in the molecular system. + * @param name is a string, which is set to the name of the atom. Memory + * must be reserved beforehand. + * @param max_len is the maximum length of name. + * @return TNG_SUCCESS (0) if successful or TNG_FAILURE (!) if a minor error + * has occured. + */ +tng_function_status tng_atom_name_of_particle_nr_get + (const tng_trajectory_t tng_data, + const int64_t nr, + char *name, + int max_len); +tng_function_status tng_atom_name_of_particle_nr_get_ + (const tng_trajectory_t tng_data, + const int64_t nr, + char *name, + int max_len) +{ + return(tng_atom_name_of_particle_nr_get(tng_data, nr, name, max_len)); +} + +/** * @brief Add a particle mapping table. * @details Each particle mapping table will be written as a separate block, * followed by the data blocks for the corresponding particles. In most cases diff --git a/src/tests/tng_io_read_pos.c b/src/tests/tng_io_read_pos.c index 753f475..5d3be64 100644 --- a/src/tests/tng_io_read_pos.c +++ b/src/tests/tng_io_read_pos.c @@ -12,6 +12,7 @@ int main(int argc, char **argv) int64_t particle = 0; // Set a default frame range int first_frame = 0, last_frame = 50; + char name[64]; if(argc <= 1) { @@ -49,6 +50,16 @@ int main(int argc, char **argv) n_frames = last_frame - first_frame + 1; + if(tng_atom_name_of_particle_nr_get(traj, particle, name, sizeof(name)) == + TNG_SUCCESS) + { + printf("Particle: %s\n", name); + } + else + { + printf("Particle name not found\n"); + } + // Get the positions of all particles in the requested frame range. // The positions are stored in the positions array. // N.B. No proper error checks. @@ -56,27 +67,34 @@ int main(int argc, char **argv) last_frame, TNG_USE_HASH, &positions, &n_particles, &n_values_per_frame, &data_type) == TNG_SUCCESS) { - // Print the positions of the wanted particle (zero based) - for(i=0; i<n_frames; i++) + if(particle >= n_particles) + { + printf("Chosen particle out of range. Only %"PRId64" particles in trajectory.\n", n_particles); + } + else { - printf("%d", first_frame + i); - for(j=0; j<n_values_per_frame; j++) + // Print the positions of the wanted particle (zero based) + for(i=0; i<n_frames; i++) { - switch(data_type) + printf("%d", first_frame + i); + for(j=0; j<n_values_per_frame; j++) { - case TNG_INT_DATA: - printf("\t%"PRId64"", positions[i][particle][j].i); - break; - case TNG_FLOAT_DATA: - printf("\t%f", positions[i][particle][j].f); - break; - case TNG_DOUBLE_DATA: - printf("\t%f", positions[i][particle][j].d); - break; - default: - break; + switch(data_type) + { + case TNG_INT_DATA: + printf("\t%"PRId64"", positions[i][particle][j].i); + break; + case TNG_FLOAT_DATA: + printf("\t%f", positions[i][particle][j].f); + break; + case TNG_DOUBLE_DATA: + printf("\t%f", positions[i][particle][j].d); + break; + default: + break; + } + printf("\n"); } - printf("\n"); } } } |