fix density normaliaztion mechanism
Some checks failed
studiorailgun/Renderer/pipeline/head There was a failure building this commit
Some checks failed
studiorailgun/Renderer/pipeline/head There was a failure building this commit
This commit is contained in:
parent
72f9fcdcba
commit
94e4e10a78
2
src/main/c/includes/fluid/env/environment.h
vendored
2
src/main/c/includes/fluid/env/environment.h
vendored
@ -71,7 +71,7 @@ typedef struct {
|
||||
typedef struct {
|
||||
double existingDensity;
|
||||
double newDensity;
|
||||
float normalizationRatio;
|
||||
double normalizationRatio;
|
||||
int frame;
|
||||
} FluidSimState;
|
||||
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
#ifndef FLUID_GRID2_MAINFUNC
|
||||
#define FLUID_GRID2_MAINFUNC
|
||||
|
||||
|
||||
#include "public.h"
|
||||
#include "fluid/env/environment.h"
|
||||
|
||||
|
||||
@ -19,26 +19,13 @@
|
||||
* @param environment The environment data
|
||||
* @param timestep The timestep to simulate by
|
||||
*/
|
||||
void fluid_grid2_simulate(int numChunks, Chunk ** passedInChunks, Environment * environment, float timestep);
|
||||
LIBRARY_API void fluid_grid2_simulate(int numChunks, Chunk ** passedInChunks, Environment * environment, float timestep);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* The main simulation function
|
||||
*/
|
||||
void fluid_grid2_simulate(
|
||||
int numChunks,
|
||||
Chunk ** passedInChunks,
|
||||
Environment * environment,
|
||||
jfloat timestep
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -47,7 +34,7 @@ void fluid_grid2_simulate(
|
||||
/**
|
||||
* Allocates the arrays necessary for grid2 simulation
|
||||
*/
|
||||
void fluid_grid2_allocate_arrays();
|
||||
LIBRARY_API void fluid_grid2_allocate_arrays();
|
||||
|
||||
|
||||
|
||||
|
||||
@ -26,8 +26,6 @@ void fluid_grid2_addDensity(
|
||||
float * x = GET_ARR_RAW(d,CENTER_LOC);
|
||||
float * s = GET_ARR_RAW(d0,CENTER_LOC);
|
||||
for(i=0; i<size; i++){
|
||||
environment->state.newDensity = environment->state.newDensity + dt * s[i];
|
||||
environment->state.existingDensity = environment->state.existingDensity + x[i];
|
||||
x[i] += dt*s[i];
|
||||
if(x[i] < MIN_FLUID_VALUE){
|
||||
x[i] = MIN_FLUID_VALUE;
|
||||
@ -55,10 +53,6 @@ LIBRARY_API void fluid_grid2_solveDiffuseDensity(
|
||||
__m256 aScalar = _mm256_set1_ps(a);
|
||||
__m256 cScalar = _mm256_set1_ps(c);
|
||||
|
||||
// for(int l = 0; l < FLUID_GRID2_LINEARSOLVERTIMES; l++){
|
||||
// solver_multigrid_iterate(x,x0,a,c);
|
||||
// }
|
||||
|
||||
//transform u direction
|
||||
for(k=1; k<DIM-1; k++){
|
||||
for(j=1; j<DIM-1; j++){
|
||||
|
||||
@ -43,7 +43,7 @@ float * fluid_grid2_neighborArr_v0;
|
||||
float * fluid_grid2_neighborArr_w0;
|
||||
float * fluid_grid2_neighborArr_bounds;
|
||||
|
||||
void fluid_grid2_simulate(
|
||||
LIBRARY_API void fluid_grid2_simulate(
|
||||
int numChunks,
|
||||
Chunk ** passedInChunks,
|
||||
Environment * environment,
|
||||
@ -167,6 +167,10 @@ void fluid_grid2_simulate(
|
||||
environment->state.newDensity = 0;
|
||||
for(int i = 0; i < numChunks; i++){
|
||||
Chunk * currentChunk = chunks[i];
|
||||
|
||||
environment->state.existingDensity = environment->state.existingDensity + fluid_grid2_calculateSum(currentChunk->d);
|
||||
environment->state.newDensity = environment->state.newDensity + fluid_grid2_calculateSum(currentChunk->d0);
|
||||
|
||||
fluid_grid2_addDensity(environment,currentChunk->d,currentChunk->d0,timestep);
|
||||
|
||||
//swap all density arrays
|
||||
@ -198,9 +202,10 @@ void fluid_grid2_simulate(
|
||||
Chunk * currentChunk = chunks[i];
|
||||
transformedDensity = transformedDensity + fluid_grid2_calculateSum(currentChunk->d);
|
||||
}
|
||||
float normalizationRatio = 0;
|
||||
double normalizationRatio = 0;
|
||||
if(transformedDensity != 0){
|
||||
normalizationRatio = (environment->state.existingDensity + environment->state.newDensity) / transformedDensity;
|
||||
double expectedNewSum = environment->state.existingDensity + environment->state.newDensity;
|
||||
normalizationRatio = expectedNewSum / transformedDensity;
|
||||
environment->state.normalizationRatio = normalizationRatio;
|
||||
}
|
||||
for(int i = 0; i < numChunks; i++){
|
||||
@ -279,7 +284,7 @@ static inline void fluid_grid2_clearArr(float ** d){
|
||||
/**
|
||||
* Allocates the arrays necessary for grid2 simulation
|
||||
*/
|
||||
void fluid_grid2_allocate_arrays(){
|
||||
LIBRARY_API void fluid_grid2_allocate_arrays(){
|
||||
fluid_grid2_border_mask = (float *)calloc(1,DIM * DIM * DIM * sizeof(float));
|
||||
fluid_grid2_border_mask_inverted = (float *)calloc(1,DIM * DIM * DIM * sizeof(float));
|
||||
for(int x = 0; x < DIM; x++){
|
||||
|
||||
@ -83,8 +83,13 @@ double fluid_grid2_calculateSum(float ** d){
|
||||
int size=DIM*DIM*DIM;
|
||||
float * x = GET_ARR_RAW(d,CENTER_LOC);
|
||||
double rVal = 0;
|
||||
for(j=0; j<size; j++){
|
||||
rVal = rVal + x[j];
|
||||
for(int i = 1; i < DIM - 1; i++){
|
||||
for(int j = 1; j < DIM - 1; j++){
|
||||
for(int k = 1; k < DIM - 1; k++){
|
||||
rVal = rVal + x[IX(i,j,k)];
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return rVal;
|
||||
}
|
||||
|
||||
148
src/test/c/fluid/sim/grid2/full_sim_tests.c
Normal file
148
src/test/c/fluid/sim/grid2/full_sim_tests.c
Normal file
@ -0,0 +1,148 @@
|
||||
#include <math.h>
|
||||
|
||||
#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/density.h"
|
||||
#include "fluid/sim/grid2/grid2.h"
|
||||
#include "fluid/sim/grid2/solver_consts.h"
|
||||
#include "fluid/sim/grid2/utilities.h"
|
||||
#include "fluid/sim/grid2/velocity.h"
|
||||
#include "math/ode/multigrid.h"
|
||||
#include "../../../util/chunk_test_utils.h"
|
||||
#include "../../../util/test.h"
|
||||
|
||||
/**
|
||||
* Center of the advection cell
|
||||
*/
|
||||
#define FLUID_GRID2_PROJECTION_CELL_CENTER 24
|
||||
|
||||
/**
|
||||
* Error margin for tests
|
||||
*/
|
||||
#define FLUID_GRID2_PROJECTION_ERROR_MARGIN 0.00001f
|
||||
|
||||
/**
|
||||
* Testing full sim routine
|
||||
*/
|
||||
int fluid_sim_grid2_full_sim_test1(){
|
||||
printf("fluid_sim_grid2_full_sim_test1\n");
|
||||
int rVal = 0;
|
||||
Environment * env = fluid_environment_create();
|
||||
Chunk ** queue = NULL;
|
||||
queue = createChunkGrid(env,3,3,3);
|
||||
|
||||
|
||||
|
||||
//setup chunk values
|
||||
Chunk * currentChunk = queue[0];
|
||||
currentChunk->d[CENTER_LOC][IX(4,4,4)] = MAX_FLUID_VALUE;
|
||||
currentChunk->u[CENTER_LOC][IX(4,4,4)] = MAX_FLUID_VALUE;
|
||||
float beforeSum = chunk_queue_sum_density(queue);
|
||||
|
||||
//actually simulate
|
||||
int frameCount = 1;
|
||||
for(int frame = 0; frame < frameCount; frame++){
|
||||
fluid_grid2_simulate(3*3*3,queue,env,FLUID_GRID2_SIM_STEP);
|
||||
}
|
||||
|
||||
//test the result
|
||||
float afterSum = chunk_queue_sum_density(queue);
|
||||
if(fabs(beforeSum - afterSum) > FLUID_GRID2_PROJECTION_ERROR_MARGIN){
|
||||
rVal += assertEqualsFloat(beforeSum,afterSum,"Advection changed density! %f %f \n");
|
||||
}
|
||||
|
||||
return rVal;
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing full sim routine
|
||||
*/
|
||||
int fluid_sim_grid2_full_sim_test2(){
|
||||
printf("fluid_sim_grid2_full_sim_test2\n");
|
||||
int rVal = 0;
|
||||
Environment * env = fluid_environment_create();
|
||||
Chunk ** queue = NULL;
|
||||
queue = createChunkGrid(env,3,3,3);
|
||||
|
||||
|
||||
|
||||
//setup chunk values
|
||||
Chunk * currentChunk = queue[0];
|
||||
currentChunk->d[CENTER_LOC][IX(4,4,4)] = MAX_FLUID_VALUE;
|
||||
currentChunk->u[CENTER_LOC][IX(4,4,4)] = MAX_FLUID_VALUE;
|
||||
float beforeSum = chunk_queue_sum_density(queue);
|
||||
|
||||
//actually simulate
|
||||
int frameCount = 50;
|
||||
for(int frame = 0; frame < frameCount; frame++){
|
||||
fluid_grid2_simulate(3*3*3,queue,env,FLUID_GRID2_SIM_STEP);
|
||||
}
|
||||
|
||||
//test the result
|
||||
float afterSum = chunk_queue_sum_density(queue);
|
||||
if(fabs(beforeSum - afterSum) > FLUID_GRID2_PROJECTION_ERROR_MARGIN){
|
||||
rVal += assertEqualsFloat(beforeSum,afterSum,"Advection changed density! %f %f \n");
|
||||
}
|
||||
|
||||
return rVal;
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing full sim routine
|
||||
*/
|
||||
int fluid_sim_grid2_full_sim_test3(){
|
||||
printf("fluid_sim_grid2_full_sim_test3\n");
|
||||
int rVal = 0;
|
||||
Environment * env = fluid_environment_create();
|
||||
Chunk ** queue = NULL;
|
||||
queue = createChunkGrid(env,3,3,3);
|
||||
|
||||
|
||||
|
||||
//setup chunk values
|
||||
Chunk * currentChunk = queue[0];
|
||||
chunk_fill_real(queue[13]->d[CENTER_LOC],MAX_FLUID_VALUE);
|
||||
chunk_fill_real(queue[13]->u[CENTER_LOC],MAX_FLUID_VALUE);
|
||||
float beforeSum = chunk_queue_sum_density(queue);
|
||||
|
||||
//actually simulate
|
||||
int frameCount = 50;
|
||||
for(int frame = 0; frame < frameCount; frame++){
|
||||
fluid_grid2_simulate(3*3*3,queue,env,FLUID_GRID2_SIM_STEP);
|
||||
printf("Existing sum: %lf\n", env->state.existingDensity);
|
||||
printf("Added density: %lf\n", env->state.newDensity);
|
||||
printf("Adjustment Ratio: %f\n", env->state.normalizationRatio);
|
||||
float afterSum = chunk_queue_sum_density(queue);
|
||||
printf("AFter transform sum: %f\n",afterSum);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
//test the result
|
||||
float afterSum = chunk_queue_sum_density(queue);
|
||||
if(fabs(beforeSum - afterSum) > FLUID_GRID2_PROJECTION_ERROR_MARGIN){
|
||||
rVal += assertEqualsFloat(beforeSum,afterSum,"Advection changed density! %f %f \n");
|
||||
}
|
||||
|
||||
return rVal;
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing full sim routines
|
||||
*/
|
||||
int fluid_sim_grid2_full_sim_tests(int argc, char **argv){
|
||||
int rVal = 0;
|
||||
|
||||
solver_multigrid_allocate();
|
||||
fluid_grid2_allocate_arrays();
|
||||
|
||||
rVal += fluid_sim_grid2_full_sim_test1();
|
||||
rVal += fluid_sim_grid2_full_sim_test2();
|
||||
rVal += fluid_sim_grid2_full_sim_test3();
|
||||
|
||||
return rVal;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user