From 08177bebed8d554f8ac131ff5c08809ef6c81853 Mon Sep 17 00:00:00 2001 From: austin Date: Mon, 9 Dec 2024 19:02:23 -0500 Subject: [PATCH] velocity diffusion testing --- src/main/c/includes/fluid/queue/chunk.h | 7 + .../c/includes/fluid/sim/grid2/utilities.h | 11 +- .../c/includes/fluid/sim/grid2/velocity.h | 2 +- src/main/c/src/fluid/sim/grid2/utilities.c | 11 ++ src/main/c/src/fluid/sim/grid2/velocity.c | 2 +- .../c/fluid/sim/cellular/cellular_tests.c | 98 +++++------ .../c/fluid/sim/grid2/density_diffuse_tests.c | 62 ++++++- .../fluid/sim/grid2/velocity_diffuse_tests.c | 155 ++++++++++++++++++ src/test/c/util/chunk_test_utils.c | 90 +++++++++- src/test/c/util/chunk_test_utils.h | 20 ++- 10 files changed, 389 insertions(+), 69 deletions(-) create mode 100644 src/test/c/fluid/sim/grid2/velocity_diffuse_tests.c diff --git a/src/main/c/includes/fluid/queue/chunk.h b/src/main/c/includes/fluid/queue/chunk.h index 7e737a32..74b40add 100644 --- a/src/main/c/includes/fluid/queue/chunk.h +++ b/src/main/c/includes/fluid/queue/chunk.h @@ -6,7 +6,14 @@ #ifndef CHUNK_H #define CHUNK_H +/** + * The minimum fluid value + */ #define MIN_FLUID_VALUE 0.0f + +/** + * The maximum fluid value + */ #define MAX_FLUID_VALUE 1.0f /** diff --git a/src/main/c/includes/fluid/sim/grid2/utilities.h b/src/main/c/includes/fluid/sim/grid2/utilities.h index fb99441d..344d0de9 100644 --- a/src/main/c/includes/fluid/sim/grid2/utilities.h +++ b/src/main/c/includes/fluid/sim/grid2/utilities.h @@ -24,13 +24,6 @@ LIBRARY_API void fluid_grid2_setBoundsToNeighborsRaw( - - - - - - - /** * Sums the density of the chunk */ @@ -38,6 +31,10 @@ double fluid_grid2_calculateSum(float ** d); +/** + * Flips two array matricies + */ +LIBRARY_API void fluid_grid2_flip_arrays(float ** array1, float ** array2); diff --git a/src/main/c/includes/fluid/sim/grid2/velocity.h b/src/main/c/includes/fluid/sim/grid2/velocity.h index 304939f5..0b3f69ca 100644 --- a/src/main/c/includes/fluid/sim/grid2/velocity.h +++ b/src/main/c/includes/fluid/sim/grid2/velocity.h @@ -117,7 +117,7 @@ void fluid_grid2_advect_velocity(int b, float ** jrd, float ** jrd0, float * u, /* * Solves vector diffusion along all axis */ -void fluid_grid2_solveVectorDiffuse ( +LIBRARY_API void fluid_grid2_solveVectorDiffuse ( float ** jru, float ** jrv, float ** jrw, diff --git a/src/main/c/src/fluid/sim/grid2/utilities.c b/src/main/c/src/fluid/sim/grid2/utilities.c index 5ff62bb4..804232f1 100644 --- a/src/main/c/src/fluid/sim/grid2/utilities.c +++ b/src/main/c/src/fluid/sim/grid2/utilities.c @@ -91,4 +91,15 @@ double fluid_grid2_calculateSum(float ** d){ return rVal; } +/** + * Flips two array matricies + */ +LIBRARY_API void fluid_grid2_flip_arrays(float ** array1, float ** array2){ + float * tmpArr; + for(int j = 0; j < 27; j++){ + tmpArr = array1[j]; + array1[j] = array2[j]; + array2[j] = tmpArr; + } +} diff --git a/src/main/c/src/fluid/sim/grid2/velocity.c b/src/main/c/src/fluid/sim/grid2/velocity.c index b678f5cb..37e7c622 100644 --- a/src/main/c/src/fluid/sim/grid2/velocity.c +++ b/src/main/c/src/fluid/sim/grid2/velocity.c @@ -36,7 +36,7 @@ void fluid_grid2_addSourceToVectors /* * Solves vector diffusion along all axis */ -void fluid_grid2_solveVectorDiffuse ( +LIBRARY_API void fluid_grid2_solveVectorDiffuse ( float ** jru, float ** jrv, float ** jrw, diff --git a/src/test/c/fluid/sim/cellular/cellular_tests.c b/src/test/c/fluid/sim/cellular/cellular_tests.c index 4a32fc86..6fdc9e52 100644 --- a/src/test/c/fluid/sim/cellular/cellular_tests.c +++ b/src/test/c/fluid/sim/cellular/cellular_tests.c @@ -394,13 +394,13 @@ int fluid_sim_cellular_stability_test1(){ queue[1]->d[CENTER_LOC][IX(1,1,1)] = CELLULAR_TEST_PLACE_VAL; //check sum beforehand - float originalSum = chunk_queue_sum(queue); + float originalSum = chunk_queue_sum_density(queue); //dispatch and simulate int frameCount = 100; for(int frameCounter = 0; frameCounter < frameCount; frameCounter++){ - float currentSum = chunk_queue_sum(queue); + float currentSum = chunk_queue_sum_density(queue); if(currentSum != originalSum){ printf("Failed to equal sums! \n"); rVal += assertEqualsFloat(currentSum,originalSum,"Sums are not identical! %f %f \n"); @@ -424,7 +424,7 @@ int fluid_sim_cellular_stability_test1(){ // printf("transfered value: %f \n",transferedValue); //check sum beforehand - float afterSum = chunk_queue_sum(queue); + float afterSum = chunk_queue_sum_density(queue); rVal += assertEqualsFloat(originalSum,afterSum,"cellular sim was unstable! %f %f \n"); @@ -463,13 +463,13 @@ int fluid_sim_cellular_stability_test2(){ queue[13]->d[CENTER_LOC][IX(5,5,5)] = CELLULAR_TEST_PLACE_VAL; //check sum beforehand - float originalSum = chunk_queue_sum(queue); + float originalSum = chunk_queue_sum_density(queue); //dispatch and simulate int frameCount = 100; for(int frameCounter = 0; frameCounter < frameCount; frameCounter++){ - float currentSum = chunk_queue_sum(queue); + float currentSum = chunk_queue_sum_density(queue); if(currentSum != originalSum){ printf("Failed to equal sums! \n"); rVal += assertEqualsFloat(currentSum,originalSum,"Sums are not identical! %f %f \n"); @@ -493,7 +493,7 @@ int fluid_sim_cellular_stability_test2(){ // printf("transfered value: %f \n",transferedValue); //check sum beforehand - float afterSum = chunk_queue_sum(queue); + float afterSum = chunk_queue_sum_density(queue); rVal += assertEqualsFloat(originalSum,afterSum,"cellular sim was unstable! %f %f \n"); @@ -532,13 +532,13 @@ int fluid_sim_cellular_stability_test3(){ queue[13]->d[CENTER_LOC][IX(5,5,5)] = MAX_FLUID_VALUE; //check sum beforehand - float originalSum = chunk_queue_sum(queue); + float originalSum = chunk_queue_sum_density(queue); //dispatch and simulate int frameCount = 100; int frameCounter; for(frameCounter = 0; frameCounter < frameCount; frameCounter++){ - float currentSum = chunk_queue_sum(queue); + float currentSum = chunk_queue_sum_density(queue); float delta = fabs(originalSum - currentSum); if(delta > FLUID_CELLULAR_TOLLERABLE_LOSS_THRESHOLD2){ printf("Failed to equal sums! \n"); @@ -562,7 +562,7 @@ int fluid_sim_cellular_stability_test3(){ fluid_solve_bounds(chunkCount,queue,env); //check sum beforehand - float afterSum = chunk_queue_sum(queue); + float afterSum = chunk_queue_sum_density(queue); //diff the sums to see if we've changed value a lot float delta = fabs(originalSum - afterSum); @@ -603,13 +603,13 @@ int fluid_sim_cellular_stability_test4(){ queue[13]->d[CENTER_LOC][IX(1,1,1)] = MAX_FLUID_VALUE; //check sum beforehand - float originalSum = chunk_queue_sum(queue); + float originalSum = chunk_queue_sum_density(queue); //dispatch and simulate int frameCount = 100; int frameCounter; for(frameCounter = 0; frameCounter < frameCount; frameCounter++){ - float currentSum = chunk_queue_sum(queue); + float currentSum = chunk_queue_sum_density(queue); float delta = fabs(originalSum - currentSum); if(delta > FLUID_CELLULAR_TOLLERABLE_LOSS_THRESHOLD2){ printf("Failed to equal sums! \n"); @@ -633,7 +633,7 @@ int fluid_sim_cellular_stability_test4(){ fluid_solve_bounds(chunkCount,queue,env); //check sum beforehand - float afterSum = chunk_queue_sum(queue); + float afterSum = chunk_queue_sum_density(queue); //diff the sums to see if we've changed value a lot float delta = fabs(originalSum - afterSum); @@ -681,13 +681,13 @@ int fluid_sim_cellular_stability_test5(){ queue[13]->d[CENTER_LOC][IX(DIM-2,DIM-2,DIM-2)] = MAX_FLUID_VALUE; //check sum beforehand - float originalSum = chunk_queue_sum(queue); + float originalSum = chunk_queue_sum_density(queue); //dispatch and simulate int frameCount = 100; int frameCounter; for(frameCounter = 0; frameCounter < frameCount; frameCounter++){ - float currentSum = chunk_queue_sum(queue); + float currentSum = chunk_queue_sum_density(queue); float delta = fabs(originalSum - currentSum); if(delta > FLUID_CELLULAR_TOLLERABLE_LOSS_THRESHOLD2){ printf("Failed to equal sums! \n"); @@ -711,7 +711,7 @@ int fluid_sim_cellular_stability_test5(){ fluid_solve_bounds(chunkCount,queue,env); //check sum beforehand - float afterSum = chunk_queue_sum(queue); + float afterSum = chunk_queue_sum_density(queue); //diff the sums to see if we've changed value a lot float delta = fabs(originalSum - afterSum); @@ -759,13 +759,13 @@ int fluid_sim_cellular_stability_test6(){ queue[13]->d[CENTER_LOC][IX(2,2,2)] = MAX_FLUID_VALUE; //check sum beforehand - float originalSum = chunk_queue_sum(queue); + float originalSum = chunk_queue_sum_density(queue); //dispatch and simulate int frameCount = 100; int frameCounter; for(frameCounter = 0; frameCounter < frameCount; frameCounter++){ - float currentSum = chunk_queue_sum(queue); + float currentSum = chunk_queue_sum_density(queue); float delta = fabs(originalSum - currentSum); if(delta > FLUID_CELLULAR_TOLLERABLE_LOSS_THRESHOLD2){ printf("Failed to equal sums! \n"); @@ -789,7 +789,7 @@ int fluid_sim_cellular_stability_test6(){ fluid_solve_bounds(chunkCount,queue,env); //check sum beforehand - float afterSum = chunk_queue_sum(queue); + float afterSum = chunk_queue_sum_density(queue); //diff the sums to see if we've changed value a lot float delta = fabs(originalSum - afterSum); @@ -838,19 +838,19 @@ int fluid_sim_cellular_stability_test7(){ } //check sum beforehand - float originalSum = chunk_queue_sum(queue); + float originalSum = chunk_queue_sum_density(queue); //dispatch and simulate int frameCount = 1000; int frameCounter; for(frameCounter = 0; frameCounter < frameCount; frameCounter++){ - float currentSum = chunk_queue_sum(queue); + float currentSum = chunk_queue_sum_density(queue); // printf("frame: %d --- %f \n", frameCounter,currentSum); fluid_solve_bounds(chunkCount,queue,env); fluid_dispatch(chunkCount,queue,env,FLUID_DISPATCHER_OVERRIDE_CELLULAR); fluid_simulate(env); fluid_solve_bounds(chunkCount,queue,env); - float postSum = chunk_queue_sum(queue); + float postSum = chunk_queue_sum_density(queue); float delta = fabs(originalSum - postSum); if(delta > FLUID_CELLULAR_TOLLERABLE_LOSS_THRESHOLD1){ printf("Failed to equal sums! \n"); @@ -869,7 +869,7 @@ int fluid_sim_cellular_stability_test7(){ fluid_solve_bounds(chunkCount,queue,env); //check sum beforehand - float afterSum = chunk_queue_sum(queue); + float afterSum = chunk_queue_sum_density(queue); //diff the sums to see if we've changed value a lot float delta = fabs(originalSum - afterSum); @@ -917,13 +917,13 @@ int fluid_sim_cellular_stability_test8(){ fluid_sim_cellular_test_get_chunk(queue,0,0,0)->d[CENTER_LOC][IX(DIM-2,DIM-2,DIM-2)] = MAX_FLUID_VALUE; //check sum beforehand - float originalSum = chunk_queue_sum(queue); + float originalSum = chunk_queue_sum_density(queue); //dispatch and simulate int frameCount = 100; int frameCounter; for(frameCounter = 0; frameCounter < frameCount; frameCounter++){ - float currentSum = chunk_queue_sum(queue); + float currentSum = chunk_queue_sum_density(queue); float delta = fabs(originalSum - currentSum); if(delta > FLUID_CELLULAR_TOLLERABLE_LOSS_THRESHOLD2){ printf("Failed to equal sums! \n"); @@ -947,7 +947,7 @@ int fluid_sim_cellular_stability_test8(){ fluid_solve_bounds(chunkCount,queue,env); //check sum beforehand - float afterSum = chunk_queue_sum(queue); + float afterSum = chunk_queue_sum_density(queue); //diff the sums to see if we've changed value a lot float delta = fabs(originalSum - afterSum); @@ -996,13 +996,13 @@ int fluid_sim_cellular_stability_test9(){ fluid_sim_cellular_test_get_chunk(queue,0,1,0)->d[CENTER_LOC][IX(DIM-2,DIM-2,DIM-2)] = MAX_FLUID_VALUE; //check sum beforehand - float originalSum = chunk_queue_sum(queue); + float originalSum = chunk_queue_sum_density(queue); //dispatch and simulate int frameCount = 100; int frameCounter; for(frameCounter = 0; frameCounter < frameCount; frameCounter++){ - float currentSum = chunk_queue_sum(queue); + float currentSum = chunk_queue_sum_density(queue); float delta = fabs(originalSum - currentSum); if(delta > FLUID_CELLULAR_TOLLERABLE_LOSS_THRESHOLD2){ printf("Failed to equal sums! \n"); @@ -1026,7 +1026,7 @@ int fluid_sim_cellular_stability_test9(){ fluid_solve_bounds(chunkCount,queue,env); //check sum beforehand - float afterSum = chunk_queue_sum(queue); + float afterSum = chunk_queue_sum_density(queue); //diff the sums to see if we've changed value a lot float delta = fabs(originalSum - afterSum); @@ -1074,13 +1074,13 @@ int fluid_sim_cellular_stability_test10(){ fluid_sim_cellular_test_get_chunk(queue,1,1,1)->d[CENTER_LOC][IX(DIM-2,DIM-2,DIM-2)] = MAX_FLUID_VALUE; //check sum beforehand - float originalSum = chunk_queue_sum(queue); + float originalSum = chunk_queue_sum_density(queue); //dispatch and simulate int frameCount = 100; int frameCounter; for(frameCounter = 0; frameCounter < frameCount; frameCounter++){ - float currentSum = chunk_queue_sum(queue); + float currentSum = chunk_queue_sum_density(queue); float delta = fabs(originalSum - currentSum); if(delta > FLUID_CELLULAR_TOLLERABLE_LOSS_THRESHOLD2){ printf("Failed to equal sums! \n"); @@ -1104,7 +1104,7 @@ int fluid_sim_cellular_stability_test10(){ fluid_solve_bounds(chunkCount,queue,env); //check sum beforehand - float afterSum = chunk_queue_sum(queue); + float afterSum = chunk_queue_sum_density(queue); //diff the sums to see if we've changed value a lot float delta = fabs(originalSum - afterSum); @@ -1145,13 +1145,13 @@ int fluid_sim_cellular_stability_test11(){ chunk_fill_real(queue[10]->d[CENTER_LOC],CELLULAR_TEST_PLACE_VAL); //check sum beforehand - float originalSum = chunk_queue_sum(queue); + float originalSum = chunk_queue_sum_density(queue); //dispatch and simulate int frameCount = 1000; int frameCounter; for(frameCounter = 0; frameCounter < frameCount; frameCounter++){ - float currentSum = chunk_queue_sum(queue); + float currentSum = chunk_queue_sum_density(queue); float delta = fabs(originalSum - currentSum); if(delta > FLUID_CELLULAR_TOLLERABLE_LOSS_THRESHOLD1){ printf("Failed to equal sums! \n"); @@ -1174,7 +1174,7 @@ int fluid_sim_cellular_stability_test11(){ fluid_solve_bounds(chunkCount,queue,env); //check sum beforehand - float afterSum = chunk_queue_sum(queue); + float afterSum = chunk_queue_sum_density(queue); //diff the sums to see if we've changed value a lot float delta = fabs(originalSum - afterSum); @@ -1219,13 +1219,13 @@ int fluid_sim_cellular_stability_test12(){ } //check sum beforehand - float originalSum = chunk_queue_sum(queue); + float originalSum = chunk_queue_sum_density(queue); //dispatch and simulate int frameCount = 1000; int frameCounter; for(frameCounter = 0; frameCounter < frameCount; frameCounter++){ - float currentSum = chunk_queue_sum(queue); + float currentSum = chunk_queue_sum_density(queue); float delta = fabs(originalSum - currentSum); if(delta > FLUID_CELLULAR_TOLLERABLE_LOSS_THRESHOLD1){ printf("Failed to equal sums! \n"); @@ -1248,7 +1248,7 @@ int fluid_sim_cellular_stability_test12(){ fluid_solve_bounds(chunkCount,queue,env); //check sum beforehand - float afterSum = chunk_queue_sum(queue); + float afterSum = chunk_queue_sum_density(queue); //diff the sums to see if we've changed value a lot float delta = fabs(originalSum - afterSum); @@ -1298,13 +1298,13 @@ int fluid_sim_cellular_stability_test13(){ } //check sum beforehand - float originalSum = chunk_queue_sum(queue); + float originalSum = chunk_queue_sum_density(queue); //dispatch and simulate int frameCount = 1000; int frameCounter; for(frameCounter = 0; frameCounter < frameCount; frameCounter++){ - float currentSum = chunk_queue_sum(queue); + float currentSum = chunk_queue_sum_density(queue); float delta = fabs(originalSum - currentSum); if(delta > FLUID_CELLULAR_TOLLERABLE_LOSS_THRESHOLD1){ printf("Failed to equal sums! \n"); @@ -1327,7 +1327,7 @@ int fluid_sim_cellular_stability_test13(){ fluid_solve_bounds(chunkCount,queue,env); //check sum beforehand - float afterSum = chunk_queue_sum(queue); + float afterSum = chunk_queue_sum_density(queue); //diff the sums to see if we've changed value a lot float delta = fabs(originalSum - afterSum); @@ -1377,7 +1377,7 @@ int fluid_sim_cellular_stability_test14(){ } //check sum beforehand - float originalSum = chunk_queue_sum(queue); + float originalSum = chunk_queue_sum_density(queue); float slice[16 * 3][16 * 3]; int compareX = 2; @@ -1387,7 +1387,7 @@ int fluid_sim_cellular_stability_test14(){ int frameCount = 1000; int frameCounter; for(frameCounter = 0; frameCounter < frameCount; frameCounter++){ - float currentSum = chunk_queue_sum(queue); + float currentSum = chunk_queue_sum_density(queue); float delta = fabs(originalSum - currentSum); if(delta > FLUID_CELLULAR_TOLLERABLE_LOSS_THRESHOLD1){ fluid_sim_cellular_test_print_slice_big_vis(queue,1); @@ -1410,7 +1410,7 @@ int fluid_sim_cellular_stability_test14(){ fluid_solve_bounds(chunkCount,queue,env); //check sum beforehand - float afterSum = chunk_queue_sum(queue); + float afterSum = chunk_queue_sum_density(queue); //diff the sums to see if we've changed value a lot float delta = fabs(originalSum - afterSum); @@ -1461,7 +1461,7 @@ int fluid_sim_cellular_stability_test15(){ } //check sum beforehand - float originalSum = chunk_queue_sum(queue); + float originalSum = chunk_queue_sum_density(queue); float slice[16 * 3][16 * 3]; int compareX = 2; @@ -1471,7 +1471,7 @@ int fluid_sim_cellular_stability_test15(){ int frameCount = 100; int frameCounter; for(frameCounter = 0; frameCounter < frameCount; frameCounter++){ - float currentSum = chunk_queue_sum(queue); + float currentSum = chunk_queue_sum_density(queue); float delta = fabs(originalSum - currentSum); if(delta > FLUID_CELLULAR_TOLLERABLE_LOSS_THRESHOLD1){ printf("SLICE 1\n\n"); @@ -1498,7 +1498,7 @@ int fluid_sim_cellular_stability_test15(){ fluid_solve_bounds(chunkCount,queue,env); //check sum beforehand - float afterSum = chunk_queue_sum(queue); + float afterSum = chunk_queue_sum_density(queue); //diff the sums to see if we've changed value a lot float delta = fabs(originalSum - afterSum); @@ -1548,7 +1548,7 @@ int fluid_sim_cellular_stability_test16(){ } //check sum beforehand - float originalSum = chunk_queue_sum(queue); + float originalSum = chunk_queue_sum_density(queue); float slice[16 * 3][16 * 3]; int compareX = 2; @@ -1558,7 +1558,7 @@ int fluid_sim_cellular_stability_test16(){ int frameCount = 1000; int frameCounter; for(frameCounter = 0; frameCounter < frameCount; frameCounter++){ - float currentSum = chunk_queue_sum(queue); + float currentSum = chunk_queue_sum_density(queue); float delta = fabs(originalSum - currentSum); if(delta > FLUID_CELLULAR_TOLLERABLE_LOSS_THRESHOLD1){ fluid_sim_cellular_test_print_slice_big_vis(queue,1); @@ -1581,7 +1581,7 @@ int fluid_sim_cellular_stability_test16(){ fluid_solve_bounds(chunkCount,queue,env); //check sum beforehand - float afterSum = chunk_queue_sum(queue); + float afterSum = chunk_queue_sum_density(queue); //diff the sums to see if we've changed value a lot float delta = fabs(originalSum - afterSum); diff --git a/src/test/c/fluid/sim/grid2/density_diffuse_tests.c b/src/test/c/fluid/sim/grid2/density_diffuse_tests.c index b58bb358..fa881f2c 100644 --- a/src/test/c/fluid/sim/grid2/density_diffuse_tests.c +++ b/src/test/c/fluid/sim/grid2/density_diffuse_tests.c @@ -87,7 +87,66 @@ int fluid_sim_grid2_density_diffuse_test1(){ } //sum the result - float afterSum = chunk_queue_sum(queue); + float afterSum = chunk_queue_sum_density(queue); + + rVal += assertEqualsFloat(afterSum,MAX_FLUID_VALUE,"Density diffuse step changed density sum! %f %f \n"); + + return rVal; +} + + +/** + * Testing density diffusion + */ +int fluid_sim_grid2_density_diffuse_test2(){ + int rVal = 0; + printf("fluid_sim_grid2_density_diffuse_test2\n"); + Environment * env = fluid_environment_create(); + int chunkCount = 27; + + Chunk ** queue = NULL; + for(int i = 0; i < chunkCount; i++){ + arrput(queue,chunk_create( + fluid_sim_grid2_density_diffuse_tests_kernelx[i], + fluid_sim_grid2_density_diffuse_tests_kernely[i], + fluid_sim_grid2_density_diffuse_tests_kernelz[i] + )); + chunk_fill(queue[i],0); + } + //link neighbors + chunk_link_neighbors(queue); + fluid_solve_bounds(1,queue,env); + + + + + Chunk * currentChunk = queue[0]; + currentChunk->d[CENTER_LOC][IX(2,2,2)] = MAX_FLUID_VALUE; + + int frameCount = 100; + for(int frame = 0; frame < frameCount; frame++){ + float * tmpArr; + for(int j = 0; j < 27; j++){ + tmpArr = currentChunk->d[j]; + currentChunk->d[j] = currentChunk->d0[j]; + currentChunk->d0[j] = tmpArr; + } + //diffuse density + for(int l = 0; l < FLUID_GRID2_LINEARSOLVERTIMES; l++){ + fluid_grid2_solveDiffuseDensity(currentChunk->d,currentChunk->d0,FLUID_GRID2_DIFFUSION_CONSTANT,FLUID_GRID2_SIM_STEP); + fluid_grid2_setBoundsToNeighborsRaw(currentChunk->chunkMask,0,currentChunk->d); + } + //swap all density arrays + //swap vector fields + for(int j = 0; j < 27; j++){ + tmpArr = currentChunk->d[j]; + currentChunk->d[j] = currentChunk->d0[j]; + currentChunk->d0[j] = tmpArr; + } + } + + //sum the result + float afterSum = chunk_queue_sum_density(queue); rVal += assertEqualsFloat(afterSum,MAX_FLUID_VALUE,"Density diffuse step changed density sum! %f %f \n"); @@ -103,6 +162,7 @@ int fluid_sim_grid2_density_diffuse_tests(int argc, char **argv){ int rVal = 0; rVal += fluid_sim_grid2_density_diffuse_test1(); + rVal += fluid_sim_grid2_density_diffuse_test2(); return rVal; } \ No newline at end of file diff --git a/src/test/c/fluid/sim/grid2/velocity_diffuse_tests.c b/src/test/c/fluid/sim/grid2/velocity_diffuse_tests.c new file mode 100644 index 00000000..7a26603f --- /dev/null +++ b/src/test/c/fluid/sim/grid2/velocity_diffuse_tests.c @@ -0,0 +1,155 @@ + +#include "stb/stb_ds.h" + +#include "fluid/queue/boundsolver.h" +#include "fluid/queue/chunkmask.h" +#include "fluid/queue/chunk.h" +#include "fluid/env/environment.h" +#include "fluid/env/utilities.h" +#include "fluid/sim/grid2/solver_consts.h" +#include "fluid/sim/grid2/utilities.h" +#include "fluid/sim/grid2/velocity.h" +#include "../../../util/chunk_test_utils.h" +#include "../../../util/test.h" + + +/** + * Placement value for populating velocity arrays + */ +#define FLUID_GRID2_VELOCITY_DIFFUSE_TESTS_PLACEMENT_VAL 1.0f + + + + + +/** + * Testing velocity diffusion + */ +int fluid_sim_grid2_velocity_diffuse_test1(){ + int rVal = 0; + printf("fluid_sim_grid2_velocity_diffuse_test1\n"); + Environment * env = fluid_environment_create(); + Chunk ** queue = NULL; + queue = createChunkGrid(env,3,3,3); + + + + + Chunk * currentChunk = queue[0]; + currentChunk->u[CENTER_LOC][IX(2,2,2)] = FLUID_GRID2_VELOCITY_DIFFUSE_TESTS_PLACEMENT_VAL; + currentChunk->v[CENTER_LOC][IX(3,2,2)] = FLUID_GRID2_VELOCITY_DIFFUSE_TESTS_PLACEMENT_VAL; + currentChunk->w[CENTER_LOC][IX(2,2,3)] = FLUID_GRID2_VELOCITY_DIFFUSE_TESTS_PLACEMENT_VAL; + + float beforeSumX = chunk_queue_sum_velocity(queue,FLUID_GRID2_BOUND_DIR_U); + float beforeSumY = chunk_queue_sum_velocity(queue,FLUID_GRID2_BOUND_DIR_V); + float beforeSumZ = chunk_queue_sum_velocity(queue,FLUID_GRID2_BOUND_DIR_W); + rVal += assertEqualsFloat(beforeSumX,FLUID_GRID2_VELOCITY_DIFFUSE_TESTS_PLACEMENT_VAL,"x-velocity diffuse step changed velocity sum! %f %f \n"); + rVal += assertEqualsFloat(beforeSumY,FLUID_GRID2_VELOCITY_DIFFUSE_TESTS_PLACEMENT_VAL,"y-velocity diffuse step changed velocity sum! %f %f \n"); + rVal += assertEqualsFloat(beforeSumZ,FLUID_GRID2_VELOCITY_DIFFUSE_TESTS_PLACEMENT_VAL,"z-velocity diffuse step changed velocity sum! %f %f \n"); + + fluid_grid2_flip_arrays(currentChunk->u,currentChunk->u0); + fluid_grid2_flip_arrays(currentChunk->v,currentChunk->v0); + fluid_grid2_flip_arrays(currentChunk->w,currentChunk->w0); + //diffuse density + //solve vector diffusion + for(int l = 0; l < FLUID_GRID2_VECTOR_DIFFUSE_TIMES; l++){ + //solve vector diffusion + fluid_grid2_solveVectorDiffuse(currentChunk->u,currentChunk->v,currentChunk->w,currentChunk->u0,currentChunk->v0,currentChunk->w0,FLUID_GRID2_SIM_STEP); + //update array for vectors + fluid_grid2_setBoundsToNeighborsRaw(currentChunk->chunkMask,FLUID_GRID2_BOUND_DIR_U,currentChunk->u); + fluid_grid2_setBoundsToNeighborsRaw(currentChunk->chunkMask,FLUID_GRID2_BOUND_DIR_V,currentChunk->v); + fluid_grid2_setBoundsToNeighborsRaw(currentChunk->chunkMask,FLUID_GRID2_BOUND_DIR_W,currentChunk->w); + } + //swap all density arrays + //swap vector fields + fluid_grid2_flip_arrays(currentChunk->u,currentChunk->u0); + fluid_grid2_flip_arrays(currentChunk->v,currentChunk->v0); + fluid_grid2_flip_arrays(currentChunk->w,currentChunk->w0); + + //sum the result + float afterSumX = chunk_queue_sum_velocity(queue,FLUID_GRID2_BOUND_DIR_U); + float afterSumY = chunk_queue_sum_velocity(queue,FLUID_GRID2_BOUND_DIR_V); + float afterSumZ = chunk_queue_sum_velocity(queue,FLUID_GRID2_BOUND_DIR_W); + + //actually check + rVal += assertEqualsFloat(beforeSumX,afterSumX,"x-velocity diffuse step changed velocity sum! %f %f \n"); + rVal += assertEqualsFloat(beforeSumY,afterSumY,"y-velocity diffuse step changed velocity sum! %f %f \n"); + rVal += assertEqualsFloat(beforeSumZ,afterSumZ,"z-velocity diffuse step changed velocity sum! %f %f \n"); + + return rVal; +} + + +/** + * Testing velocity diffusion + */ +int fluid_sim_grid2_velocity_diffuse_test2(){ + int rVal = 0; + printf("fluid_sim_grid2_velocity_diffuse_test2\n"); + Environment * env = fluid_environment_create(); + Chunk ** queue = NULL; + queue = createChunkGrid(env,3,3,3); + + + + + Chunk * currentChunk = queue[0]; + currentChunk->u[CENTER_LOC][IX(2,2,2)] = FLUID_GRID2_VELOCITY_DIFFUSE_TESTS_PLACEMENT_VAL; + currentChunk->v[CENTER_LOC][IX(3,2,2)] = FLUID_GRID2_VELOCITY_DIFFUSE_TESTS_PLACEMENT_VAL; + currentChunk->w[CENTER_LOC][IX(2,2,3)] = FLUID_GRID2_VELOCITY_DIFFUSE_TESTS_PLACEMENT_VAL; + + float beforeSumX = chunk_queue_sum_velocity(queue,FLUID_GRID2_BOUND_DIR_U); + float beforeSumY = chunk_queue_sum_velocity(queue,FLUID_GRID2_BOUND_DIR_V); + float beforeSumZ = chunk_queue_sum_velocity(queue,FLUID_GRID2_BOUND_DIR_W); + rVal += assertEqualsFloat(beforeSumX,FLUID_GRID2_VELOCITY_DIFFUSE_TESTS_PLACEMENT_VAL,"x-velocity diffuse step changed velocity sum! %f %f \n"); + rVal += assertEqualsFloat(beforeSumY,FLUID_GRID2_VELOCITY_DIFFUSE_TESTS_PLACEMENT_VAL,"y-velocity diffuse step changed velocity sum! %f %f \n"); + rVal += assertEqualsFloat(beforeSumZ,FLUID_GRID2_VELOCITY_DIFFUSE_TESTS_PLACEMENT_VAL,"z-velocity diffuse step changed velocity sum! %f %f \n"); + + int frameCount = 50; + for(int frame = 0; frame < frameCount; frame++){ + fluid_grid2_flip_arrays(currentChunk->u,currentChunk->u0); + fluid_grid2_flip_arrays(currentChunk->v,currentChunk->v0); + fluid_grid2_flip_arrays(currentChunk->w,currentChunk->w0); + //diffuse density + //solve vector diffusion + for(int l = 0; l < FLUID_GRID2_VECTOR_DIFFUSE_TIMES; l++){ + //solve vector diffusion + fluid_grid2_solveVectorDiffuse(currentChunk->u,currentChunk->v,currentChunk->w,currentChunk->u0,currentChunk->v0,currentChunk->w0,FLUID_GRID2_SIM_STEP); + //update array for vectors + fluid_grid2_setBoundsToNeighborsRaw(currentChunk->chunkMask,FLUID_GRID2_BOUND_DIR_U,currentChunk->u); + fluid_grid2_setBoundsToNeighborsRaw(currentChunk->chunkMask,FLUID_GRID2_BOUND_DIR_V,currentChunk->v); + fluid_grid2_setBoundsToNeighborsRaw(currentChunk->chunkMask,FLUID_GRID2_BOUND_DIR_W,currentChunk->w); + } + //swap all density arrays + //swap vector fields + fluid_grid2_flip_arrays(currentChunk->u,currentChunk->u0); + fluid_grid2_flip_arrays(currentChunk->v,currentChunk->v0); + fluid_grid2_flip_arrays(currentChunk->w,currentChunk->w0); + } + + //sum the result + float afterSumX = chunk_queue_sum_velocity(queue,FLUID_GRID2_BOUND_DIR_U); + float afterSumY = chunk_queue_sum_velocity(queue,FLUID_GRID2_BOUND_DIR_V); + float afterSumZ = chunk_queue_sum_velocity(queue,FLUID_GRID2_BOUND_DIR_W); + + //actually check + rVal += assertEqualsFloat(beforeSumX,afterSumX,"x-velocity diffuse step changed velocity sum! %f %f \n"); + rVal += assertEqualsFloat(beforeSumY,afterSumY,"y-velocity diffuse step changed velocity sum! %f %f \n"); + rVal += assertEqualsFloat(beforeSumZ,afterSumZ,"z-velocity diffuse step changed velocity sum! %f %f \n"); + + return rVal; +} + + + + +/** + * Testing velocity diffusion + */ +int fluid_sim_grid2_velocity_diffuse_tests(int argc, char **argv){ + int rVal = 0; + + rVal += fluid_sim_grid2_velocity_diffuse_test1(); + + return rVal; +} \ No newline at end of file diff --git a/src/test/c/util/chunk_test_utils.c b/src/test/c/util/chunk_test_utils.c index 688b0ba1..fb08c2e3 100644 --- a/src/test/c/util/chunk_test_utils.c +++ b/src/test/c/util/chunk_test_utils.c @@ -1,9 +1,32 @@ #include #include "stb/stb_ds.h" +#include "fluid/queue/boundsolver.h" #include "fluid/queue/chunk.h" #include "fluid/queue/chunkmask.h" +#include "fluid/env/environment.h" #include "fluid/env/utilities.h" +#include "fluid/sim/grid2/velocity.h" + + + +int chunk_test_utils_kernelx[27] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, + 1, 1, 1, 1, 1, 1, 1, 1, 1, + 2, 2, 2, 2, 2, 2, 2, 2, 2, +}; + +int chunk_test_utils_kernely[27] = { + 0, 0, 0, 1, 1, 1, 2, 2, 2, + 0, 0, 0, 1, 1, 1, 2, 2, 2, + 0, 0, 0, 1, 1, 1, 2, 2, 2, +}; + +int chunk_test_utils_kernelz[27] = { + 0, 1, 2, 0, 1, 2, 0, 1, 2, + 0, 1, 2, 0, 1, 2, 0, 1, 2, + 0, 1, 2, 0, 1, 2, 0, 1, 2, +}; /** * Creates a chunk at a world position @@ -230,11 +253,10 @@ void chunk_link_neighbors(Chunk ** chunks){ } /** - * Sums all chunks in a queue + * Sums density all chunks in a queue */ -float chunk_queue_sum(Chunk ** chunks){ +float chunk_queue_sum_density(Chunk ** chunks){ float sum = 0; - // printf("\nsum\n"); int chunkCount = arrlen(chunks); for(int i = 0; i < chunkCount; i++){ Chunk * current = chunks[i]; @@ -242,14 +264,35 @@ float chunk_queue_sum(Chunk ** chunks){ for(int y = 1; y < DIM - 1; y++){ for(int z = 1; z < DIM - 1; z++){ sum = sum + current->d[CENTER_LOC][IX(x,y,z)]; - // if(current->d[CENTER_LOC][IX(x,y,z)] > 0){ - // printf("%d %d %d\n",x,y,z); - // } } } } } - // printf("\n\n"); + return sum; +} + +/** + * Sums velocity in all chunks in a queue + */ +float chunk_queue_sum_velocity(Chunk ** chunks, int axis){ + float sum = 0; + int chunkCount = arrlen(chunks); + for(int i = 0; i < chunkCount; i++){ + Chunk * current = chunks[i]; + for(int x = 1; x < DIM - 1; x++){ + for(int y = 1; y < DIM - 1; y++){ + for(int z = 1; z < DIM - 1; z++){ + if(axis == FLUID_GRID2_BOUND_DIR_U){ + sum = sum + current->u[CENTER_LOC][IX(x,y,z)]; + } else if(axis == FLUID_GRID2_BOUND_DIR_V){ + sum = sum + current->v[CENTER_LOC][IX(x,y,z)]; + } else if(axis == FLUID_GRID2_BOUND_DIR_W){ + sum = sum + current->w[CENTER_LOC][IX(x,y,z)]; + } + } + } + } + } return sum; } @@ -258,4 +301,35 @@ float chunk_queue_sum(Chunk ** chunks){ */ int util_chunk_test_utils(){ return 0; -} \ No newline at end of file +} + + +/** + * Creates a grid of chunks with the specified dimensions + * @param env The simulation environment + * @param width The width of the grid in number of chunks + * @param height The height of the grid in number of chunks + * @param length The length of the grid in number of chunks + * @return The list of chunks + */ +Chunk ** createChunkGrid(Environment * env, int width, int height, int length){ + Chunk ** rVal = NULL; + for(int x = 0; x < width; x++){ + for(int y = 0; y < height; y++){ + for(int z = 0; z < length; z++){ + Chunk * chunk = chunk_create(x,y,z); + arrput(rVal,chunk); + chunk_fill(chunk,0); + } + } + } + int numChunks = arrlen(rVal); + //link neighbors + chunk_link_neighbors(rVal); + //set bounds + fluid_solve_bounds(numChunks,rVal,env); + return rVal; +} + + + diff --git a/src/test/c/util/chunk_test_utils.h b/src/test/c/util/chunk_test_utils.h index f9bbc8c0..19eb0a56 100644 --- a/src/test/c/util/chunk_test_utils.h +++ b/src/test/c/util/chunk_test_utils.h @@ -2,6 +2,7 @@ #ifndef CHUNK_TEST_UTILS_H #define CHUNK_TEST_UTILS_H +#include "fluid/env/environment.h" #include "fluid/queue/chunk.h" @@ -96,8 +97,23 @@ void chunk_free_queue(Chunk ** chunks); void chunk_link_neighbors(Chunk ** chunks); /** - * Sums all chunks in a queue + * Sums density all chunks in a queue */ -float chunk_queue_sum(Chunk ** chunks); +float chunk_queue_sum_density(Chunk ** chunks); + +/** + * Sums velocity in all chunks in a queue + */ +float chunk_queue_sum_velocity(Chunk ** chunks, int axis); + +/** + * Creates a grid of chunks with the specified dimensions + * @param env The simulation environment + * @param width The width of the grid in number of chunks + * @param height The height of the grid in number of chunks + * @param length The length of the grid in number of chunks + * @return The list of chunks + */ +Chunk ** createChunkGrid(Environment * env, int width, int height, int length); #endif \ No newline at end of file