From 8a67458470f56919b63f349bddae1ad432b6973f Mon Sep 17 00:00:00 2001 From: unknown <> Date: Fri, 4 Aug 2023 20:15:29 -0400 Subject: [PATCH] Successful multichunking --- src/main/c/chunkmask.c | 42 +- src/main/c/densitystep.c | 69 ++- src/main/c/includes/electrosphere_FluidSim.h | 12 +- src/main/c/velocitystep.c | 502 ++++++++++++------- src/main/java/electrosphere/FluidSim.java | 406 ++++++++++++--- src/main/java/electrosphere/Main.java | 8 +- 6 files changed, 738 insertions(+), 301 deletions(-) diff --git a/src/main/c/chunkmask.c b/src/main/c/chunkmask.c index a83e330..06f879f 100644 --- a/src/main/c/chunkmask.c +++ b/src/main/c/chunkmask.c @@ -60,43 +60,43 @@ const uint32_t CHUNK_INDEX_ARR[] = { //control offsetting the advect sampler location if a valid neighbor chunk is hit const char CHUNK_NORMALIZE_U[] = { - -1, 0, 1, - -1, 0, 1, - -1, 0, 1, + 1, 0, -1, + 1, 0, -1, + 1, 0, -1, - -1, 0, 1, - -1, 0, 1, - -1, 0, 1, + 1, 0, -1, + 1, 0, -1, + 1, 0, -1, - -1, 0, 1, - -1, 0, 1, - -1, 0, 1, + 1, 0, -1, + 1, 0, -1, + 1, 0, -1, }; const char CHUNK_NORMALIZE_V[] = { - -1, -1, -1, - 0, 0, 0, 1, 1, 1, + 0, 0, 0, + -1, -1, -1, - -1, -1, -1, + 1, 1, 1, 0, 0, 0, - 1, 1, 1, + -1, -1, -1, - -1, -1, -1, + 1, 1, 1, 0, 0, 0, - 1, 1, 1, + -1, -1, -1, }; const char CHUNK_NORMALIZE_W[] = { - -1, -1, -1, - -1, -1, -1, - -1, -1, -1, + 1, 1, 1, + 1, 1, 1, + 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 1, 1, 1, - 1, 1, 1, - 1, 1, 1, + -1, -1, -1, + -1, -1, -1, + -1, -1, -1, }; \ No newline at end of file diff --git a/src/main/c/densitystep.c b/src/main/c/densitystep.c index e322e50..787dc5b 100644 --- a/src/main/c/densitystep.c +++ b/src/main/c/densitystep.c @@ -105,7 +105,7 @@ JNIEXPORT void JNICALL Java_electrosphere_FluidSim_advectDensity 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, jobjectArray d0, float * u, float * v, float * w, float dt){ +void advectDensity(JNIEnv * env, uint32_t chunk_mask, int N, int b, jobjectArray jrd, jobjectArray jrd0, 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; @@ -114,14 +114,12 @@ void advectDensity(JNIEnv * env, uint32_t chunk_mask, int N, int b, jobjectArray float * d = GET_ARR(env,jrd,CENTER_LOC); - float * x0 = GET_ARR(env,d0,CENTER_LOC); - - float * sampleArr = x0; + float * d0 = GET_ARR(env,jrd0,CENTER_LOC); for(k=1; k= N - 1){ + //cases to consider: + //m = 0, x = 17.01 + //m = 2, x = 20 + x = N-1; + i0=(int)N-2; + i1=N-1; + s0 = 0.001f; + s1 = 0.999f; + } else { + i0=(int)x; + i1=i0+1; + s1 = x-i0; + s0 = 1-s1; + } + //clamp location within chunk - if (x<0.5f) x=0.5f; - if (x>N+0.5f) x=N+0.5f; + // if (x<0.5f) x=0.5f; + // if (x>N+0.5f) x=N+0.5f; if (y<0.5f) y=0.5f; if (y>N+0.5f) y=N+0.5f; if (z<0.5f) z=0.5f; if (z>N+0.5f) z=N+0.5f; //get actual indices - i0=(int)x; - i1=i0+1; + // i0=(int)x; + // i1=i0+1; j0=(int)y; j1=j0+1; k0=(int)z; k1=k0+1; //calculate percentage of each index - s1 = x-i0; - s0 = 1-s1; + // s1 = x-i0; + // s0 = 1-s1; t1 = y-j0; t0 = 1-t1; u1 = z-k0; @@ -213,16 +236,16 @@ void advectDensity(JNIEnv * env, uint32_t chunk_mask, int N, int b, jobjectArray // } d[IX(i,j,k)] = s0*( - t0*u0*sampleArr[IX(i0,j0,k0)]+ - t1*u0*sampleArr[IX(i0,j1,k0)]+ - t0*u1*sampleArr[IX(i0,j0,k1)]+ - t1*u1*sampleArr[IX(i0,j1,k1)] + t0*u0*d0[IX(i0,j0,k0)]+ + t1*u0*d0[IX(i0,j1,k0)]+ + t0*u1*d0[IX(i0,j0,k1)]+ + t1*u1*d0[IX(i0,j1,k1)] )+ s1*( - t0*u0*sampleArr[IX(i1,j0,k0)]+ - t1*u0*sampleArr[IX(i1,j1,k0)]+ - t0*u1*sampleArr[IX(i1,j0,k1)]+ - t1*u1*sampleArr[IX(i1,j1,k1)] + t0*u0*d0[IX(i1,j0,k0)]+ + t1*u0*d0[IX(i1,j1,k0)]+ + t0*u1*d0[IX(i1,j0,k1)]+ + t1*u1*d0[IX(i1,j1,k1)] ); } } diff --git a/src/main/c/includes/electrosphere_FluidSim.h b/src/main/c/includes/electrosphere_FluidSim.h index f12e120..4d2531b 100644 --- a/src/main/c/includes/electrosphere_FluidSim.h +++ b/src/main/c/includes/electrosphere_FluidSim.h @@ -10,13 +10,13 @@ extern "C" { #undef electrosphere_FluidSim_DIM #define electrosphere_FluidSim_DIM 18L #undef electrosphere_FluidSim_DIFFUSION_CONSTANT -#define electrosphere_FluidSim_DIFFUSION_CONSTANT 1.0E-5f +#define electrosphere_FluidSim_DIFFUSION_CONSTANT 0.0f #undef electrosphere_FluidSim_VISCOSITY_CONSTANT -#define electrosphere_FluidSim_VISCOSITY_CONSTANT 1.0E-5f +#define electrosphere_FluidSim_VISCOSITY_CONSTANT 0.0f #undef electrosphere_FluidSim_LINEARSOLVERTIMES -#define electrosphere_FluidSim_LINEARSOLVERTIMES 10L +#define electrosphere_FluidSim_LINEARSOLVERTIMES 20L #undef electrosphere_FluidSim_GRAVITY -#define electrosphere_FluidSim_GRAVITY -1000.0f +#define electrosphere_FluidSim_GRAVITY -100.0f /* * Class: electrosphere_FluidSim * Method: simulate @@ -116,10 +116,10 @@ JNIEXPORT void JNICALL Java_electrosphere_FluidSim_setBoundsToNeighbors /* * Class: electrosphere_FluidSim * Method: copyNeighbors - * Signature: (III[Ljava/nio/ByteBuffer;)V + * Signature: (IIII[Ljava/nio/ByteBuffer;)V */ JNIEXPORT void JNICALL Java_electrosphere_FluidSim_copyNeighbors - (JNIEnv *, jobject, jint, jint, jint, jobjectArray); + (JNIEnv *, jobject, jint, jint, jint, jint, jobjectArray); #ifdef __cplusplus } diff --git a/src/main/c/velocitystep.c b/src/main/c/velocitystep.c index 5d6b86c..fcafbf3 100644 --- a/src/main/c/velocitystep.c +++ b/src/main/c/velocitystep.c @@ -16,7 +16,7 @@ #define SET_BOUND_USE_NEIGHBOR 1 void add_source(int N, float * x, float * s, float dt); -void advect(JNIEnv * env, uint32_t chunk_mask, int N, int b, jobjectArray jrd, float * d0, float * u, float * v, float * w, float dt); +void advect(JNIEnv * env, uint32_t chunk_mask, int N, int b, jobjectArray jrd, jobjectArray jrd0, float * u, float * v, float * w, float dt); /* @@ -66,7 +66,7 @@ JNIEXPORT void JNICALL Java_electrosphere_FluidSim_solveVectorDiffuse jfloat DIFFUSION_CONST, jfloat VISCOSITY_CONST, jfloat dt){ - float a=dt*DIFFUSION_CONST*N*N*N; + float a=dt*VISCOSITY_CONST*N*N*N; float c=1+6*a; int i, j, k, l, m; float * u = GET_ARR(env,jru,CENTER_LOC); @@ -177,7 +177,9 @@ JNIEXPORT void JNICALL Java_electrosphere_FluidSim_setupProjection jfloat dt){ int i, j, k; - __m256 nVector = _mm256_set1_ps(N); + __m256 xVector = _mm256_set1_ps(N); + __m256 yVector = _mm256_set1_ps(N); + __m256 zVector = _mm256_set1_ps(N); __m256 constScalar = _mm256_set1_ps(-1.0/3.0); __m256 zeroVec = _mm256_set1_ps(0); __m256 vector, vector2, vector3; @@ -189,52 +191,64 @@ JNIEXPORT void JNICALL Java_electrosphere_FluidSim_setupProjection float * p = GET_ARR(env,jru0,CENTER_LOC); float * div = GET_ARR(env,jrv0,CENTER_LOC); + float scalar = 1.0/3.0; + float h = 1.0/N; + for(k=1; kN-1){ - for(i=i-8; i < N-1; i++){ - x[IX(i,j,k)] = (x0[IX(i,j,k)] + a*(x[IX(i-1,j,k)]+x[IX(i+1,j,k)]+x[IX(i,j-1,k)]+x[IX(i,j+1,k)]+x[IX(i,j,k-1)]+x[IX(i,j,k+1)]))/c; - } + // for(i = 1; i < N-1; i=i+8){ + // __m256 vector = _mm256_loadu_ps(&p[IX(i-1,j,k)]); + // vector = _mm256_add_ps(vector,_mm256_loadu_ps(&p[IX(i+1,j,k)])); + // vector = _mm256_add_ps(vector,_mm256_loadu_ps(&p[IX(i,j-1,k)])); + // vector = _mm256_add_ps(vector,_mm256_loadu_ps(&p[IX(i,j+1,k)])); + // vector = _mm256_add_ps(vector,_mm256_loadu_ps(&p[IX(i,j,k-1)])); + // vector = _mm256_add_ps(vector,_mm256_loadu_ps(&p[IX(i,j,k+1)])); + // // vector = _mm256_mul_ps(vector,aScalar); + // vector = _mm256_add_ps(vector,_mm256_loadu_ps(&div[IX(i,j,k)])); + // vector = _mm256_div_ps(vector,cScalar); + // _mm256_storeu_ps(&p[IX(i,j,k)],vector); + // } + // //If there is any leftover, perform manual solving + // if(i>N-1){ + // for(i=i-8; 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; + // } + // } + 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; } } } @@ -309,9 +326,10 @@ JNIEXPORT void JNICALL Java_electrosphere_FluidSim_finalizeProjection jfloat VISCOSITY_CONST, jfloat dt){ int i, j, k; - __m256 nVector = _mm256_set1_ps(N); - __m256 constScalar = _mm256_set1_ps(0.5f*N); - __m256 zeroVec = _mm256_set1_ps(0); + // __m256 constScalar = _mm256_set1_ps(0.5f*N); + __m256 xScalar = _mm256_set1_ps(0.5*N); + __m256 yScalar = _mm256_set1_ps(0.5*N); + __m256 zScalar = _mm256_set1_ps(0.5*N); __m256 vector, vector2, vector3; float * u = GET_ARR(env,jru,CENTER_LOC); @@ -321,59 +339,66 @@ JNIEXPORT void JNICALL Java_electrosphere_FluidSim_finalizeProjection float * p = GET_ARR(env,jru0,CENTER_LOC); float * div = GET_ARR(env,jrv0,CENTER_LOC); + float h = 1.0 / N; + for ( k=1 ; k= N-1){ m += 1; } - if(y < 1){ n -= 1; } - if(y >= N-1){ n += 1; } - if(z < 1){ o -= 1; } - if(z >= N-1){ o += 1; } + if(x < 0){ m += 1; } + else if(x >= N){ m -= 1; } + if(y < 0){ n += 1; } + else if(y >= N){ n -= 1; } + if(z < 0){ o += 1; } + else if(z >= N){ o -= 1; } //If the out of bounds coordinate is in bounds for a neighbor chunk, use that chunk as source instead - // if(CK(m,n,o) != CENTER_LOC){ - // printf("Looking in border chunk\n"); - // } - // if(x > 16){ - // printf("%f %d %d %d\n",m,n,o); - // } - // if(CK(m,n,o) != CENTER_LOC && ARR_EXISTS(chunk_mask,m,n,o)){ - // // printf("Hit other chunk\n"); - // sampleArr = GET_ARR(env,jrd,CK(m,n,o)); - // x = x + CHUNK_NORMALIZE_U[CK(m,n,o)] * N; - // y = y + CHUNK_NORMALIZE_V[CK(m,n,o)] * N; - // z = z + CHUNK_NORMALIZE_W[CK(m,n,o)] * N; - // } + if(CK(m,n,o) != CENTER_LOC && ARR_EXISTS(chunk_mask,m,n,o)){ + + // if(i == 1 && j == 1 && k == 1){ + // printf("\narr indices: %d %d %d\n\n",m,n,o); + // } + + //cases: + //if x = 17.01, m = 2 + // 17 in current array is 1 in neighbor + // 18 in current array is 2 in neighbor + // 19 in current array is 3 in neighbor + //want to sample neighbor array at 1 & 2 + //x becomes 1.01, sampling new array (keep in mind that 0 in the new array should contain the current array values) + //modification: subtract 16 + + //cases: + //if x = 16.99, m = 2 + // 16 in current array is 0 in neighbor + // 17 in current array is 1 in neighbor + // 18 in current array is 2 in neighbor + // 19 in current array is 3 in neighbor + //want to sample current array still + //x becomes 1.01, sampling new array (keep in mind that 0 in the new array should contain the current array values) + //modification: no modification + + //if x = 0.01, m = 0 + // 0 in current array is 16 in neighbor + //-1 in current array is 15 in neighbor + //-2 in current array is 14 in neighbor + //want to sample current array still + //x becomes 15.01, sampling new array (keep in mind that 17 in the new array should contain the current array) + //modification: no modification + + //if x = -0.01, m = 0 + // 0 in current array is 16 in neighbor + //-1 in current array is 15 in neighbor + //-2 in current array is 14 in neighbor + //want to sample -1 & 0, so i0 becomes 15 + //x becomes 15.99, sampling new array (keep in mind that 17 in the new array should contain the current array) + //modification: add 16 + + //if x = -2, m = 0 + // 0 in current array is 16 in neighbor + //-1 in current array is 15 in neighbor + //-2 in current array is 14 in neighbor + //x becomes 14, sampling new array (keep in mind that 17 in the new array should contain the current array) + //modification: add 16 + + + // printf("Hit other chunk\n"); + d0 = GET_ARR(env,jrd0,CK(m,n,o)); + x = x + CHUNK_NORMALIZE_U[CK(m,n,o)] * (N-2); + // printf("%d => %f\n",m,x); + y = y + CHUNK_NORMALIZE_V[CK(m,n,o)] * (N-2); + z = z + CHUNK_NORMALIZE_W[CK(m,n,o)] * (N-2); + } //clamp location within chunk - if (x<0.5f) x=0.5f; - if (x>N+0.5f) x=N+0.5f; - if (y<0.5f) y=0.5f; - if (y>N+0.5f) y=N+0.5f; - if (z<0.5f) z=0.5f; - if (z>N+0.5f) z=N+0.5f; + //get indices, and calculate percentage to pull from each index + if(x < 0.001f){ + //cases to consider: + //m = 0, x = -10 + //m = 2, x = 0.01 + x=0.001f; + i0=(int)0; + i1=1; + s0 = 0.999f; + s1 = 0.001f; + } else if(x > N - 1){ + //cases to consider: + //m = 0, x = 17.01 + //m = 2, x = 20 + x = N-1; + i0=(int)N-2; + i1=N-1; + s0 = 0.001f; + s1 = 0.999f; + } else { + i0=(int)x; + i1=i0+1; + s1 = x-i0; + s0 = 1-s1; + } + + if(y < 0.001f){ + //cases to consider: + //m = 0, x = -10 + //m = 2, x = 0.01 + y=0.001f; + j0=(int)0; + j1=1; + t0 = 0.999f; + t1 = 0.001f; + } else if(y > N - 1){ + //cases to consider: + //m = 0, x = 17.01 + //m = 2, x = 20 + y = N-1; + j0=(int)N-2; + j1=N-1; + t0 = 0.001f; + t1 = 0.999f; + } else { + j0=(int)y; + j1=j0+1; + t1 = y-j0; + t0 = 1-t1; + } + + + if(z < 0.001f){ + //cases to consider: + //m = 0, x = -10 + //m = 2, x = 0.01 + z=0.001f; + k0=(int)0; + k1=1; + u0 = 0.999f; + u1 = 0.001f; + } else if(z > N - 1){ + //cases to consider: + //m = 0, x = 17.01 + //m = 2, x = 20 + z = N-1; + k0=(int)N-2; + k1=N-1; + u0 = 0.001f; + u1 = 0.999f; + } else { + k0=(int)z; + k1=k0+1; + u1 = z-k0; + u0 = 1-u1; + } + + // if (x<0.001f) x=0.001f; + // if (x>N+0.5f) x=N+0.5f; + // if (y<0.001f) y=0.001f; + // if (y>N+0.5f) y=N+0.5f; + // if (z<0.001f) z=0.001f; + // if (z>N+0.5f) z=N+0.5f; //get actual indices - i0=(int)x; - i1=i0+1; - j0=(int)y; - j1=j0+1; - k0=(int)z; - k1=k0+1; + // i0=(int)x; + // i1=i0+1; + // j0=(int)y; + // j1=j0+1; + // k0=(int)z; + // k1=k0+1; //calculate percentage of each index - s1 = x-i0; - s0 = 1-s1; - t1 = y-j0; - t0 = 1-t1; - u1 = z-k0; - u0 = 1-u1; + // s1 = x-i0; + // s0 = 1-s1; + // t1 = y-j0; + // t0 = 1-t1; + // u1 = z-k0; + // u0 = 1-u1; - if(i0 >= N){ - i0 = N - 1; - } + // if(i0 >= N){ + // i0 = N - 1; + // } // if(i0 < 0){ // i0 = 0; // } @@ -487,9 +632,9 @@ void advect(JNIEnv * env, uint32_t chunk_mask, int N, int b, jobjectArray jrd, f // if(k0 < 0){ // k0 = 0; // } - if(i1 >= N){ - i1 = N - 1; - } + // if(i1 >= N){ + // i1 = N - 1; + // } // if(i1 < 0){ // i1 = 0; // } @@ -507,17 +652,25 @@ void advect(JNIEnv * env, uint32_t chunk_mask, int N, int b, jobjectArray jrd, f // } d[IX(i,j,k)] = s0*( - t0*u0*sampleArr[IX(i0,j0,k0)]+ - t1*u0*sampleArr[IX(i0,j1,k0)]+ - t0*u1*sampleArr[IX(i0,j0,k1)]+ - t1*u1*sampleArr[IX(i0,j1,k1)] + t0*u0*d0[IX(i0,j0,k0)]+ + t1*u0*d0[IX(i0,j1,k0)]+ + t0*u1*d0[IX(i0,j0,k1)]+ + t1*u1*d0[IX(i0,j1,k1)] )+ s1*( - t0*u0*sampleArr[IX(i1,j0,k0)]+ - t1*u0*sampleArr[IX(i1,j1,k0)]+ - t0*u1*sampleArr[IX(i1,j0,k1)]+ - t1*u1*sampleArr[IX(i1,j1,k1)] + t0*u0*d0[IX(i1,j0,k0)]+ + t1*u0*d0[IX(i1,j1,k0)]+ + t0*u1*d0[IX(i1,j0,k1)]+ + t1*u1*d0[IX(i1,j1,k1)] ); + // if(i == 1 && j == 1 && k == 1 && m == 2){ + // printf("%d %d %d\n",m,n,o); + // printf("%d %d %d %d %d %d\n",i0,i1,j0,j1,k0,k1); + // printf("%.2f vs\n",d0[IX(i,j,k)]); + // printf("%.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f\n\n", + // d0[IX(i0,j0,k0)],d0[IX(i1,j0,k0)],d0[IX(i0,j1,k0)],d0[IX(i1,j1,k0)], + // d0[IX(i0,j0,k1)],d0[IX(i1,j0,k1)],d0[IX(i0,j1,k1)],d0[IX(i1,j1,k1)]); + // } } } } @@ -607,6 +760,7 @@ JNIEXPORT void JNICALL Java_electrosphere_FluidSim_copyNeighbors jobject this, jint N, jint chunk_mask, + jint cx, jint vector_dir, jobjectArray neighborArray){ int DIM = N; @@ -821,4 +975,4 @@ JNIEXPORT void JNICALL Java_electrosphere_FluidSim_copyNeighbors - } \ No newline at end of file +} \ No newline at end of file diff --git a/src/main/java/electrosphere/FluidSim.java b/src/main/java/electrosphere/FluidSim.java index 7925ab0..6fb915c 100644 --- a/src/main/java/electrosphere/FluidSim.java +++ b/src/main/java/electrosphere/FluidSim.java @@ -66,18 +66,18 @@ public class FluidSim { float[] wArrayView = new float[DIM * DIM * DIM]; //these should be set to the float[] u0ArrayView = new float[DIM * DIM * DIM]; - float[] v0ArrayView = new float[DIM * DIM * DIM]; + public float[] v0ArrayView = new float[DIM * DIM * DIM]; float[] w0ArrayView = new float[DIM * DIM * DIM]; int chunkMask = 0; - static final float DIFFUSION_CONSTANT = 0.00001f; - static final float VISCOSITY_CONSTANT = 0.00001f; + static final float DIFFUSION_CONSTANT = 0.0f; + static final float VISCOSITY_CONSTANT = 0.0f; - static final int LINEARSOLVERTIMES = 10; + static final int LINEARSOLVERTIMES = 20; - static final float GRAVITY = -1000f; + static final float GRAVITY = -100f; public void setup(Vector3i offset){ //allocate buffers for this chunk @@ -110,17 +110,17 @@ public class FluidSim { for(int i = 0; i < DIM; i++){ for(int j = 0; j < DIM; j++){ for(int k = 0; k < DIM; k++){ - if(offset.x == 1){ + if(offset.x == 0 && offset.y == 0 && offset.z == 0){ if( - Math.abs(16 - i) < 4 && - Math.abs(j) < 4 && - Math.abs(16 - k) < 4 && + Math.abs(16 - i) < 5 && + Math.abs(j) < 5 && + Math.abs(16 - k) < 5 && i < 17 && i > 0 && j < 17 && j > 0 && k < 17 && k > 0 ){ xf.put(1); - uf.put(-50); + uf.put(50); vf.put(0); wf.put(0); } else { @@ -131,17 +131,17 @@ public class FluidSim { } } else { if( - Math.abs(0 - i) < 4 && - Math.abs(j) < 4 && - Math.abs(0 - k) < 4 && + Math.abs(0 - i) < 5 && + Math.abs(j) < 5 && + Math.abs(0 - k) < 5 && i < 17 && i > 0 && j < 17 && j > 0 && k < 17 && k > 0 ){ - xf.put(1); - uf.put(50); - vf.put(0); - wf.put(rand.nextFloat() * 0.1f); + // xf.put(1); + // uf.put(50); + // vf.put(0); + // wf.put(rand.nextFloat() * 0.1f); } else { xf.put(0); uf.put(0); @@ -175,13 +175,34 @@ public class FluidSim { // //Vector stage solveChunkMask(simArray); + // System.out.println("Prior to add"); + // System.out.println(sumAllU(simArray) + " " + sumAllU0(simArray)); addVectorSources(simArray, timestep); + // System.out.println("after add"); + // System.out.println(sumAllU(simArray) + " " + sumAllU0(simArray)); swapAllVectorFields(simArray, timestep); + // System.out.println("after swap 11"); + // System.out.println(sumAllU(simArray) + " " + sumAllU0(simArray)); solveVectorDiffusion(simArray, timestep); - solveProjection(simArray, timestep); + // System.out.println("after diffuse"); + // System.out.println(sumAllU(simArray) + " " + sumAllU0(simArray)); + solveProjection(simArray, step, timestep); + // System.out.println("after proj 1"); + // System.out.println(sumAllU(simArray) + " " + sumAllU0(simArray)); swapAllVectorFields(simArray, timestep); + // System.out.println("after swap 2"); + // System.out.println(sumAllU(simArray) + " " + sumAllU0(simArray)); advectVectorsAcrossBoundaries(simArray, timestep); - solveProjection(simArray, timestep); + // System.out.println("after advect"); + // System.out.println(sumAllU(simArray) + " " + sumAllU0(simArray)); + solveProjection(simArray, step, timestep); + // System.out.println("after proj 2"); + // System.out.println(sumAllU(simArray) + " " + sumAllU0(simArray)); + // System.out.println("\n\n\n\n\n"); + // if(step == 7){ + // System.out.println(step); + // // System.exit(1); + // } // //Density stage @@ -191,6 +212,19 @@ public class FluidSim { swapAllDensityArrays(simArray, timestep); advectDensity(simArray, timestep); // mirrorNeighborDensities(simArray, 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++){ + // // + // //Reads out the results of the fluid sim + // // + // simArray[x][y][z].readDataIntoArrays(); + // } + // } + // } + // System.out.println("End density stage"); + // System.out.println(sumAllDensity(simArray)); @@ -208,6 +242,130 @@ public class FluidSim { } } + private static double sumAllDensity(FluidSim[][][] simArray){ + double rVal = 0; + 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++){ + rVal = rVal + simArray[x][y][z].sumDensity(); + } + } + } + return rVal; + } + + private double sumDensity(){ + double rVal = 0; + for(int x = 1; x < DIM - 1; x++){ + for(int y = 1; y < DIM - 1; y++){ + for(int z = 1; z < DIM - 1; z++){ + rVal = rVal + densityArrayView[IX(x,y,z)]; + } + } + } + return rVal; + } + + private double sumU(){ + double rVal = 0; + for(int x = 1; x < DIM - 1; x++){ + for(int y = 1; y < DIM - 1; y++){ + for(int z = 1; z < DIM - 1; z++){ + rVal = rVal + Math.abs(uArrayView[IX(x,y,z)]); + } + } + } + return rVal; + } + + private static double sumAllU(FluidSim[][][] simArray){ + double rVal = 0; + 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].readDataIntoArrays(); + rVal = rVal + simArray[x][y][z].sumU(); + } + } + } + return rVal; + } + + private double sumU0(){ + double rVal = 0; + for(int x = 1; x < DIM - 1; x++){ + for(int y = 1; y < DIM - 1; y++){ + for(int z = 1; z < DIM - 1; z++){ + rVal = rVal + Math.abs(u0ArrayView[IX(x,y,z)]); + } + } + } + return rVal; + } + + private static double sumAllU0(FluidSim[][][] simArray){ + double rVal = 0; + 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].readDataIntoArrays(); + rVal = rVal + simArray[x][y][z].sumU0(); + } + } + } + return rVal; + } + + private double sumV(){ + double rVal = 0; + for(int x = 1; x < DIM - 1; x++){ + for(int y = 1; y < DIM - 1; y++){ + for(int z = 1; z < DIM - 1; z++){ + rVal = rVal + Math.abs(vArrayView[IX(x,y,z)]); + } + } + } + return rVal; + } + + private static double sumAllV(FluidSim[][][] simArray){ + double rVal = 0; + 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].readDataIntoArrays(); + rVal = rVal + simArray[x][y][z].sumV(); + } + } + } + return rVal; + } + + private double sumV0(){ + double rVal = 0; + for(int x = 1; x < DIM - 1; x++){ + for(int y = 1; y < DIM - 1; y++){ + for(int z = 1; z < DIM - 1; z++){ + rVal = rVal + Math.abs(v0ArrayView[IX(x,y,z)]); + } + } + } + return rVal; + } + + private static double sumAllV0(FluidSim[][][] simArray){ + double rVal = 0; + 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].readDataIntoArrays(); + rVal = rVal + simArray[x][y][z].sumV0(); + } + } + } + return rVal; + } + private static void solveChunkMask(FluidSim[][][] simArray){ for(int x = 0; x < simArray.length; x++){ for(int y = 0; y < simArray[0].length; y++){ @@ -239,18 +397,18 @@ 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 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++){ @@ -269,29 +427,73 @@ public class FluidSim { simArray[x][y][z].setBoundsToNeighborsWrapper(1, simArray[x][y][z].uVector); simArray[x][y][z].setBoundsToNeighborsWrapper(2, simArray[x][y][z].vVector); simArray[x][y][z].setBoundsToNeighborsWrapper(3, simArray[x][y][z].wVector); + simArray[x][y][z].copyNeighborsWrapper(1, x, simArray[x][y][z].uVector); + simArray[x][y][z].copyNeighborsWrapper(2, x, simArray[x][y][z].vVector); + simArray[x][y][z].copyNeighborsWrapper(3, x, simArray[x][y][z].wVector); } } } } } - private static void solveProjection(FluidSim[][][] simArray, float timestep){ + private static void solveProjection(FluidSim[][][] simArray, int step, 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); + simArray[x][y][z].setBoundsToNeighborsWrapper(1, simArray[x][y][z].uVector); + simArray[x][y][z].setBoundsToNeighborsWrapper(2, simArray[x][y][z].vVector); + simArray[x][y][z].setBoundsToNeighborsWrapper(3, simArray[x][y][z].wVector); + simArray[x][y][z].setBoundsToNeighborsWrapper(1, simArray[x][y][z].uAdditionVector); + simArray[x][y][z].setBoundsToNeighborsWrapper(2, simArray[x][y][z].vAdditionVector); + simArray[x][y][z].copyNeighborsWrapper(1, x, simArray[x][y][z].uVector); + simArray[x][y][z].copyNeighborsWrapper(2, x, simArray[x][y][z].vVector); + simArray[x][y][z].copyNeighborsWrapper(3, x, simArray[x][y][z].wVector); + simArray[x][y][z].copyNeighborsWrapper(1, x, simArray[x][y][z].uAdditionVector); + simArray[x][y][z].copyNeighborsWrapper(2, x, simArray[x][y][z].vAdditionVector); } } } + // System.out.println("after swap in proj"); + // System.out.println(sumAllV(simArray) + " " + sumAllV0(simArray)); + // simArray[0][0][0].copyNeighborsWrapper(2, 0, simArray[0][0][0].vAdditionVector); + // simArray[1][0][0].copyNeighborsWrapper(2, 1, simArray[1][0][0].vAdditionVector); + // simArray[0][0][0].readDataIntoArrays(); + // simArray[1][0][0].readDataIntoArrays(); + // System.out.println("\n\n\n"); + // System.out.println("0 0 0"); + // for(int i = 0; i < 18; i++){ + // for(int j = 0; j < 18; j++){ + // System.out.print(simArray[0][0][0].vArrayView[IX(16,i,j)] + " "); + // } + // System.out.println(); + // } + // System.out.println("\n\n\n"); + // System.out.println("1 0 0"); + // for(int i = 0; i < 18; i++){ + // for(int j = 0; j < 18; j++){ + // System.out.print(simArray[1][0][0].vArrayView[IX(0,i,j)] + " "); + // } + // System.out.println(); + // } + // System.out.println("\n\n\n"); + // for(int i = 0; i < 18; i++){ + // for(int j = 0; j < 18; j++){ + // System.out.print(simArray[1][0][0].vArrayView[IX(0,i,j)] - simArray[0][0][0].vArrayView[IX(16,i,j)] + " "); + // } + // System.out.println(); + // } + // float value = simArray[1][0][0].vArrayView[IX(0,1,1)] - simArray[0][0][0].vArrayView[IX(16,1,1)]; + // System.out.println(simArray[0][0][0].vArrayView[IX(17,1,1)] + " " + simArray[1][0][0].vArrayView[IX(1,1,1)]); + // System.out.println("\n\n\n"); + // if(step == 1){ + // System.exit(1); + // } 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++){ + // System.out.println("Setup " + x + " " + y + " " + z); //setup projection across boundaries //... //set boundaries appropriately @@ -300,14 +502,20 @@ public class FluidSim { } } } + // System.out.println("after setup proj"); + // System.out.println(sumAllV(simArray) + " " + sumAllV0(simArray)); 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].setBoundsToNeighborsWrapper(0, simArray[x][y][z].uAdditionVector); simArray[x][y][z].setBoundsToNeighborsWrapper(0, simArray[x][y][z].vAdditionVector); + simArray[x][y][z].copyNeighborsWrapper(0, x, simArray[x][y][z].uAdditionVector); + simArray[x][y][z].copyNeighborsWrapper(0, x, simArray[x][y][z].vAdditionVector); } } } + // System.out.println("after bound set 1 in proj"); + // System.out.println(sumAllV(simArray) + " " + sumAllV0(simArray)); //samples u0, v0 //sets u0 //these should have just been mirrored in the above @@ -328,11 +536,15 @@ public class FluidSim { for(int y = 0; y < simArray[0].length; y++){ for(int z = 0; z < simArray[0][0].length; z++){ simArray[x][y][z].setBoundsToNeighborsWrapper(0, simArray[x][y][z].uAdditionVector); - // simArray[x][y][z].setBoundsToNeighborsWrapper(0, simArray[x][y][z].vAdditionVector); + simArray[x][y][z].copyNeighborsWrapper(0, x, simArray[x][y][z].uAdditionVector); } } } + // System.out.println("after proj iteration"); + // System.out.println(sumAllV(simArray) + " " + sumAllV0(simArray)); } + // System.out.println("after proj solver"); + // System.out.println(sumAllV(simArray) + " " + sumAllV0(simArray)); //samples u,v,w,u0 //sets u,v,w //Finalize projection @@ -345,6 +557,8 @@ public class FluidSim { } } } + // System.out.println("after finalize proj"); + // System.out.println(sumAllV(simArray) + " " + sumAllV0(simArray)); //set boundaries a final time for u,v,w //... for(int x = 0; x < simArray.length; x++){ @@ -353,9 +567,15 @@ public class FluidSim { simArray[x][y][z].setBoundsToNeighborsWrapper(1, simArray[x][y][z].uVector); simArray[x][y][z].setBoundsToNeighborsWrapper(2, simArray[x][y][z].vVector); simArray[x][y][z].setBoundsToNeighborsWrapper(3, simArray[x][y][z].wVector); - simArray[x][y][z].copyNeighborsWrapper(2, simArray[x][y][z].uAdditionVector); - simArray[x][y][z].copyNeighborsWrapper(2, simArray[x][y][z].vAdditionVector); - simArray[x][y][z].copyNeighborsWrapper(2, simArray[x][y][z].wAdditionVector); + simArray[x][y][z].copyNeighborsWrapper(1, x, simArray[x][y][z].uVector); + simArray[x][y][z].copyNeighborsWrapper(2, x, simArray[x][y][z].vVector); + simArray[x][y][z].copyNeighborsWrapper(3, x, simArray[x][y][z].wVector); + simArray[x][y][z].setBoundsToNeighborsWrapper(1, simArray[x][y][z].uAdditionVector); + simArray[x][y][z].setBoundsToNeighborsWrapper(2, simArray[x][y][z].vAdditionVector); + simArray[x][y][z].setBoundsToNeighborsWrapper(3, simArray[x][y][z].wAdditionVector); + simArray[x][y][z].copyNeighborsWrapper(0, x, simArray[x][y][z].uAdditionVector); + simArray[x][y][z].copyNeighborsWrapper(0, x, simArray[x][y][z].vAdditionVector); + simArray[x][y][z].copyNeighborsWrapper(0, x, simArray[x][y][z].wAdditionVector); } } } @@ -383,11 +603,12 @@ public class FluidSim { 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(1, x, simArray[x][y][z].uVector); + simArray[x][y][z].copyNeighborsWrapper(2, x, simArray[x][y][z].vVector); + simArray[x][y][z].copyNeighborsWrapper(3, x, simArray[x][y][z].wVector); + simArray[x][y][z].copyNeighborsWrapper(1, x, simArray[x][y][z].uAdditionVector); + simArray[x][y][z].copyNeighborsWrapper(2, x, simArray[x][y][z].vAdditionVector); + simArray[x][y][z].copyNeighborsWrapper(2, x, simArray[x][y][z].wAdditionVector); } } } @@ -400,21 +621,43 @@ public class FluidSim { // densityAddition = density[13]; // density[13] = tmp; //swap u0 <-> u - tmp = uAdditionVector[13]; - uAdditionVector[13] = uVector[13]; - uVector[13] = tmp; - //swap v0 <-> v - tmp = vAdditionVector[13]; - vAdditionVector[13] = vVector[13]; - vVector[13] = tmp; - //swap w0 <-> w - tmp = wAdditionVector[13]; - wAdditionVector[13] = wVector[13]; - wVector[13] = tmp; + for(int i = 0; i < 27; i++){ + tmp = uAdditionVector[i]; + uAdditionVector[i] = uVector[i]; + uVector[i] = tmp; + //swap v0 <-> v + tmp = vAdditionVector[i]; + vAdditionVector[i] = vVector[i]; + vVector[i] = tmp; + //swap w0 <-> w + tmp = wAdditionVector[i]; + wAdditionVector[i] = wVector[i]; + wVector[i] = tmp; + } //... } private static void advectVectorsAcrossBoundaries(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].setBoundsToNeighborsWrapper(1, simArray[x][y][z].uVector); + simArray[x][y][z].setBoundsToNeighborsWrapper(2, simArray[x][y][z].vVector); + simArray[x][y][z].setBoundsToNeighborsWrapper(3, simArray[x][y][z].wVector); + simArray[x][y][z].setBoundsToNeighborsWrapper(1, simArray[x][y][z].uAdditionVector); + simArray[x][y][z].setBoundsToNeighborsWrapper(2, simArray[x][y][z].vAdditionVector); + simArray[x][y][z].setBoundsToNeighborsWrapper(3, simArray[x][y][z].wAdditionVector); + simArray[x][y][z].copyNeighborsWrapper(1, x, simArray[x][y][z].uVector); + simArray[x][y][z].copyNeighborsWrapper(2, x, simArray[x][y][z].vVector); + simArray[x][y][z].copyNeighborsWrapper(3, x, simArray[x][y][z].wVector); + simArray[x][y][z].copyNeighborsWrapper(1, x, simArray[x][y][z].uAdditionVector); + simArray[x][y][z].copyNeighborsWrapper(2, x, simArray[x][y][z].vAdditionVector); + simArray[x][y][z].copyNeighborsWrapper(3, x, simArray[x][y][z].wAdditionVector); + } + } + } + // System.out.println("after first bound swap in advect"); + // System.out.println(sumAllU(simArray) + " " + sumAllU0(simArray)); //samples u,v,w,u0,v0,w0 for(int x = 0; x < simArray.length; x++){ for(int y = 0; y < simArray[0].length; y++){ @@ -427,6 +670,8 @@ public class FluidSim { } } } + // System.out.println("before bound swap in advect"); + // System.out.println(sumAllU(simArray) + " " + sumAllU0(simArray)); //mirror neighbor data for(int x = 0; x < simArray.length; x++){ for(int y = 0; y < simArray[0].length; y++){ @@ -434,6 +679,9 @@ public class FluidSim { simArray[x][y][z].setBoundsToNeighborsWrapper(1, simArray[x][y][z].uVector); simArray[x][y][z].setBoundsToNeighborsWrapper(2, simArray[x][y][z].vVector); simArray[x][y][z].setBoundsToNeighborsWrapper(3, simArray[x][y][z].wVector); + simArray[x][y][z].copyNeighborsWrapper(1, x, simArray[x][y][z].uVector); + simArray[x][y][z].copyNeighborsWrapper(2, x, simArray[x][y][z].vVector); + simArray[x][y][z].copyNeighborsWrapper(3, x, simArray[x][y][z].wVector); } } } @@ -464,17 +712,19 @@ public class FluidSim { 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); + simArray[x][y][z].copyNeighborsWrapper(0, x, simArray[x][y][z].density); + simArray[x][y][z].copyNeighborsWrapper(0, x, simArray[x][y][z].densityAddition); } } } } private void swapDensityArrays(){ - ByteBuffer tmp = density[13]; - density[13] = densityAddition[13]; - densityAddition[13] = tmp; + for(int i = 0; i < 27; i++){ + ByteBuffer tmp = density[i]; + density[i] = densityAddition[i]; + densityAddition[i] = tmp; + } } private static void diffuseDensity(FluidSim[][][] simArray, float timestep){ @@ -622,14 +872,6 @@ public class FluidSim { } private native void solveProjection(int DIM_X, int chunkMask, ByteBuffer[] u, ByteBuffer v[], ByteBuffer w[], ByteBuffer u0[], ByteBuffer v0[], ByteBuffer w0[], float DIFFUSION_CONSTANT, float VISCOSITY_CONSTANT, float timestep); - /** - * Solve projection system - */ - // private void setProjectionBordersWrapper(float timestep){ - // setProjectionBorders(DIM, chunkMask, uVector, vVector, wVector, uAdditionVector, vAdditionVector, wAdditionVector, DIFFUSION_CONSTANT, VISCOSITY_CONSTANT, timestep); - // } - // private native void setProjectionBorders(int DIM_X, int chunkMask, ByteBuffer[] u, ByteBuffer v[], ByteBuffer w[], ByteBuffer u0[], ByteBuffer v0[], ByteBuffer w0[], float DIFFUSION_CONSTANT, float VISCOSITY_CONSTANT, float timestep); - /** * Does work like subtracting curl from vector field, setting boundaries, etc */ @@ -683,10 +925,11 @@ public class FluidSim { * 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 void copyNeighborsWrapper(int vectorDir, int x, ByteBuffer[] neighborMap){ + copyNeighbors(DIM, chunkMask, x, vectorDir, neighborMap); } - private native void copyNeighbors(int DIM_X, int chunkMask, int vectorDir, ByteBuffer[] neighborMap); + private native void copyNeighbors(int DIM_X, int chunkMask, int x, int vectorDir, ByteBuffer[] neighborMap); + @@ -759,10 +1002,22 @@ public class FluidSim { if(wVector[13].position() > 0){ wVector[13].position(0); } + if(uAdditionVector[13].position() > 0){ + uAdditionVector[13].position(0); + } + if(vAdditionVector[13].position() > 0){ + vAdditionVector[13].position(0); + } + if(wAdditionVector[13].position() > 0){ + wAdditionVector[13].position(0); + } FloatBuffer xFloatView = density[13].asFloatBuffer(); FloatBuffer uFloatView = uVector[13].asFloatBuffer(); FloatBuffer vFloatView = vVector[13].asFloatBuffer(); FloatBuffer wFloatView = wVector[13].asFloatBuffer(); + FloatBuffer u0FloatView = uAdditionVector[13].asFloatBuffer(); + FloatBuffer v0FloatView = vAdditionVector[13].asFloatBuffer(); + FloatBuffer w0FloatView = wAdditionVector[13].asFloatBuffer(); for(int i = 0; i < DIM; i++){ for(int j = 0; j < DIM; j++){ for(int k = 0; k < DIM; k++){ @@ -770,6 +1025,9 @@ public class FluidSim { uArrayView[IX(i,j,k)] = uFloatView.get(); vArrayView[IX(i,j,k)] = vFloatView.get(); wArrayView[IX(i,j,k)] = wFloatView.get(); + u0ArrayView[IX(i,j,k)] = u0FloatView.get(); + v0ArrayView[IX(i,j,k)] = v0FloatView.get(); + w0ArrayView[IX(i,j,k)] = w0FloatView.get(); } } } @@ -814,7 +1072,9 @@ public class FluidSim { for(int i = 0; i < DIM; i++){ for(int j = 0; j < DIM; j++){ for(int k = 0; k < DIM; k++){ + u0ArrayView[IX(i,j,k)] = 0; v0ArrayView[IX(i,j,k)] = densityArrayView[IX(i,j,k)] * GRAVITY; + w0ArrayView[IX(i,j,k)] = 0; } } } diff --git a/src/main/java/electrosphere/Main.java b/src/main/java/electrosphere/Main.java index 7a80154..ec418fb 100644 --- a/src/main/java/electrosphere/Main.java +++ b/src/main/java/electrosphere/Main.java @@ -22,7 +22,7 @@ public class Main { public static void main(String args[]){ - int dim = 2; + int dim = 5; int i = 0; long time = 0; long lastTime = 0; @@ -36,9 +36,9 @@ public class Main { Mesh.initShaderProgram(); - FluidSim[][][] simArray = initFluidSim(dim,dim,dim); + FluidSim[][][] simArray = initFluidSim(dim,1,dim); - Mesh[][][] meshArray = initMeshes(dim,dim,dim,simArray); + Mesh[][][] meshArray = initMeshes(dim,1,dim,simArray); @@ -55,7 +55,7 @@ public class Main { // //Simulate // - FluidSim.simChunks(simArray,i,0.001f); + FluidSim.simChunks(simArray,i,0.01f); time = time + (System.currentTimeMillis() - lastTime); // //Remesh