From c35a599f6b633707a341c8864fdd839bd9562dc3 Mon Sep 17 00:00:00 2001 From: unknown <> Date: Mon, 24 Jul 2023 18:39:19 -0400 Subject: [PATCH] Setup work for chunk sharing --- src/main/c/densitystep.c | 23 ++-- src/main/c/includes/electrosphere_FluidSim.h | 20 ++- src/main/c/velocitystep.c | 32 +++++ src/main/java/electrosphere/FluidSim.java | 133 +++++++++++++------ 4 files changed, 150 insertions(+), 58 deletions(-) diff --git a/src/main/c/densitystep.c b/src/main/c/densitystep.c index 7951f27..e322e50 100644 --- a/src/main/c/densitystep.c +++ b/src/main/c/densitystep.c @@ -6,7 +6,7 @@ #include "includes/utilities.h" #include "includes/chunkmask.h" -void advectDensity(JNIEnv * env, uint32_t chunk_mask, int N, int b, jobjectArray jrd, float * d0, float * u, float * v, float * w, float dt); +void advectDensity(JNIEnv * env, uint32_t chunk_mask, int N, int b, jobjectArray jrd, jobjectArray d0, float * u, float * v, float * w, float dt); /* * Class: electrosphere_FluidSim @@ -19,12 +19,12 @@ JNIEXPORT void JNICALL Java_electrosphere_FluidSim_addDensity jint N, jint chunk_mask, jobjectArray jrx, - jobject x0, + jobjectArray x0, jfloat dt){ int i; int size=N*N*N; float * x = GET_ARR(env,jrx,CENTER_LOC); - float * s = (*env)->GetDirectBufferAddress(env,x0); + float * s = GET_ARR(env,x0,CENTER_LOC); for(i=0; iGetDirectBufferAddress(env,jrx0); + float * x0 = GET_ARR(env,jrx0,CENTER_LOC); __m256 aScalar = _mm256_set1_ps(a); __m256 cScalar = _mm256_set1_ps(c); @@ -95,18 +95,17 @@ JNIEXPORT void JNICALL Java_electrosphere_FluidSim_advectDensity jint N, jint chunk_mask, jobjectArray jrx, - jobject jrx0, + jobjectArray jrx0, jobjectArray jru, jobjectArray jrv, jobjectArray jrw, jfloat DIFFUSION_CONST, jfloat VISCOSITY_CONST, jfloat dt){ - float * x0 = (*env)->GetDirectBufferAddress(env,jrx0); - advectDensity(env,chunk_mask,N,3,jrx,x0,GET_ARR(env,jru,CENTER_LOC),GET_ARR(env,jrv,CENTER_LOC),GET_ARR(env,jrw,CENTER_LOC),dt); + advectDensity(env,chunk_mask,N,3,jrx,jrx0,GET_ARR(env,jru,CENTER_LOC),GET_ARR(env,jrv,CENTER_LOC),GET_ARR(env,jrw,CENTER_LOC),dt); } -void advectDensity(JNIEnv * env, uint32_t chunk_mask, int N, int b, jobjectArray jrd, float * d0, float * u, float * v, float * w, float dt){ +void advectDensity(JNIEnv * env, uint32_t chunk_mask, int N, int b, jobjectArray jrd, jobjectArray d0, float * u, float * v, float * w, float dt){ int i, j, k, i0, j0, k0, i1, j1, k1; int m,n,o; float x, y, z, s0, t0, s1, t1, u1, u0, dtx,dty,dtz; @@ -115,12 +114,14 @@ void advectDensity(JNIEnv * env, uint32_t chunk_mask, int N, int b, jobjectArray float * d = GET_ARR(env,jrd,CENTER_LOC); - float * sampleArr = d0; + float * x0 = GET_ARR(env,d0,CENTER_LOC); + + float * sampleArr = x0; for(k=1; k 0 && j < 17 && j > 0 && k < 17 && k > 0 ){ xf.put(1); - uf.put(1); - vf.put(-1f); - wf.put(rand.nextFloat() * 0.1f); + uf.put(-50); + vf.put(0); + wf.put(0); } else { xf.put(0); uf.put(0); @@ -131,16 +131,16 @@ public class FluidSim { } } else { if( - Math.abs(8 - i) < 4 && + Math.abs(0 - i) < 4 && Math.abs(j) < 4 && - Math.abs(16 - k) < 4 && + Math.abs(0 - k) < 4 && i < 17 && i > 0 && j < 17 && j > 0 && k < 17 && k > 0 ){ xf.put(1); - uf.put(1); - vf.put(-1f); + uf.put(50); + vf.put(0); wf.put(rand.nextFloat() * 0.1f); } else { xf.put(0); @@ -206,27 +206,6 @@ public class FluidSim { } } - /** - * Runs a frame of the fluid simulation - */ - private void simulate(int step, float timestep){ - simulate( - DIM, - 0, - density, - densityAddition, - uVector, - vVector, - wVector, - uAdditionVector, - vAdditionVector, - wAdditionVector, - DIFFUSION_CONSTANT, - VISCOSITY_CONSTANT, - timestep - ); - } - private static void solveChunkMask(FluidSim[][][] simArray){ for(int x = 0; x < simArray.length; x++){ for(int y = 0; y < simArray[0].length; y++){ @@ -256,6 +235,20 @@ public class FluidSim { } private static void solveVectorDiffusion(FluidSim[][][] simArray, float timestep){ + //samples u,v,w,u0,v0,w0 + //sets u,v,w + for(int x = 0; x < simArray.length; x++){ + for(int y = 0; y < simArray[0].length; y++){ + for(int z = 0; z < simArray[0][0].length; z++){ + simArray[x][y][z].copyNeighborsWrapper(1, simArray[x][y][z].uVector); + simArray[x][y][z].copyNeighborsWrapper(2, simArray[x][y][z].vVector); + simArray[x][y][z].copyNeighborsWrapper(3, simArray[x][y][z].wVector); + simArray[x][y][z].copyNeighborsWrapper(1, simArray[x][y][z].uAdditionVector); + simArray[x][y][z].copyNeighborsWrapper(2, simArray[x][y][z].vAdditionVector); + simArray[x][y][z].copyNeighborsWrapper(3, simArray[x][y][z].wAdditionVector); + } + } + } for(int l = 0; l < LINEARSOLVERTIMES; l++){ for(int x = 0; x < simArray.length; x++){ for(int y = 0; y < simArray[0].length; y++){ @@ -281,6 +274,19 @@ public class FluidSim { } private static void solveProjection(FluidSim[][][] simArray, float timestep){ + //samples u,v,w + //sets u0,v0 + for(int x = 0; x < simArray.length; x++){ + for(int y = 0; y < simArray[0].length; y++){ + for(int z = 0; z < simArray[0][0].length; z++){ + simArray[x][y][z].copyNeighborsWrapper(1, simArray[x][y][z].uVector); + simArray[x][y][z].copyNeighborsWrapper(2, simArray[x][y][z].vVector); + simArray[x][y][z].copyNeighborsWrapper(3, simArray[x][y][z].wVector); + simArray[x][y][z].copyNeighborsWrapper(1, simArray[x][y][z].uAdditionVector); + simArray[x][y][z].copyNeighborsWrapper(2, simArray[x][y][z].vAdditionVector); + } + } + } for(int x = 0; x < simArray.length; x++){ for(int y = 0; y < simArray[0].length; y++){ for(int z = 0; z < simArray[0][0].length; z++){ @@ -300,6 +306,9 @@ public class FluidSim { } } } + //samples u0, v0 + //sets u0 + //these should have just been mirrored in the above // //Perform main projection solver for(int l = 0; l < LINEARSOLVERTIMES; l++){ @@ -322,6 +331,8 @@ public class FluidSim { } } } + //samples u,v,w,u0 + //sets u,v,w //Finalize projection for(int x = 0; x < simArray.length; x++){ for(int y = 0; y < simArray[0].length; y++){ @@ -363,6 +374,18 @@ public class FluidSim { } } } + //then need to mirror each array as relevant + for(int x = 0; x < simArray.length; x++){ + for(int y = 0; y < simArray[0].length; y++){ + for(int z = 0; z < simArray[0][0].length; z++){ + simArray[x][y][z].copyNeighborsWrapper(1, simArray[x][y][z].uVector); + simArray[x][y][z].copyNeighborsWrapper(2, simArray[x][y][z].vVector); + simArray[x][y][z].copyNeighborsWrapper(3, simArray[x][y][z].wVector); + simArray[x][y][z].copyNeighborsWrapper(1, simArray[x][y][z].uAdditionVector); + simArray[x][y][z].copyNeighborsWrapper(2, simArray[x][y][z].vAdditionVector); + } + } + } } private void swapVectorFields(){ @@ -387,6 +410,7 @@ public class FluidSim { } private static void advectVectorsAcrossBoundaries(FluidSim[][][] simArray, float timestep){ + //samples u,v,w,u0,v0,w0 for(int x = 0; x < simArray.length; x++){ for(int y = 0; y < simArray[0].length; y++){ for(int z = 0; z < simArray[0][0].length; z++){ @@ -424,10 +448,28 @@ public class FluidSim { } } + private void swapAllDensityArrays(FluidSim[][][] simArray, float timestep){ + for(int x = 0; x < simArray.length; x++){ + for(int y = 0; y < simArray[0].length; y++){ + for(int z = 0; z < simArray[0][0].length; z++){ + simArray[x][y][z].swapDensityArrays(); + } + } + } + for(int x = 0; x < simArray.length; x++){ + for(int y = 0; y < simArray[0].length; y++){ + for(int z = 0; z < simArray[0][0].length; z++){ + simArray[x][y][z].copyNeighborsWrapper(0, simArray[x][y][z].density); + simArray[x][y][z].copyNeighborsWrapper(0, simArray[x][y][z].densityAddition); + } + } + } + } + private void swapDensityArrays(){ ByteBuffer tmp = density[13]; - density[13] = densityAddition; - densityAddition = tmp; + density[13] = densityAddition[13]; + densityAddition[13] = tmp; } private static void diffuseDensity(FluidSim[][][] simArray, float timestep){ @@ -605,7 +647,7 @@ public class FluidSim { private void addDensityWrapper(float timestep){ addDensity(DIM, chunkMask, density, densityAddition, timestep); } - private native void addDensity(int DIM_X, int chunkMask, ByteBuffer[] x, ByteBuffer x0, float timestep); + private native void addDensity(int DIM_X, int chunkMask, ByteBuffer[] x, ByteBuffer[] x0, float timestep); /** * Solve density diffusion @@ -613,7 +655,7 @@ public class FluidSim { private void solveDiffuseDensityWrapper(float timestep){ solveDiffuseDensity(DIM, chunkMask, density, densityAddition, uVector, vVector, wVector, DIFFUSION_CONSTANT, VISCOSITY_CONSTANT, timestep); } - private native void solveDiffuseDensity(int DIM_X, int chunkMask, ByteBuffer[] x, ByteBuffer x0, ByteBuffer[] u, ByteBuffer v[], ByteBuffer w[], float DIFFUSION_CONSTANT, float VISCOSITY_CONSTANT, float timestep); + private native void solveDiffuseDensity(int DIM_X, int chunkMask, ByteBuffer[] x, ByteBuffer[] x0, ByteBuffer[] u, ByteBuffer v[], ByteBuffer w[], float DIFFUSION_CONSTANT, float VISCOSITY_CONSTANT, float timestep); /** * Solve density diffusion @@ -621,7 +663,7 @@ public class FluidSim { private void advectDensityWrapper(float timestep){ advectDensity(DIM, chunkMask, density, densityAddition, uVector, vVector, wVector, DIFFUSION_CONSTANT, VISCOSITY_CONSTANT, timestep); } - private native void advectDensity(int DIM_X, int chunkMask, ByteBuffer[] x, ByteBuffer x0, ByteBuffer[] u, ByteBuffer v[], ByteBuffer w[], float DIFFUSION_CONSTANT, float VISCOSITY_CONSTANT, float timestep); + private native void advectDensity(int DIM_X, int chunkMask, ByteBuffer[] x, ByteBuffer[] x0, ByteBuffer[] u, ByteBuffer v[], ByteBuffer w[], float DIFFUSION_CONSTANT, float VISCOSITY_CONSTANT, float timestep); /** @@ -632,6 +674,15 @@ public class FluidSim { } private native void setBoundsToNeighbors(int DIM_X, int chunkMask, int vectorDir, ByteBuffer[] neighborMap); + /** + * Sets the bounds of the neighbormap to neighbor values if available, otherwise doesn't mess with them. + * This is to make sure zeroing out doesn't mess up the sim + */ + private void copyNeighborsWrapper(int vectorDir, ByteBuffer[] neighborMap){ + copyNeighbors(DIM, chunkMask, vectorDir, neighborMap); + } + private native void copyNeighbors(int DIM_X, int chunkMask, int vectorDir, ByteBuffer[] neighborMap); + @@ -723,8 +774,8 @@ public class FluidSim { * Writes data from the java-side arrays into buffers that get passed into c-side */ private void writeNewStateIntoBuffers(){ - if(densityAddition.position() > 0){ - densityAddition.position(0); + if(densityAddition[13].position() > 0){ + densityAddition[13].position(0); } if(uAdditionVector[13].position() > 0){ uAdditionVector[13].position(0); @@ -735,7 +786,7 @@ public class FluidSim { if(wAdditionVector[13].position() > 0){ wAdditionVector[13].position(0); } - FloatBuffer x0FloatView = densityAddition.asFloatBuffer(); + FloatBuffer x0FloatView = densityAddition[13].asFloatBuffer(); FloatBuffer u0FloatView = uAdditionVector[13].asFloatBuffer(); FloatBuffer v0FloatView = vAdditionVector[13].asFloatBuffer(); FloatBuffer w0FloatView = wAdditionVector[13].asFloatBuffer();