work on cellular stability
Some checks failed
studiorailgun/Renderer/pipeline/head There was a failure building this commit

This commit is contained in:
austin 2024-12-07 18:59:49 -05:00
parent 1c73c2d5ea
commit c8cf14fed1
9 changed files with 219 additions and 95 deletions

View File

@ -6,6 +6,10 @@
#include "fluid/env/environment.h"
/**
* Lateral diffusion rate of the fluids
*/
#define FLUID_CELLULAR_DIFFUSE_RATE2 0.1f
/**
* Simulates the cellular chunk queue

View File

@ -62,6 +62,11 @@ JNIEXPORT void JNICALL Java_electrosphere_server_fluid_simulator_FluidAccelerate
fluid_dispatch(numReadIn,chunkViewC,environment);
fluid_simulate(environment);
updateMetadata(env,numReadIn,chunkViewC,environment);
//solve bounds afterwards to properly push data back into real arrays
//ie, if data is pushed out of bounds from one chunk to another, must
//then copy it into the in-bounds chunk
fluid_solve_bounds(numReadIn,chunkViewC,environment);
}
/**

View File

@ -8,7 +8,7 @@
#define FLUID_CELLULAR_DIFFUSE_RATE 0.001
#define FLUID_CELLULAR_DIFFUSE_RATE2 0.1
#define FLUID_CELLULAR_KERNEL_SIZE 4
#define FLUID_CELLULAR_KERNEL_PERMUTATIONS 4
@ -63,61 +63,9 @@ LIBRARY_API void fluid_cellular_simulate(Environment * environment){
// int permutation = randutils_map(randutils_rand2(environment->state.frame,y + 1),0,FLUID_CELLULAR_KERNEL_PERMUTATIONS - 1);
for(int x = 0; x < DIM; x++){
for(int z = 0; z < DIM; z++){
//diffuse density
// d[IX(x,y,z)] = d[IX(x,y,z)];
// if(x > 1){
// d[IX(x,y,z)] += (d[IX(x-1,y,z)] - d[IX(x,y,z)]) * FLUID_CELLULAR_DIFFUSE_RATE;
// }
// if(x < DIM-2){
// d[IX(x,y,z)] += (d[IX(x+1,y,z)] - d[IX(x,y,z)]) * FLUID_CELLULAR_DIFFUSE_RATE;
// }
// if(y > 1){
// d[IX(x,y,z)] += (d[IX(x,y-1,z)] - d[IX(x,y,z)]) * FLUID_CELLULAR_DIFFUSE_RATE;
// }
// if(y < DIM-2){
// d[IX(x,y,z)] += (d[IX(x,y+1,z)] - d[IX(x,y,z)]) * FLUID_CELLULAR_DIFFUSE_RATE;
// }
// if(z > 1){
// d[IX(x,y,z)] += (d[IX(x,y,z-1)] - d[IX(x,y,z)]) * FLUID_CELLULAR_DIFFUSE_RATE;
// }
// if(z < DIM-2){
// d[IX(x,y,z)] += (d[IX(x,y,z+1)] - d[IX(x,y,z)]) * FLUID_CELLULAR_DIFFUSE_RATE;
// }
if(bounds[IX(x,y,z)] > BOUND_CUTOFF_VALUE){
continue;
}
// for(int j = 0; j < FLUID_CELLULAR_KERNEL_SIZE; j++){
// density = d[IX(x,y,z)];
// int nX = x + fluid_cellular_kernel_x[j];
// int nZ = z + fluid_cellular_kernel_z[j];
// float nDensity = d[IX(nX,y,nZ)];
// if(nDensity > MIN_FLUID_VALUE){
// float lateralDiff = nDensity - density;
// if(lateralDiff > 0){
// float maxIntake = MAX_FLUID_VALUE - density;
// if(maxIntake > 0){
// float transferLateral;
// transferLateral = lateralDiff;
// if(nDensity < transferLateral){
// transferLateral = nDensity;
// }
// if(maxIntake < transferLateral){
// transferLateral = maxIntake;
// }
// if(transferLateral > FLUID_CELLULAR_DIFFUSE_RATE2){
// transferLateral = FLUID_CELLULAR_DIFFUSE_RATE2;
// }
// d[IX(nX,y,nZ)] -= transferLateral;
// d[IX(x,y,z)] += transferLateral;
// // if(d[IX(nX,y,nZ)] < MIN_FLUID_VALUE){
// // d[IX(x,y,z)] += d[IX(nX,y,nZ)];
// // d[IX(nX,y,nZ)] = MIN_FLUID_VALUE;
// // }
// }
// }
// }
// }
if(d[IX(x,y,z)] <= MIN_FLUID_VALUE){
continue;
} else {
@ -125,14 +73,31 @@ LIBRARY_API void fluid_cellular_simulate(Environment * environment){
if(y > 0){
float nBound = bounds[IX(x,y-1,z)];
if(nBound <= BOUND_CUTOFF_VALUE){
if(d[IX(x,y-1,z)] <= MIN_FLUID_VALUE){
d[IX(x,y-1,z)] = d[IX(x,y,z)];
d[IX(x,y,z)] = MIN_FLUID_VALUE;
if(d[IX(x,y-1,z)] <= MAX_FLUID_VALUE - FLUID_CELLULAR_DIFFUSE_RATE2){
// d[IX(x,y-1,z)] = d[IX(x,y,z)];
// d[IX(x,y,z)] = MIN_FLUID_VALUE;
float transfer = FLUID_CELLULAR_DIFFUSE_RATE2;
if(d[IX(x,y,z)] < FLUID_CELLULAR_DIFFUSE_RATE2){
transfer = d[IX(x,y,z)];
}
// printf("[%d %d %d] <%d,%d,%d> --> <%d,%d,%d> %f \n",worldX,worldY,worldZ,x,y,z,x,y-1,z,transfer);
// printf("%f %f \n",d[IX(x,y,z)],d[IX(x,y,z)]);
d[IX(x,y-1,z)] = d[IX(x,y-1,z)] + transfer;
d[IX(x,y,z)] = d[IX(x,y,z)] - transfer;
// printf("%f %f \n",d[IX(x,y,z)],d[IX(x,y-1,z)]);
// printf("\n");
continue;
}
}
}
//transfer laterally
// [0 0 0]<1,1,17> -> 1,17
// [0 0 1]<1,1, 1> -> 1,17
// [0 0 0]<1,1,15> -> 1,15
// [0 0 0]<1,1,16> -> 1,16
// [0 0 1]<1,1, 0> -> 1,16
//calculate permutation based on the location of the cell
if(x == 0){
permuteX = DIM-2 + (CHUNK_SPACING * (worldX - 1));
@ -152,19 +117,30 @@ LIBRARY_API void fluid_cellular_simulate(Environment * environment){
} else {
permuteZ = z + (CHUNK_SPACING * worldZ);
}
int permutation = (permuteZ % (FLUID_CELLULAR_KERNEL_PERMUTATIONS / 2)) + ((permuteX % (FLUID_CELLULAR_KERNEL_PERMUTATIONS / 2)) * (FLUID_CELLULAR_KERNEL_PERMUTATIONS / 2));
int permutation = (permuteZ % (FLUID_CELLULAR_KERNEL_PERMUTATIONS / 2)) + (((permuteX % (FLUID_CELLULAR_KERNEL_PERMUTATIONS / 2))) * (FLUID_CELLULAR_KERNEL_PERMUTATIONS / 2));
// for(int j = 0; j < FLUID_CELLULAR_KERNEL_SIZE; j++){
int nX = x + fluid_cellular_kernel_x[shift][permutation];
int nZ = z + fluid_cellular_kernel_z[shift][permutation];
// printf("[%d %d %d] <%d,%d,%d>\n",worldX,worldY,worldZ,x,y,z);
// printf("%d %d \n",permuteX,permuteZ);
if(nX < 0 || nX >= DIM || nZ < 0 || nZ >= DIM){
continue;
}
if(bounds[IX(nX,y,nZ)] <= BOUND_CUTOFF_VALUE){
if(d[IX(nX,y,nZ)] <= MIN_FLUID_VALUE){
// printf("%d %d %d --> %d %d %d \n",x,y,z,nX,y,nZ);
d[IX(nX,y,nZ)] = d[IX(x,y,z)];
d[IX(x,y,z)] = MIN_FLUID_VALUE;
break;
if(d[IX(nX,y,nZ)] <= MAX_FLUID_VALUE - FLUID_CELLULAR_DIFFUSE_RATE2 && d[IX(nX,y,nZ)] < d[IX(x,y,z)]){
float transfer = FLUID_CELLULAR_DIFFUSE_RATE2;
if(d[IX(x,y,z)] < FLUID_CELLULAR_DIFFUSE_RATE2){
transfer = d[IX(x,y,z)];
}
// printf("[%d %d %d] <%d,%d,%d> --> <%d,%d,%d> \n",worldX,worldY,worldZ,x,y,z,nX,y,nZ);
// printf("%f %d %d \n",transfer,permuteX,permuteZ);
// printf("%f %f \n",d[IX(x,y,z)],d[IX(nX,y,nZ)]);
// d[IX(nX,y,nZ)] = d[IX(x,y,z)];
// d[IX(x,y,z)] = MIN_FLUID_VALUE;
d[IX(nX,y,nZ)] = d[IX(nX,y,nZ)] + transfer;
d[IX(x,y,z)] = d[IX(x,y,z)] - transfer;
// printf("%f %f \n",d[IX(x,y,z)],d[IX(nX,y,nZ)]);
// printf("\n");
}
}
// }

View File

@ -53,6 +53,7 @@ foreach (TEST_FILE ${TEST_SOURCES})
if(TEST_PATH)
add_test(NAME ${TEST_NAME} COMMAND test_runner ${TEST_PATH}/${TEST_NAME})
else()
message(FATAL_ERROR ${TEST_NAME})
add_test(NAME ${TEST_NAME} COMMAND test_runner ${TEST_NAME})
endif()
endforeach ()

View File

@ -1,11 +0,0 @@
#include <stdio.h>
//library includes
//include stb ds
#define STB_DS_IMPLEMENTATION
#include "stb/stb_ds.h"
int StormEngineTests(int argc, char **argv){
printf("it lives\n");
return 0;
}

View File

@ -1,5 +1,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "stb/stb_ds.h"
@ -11,12 +12,14 @@
#include "fluid/queue/boundsolver.h"
#include "fluid/dispatch/dispatcher.h"
#include "fluid/sim/simulator.h"
#include "fluid/sim/cellular/cellular.h"
#include "../../../util/test.h"
#include "../../../util/chunk_test_utils.h"
#define CELLULAR_TEST_PLACE_VAL 0.1f
#define CELLULAR_TEST_PLACE_VAL (FLUID_CELLULAR_DIFFUSE_RATE2 + 0.13456f)
#define TOLLERABLE_LOSS_THRESHOLD 0.1f
int fluid_sim_cellular_cellular_tests_kernelx[27] = {
0, 0, 0, 0, 0, 0, 0, 0, 0,
@ -43,6 +46,7 @@ int fluid_sim_cellular_bounds_test1(){
printf("fluid_sim_cellular_bounds_test1\n");
Environment * env = fluid_environment_create();
env->state.frame += 1;
int chunkCount = 27;
@ -80,14 +84,18 @@ int fluid_sim_cellular_bounds_test1(){
fluid_dispatch(chunkCount,queue,env);
fluid_simulate(env);
//solve bounds afterwards to properly push data back into real arrays
//ie, if data is pushed out of bounds from one chunk to another, must
//then copy it into the in-bounds chunk
fluid_solve_bounds(chunkCount,queue,env);
//assert that the density moved
{
float borderVal = queue[0]->d[CENTER_LOC][IX(1,1,DIM-2)];
float orderBorderVal = queue[0]->d[CENTER_LOC][IX(1,1,DIM-3)];
float orderBorderVal = queue[0]->d[CENTER_LOC][IX(1,1,DIM-1)];
float transferedVal = queue[1]->d[CENTER_LOC][IX(1,1,0)];
rVal += assertEqualsFloat(borderVal,MIN_FLUID_VALUE,"Border value has not changed! -- %f %f \n");
rVal += assertEqualsFloat(orderBorderVal,CELLULAR_TEST_PLACE_VAL,"Border value has not moved! -- %f %f \n");
rVal += assertEqualsFloat(transferedVal,CELLULAR_TEST_PLACE_VAL,"Value was overwritten on border! -- %f %f \n");
rVal += assertEqualsFloat(borderVal,CELLULAR_TEST_PLACE_VAL - FLUID_CELLULAR_DIFFUSE_RATE2,"Border value has not changed! -- %f %f \n");
rVal += assertEqualsFloat(orderBorderVal,FLUID_CELLULAR_DIFFUSE_RATE2,"Border value has not moved! -- %f %f \n");
}
printf("\n");
@ -99,7 +107,7 @@ int fluid_sim_cellular_bounds_test2(){
printf("fluid_sim_cellular_bounds_test2\n");
Environment * env = fluid_environment_create();
env->state.frame += 10;
env->state.frame += 1;
int chunkCount = 27;
@ -136,18 +144,19 @@ int fluid_sim_cellular_bounds_test2(){
//dispatch and simulate
fluid_dispatch(chunkCount,queue,env);
fluid_simulate(env);
//solve bounds afterwards to properly push data back into real arrays
//ie, if data is pushed out of bounds from one chunk to another, must
//then copy it into the in-bounds chunk
fluid_solve_bounds(chunkCount,queue,env);
fluid_dispatch(chunkCount,queue,env);
fluid_simulate(env);
//assert that the density moved
{
float borderOldVal = queue[0]->d[CENTER_LOC][IX(1,1,DIM-1)];
float borderNewVal = queue[0]->d[CENTER_LOC][IX(1,1,DIM-2)];
float transferedVal = queue[1]->d[CENTER_LOC][IX(1,1,0)];
rVal += assertEqualsFloat(borderOldVal,MIN_FLUID_VALUE,"Border old val has not changed! -- %f %f \n");
rVal += assertEqualsFloat(borderNewVal,CELLULAR_TEST_PLACE_VAL,"Border new val not occupied! -- %f %f \n");
rVal += assertEqualsFloat(transferedVal,CELLULAR_TEST_PLACE_VAL,"Value was overwritten on border! -- %f %f \n");
rVal += assertEqualsFloat(borderOldVal,CELLULAR_TEST_PLACE_VAL - FLUID_CELLULAR_DIFFUSE_RATE2,"Border old val has not changed! -- %f %f \n");
rVal += assertEqualsFloat(borderNewVal,FLUID_CELLULAR_DIFFUSE_RATE2,"Border new val not occupied! -- %f %f \n");
}
printf("\n");
@ -187,14 +196,31 @@ int fluid_sim_cellular_stability_test1(){
//dispatch and simulate
int frameCount = 50;
for(int i = 0; i < frameCount; i++){
int frameCount = 5000;
for(int frameCounter = 0; frameCounter < frameCount; frameCounter++){
float currentSum = chunk_queue_sum(queue);
if(currentSum != originalSum){
printf("Failed to equal sums! \n");
rVal += assertEqualsFloat(currentSum,originalSum,"Sums are not identical! %f %f \n");
break;
}
printf("frame: %d --- %f \n", frameCounter,currentSum);
fluid_solve_bounds(chunkCount,queue,env);
fluid_dispatch(chunkCount,queue,env);
fluid_simulate(env);
fluid_solve_bounds(chunkCount,queue,env);
printf("\n");
env->state.frame++;
}
//solve bounds afterwards to properly push data back into real arrays
//ie, if data is pushed out of bounds from one chunk to another, must
//then copy it into the in-bounds chunk
fluid_solve_bounds(chunkCount,queue,env);
// float transferedValue = queue[0]->d[CENTER_LOC][IX(1,1,16)];
// printf("transfered value: %f \n",transferedValue);
//check sum beforehand
float afterSum = chunk_queue_sum(queue);
@ -204,6 +230,8 @@ int fluid_sim_cellular_stability_test1(){
return rVal;
}
int fluid_sim_cellular_stability_test2(){
int rVal = 0;
printf("fluid_sim_cellular_stability_test2\n");
@ -229,21 +257,39 @@ int fluid_sim_cellular_stability_test2(){
chunk_fill(queue[i],0);
}
//fill the 13th chunk
chunk_fill(queue[13],CELLULAR_TEST_PLACE_VAL);
//set border of 0,0,0 to push a value into z
queue[13]->d[CENTER_LOC][IX(5,5,5)] = CELLULAR_TEST_PLACE_VAL;
//check sum beforehand
float originalSum = chunk_queue_sum(queue);
//dispatch and simulate
int frameCount = 50;
for(int i = 0; i < frameCount; i++){
int frameCount = 5000;
for(int frameCounter = 0; frameCounter < frameCount; frameCounter++){
float currentSum = chunk_queue_sum(queue);
if(currentSum != originalSum){
printf("Failed to equal sums! \n");
rVal += assertEqualsFloat(currentSum,originalSum,"Sums are not identical! %f %f \n");
break;
}
printf("frame: %d --- %f \n", frameCounter,currentSum);
fluid_solve_bounds(chunkCount,queue,env);
fluid_dispatch(chunkCount,queue,env);
fluid_simulate(env);
fluid_solve_bounds(chunkCount,queue,env);
printf("\n");
env->state.frame++;
}
//solve bounds afterwards to properly push data back into real arrays
//ie, if data is pushed out of bounds from one chunk to another, must
//then copy it into the in-bounds chunk
fluid_solve_bounds(chunkCount,queue,env);
// float transferedValue = queue[0]->d[CENTER_LOC][IX(1,1,16)];
// printf("transfered value: %f \n",transferedValue);
//check sum beforehand
float afterSum = chunk_queue_sum(queue);
@ -254,13 +300,86 @@ int fluid_sim_cellular_stability_test2(){
}
int fluid_sim_cellular_stability_test3(){
int rVal = 0;
printf("fluid_sim_cellular_stability_test3\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_cellular_cellular_tests_kernelx[i],
fluid_sim_cellular_cellular_tests_kernely[i],
fluid_sim_cellular_cellular_tests_kernelz[i]
));
}
//link neighbors
chunk_link_neighbors(queue);
//fill them with values
for(int i = 0; i < chunkCount; i++){
chunk_fill(queue[i],0);
}
//fill the 10th chunk
chunk_fill_real(queue[10]->d[CENTER_LOC],CELLULAR_TEST_PLACE_VAL);
//check sum beforehand
float originalSum = chunk_queue_sum(queue);
//dispatch and simulate
int frameCount = 50;
int frameCounter;
for(frameCounter = 0; frameCounter < frameCount; frameCounter++){
float currentSum = chunk_queue_sum(queue);
float delta = fabs(originalSum - currentSum);
if(delta > TOLLERABLE_LOSS_THRESHOLD){
printf("Failed to equal sums! \n");
rVal += assertEqualsFloat(currentSum,originalSum,"Sums are not identical! %f %f \n");
printf("desync by frame %d \n",frameCounter);
break;
}
// printf("frame: %d --- %f \n", frameCounter,currentSum);
fluid_solve_bounds(chunkCount,queue,env);
fluid_dispatch(chunkCount,queue,env);
fluid_simulate(env);
fluid_solve_bounds(chunkCount,queue,env);
// printf("\n");
env->state.frame++;
}
//solve bounds afterwards to properly push data back into real arrays
//ie, if data is pushed out of bounds from one chunk to another, must
//then copy it into the in-bounds chunk
fluid_solve_bounds(chunkCount,queue,env);
//check sum beforehand
float afterSum = chunk_queue_sum(queue);
//diff the sums to see if we've changed value a lot
float delta = fabs(originalSum - afterSum);
if(delta > TOLLERABLE_LOSS_THRESHOLD){
rVal += assertEqualsFloat(originalSum,afterSum,"cellular sim was unstable! %f %f \n");
}
printf("\n");
return rVal;
}
int fluid_sim_cellular_cellular_tests(int argc, char **argv){
int rVal = 0;
rVal += fluid_sim_cellular_bounds_test1();
rVal += fluid_sim_cellular_bounds_test2();
// rVal += fluid_sim_cellular_bounds_test1();
// rVal += fluid_sim_cellular_bounds_test2();
rVal += fluid_sim_cellular_stability_test1();
rVal += fluid_sim_cellular_stability_test2();
// rVal += fluid_sim_cellular_stability_test3();
return rVal;
}

View File

@ -95,6 +95,21 @@ void chunk_fill(Chunk * chunk, float val){
}
}
/**
* Fills a chunk with a value
* @param chunk The chunk to fill
* @param val The value to fill
*/
void chunk_fill_real(float * arr, float val){
for(int x = 1; x < DIM - 2; x++){
for(int y = 1; y < DIM - 2; y++){
for(int z = 1; z < DIM - 2; z++){
arr[IX(x,y,z)] = val;
}
}
}
}
/**
* Used in chunk_link_neighbors
@ -219,17 +234,22 @@ void chunk_link_neighbors(Chunk ** chunks){
*/
float chunk_queue_sum(Chunk ** chunks){
float sum = 0;
// printf("\nsum\n");
int chunkCount = arrlen(chunks);
for(int i = 0; i < chunkCount; i++){
Chunk * current = chunks[i];
for(int x = 1; x < DIM - 2; x++){
for(int y = 1; y < DIM - 2; y++){
for(int z = 1; z < DIM - 2; z++){
for(int x = 1; x < DIM - 1; x++){
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;
}

View File

@ -78,6 +78,13 @@ Chunk ** chunk_create_queue(int size);
*/
void chunk_fill(Chunk * chunk, float val);
/**
* Fills a chunk with a value
* @param chunk The chunk to fill
* @param val The value to fill
*/
void chunk_fill_real(float * arr, float val);
/**
* Frees a chunk queue
*/

View File

@ -1,6 +1,9 @@
#include <stdlib.h>
#include <stdio.h>
#define STB_DS_IMPLEMENTATION
#include "stb/stb_ds.h"
#include "test.h"
int assertEquals(int a, int b, char * msg){