diff options
author | Magnus Lundborg <lundborg.magnus@gmail.com> | 2012-12-10 07:35:31 (GMT) |
---|---|---|
committer | Magnus Lundborg <lundborg.magnus@gmail.com> | 2012-12-10 07:35:31 (GMT) |
commit | 6f0c05b9a0f9913823742a59712d54821306262b (patch) | |
tree | d1baba30ec6dd3db71ecd3df80b9396cf7d2becb /src | |
parent | f4f0d515420e642e1f58ca44c831743a62aef10a (diff) |
Fix bugs in data retrieval functions.
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/tng_io.c | 115 | ||||
-rw-r--r-- | src/tests/tng_io_testing.c | 42 |
2 files changed, 130 insertions, 27 deletions
diff --git a/src/lib/tng_io.c b/src/lib/tng_io.c index 0c60145..437b3b5 100644 --- a/src/lib/tng_io.c +++ b/src/lib/tng_io.c @@ -6891,7 +6891,8 @@ tng_function_status tng_data_get(tng_trajectory_t tng_data, int64_t block_id, union data_values ***values) { - int i, block_index, size; + int64_t n_frames, n_vals_per_frame; + int i, j, block_index, size, len; struct tng_data *data, *new_data; struct tng_trajectory_frame_set *frame_set = &tng_data->current_trajectory_frame_set; @@ -6909,7 +6910,7 @@ tng_function_status tng_data_get(tng_trajectory_t tng_data, } } - if(block_index <= 0) + 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). */ @@ -6922,7 +6923,7 @@ tng_function_status tng_data_get(tng_trajectory_t tng_data, break; } } - if(block_index <= 0) + if(block_index < 0) { return(TNG_FAILURE); } @@ -6934,30 +6935,58 @@ tng_function_status tng_data_get(tng_trajectory_t tng_data, new_data->n_values_per_frame = 0; new_data->n_frames = 0; new_data->values = 0; - tng_allocate_data_mem(tng_data, new_data, data->n_frames, - data->n_values_per_frame); + n_vals_per_frame = data->n_values_per_frame; + if(tng_allocate_data_mem(tng_data, new_data, data->n_frames, + n_vals_per_frame) != TNG_SUCCESS) + { + return(TNG_CRITICAL); + } + n_frames = max(1, data->n_frames); + + *values = new_data->values; switch(data->datatype) { case TNG_CHAR_DATA: - size = 1; + for(i=n_frames; i--;) + { + for(j=n_vals_per_frame; j--;) + { + len = strlen(data->values[i][j].c) + 1; + (*values)[i][j].c = malloc(len); + strncpy((*values)[i][j].c, data->values[i][j].c, len); + } + } break; case TNG_INT_DATA: - size = sizeof(int64_t); + for(i=n_frames; i--;) + { + for(j=n_vals_per_frame; j--;) + { + (*values)[i][j].i = data->values[i][j].i; + } + } break; case TNG_FLOAT_DATA: - size = sizeof(float); + for(i=n_frames; i--;) + { + for(j=n_vals_per_frame; j--;) + { + (*values)[i][j].f = data->values[i][j].f; + } + } break; case TNG_DOUBLE_DATA: default: - size = sizeof(double); + for(i=n_frames; i--;) + { + for(j=n_vals_per_frame; j--;) + { + (*values)[i][j].d = data->values[i][j].d; + } + } } - memcpy(new_data->values, data->values, size * data->n_frames * - data->n_values_per_frame); - - *values = new_data->values; - return(TNG_SUCCESS); } @@ -6975,7 +7004,8 @@ tng_function_status tng_particle_data_get(tng_trajectory_t tng_data, int64_t block_id, union data_values ****values) { - int i, block_index, size; + int64_t n_frames, n_vals_per_frame; + int i, j, k, block_index, size, len; int64_t n_particles; struct tng_particle_data *data, *new_data; struct tng_trajectory_frame_set *frame_set = @@ -7035,6 +7065,7 @@ tng_function_status tng_particle_data_get(tng_trajectory_t tng_data, new_data->n_values_per_frame = 0; new_data->n_frames = 0; new_data->values = 0; + n_vals_per_frame = data->n_values_per_frame; if(tng_allocate_particle_data_mem(tng_data, new_data, data->n_frames, n_particles, data->n_values_per_frame) != TNG_SUCCESS) @@ -7042,27 +7073,63 @@ tng_function_status tng_particle_data_get(tng_trajectory_t tng_data, return(TNG_CRITICAL); } + n_frames = max(1, data->n_frames); + + *values = new_data->values; switch(data->datatype) { case TNG_CHAR_DATA: - size = 1; + for(i=n_frames; i--;) + { + for(j=n_particles; j--;) + { + for(k=n_vals_per_frame; k--;) + { + len = strlen(data->values[i][j][k].c) + 1; + (*values)[i][j][k].c = malloc(len); + strncpy((*values)[i][j][k].c, data->values[i][j][k].c, len); + } + } + } break; case TNG_INT_DATA: - size = sizeof(int64_t); + for(i=n_frames; i--;) + { + for(j=n_particles; j--;) + { + for(k=n_vals_per_frame; k--;) + { + (*values)[i][j][k].i = data->values[i][j][k].i; + } + } + } break; case TNG_FLOAT_DATA: - size = sizeof(float); + for(i=n_frames; i--;) + { + for(j=n_particles; j--;) + { + for(k=n_vals_per_frame; k--;) + { + (*values)[i][j][k].f = data->values[i][j][k].f; + } + } + } break; case TNG_DOUBLE_DATA: default: - size = sizeof(double); + for(i=n_frames; i--;) + { + for(j=n_particles; j--;) + { + for(k=n_vals_per_frame; k--;) + { + (*values)[i][j][k].d = data->values[i][j][k].d; + } + } + } } - memcpy(new_data->values, data->values, size * data->n_frames * - n_particles * data->n_values_per_frame); - - *values = new_data->values; - return(TNG_SUCCESS); } diff --git a/src/tests/tng_io_testing.c b/src/tests/tng_io_testing.c index e10e829..506a5b4 100644 --- a/src/tests/tng_io_testing.c +++ b/src/tests/tng_io_testing.c @@ -263,17 +263,43 @@ static tng_function_status tng_test_write_and_read_traj(tng_trajectory_t traj) return(stat); } +/* This test relies on knowing that the box shape is stored as double */ +tng_function_status tng_test_get_box_data(tng_trajectory_t traj) +{ + union data_values **values = 0; + + if(tng_data_get(traj, TNG_TRAJ_BOX_SHAPE, &values) != + TNG_SUCCESS) + { + printf("Failed getting box shape. %s: %d\n", __FILE__, __LINE__); + return(TNG_CRITICAL); + } + + + int64_t i; + + printf("Box shape:"); + + for(i = 0; i<9; i++) + { + printf("\t%f", (values[0][i]).d); + } + printf("\n"); + + return(TNG_SUCCESS); +} + /* This test relies on knowing that the positions are stored as float * and that the data is not sparse (i.e. as many frames in the data * as in the frame set */ -tng_function_status tng_test_get_particle_data(tng_trajectory_t traj) +tng_function_status tng_test_get_positions_data(tng_trajectory_t traj) { union data_values ***values = 0; if(tng_particle_data_get(traj, TNG_TRAJ_POSITIONS, &values) != TNG_SUCCESS) { - printf("Failed getting particle data. %s: %d\n", __FILE__, __LINE__); + printf("Failed getting particle positions. %s: %d\n", __FILE__, __LINE__); return(TNG_CRITICAL); } @@ -330,6 +356,16 @@ int main() printf("Test Read and write file:\t\t\tSucceeded.\n"); } + if(tng_test_get_box_data(&traj) != TNG_SUCCESS) + { + printf("Test Get data:\t\t\tFailed. %s: %d\n", + __FILE__, __LINE__); + } + else + { + printf("Test Get data:\t\t\tSucceeded.\n"); + } + if(tng_trajectory_destroy(&traj) == TNG_CRITICAL || tng_trajectory_init(&traj) == TNG_CRITICAL) { @@ -354,7 +390,7 @@ int main() printf("Test Write and read file:\t\t\tSucceeded.\n"); } - if(tng_test_get_particle_data(&traj) != TNG_SUCCESS) + if(tng_test_get_positions_data(&traj) != TNG_SUCCESS) { printf("Test Get particle data:\t\t\tFailed. %s: %d\n", __FILE__, __LINE__); |