From da4e46daafd15a4c1482653c60964b137b0b19aa Mon Sep 17 00:00:00 2001 From: austin Date: Wed, 4 Dec 2024 22:19:21 -0500 Subject: [PATCH] chunk tests --- src/main/c/includes/fluid/chunk.h | 2 +- src/main/c/src/fluid/chunk.c | 184 +++++++++++++----------------- test/c/fluid/chunk_tests.c | 36 ++++-- 3 files changed, 106 insertions(+), 116 deletions(-) diff --git a/src/main/c/includes/fluid/chunk.h b/src/main/c/includes/fluid/chunk.h index 956bdc99..63aa0c6b 100644 --- a/src/main/c/includes/fluid/chunk.h +++ b/src/main/c/includes/fluid/chunk.h @@ -131,7 +131,7 @@ LIBRARY_API void fluid_sparse_array_add_chunk(SparseChunkArray * array, Chunk * /** * Adds a chunk to the sparse array */ -LIBRARY_API void fluid_sparse_array_remove_chunk(SparseChunkArray * array, Chunk * chunk, int x, int y, int z, int copy); +LIBRARY_API void fluid_sparse_array_remove_chunk(SparseChunkArray * array, Chunk * chunk, int x, int y, int z); /** * Gets the index for a point in the chunk diff --git a/src/main/c/src/fluid/chunk.c b/src/main/c/src/fluid/chunk.c index 39a467a1..6f6b1ae8 100644 --- a/src/main/c/src/fluid/chunk.c +++ b/src/main/c/src/fluid/chunk.c @@ -18,19 +18,35 @@ LIBRARY_API SparseChunkArray * fluid_sparse_array_create(){ //allocate the object itself SparseChunkArray * rVal = (SparseChunkArray *)malloc(sizeof(SparseChunkArray)); + if(rVal == NULL){ + return NULL; + } //allocate the sub-arrays - rVal->d = (float *)malloc(SPARSE_ARRAY_FULL_SIZE * sizeof(float)); - rVal->d0 = (float *)malloc(SPARSE_ARRAY_FULL_SIZE * sizeof(float)); - rVal->u = (float *)malloc(SPARSE_ARRAY_FULL_SIZE * sizeof(float)); - rVal->v = (float *)malloc(SPARSE_ARRAY_FULL_SIZE * sizeof(float)); - rVal->w = (float *)malloc(SPARSE_ARRAY_FULL_SIZE * sizeof(float)); - rVal->u0 = (float *)malloc(SPARSE_ARRAY_FULL_SIZE * sizeof(float)); - rVal->v0 = (float *)malloc(SPARSE_ARRAY_FULL_SIZE * sizeof(float)); - rVal->w0 = (float *)malloc(SPARSE_ARRAY_FULL_SIZE * sizeof(float)); + rVal->d = (float *)calloc(1,SPARSE_ARRAY_FULL_SIZE * sizeof(float)); + rVal->d0 = (float *)calloc(1,SPARSE_ARRAY_FULL_SIZE * sizeof(float)); + rVal->u = (float *)calloc(1,SPARSE_ARRAY_FULL_SIZE * sizeof(float)); + rVal->v = (float *)calloc(1,SPARSE_ARRAY_FULL_SIZE * sizeof(float)); + rVal->w = (float *)calloc(1,SPARSE_ARRAY_FULL_SIZE * sizeof(float)); + rVal->u0 = (float *)calloc(1,SPARSE_ARRAY_FULL_SIZE * sizeof(float)); + rVal->v0 = (float *)calloc(1,SPARSE_ARRAY_FULL_SIZE * sizeof(float)); + rVal->w0 = (float *)calloc(1,SPARSE_ARRAY_FULL_SIZE * sizeof(float)); //allocate the chunk-tracking array - rVal->chunks = (Chunk **)malloc(sizeof(Chunk *) * SPARSE_ARRAY_TOTAL_CHUNKS); + rVal->chunks = (Chunk **)calloc(1,sizeof(Chunk *) * SPARSE_ARRAY_TOTAL_CHUNKS); + + if(rVal->d == NULL){ + free(rVal->w0); + free(rVal->v0); + free(rVal->u0); + free(rVal->w); + free(rVal->v); + free(rVal->u); + free(rVal->d0); + free(rVal->d); + free(rVal); + return NULL; + } return rVal; } @@ -80,46 +96,46 @@ LIBRARY_API void fluid_sparse_array_add_chunk(SparseChunkArray * array, Chunk * j = n + 1; k = o + 1; array->d[GVI( - minPos + offsetX + i, - minPos + offsetY + j, - minPos + offsetZ + k + minPos + offsetX + m, + minPos + offsetY + n, + minPos + offsetZ + o )] = chunk->d[CENTER_LOC][IX(i,j,k)]; array->d0[GVI( - minPos + offsetX + i, - minPos + offsetY + j, - minPos + offsetZ + k + minPos + offsetX + m, + minPos + offsetY + n, + minPos + offsetZ + o )] = chunk->d0[CENTER_LOC][IX(i,j,k)]; array->u[GVI( - minPos + offsetX + i, - minPos + offsetY + j, - minPos + offsetZ + k + minPos + offsetX + m, + minPos + offsetY + n, + minPos + offsetZ + o )] = chunk->u[CENTER_LOC][IX(i,j,k)]; array->v[GVI( - minPos + offsetX + i, - minPos + offsetY + j, - minPos + offsetZ + k + minPos + offsetX + m, + minPos + offsetY + n, + minPos + offsetZ + o )] = chunk->v[CENTER_LOC][IX(i,j,k)]; array->w[GVI( - minPos + offsetX + i, - minPos + offsetY + j, - minPos + offsetZ + k + minPos + offsetX + m, + minPos + offsetY + n, + minPos + offsetZ + o )] = chunk->w[CENTER_LOC][IX(i,j,k)]; array->u0[GVI( - minPos + offsetX + i, - minPos + offsetY + j, - minPos + offsetZ + k + minPos + offsetX + m, + minPos + offsetY + n, + minPos + offsetZ + o )] = chunk->d[CENTER_LOC][IX(i,j,k)]; array->v0[GVI( - minPos + offsetX + i, - minPos + offsetY + j, - minPos + offsetZ + k + minPos + offsetX + m, + minPos + offsetY + n, + minPos + offsetZ + o )] = chunk->d[CENTER_LOC][IX(i,j,k)]; array->w0[GVI( - minPos + offsetX + i, - minPos + offsetY + j, - minPos + offsetZ + k + minPos + offsetX + m, + minPos + offsetY + n, + minPos + offsetZ + o )] = chunk->d[CENTER_LOC][IX(i,j,k)]; } } @@ -132,12 +148,12 @@ LIBRARY_API void fluid_sparse_array_add_chunk(SparseChunkArray * array, Chunk * /** * Adds a chunk to the sparse array */ -LIBRARY_API void fluid_sparse_array_remove_chunk(SparseChunkArray * array, Chunk * chunk, int x, int y, int z, int copy){ +LIBRARY_API void fluid_sparse_array_remove_chunk(SparseChunkArray * array, Chunk * chunk, int x, int y, int z){ //solve chunk offsets int offsetX = solveOffset(x); int offsetY = solveOffset(y); int offsetZ = solveOffset(z); - int minPos = SPARSE_ARRAY_BORDER_SIZE / 2; + int minPos = (SPARSE_ARRAY_BORDER_SIZE / 2); int i, j, k; for(int m = 0; m < MAIN_ARRAY_DIM; m++){ @@ -147,96 +163,50 @@ LIBRARY_API void fluid_sparse_array_remove_chunk(SparseChunkArray * array, Chunk j = n + 1; k = o + 1; - //copy density to chunk - chunk->d[CENTER_LOC][IX(i,j,k)] = array->d[GVI( - minPos + offsetX + i, - minPos + offsetY + j, - minPos + offsetZ + k - )]; - chunk->d0[CENTER_LOC][IX(i,j,k)] = array->d0[GVI( - minPos + offsetX + i, - minPos + offsetY + j, - minPos + offsetZ + k - )]; - //zero density in the main array array->d[GVI( - minPos + offsetX + i, - minPos + offsetY + j, - minPos + offsetZ + k + minPos + offsetX + m, + minPos + offsetY + n, + minPos + offsetZ + o )] = 0; array->d0[GVI( - minPos + offsetX + i, - minPos + offsetY + j, - minPos + offsetZ + k + minPos + offsetX + m, + minPos + offsetY + n, + minPos + offsetZ + o )] = 0; - //copy velocity to the chunk - chunk->u[CENTER_LOC][IX(i,j,k)] = array->u[GVI( - minPos + offsetX + i, - minPos + offsetY + j, - minPos + offsetZ + k - )]; - chunk->v[CENTER_LOC][IX(i,j,k)] = array->v[GVI( - minPos + offsetX + i, - minPos + offsetY + j, - minPos + offsetZ + k - )]; - chunk->w[CENTER_LOC][IX(i,j,k)] = array->w[GVI( - minPos + offsetX + i, - minPos + offsetY + j, - minPos + offsetZ + k - )]; - //zero velocity in the main array array->u[GVI( - minPos + offsetX + i, - minPos + offsetY + j, - minPos + offsetZ + k + minPos + offsetX + m, + minPos + offsetY + n, + minPos + offsetZ + o )] = 0; array->v[GVI( - minPos + offsetX + i, - minPos + offsetY + j, - minPos + offsetZ + k + minPos + offsetX + m, + minPos + offsetY + n, + minPos + offsetZ + o )] = 0; array->w[GVI( - minPos + offsetX + i, - minPos + offsetY + j, - minPos + offsetZ + k - )]; - - //copy velocity deltas to the chunk - chunk->d[CENTER_LOC][IX(i,j,k)] = array->u0[GVI( - minPos + offsetX + i, - minPos + offsetY + j, - minPos + offsetZ + k - )]; - chunk->d[CENTER_LOC][IX(i,j,k)] = array->v0[GVI( - minPos + offsetX + i, - minPos + offsetY + j, - minPos + offsetZ + k - )]; - chunk->d[CENTER_LOC][IX(i,j,k)] = array->w0[GVI( - minPos + offsetX + i, - minPos + offsetY + j, - minPos + offsetZ + k + minPos + offsetX + m, + minPos + offsetY + n, + minPos + offsetZ + o )]; //zero elocity deltas in the main array array->u0[GVI( - minPos + offsetX + i, - minPos + offsetY + j, - minPos + offsetZ + k + minPos + offsetX + m, + minPos + offsetY + n, + minPos + offsetZ + o )] = 0; array->v0[GVI( - minPos + offsetX + i, - minPos + offsetY + j, - minPos + offsetZ + k + minPos + offsetX + m, + minPos + offsetY + n, + minPos + offsetZ + o )] = 0; array->w0[GVI( - minPos + offsetX + i, - minPos + offsetY + j, - minPos + offsetZ + k + minPos + offsetX + m, + minPos + offsetY + n, + minPos + offsetZ + o )] = 0; } } @@ -264,12 +234,12 @@ int GCI(int x, int y, int z){ * Gets the index of the value at a given position within the sparse array */ int GVI(int x, int y, int z){ - return x * SPARSE_ARRAY_RAW_DIM * SPARSE_ARRAY_RAW_DIM + y * SPARSE_ARRAY_RAW_DIM + z; + return (x * SPARSE_ARRAY_RAW_DIM * SPARSE_ARRAY_RAW_DIM) + (y * SPARSE_ARRAY_RAW_DIM) + z; } /** * Solves the offset into the sparse chunk aray to iterate from for this chunk's position */ int solveOffset(int chunkPos){ - return SPARSE_ARRAY_BORDER_SIZE / 2 + chunkPos * MAIN_ARRAY_DIM; + return (chunkPos * MAIN_ARRAY_DIM); } diff --git a/test/c/fluid/chunk_tests.c b/test/c/fluid/chunk_tests.c index 0f38665c..6ef1f826 100644 --- a/test/c/fluid/chunk_tests.c +++ b/test/c/fluid/chunk_tests.c @@ -13,6 +13,10 @@ int fluid_chunk_tests(int argc, char **argv){ int rVal = 0; //allocate a sparse array SparseChunkArray * sparseArray = fluid_sparse_array_create(); + if(sparseArray == NULL){ + printf("Failed to allocate sparseArray!\n"); + return 1; + } //create chunks to add to the sparse array Chunk * chunk1 = (Chunk *)malloc(sizeof(Chunk)); @@ -43,23 +47,39 @@ int fluid_chunk_tests(int argc, char **argv){ for(int x = 0; x < DIM; x++){ for(int y = 0; y < DIM; y++){ for(int z = 0; z < DIM; z++){ - chunk1->d[CENTER_LOC][x * DIM * DIM + y * DIM + z] = 2; + chunk2->d[CENTER_LOC][x * DIM * DIM + y * DIM + z] = 2; } } } //add a chunk fluid_sparse_array_add_chunk(sparseArray,chunk1,0,0,0); - rVal =+ assertEquals(sparseArray->d[fluid_sparse_array_get_index(sparseArray,0,0,0)],0,"index 0,0,0 in the sparse array should be a border value -- %d %d\n"); - rVal =+ assertEquals(chunk1->d[CENTER_LOC][IX(1,1,1)],1,"chunk1 should have a value of 1 -- %d %d\n"); - rVal =+ assertEquals(sparseArray->d[fluid_sparse_array_get_index(sparseArray,1,1,1)],chunk1->d[CENTER_LOC][IX(1,1,1)],"index 1,1,1 in the sparse array should be 0,0,0 of the chunk stored at 0,0,0 -- %d %d\n"); + rVal += assertEquals(sparseArray->d[fluid_sparse_array_get_index(sparseArray,0,0,0)],0,"index 0,0,0 in the sparse array should be a border value -- %d %d\n"); + rVal += assertEquals(chunk1->d[CENTER_LOC][IX(1,1,1)],1,"chunk1 should have a value of 1 -- %d %d\n"); + rVal += assertEquals(sparseArray->d[fluid_sparse_array_get_index(sparseArray,1,1,1)],chunk1->d[CENTER_LOC][IX(1,1,1)],"index 1,1,1 in the sparse array should be 1,1,1 of the chunk stored at 0,0,0 -- %d %d\n"); //make sure adding a second chunk doesn't override the first one fluid_sparse_array_add_chunk(sparseArray,chunk2,1,0,0); - rVal =+ assertEquals(sparseArray->d[fluid_sparse_array_get_index(sparseArray,0,0,0)],0,"index 0,0,0 in the sparse array should be a border value -- %d %d\n"); - rVal =+ assertEquals(sparseArray->d[fluid_sparse_array_get_index(sparseArray,1,1,1)],chunk1->d[CENTER_LOC][IX(1,1,1)],"index 1,1,1 in the sparse array should be 0,0,0 of the chunk stored at 0,0,0 -- %d %d\n"); - rVal =+ assertEquals(sparseArray->d[fluid_sparse_array_get_index(sparseArray,16,16,16)],chunk1->d[CENTER_LOC][IX(15,15,15)],"index 16,16,16 in the sparse array should be 15,15,15 of the chunk stored at 0,0,0 -- %d %d\n"); - rVal =+ assertEquals(sparseArray->d[fluid_sparse_array_get_index(sparseArray,17,17,17)],chunk2->d[CENTER_LOC][IX(1,1,1)],"index 17,17,17 in the sparse array should be 1,1,1 of the chunk stored at 1,0,0 -- %d %d\n"); + rVal += assertEquals(sparseArray->d[fluid_sparse_array_get_index(sparseArray,0,0,0)],0,"index 0,0,0 in the sparse array should be a border value -- %d %d\n"); + rVal += assertEquals(sparseArray->d[fluid_sparse_array_get_index(sparseArray,1,1,1)],chunk1->d[CENTER_LOC][IX(1,1,1)],"index 1,1,1 in the sparse array should be 0,0,0 of the chunk stored at 0,0,0 -- %d %d\n"); + rVal += assertEquals(sparseArray->d[fluid_sparse_array_get_index(sparseArray,16,16,16)],chunk1->d[CENTER_LOC][IX(15,15,15)],"index 16,16,16 in the sparse array should be 15,15,15 of the chunk stored at 0,0,0 -- %d %d\n"); + rVal += assertEquals(sparseArray->d[fluid_sparse_array_get_index(sparseArray,17,1,1)],chunk2->d[CENTER_LOC][IX(1,1,1)],"index 17,1,1 in the sparse array should be 1,1,1 of the chunk stored at 1,0,0 -- %d %d\n"); + + + //set some value in sparse array for future tests + sparseArray->d[fluid_sparse_array_get_index(sparseArray,17,1,1)] = 6; + sparseArray->d[fluid_sparse_array_get_index(sparseArray,1,1,1)] = 6; + + //check zeroing out behavior of removing chunks + fluid_sparse_array_remove_chunk(sparseArray,chunk2,1,0,0); + rVal += assertEquals(sparseArray->d[fluid_sparse_array_get_index(sparseArray,17,1,1)],0,"index 17,1,1 in the sparse array should be 1,1,1 of the chunk stored at 1,0,0 -- %d %d\n"); + + //check no-copying behavior of removing chunks + rVal += assertNotEquals(chunk2->d[CENTER_LOC][IX(1,1,1)],6,"chunk should NOT have received the value from the sparse array -- %d %d \n"); + + //check no-copying behavior of removing chunks + fluid_sparse_array_remove_chunk(sparseArray,chunk1,0,0,0); + rVal += assertNotEquals(chunk1->d[CENTER_LOC][IX(1,1,1)],6,"chunk should have received the value from the sparse array -- %d %d \n"); //free a sparse array