diff options
Diffstat (limited to 'src/lib/tng_io.c')
-rw-r--r-- | src/lib/tng_io.c | 2554 |
1 files changed, 1516 insertions, 1038 deletions
diff --git a/src/lib/tng_io.c b/src/lib/tng_io.c index f43de8f..410fb7c 100644 --- a/src/lib/tng_io.c +++ b/src/lib/tng_io.c @@ -27,6 +27,7 @@ #include "tng_io.h" #include "md5.h" +#include "compression/tng_compress.h" struct tng_bond { @@ -200,9 +201,11 @@ struct tng_particle_data { int64_t codec_id; /** The multiplier used for getting integer values for compression */ double compression_multiplier; - /** A 3-dimensional array of values, sized - * n_frames * n_particles * n_values_per_frame */ - union data_values ***values; + /** A 1-dimensional array of values of length + * [sizeof (datatype)] * n_frames * n_particles * n_values_per_frame */ + void *values; + /** If storing character data store it in a 3-dimensional array */ + char ****strings; }; struct tng_non_particle_data { @@ -227,8 +230,11 @@ struct tng_non_particle_data { /** Compressed data is stored as integers. This compression multiplier is * the multiplication factor to convert from integer to float/double */ double compression_multiplier; - /** A 2-dimensional array of values, sized n_frames * n_values_per_frame */ - union data_values **values; + /** A 1-dimensional array of values of length + * [sizeof (datatype)] * n_frames * n_values_per_frame */ + void *values; + /** If storing character data store it in a 3-dimensional array */ + char ***strings; }; @@ -337,6 +343,11 @@ struct tng_trajectory { int n_data_blocks; /** A list of frame and particle indepdendent data blocks */ struct tng_non_particle_data *non_tr_data; + + /** TNG compression algorithm for compressing positions */ + int *compress_algo_pos; + /** TNG compression algorithm for compressing velocities */ + int *compress_algo_vel; }; #ifndef USE_WINDOWS @@ -353,20 +364,239 @@ struct tng_trajectory { #define TNG_SNPRINTF snprintf #endif +tng_function_status tng_util_trajectory_open(const char *filename, + const char mode, + tng_trajectory_t *tng_data_p) +{ + if(mode != 'r' && mode != 'w' && mode != 'a') + { + return(TNG_FAILURE); + } + + if(tng_trajectory_init(tng_data_p) != TNG_SUCCESS) + { + tng_trajectory_destroy(tng_data_p); + return(TNG_CRITICAL); + } + + if(mode == 'r' || mode == 'a') + { + tng_input_file_set(*tng_data_p, filename); + + // Read the file headers + tng_file_headers_read(*tng_data_p, TNG_USE_HASH); + } + + if(mode == 'w' || mode == 'a') + { + tng_output_file_set(*tng_data_p, filename); + } + + return(TNG_SUCCESS); +} + +tng_function_status tng_util_trajectory_close(tng_trajectory_t *tng_data_p) +{ + return(tng_trajectory_destroy(tng_data_p)); +} + +tng_function_status tng_util_trajectory_molecules_get(tng_trajectory_t tng_data, + int64_t *n_mols, + int64_t *molecule_cnt_list, + tng_molecule_t *mols) +{ + /* FIXME: This should return a copy of the molecules instead */ + *n_mols = tng_data->n_molecules; + + molecule_cnt_list = tng_data->molecule_cnt_list; + mols = &tng_data->molecules; + + return(TNG_SUCCESS); +} + +tng_function_status tng_util_trajectory_molecule_add(tng_trajectory_t tng_data, + const char *name, + const int64_t cnt, + tng_molecule_t *mol) +{ + tng_function_status stat; + stat = tng_molecule_add(tng_data, name, mol); + if(stat != TNG_SUCCESS) + { + return(stat); + } + stat = tng_molecule_cnt_set(tng_data, *mol, cnt); + + return(stat); +} + +tng_function_status tng_util_molecule_particles_get(tng_trajectory_t tng_data, + const tng_molecule_t mol, + int64_t *n_particles, + char ***names, + char ***types, + char ***res_names, + int64_t **res_ids, + char ***chain_names, + int64_t **chain_ids) +{ + tng_atom_t atom; + tng_residue_t res; + tng_chain_t chain; + int64_t i; + + *n_particles = mol->n_atoms; + + **names = malloc(sizeof(char *) * *n_particles); + **types = malloc(sizeof(char *) * *n_particles); + **res_names = malloc(sizeof(char *) * *n_particles); + **chain_names = malloc(sizeof(char *) * *n_particles); + *res_ids = malloc(sizeof(int64_t) * *n_particles); + *chain_ids = malloc(sizeof(int64_t) * *n_particles); + + for(i = 0; i < *n_particles; i++) + { + atom = &mol->atoms[i]; + res = atom->residue; + chain = res->chain; + *names[i] = malloc(strlen(atom->name)); + strcpy(*names[i], atom->name); + *types[i] = malloc(strlen(atom->atom_type)); + strcpy(*types[i], atom->atom_type); + *res_names[i] = malloc(strlen(res->name)); + strcpy(*res_names[i], res->name); + *chain_names[i] = malloc(strlen(chain->name)); + strcpy(*chain_names[i], chain->name); + *res_ids[i] = res->id; + *chain_ids[i] = chain->id; + } + + return(TNG_SUCCESS); +} + +tng_function_status tng_util_molecule_particles_set(tng_trajectory_t tng_data, + tng_molecule_t mol, + const int64_t n_particles, + const char **names, + const char **types, + const char **res_names, + const int64_t *res_ids, + const char **chain_names, + const int64_t *chain_ids) +{ + int64_t i; + tng_chain_t chain; + tng_residue_t residue; + tng_atom_t atom; + tng_function_status stat; + + for(i = 0; i < n_particles; i++) + { + if(tng_molecule_chain_find(tng_data, mol, chain_names[i], chain_ids[i], + &chain) == TNG_FAILURE) + { + stat = tng_molecule_chain_add(tng_data, mol, chain_names[i], + &chain); + if(stat != TNG_SUCCESS) + { + return(stat); + } + } + if(tng_chain_residue_find(tng_data, chain, res_names[i], res_ids[i], + &residue) == TNG_FAILURE) + { + stat = tng_chain_residue_add(tng_data, chain, res_names[i], + &residue); + if(stat != TNG_SUCCESS) + { + return(stat); + } + } + stat = tng_residue_atom_add(tng_data, residue, names[i], types[i], &atom); + if(stat != TNG_SUCCESS) + { + return(stat); + } + } + return(TNG_SUCCESS); +} + +tng_function_status tng_util_pos_read(tng_trajectory_t tng_data, + float *positions) +{ +} + +tng_function_status tng_util_vel_read(tng_trajectory_t tng_data, + float *velocities) +{ +} + +tng_function_status tng_util_force_read(tng_trajectory_t tng_data, + float *forces) +{ +} + +tng_function_status tng_util_pos_read_range(tng_trajectory_t tng_data, + const int64_t first_frame, + const int64_t last_frame, + float *positions) +{ +} + +tng_function_status tng_util_vel_read_range(tng_trajectory_t tng_data, + const int64_t first_frame, + const int64_t last_frame, + float *velocities) +{ +} + +tng_function_status tng_util_force_read_range(tng_trajectory_t tng_data, + const int64_t first_frame, + const int64_t last_frame, + float *forces) +{ +} + +tng_function_status tng_util_pos_write(tng_trajectory_t tng_data, + const int64_t frame_nr, + const float *positions) +{ +} + +tng_function_status tng_util_vel_write(tng_trajectory_t tng_data, + const int64_t frame_nr, + const float *velocities) +{ +} + +tng_function_status tng_util_force_write(tng_trajectory_t tng_data, + const int64_t frame_nr, + const float *forces) +{ +} + static TNG_INLINE int tng_min(int a, int b) { - int r=a; - if (b<a) - r=b; - return r; + int r=a; + + if (b<a) + { + r=b; + } + + return r; } static TNG_INLINE int tng_max(int a, int b) { - int r=a; - if (b>a) - r=b; - return r; + int r=a; + + if (b>a) + { + r=b; + } + + return r; } /** This function swaps the byte order of a 32 bit numerical variable @@ -3633,10 +3863,135 @@ static tng_function_status tng_particle_data_block_create return(TNG_SUCCESS); } +static tng_function_status tng_compress(tng_trajectory_t tng_data, + tng_gen_block_t block, + int64_t n_frames, + int64_t n_particles, + void *start_pos, int len) +{ + int nalgo; + int new_len; + char *dest, *temp; + + if(block->id != TNG_TRAJ_POSITIONS && + block->id != TNG_TRAJ_VELOCITIES) + { + printf("Can only compress positions and velocities with the" + "TNG method.\n"); + return(TNG_FAILURE); + } + + if(block->id == TNG_TRAJ_POSITIONS) + { + if(!tng_data->compress_algo_pos) + { + nalgo = tng_compress_nalgo(); + tng_data->compress_algo_pos=malloc(nalgo * + sizeof *tng_data->compress_algo_pos); + dest = tng_compress_pos_find_algo(start_pos, n_particles, + n_frames, + 0.01, 0, + tng_data->compress_algo_pos, + &new_len); + } + else + { + dest = tng_compress_pos(start_pos, n_particles, + n_frames, 0.01, 0, + tng_data->compress_algo_pos, &new_len); + } + } + else + { + if(!tng_data->compress_algo_vel) + { + nalgo = tng_compress_nalgo(); + tng_data->compress_algo_vel=malloc(nalgo * + sizeof *tng_data->compress_algo_vel); + dest = tng_compress_vel_find_algo(start_pos, n_particles, + n_frames, + 0.01, 0, + tng_data->compress_algo_vel, + &new_len); + } + else + { + dest = tng_compress_vel(start_pos, n_particles, + n_frames, 0.01, 0, + tng_data->compress_algo_vel, &new_len); + } + } + + memcpy(start_pos, dest, new_len); + + free(dest); + + block->block_contents_size = new_len + (block->block_contents_size - len); + + temp = realloc(block->block_contents, block->block_contents_size); + if(!temp) + { + free(block->block_contents); + printf("Cannot allocate memory (%"PRId64" bytes). %s: %d\n", + block->block_contents_size, __FILE__, __LINE__); + return(TNG_CRITICAL); + } + + block->block_contents = temp; + + return(TNG_SUCCESS); +} + +static tng_function_status tng_uncompress(tng_trajectory_t tng_data, + tng_gen_block_t block, + void *start_pos, + unsigned long uncompressed_len) +{ + char *temp; + double *dest = 0; + int offset; + + if(block->id != TNG_TRAJ_POSITIONS && + block->id != TNG_TRAJ_VELOCITIES) + { + printf("Can only uncompress positions and velocities with the" + "TNG method.\n"); + return(TNG_FAILURE); + } + + if(tng_compress_uncompress(start_pos, dest) == 1) + { + printf("Cannot uncompress TNG compressed block.\n"); + return(TNG_FAILURE); + } + + offset = start_pos - (void *)block->block_contents; + + + block->block_contents_size = uncompressed_len + offset; + + temp = realloc(block->block_contents, uncompressed_len + offset); + if(!temp) + { + free(block->block_contents); + free(dest); + printf("Cannot allocate memory (%"PRId64" bytes). %s: %d\n", + block->block_contents_size, __FILE__, __LINE__); + return(TNG_CRITICAL); + } + + memcpy(temp + offset, dest, uncompressed_len); + + block->block_contents = temp; + + free(dest); + return(TNG_SUCCESS); +} + #ifdef USE_ZLIB static tng_function_status tng_gzip_compress(tng_trajectory_t tng_data, tng_gen_block_t block, - void *start_pos, int len) + void *start_pos, const int len) { Bytef *dest; char *temp; @@ -3663,6 +4018,8 @@ static tng_function_status tng_gzip_compress(tng_trajectory_t tng_data, memcpy(start_pos, dest, max_len); + free(dest); + block->block_contents_size = max_len + (block->block_contents_size - len); temp = realloc(block->block_contents, block->block_contents_size); @@ -3758,67 +4115,95 @@ static tng_function_status tng_allocate_particle_data_mem const int64_t n_particles, const int64_t n_values_per_frame) { - union data_values ***values; - int64_t i, j, k; + void ***values; + int64_t i, j, k, size; - if(data->values) + if(data->strings && data->datatype == TNG_CHAR_DATA) { for(i = data->n_frames; i--;) { for(j = n_particles; j--;) { - free(data->values[i][j]); + for(k = data->n_values_per_frame; k--;) + { + if(data->strings[i][j][k]) + { + free(data->strings[i][j][k]); + data->strings[i][j][k] = 0; + } + } + free(data->strings[i][j]); + data->strings[i][j] = 0; } - free(data->values[i]); + free(data->strings[i]); } } data->n_frames = n_frames; n_frames = tng_max(1, n_frames); data->stride_length = tng_max(1, stride_length); data->n_values_per_frame = n_values_per_frame; - values = realloc(data->values, sizeof(union data_values **) * n_frames); - if(!values) - { - printf("Cannot allocate memory (%"PRId64" bytes). %s: %d\n", - sizeof(union data_values **) * n_frames, - __FILE__, __LINE__); - free(data->values); - return(TNG_CRITICAL); - } - data->values = values; - - for(i = n_frames; i-- ;) + if(data->datatype == TNG_CHAR_DATA) { - data->values[i] = malloc(sizeof(union data_values *) * - n_particles); - if(!data->values[i]) + data->strings = malloc(sizeof(char ***) * n_frames); + for(i = n_frames; i-- ;) { - printf("Cannot allocate memory (%"PRId64" bytes). %s: %d\n", - sizeof(union data_values *) * n_particles, - __FILE__, __LINE__); - return(TNG_CRITICAL); - } - for(j = n_particles; j--;) - { - data->values[i][j] = malloc(sizeof(union data_values) * - n_values_per_frame); - if(!data->values[i][j]) + data->strings[i] = malloc(sizeof(char **) * + n_particles); + if(!data->strings[i]) { printf("Cannot allocate memory (%"PRId64" bytes). %s: %d\n", - sizeof(union data_values) * n_values_per_frame, + sizeof(union data_values *) * n_particles, __FILE__, __LINE__); return(TNG_CRITICAL); } - if(data->datatype == TNG_CHAR_DATA) + for(j = n_particles; j--;) { + data->strings[i][j] = malloc(sizeof(char *) * + n_values_per_frame); + if(!data->strings[i][j]) + { + printf("Cannot allocate memory (%"PRId64" bytes). %s: %d\n", + sizeof(union data_values) * n_values_per_frame, + __FILE__, __LINE__); + return(TNG_CRITICAL); + } for(k = n_values_per_frame; k--;) { - data->values[i][j][k].c = 0; + data->strings[i][j][k] = 0; } } } } + else + { + switch(data->datatype) + { + case TNG_INT_DATA: + size = sizeof(int64_t); + break; + case TNG_FLOAT_DATA: + size = sizeof(float); + break; + case TNG_DOUBLE_DATA: + default: + size = sizeof(double); + } + + values = realloc(data->values, + size * (n_frames/data->stride_length) * + n_particles * n_values_per_frame); + if(!values) + { + printf("Cannot allocate memory (%"PRId64" bytes). %s: %d\n", + size * (n_frames/data->stride_length) * + n_particles * n_values_per_frame, + __FILE__, __LINE__); + free(data->values); + return(TNG_CRITICAL); + } + data->values = values; + } return(TNG_SUCCESS); } @@ -3866,7 +4251,7 @@ static tng_function_status tng_particle_data_read int64_t block_index, i, j, k, tot_n_particles; int size, len; unsigned long data_size; - union data_values **first_dim_values, *second_dim_values; + char ***first_dim_values, **second_dim_values; tng_particle_data_t data; tng_trajectory_frame_set_t frame_set = &tng_data->current_trajectory_frame_set; @@ -3958,6 +4343,8 @@ static tng_function_status tng_particle_data_read data->datatype = datatype; data->values = 0; + /* FIXME: Memory leak from strings. */ + data->strings = 0; data->n_frames = 0; data->codec_id = codec_id; data->compression_multiplier = multiplier; @@ -4020,70 +4407,11 @@ static tng_function_status tng_particle_data_read n_frames = tng_max(1, n_frames / stride_length); - /* FIXME: If not using a union to store data a whole dimension - * or the whole block can be read at once if byte swapping is not needed */ - switch(datatype) + if(datatype == TNG_CHAR_DATA) { - case TNG_FLOAT_DATA: - for(i = 0; i < n_frames; i++) - { - first_dim_values = data->values[i]; - for(j = num_first_particle; j < num_first_particle + n_particles; - j++) - { - second_dim_values = first_dim_values[j]; - for(k = 0; k < n_values; k++) - { - memcpy(&second_dim_values[k].f, - block->block_contents+*offset, - size); - if(tng_data->input_endianness_swap_func_32) - { - if(tng_data->input_endianness_swap_func_32(tng_data, - (int32_t *)&second_dim_values[k]) - != TNG_SUCCESS) - { - printf("Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } - } - *offset += size; - } - } - } - break; - case TNG_INT_DATA: for(i = 0; i < n_frames; i++) { - first_dim_values = data->values[i]; - for(j = num_first_particle; j < num_first_particle + n_particles; - j++) - { - second_dim_values = first_dim_values[j]; - for(k = 0; k < n_values; k++) - { - memcpy(&second_dim_values[k].i, - block->block_contents+*offset, - size); - if(tng_data->input_endianness_swap_func_64) - { - if(tng_data->input_endianness_swap_func_64(tng_data, - (int64_t *)&second_dim_values[k]) - != TNG_SUCCESS) - { - printf("Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } - } - *offset += size; - } - } - } - break; - case TNG_CHAR_DATA: - for(i = 0; i < n_frames; i++) - { - first_dim_values = data->values[i]; + first_dim_values = data->strings[i]; for(j = num_first_particle; j < num_first_particle + n_particles; j++) { @@ -4092,51 +4420,63 @@ static tng_function_status tng_particle_data_read { len = tng_min(strlen(block->block_contents+*offset) + 1, TNG_MAX_STR_LEN); - if(second_dim_values[k].c) + if(second_dim_values[k]) { - free(second_dim_values[k].c); + free(second_dim_values[k]); } - second_dim_values[k].c = malloc(len); - if(!second_dim_values[k].c) + second_dim_values[k] = malloc(len); + if(!second_dim_values[k]) { printf("Cannot allocate memory (%d bytes). %s: %d\n", len, __FILE__, __LINE__); return(TNG_CRITICAL); } - strncpy(second_dim_values[k].c, + strncpy(second_dim_values[k], block->block_contents+*offset, len); *offset += len; } } } - break; - case TNG_DOUBLE_DATA: - default: - for(i = 0; i < n_frames; i++) + } + else + { + memcpy(data->values, block->block_contents + *offset, + block->block_contents_size - *offset); + switch(datatype) { - first_dim_values = data->values[i]; - for(j = num_first_particle; j < num_first_particle + n_particles; - j++) + case TNG_FLOAT_DATA: + if(tng_data->input_endianness_swap_func_32) { - second_dim_values = first_dim_values[j]; - for(k = 0; k < n_values; k++) + for(i = 0; i < (block->block_contents_size - *offset); i+=size) { - memcpy(&second_dim_values[k].d, - block->block_contents+*offset, - size); - if(tng_data->input_endianness_swap_func_64) + if(tng_data->input_endianness_swap_func_32(tng_data, + (int32_t *)(data->values + i)) + != TNG_SUCCESS) { - if(tng_data->input_endianness_swap_func_64(tng_data, - (int64_t *)&second_dim_values[k]) - != TNG_SUCCESS) - { - printf("Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + printf("Cannot swap byte order. %s: %d\n", + __FILE__, __LINE__); + } + } + } + break; + case TNG_INT_DATA: + case TNG_DOUBLE_DATA: + if(tng_data->input_endianness_swap_func_64) + { + for(i = 0; i < (block->block_contents_size - *offset); i+=size) + { + if(tng_data->input_endianness_swap_func_64(tng_data, + (int64_t *)(data->values + i)) + != TNG_SUCCESS) + { + printf("Cannot swap byte order. %s: %d\n", + __FILE__, __LINE__); } - *offset += size; } } + break; + case TNG_CHAR_DATA: + break; } } return(TNG_SUCCESS); @@ -4163,9 +4503,8 @@ static tng_function_status tng_particle_data_block_write int64_t n_particles, num_first_particle, n_frames, stride_length; int i, j, k, offset = 0, size, len, data_start_pos; char dependency, temp, *temp_name; - double multiplier, d_temp; - float f_temp; - union data_values **first_dim_values, *second_dim_values; + double multiplier; + char ***first_dim_values, **second_dim_values; tng_trajectory_frame_set_t frame_set = &tng_data->current_trajectory_frame_set; @@ -4292,7 +4631,7 @@ static tng_function_status tng_particle_data_block_write { for(i = n_frames; i--;) { - first_dim_values = data->values[i]; + first_dim_values = data->strings[i]; for(j = num_first_particle; j < num_first_particle + n_particles; j++) { @@ -4300,7 +4639,7 @@ static tng_function_status tng_particle_data_block_write for(k = data->n_values_per_frame; k--;) { block->block_contents_size += - strlen(second_dim_values[k].c) + 1; + strlen(second_dim_values[k]) + 1; } } } @@ -4451,152 +4790,96 @@ static tng_function_status tng_particle_data_block_write } offset += sizeof(n_particles); - - if(data->values) + if(data->datatype == TNG_CHAR_DATA) { - /* FIXME: If not using a union to store data a whole dimension or the - * whole block can be written at once if byte swapping is not needed */ - switch(data->datatype) + if(data->strings) { - case TNG_FLOAT_DATA: - /* For speed reasons the compression multiplier is not used if the data - * is not compressed. */ - if(data->codec_id == TNG_UNCOMPRESSED) + for(i = 0; i < data->n_frames / stride_length; i++) { - for(i = 0; i < data->n_frames / stride_length; i++) + first_dim_values = data->strings[i]; + for(j = num_first_particle; j < num_first_particle + n_particles; + j++) { - first_dim_values = data->values[i]; - for(j = num_first_particle; j < num_first_particle + n_particles; - j++) + second_dim_values = first_dim_values[j]; + for(k = 0; k < data->n_values_per_frame; k++) { - second_dim_values = first_dim_values[j]; - for(k = 0; k < data->n_values_per_frame; k++) - { - memcpy(block->block_contents+offset, - &second_dim_values[k].f, - size); - if(tng_data->output_endianness_swap_func_32) - { - if(tng_data->output_endianness_swap_func_32(tng_data, - (int32_t *)block->header_contents+offset) - != TNG_SUCCESS) - { - printf("Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } - } - offset += size; - } + len = strlen(second_dim_values[k]) + 1; + strncpy(block->block_contents+offset, + second_dim_values[k], len); + offset += len; } } } - else + } + } + else if(data->values) + { + memcpy(block->block_contents + offset, data->values, + block->block_contents_size - offset); + switch(data->datatype) + { + case TNG_FLOAT_DATA: + if(data->codec_id == TNG_UNCOMPRESSED) { - multiplier = data->compression_multiplier; - for(i = 0; i < data->n_frames / stride_length; i++) + if(tng_data->input_endianness_swap_func_32) { - first_dim_values = data->values[i]; - for(j = num_first_particle; j < num_first_particle + n_particles; - j++) + for(i = 0; i < (block->block_contents_size - offset) / size; i++) { - second_dim_values = first_dim_values[j]; - for(k = 0; k < data->n_values_per_frame; k++) + if(tng_data->input_endianness_swap_func_32(tng_data, + (int32_t *)(block->block_contents + i)) + != TNG_SUCCESS) { - f_temp = second_dim_values[k].f * multiplier; - memcpy(block->block_contents+offset, - &f_temp, size); - if(tng_data->output_endianness_swap_func_32) - { - if(tng_data->output_endianness_swap_func_32(tng_data, - (int32_t *)block->header_contents+offset) - != TNG_SUCCESS) - { - printf("Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } - } - offset += size; + printf("Cannot swap byte order. %s: %d\n", + __FILE__, __LINE__); } } } } - break; - - case TNG_INT_DATA: - for(i = 0; i < data->n_frames / stride_length; i++) + else { - first_dim_values = data->values[i]; - for(j = num_first_particle; j < num_first_particle + n_particles; - j++) + multiplier = data->compression_multiplier; + for(i = 0; i < (block->block_contents_size - offset) / size; i++) { - second_dim_values = first_dim_values[j]; - for(k = 0; k < data->n_values_per_frame; k++) + *(float *)(block->block_contents + i) *= multiplier; + if(tng_data->input_endianness_swap_func_32 && + tng_data->input_endianness_swap_func_32(tng_data, + (int32_t *)(block->block_contents + i)) + != TNG_SUCCESS) { - memcpy(block->block_contents+offset, - &second_dim_values[k].i, - size); - if(tng_data->output_endianness_swap_func_64) - { - if(tng_data->output_endianness_swap_func_64(tng_data, - (int64_t *)block->header_contents+offset) - != TNG_SUCCESS) - { - printf("Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } - } - offset += size; + printf("Cannot swap byte order. %s: %d\n", + __FILE__, __LINE__); } } } break; - case TNG_CHAR_DATA: - for(i = 0; i < data->n_frames / stride_length; i++) + case TNG_INT_DATA: + if(tng_data->input_endianness_swap_func_64) { - first_dim_values = data->values[i]; - for(j = num_first_particle; j < num_first_particle + n_particles; - j++) + for(i = offset; i < block->block_contents_size; i+=size) { - second_dim_values = first_dim_values[j]; - for(k = 0; k < data->n_values_per_frame; k++) + if(tng_data->input_endianness_swap_func_64(tng_data, + (int64_t *)(block->block_contents + i)) + != TNG_SUCCESS) { - len = strlen(second_dim_values[k].c) + 1; - strncpy(block->block_contents+offset, - second_dim_values[k].c, len); - offset += len; + printf("Cannot swap byte order. %s: %d\n", + __FILE__, __LINE__); } } } break; case TNG_DOUBLE_DATA: - default: - /* For speed reasons the compression multiplier is not used if the data - * is not compressed.*/ if(data->codec_id == TNG_UNCOMPRESSED) { - for(i = 0; i < data->n_frames / stride_length; i++) + if(tng_data->input_endianness_swap_func_64) { - first_dim_values = data->values[i]; - for(j = num_first_particle; j < num_first_particle + n_particles; - j++) + for(i = offset; i < block->block_contents_size; i+=size) { - second_dim_values = first_dim_values[j]; - for(k = 0; k < data->n_values_per_frame; k++) + if(tng_data->input_endianness_swap_func_64(tng_data, + (int64_t *)(block->block_contents + i)) + != TNG_SUCCESS) { - memcpy(block->block_contents+offset, - &second_dim_values[k].d, - size); - if(tng_data->output_endianness_swap_func_64) - { - if(tng_data->output_endianness_swap_func_64(tng_data, - (int64_t *)block->header_contents+offset) - != TNG_SUCCESS) - { - printf("Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } - } - offset += size; + printf("Cannot swap byte order. %s: %d\n", + __FILE__, __LINE__); } } } @@ -4604,34 +4887,22 @@ static tng_function_status tng_particle_data_block_write else { multiplier = data->compression_multiplier; - for(i = 0; i < data->n_frames / stride_length; i++) + for(i = offset; i < block->block_contents_size; i+=size) { - first_dim_values = data->values[i]; - for(j = num_first_particle; j < num_first_particle + n_particles; - j++) + *(double *)(block->block_contents + i) *= multiplier; + if(tng_data->input_endianness_swap_func_64 && + tng_data->input_endianness_swap_func_64(tng_data, + (int64_t *)(block->block_contents + i)) + != TNG_SUCCESS) { - second_dim_values = first_dim_values[j]; - for(k = 0; k < data->n_values_per_frame; k++) - { - d_temp = second_dim_values[k].d * multiplier; - memcpy(block->block_contents+offset, - &d_temp, - size); - if(tng_data->output_endianness_swap_func_64) - { - if(tng_data->output_endianness_swap_func_64(tng_data, - (int64_t *)block->header_contents+offset) - != TNG_SUCCESS) - { - printf("Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } - } - offset += size; - } + printf("Cannot swap byte order. %s: %d\n", + __FILE__, __LINE__); } } } + break; + case TNG_CHAR_DATA: + break; } } else @@ -4754,63 +5025,79 @@ static tng_function_status tng_allocate_data_mem int64_t stride_length, const int64_t n_values_per_frame) { - union data_values **values; - int64_t i, j; + void **values; + int64_t i, j, size; - if(data->values) + if(data->strings && data->datatype == TNG_CHAR_DATA) { for(i = data->n_frames; i--;) { - if(data->datatype == TNG_CHAR_DATA) + for(j = data->n_values_per_frame; j--;) { - for(j = data->n_values_per_frame; j--;) + if(data->strings[i][j]) { - if(data->values[i][j].c) - { - free(data->values[i][j].c); - data->values[i][j].c = 0; - } + free(data->strings[i][j]); + data->strings[i][j] = 0; } } - free(data->values[i]); + free(data->strings[i]); + data->strings[i] = 0; } } data->n_frames = n_frames; data->stride_length = tng_max(1, stride_length); n_frames = tng_max(1, n_frames); data->n_values_per_frame = n_values_per_frame; - values = realloc(data->values, - sizeof(union data_values *) * - n_frames); - if(!values) - { - printf("Cannot allocate memory (%"PRId64" bytes). %s: %d\n", - sizeof(union data_values *) * n_frames, - __FILE__, __LINE__); - free(data->values); - return(TNG_CRITICAL); - } - data->values = values; - for(i = n_frames; i-- ;) + if(data->datatype == TNG_CHAR_DATA) { - data->values[i] = malloc(sizeof(union data_values) * - n_values_per_frame); - if(!data->values[i]) - { - printf("Cannot allocate memory (%"PRId64" bytes). %s: %d\n", - sizeof(union data_values) * n_values_per_frame, - __FILE__, __LINE__); - return(TNG_CRITICAL); - } - if(data->datatype == TNG_CHAR_DATA) + data->strings = malloc(sizeof(char **) * n_frames); + for(i = n_frames; i-- ;) { + data->strings[i] = malloc(sizeof(char *) * n_values_per_frame); + if(!data->strings[i]) + { + printf("Cannot allocate memory (%"PRId64" bytes). %s: %d\n", + n_values_per_frame, + __FILE__, __LINE__); + return(TNG_CRITICAL); + } for(j = n_values_per_frame; j--;) { - data->values[i][j].c = 0; + data->strings[i][j] = 0; } } } + else + { + switch(data->datatype) + { + case TNG_INT_DATA: + size = sizeof(int64_t); + break; + case TNG_FLOAT_DATA: + size = sizeof(float); + break; + case TNG_DOUBLE_DATA: + default: + size = sizeof(double); + } + + values = realloc(data->values, + size * (n_frames/data->stride_length) * + n_values_per_frame); + if(!values) + { + printf("Cannot allocate memory (%"PRId64" bytes). %s: %d\n", + size * (n_frames/data->stride_length) * + n_values_per_frame, + __FILE__, __LINE__); + free(data->values); + return(TNG_CRITICAL); + } + data->values = values; + } + return(TNG_SUCCESS); } @@ -4941,6 +5228,8 @@ static tng_function_status tng_data_read(tng_trajectory_t tng_data, data->datatype = datatype; data->values = 0; + /* FIXME: Memory leak from strings. */ + data->strings = 0; data->n_frames = 0; data->codec_id = codec_id; data->compression_multiplier = multiplier; @@ -4951,12 +5240,6 @@ static tng_function_status tng_data_read(tng_trajectory_t tng_data, data_size = (n_frames / stride_length) * size * n_values; switch(codec_id) { - case TNG_XTC_COMPRESSION: - printf("XTC compression not implemented yet.\n"); - break; - case TNG_TNG_COMPRESSION: - printf("TNG compression not implemented yet.\n"); - break; #ifdef USE_ZLIB case TNG_GZIP_COMPRESSION: // printf("Before compression: %"PRId64"\n", block->block_contents_size); @@ -4992,96 +5275,70 @@ static tng_function_status tng_data_read(tng_trajectory_t tng_data, n_frames = tng_max(1, n_frames / stride_length); - /* FIXME: If not using a union to store data a whole dimension - * or the whole block can be read at once if byte swapping is not needed */ - switch(datatype) + if(datatype == TNG_CHAR_DATA) { - case TNG_FLOAT_DATA: for(i = 0; i < n_frames; i++) { for(j = 0; j < n_values; j++) { - memcpy(&data->values[i][j].f, block->block_contents+*offset, - size); - if(tng_data->input_endianness_swap_func_32) + len = tng_min(strlen(block->block_contents+*offset) + 1, + TNG_MAX_STR_LEN); + if(data->strings[i][j]) { - if(tng_data->input_endianness_swap_func_32(tng_data, - (int32_t *)&data->values[i][j]) - != TNG_SUCCESS) - { - printf("Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + free(data->strings[i][j]); + } + data->strings[i][j] = malloc(len); + if(!data->strings[i][j]) + { + printf("Cannot allocate memory (%d bytes). %s: %d\n", + len, __FILE__, __LINE__); + return(TNG_CRITICAL); } - *offset += size; + strncpy(data->strings[i][j], block->block_contents+*offset, + len); + *offset += len; } } - break; - case TNG_INT_DATA: - for(i = 0; i < n_frames; i++) + } + else + { + memcpy(data->values, block->block_contents + *offset, + block->block_contents_size - *offset); + switch(datatype) { - for(j = 0; j < n_values; j++) + case TNG_FLOAT_DATA: + if(tng_data->input_endianness_swap_func_32) { - memcpy(&data->values[i][j].i, block->block_contents+*offset, - size); - if(tng_data->input_endianness_swap_func_64) + for(i = 0; i < (block->block_contents_size - *offset); i+=size) { - if(tng_data->input_endianness_swap_func_64(tng_data, - (int64_t *)&data->values[i][j]) + if(tng_data->input_endianness_swap_func_32(tng_data, + (int32_t *)(data->values + i)) != TNG_SUCCESS) { printf("Cannot swap byte order. %s: %d\n", __FILE__, __LINE__); } } - *offset += size; } - } - break; - case TNG_CHAR_DATA: - for(i = 0; i < n_frames; i++) - { - for(j = 0; j < n_values; j++) - { - len = tng_min(strlen(block->block_contents+*offset) + 1, - TNG_MAX_STR_LEN); - if(data->values[i][j].c) - { - free(data->values[i][j].c); - } - data->values[i][j].c = malloc(len); - if(!data->values[i][j].c) - { - printf("Cannot allocate memory (%d bytes). %s: %d\n", - len, __FILE__, __LINE__); - return(TNG_CRITICAL); - } - strncpy(data->values[i][j].c, block->block_contents+*offset, - len); - *offset += len; - } - } - break; - case TNG_DOUBLE_DATA: - default: - for(i = 0; i < n_frames; i++) - { - for(j = 0; j < n_values; j++) + break; + case TNG_INT_DATA: + case TNG_DOUBLE_DATA: + if(tng_data->input_endianness_swap_func_64) { - memcpy(&data->values[i][j].d, block->block_contents+*offset, - size); - if(tng_data->input_endianness_swap_func_64) + for(i = 0; i < (block->block_contents_size - *offset); i+=size) { if(tng_data->input_endianness_swap_func_64(tng_data, - (int64_t *)&data->values[i][j]) + (int64_t *)(data->values + i)) != TNG_SUCCESS) { printf("Cannot swap byte order. %s: %d\n", __FILE__, __LINE__); } } - *offset += size; } + break; + case TNG_CHAR_DATA: + break; } } return(TNG_SUCCESS); @@ -5105,8 +5362,7 @@ static tng_function_status tng_data_block_write(tng_trajectory_t tng_data, int64_t n_frames, stride_length; int i, j, offset = 0, size, len, data_start_pos; char temp, dependency, *temp_name; - double multiplier, d_temp; - float f_temp; + double multiplier; tng_trajectory_frame_set_t frame_set = &tng_data->current_trajectory_frame_set; @@ -5202,7 +5458,7 @@ static tng_function_status tng_data_block_write(tng_trajectory_t tng_data, { for(j = data->n_values_per_frame; j--;) { - block->block_contents_size += strlen(data->values[i][j].c) + 1; + block->block_contents_size += strlen(data->strings[i][j]) + 1; } } } @@ -5337,145 +5593,113 @@ static tng_function_status tng_data_block_write(tng_trajectory_t tng_data, offset += sizeof(data->stride_length); } - if(data->values) + if(data->datatype == TNG_CHAR_DATA) { - /* FIXME: If not using a union to store data a whole dimension or the - * whole block can be written at once if byte swapping is not needed */ + if(data->strings) + { + for(i = 0; i < n_frames / stride_length; i++) + { + for(j = 0; j < data->n_values_per_frame; j++) + { + len = strlen(data->strings[i][j]) + 1; + strncpy(block->block_contents+offset, data->strings[i][j], + len); + offset += len; + } + } + } + } + else if(data->values) + { + memcpy(block->block_contents + offset, data->values, + block->block_contents_size - offset); switch(data->datatype) { case TNG_FLOAT_DATA: - /* For speed reasons the compression multiplier is not used if the data - * is not compressed.*/ if(data->codec_id == TNG_UNCOMPRESSED) { - for(i = 0; i < n_frames / stride_length; i++) + if(tng_data->input_endianness_swap_func_32) { - for(j = 0; j < data->n_values_per_frame; j++) + for(i = 0; i < (block->block_contents_size - offset) / size; i++) { - memcpy(block->block_contents+offset, &data->values[i][j].f, - size); - if(tng_data->output_endianness_swap_func_32) + if(tng_data->input_endianness_swap_func_32(tng_data, + (int32_t *)(block->block_contents + i)) + != TNG_SUCCESS) { - if(tng_data->output_endianness_swap_func_32(tng_data, - (int32_t *)block->header_contents+offset) - != TNG_SUCCESS) - { - printf("Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + printf("Cannot swap byte order. %s: %d\n", + __FILE__, __LINE__); } - offset += size; } } } else { multiplier = data->compression_multiplier; - for(i = 0; i < n_frames / stride_length; i++) + for(i = 0; i < (block->block_contents_size - offset) / size; i++) { - for(j = 0; j < data->n_values_per_frame; j++) + *(float *)(block->block_contents + i) *= multiplier; + if(tng_data->input_endianness_swap_func_32 && + tng_data->input_endianness_swap_func_32(tng_data, + (int32_t *)(block->block_contents + i)) + != TNG_SUCCESS) { - f_temp = data->values[i][j].f * multiplier; - memcpy(block->block_contents+offset, &f_temp, - size); - if(tng_data->output_endianness_swap_func_32) - { - if(tng_data->output_endianness_swap_func_32(tng_data, - (int32_t *)block->header_contents+offset) - != TNG_SUCCESS) - { - printf("Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } - } - offset += size; + printf("Cannot swap byte order. %s: %d\n", + __FILE__, __LINE__); } } } break; case TNG_INT_DATA: - for(i = 0; i < n_frames / stride_length; i++) + if(tng_data->input_endianness_swap_func_64) { - for(j = 0; j < data->n_values_per_frame; j++) + for(i = offset; i < block->block_contents_size; i+=size) { - memcpy(block->block_contents+offset, &data->values[i][j].i, - size); - if(tng_data->output_endianness_swap_func_64) + if(tng_data->input_endianness_swap_func_64(tng_data, + (int64_t *)(block->block_contents + i)) + != TNG_SUCCESS) { - if(tng_data->output_endianness_swap_func_64(tng_data, - (int64_t *)block->header_contents+offset) - != TNG_SUCCESS) - { - printf("Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + printf("Cannot swap byte order. %s: %d\n", + __FILE__, __LINE__); } - offset += size; - } - } - break; - case TNG_CHAR_DATA: - for(i = 0; i < n_frames / stride_length; i++) - { - for(j = 0; j < data->n_values_per_frame; j++) - { - len = strlen(data->values[i][j].c) + 1; - strncpy(block->block_contents+offset, data->values[i][j].c, - len); - offset += len; } } break; case TNG_DOUBLE_DATA: - default: - /* For speed reasons the compression multiplier is not used if the data - * is not compressed.*/ if(data->codec_id == TNG_UNCOMPRESSED) { - for(i = 0; i < n_frames / stride_length; i++) + if(tng_data->input_endianness_swap_func_64) { - for(j = 0; j < data->n_values_per_frame; j++) + for(i = offset; i < block->block_contents_size; i+=size) { - memcpy(block->block_contents+offset, &data->values[i][j].d, - size); - if(tng_data->output_endianness_swap_func_64) + if(tng_data->input_endianness_swap_func_64(tng_data, + (int64_t *)(block->block_contents + i)) + != TNG_SUCCESS) { - if(tng_data->output_endianness_swap_func_64(tng_data, - (int64_t *)block->header_contents+offset) - != TNG_SUCCESS) - { - printf("Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } + printf("Cannot swap byte order. %s: %d\n", + __FILE__, __LINE__); } - offset += size; } } } else { multiplier = data->compression_multiplier; - for(i = 0; i < n_frames / stride_length; i++) + for(i = offset; i < block->block_contents_size; i+=size) { - for(j = 0; j < data->n_values_per_frame; j++) + *(double *)(block->block_contents + i) *= multiplier; + if(tng_data->input_endianness_swap_func_64 && + tng_data->input_endianness_swap_func_64(tng_data, + (int64_t *)(block->block_contents + i)) + != TNG_SUCCESS) { - d_temp = data->values[i][j].d * multiplier; - memcpy(block->block_contents+offset, &d_temp, - size); - if(tng_data->output_endianness_swap_func_64) - { - if(tng_data->output_endianness_swap_func_64(tng_data, - (int64_t *)block->header_contents+offset) - != TNG_SUCCESS) - { - printf("Cannot swap byte order. %s: %d\n", - __FILE__, __LINE__); - } - } - offset += size; + printf("Cannot swap byte order. %s: %d\n", + __FILE__, __LINE__); } } } + break; + case TNG_CHAR_DATA: + break; } } else @@ -5485,12 +5709,6 @@ static tng_function_status tng_data_block_write(tng_trajectory_t tng_data, switch(data->codec_id) { - case TNG_XTC_COMPRESSION: - printf("XTC compression not implemented yet.\n"); - break; - case TNG_TNG_COMPRESSION: - printf("TNG compression not implemented yet.\n"); - break; #ifdef USE_ZLIB case TNG_GZIP_COMPRESSION: // printf("Before compression: %"PRId64"\n", block->block_contents_size); @@ -6386,8 +6604,8 @@ static tng_function_status tng_atom_destroy(tng_atom_t atom) } tng_function_status DECLSPECDLLEXPORT tng_molecule_add(tng_trajectory_t tng_data, - const char *name, - tng_molecule_t *molecule) + const char *name, + tng_molecule_t *molecule) { tng_molecule_t new_molecules; int64_t *new_molecule_cnt_list; @@ -6458,8 +6676,8 @@ tng_function_status DECLSPECDLLEXPORT tng_molecule_add(tng_trajectory_t tng_data } tng_function_status DECLSPECDLLEXPORT tng_molecule_name_set(tng_trajectory_t tng_data, - tng_molecule_t molecule, - const char *new_name) + tng_molecule_t molecule, + const char *new_name) { int len; @@ -6489,8 +6707,8 @@ tng_function_status DECLSPECDLLEXPORT tng_molecule_name_set(tng_trajectory_t tng } tng_function_status DECLSPECDLLEXPORT tng_molecule_cnt_get(tng_trajectory_t tng_data, - tng_molecule_t molecule, - int64_t *cnt) + tng_molecule_t molecule, + int64_t *cnt) { int i, index = -1; @@ -6512,8 +6730,8 @@ tng_function_status DECLSPECDLLEXPORT tng_molecule_cnt_get(tng_trajectory_t tng_ } tng_function_status DECLSPECDLLEXPORT tng_molecule_cnt_set(tng_trajectory_t tng_data, - tng_molecule_t molecule, - const int64_t cnt) + tng_molecule_t molecule, + const int64_t cnt) { int i, index = -1, old_cnt; @@ -6539,10 +6757,10 @@ tng_function_status DECLSPECDLLEXPORT tng_molecule_cnt_set(tng_trajectory_t tng_ } tng_function_status DECLSPECDLLEXPORT tng_molecule_chain_find(tng_trajectory_t tng_data, - tng_molecule_t molecule, - const char *name, - int64_t nr, - tng_chain_t *chain) + tng_molecule_t molecule, + const char *name, + int64_t nr, + tng_chain_t *chain) { int i, n_chains; @@ -6567,9 +6785,9 @@ tng_function_status DECLSPECDLLEXPORT tng_molecule_chain_find(tng_trajectory_t t /* FIXME: For v. 2 the chain nr should also be possible to specify. */ tng_function_status DECLSPECDLLEXPORT tng_molecule_chain_add(tng_trajectory_t tng_data, - tng_molecule_t molecule, - const char *name, - tng_chain_t *chain) + tng_molecule_t molecule, + const char *name, + tng_chain_t *chain) { tng_chain_t new_chains; @@ -6604,8 +6822,8 @@ tng_function_status DECLSPECDLLEXPORT tng_molecule_chain_add(tng_trajectory_t tn } tng_function_status DECLSPECDLLEXPORT tng_chain_name_set(tng_trajectory_t tng_data, - tng_chain_t chain, - const char *new_name) + tng_chain_t chain, + const char *new_name) { int len; @@ -6635,10 +6853,10 @@ tng_function_status DECLSPECDLLEXPORT tng_chain_name_set(tng_trajectory_t tng_da } tng_function_status DECLSPECDLLEXPORT tng_chain_residue_find(tng_trajectory_t tng_data, - tng_chain_t chain, - const char *name, - int64_t nr, - tng_residue_t *residue) + tng_chain_t chain, + const char *name, + int64_t nr, + tng_residue_t *residue) { int i, n_residues; @@ -6663,9 +6881,9 @@ tng_function_status DECLSPECDLLEXPORT tng_chain_residue_find(tng_trajectory_t tn /* FIXME: For v. 2 the residue nr should also be possible to specify. */ tng_function_status DECLSPECDLLEXPORT tng_chain_residue_add(tng_trajectory_t tng_data, - tng_chain_t chain, - const char *name, - tng_residue_t *residue) + tng_chain_t chain, + const char *name, + tng_residue_t *residue) { int curr_index; tng_residue_t new_residues, temp_residue, last_residue; @@ -6741,8 +6959,8 @@ tng_function_status DECLSPECDLLEXPORT tng_chain_residue_add(tng_trajectory_t tng } tng_function_status DECLSPECDLLEXPORT tng_residue_name_set(tng_trajectory_t tng_data, - tng_residue_t residue, - const char *new_name) + tng_residue_t residue, + const char *new_name) { int len; @@ -6773,10 +6991,10 @@ tng_function_status DECLSPECDLLEXPORT tng_residue_name_set(tng_trajectory_t tng_ /* FIXME: For v. 2 the atom nr should also be possible to specify. */ tng_function_status DECLSPECDLLEXPORT tng_residue_atom_add(tng_trajectory_t tng_data, - tng_residue_t residue, - const char *atom_name, - const char *atom_type, - tng_atom_t *atom) + tng_residue_t residue, + const char *atom_name, + const char *atom_type, + tng_atom_t *atom) { tng_atom_t new_atoms; tng_molecule_t molecule = residue->chain->molecule; @@ -6819,7 +7037,7 @@ tng_function_status DECLSPECDLLEXPORT tng_residue_atom_add(tng_trajectory_t tng_ tng_function_status DECLSPECDLLEXPORT tng_molecule_init(const tng_trajectory_t tng_data, - tng_molecule_t molecule) + tng_molecule_t molecule) { molecule->quaternary_str = 1; molecule->name = 0; @@ -6836,7 +7054,7 @@ tng_function_status DECLSPECDLLEXPORT tng_molecule_init(const tng_trajectory_t t } tng_function_status DECLSPECDLLEXPORT tng_molecule_destroy(const tng_trajectory_t tng_data, - tng_molecule_t molecule) + tng_molecule_t molecule) { int i; @@ -6899,9 +7117,9 @@ tng_function_status DECLSPECDLLEXPORT tng_molecule_destroy(const tng_trajectory_ tng_function_status DECLSPECDLLEXPORT tng_molecule_name_of_particle_nr_get (const tng_trajectory_t tng_data, - const int64_t nr, - char *name, - int max_len) + const int64_t nr, + char *name, + int max_len) { int64_t cnt = 0, i, *molecule_cnt_list; tng_molecule_t mol; @@ -7290,6 +7508,9 @@ tng_function_status DECLSPECDLLEXPORT tng_trajectory_init(tng_trajectory_t *tng_ tng_data->non_tr_particle_data = 0; tng_data->non_tr_data = 0; + tng_data->compress_algo_pos = 0; + tng_data->compress_algo_vel = 0; + frame_set->first_frame = -1; frame_set->n_mapping_blocks = 0; frame_set->mappings = 0; @@ -7320,54 +7541,54 @@ tng_function_status DECLSPECDLLEXPORT tng_trajectory_init(tng_trajectory_t *tng_ static int32_t endianness_32 = 0x01234567; /* 0x01234567 */ if ( *(const uint8_t*)&endianness_32 == 0x01 ) - { - tng_data->endianness_32 = TNG_BIG_ENDIAN_32; - } + { + tng_data->endianness_32 = TNG_BIG_ENDIAN_32; + } /* 0x67452301 */ else if( *(const uint8_t*)&endianness_32 == 0x67 ) - { - tng_data->endianness_32 = TNG_LITTLE_ENDIAN_32; - - } - + { + tng_data->endianness_32 = TNG_LITTLE_ENDIAN_32; + + } + /* 0x45670123 */ else if ( *(const uint8_t*)&endianness_32 == 0x45 ) - { - tng_data->endianness_32 = TNG_BYTE_PAIR_SWAP_32; - } + { + tng_data->endianness_32 = TNG_BYTE_PAIR_SWAP_32; + } } { static int64_t endianness_64 = 0x0123456789ABCDEF; /* 0x0123456789ABCDEF */ if ( *(const uint8_t*)&endianness_64 == 0x01 ) - { - tng_data->endianness_64 = TNG_BIG_ENDIAN_64; - } + { + tng_data->endianness_64 = TNG_BIG_ENDIAN_64; + } /* 0xEFCDAB8967452301 */ else if ( *(const uint8_t*)&endianness_64 == 0xEF ) - { - tng_data->endianness_64 = TNG_LITTLE_ENDIAN_64; - } + { + tng_data->endianness_64 = TNG_LITTLE_ENDIAN_64; + } /* 0x89ABCDEF01234567 */ else if ( *(const uint8_t*)&endianness_64 == 0x89 ) - { - tng_data->endianness_64 = TNG_QUAD_SWAP_64; - } + { + tng_data->endianness_64 = TNG_QUAD_SWAP_64; + } /* 0x45670123CDEF89AB */ else if ( *(const uint8_t*)&endianness_64 == 0x45 ) - { - tng_data->endianness_64 = TNG_BYTE_PAIR_SWAP_64; - } + { + tng_data->endianness_64 = TNG_BYTE_PAIR_SWAP_64; + } /* 0x23016745AB89EFCD */ else if ( *(const uint8_t*)&endianness_64 == 0x23 ) - { - tng_data->endianness_64 = TNG_BYTE_SWAP_64; - } + { + tng_data->endianness_64 = TNG_BYTE_SWAP_64; + } } /* By default do not swap the byte order, i.e. keep the byte order of the @@ -7388,8 +7609,8 @@ tng_function_status DECLSPECDLLEXPORT tng_trajectory_init(tng_trajectory_t *tng_ tng_function_status DECLSPECDLLEXPORT tng_trajectory_destroy(tng_trajectory_t *tng_data_p) { - int64_t n_particles; - int i; + int i, j, k, l; + int64_t n_particles, n_values_per_frame; tng_trajectory_t tng_data = *tng_data_p; tng_trajectory_frame_set_t frame_set = &tng_data->current_trajectory_frame_set; @@ -7505,7 +7726,7 @@ tng_function_status DECLSPECDLLEXPORT tng_trajectory_destroy(tng_trajectory_t *t if(tng_data->var_num_atoms_flag) { - n_particles = tng_data->current_trajectory_frame_set.n_particles; + n_particles = frame_set->n_particles; } else { @@ -7516,14 +7737,44 @@ tng_function_status DECLSPECDLLEXPORT tng_trajectory_destroy(tng_trajectory_t *t { for(i = tng_data->n_particle_data_blocks; i--; ) { - tng_particle_data_values_free(tng_data, - tng_data->non_tr_particle_data[i]. - values, 1, - n_particles, - tng_data->non_tr_particle_data[i]. - n_values_per_frame, - tng_data->non_tr_particle_data[i]. - datatype); + if(tng_data->non_tr_particle_data[i].values) + { + free(tng_data->non_tr_particle_data[i].values); + tng_data->non_tr_particle_data[i].values = 0; + } + + if(tng_data->non_tr_particle_data[i].strings) + { + n_values_per_frame = tng_data->non_tr_particle_data[i]. + n_values_per_frame; + if(tng_data->non_tr_particle_data[i].strings[0]) + { + for(j = n_particles; j--;) + { + if(tng_data->non_tr_particle_data[i].strings[0][j]) + { + for(k = n_values_per_frame; k--;) + { + if(tng_data->non_tr_particle_data[i]. + strings[0][j][k]) + { + free(tng_data->non_tr_particle_data[i]. + strings[0][j][k]); + tng_data->non_tr_particle_data[i]. + strings[0][j][k] = 0; + } + } + free(tng_data->non_tr_particle_data[i]. + strings[0][j]); + tng_data->non_tr_particle_data[i].strings[0][j] = 0; + } + } + free(tng_data->non_tr_particle_data[i].strings[0]); + tng_data->non_tr_particle_data[i].strings[0] = 0; + } + free(tng_data->non_tr_particle_data[i].strings); + tng_data->non_tr_particle_data[i].strings = 0; + } if(tng_data->non_tr_particle_data[i].block_name) { @@ -7539,10 +7790,32 @@ tng_function_status DECLSPECDLLEXPORT tng_trajectory_destroy(tng_trajectory_t *t { for(i = tng_data->n_data_blocks; i--;) { - tng_data_values_free(tng_data, - tng_data->non_tr_data[i].values, 1, - tng_data->non_tr_data[i].n_values_per_frame, - tng_data->non_tr_data[i].datatype); + if(tng_data->non_tr_data[i].values) + { + free(tng_data->non_tr_data[i].values); + tng_data->non_tr_data[i].values = 0; + } + + if(tng_data->non_tr_data[i].strings) + { + n_values_per_frame = tng_data->non_tr_data[i]. + n_values_per_frame; + if(tng_data->non_tr_data[i].strings[0]) + { + for(j = n_values_per_frame; j--;) + { + if(tng_data->non_tr_data[i].strings[0][j]) + { + free(tng_data->non_tr_data[i].strings[0][j]); + tng_data->non_tr_data[i].strings[0][j] = 0; + } + } + free(tng_data->non_tr_data[i].strings[0]); + tng_data->non_tr_data[i].strings[0] = 0; + } + free(tng_data->non_tr_data[i].strings); + tng_data->non_tr_data[i].strings = 0; + } if(tng_data->non_tr_data[i].block_name) { @@ -7557,20 +7830,64 @@ tng_function_status DECLSPECDLLEXPORT tng_trajectory_destroy(tng_trajectory_t *t tng_data->n_particle_data_blocks = 0; tng_data->n_data_blocks = 0; + if(tng_data->compress_algo_pos) + { + free(tng_data->compress_algo_pos); + tng_data->compress_algo_pos = 0; + } + if(tng_data->compress_algo_vel) + { + free(tng_data->compress_algo_vel); + tng_data->compress_algo_vel = 0; + } + if(frame_set->tr_particle_data) { for(i = frame_set->n_particle_data_blocks; i--; ) { - tng_particle_data_values_free(tng_data, - frame_set->tr_particle_data[i]. - values, - frame_set->tr_particle_data[i]. - n_frames, - n_particles, - frame_set->tr_particle_data[i]. - n_values_per_frame, - frame_set->tr_particle_data[i]. - datatype); + if(frame_set->tr_particle_data[i].values) + { + free(frame_set->tr_particle_data[i].values); + frame_set->tr_particle_data[i].values = 0; + } + + if(frame_set->tr_particle_data[i].strings) + { + n_values_per_frame = frame_set->tr_particle_data[i]. + n_values_per_frame; + for(j = frame_set->tr_particle_data[i].n_frames; j--;) + { + if(frame_set->tr_particle_data[i].strings[j]) + { + for(k = n_particles; k--;) + { + if(frame_set->tr_particle_data[i]. + strings[j][k]) + { + for(l = n_values_per_frame; l--;) + { + if(frame_set->tr_particle_data[i]. + strings[j][k][l]) + { + free(frame_set->tr_particle_data[i]. + strings[j][k][l]); + frame_set->tr_particle_data[i]. + strings[j][k][l] = 0; + } + } + free(frame_set->tr_particle_data[i]. + strings[j][k]); + frame_set->tr_particle_data[i]. + strings[j][k] = 0; + } + } + free(frame_set->tr_particle_data[i].strings[j]); + frame_set->tr_particle_data[i].strings[j] = 0; + } + } + free(frame_set->tr_particle_data[i].strings); + frame_set->tr_particle_data[i].strings = 0; + } if(frame_set->tr_particle_data[i].block_name) { @@ -7586,12 +7903,36 @@ tng_function_status DECLSPECDLLEXPORT tng_trajectory_destroy(tng_trajectory_t *t { for(i = frame_set->n_data_blocks; i--;) { - tng_data_values_free(tng_data, - frame_set->tr_data[i].values, - frame_set->tr_data[i].n_frames, - frame_set->tr_data[i]. - n_values_per_frame, - frame_set->tr_data[i].datatype); + if(frame_set->tr_data[i].values) + { + free(frame_set->tr_data[i].values); + frame_set->tr_data[i].values = 0; + } + + if(frame_set->tr_data[i].strings) + { + n_values_per_frame = frame_set->tr_data[i]. + n_values_per_frame; + for(j = frame_set->tr_data[i].n_frames; j--;) + { + if(frame_set->tr_data[i].strings[j]) + { + for(k = n_values_per_frame; k--;) + { + if(frame_set->tr_data[i].strings[j][k]) + { + free(frame_set->tr_data[i].strings[j][k]); + frame_set->tr_data[i].strings[j][k] = 0; + } + } + free(frame_set->tr_data[i].strings[j]); + frame_set->tr_data[i].strings[j] = 0; + } + } + free(frame_set->tr_data[i].strings); + frame_set->tr_data[i].strings = 0; + } + if(frame_set->tr_data[i].block_name) { free(frame_set->tr_data[i].block_name); @@ -7702,6 +8043,9 @@ tng_function_status DECLSPECDLLEXPORT tng_trajectory_init_from_src(tng_trajector dest->non_tr_particle_data = 0; dest->non_tr_data = 0; + dest->compress_algo_pos = 0; + dest->compress_algo_vel = 0; + frame_set->first_frame = -1; frame_set->n_mapping_blocks = 0; frame_set->mappings = 0; @@ -7753,7 +8097,7 @@ tng_function_status DECLSPECDLLEXPORT tng_input_file_get(const tng_trajectory_t } tng_function_status DECLSPECDLLEXPORT tng_input_file_set(tng_trajectory_t tng_data, - const char *file_name) + const char *file_name) { int len; char *temp; @@ -7799,7 +8143,7 @@ tng_function_status tng_output_file_get(const tng_trajectory_t tng_data, } tng_function_status DECLSPECDLLEXPORT tng_output_file_set(tng_trajectory_t tng_data, - const char *file_name) + const char *file_name) { int len; char *temp; @@ -7968,7 +8312,7 @@ tng_function_status DECLSPECDLLEXPORT tng_output_file_endianness_set tng_function_status DECLSPECDLLEXPORT tng_first_program_name_get (const tng_trajectory_t tng_data, - char *name, const int max_len) + char *name, const int max_len) { strncpy(name, tng_data->first_program_name, max_len - 1); name[max_len - 1] = 0; @@ -7981,7 +8325,7 @@ tng_function_status DECLSPECDLLEXPORT tng_first_program_name_get } tng_function_status DECLSPECDLLEXPORT tng_first_program_name_set(tng_trajectory_t tng_data, - const char *new_name) + const char *new_name) { int len; @@ -8010,7 +8354,7 @@ tng_function_status DECLSPECDLLEXPORT tng_first_program_name_set(tng_trajectory_ tng_function_status DECLSPECDLLEXPORT tng_last_program_name_get (const tng_trajectory_t tng_data, - char *name, const int max_len) + char *name, const int max_len) { strncpy(name, tng_data->last_program_name, max_len - 1); name[max_len - 1] = 0; @@ -8024,7 +8368,7 @@ tng_function_status DECLSPECDLLEXPORT tng_last_program_name_get tng_function_status DECLSPECDLLEXPORT tng_last_program_name_set (tng_trajectory_t tng_data, - const char *new_name) + const char *new_name) { int len; @@ -8053,7 +8397,7 @@ tng_function_status DECLSPECDLLEXPORT tng_last_program_name_set tng_function_status DECLSPECDLLEXPORT tng_first_user_name_get (const tng_trajectory_t tng_data, - char *name, const int max_len) + char *name, const int max_len) { strncpy(name, tng_data->first_user_name, max_len - 1); name[max_len - 1] = 0; @@ -8067,7 +8411,7 @@ tng_function_status DECLSPECDLLEXPORT tng_first_user_name_get tng_function_status DECLSPECDLLEXPORT tng_first_user_name_set (tng_trajectory_t tng_data, - const char *new_name) + const char *new_name) { int len; @@ -8098,7 +8442,7 @@ tng_function_status DECLSPECDLLEXPORT tng_first_user_name_set tng_function_status DECLSPECDLLEXPORT tng_last_user_name_get (const tng_trajectory_t tng_data, - char *name, const int max_len) + char *name, const int max_len) { strncpy(name, tng_data->last_user_name, max_len - 1); name[max_len - 1] = 0; @@ -8112,7 +8456,7 @@ tng_function_status DECLSPECDLLEXPORT tng_last_user_name_get tng_function_status DECLSPECDLLEXPORT tng_last_user_name_set (tng_trajectory_t tng_data, - const char *new_name) + const char *new_name) { int len; @@ -8143,7 +8487,7 @@ tng_function_status DECLSPECDLLEXPORT tng_last_user_name_set tng_function_status DECLSPECDLLEXPORT tng_first_computer_name_get (const tng_trajectory_t tng_data, - char *name, const int max_len) + char *name, const int max_len) { strncpy(name, tng_data->first_computer_name, max_len - 1); name[max_len - 1] = 0; @@ -8157,7 +8501,7 @@ tng_function_status DECLSPECDLLEXPORT tng_first_computer_name_get tng_function_status DECLSPECDLLEXPORT tng_first_computer_name_set (tng_trajectory_t tng_data, - const char *new_name) + const char *new_name) { int len; @@ -8188,7 +8532,7 @@ tng_function_status DECLSPECDLLEXPORT tng_first_computer_name_set tng_function_status DECLSPECDLLEXPORT tng_last_computer_name_get (const tng_trajectory_t tng_data, - char *name, const int max_len) + char *name, const int max_len) { strncpy(name, tng_data->last_computer_name, max_len - 1); name[max_len - 1] = 0; @@ -8202,7 +8546,7 @@ tng_function_status DECLSPECDLLEXPORT tng_last_computer_name_get tng_function_status DECLSPECDLLEXPORT tng_last_computer_name_set (tng_trajectory_t tng_data, - const char *new_name) + const char *new_name) { int len; @@ -8234,7 +8578,7 @@ tng_function_status DECLSPECDLLEXPORT tng_last_computer_name_set tng_function_status DECLSPECDLLEXPORT tng_first_signature_get (const tng_trajectory_t tng_data, - char *signature, const int max_len) + char *signature, const int max_len) { strncpy(signature, tng_data->first_pgp_signature, max_len - 1); signature[max_len - 1] = 0; @@ -8248,7 +8592,7 @@ tng_function_status DECLSPECDLLEXPORT tng_first_signature_get tng_function_status DECLSPECDLLEXPORT tng_first_signature_set (tng_trajectory_t tng_data, - const char *signature) + const char *signature) { int len; @@ -8280,7 +8624,7 @@ tng_function_status DECLSPECDLLEXPORT tng_first_signature_set tng_function_status DECLSPECDLLEXPORT tng_last_signature_get (const tng_trajectory_t tng_data, - char *signature, const int max_len) + char *signature, const int max_len) { strncpy(signature, tng_data->last_pgp_signature, max_len - 1); signature[max_len - 1] = 0; @@ -8294,7 +8638,7 @@ tng_function_status DECLSPECDLLEXPORT tng_last_signature_get tng_function_status DECLSPECDLLEXPORT tng_last_signature_set (tng_trajectory_t tng_data, - const char *signature) + const char *signature) { int len; @@ -8326,7 +8670,7 @@ tng_function_status DECLSPECDLLEXPORT tng_last_signature_set tng_function_status DECLSPECDLLEXPORT tng_forcefield_name_get (const tng_trajectory_t tng_data, - char *name, const int max_len) + char *name, const int max_len) { strncpy(name, tng_data->forcefield_name, max_len - 1); name[max_len - 1] = 0; @@ -8340,7 +8684,7 @@ tng_function_status DECLSPECDLLEXPORT tng_forcefield_name_get tng_function_status DECLSPECDLLEXPORT tng_forcefield_name_set (tng_trajectory_t tng_data, - const char *new_name) + const char *new_name) { int len; @@ -8371,7 +8715,7 @@ tng_function_status DECLSPECDLLEXPORT tng_forcefield_name_set tng_function_status DECLSPECDLLEXPORT tng_medium_stride_length_get (const tng_trajectory_t tng_data, - int64_t *len) + int64_t *len) { *len = tng_data->medium_stride_length; @@ -8380,7 +8724,7 @@ tng_function_status DECLSPECDLLEXPORT tng_medium_stride_length_get tng_function_status DECLSPECDLLEXPORT tng_medium_stride_length_set (tng_trajectory_t tng_data, - const int64_t len) + const int64_t len) { if(len >= tng_data->long_stride_length) { @@ -8393,7 +8737,7 @@ tng_function_status DECLSPECDLLEXPORT tng_medium_stride_length_set tng_function_status DECLSPECDLLEXPORT tng_long_stride_length_get (const tng_trajectory_t tng_data, - int64_t *len) + int64_t *len) { *len = tng_data->long_stride_length; @@ -8402,7 +8746,7 @@ tng_function_status DECLSPECDLLEXPORT tng_long_stride_length_get tng_function_status DECLSPECDLLEXPORT tng_long_stride_length_set (tng_trajectory_t tng_data, - const int64_t len) + const int64_t len) { if(len <= tng_data->medium_stride_length) { @@ -8415,7 +8759,7 @@ tng_function_status DECLSPECDLLEXPORT tng_long_stride_length_set tng_function_status DECLSPECDLLEXPORT tng_input_file_len_get (const tng_trajectory_t tng_data, - int64_t *len) + int64_t *len) { *len = tng_data->input_file_len; @@ -8424,7 +8768,7 @@ tng_function_status DECLSPECDLLEXPORT tng_input_file_len_get tng_function_status DECLSPECDLLEXPORT tng_num_frames_get (const tng_trajectory_t tng_data, - int64_t *n) + int64_t *n) { tng_gen_block_t block; tng_function_status stat; @@ -8469,7 +8813,7 @@ tng_function_status DECLSPECDLLEXPORT tng_num_frames_get tng_function_status DECLSPECDLLEXPORT tng_num_particles_get (const tng_trajectory_t tng_data, - int64_t *n) + int64_t *n) { if(tng_data->var_num_atoms_flag == TNG_CONSTANT_N_ATOMS) { @@ -8485,7 +8829,7 @@ tng_function_status DECLSPECDLLEXPORT tng_num_particles_get tng_function_status DECLSPECDLLEXPORT tng_num_molecules_get (const tng_trajectory_t tng_data, - int64_t *n) + int64_t *n) { int64_t *cnt_list, cnt = 0, i; @@ -8528,7 +8872,7 @@ tng_function_status DECLSPECDLLEXPORT tng_num_frames_per_frame_set_set tng_function_status DECLSPECDLLEXPORT tng_num_frame_sets_get (const tng_trajectory_t tng_data, - int64_t *n) + int64_t *n) { int64_t long_stride_length, medium_stride_length; int64_t file_pos; @@ -8677,7 +9021,7 @@ tng_function_status DECLSPECDLLEXPORT tng_current_frame_set_get tng_function_status DECLSPECDLLEXPORT tng_frame_set_nr_find (tng_trajectory_t tng_data, - const int64_t nr) + const int64_t nr) { int64_t long_stride_length, medium_stride_length; int64_t file_pos, curr_nr = 0, n_frame_sets; @@ -8992,7 +9336,7 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_set_nr_find tng_function_status DECLSPECDLLEXPORT tng_frame_set_of_frame_find (tng_trajectory_t tng_data, - const int64_t frame) + const int64_t frame) { int64_t first_frame, last_frame, n_frames_per_frame_set; int64_t long_stride_length, medium_stride_length; @@ -9738,8 +10082,8 @@ tng_function_status tng_frame_set_write(tng_trajectory_t tng_data, tng_function_status DECLSPECDLLEXPORT tng_frame_set_new (tng_trajectory_t tng_data, - const int64_t first_frame, - const int64_t n_frames) + const int64_t first_frame, + const int64_t n_frames) { int i; tng_gen_block_t block; @@ -9931,21 +10275,21 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_set_new tng_function_status DECLSPECDLLEXPORT tng_data_block_add (tng_trajectory_t tng_data, - const int64_t id, - const char *block_name, - const tng_data_type datatype, - const tng_block_type block_type_flag, - int64_t n_frames, - const int64_t n_values_per_frame, - int64_t stride_length, - const int64_t codec_id, - void *new_data) + const int64_t id, + const char *block_name, + const tng_data_type datatype, + const tng_block_type block_type_flag, + int64_t n_frames, + const int64_t n_values_per_frame, + int64_t stride_length, + const int64_t codec_id, + void *new_data) { int i, j, block_index, size, len; tng_trajectory_frame_set_t frame_set; tng_non_particle_data_t data; - union data_values *first_dim_values; - char *orig; + char **first_dim_values; + void *orig; char *new_data_c=new_data; frame_set = &tng_data->current_trajectory_frame_set; @@ -10016,6 +10360,8 @@ tng_function_status DECLSPECDLLEXPORT tng_data_block_add data->stride_length = tng_max(stride_length, 1); data->values = 0; + /* FIXME: Memory leak from strings. */ + data->strings = 0; data->n_values_per_frame = n_values_per_frame; data->n_frames = n_frames; data->codec_id = codec_id; @@ -10030,8 +10376,6 @@ tng_function_status DECLSPECDLLEXPORT tng_data_block_add case TNG_INT_DATA: size = sizeof(int64_t); break; - case TNG_CHAR_DATA: - size = sizeof(char); case TNG_DOUBLE_DATA: default: size = sizeof(double); @@ -10057,105 +10401,59 @@ tng_function_status DECLSPECDLLEXPORT tng_data_block_add frame_set->n_written_frames = n_frames; } - switch(datatype) + if(datatype == TNG_CHAR_DATA) { - case TNG_FLOAT_DATA: for(i = 0; i < n_frames / stride_length; i++) { - first_dim_values = data->values[i]; - for(j = 0; j < n_values_per_frame; j++) - { - memcpy(&first_dim_values[j].f, - new_data_c, size); - new_data_c += size; - } - } - break; - case TNG_CHAR_DATA: - for(i = 0; i < n_frames / stride_length; i++) - { - first_dim_values = data->values[i]; + first_dim_values = data->strings[i]; for(j = 0; j < n_values_per_frame; j++) { len = tng_min(strlen(new_data_c) + 1, TNG_MAX_STR_LEN); - if(first_dim_values[j].c) + if(first_dim_values[j]) { - free(first_dim_values[j].c); + free(first_dim_values[j]); } - first_dim_values[j].c = malloc(len); - if(!first_dim_values[j].c) + first_dim_values[j] = malloc(len); + if(!first_dim_values[j]) { printf("Cannot allocate memory (%d bytes). %s: %d\n", len, __FILE__, __LINE__); return(TNG_CRITICAL); } - strncpy(first_dim_values[j].c, + strncpy(first_dim_values[j], new_data_c, len); - new_data_c += len; - } - } - break; - case TNG_INT_DATA: - for(i = 0; i < n_frames / stride_length; i++) - { - first_dim_values = data->values[i]; - for(j = 0; j < n_values_per_frame; j++) - { - memcpy(&first_dim_values[j].i, - new_data_c, size); - new_data_c += size; - } - } - break; - case TNG_DOUBLE_DATA: - default: - for(i = 0; i < n_frames / stride_length; i++) - { - first_dim_values = data->values[i]; - for(j = 0; j < n_values_per_frame; j++) - { - memcpy(&first_dim_values[j].d, - new_data_c, size); - new_data_c += size; + new_data += len; } } } - - new_data_c = orig; + else + { + memcpy(data->values, new_data, size * n_frames / stride_length * + n_values_per_frame); + } } -// else -// { -// for(i = 0; i < n_frames / stride_length; i++) -// { -// first_dim_values = data->values[i]; -// for(j = 0; j < n_values_per_frame; j++) -// { -// first_dim_values[j].d = 0; -// } -// } -// } return(TNG_SUCCESS); } tng_function_status DECLSPECDLLEXPORT tng_particle_data_block_add (tng_trajectory_t tng_data, - const int64_t id, - const char *block_name, - const tng_data_type datatype, - const tng_block_type block_type_flag, - int64_t n_frames, - const int64_t n_values_per_frame, - int64_t stride_length, - const int64_t num_first_particle, - const int64_t n_particles, - const int64_t codec_id, - void *new_data) + const int64_t id, + const char *block_name, + const tng_data_type datatype, + const tng_block_type block_type_flag, + int64_t n_frames, + const int64_t n_values_per_frame, + int64_t stride_length, + const int64_t num_first_particle, + const int64_t n_particles, + const int64_t codec_id, + void *new_data) { int i, j, k, block_index, size, len; int64_t tot_n_particles; - union data_values **first_dim_values, *second_dim_values; + char ***first_dim_values, **second_dim_values; tng_trajectory_frame_set_t frame_set; tng_particle_data_t data; char *orig; @@ -10231,6 +10529,8 @@ tng_function_status DECLSPECDLLEXPORT tng_particle_data_block_add data->stride_length = tng_max(stride_length, 1); data->values = 0; + /* FIXME: Memory leak from strings. */ + data->strings = 0; data->n_values_per_frame = n_values_per_frame; data->n_frames = n_frames; data->codec_id = codec_id; @@ -10267,48 +10567,11 @@ tng_function_status DECLSPECDLLEXPORT tng_particle_data_block_add frame_set->n_written_frames = n_frames; } - switch(datatype) + if(datatype == TNG_CHAR_DATA) { - case TNG_FLOAT_DATA: - size = sizeof(float); for(i = 0; i < n_frames / stride_length; i++) { - first_dim_values = data->values[i]; - for(j = num_first_particle; j < num_first_particle + n_particles; - j++) - { - second_dim_values = first_dim_values[j]; - for(k = 0; k < n_values_per_frame; k++) - { - memcpy(&second_dim_values[k].f, - new_data_c, size); - new_data_c += size; - } - } - } - break; - case TNG_INT_DATA: - size = sizeof(int64_t); - for(i = 0; i < n_frames / stride_length; i++) - { - first_dim_values = data->values[i]; - for(j = num_first_particle; j < num_first_particle + n_particles; - j++) - { - second_dim_values = first_dim_values[j]; - for(k = 0; k < n_values_per_frame; k++) - { - memcpy(&second_dim_values[k].i, - new_data_c, size); - new_data_c += size; - } - } - } - break; - case TNG_CHAR_DATA: - for(i = 0; i < n_frames / stride_length; i++) - { - first_dim_values = data->values[i]; + first_dim_values = data->strings[i]; for(j = num_first_particle; j < num_first_particle + n_particles; j++) { @@ -10317,75 +10580,55 @@ tng_function_status DECLSPECDLLEXPORT tng_particle_data_block_add { len = tng_min(strlen(new_data_c) + 1, TNG_MAX_STR_LEN); - if(second_dim_values[k].c) + if(second_dim_values[k]) { - free(second_dim_values[k].c); + free(second_dim_values[k]); } - second_dim_values[k].c = malloc(len); - if(!second_dim_values[k].c) + second_dim_values[k] = malloc(len); + if(!second_dim_values[k]) { printf("Cannot allocate memory (%d bytes). %s: %d\n", len, __FILE__, __LINE__); return(TNG_CRITICAL); } - strncpy(second_dim_values[k].c, + strncpy(second_dim_values[k], new_data_c, len); new_data_c += len; } } } - break; - case TNG_DOUBLE_DATA: - default: - size = sizeof(double); - for(i = 0; i < n_frames / stride_length; i++) + } + else + { + switch(datatype) { - first_dim_values = data->values[i]; - for(j = num_first_particle; j < num_first_particle + n_particles; - j++) - { - second_dim_values = first_dim_values[j]; - for(k = 0; k < n_values_per_frame; k++) - { - memcpy(&second_dim_values[k].d, - new_data_c, size); - new_data_c += size; - } - } + case TNG_CHAR_DATA: + break; + case TNG_INT_DATA: + size = sizeof(int64_t); + break; + case TNG_FLOAT_DATA: + size = sizeof(float); + break; + case TNG_DOUBLE_DATA: + default: + size = sizeof(double); } - break; - } - new_data_c = orig; + memcpy(data->values, new_data, size * n_frames / stride_length * + n_particles * n_values_per_frame); + } } - /* Otherwise fill the data block with zeroes */ -// else -// { -// for(i = 0; i < n_frames / stride_length; i++) -// { -// first_dim_values = data->values[i]; -// for(j = num_first_particle; j < num_first_particle + n_particles; -// j++) -// { -// second_dim_values = first_dim_values[j]; -// for(k = 0; k < n_values_per_frame; k++) -// { -// second_dim_values[k].d = 0; -// } -// } -// } -// } - return(TNG_SUCCESS); } tng_function_status DECLSPECDLLEXPORT tng_frame_data_write (tng_trajectory_t tng_data, - const int64_t frame_nr, - const int64_t block_id, - const void *values, - const tng_hash_mode hash_mode) + const int64_t frame_nr, + const int64_t block_id, + const void *values, + const tng_hash_mode hash_mode) { int64_t header_pos, file_pos; int64_t output_file_len, n_values_per_frame, size, contents_size; @@ -10793,12 +11036,12 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_data_write tng_function_status DECLSPECDLLEXPORT tng_frame_particle_data_write (tng_trajectory_t tng_data, - const int64_t frame_nr, - const int64_t block_id, - const int64_t val_first_particle, - const int64_t val_n_particles, - const void *values, - const tng_hash_mode hash_mode) + const int64_t frame_nr, + const int64_t block_id, + const int64_t val_first_particle, + const int64_t val_n_particles, + const void *values, + const tng_hash_mode hash_mode) { int64_t header_pos, file_pos, tot_n_particles; int64_t output_file_len, n_values_per_frame, size, contents_size; @@ -11322,12 +11565,61 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_particle_data_write return(TNG_SUCCESS); } +static tng_function_status tng_data_values_alloc + (const tng_trajectory_t tng_data, + union data_values ***values, + const int64_t n_frames, + const int64_t n_values_per_frame, + const tng_data_type type) +{ + int64_t i; + tng_function_status stat; + + if(*values) + { + stat = tng_data_values_free(tng_data, *values, n_frames, + n_values_per_frame, + type); + if(stat != TNG_SUCCESS) + { + printf("Cannot free particle data values. %s: %d\n", + __FILE__, __LINE__); + return(stat); + } + } + *values = malloc(sizeof(union data_values **) * n_frames); + if(!*values) + { + printf("Cannot allocate memory (%"PRId64" bytes). %s: %d\n", + sizeof(union data_values **) * n_frames, + __FILE__, __LINE__); + return(TNG_CRITICAL); + + } + + for(i = n_frames; i--;) + { + (*values)[i] = malloc(sizeof(union data_values) * + n_values_per_frame); + if(!(*values)[i]) + { + printf("Cannot allocate memory (%"PRId64" bytes). %s: %d\n", + sizeof(union data_values) * n_values_per_frame, + __FILE__, __LINE__); + free(values); + values = 0; + return(TNG_CRITICAL); + } + } + return(TNG_SUCCESS); +} + tng_function_status DECLSPECDLLEXPORT tng_data_values_free (const tng_trajectory_t tng_data, - union data_values **values, - const int64_t n_frames, - const int64_t n_values_per_frame, - const tng_data_type type) + union data_values **values, + const int64_t n_frames, + const int64_t n_values_per_frame, + const tng_data_type type) { int i, j; @@ -11359,6 +11651,71 @@ tng_function_status DECLSPECDLLEXPORT tng_data_values_free return(TNG_SUCCESS); } +static tng_function_status tng_particle_data_values_alloc + (const tng_trajectory_t tng_data, + union data_values ****values, + const int64_t n_frames, + const int64_t n_particles, + const int64_t n_values_per_frame, + const tng_data_type type) +{ + int64_t i, j; + tng_function_status stat; + + if(*values) + { + stat = tng_particle_data_values_free(tng_data, *values, n_frames, + n_particles, n_values_per_frame, + type); + if(stat != TNG_SUCCESS) + { + printf("Cannot free particle data values. %s: %d\n", + __FILE__, __LINE__); + return(stat); + } + } + *values = malloc(sizeof(union data_values **) * n_frames); + if(!*values) + { + printf("Cannot allocate memory (%"PRId64" bytes). %s: %d\n", + sizeof(union data_values **) * n_frames, + __FILE__, __LINE__); + return(TNG_CRITICAL); + + } + + for(i = n_frames; i--;) + { + (*values)[i] = malloc(sizeof(union data_values *) * + n_particles); + if(!(*values)[i]) + { + printf("Cannot allocate memory (%"PRId64" bytes). %s: %d\n", + sizeof(union data_values *) * n_particles, + __FILE__, __LINE__); + free(*values); + *values = 0; + return(TNG_CRITICAL); + } + for(j = n_particles; j--;) + { + (*values)[i][j] = malloc(sizeof(union data_values) * + n_values_per_frame); + if(!(*values)[i][j]) + { + printf("Cannot allocate memory (%"PRId64" bytes). %s: %d\n", + sizeof(union data_values *) * n_particles, + __FILE__, __LINE__); + tng_particle_data_values_free(tng_data, *values, n_frames, + n_particles, n_values_per_frame, + type); + *values = 0; + return(TNG_CRITICAL); + } + } + } + return(TNG_SUCCESS); +} tng_function_status DECLSPECDLLEXPORT tng_particle_data_values_free (const tng_trajectory_t tng_data, @@ -11406,15 +11763,15 @@ tng_function_status DECLSPECDLLEXPORT tng_particle_data_values_free tng_function_status DECLSPECDLLEXPORT tng_data_get (tng_trajectory_t tng_data, - const int64_t block_id, - union data_values ***values, - int64_t *n_frames, - int64_t *n_values_per_frame, - tng_data_type *type) + const int64_t block_id, + union data_values ***values, + int64_t *n_frames, + int64_t *n_values_per_frame, + tng_data_type *type) { int64_t file_pos; - int i, j, block_index, len; - tng_non_particle_data_t data, new_data; + int i, j, block_index, len, size; + tng_non_particle_data_t data; tng_trajectory_frame_set_t frame_set = &tng_data->current_trajectory_frame_set; tng_gen_block_t block; @@ -11492,25 +11849,21 @@ tng_function_status DECLSPECDLLEXPORT tng_data_get } } - /* A bit hackish to create a new data struct before returning the data */ - new_data = malloc(sizeof(struct tng_non_particle_data)); - - new_data->n_values_per_frame = 0; - new_data->n_frames = 0; - new_data->values = 0; - new_data->datatype = data->datatype; + *n_frames = tng_max(1, data->n_frames); *n_values_per_frame = data->n_values_per_frame; - if(tng_allocate_data_mem(tng_data, new_data, data->n_frames, - data->stride_length, *n_values_per_frame) - != TNG_SUCCESS) + *type = data->datatype; + + if(*values == 0) { - return(TNG_CRITICAL); + if(tng_data_values_alloc(tng_data, values, *n_frames, + *n_values_per_frame, + *type) + != TNG_SUCCESS) + { + return(TNG_CRITICAL); + } } - *n_frames = tng_max(1, data->n_frames); - - *values = new_data->values; - *type = data->datatype; switch(*type) { case TNG_CHAR_DATA: @@ -11518,59 +11871,180 @@ tng_function_status DECLSPECDLLEXPORT tng_data_get { for(j=*n_values_per_frame; j--;) { - len = strlen(data->values[i][j].c) + 1; + len = strlen(data->strings[i][j]) + 1; (*values)[i][j].c = malloc(len); - strncpy((*values)[i][j].c, data->values[i][j].c, len); + strncpy((*values)[i][j].c, data->strings[i][j], len); } } break; case TNG_INT_DATA: + size = sizeof(int); for(i=*n_frames; i--;) { for(j=*n_values_per_frame; j--;) { - (*values)[i][j].i = data->values[i][j].i; + (*values)[i][j].i = *(int *)(data->values + size * + (i*(*n_values_per_frame) + j)); } } break; case TNG_FLOAT_DATA: + size = sizeof(float); for(i=*n_frames; i--;) { for(j=*n_values_per_frame; j--;) { - (*values)[i][j].f = data->values[i][j].f; + (*values)[i][j].f = *(float *)(data->values + size * + (i*(*n_values_per_frame) + j)); } } break; case TNG_DOUBLE_DATA: default: + size = sizeof(double); for(i=*n_frames; i--;) { for(j=*n_values_per_frame; j--;) { - (*values)[i][j].d = data->values[i][j].d; + (*values)[i][j].d = *(double *)(data->values + size * + (i*(*n_values_per_frame) + j)); + } + } + } + + return(TNG_SUCCESS); +} + +tng_function_status tng_data_vector_get(tng_trajectory_t tng_data, + const int64_t block_id, + void **values, + int64_t *n_frames, + int64_t *n_values_per_frame, + tng_data_type *type) +{ + int64_t file_pos; + int i, block_index, data_size, size; + tng_non_particle_data_t data; + tng_trajectory_frame_set_t frame_set = + &tng_data->current_trajectory_frame_set; + tng_gen_block_t block; + tng_function_status stat; + + block_index = -1; + /* See if there is a data block of this ID. + * Start checking the last read frame set */ + for(i = frame_set->n_data_blocks; i-- ;) + { + data = &frame_set->tr_data[i]; + if(data->block_id == block_id) + { + block_index = i; + break; + } + } + + if(block_index < 0) + { + /* If the data block was not found in the frame set + * look for it in the non-trajectory data (in tng_data). */ + for(i = tng_data->n_data_blocks; i-- ;) + { + data = &tng_data->non_tr_data[i]; + if(data->block_id == block_id) + { + block_index = i; + break; } } + if(block_index < 0) + { + tng_block_init(&block); + file_pos = ftell(tng_data->input_file); + /* Read all blocks until next frame set block */ + stat = tng_block_header_read(tng_data, block); + while(file_pos < tng_data->input_file_len && + stat != TNG_CRITICAL && + block->id != TNG_TRAJECTORY_FRAME_SET) + { + /* Use hash by default */ + stat = tng_block_read_next(tng_data, block, + TNG_USE_HASH); + if(stat != TNG_CRITICAL) + { + file_pos = ftell(tng_data->input_file); + if(file_pos < tng_data->input_file_len) + { + stat = tng_block_header_read(tng_data, block); + } + } + } + tng_block_destroy(&block); + if(stat == TNG_CRITICAL) + { + printf("Cannot read block header at pos %"PRId64". %s: %d\n", + file_pos, __FILE__, __LINE__); + return(stat); + } + + for(i = frame_set->n_data_blocks; i-- ;) + { + data = &frame_set->tr_data[i]; + if(data->block_id == block_id) + { + block_index = i; + break; + } + } + if(block_index < 0) + { + return(TNG_FAILURE); + } + } + } + + *type = data->datatype; + + switch(*type) + { + case TNG_CHAR_DATA: + return(TNG_FAILURE); + case TNG_INT_DATA: + size = sizeof(int64_t); + break; + case TNG_FLOAT_DATA: + size = sizeof(float); + break; + case TNG_DOUBLE_DATA: + default: + size = sizeof(double); } - free(new_data); + *n_frames = tng_max(1, data->n_frames); + *n_values_per_frame = data->n_values_per_frame; + + data_size = (*n_frames / data->stride_length) * size * + *n_values_per_frame; + + *values = malloc(data_size); + + memcpy(values, data->values, data_size); return(TNG_SUCCESS); } tng_function_status DECLSPECDLLEXPORT tng_data_interval_get (tng_trajectory_t tng_data, - const int64_t block_id, - const int64_t start_frame_nr, - const int64_t end_frame_nr, - const tng_hash_mode hash_mode, - union data_values ***values, - int64_t *n_values_per_frame, - tng_data_type *type) + const int64_t block_id, + const int64_t start_frame_nr, + const int64_t end_frame_nr, + const tng_hash_mode hash_mode, + union data_values ***values, + int64_t *n_values_per_frame, + tng_data_type *type) { int64_t i, j, n_frames, file_pos, current_frame_pos; - int block_index, len; - tng_non_particle_data_t data, new_data; + int block_index, len, size; + tng_non_particle_data_t data; tng_trajectory_frame_set_t frame_set; tng_gen_block_t block; tng_function_status stat; @@ -11633,32 +12107,19 @@ tng_function_status DECLSPECDLLEXPORT tng_data_interval_get } n_frames = end_frame_nr - start_frame_nr + 1; - - /* A bit hackish to create a new data struct before returning the data */ + *n_values_per_frame = data->n_values_per_frame; + *type = data->datatype; if(*values == 0) { - new_data = malloc(sizeof(struct tng_non_particle_data)); - - new_data->n_values_per_frame = 0; - new_data->n_frames = 0; - new_data->values = 0; - new_data->datatype = data->datatype; - *n_values_per_frame = data->n_values_per_frame; - if(tng_allocate_data_mem(tng_data, new_data, n_frames, - data->stride_length, - data->n_values_per_frame) != TNG_SUCCESS) + if(tng_data_values_alloc(tng_data, values, n_frames, + *n_values_per_frame, + *type) != TNG_SUCCESS) { - free(new_data); return(TNG_CRITICAL); } - - *values = new_data->values; - - free(new_data); } - *type = data->datatype; current_frame_pos = start_frame_nr - frame_set->first_frame; /* It's not very elegant to reuse so much of the code in the different case * statements, but it's unnecessarily slow to have the switch-case block @@ -11679,14 +12140,15 @@ tng_function_status DECLSPECDLLEXPORT tng_data_interval_get } for(j=*n_values_per_frame; j--;) { - len = strlen(data->values[current_frame_pos][j].c) + 1; + len = strlen(data->strings[current_frame_pos][j]) + 1; (*values)[i][j].c = malloc(len); - strncpy((*values)[i][j].c, data->values[current_frame_pos][j].c, len); + strncpy((*values)[i][j].c, data->strings[current_frame_pos][j], len); } current_frame_pos++; } break; case TNG_INT_DATA: + size = sizeof(int); for(i=0; i<n_frames; i++) { if(current_frame_pos == frame_set->n_frames) @@ -11700,12 +12162,15 @@ tng_function_status DECLSPECDLLEXPORT tng_data_interval_get } for(j=*n_values_per_frame; j--;) { - (*values)[i][j].i = data->values[current_frame_pos][j].i; + (*values)[i][j].i = *(int *)(data->values + size * + (current_frame_pos * + (*n_values_per_frame) + j)); } current_frame_pos++; } break; case TNG_FLOAT_DATA: + size = sizeof(float); for(i=0; i<n_frames; i++) { if(current_frame_pos == frame_set->n_frames) @@ -11719,13 +12184,16 @@ tng_function_status DECLSPECDLLEXPORT tng_data_interval_get } for(j=*n_values_per_frame; j--;) { - (*values)[i][j].f = data->values[current_frame_pos][j].f; + (*values)[i][j].f = *(float *)(data->values + size * + (current_frame_pos * + (*n_values_per_frame) + j)); } current_frame_pos++; } break; case TNG_DOUBLE_DATA: default: + size = sizeof(double); for(i=0; i<n_frames; i++) { if(current_frame_pos == frame_set->n_frames) @@ -11739,7 +12207,9 @@ tng_function_status DECLSPECDLLEXPORT tng_data_interval_get } for(j=*n_values_per_frame; j--;) { - (*values)[i][j].d = data->values[current_frame_pos][j].d; + (*values)[i][j].d = *(double *)(data->values + size * + (current_frame_pos * + (*n_values_per_frame) + j)); } current_frame_pos++; } @@ -11751,16 +12221,16 @@ tng_function_status DECLSPECDLLEXPORT tng_data_interval_get tng_function_status DECLSPECDLLEXPORT tng_particle_data_get (tng_trajectory_t tng_data, - const int64_t block_id, - union data_values ****values, - int64_t *n_frames, - int64_t *n_particles, - int64_t *n_values_per_frame, - tng_data_type *type) -{ - int64_t i, j, k, mapping, file_pos; - int block_index, len; - tng_particle_data_t data, new_data; + const int64_t block_id, + union data_values ****values, + int64_t *n_frames, + int64_t *n_particles, + int64_t *n_values_per_frame, + tng_data_type *type) +{ + int64_t i, j, k, mapping, file_pos, i_step; + int block_index, len, size; + tng_particle_data_t data; tng_trajectory_frame_set_t frame_set = &tng_data->current_trajectory_frame_set; tng_gen_block_t block; @@ -11854,34 +12324,21 @@ tng_function_status DECLSPECDLLEXPORT tng_particle_data_get *n_particles = tng_data->n_particles; } - /* A bit hackish to create a new data struct before returning the data */ + *n_frames = tng_max(1, data->n_frames); + *n_values_per_frame = data->n_values_per_frame; + *type = data->datatype; if(*values == 0) { - new_data = malloc(sizeof(struct tng_particle_data)); - - new_data->n_values_per_frame = 0; - new_data->n_frames = 0; - new_data->values = 0; - new_data->datatype = data->datatype; - *n_values_per_frame = data->n_values_per_frame; - if(tng_allocate_particle_data_mem(tng_data, new_data, data->n_frames, - data->stride_length, - *n_particles, data->n_values_per_frame) + if(tng_particle_data_values_alloc(tng_data, values, *n_frames, + *n_particles, *n_values_per_frame, + *type) != TNG_SUCCESS) { - free(new_data); return(TNG_CRITICAL); } - - *n_frames = tng_max(1, data->n_frames); - - *values = new_data->values; - - free(new_data); } - *type = data->datatype; /* It's not very elegant to reuse so much of the code in the different case * statements, but it's unnecessarily slow to have the switch-case block * inside the for loops. */ @@ -11895,14 +12352,17 @@ tng_function_status DECLSPECDLLEXPORT tng_particle_data_get tng_particle_mapping_get_real_particle(frame_set, j, &mapping); for(k=*n_values_per_frame; k--;) { - len = strlen(data->values[i][j][k].c) + 1; - (*values)[i][j][k].c = malloc(len); - strncpy((*values)[i][mapping][k].c, data->values[i][j][k].c, len); + len = strlen(data->strings[i][j][k]) + 1; + (*values)[i][mapping][k].c = malloc(len); + strncpy((*values)[i][mapping][k].c, + data->strings[i][j][k], len); } } } break; case TNG_INT_DATA: + size = sizeof(int); + i_step = (*n_particles) * (*n_values_per_frame); for(i=*n_frames; i--;) { for(j=*n_particles; j--;) @@ -11910,12 +12370,17 @@ tng_function_status DECLSPECDLLEXPORT tng_particle_data_get tng_particle_mapping_get_real_particle(frame_set, j, &mapping); for(k=*n_values_per_frame; k--;) { - (*values)[i][mapping][k].i = data->values[i][j][k].i; + (*values)[i][mapping][k].i = *(int *) + (data->values + size * + (i * i_step + j * + (*n_values_per_frame) + k)); } } } break; case TNG_FLOAT_DATA: + size = sizeof(float); + i_step = (*n_particles) * (*n_values_per_frame); for(i=*n_frames; i--;) { for(j=*n_particles; j--;) @@ -11923,13 +12388,18 @@ tng_function_status DECLSPECDLLEXPORT tng_particle_data_get tng_particle_mapping_get_real_particle(frame_set, j, &mapping); for(k=*n_values_per_frame; k--;) { - (*values)[i][mapping][k].f = data->values[i][j][k].f; + (*values)[i][mapping][k].f = *(float *) + (data->values + size * + (i * i_step + j * + (*n_values_per_frame) + k)); } } } break; case TNG_DOUBLE_DATA: default: + size = sizeof(double); + i_step = (*n_particles) * (*n_values_per_frame); for(i=*n_frames; i--;) { for(j=*n_particles; j--;) @@ -11937,7 +12407,10 @@ tng_function_status DECLSPECDLLEXPORT tng_particle_data_get tng_particle_mapping_get_real_particle(frame_set, j, &mapping); for(k=*n_values_per_frame; k--;) { - (*values)[i][mapping][k].d = data->values[i][j][k].d; + (*values)[i][mapping][k].d = *(double *) + (data->values + size * + (i * i_step + j * + (*n_values_per_frame) + k)); } } } @@ -11949,18 +12422,18 @@ tng_function_status DECLSPECDLLEXPORT tng_particle_data_get tng_function_status DECLSPECDLLEXPORT tng_particle_data_interval_get (tng_trajectory_t tng_data, - const int64_t block_id, - const int64_t start_frame_nr, - const int64_t end_frame_nr, - const tng_hash_mode hash_mode, - union data_values ****values, - int64_t *n_particles, - int64_t *n_values_per_frame, - tng_data_type *type) -{ - int64_t i, j, k, mapping, n_frames, file_pos, current_frame_pos; - int block_index, len; - tng_particle_data_t data, new_data; + const int64_t block_id, + const int64_t start_frame_nr, + const int64_t end_frame_nr, + const tng_hash_mode hash_mode, + union data_values ****values, + int64_t *n_particles, + int64_t *n_values_per_frame, + tng_data_type *type) +{ + int64_t i, j, k, mapping, n_frames, file_pos, current_frame_pos, i_step; + int block_index, len, size; + tng_particle_data_t data; tng_trajectory_frame_set_t frame_set; tng_gen_block_t block; tng_function_status stat; @@ -12035,33 +12508,20 @@ tng_function_status DECLSPECDLLEXPORT tng_particle_data_interval_get } n_frames = end_frame_nr - start_frame_nr + 1; - - /* A bit hackish to create a new data struct before returning the data */ + *n_values_per_frame = data->n_values_per_frame; + *type = data->datatype; if(*values == 0) { - new_data = malloc(sizeof(struct tng_particle_data)); - - new_data->n_values_per_frame = 0; - new_data->n_frames = 0; - new_data->values = 0; - new_data->datatype = data->datatype; - *n_values_per_frame = data->n_values_per_frame; - if(tng_allocate_particle_data_mem(tng_data, new_data, n_frames, - data->stride_length, - *n_particles, data->n_values_per_frame) != - TNG_SUCCESS) + if(tng_particle_data_values_alloc(tng_data, values, n_frames, + *n_particles, *n_values_per_frame, + *type) + != TNG_SUCCESS) { - free(new_data); return(TNG_CRITICAL); } - - *values = new_data->values; - - free(new_data); } - *type = data->datatype; current_frame_pos = start_frame_nr - frame_set->first_frame; /* It's not very elegant to reuse so much of the code in the different case * statements, but it's unnecessarily slow to have the switch-case block @@ -12085,15 +12545,17 @@ tng_function_status DECLSPECDLLEXPORT tng_particle_data_interval_get tng_particle_mapping_get_real_particle(frame_set, j, &mapping); for(k=*n_values_per_frame; k--;) { - len = strlen(data->values[current_frame_pos][j][k].c) + 1; - (*values)[i][j][k].c = malloc(len); - strncpy((*values)[i][j][k].c, data->values[current_frame_pos][j][k].c, len); + len = strlen(data->strings[current_frame_pos][j][k]) + 1; + (*values)[i][mapping][k].c = malloc(len); + strncpy((*values)[i][mapping][k].c, data->strings[current_frame_pos][j][k], len); } } current_frame_pos++; } break; case TNG_INT_DATA: + size = sizeof(int); + i_step = (*n_particles) * (*n_values_per_frame); for(i=0; i<n_frames; i++) { if(current_frame_pos == frame_set->n_frames) @@ -12110,13 +12572,19 @@ tng_function_status DECLSPECDLLEXPORT tng_particle_data_interval_get tng_particle_mapping_get_real_particle(frame_set, j, &mapping); for(k=*n_values_per_frame; k--;) { - (*values)[i][mapping][k].i = data->values[current_frame_pos][j][k].i; + (*values)[i][mapping][k].i = *(int *) + (data->values + size * + (current_frame_pos * + i_step + j * + (*n_values_per_frame) + k)); } } current_frame_pos++; } break; case TNG_FLOAT_DATA: + size = sizeof(float); + i_step = (*n_particles) * (*n_values_per_frame); for(i=0; i<n_frames; i++) { if(current_frame_pos == frame_set->n_frames) @@ -12133,7 +12601,11 @@ tng_function_status DECLSPECDLLEXPORT tng_particle_data_interval_get tng_particle_mapping_get_real_particle(frame_set, j, &mapping); for(k=*n_values_per_frame; k--;) { - (*values)[i][mapping][k].f = data->values[current_frame_pos][j][k].f; + (*values)[i][mapping][k].f = *(float *) + (data->values + size * + (current_frame_pos * + i_step + j * + (*n_values_per_frame) + k)); } } current_frame_pos++; @@ -12141,6 +12613,8 @@ tng_function_status DECLSPECDLLEXPORT tng_particle_data_interval_get break; case TNG_DOUBLE_DATA: default: + size = sizeof(double); + i_step = (*n_particles) * (*n_values_per_frame); for(i=0; i<n_frames; i++) { if(current_frame_pos == frame_set->n_frames) @@ -12157,7 +12631,11 @@ tng_function_status DECLSPECDLLEXPORT tng_particle_data_interval_get tng_particle_mapping_get_real_particle(frame_set, j, &mapping); for(k=*n_values_per_frame; k--;) { - (*values)[i][mapping][k].d = data->values[current_frame_pos][j][k].d; + (*values)[i][mapping][k].d = *(double *) + (data->values + size * + (current_frame_pos * + i_step + j * + (*n_values_per_frame) + k)); } } current_frame_pos++; @@ -12170,7 +12648,7 @@ tng_function_status DECLSPECDLLEXPORT tng_particle_data_interval_get tng_function_status DECLSPECDLLEXPORT tng_time_get_str (const tng_trajectory_t tng_data, - char *time) + char *time) { struct tm *time_data; time_t secs; @@ -12378,7 +12856,7 @@ tng_function_status DECLSPECDLLEXPORT tng_last_computer_name_set_(tng_trajectory tng_function_status DECLSPECDLLEXPORT tng_first_signature_get_ (const tng_trajectory_t tng_data, - char *signature, const int max_len) + char *signature, const int max_len) { return(tng_first_signature_get(tng_data, signature, max_len)); } @@ -12399,15 +12877,15 @@ tng_function_status DECLSPECDLLEXPORT tng_first_signature_set_(tng_trajectory_t tng_function_status DECLSPECDLLEXPORT tng_last_signature_get_ (const tng_trajectory_t tng_data, - char *signature, const int max_len) + char *signature, const int max_len) { return(tng_last_signature_get(tng_data, signature, max_len)); } tng_function_status DECLSPECDLLEXPORT tng_last_signature_set_ (tng_trajectory_t tng_data, - const char *signature, - int sign_len) + const char *signature, + int sign_len) { char *sign = malloc(sign_len + 1); tng_function_status stat; @@ -12421,15 +12899,15 @@ tng_function_status DECLSPECDLLEXPORT tng_last_signature_set_ tng_function_status DECLSPECDLLEXPORT tng_forcefield_name_get_ (const tng_trajectory_t tng_data, - char *name, const int max_len) + char *name, const int max_len) { return(tng_forcefield_name_get(tng_data, name, max_len)); } tng_function_status DECLSPECDLLEXPORT tng_forcefield_name_set_ (tng_trajectory_t tng_data, - const char *new_name, - int name_len) + const char *new_name, + int name_len) { char *name = malloc(name_len + 1); tng_function_status stat; @@ -12443,56 +12921,56 @@ tng_function_status DECLSPECDLLEXPORT tng_forcefield_name_set_ tng_function_status DECLSPECDLLEXPORT tng_medium_stride_length_get_ (const tng_trajectory_t tng_data, - int64_t *len) + int64_t *len) { return(tng_medium_stride_length_get(tng_data, len)); } tng_function_status DECLSPECDLLEXPORT tng_medium_stride_length_set_ (tng_trajectory_t tng_data, - const int64_t *len) + const int64_t *len) { return(tng_medium_stride_length_set(tng_data, *len)); } tng_function_status DECLSPECDLLEXPORT tng_long_stride_length_get_ (const tng_trajectory_t tng_data, - int64_t *len) + int64_t *len) { return(tng_long_stride_length_get(tng_data, len)); } tng_function_status DECLSPECDLLEXPORT tng_long_stride_length_set_ (tng_trajectory_t tng_data, - const int64_t *len) + const int64_t *len) { return(tng_long_stride_length_set(tng_data, *len)); } tng_function_status DECLSPECDLLEXPORT tng_input_file_len_get_ (const tng_trajectory_t tng_data, - int64_t *len) + int64_t *len) { return(tng_input_file_len_get(tng_data, len)); } tng_function_status DECLSPECDLLEXPORT tng_num_frames_get_ (const tng_trajectory_t tng_data, - int64_t *n) + int64_t *n) { return(tng_num_frames_get(tng_data, n)); } tng_function_status DECLSPECDLLEXPORT tng_num_particles_get_ (const tng_trajectory_t tng_data, - int64_t *n) + int64_t *n) { return(tng_num_particles_get(tng_data, n)); } tng_function_status DECLSPECDLLEXPORT tng_num_molecules_get_ (const tng_trajectory_t tng_data, - int64_t *n) + int64_t *n) { return(tng_num_molecules_get(tng_data, n)); } @@ -12526,14 +13004,14 @@ tng_function_status DECLSPECDLLEXPORT tng_current_frame_set_get_ } tng_function_status DECLSPECDLLEXPORT tng_frame_set_nr_find_(tng_trajectory_t tng_data, - const int64_t *nr) + const int64_t *nr) { return(tng_frame_set_nr_find(tng_data, *nr)); } tng_function_status DECLSPECDLLEXPORT tng_frame_set_of_frame_find_ (tng_trajectory_t tng_data, - const int64_t *frame) + const int64_t *frame) { return(tng_frame_set_of_frame_find(tng_data, *frame)); } @@ -12572,15 +13050,15 @@ tng_function_status DECLSPECDLLEXPORT tng_molecule_init_(const tng_trajectory_t tng_function_status DECLSPECDLLEXPORT tng_molecule_destroy_ (const tng_trajectory_t tng_data, - tng_molecule_t molecule) + tng_molecule_t molecule) { return(tng_molecule_destroy(tng_data, molecule)); } tng_function_status DECLSPECDLLEXPORT tng_molecule_add_(tng_trajectory_t tng_data, - const char *name, - tng_molecule_t *molecule, - int name_len) + const char *name, + tng_molecule_t *molecule, + int name_len) { char *n = malloc(name_len + 1); tng_function_status stat; @@ -12593,9 +13071,9 @@ tng_function_status DECLSPECDLLEXPORT tng_molecule_add_(tng_trajectory_t tng_dat } tng_function_status DECLSPECDLLEXPORT tng_molecule_name_set_(tng_trajectory_t tng_data, - tng_molecule_t molecule, - const char *new_name, - int name_len) + tng_molecule_t molecule, + const char *new_name, + int name_len) { char *name = malloc(name_len + 1); tng_function_status stat; @@ -12608,25 +13086,25 @@ tng_function_status DECLSPECDLLEXPORT tng_molecule_name_set_(tng_trajectory_t tn } tng_function_status DECLSPECDLLEXPORT tng_molecule_cnt_get_(tng_trajectory_t tng_data, - tng_molecule_t molecule, - int64_t *cnt) + tng_molecule_t molecule, + int64_t *cnt) { return(tng_molecule_cnt_get(tng_data, molecule, cnt)); } tng_function_status DECLSPECDLLEXPORT tng_molecule_cnt_set_(tng_trajectory_t tng_data, - tng_molecule_t molecule, - int64_t *cnt) + tng_molecule_t molecule, + int64_t *cnt) { return(tng_molecule_cnt_set(tng_data, molecule, *cnt)); } tng_function_status DECLSPECDLLEXPORT tng_molecule_chain_find_(tng_trajectory_t tng_data, - tng_molecule_t molecule, - const char *name, - int64_t id, - tng_chain_t *chain, - int name_len) + tng_molecule_t molecule, + const char *name, + int64_t id, + tng_chain_t *chain, + int name_len) { char *n = malloc(name_len + 1); tng_function_status stat; @@ -12639,10 +13117,10 @@ tng_function_status DECLSPECDLLEXPORT tng_molecule_chain_find_(tng_trajectory_t } tng_function_status DECLSPECDLLEXPORT tng_molecule_chain_add_(tng_trajectory_t tng_data, - tng_molecule_t molecule, - const char *name, - tng_chain_t *chain, - int name_len) + tng_molecule_t molecule, + const char *name, + tng_chain_t *chain, + int name_len) { char *n = malloc(name_len + 1); tng_function_status stat; @@ -12655,9 +13133,9 @@ tng_function_status DECLSPECDLLEXPORT tng_molecule_chain_add_(tng_trajectory_t t } tng_function_status DECLSPECDLLEXPORT tng_chain_name_set_(tng_trajectory_t tng_data, - tng_chain_t chain, - const char *new_name, - int name_len) + tng_chain_t chain, + const char *new_name, + int name_len) { char *name = malloc(name_len + 1); tng_function_status stat; @@ -12670,10 +13148,10 @@ tng_function_status DECLSPECDLLEXPORT tng_chain_name_set_(tng_trajectory_t tng_d } tng_function_status DECLSPECDLLEXPORT tng_chain_residue_add_(tng_trajectory_t tng_data, - tng_chain_t chain, - const char *name, - tng_residue_t *residue, - int name_len) + tng_chain_t chain, + const char *name, + tng_residue_t *residue, + int name_len) { char *n = malloc(name_len + 1); tng_function_status stat; @@ -12686,9 +13164,9 @@ tng_function_status DECLSPECDLLEXPORT tng_chain_residue_add_(tng_trajectory_t tn } tng_function_status DECLSPECDLLEXPORT tng_residue_name_set_(tng_trajectory_t tng_data, - tng_residue_t residue, - const char *new_name, - int name_len) + tng_residue_t residue, + const char *new_name, + int name_len) { char *name = malloc(name_len + 1); tng_function_status stat; @@ -12701,12 +13179,12 @@ tng_function_status DECLSPECDLLEXPORT tng_residue_name_set_(tng_trajectory_t tng } tng_function_status DECLSPECDLLEXPORT tng_residue_atom_add_(tng_trajectory_t tng_data, - tng_residue_t residue, - const char *atom_name, - const char *atom_type, - tng_atom_t *atom, - int name_len, - int type_len) + tng_residue_t residue, + const char *atom_name, + const char *atom_type, + tng_atom_t *atom, + int name_len, + int type_len) { char *name = malloc(name_len + 1); char *type = malloc(type_len + 1); @@ -12723,9 +13201,9 @@ tng_function_status DECLSPECDLLEXPORT tng_residue_atom_add_(tng_trajectory_t tng } tng_function_status DECLSPECDLLEXPORT tng_atom_name_set_(tng_trajectory_t tng_data, - tng_atom_t atom, - const char *new_name, - int name_len) + tng_atom_t atom, + const char *new_name, + int name_len) { char *name = malloc(name_len + 1); tng_function_status stat; @@ -12738,9 +13216,9 @@ tng_function_status DECLSPECDLLEXPORT tng_atom_name_set_(tng_trajectory_t tng_da } tng_function_status DECLSPECDLLEXPORT tng_atom_type_set_(tng_trajectory_t tng_data, - tng_atom_t atom, - const char *new_type, - int type_len) + tng_atom_t atom, + const char *new_type, + int type_len) { char *type = malloc(type_len + 1); tng_function_status stat; @@ -12754,8 +13232,8 @@ tng_function_status DECLSPECDLLEXPORT tng_atom_type_set_(tng_trajectory_t tng_da tng_function_status DECLSPECDLLEXPORT tng_molecule_name_of_particle_nr_get_ (const tng_trajectory_t tng_data, - const int64_t nr, - char *name, + const int64_t nr, + char *name, int max_len) { return(tng_molecule_name_of_particle_nr_get(tng_data, nr, name, max_len)); @@ -12808,58 +13286,58 @@ tng_function_status DECLSPECDLLEXPORT tng_particle_mapping_add_ } tng_function_status DECLSPECDLLEXPORT tng_file_headers_read_(tng_trajectory_t tng_data, - const tng_hash_mode *hash_mode) + const tng_hash_mode *hash_mode) { return(tng_file_headers_read(tng_data, *hash_mode)); } tng_function_status DECLSPECDLLEXPORT tng_file_headers_write_ (tng_trajectory_t tng_data, - const tng_hash_mode *hash_mode) + const tng_hash_mode *hash_mode) { return(tng_file_headers_write(tng_data, *hash_mode)); } tng_function_status DECLSPECDLLEXPORT tng_block_read_next_ (tng_trajectory_t tng_data, - tng_gen_block_t block_data, - const tng_hash_mode *hash_mode) + tng_gen_block_t block_data, + const tng_hash_mode *hash_mode) { return(tng_block_read_next(tng_data, block_data, *hash_mode)); } tng_function_status DECLSPECDLLEXPORT tng_frame_set_read_next_ (tng_trajectory_t tng_data, - const tng_hash_mode *hash_mode) + const tng_hash_mode *hash_mode) { return(tng_frame_set_read_next(tng_data, *hash_mode)); } tng_function_status DECLSPECDLLEXPORT tng_frame_set_write_(tng_trajectory_t tng_data, - const tng_hash_mode *hash_mode) + const tng_hash_mode *hash_mode) { return(tng_frame_set_write(tng_data, *hash_mode)); } tng_function_status DECLSPECDLLEXPORT tng_frame_set_new_(tng_trajectory_t tng_data, - const int64_t *first_frame, - const int64_t *n_frames) + const int64_t *first_frame, + const int64_t *n_frames) { return(tng_frame_set_new(tng_data, *first_frame, *n_frames)); } tng_function_status DECLSPECDLLEXPORT tng_data_block_add_ (tng_trajectory_t tng_data, - const int64_t *id, - const char *block_name, - const tng_data_type *datatype, - const tng_block_type *block_type_flag, - int64_t *n_frames, - const int64_t *n_values_per_frame, - const int64_t *stride_length, - const int64_t *codec_id, - void *new_data, - int name_len) + const int64_t *id, + const char *block_name, + const tng_data_type *datatype, + const tng_block_type *block_type_flag, + int64_t *n_frames, + const int64_t *n_values_per_frame, + const int64_t *stride_length, + const int64_t *codec_id, + void *new_data, + int name_len) { char *name = malloc(name_len + 1); tng_function_status stat; @@ -12904,10 +13382,10 @@ tng_function_status DECLSPECDLLEXPORT tng_particle_data_block_add_ tng_function_status DECLSPECDLLEXPORT tng_frame_data_write_ (tng_trajectory_t tng_data, - const int64_t *frame_nr, - const int64_t *block_id, - const void *data, - const tng_hash_mode *hash_mode) + const int64_t *frame_nr, + const int64_t *block_id, + const void *data, + const tng_hash_mode *hash_mode) { return(tng_frame_data_write(tng_data, *frame_nr, *block_id, data, *hash_mode)); @@ -12915,12 +13393,12 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_data_write_ tng_function_status DECLSPECDLLEXPORT tng_frame_particle_data_write_ (tng_trajectory_t tng_data, - const int64_t *frame_nr, - const int64_t *block_id, - const int64_t *val_first_particle, - const int64_t *val_n_particles, - const void *data, - const tng_hash_mode *hash_mode) + const int64_t *frame_nr, + const int64_t *block_id, + const int64_t *val_first_particle, + const int64_t *val_n_particles, + const void *data, + const tng_hash_mode *hash_mode) { return(tng_frame_particle_data_write(tng_data, *frame_nr, *block_id, *val_first_particle, *val_n_particles, @@ -12929,10 +13407,10 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_particle_data_write_ tng_function_status DECLSPECDLLEXPORT tng_data_values_free_ (const tng_trajectory_t tng_data, - union data_values **values, - const int64_t *n_frames, - const int64_t *n_values_per_frame, - const tng_data_type *type) + union data_values **values, + const int64_t *n_frames, + const int64_t *n_values_per_frame, + const tng_data_type *type) { return(tng_data_values_free(tng_data, values, *n_frames, *n_values_per_frame, *type)); @@ -12940,11 +13418,11 @@ tng_function_status DECLSPECDLLEXPORT tng_data_values_free_ tng_function_status DECLSPECDLLEXPORT tng_particle_data_values_free_ (const tng_trajectory_t tng_data, - union data_values ***values, - const int64_t *n_frames, - const int64_t *n_particles, - const int64_t *n_values_per_frame, - const tng_data_type *type) + union data_values ***values, + const int64_t *n_frames, + const int64_t *n_particles, + const int64_t *n_values_per_frame, + const tng_data_type *type) { return(tng_particle_data_values_free(tng_data, values, *n_frames, *n_particles, *n_values_per_frame, *type)); @@ -12952,11 +13430,11 @@ tng_function_status DECLSPECDLLEXPORT tng_particle_data_values_free_ tng_function_status DECLSPECDLLEXPORT tng_data_get_ (tng_trajectory_t tng_data, - const int64_t *block_id, - union data_values ***values, - int64_t *n_frames, - int64_t *n_values_per_frame, - tng_data_type *type) + const int64_t *block_id, + union data_values ***values, + int64_t *n_frames, + int64_t *n_values_per_frame, + tng_data_type *type) { return(tng_data_get(tng_data, *block_id, values, n_frames, n_values_per_frame, type)); @@ -12964,13 +13442,13 @@ tng_function_status DECLSPECDLLEXPORT tng_data_get_ tng_function_status DECLSPECDLLEXPORT tng_data_interval_get_ (tng_trajectory_t tng_data, - const int64_t *block_id, - const int64_t *start_frame_nr, - const int64_t *end_frame_nr, - const tng_hash_mode *hash_mode, - union data_values ***values, - int64_t *n_values_per_frame, - tng_data_type *type) + const int64_t *block_id, + const int64_t *start_frame_nr, + const int64_t *end_frame_nr, + const tng_hash_mode *hash_mode, + union data_values ***values, + int64_t *n_values_per_frame, + tng_data_type *type) { return(tng_data_interval_get(tng_data, *block_id, *start_frame_nr, *end_frame_nr, *hash_mode, values, @@ -12979,12 +13457,12 @@ tng_function_status DECLSPECDLLEXPORT tng_data_interval_get_ tng_function_status DECLSPECDLLEXPORT tng_particle_data_get_ (tng_trajectory_t tng_data, - const int64_t *block_id, - union data_values ****values, - int64_t *n_frames, - int64_t *n_particles, - int64_t *n_values_per_frame, - tng_data_type *type) + const int64_t *block_id, + union data_values ****values, + int64_t *n_frames, + int64_t *n_particles, + int64_t *n_values_per_frame, + tng_data_type *type) { return(tng_particle_data_get(tng_data, *block_id, values, n_frames, n_particles, n_values_per_frame, type)); @@ -12992,14 +13470,14 @@ tng_function_status DECLSPECDLLEXPORT tng_particle_data_get_ tng_function_status DECLSPECDLLEXPORT tng_particle_data_interval_get_ (tng_trajectory_t tng_data, - const int64_t *block_id, - const int64_t *start_frame_nr, - const int64_t *end_frame_nr, - const tng_hash_mode *hash_mode, - union data_values ****values, - int64_t *n_particles, - int64_t *n_values_per_frame, - tng_data_type *type) + const int64_t *block_id, + const int64_t *start_frame_nr, + const int64_t *end_frame_nr, + const tng_hash_mode *hash_mode, + union data_values ****values, + int64_t *n_particles, + int64_t *n_values_per_frame, + tng_data_type *type) { return(tng_particle_data_interval_get(tng_data, *block_id, *start_frame_nr, *end_frame_nr, *hash_mode, values, @@ -13009,7 +13487,7 @@ tng_function_status DECLSPECDLLEXPORT tng_particle_data_interval_get_ tng_function_status DECLSPECDLLEXPORT tng_time_get_str_ (const tng_trajectory_t tng_data, - char *time, int64_t str_len) + char *time, int64_t str_len) { return(tng_time_get_str(tng_data, time)); } |