basic cleanup
All checks were successful
studiorailgun/fluid-sim/pipeline/head This commit looks good

This commit is contained in:
unknown 2024-03-15 17:22:21 -04:00
parent 69a803fb3f
commit c1d9a7a054
5 changed files with 66 additions and 78 deletions

View File

@ -8,7 +8,7 @@ uint32_t matrix_transform(JNIEnv * env, jobjectArray jrx);
/**
* Calculates the bitmask for available chunks for the provided chunk's neighbor array
*/
JNIEXPORT jint JNICALL Java_electrosphere_FluidSim_calculateChunkMask(JNIEnv * env, jobject this, jobjectArray jrx){
int calculateChunkMask(JNIEnv * env, jobjectArray jrx){
return matrix_transform(env,jrx);
}

View File

@ -6,18 +6,16 @@
#include "includes/chunkmask.h"
/*
* Class: electrosphere_FluidSim
* Method: addDensity
* Signature: (II[Ljava/nio/ByteBuffer;Ljava/nio/ByteBuffer;F)V
*/
void Java_electrosphere_FluidSim_addDensity
(
/**
* Adds density to the density array
*/
void addDensity(
int N,
int chunk_mask,
float ** d,
float ** d0,
float dt){
float dt
){
int i;
int size=N*N*N;
float * x = GET_ARR_RAW(env,d,CENTER_LOC);
@ -28,12 +26,9 @@ void Java_electrosphere_FluidSim_addDensity
}
/*
* Class: electrosphere_FluidSim
* Method: solveDiffuseDensity
* Signature: (II[Ljava/nio/ByteBuffer;Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;FFF)V
* A single iteration of the jacobi to solve density diffusion
*/
void Java_electrosphere_FluidSim_solveDiffuseDensity
(
void solveDiffuseDensity(
int N,
int chunk_mask,
float ** d,
@ -43,7 +38,8 @@ void Java_electrosphere_FluidSim_solveDiffuseDensity
float ** jrw,
float DIFFUSION_CONST,
float VISCOSITY_CONST,
float dt){
float dt
){
float a=dt*DIFFUSION_CONST*N*N*N;
float c=1+6*a;
int i, j, k, l, m;
@ -80,6 +76,9 @@ void Java_electrosphere_FluidSim_solveDiffuseDensity
}
}
/**
* Advects the density based on the vectors
*/
void advectDensity(uint32_t chunk_mask, int N, float ** d, float ** d0, float ** ur, float ** vr, float ** wr, float dt){
int i, j, k, i0, j0, k0, i1, j1, k1;
int m,n,o;

View File

@ -156,7 +156,7 @@ JNIEXPORT void JNICALL Java_electrosphere_FluidSim_simulate(
for(int i = 0; i < numChunks; i++){
Chunk * currentChunk = chunks[i];
chunkMask = currentChunk->chunkMask;
Java_electrosphere_FluidSim_addSourceToVectors(
addSourceToVectors(
DIM,
chunkMask,
currentChunk->u,
@ -229,7 +229,7 @@ JNIEXPORT void JNICALL Java_electrosphere_FluidSim_simulate(
for(int i = 0; i < numChunks; i++){
Chunk * currentChunk = chunks[i];
chunkMask = currentChunk->chunkMask;
Java_electrosphere_FluidSim_solveVectorDiffuse(DIM,chunkMask,currentChunk->u,currentChunk->v,currentChunk->w,currentChunk->u0,currentChunk->v0,currentChunk->w0,DIFFUSION_CONSTANT,VISCOSITY_CONSTANT,timestep);
solveVectorDiffuse(DIM,chunkMask,currentChunk->u,currentChunk->v,currentChunk->w,currentChunk->u0,currentChunk->v0,currentChunk->w0,DIFFUSION_CONSTANT,VISCOSITY_CONSTANT,timestep);
}
if(SAVE_STEPS){
sprintf(fileNameBuff, "./chunks/diffuseUStep%dx", l);
@ -305,7 +305,7 @@ JNIEXPORT void JNICALL Java_electrosphere_FluidSim_simulate(
for(int i = 0; i < numChunks; i++){
Chunk * currentChunk = chunks[i];
chunkMask = currentChunk->chunkMask;
Java_electrosphere_FluidSim_setupProjection(DIM,chunkMask,currentChunk->u,currentChunk->v,currentChunk->w,currentChunk->u0,currentChunk->v0,DIFFUSION_CONSTANT,VISCOSITY_CONSTANT,timestep);
setupProjection(DIM,chunkMask,currentChunk->u,currentChunk->v,currentChunk->w,currentChunk->u0,currentChunk->v0,DIFFUSION_CONSTANT,VISCOSITY_CONSTANT,timestep);
}
saveStep(chunks[0]->v0[CENTER_LOC], "./chunks/setupProj1Div");
@ -333,7 +333,7 @@ JNIEXPORT void JNICALL Java_electrosphere_FluidSim_simulate(
for(int i = 0; i < numChunks; i++){
Chunk * currentChunk = chunks[i];
chunkMask = currentChunk->chunkMask;
Java_electrosphere_FluidSim_solveProjection(DIM,chunkMask,currentChunk->u,currentChunk->v,currentChunk->w,currentChunk->u0,currentChunk->v0,currentChunk->w0,DIFFUSION_CONSTANT,VISCOSITY_CONSTANT,timestep);
solveProjection(DIM,chunkMask,currentChunk->u,currentChunk->v,currentChunk->w,currentChunk->u0,currentChunk->v0,currentChunk->w0,DIFFUSION_CONSTANT,VISCOSITY_CONSTANT,timestep);
}
if(SAVE_STEPS){
sprintf(fileNameBuff, "./chunks/proj1Step%dx", l);
@ -360,7 +360,7 @@ JNIEXPORT void JNICALL Java_electrosphere_FluidSim_simulate(
for(int i = 0; i < numChunks; i++){
Chunk * currentChunk = chunks[i];
chunkMask = currentChunk->chunkMask;
Java_electrosphere_FluidSim_finalizeProjection(DIM,chunkMask,currentChunk->u,currentChunk->v,currentChunk->w,currentChunk->u0,currentChunk->v0,currentChunk->w0,DIFFUSION_CONSTANT,VISCOSITY_CONSTANT,timestep);
finalizeProjection(DIM,chunkMask,currentChunk->u,currentChunk->v,currentChunk->w,currentChunk->u0,currentChunk->v0,currentChunk->w0,DIFFUSION_CONSTANT,VISCOSITY_CONSTANT,timestep);
}
saveStep(chunks[0]->u[CENTER_LOC], "./chunks/finalizeProj1U");
@ -458,7 +458,7 @@ JNIEXPORT void JNICALL Java_electrosphere_FluidSim_simulate(
for(int i = 0; i < numChunks; i++){
Chunk * currentChunk = chunks[i];
chunkMask = currentChunk->chunkMask;
Java_electrosphere_FluidSim_advectVectors(DIM,chunkMask,currentChunk->u,currentChunk->v,currentChunk->w,currentChunk->u0,currentChunk->v0,currentChunk->w0,DIFFUSION_CONSTANT,VISCOSITY_CONSTANT,timestep);
advectVectors(DIM,chunkMask,currentChunk->u,currentChunk->v,currentChunk->w,currentChunk->u0,currentChunk->v0,currentChunk->w0,DIFFUSION_CONSTANT,VISCOSITY_CONSTANT,timestep);
}
//update neighbor arr
for(int i = 0; i < numChunks; i++){
@ -499,7 +499,7 @@ JNIEXPORT void JNICALL Java_electrosphere_FluidSim_simulate(
for(int i = 0; i < numChunks; i++){
Chunk * currentChunk = chunks[i];
chunkMask = currentChunk->chunkMask;
Java_electrosphere_FluidSim_setupProjection(DIM,chunkMask,currentChunk->u,currentChunk->v,currentChunk->w,currentChunk->u0,currentChunk->v0,DIFFUSION_CONSTANT,VISCOSITY_CONSTANT,timestep);
setupProjection(DIM,chunkMask,currentChunk->u,currentChunk->v,currentChunk->w,currentChunk->u0,currentChunk->v0,DIFFUSION_CONSTANT,VISCOSITY_CONSTANT,timestep);
}
//update array for vectors
for(int i = 0; i < numChunks; i++){
@ -519,7 +519,7 @@ JNIEXPORT void JNICALL Java_electrosphere_FluidSim_simulate(
for(int i = 0; i < numChunks; i++){
Chunk * currentChunk = chunks[i];
chunkMask = currentChunk->chunkMask;
Java_electrosphere_FluidSim_solveProjection(DIM,chunkMask,currentChunk->u,currentChunk->v,currentChunk->w,currentChunk->u0,currentChunk->v0,currentChunk->w0,DIFFUSION_CONSTANT,VISCOSITY_CONSTANT,timestep);
solveProjection(DIM,chunkMask,currentChunk->u,currentChunk->v,currentChunk->w,currentChunk->u0,currentChunk->v0,currentChunk->w0,DIFFUSION_CONSTANT,VISCOSITY_CONSTANT,timestep);
}
for(int i = 0; i < numChunks; i++){
Chunk * currentChunk = chunks[i];
@ -534,7 +534,7 @@ JNIEXPORT void JNICALL Java_electrosphere_FluidSim_simulate(
for(int i = 0; i < numChunks; i++){
Chunk * currentChunk = chunks[i];
chunkMask = currentChunk->chunkMask;
Java_electrosphere_FluidSim_finalizeProjection(DIM,chunkMask,currentChunk->u,currentChunk->v,currentChunk->w,currentChunk->u0,currentChunk->v0,currentChunk->w0,DIFFUSION_CONSTANT,VISCOSITY_CONSTANT,timestep);
finalizeProjection(DIM,chunkMask,currentChunk->u,currentChunk->v,currentChunk->w,currentChunk->u0,currentChunk->v0,currentChunk->w0,DIFFUSION_CONSTANT,VISCOSITY_CONSTANT,timestep);
}
//set boundaries a final time for u,v,w
//...
@ -573,7 +573,7 @@ JNIEXPORT void JNICALL Java_electrosphere_FluidSim_simulate(
for(int i = 0; i < numChunks; i++){
Chunk * currentChunk = chunks[i];
chunkMask = currentChunk->chunkMask;
Java_electrosphere_FluidSim_addDensity(DIM,chunkMask,currentChunk->d,currentChunk->d0,timestep);
addDensity(DIM,chunkMask,currentChunk->d,currentChunk->d0,timestep);
}
}
//swap all density arrays
@ -602,7 +602,7 @@ JNIEXPORT void JNICALL Java_electrosphere_FluidSim_simulate(
for(int i = 0; i < numChunks; i++){
Chunk * currentChunk = chunks[i];
chunkMask = currentChunk->chunkMask;
Java_electrosphere_FluidSim_solveDiffuseDensity(DIM,chunkMask,currentChunk->d,currentChunk->d0,currentChunk->u,currentChunk->v,currentChunk->w,DIFFUSION_CONSTANT,VISCOSITY_CONSTANT,timestep);
solveDiffuseDensity(DIM,chunkMask,currentChunk->d,currentChunk->d0,currentChunk->u,currentChunk->v,currentChunk->w,DIFFUSION_CONSTANT,VISCOSITY_CONSTANT,timestep);
}
for(int i = 0; i < numChunks; i++){
Chunk * currentChunk = chunks[i];

View File

@ -16,7 +16,7 @@ JNIEXPORT jint JNICALL Java_electrosphere_FluidSim_calculateChunkMask
* Method: addSourceToVectors
* Signature: (II[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;FFF)V
*/
void Java_electrosphere_FluidSim_addSourceToVectors
void addSourceToVectors
(int, int, float **, float **, float **, float **, float **, float **, float, float, float);
/*
@ -24,7 +24,7 @@ void Java_electrosphere_FluidSim_addSourceToVectors
* Method: solveVectorDiffuse
* Signature: (II[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;FFF)V
*/
void Java_electrosphere_FluidSim_solveVectorDiffuse
void solveVectorDiffuse
(int, int, float **, float **, float **, float **, float **, float **, float, float, float);
/*
@ -32,7 +32,7 @@ void Java_electrosphere_FluidSim_solveVectorDiffuse
* Method: setupProjection
* Signature: (II[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;FFF)V
*/
void Java_electrosphere_FluidSim_setupProjection
void setupProjection
(
int N,
int chunk_mask,
@ -50,7 +50,7 @@ void Java_electrosphere_FluidSim_setupProjection
* Method: solveProjection
* Signature: (II[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;FFF)V
*/
void Java_electrosphere_FluidSim_solveProjection
void solveProjection
(int, int, float **, float **, float **, float **, float **, float **, float, float, float);
/*
@ -58,7 +58,7 @@ void Java_electrosphere_FluidSim_solveProjection
* Method: finalizeProjection
* Signature: (II[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;FFF)V
*/
void Java_electrosphere_FluidSim_finalizeProjection
void finalizeProjection
(int, int, float **, float **, float **, float **, float **, float **, float, float, float);
/*
@ -66,7 +66,7 @@ void Java_electrosphere_FluidSim_finalizeProjection
* Method: advectVectors
* Signature: (II[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;FFF)V
*/
void Java_electrosphere_FluidSim_advectVectors
void advectVectors
(int, int, float **, float **, float **, float **, float **, float **, float, float, float);
/*
@ -74,7 +74,7 @@ void Java_electrosphere_FluidSim_advectVectors
* Method: addDensity
* Signature: (II[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;F)V
*/
void Java_electrosphere_FluidSim_addDensity
void addDensity
(int, int, float **, float **, float);
/*
@ -82,7 +82,7 @@ void Java_electrosphere_FluidSim_addDensity
* Method: solveDiffuseDensity
* Signature: (II[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;FFF)V
*/
void Java_electrosphere_FluidSim_solveDiffuseDensity
void solveDiffuseDensity
(int, int, float **, float **, float **, float **, float **, float, float, float);
/*

View File

@ -23,7 +23,7 @@ void advect(uint32_t chunk_mask, int N, int b, float ** jrd, float ** jrd0, floa
/*
* Adds force to all vectors
*/
void Java_electrosphere_FluidSim_addSourceToVectors
void addSourceToVectors
(
int N,
int chunk_mask,
@ -41,6 +41,9 @@ void Java_electrosphere_FluidSim_addSourceToVectors
add_source(N,GET_ARR_RAW(env,jrw,CENTER_LOC),GET_ARR_RAW(env,jrw0,CENTER_LOC),dt);
}
/**
* Adds from a source array to a destination array
*/
void add_source(int N, float * x, float * s, float dt){
int i;
int size=N*N*N;
@ -52,8 +55,7 @@ void add_source(int N, float * x, float * s, float dt){
/*
* Solves vector diffusion along all axis
*/
void Java_electrosphere_FluidSim_solveVectorDiffuse
(
void solveVectorDiffuse (
int N,
int chunk_mask,
float ** jru,
@ -64,7 +66,8 @@ void Java_electrosphere_FluidSim_solveVectorDiffuse
float ** jrw0,
float DIFFUSION_CONST,
float VISCOSITY_CONST,
float dt){
float dt
){
float a=dt*VISCOSITY_CONST*N*N*N;
float c=1+6*a;
int i, j, k, l, m;
@ -160,8 +163,7 @@ void Java_electrosphere_FluidSim_solveVectorDiffuse
/*
* Sets up a projection system of equations
*/
void Java_electrosphere_FluidSim_setupProjection
(
void setupProjection(
int N,
int chunk_mask,
float ** ur,
@ -171,7 +173,8 @@ void Java_electrosphere_FluidSim_setupProjection
float ** divr,
float DIFFUSION_CONST,
float VISCOSITY_CONST,
float dt){
float dt
){
int i, j, k;
__m256 nVector = _mm256_set1_ps(N);
@ -236,14 +239,6 @@ void Java_electrosphere_FluidSim_setupProjection
_mm256_storeu_ps(&div[IX(i,j,k)],vector);
_mm256_storeu_ps(&p[IX(i,j,k)],zeroVec);
// for(i = 1; i < N - 1; i++){
// div[IX(i,j,k)] =
// -scalar*h*(u[IX(i+1,j,k)]-u[IX(i-1,j,k)]+
// v[IX(i,j+1,k)]-v[IX(i,j-1,k)]+
// w[IX(i,j,k+1)]-w[IX(i,j,k-1)]);
// p[IX(i,j,k)] = 0;
// }
}
}
}
@ -251,8 +246,7 @@ void Java_electrosphere_FluidSim_setupProjection
/*
* Solves a projection system of equations
*/
void Java_electrosphere_FluidSim_solveProjection
(
void solveProjection(
int N,
int chunk_mask,
float ** jru,
@ -263,7 +257,8 @@ void Java_electrosphere_FluidSim_solveProjection
float ** jrw0,
float DIFFUSION_CONST,
float VISCOSITY_CONST,
float dt){
float dt
){
int a = 1;
int c = 6;
int i, j, k, l, m;
@ -295,9 +290,6 @@ void Java_electrosphere_FluidSim_solveProjection
p[IX(i,j,k)] = (div[IX(i,j,k)] + a*(p[IX(i-1,j,k)]+p[IX(i+1,j,k)]+p[IX(i,j-1,k)]+p[IX(i,j+1,k)]+p[IX(i,j,k-1)]+p[IX(i,j,k+1)]))/c;
}
}
// for(i=1; i < N-1; i++){
// p[IX(i,j,k)] = (div[IX(i,j,k)] + a*(p[IX(i-1,j,k)]+p[IX(i+1,j,k)]+p[IX(i,j-1,k)]+p[IX(i,j+1,k)]+p[IX(i,j,k-1)]+p[IX(i,j,k+1)]))/c;
// }
}
}
}
@ -305,8 +297,7 @@ void Java_electrosphere_FluidSim_solveProjection
/*
* Finalizes a projection (subtract curl, set bounds, etc)
*/
void Java_electrosphere_FluidSim_finalizeProjection
(
void finalizeProjection(
int N,
int chunk_mask,
float ** jru,
@ -317,7 +308,8 @@ void Java_electrosphere_FluidSim_finalizeProjection
float ** jrw0,
float DIFFUSION_CONST,
float VISCOSITY_CONST,
float dt){
float dt
){
int i, j, k;
__m256 constScalar = _mm256_set1_ps(0.5f*N);
__m256 vector, vector2, vector3;
@ -389,8 +381,7 @@ void Java_electrosphere_FluidSim_finalizeProjection
/*
* Advects u, v, and w
*/
void Java_electrosphere_FluidSim_advectVectors
(
void advectVectors(
int N,
int chunk_mask,
float ** jru,
@ -401,13 +392,16 @@ void Java_electrosphere_FluidSim_advectVectors
float ** jrw0,
float DIFFUSION_CONST,
float VISCOSITY_CONST,
float dt){
float dt
){
advect(chunk_mask,N,1,jru,jru0,GET_ARR_RAW(env,jru0,CENTER_LOC),GET_ARR_RAW(env,jrv0,CENTER_LOC),GET_ARR_RAW(env,jrw0,CENTER_LOC),dt);
advect(chunk_mask,N,2,jrv,jrv0,GET_ARR_RAW(env,jru0,CENTER_LOC),GET_ARR_RAW(env,jrv0,CENTER_LOC),GET_ARR_RAW(env,jrw0,CENTER_LOC),dt);
advect(chunk_mask,N,3,jrw,jrw0,GET_ARR_RAW(env,jru0,CENTER_LOC),GET_ARR_RAW(env,jrv0,CENTER_LOC),GET_ARR_RAW(env,jrw0,CENTER_LOC),dt);
}
/**
* Actually performs the advection
*/
void advect(uint32_t chunk_mask, int N, int b, float ** jrd, float ** jrd0, float * u, float * v, float * w, float dt){
int i, j, k, i0, j0, k0, i1, j1, k1;
int m,n,o;
@ -650,25 +644,18 @@ void advect(uint32_t chunk_mask, int N, int b, float ** jrd, float ** jrd0, floa
}
}
void setBoundsToNeighborsRaw
(
/**
* Sets the bounds of this cube to those of its neighbor
*/
void setBoundsToNeighborsRaw(
int N,
int chunk_mask,
int vector_dir,
float ** neighborArray){
float ** neighborArray
){
int DIM = N;
float * target = GET_ARR_RAW(env,neighborArray,CENTER_LOC);
float * source;
// for(int x=1; x < DIM-1; x++){
// for(int y = 1; y < DIM-1; y++){
// target[IX(0,x,y)] = vector_dir==BOUND_DIR_U ? -target[IX(1,x,y)] : target[IX(1,x,y)];
// }
// }
// for(int x=1; x < DIM-1; x++){
// for(int y = 1; y < DIM-1; y++){
// target[IX(DIM-1,x,y)] = vector_dir==BOUND_DIR_U ? -target[IX(DIM-2,x,y)] : target[IX(DIM-2,x,y)];
// }
// }
//set the faces bounds
for(int x=1; x < DIM-1; x++){
for(int y = 1; y < DIM-1; y++){
@ -681,6 +668,7 @@ void setBoundsToNeighborsRaw
target[IX(x,y,DIM-1)] = vector_dir==BOUND_DIR_W ? -target[IX(x,y,DIM-2)] : target[IX(x,y,DIM-2)];
}
}
//sets the edges of the chunk
for(int x = 1; x < DIM-1; x++){
target[IX(x,0,0)] = (float)(0.5f * (target[IX(x,1,0)] + target[IX(x,0,1)]));
target[IX(x,DIM-1,0)] = (float)(0.5f * (target[IX(x,DIM-2,0)] + target[IX(x,DIM-1,1)]));
@ -699,6 +687,7 @@ void setBoundsToNeighborsRaw
target[IX(DIM-1,DIM-1,x)] = (float)(0.5f * (target[IX(DIM-2,DIM-1,x)] + target[IX(DIM-1,DIM-2,x)]));
}
//sets the corners of the chunk
target[IX(0,0,0)] = (float)((target[IX(1,0,0)]+target[IX(0,1,0)]+target[IX(0,0,1)])/3.0);
target[IX(DIM-1,0,0)] = (float)((target[IX(DIM-2,0,0)]+target[IX(DIM-1,1,0)]+target[IX(DIM-1,0,1)])/3.0);
target[IX(0,DIM-1,0)] = (float)((target[IX(1,DIM-1,0)]+target[IX(0,DIM-2,0)]+target[IX(0,DIM-1,1)])/3.0);
@ -713,13 +702,13 @@ void setBoundsToNeighborsRaw
/**
* This exclusively copies neighbors to make sure zeroing out stuff doesn't break sim
*/
void copyNeighborsRaw
(
void copyNeighborsRaw(
int N,
int chunk_mask,
int cx,
int vector_dir,
float ** neighborArray){
float ** neighborArray
){
int DIM = N;
float * target = GET_ARR_RAW(env,neighborArray,CENTER_LOC);
float * source;