Compare commits

...

61 Commits

Author SHA1 Message Date
austin
7cb7ef646c updates
All checks were successful
studiorailgun/fluid-sim/pipeline/head This commit looks good
2024-11-30 13:26:55 -05:00
unknown
db89bc4ff4 Merge branch 'master' of git.austinwhoover.com:studiorailgun/fluid-sim
All checks were successful
studiorailgun/fluid-sim/pipeline/head This commit looks good
2024-03-15 21:18:55 -04:00
unknown
74db769ab6 define fix 2024-03-15 21:18:35 -04:00
fe93aac7e5 Merge pull request 'jenkinsAutoTesting' (#1) from jenkinsAutoTesting into master
Reviewed-on: https://git.austinwhoover.com/studiorailgun/fluid-sim/pulls/1
2024-03-15 21:15:03 -04:00
unknown
3b8363507e disable testing
All checks were successful
studiorailgun/fluid-sim/pipeline/pr-master This commit looks good
studiorailgun/fluid-sim/pipeline/head This commit looks good
2024-03-15 21:13:33 -04:00
unknown
0eb314999d test fix
Some checks failed
studiorailgun/fluid-sim/pipeline/head There was a failure building this commit
2024-03-15 21:11:47 -04:00
unknown
fed89963a6 remove compile flags to potentially build lib
Some checks failed
studiorailgun/fluid-sim/pipeline/head There was a failure building this commit
2024-03-15 21:00:17 -04:00
unknown
13bda0cbf3 OS dependent library loading
Some checks failed
studiorailgun/fluid-sim/pipeline/head There was a failure building this commit
2024-03-15 20:51:46 -04:00
unknown
40b570a0b1 gitmodules potential fix
Some checks failed
studiorailgun/fluid-sim/pipeline/head There was a failure building this commit
2024-03-15 20:47:25 -04:00
unknown
7d2569f3dc Potential fix for compiling
Some checks failed
studiorailgun/fluid-sim/pipeline/head There was a failure building this commit
2024-03-15 20:32:27 -04:00
unknown
64848eecc7 enable testing
Some checks failed
studiorailgun/fluid-sim/pipeline/head There was a failure building this commit
2024-03-15 20:25:24 -04:00
unknown
c883f762d5 reproducible testing framework 2024-03-15 20:22:57 -04:00
unknown
f7ffbe3016 cleanup macro
All checks were successful
studiorailgun/fluid-sim/pipeline/head This commit looks good
2024-03-15 18:17:34 -04:00
unknown
63e3d3a878 static inline core functions
All checks were successful
studiorailgun/fluid-sim/pipeline/head This commit looks good
2024-03-15 18:12:47 -04:00
unknown
a69fc20cfb rework compilation
All checks were successful
studiorailgun/fluid-sim/pipeline/head This commit looks good
2024-03-15 17:56:56 -04:00
unknown
bea9ef3927 Move source files 2024-03-15 17:54:56 -04:00
unknown
ca8ce41c22 remove jni from c-exclusive files
All checks were successful
studiorailgun/fluid-sim/pipeline/head This commit looks good
2024-03-15 17:46:13 -04:00
unknown
3601e68b83 Separate java interactions to separate c file 2024-03-15 17:44:18 -04:00
unknown
c1d9a7a054 basic cleanup
All checks were successful
studiorailgun/fluid-sim/pipeline/head This commit looks good
2024-03-15 17:22:21 -04:00
unknown
69a803fb3f refactor add density func
All checks were successful
studiorailgun/fluid-sim/pipeline/head This commit looks good
2024-03-15 17:12:07 -04:00
unknown
7a1da2d29c stable-ish multichunk solver
All checks were successful
studiorailgun/fluid-sim/pipeline/head This commit looks good
2024-03-15 16:56:09 -04:00
unknown
d4d5d2c94e debug work in shaders 2024-03-10 20:48:51 -04:00
unknown
3e591442c9 remove jni assignments
All checks were successful
studiorailgun/fluid-sim/pipeline/head This commit looks good
2024-03-10 20:34:11 -04:00
unknown
2bd92fb719 add vscode project config
All checks were successful
studiorailgun/fluid-sim/pipeline/head This commit looks good
2024-03-10 19:19:35 -04:00
unknown
858859043b Merge branch 'master' into migration-successful 2024-03-10 19:16:57 -04:00
unknown
6a8a461298 optimization work
All checks were successful
studiorailgun/fluid-sim/pipeline/head This commit looks good
2024-03-10 19:14:50 -04:00
unknown
1230ac51cb gcc optimization
All checks were successful
studiorailgun/fluid-sim/pipeline/head This commit looks good
2024-03-10 19:05:24 -04:00
unknown
6f052e48e6 remove JNI from core routines
All checks were successful
studiorailgun/fluid-sim/pipeline/head This commit looks good
2024-03-10 18:23:54 -04:00
unknown
eac79e0afc swapping arrays is raw arrays only
All checks were successful
studiorailgun/fluid-sim/pipeline/head This commit looks good
2024-03-10 18:11:32 -04:00
unknown
f93936a96e neighbor operations use raw arrays 2024-03-10 18:09:42 -04:00
unknown
79fa7715f0 swap all remaining to raw arrays
All checks were successful
studiorailgun/fluid-sim/pipeline/head This commit looks good
2024-03-10 18:04:55 -04:00
unknown
10c3987f2d main project swapped to raw arrays 2024-03-10 17:56:49 -04:00
unknown
18ac90affa swap vector fields
All checks were successful
studiorailgun/fluid-sim/pipeline/head This commit looks good
2024-03-10 17:50:25 -04:00
unknown
52e853f8ad swap adding forces 2024-03-10 17:48:37 -04:00
unknown
6afbbd7e2a clean up density calls
All checks were successful
studiorailgun/fluid-sim/pipeline/head This commit looks good
2024-03-10 17:45:51 -04:00
unknown
6b585b367b swapped all density functions to raw arrays 2024-03-10 17:43:48 -04:00
unknown
085cd18fdf swap diffusion solver to raw arrays 2024-03-10 17:39:21 -04:00
unknown
fb81cc2982 properly swapping direct arrays
All checks were successful
studiorailgun/fluid-sim/pipeline/head This commit looks good
2024-03-10 17:35:18 -04:00
unknown
70ab3b53e9 swap neighbors raw 2024-03-10 17:31:31 -04:00
unknown
d699a16e08 density using direct access 2024-03-10 17:28:55 -04:00
unknown
12a5352ae9 rename symbols, fix bug, total allocation working
All checks were successful
studiorailgun/fluid-sim/pipeline/head This commit looks good
2024-03-10 17:23:51 -04:00
unknown
1f39a03e06 fully parse out list of chunks
All checks were successful
studiorailgun/fluid-sim/pipeline/head This commit looks good
2024-03-10 17:11:34 -04:00
unknown
1389b47ce1 handle swapping density correctly 2024-03-10 17:10:46 -04:00
unknown
880cd1b675 start converting density phase 2024-03-10 17:09:43 -04:00
unknown
6867733a0f migrate velocity phase 2024-03-10 17:08:50 -04:00
unknown
2c70036f07 first projection correct 2024-03-10 17:07:35 -04:00
unknown
f1d3c3d011 swapping arrays correctly
All checks were successful
studiorailgun/fluid-sim/pipeline/head This commit looks good
2024-03-10 17:04:36 -04:00
unknown
67f12b7eb4 Fix bug in assignment in main funcs 2024-03-10 17:01:39 -04:00
unknown
a004e7e2c3 chunk referencing 2024-03-10 16:58:32 -04:00
unknown
a41a848c04 cleanup java side somewhat
All checks were successful
studiorailgun/fluid-sim/pipeline/head This commit looks good
2024-03-10 15:54:07 -04:00
unknown
9021125a74 All function calls on C side through wrappers 2024-03-10 15:47:43 -04:00
unknown
12dfd9e6b4 work to setup migration to c 2024-03-04 21:23:36 -05:00
unknown
62373d266c density not crashing, but broken
All checks were successful
studiorailgun/fluid-sim/pipeline/head This commit looks good
2024-03-02 21:51:47 -05:00
unknown
e955c1f479 queueing logic without memleak allegedly
All checks were successful
studiorailgun/fluid-sim/pipeline/head This commit looks good
2024-03-02 18:19:22 -05:00
unknown
0a15bf062c proper thread batching
All checks were successful
studiorailgun/fluid-sim/pipeline/head This commit looks good
2024-03-01 19:17:27 -05:00
unknown
2dc54a8186 Create threadpool in c land
All checks were successful
studiorailgun/fluid-sim/pipeline/head This commit looks good
2024-02-29 22:22:50 -05:00
unknown
1e16151860 properly init stb with jenkins
All checks were successful
studiorailgun/fluid-sim/pipeline/head This commit looks good
2024-02-29 21:20:59 -05:00
unknown
0794c88990 Jenkinsfile update
All checks were successful
studiorailgun/fluid-sim/pipeline/head This commit looks good
2024-02-29 21:13:36 -05:00
unknown
eae31e2ec3 update jenkinsfile
All checks were successful
studiorailgun/fluid-sim/pipeline/head This commit looks good
2024-02-29 20:59:32 -05:00
unknown
08c95a376a add jenkinsfile 2024-02-29 20:59:07 -05:00
unknown
4bbc4883b6 reorg + add stb dependency 2024-02-29 20:54:47 -05:00
273 changed files with 2427 additions and 1155 deletions

3
.gitignore vendored
View File

@ -5,7 +5,6 @@
/.settings
/.classpath
/.project
/.vscode
/shared-folder
/shared-folder/**
/src/main/c/lib/**
/chunks

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "src/main/c/lib/stb"]
path = src/main/c/lib/stb
url = https://github.com/nothings/stb.git

22
.vscode/c_cpp_properties.json vendored Normal file
View File

@ -0,0 +1,22 @@
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"C:/Program Files/Eclipse Adoptium/jdk-21.0.5.11-hotspot/include/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.19041.0",
"compilerPath": "C:/ProgramData/mingw64/mingw64/bin/gcc.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "windows-gcc-x64"
}
],
"version": 4
}

6
.vscode/extensions.json vendored Normal file
View File

@ -0,0 +1,6 @@
{
"recommendations": [
"ms-vscode.cpptools-extension-pack",
"vscjava.vscode-java-pack"
]
}

20
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,20 @@
{
"configurations": [
{
"type": "java",
"name": "Launch Java Program",
"request": "launch",
"mainClass": "electrosphere.Main",
"preLaunchTask": "compileNoSteps",
"vmArgs": "-Djava.library.path=./shared-folder"
},
{
"type": "java",
"name": "Launch Java Program (SAVE CHUNKS TO DISK)",
"request": "launch",
"mainClass": "electrosphere.Main",
"preLaunchTask": "compileWithSteps",
"vmArgs": "-Djava.library.path=./shared-folder"
}
]
}

21
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,21 @@
{
"java.configuration.updateBuildConfiguration": "automatic",
"files.associations": {
"stdio.h": "c",
"jni.h": "c",
"immintrin.h": "c",
"stdint.h": "c",
"utilities.h": "c",
"chunkmask.h": "c",
"pthread.h": "c",
"semaphore.h": "c",
"libfluidsim.h": "c",
"stb_ds.h": "c",
"threadpool.h": "c",
"electrosphere_fluidsim.h": "c",
"type_traits": "c",
"mainfunctions.h": "c",
"chunk.h": "c",
"simulation.h": "c"
}
}

15
.vscode/tasks.json vendored Normal file
View File

@ -0,0 +1,15 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "compileWithSteps",
"type": "shell",
"command": "SAVE_STEPS=1 ./src/main/c/compile.sh",
},
{
"label": "compileNoSteps",
"type": "shell",
"command": "SAVE_STEPS=0 ./src/main/c/compile.sh",
}
]
}

11
README.md Normal file
View File

@ -0,0 +1,11 @@
# Fluid Sim
## How to run
Clone with `git clone --recurse-submodules git@git.austinwhoover.com:studiorailgun/fluid-sim.git`
Then run with VSCode debug plugin

9
compare.sh Normal file
View File

@ -0,0 +1,9 @@
search_dir=./chunks
for entry in "$search_dir"/*
do
if ! diff "/c/Users/satellite/Documents/fluid-sim/$entry" "/c/Users/satellite/Documents/fluid-sim-the-golden-code/$entry" > /dev/null;
then
echo "$entry differs"
fi
done
echo "done!"

View File

@ -73,6 +73,9 @@
</goals>
<configuration>
<executable>bash</executable>
<environmentVariables>
<SAVE_STEPS>0</SAVE_STEPS>
</environmentVariables>
<arguments>
<argument>./src/main/c/compile.sh</argument>
</arguments>
@ -80,6 +83,10 @@
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
</plugin>
</plugins>
</build>
<profiles>
@ -103,6 +110,56 @@
</properties>
</profile>
</profiles>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.10.2</version>
<scope>test</scope>
<exclusions>
<exclusion>
<artifactId>junit-platform-engine</artifactId>
<groupId>org.junit.platform</groupId>
</exclusion>
<exclusion>
<artifactId>junit-jupiter-api</artifactId>
<groupId>org.junit.jupiter</groupId>
</exclusion>
<exclusion>
<artifactId>apiguardian-api</artifactId>
<groupId>org.apiguardian</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>1.10.2</version>
<scope>test</scope>
<exclusions>
<exclusion>
<artifactId>junit</artifactId>
<groupId>junit</groupId>
</exclusion>
<exclusion>
<artifactId>junit-platform-launcher</artifactId>
<groupId>org.junit.platform</groupId>
</exclusion>
<exclusion>
<artifactId>junit-platform-suite-api</artifactId>
<groupId>org.junit.platform</groupId>
</exclusion>
<exclusion>
<artifactId>junit-platform-suite-commons</artifactId>
<groupId>org.junit.platform</groupId>
</exclusion>
<exclusion>
<artifactId>apiguardian-api</artifactId>
<groupId>org.apiguardian</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>

26
optimizationideas.md Normal file
View File

@ -0,0 +1,26 @@
'sleep' chunks if there are no massive velocity swings for ~100 frames
- unsleep as soon as new density or velocity is added
VVV These two may not work because the projection phase currently iterates over all cells with avx instructions
it also may generally not work as fast moving water basically works by having empty cells query area that already has water (so maybe no sleeping air)
'sleep' individual cells that are full of water and fully surrounded by water
'sleep' terrain cells
'sleep' "inactive" cells
- When reading data back into java, while iterating over each cell check if they've changed value
- If not, add 1 to an accumulating array alongside the other ones (d, u, etc)
- In C, if the accumulating array is >100 or some threshold, skip the cell
- If a force gets added java side that is greater than some threshold, set all accumulating array to 0
multigrid method for projection code
intelligent neighbor awakening --
If a neighbor is asleep (say a neighbor full of air above this cell),
do not simulate that neighbor at all
Instead poll the currently awake cell for large velocity spikes in the direction of the neighbor
If there is a large spike, awake the neighbor cell
if a neighbor is awoken, the simulator should go back through and simulate for that chunk as well (idk if this is possible)
use avx bitmasking for handling terrain in projection code ???

22
pom.xml
View File

@ -64,6 +64,7 @@
<dependencies>
<!--lwjgl-->
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl</artifactId>
@ -109,6 +110,19 @@
<artifactId>lwjgl-opengl</artifactId>
<classifier>${lwjgl.natives}</classifier>
</dependency>
<!--junit-->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.10.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>1.10.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
@ -181,6 +195,9 @@
</goals>
<configuration>
<executable>bash</executable>
<environmentVariables>
<SAVE_STEPS>0</SAVE_STEPS>
</environmentVariables>
<arguments>
<argument>./src/main/c/compile.sh</argument>
</arguments>
@ -188,6 +205,11 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
</plugin>
</plugins>
</build>

View File

@ -1,102 +0,0 @@
#include <jni.h>
#include <stdint.h>
#include "includes/utilities.h"
#include "includes/chunkmask.h"
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){
return matrix_transform(env,jrx);
}
/**
* Calculates a mask that represents all nearby chunks that are actually accessible and exist
*/
uint32_t matrix_transform(JNIEnv * env, jobjectArray jrx){
//The returned value, an availability mask that contains the availability of each neighbor chunk
uint32_t rVal = 0;
//Add to maks for initial chunks
for(int i = 0; i < CENTER_LOC; i++){
if((*env)->GetObjectArrayElement(env,jrx,i)!=NULL){
rVal = rVal + 1;
}
rVal = rVal << 1;
}
//add 1 for center chunk because we already have that
rVal = rVal + 1;
rVal = rVal << 1;
//continue on for remaining chunks
for(int i = CENTER_LOC+1; i < 27; i++){
if((*env)->GetObjectArrayElement(env,jrx,i)!=NULL){
rVal = rVal + 1;
}
if(i < 26){
rVal = rVal << 1;
}
}
return rVal;
}
const uint32_t CHUNK_INDEX_ARR[] = {
CHUNK_000, CHUNK_100, CHUNK_200,
CHUNK_010, CHUNK_110, CHUNK_210,
CHUNK_020, CHUNK_120, CHUNK_220,
CHUNK_001, CHUNK_101, CHUNK_201,
CHUNK_011, CHUNK_111, CHUNK_211,
CHUNK_021, CHUNK_121, CHUNK_221,
CHUNK_002, CHUNK_102, CHUNK_202,
CHUNK_012, CHUNK_112, CHUNK_212,
CHUNK_022, CHUNK_122, CHUNK_222,
};
//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,
};
const char CHUNK_NORMALIZE_V[] = {
1, 1, 1,
0, 0, 0,
-1, -1, -1,
1, 1, 1,
0, 0, 0,
-1, -1, -1,
1, 1, 1,
0, 0, 0,
-1, -1, -1,
};
const char CHUNK_NORMALIZE_W[] = {
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,
};

View File

@ -1,5 +1,7 @@
cd ./src/main/c
echo "SAVE STEPS $SAVE_STEPS"
LIB_ENDING=".so"
BASE_INCLUDE_DIR=""
OS_INCLUDE_DIR=""
@ -41,19 +43,29 @@ rm -f ./*.dll
#compile object files
COMPILE_FLAGS="-c -fPIC -m64 -mavx -mavx2 -O1"
INPUT_FILES="./densitystep.c"
OUTPUT_FILE="./densitystep.o"
# COMPILE_FLAGS="-c -fPIC -m64 -mavx -mavx2 -march=native -Ofast -msse -msse2 -msse3 -mmmx -m3dnow"
# INPUT_FILES="./src/densitystep.c"
# OUTPUT_FILE="./densitystep.o"
# gcc $COMPILE_FLAGS -I"$BASE_INCLUDE_DIR" -I"$OS_INCLUDE_DIR" $INPUT_FILES -o $OUTPUT_FILE
# COMPILE_FLAGS="-c -fPIC -m64 -mavx -mavx2 -march=native -Ofast -msse -msse2 -msse3 -mmmx -m3dnow"
# INPUT_FILES="./src/velocitystep.c"
# OUTPUT_FILE="./velocitystep.o"
# gcc $COMPILE_FLAGS -I"$BASE_INCLUDE_DIR" -I"$OS_INCLUDE_DIR" $INPUT_FILES -o $OUTPUT_FILE
# COMPILE_FLAGS="-c -fPIC -m64 -mavx -mavx2 -march=native -Ofast -msse -msse2 -msse3 -mmmx -m3dnow"
# INPUT_FILES="./src/chunkmask.c"
# OUTPUT_FILE="./chunkmask.o"
# gcc $COMPILE_FLAGS -I"$BASE_INCLUDE_DIR" -I"$OS_INCLUDE_DIR" $INPUT_FILES -o $OUTPUT_FILE
COMPILE_FLAGS="-c -fPIC -m64 -mavx -mavx2 -march=native -Ofast -DSAVE_STEPS=$SAVE_STEPS"
INPUT_FILES="./src/javainterface.c"
OUTPUT_FILE="./javainterface.o"
gcc $COMPILE_FLAGS -I"$BASE_INCLUDE_DIR" -I"$OS_INCLUDE_DIR" $INPUT_FILES -o $OUTPUT_FILE
COMPILE_FLAGS="-c -fPIC -m64 -mavx -mavx2 -O1"
INPUT_FILES="./velocitystep.c"
OUTPUT_FILE="./velocitystep.o"
gcc $COMPILE_FLAGS -I"$BASE_INCLUDE_DIR" -I"$OS_INCLUDE_DIR" $INPUT_FILES -o $OUTPUT_FILE
COMPILE_FLAGS="-c -fPIC -m64 -mavx -mavx2 -O1"
INPUT_FILES="./chunkmask.c"
OUTPUT_FILE="./chunkmask.o"
COMPILE_FLAGS="-c -fPIC -m64 -mavx -mavx2 -march=native -Ofast -DSAVE_STEPS=$SAVE_STEPS"
INPUT_FILES="./src/fluidsim.c"
OUTPUT_FILE="./fluidsim.o"
gcc $COMPILE_FLAGS -I"$BASE_INCLUDE_DIR" -I"$OS_INCLUDE_DIR" $INPUT_FILES -o $OUTPUT_FILE
@ -61,7 +73,7 @@ gcc $COMPILE_FLAGS -I"$BASE_INCLUDE_DIR" -I"$OS_INCLUDE_DIR" $INPUT_FILES -o $OU
#compile shared object file
OUTPUT_FILE="libfluidsim$LIB_ENDING"
COMPILE_FLAGS="-shared"
INPUT_FILES="densitystep.o velocitystep.o chunkmask.o"
INPUT_FILES="fluidsim.o javainterface.o"
gcc $COMPILE_FLAGS $INPUT_FILES -o $OUTPUT_FILE
#move to resources

View File

@ -0,0 +1,19 @@
#ifndef CHUNK_H
#define CHUNK_H
/**
* A chunk
*/
typedef struct {
float * d[27];
float * d0[27];
float * u[27];
float * v[27];
float * w[27];
float * u0[27];
float * v0[27];
float * w0[27];
int chunkMask;
} Chunk;
#endif

View File

@ -10,116 +10,20 @@ extern "C" {
#undef electrosphere_FluidSim_DIM
#define electrosphere_FluidSim_DIM 18L
#undef electrosphere_FluidSim_DIFFUSION_CONSTANT
#define electrosphere_FluidSim_DIFFUSION_CONSTANT 0.0f
#define electrosphere_FluidSim_DIFFUSION_CONSTANT 1.0E-5f
#undef electrosphere_FluidSim_VISCOSITY_CONSTANT
#define electrosphere_FluidSim_VISCOSITY_CONSTANT 0.0f
#define electrosphere_FluidSim_VISCOSITY_CONSTANT 1.0E-5f
#undef electrosphere_FluidSim_LINEARSOLVERTIMES
#define electrosphere_FluidSim_LINEARSOLVERTIMES 20L
#undef electrosphere_FluidSim_GRAVITY
#define electrosphere_FluidSim_GRAVITY -100.0f
#define electrosphere_FluidSim_GRAVITY -1000.0f
/*
* Class: electrosphere_FluidSim
* Method: simulate
* Signature: (II[Ljava/nio/ByteBuffer;Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;FFF)V
* Signature: (Ljava/util/List;F)V
*/
JNIEXPORT void JNICALL Java_electrosphere_FluidSim_simulate
(JNIEnv *, jobject, jint, jint, jobjectArray, jobject, jobjectArray, jobjectArray, jobjectArray, jobjectArray, jobjectArray, jobjectArray, jfloat, jfloat, jfloat);
/*
* Class: electrosphere_FluidSim
* Method: calculateChunkMask
* Signature: ([Ljava/nio/ByteBuffer;)I
*/
JNIEXPORT jint JNICALL Java_electrosphere_FluidSim_calculateChunkMask
(JNIEnv *, jobject, jobjectArray);
/*
* Class: electrosphere_FluidSim
* Method: addSourceToVectors
* Signature: (II[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;FFF)V
*/
JNIEXPORT void JNICALL Java_electrosphere_FluidSim_addSourceToVectors
(JNIEnv *, jobject, jint, jint, jobjectArray, jobjectArray, jobjectArray, jobjectArray, jobjectArray, jobjectArray, jfloat, jfloat, jfloat);
/*
* Class: electrosphere_FluidSim
* Method: solveVectorDiffuse
* Signature: (II[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;FFF)V
*/
JNIEXPORT void JNICALL Java_electrosphere_FluidSim_solveVectorDiffuse
(JNIEnv *, jobject, jint, jint, jobjectArray, jobjectArray, jobjectArray, jobjectArray, jobjectArray, jobjectArray, jfloat, jfloat, jfloat);
/*
* Class: electrosphere_FluidSim
* Method: setupProjection
* Signature: (II[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;FFF)V
*/
JNIEXPORT void JNICALL Java_electrosphere_FluidSim_setupProjection
(JNIEnv *, jobject, jint, jint, jobjectArray, jobjectArray, jobjectArray, jobjectArray, jobjectArray, jobjectArray, jfloat, jfloat, jfloat);
/*
* Class: electrosphere_FluidSim
* Method: solveProjection
* Signature: (II[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;FFF)V
*/
JNIEXPORT void JNICALL Java_electrosphere_FluidSim_solveProjection
(JNIEnv *, jobject, jint, jint, jobjectArray, jobjectArray, jobjectArray, jobjectArray, jobjectArray, jobjectArray, jfloat, jfloat, jfloat);
/*
* Class: electrosphere_FluidSim
* Method: finalizeProjection
* Signature: (II[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;FFF)V
*/
JNIEXPORT void JNICALL Java_electrosphere_FluidSim_finalizeProjection
(JNIEnv *, jobject, jint, jint, jobjectArray, jobjectArray, jobjectArray, jobjectArray, jobjectArray, jobjectArray, jfloat, jfloat, jfloat);
/*
* Class: electrosphere_FluidSim
* Method: advectVectors
* Signature: (II[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;FFF)V
*/
JNIEXPORT void JNICALL Java_electrosphere_FluidSim_advectVectors
(JNIEnv *, jobject, jint, jint, jobjectArray, jobjectArray, jobjectArray, jobjectArray, jobjectArray, jobjectArray, jfloat, jfloat, jfloat);
/*
* Class: electrosphere_FluidSim
* Method: addDensity
* Signature: (II[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;F)V
*/
JNIEXPORT void JNICALL Java_electrosphere_FluidSim_addDensity
(JNIEnv *, jobject, jint, jint, jobjectArray, jobjectArray, jfloat);
/*
* Class: electrosphere_FluidSim
* Method: solveDiffuseDensity
* Signature: (II[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;FFF)V
*/
JNIEXPORT void JNICALL Java_electrosphere_FluidSim_solveDiffuseDensity
(JNIEnv *, jobject, jint, jint, jobjectArray, jobjectArray, jobjectArray, jobjectArray, jobjectArray, jfloat, jfloat, jfloat);
/*
* Class: electrosphere_FluidSim
* Method: advectDensity
* Signature: (II[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;FFF)V
*/
JNIEXPORT void JNICALL Java_electrosphere_FluidSim_advectDensity
(JNIEnv *, jobject, jint, jint, jobjectArray, jobjectArray, jobjectArray, jobjectArray, jobjectArray, jfloat, jfloat, jfloat);
/*
* Class: electrosphere_FluidSim
* Method: setBoundsToNeighbors
* Signature: (III[Ljava/nio/ByteBuffer;)V
*/
JNIEXPORT void JNICALL Java_electrosphere_FluidSim_setBoundsToNeighbors
(JNIEnv *, jobject, jint, jint, jint, jobjectArray);
/*
* Class: electrosphere_FluidSim
* Method: copyNeighbors
* Signature: (IIII[Ljava/nio/ByteBuffer;)V
*/
JNIEXPORT void JNICALL Java_electrosphere_FluidSim_copyNeighbors
(JNIEnv *, jobject, jint, jint, jint, jint, jobjectArray);
(JNIEnv *, jclass, jobject, jfloat);
#ifdef __cplusplus
}

View File

@ -0,0 +1,101 @@
#ifndef MAINFUNC
#define MAINFUNC
/*
* Class: electrosphere_FluidSim
* Method: addSourceToVectors
* Signature: (II[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;FFF)V
*/
static inline void addSourceToVectors
(int, int, float **, float **, float **, float **, float **, float **, float, float, float);
/*
* Class: electrosphere_FluidSim
* Method: solveVectorDiffuse
* Signature: (II[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;FFF)V
*/
static inline void solveVectorDiffuse
(int, int, float **, float **, float **, float **, float **, float **, float, float, float);
/*
* Class: electrosphere_FluidSim
* Method: setupProjection
* Signature: (II[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;FFF)V
*/
static inline void setupProjection
(
int N,
int chunk_mask,
float ** ur,
float ** vr,
float ** wr,
float ** pr,
float ** divr,
float DIFFUSION_CONST,
float VISCOSITY_CONST,
float dt);
/*
* Class: electrosphere_FluidSim
* Method: solveProjection
* Signature: (II[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;FFF)V
*/
static inline void solveProjection
(int, int, float **, float **, float **, float **, float **, float **, float, float, float);
/*
* Class: electrosphere_FluidSim
* Method: finalizeProjection
* Signature: (II[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;FFF)V
*/
static inline void finalizeProjection
(int, int, float **, float **, float **, float **, float **, float **, float, float, float);
/*
* Class: electrosphere_FluidSim
* Method: advectVectors
* Signature: (II[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;FFF)V
*/
static inline void advectVectors
(int, int, float **, float **, float **, float **, float **, float **, float, float, float);
/*
* Class: electrosphere_FluidSim
* Method: addDensity
* Signature: (II[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;F)V
*/
static inline void addDensity
(int, int, float **, float **, float);
/*
* Class: electrosphere_FluidSim
* Method: solveDiffuseDensity
* Signature: (II[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;FFF)V
*/
static inline void solveDiffuseDensity
(int, int, float **, float **, float **, float **, float **, float, float, float);
/*
* Class: electrosphere_FluidSim
* Method: advectDensity
* Signature: (II[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;FFF)V
*/
static inline void advectDensity(uint32_t chunk_mask, int N, float ** d, float ** d0, float ** ur, float ** vr, float ** wr, float dt);
static inline void setBoundsToNeighborsRaw
(
int N,
int chunk_mask,
int vector_dir,
float ** neighborArray);
static inline void copyNeighborsRaw
(
int N,
int chunk_mask,
int cx,
int vector_dir,
float ** neighborArray);
#endif

View File

@ -0,0 +1,8 @@
#ifndef SIMULATION_H
#define SIMULATION_H
#include "./chunk.h"
void simulate(int numChunks, Chunk ** passedInChunks, float timestep);
#endif

View File

@ -1,4 +1,3 @@
#include <jni.h>
#include <stdint.h>
#ifndef UTILITIES_H
@ -7,7 +6,7 @@
#define SWAP(x0,x) {float *tmp=x0;x0=x;x=tmp;}
#define IX(i,j,k) ((i)+(N)*(j)+(N*N)*(k))
#define CK(m,n,o) ((m)+(n)*(3)+(o)*(3)*(3))
#define GET_ARR(env,src,i) (*env)->GetDirectBufferAddress(env,(*env)->GetObjectArrayElement(env,src,i))
#define GET_ARR_RAW(src,i) src[i]
#define ARR_EXISTS(chunk_mask,m,n,o) (chunk_mask & CHUNK_INDEX_ARR[CK(m,n,o)]) > 0
#endif

1
src/main/c/lib/stb Submodule

@ -0,0 +1 @@
Subproject commit ae721c50eaf761660b4f90cc590453cdb0c2acd0

View File

@ -0,0 +1,61 @@
#include <stdint.h>
#include "../includes/utilities.h"
#include "../includes/chunkmask.h"
const uint32_t CHUNK_INDEX_ARR[] = {
CHUNK_000, CHUNK_100, CHUNK_200,
CHUNK_010, CHUNK_110, CHUNK_210,
CHUNK_020, CHUNK_120, CHUNK_220,
CHUNK_001, CHUNK_101, CHUNK_201,
CHUNK_011, CHUNK_111, CHUNK_211,
CHUNK_021, CHUNK_121, CHUNK_221,
CHUNK_002, CHUNK_102, CHUNK_202,
CHUNK_012, CHUNK_112, CHUNK_212,
CHUNK_022, CHUNK_122, CHUNK_222,
};
//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,
};
const char CHUNK_NORMALIZE_V[] = {
1, 1, 1,
0, 0, 0,
-1, -1, -1,
1, 1, 1,
0, 0, 0,
-1, -1, -1,
1, 1, 1,
0, 0, 0,
-1, -1, -1,
};
const char CHUNK_NORMALIZE_W[] = {
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,
};

View File

@ -1,58 +1,50 @@
#include <jni.h>
#include <stdio.h>
#include <immintrin.h>
#include <stdint.h>
#include "includes/utilities.h"
#include "includes/chunkmask.h"
#include "../includes/utilities.h"
#include "../includes/chunkmask.h"
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
* Method: addDensity
* Signature: (II[Ljava/nio/ByteBuffer;Ljava/nio/ByteBuffer;F)V
*/
JNIEXPORT void JNICALL Java_electrosphere_FluidSim_addDensity
(JNIEnv * env,
jobject this,
jint N,
jint chunk_mask,
jobjectArray jrx,
jobjectArray x0,
jfloat dt){
/**
* Adds density to the density array
*/
static inline void addDensity(
int N,
int chunk_mask,
float ** d,
float ** d0,
float dt
){
int i;
int size=N*N*N;
float * x = GET_ARR(env,jrx,CENTER_LOC);
float * s = GET_ARR(env,x0,CENTER_LOC);
float * x = GET_ARR_RAW(d,CENTER_LOC);
float * s = GET_ARR_RAW(d0,CENTER_LOC);
for(i=0; i<size; i++){
x[i] += dt*s[i];
}
}
/*
* 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
*/
JNIEXPORT void JNICALL Java_electrosphere_FluidSim_solveDiffuseDensity
(JNIEnv * env,
jobject this,
jint N,
jint chunk_mask,
jobjectArray jrx,
jobjectArray jrx0,
jobjectArray jru,
jobjectArray jrv,
jobjectArray jrw,
jfloat DIFFUSION_CONST,
jfloat VISCOSITY_CONST,
jfloat dt){
static inline void solveDiffuseDensity(
int N,
int chunk_mask,
float ** d,
float ** d0,
float ** jru,
float ** jrv,
float ** jrw,
float DIFFUSION_CONST,
float VISCOSITY_CONST,
float dt
){
float a=dt*DIFFUSION_CONST*N*N*N;
float c=1+6*a;
int i, j, k, l, m;
float * x = GET_ARR(env,jrx,CENTER_LOC);
float * x0 = GET_ARR(env,jrx0,CENTER_LOC);
float * x = GET_ARR_RAW(d,CENTER_LOC);
float * x0 = GET_ARR_RAW(d0,CENTER_LOC);
__m256 aScalar = _mm256_set1_ps(a);
__m256 cScalar = _mm256_set1_ps(c);
@ -84,42 +76,27 @@ JNIEXPORT void JNICALL Java_electrosphere_FluidSim_solveDiffuseDensity
}
}
/*
* Class: electrosphere_FluidSim
* Method: advectDensity
* Signature: (II[Ljava/nio/ByteBuffer;Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;[Ljava/nio/ByteBuffer;FFF)V
*/
JNIEXPORT void JNICALL Java_electrosphere_FluidSim_advectDensity
(JNIEnv * env,
jobject this,
jint N,
jint chunk_mask,
jobjectArray jrx,
jobjectArray jrx0,
jobjectArray jru,
jobjectArray jrv,
jobjectArray jrw,
jfloat DIFFUSION_CONST,
jfloat VISCOSITY_CONST,
jfloat 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, jobjectArray jrd0, float * u, float * v, float * w, float dt){
/**
* Advects the density based on the vectors
*/
static inline 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;
float x, y, z, s0, t0, s1, t1, u1, u0, dtx,dty,dtz;
dtx=dty=dtz=dt*N;
float * d = GET_ARR(env,jrd,CENTER_LOC);
float * center_d = GET_ARR_RAW(d,CENTER_LOC);
float * center_d0 = GET_ARR_RAW(d0,CENTER_LOC);
float * d0 = GET_ARR(env,jrd0,CENTER_LOC);
float * u = GET_ARR_RAW(ur,CENTER_LOC);
float * v = GET_ARR_RAW(vr,CENTER_LOC);
float * w = GET_ARR_RAW(wr,CENTER_LOC);
for(k=1; k<N-1; k++){
for(j=1; j<N-1; j++){
for(i=1; i<N-1; i++){
d0 = GET_ARR(env,jrd0,CENTER_LOC);
center_d0 = GET_ARR_RAW(d0,CENTER_LOC);
//calculate location to pull from
x = i-dtx*u[IX(i,j,k)];
y = j-dty*v[IX(i,j,k)];
@ -234,18 +211,18 @@ void advectDensity(JNIEnv * env, uint32_t chunk_mask, int N, int b, jobjectArray
// if(k1 < 0){
// k1 = 0;
// }
d[IX(i,j,k)] =
center_d[IX(i,j,k)] =
s0*(
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)]
t0*u0*center_d0[IX(i0,j0,k0)]+
t1*u0*center_d0[IX(i0,j1,k0)]+
t0*u1*center_d0[IX(i0,j0,k1)]+
t1*u1*center_d0[IX(i0,j1,k1)]
)+
s1*(
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)]
t0*u0*center_d0[IX(i1,j0,k0)]+
t1*u0*center_d0[IX(i1,j1,k0)]+
t0*u1*center_d0[IX(i1,j0,k1)]+
t1*u1*center_d0[IX(i1,j1,k1)]
);
}
}

542
src/main/c/src/fluidsim.c Normal file
View File

@ -0,0 +1,542 @@
#include <stdint.h>
#include "../includes/utilities.h"
#include "../includes/chunkmask.h"
#include "../includes/electrosphere_FluidSim.h"
#include "../includes/mainFunctions.h"
#include "../includes/chunk.h"
#include "../includes/simulation.h"
#include "./chunkmask.c"
#include "./velocitystep.c"
#include "./densitystep.c"
#ifndef SAVE_STEPS
#define SAVE_STEPS 0
#endif
#define DIM 18
#define LINEARSOLVERTIMES 20
#define REALLY_SMALL_VALUE 0.00001
#define DIFFUSION_CONSTANT 0.00001
#define VISCOSITY_CONSTANT 0.00001
char fileNameBuff[50];
//all chunks
Chunk ** chunks = NULL;
//jni help:
//https://stackoverflow.com/questions/39823375/clarification-about-getfieldid
static inline void saveStep(float * values, const char * name);
void simulate(
int numChunks,
Chunk ** passedInChunks,
jfloat timestep
){
chunks = passedInChunks;
// printf("%p\n",chunks[0].d);
saveStep(chunks[0]->u[CENTER_LOC], "./chunks/beginU");
saveStep(chunks[0]->v[CENTER_LOC], "./chunks/beginV");
saveStep(chunks[0]->w[CENTER_LOC], "./chunks/beginW");
saveStep(chunks[0]->u0[CENTER_LOC], "./chunks/beginU0");
saveStep(chunks[0]->v0[CENTER_LOC], "./chunks/beginV0");
saveStep(chunks[0]->w0[CENTER_LOC], "./chunks/beginW0");
//solve chunk mask
for(int i = 0; i < numChunks; i++){
Chunk * currentChunk = chunks[i];
addSourceToVectors(
DIM,
currentChunk->chunkMask,
currentChunk->u,
currentChunk->v,
currentChunk->w,
currentChunk->u0,
currentChunk->v0,
currentChunk->w0,
DIFFUSION_CONSTANT,
VISCOSITY_CONSTANT,
timestep
);
saveStep(currentChunk->u[CENTER_LOC], "./chunks/addSrcU");
saveStep(currentChunk->v[CENTER_LOC], "./chunks/addSrcV");
saveStep(currentChunk->w[CENTER_LOC], "./chunks/addSrcW");
saveStep(currentChunk->u0[CENTER_LOC], "./chunks/addSrcU0");
saveStep(currentChunk->v0[CENTER_LOC], "./chunks/addSrcV0");
saveStep(currentChunk->w0[CENTER_LOC], "./chunks/addSrcW0");
}
//swap all vector fields
{
//swap vector fields
for(int i = 0; i < numChunks; i++){
Chunk * currentChunk = chunks[i];
float * tmpArr;
for(int j = 0; j < 27; j++){
tmpArr = currentChunk->u[j];
currentChunk->u[j] = currentChunk->u0[j];
currentChunk->u0[j] = tmpArr;
}
for(int j = 0; j < 27; j++){
tmpArr = currentChunk->v[j];
currentChunk->v[j] = currentChunk->v0[j];
currentChunk->v0[j] = tmpArr;
}
for(int j = 0; j < 27; j++){
tmpArr = currentChunk->w[j];
currentChunk->w[j] = currentChunk->w0[j];
currentChunk->w0[j] = tmpArr;
}
}
//copy neighbors
for(int i = 0; i < numChunks; i++){
Chunk * currentChunk = chunks[i];
copyNeighborsRaw(DIM,currentChunk->chunkMask,0,1,currentChunk->u);
copyNeighborsRaw(DIM,currentChunk->chunkMask,0,2,currentChunk->v);
copyNeighborsRaw(DIM,currentChunk->chunkMask,0,3,currentChunk->w);
copyNeighborsRaw(DIM,currentChunk->chunkMask,0,1,currentChunk->u0);
copyNeighborsRaw(DIM,currentChunk->chunkMask,0,2,currentChunk->v0);
copyNeighborsRaw(DIM,currentChunk->chunkMask,0,3,currentChunk->w0);
}
}
saveStep(chunks[0]->u[CENTER_LOC], "./chunks/swapU");
saveStep(chunks[0]->v[CENTER_LOC], "./chunks/swapV");
saveStep(chunks[0]->w[CENTER_LOC], "./chunks/swapW");
saveStep(chunks[0]->u0[CENTER_LOC], "./chunks/swapU0");
saveStep(chunks[0]->v0[CENTER_LOC], "./chunks/swapV0");
saveStep(chunks[0]->w0[CENTER_LOC], "./chunks/swapW0");
// printf("after swap vecs u\n");
// printLayer(chunks[0]->u[CENTER_LOC],targetLayer);
// printf("after swap vecs u0\n");
// printLayer(chunks[0]->u0[CENTER_LOC],targetLayer);
//solve vector diffusion
{
for(int l = 0; l < LINEARSOLVERTIMES; l++){
//solve vector diffusion
for(int i = 0; i < numChunks; i++){
Chunk * currentChunk = chunks[i];
solveVectorDiffuse(DIM,currentChunk->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);
saveStep(chunks[0]->u[CENTER_LOC], fileNameBuff);
sprintf(fileNameBuff, "./chunks/diffuseUStep%dx0", l);
saveStep(chunks[0]->u0[CENTER_LOC], fileNameBuff);
sprintf(fileNameBuff, "./chunks/diffuseVStep%dx", l);
saveStep(chunks[0]->v[CENTER_LOC], fileNameBuff);
sprintf(fileNameBuff, "./chunks/diffuseVStep%dx0", l);
saveStep(chunks[0]->v0[CENTER_LOC], fileNameBuff);
sprintf(fileNameBuff, "./chunks/diffuseWStep%dx", l);
saveStep(chunks[0]->w[CENTER_LOC], fileNameBuff);
sprintf(fileNameBuff, "./chunks/diffuseWStep%dx0", l);
saveStep(chunks[0]->w0[CENTER_LOC], fileNameBuff);
}
//update array for vectors
for(int i = 0; i < numChunks; i++){
Chunk * currentChunk = chunks[i];
setBoundsToNeighborsRaw(DIM,currentChunk->chunkMask,1,currentChunk->u);
setBoundsToNeighborsRaw(DIM,currentChunk->chunkMask,2,currentChunk->v);
setBoundsToNeighborsRaw(DIM,currentChunk->chunkMask,3,currentChunk->w);
// setBoundsToNeighborsRaw(DIM,chunkMask,1,currentChunk->u0);
// setBoundsToNeighborsRaw(DIM,chunkMask,2,currentChunk->v0);
// setBoundsToNeighborsRaw(DIM,chunkMask,3,currentChunk->w0);
copyNeighborsRaw(DIM,currentChunk->chunkMask,0,0,currentChunk->u);
copyNeighborsRaw(DIM,currentChunk->chunkMask,0,0,currentChunk->v);
copyNeighborsRaw(DIM,currentChunk->chunkMask,0,0,currentChunk->w);
copyNeighborsRaw(DIM,currentChunk->chunkMask,0,0,currentChunk->u0);
copyNeighborsRaw(DIM,currentChunk->chunkMask,0,0,currentChunk->v0);
copyNeighborsRaw(DIM,currentChunk->chunkMask,0,0,currentChunk->w0);
}
if(SAVE_STEPS){
sprintf(fileNameBuff, "./chunks/diffuseUStep%dxBnd", l);
saveStep(chunks[0]->u[CENTER_LOC], fileNameBuff);
sprintf(fileNameBuff, "./chunks/diffuseUStep%dx0Bnd", l);
saveStep(chunks[0]->u0[CENTER_LOC], fileNameBuff);
sprintf(fileNameBuff, "./chunks/diffuseVStep%dxBnd", l);
saveStep(chunks[0]->v[CENTER_LOC], fileNameBuff);
sprintf(fileNameBuff, "./chunks/diffuseVStep%dx0Bnd", l);
saveStep(chunks[0]->v0[CENTER_LOC], fileNameBuff);
sprintf(fileNameBuff, "./chunks/diffuseWStep%dxBnd", l);
saveStep(chunks[0]->w[CENTER_LOC], fileNameBuff);
sprintf(fileNameBuff, "./chunks/diffuseWStep%dx0Bnd", l);
saveStep(chunks[0]->w0[CENTER_LOC], fileNameBuff);
}
}
}
saveStep(chunks[0]->u[CENTER_LOC], "./chunks/diffuseU");
saveStep(chunks[0]->v[CENTER_LOC], "./chunks/diffuseV");
saveStep(chunks[0]->w[CENTER_LOC], "./chunks/diffuseW");
saveStep(chunks[0]->u0[CENTER_LOC], "./chunks/diffuseU0");
saveStep(chunks[0]->v0[CENTER_LOC], "./chunks/diffuseV0");
saveStep(chunks[0]->w0[CENTER_LOC], "./chunks/diffuseW0");
//solve projection
{
//update array for vectors
for(int i = 0; i < numChunks; i++){
Chunk * currentChunk = chunks[i];
// setBoundsToNeighborsRaw(DIM,chunkMask,1,currentChunk->u);
// setBoundsToNeighborsRaw(DIM,chunkMask,2,currentChunk->v);
// setBoundsToNeighborsRaw(DIM,chunkMask,3,currentChunk->w);
// setBoundsToNeighborsRaw(DIM,chunkMask,1,currentChunk->u0);
// setBoundsToNeighborsRaw(DIM,chunkMask,2,currentChunk->v0);
copyNeighborsRaw(DIM,currentChunk->chunkMask,0,1,currentChunk->u);
copyNeighborsRaw(DIM,currentChunk->chunkMask,0,2,currentChunk->v);
copyNeighborsRaw(DIM,currentChunk->chunkMask,0,3,currentChunk->w);
copyNeighborsRaw(DIM,currentChunk->chunkMask,0,1,currentChunk->u0);
copyNeighborsRaw(DIM,currentChunk->chunkMask,0,2,currentChunk->v0);
}
//setup projection
for(int i = 0; i < numChunks; i++){
Chunk * currentChunk = chunks[i];
setupProjection(DIM,currentChunk->chunkMask,currentChunk->u,currentChunk->v,currentChunk->w,currentChunk->u0,currentChunk->v0,DIFFUSION_CONSTANT,VISCOSITY_CONSTANT,timestep);
}
saveStep(chunks[0]->v0[CENTER_LOC], "./chunks/setupProj1Div");
saveStep(chunks[0]->u0[CENTER_LOC], "./chunks/setupProj1P");
//update array for vectors
for(int i = 0; i < numChunks; i++){
Chunk * currentChunk = chunks[i];
setBoundsToNeighborsRaw(DIM,currentChunk->chunkMask,0,currentChunk->u0);
setBoundsToNeighborsRaw(DIM,currentChunk->chunkMask,0,currentChunk->v0);
copyNeighborsRaw(DIM,currentChunk->chunkMask,0,0,currentChunk->u0);
copyNeighborsRaw(DIM,currentChunk->chunkMask,0,0,currentChunk->v0);
}
saveStep(chunks[0]->v0[CENTER_LOC], "./chunks/setupProj1DivBnd");
saveStep(chunks[0]->u0[CENTER_LOC], "./chunks/setupProj1PBnd");
//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++){
for(int i = 0; i < numChunks; i++){
Chunk * currentChunk = chunks[i];
solveProjection(DIM,currentChunk->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);
saveStep(chunks[0]->u0[CENTER_LOC], fileNameBuff);
sprintf(fileNameBuff, "./chunks/proj1Step%dx0", l);
saveStep(chunks[0]->v0[CENTER_LOC], fileNameBuff);
}
for(int i = 0; i < numChunks; i++){
Chunk * currentChunk = chunks[i];
setBoundsToNeighborsRaw(DIM,currentChunk->chunkMask,0,currentChunk->u0);
copyNeighborsRaw(DIM,currentChunk->chunkMask,0,0,currentChunk->u0);
}
if(SAVE_STEPS){
sprintf(fileNameBuff, "./chunks/proj1Step%dxBnd", l);
saveStep(chunks[0]->u0[CENTER_LOC], fileNameBuff);
sprintf(fileNameBuff, "./chunks/proj1Step%dx0Bnd", l);
saveStep(chunks[0]->v0[CENTER_LOC], fileNameBuff);
}
}
//samples u,v,w,u0
//sets u,v,w
//Finalize projection
for(int i = 0; i < numChunks; i++){
Chunk * currentChunk = chunks[i];
finalizeProjection(DIM,currentChunk->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");
saveStep(chunks[0]->v[CENTER_LOC], "./chunks/finalizeProj1V");
saveStep(chunks[0]->w[CENTER_LOC], "./chunks/finalizeProj1W");
// exit(0);
//set boundaries a final time for u,v,w
//...
for(int i = 0; i < numChunks; i++){
Chunk * currentChunk = chunks[i];
setBoundsToNeighborsRaw(DIM,currentChunk->chunkMask,1,currentChunk->u);
setBoundsToNeighborsRaw(DIM,currentChunk->chunkMask,2,currentChunk->v);
setBoundsToNeighborsRaw(DIM,currentChunk->chunkMask,3,currentChunk->w);
setBoundsToNeighborsRaw(DIM,currentChunk->chunkMask,1,currentChunk->u0);
setBoundsToNeighborsRaw(DIM,currentChunk->chunkMask,2,currentChunk->v0);
setBoundsToNeighborsRaw(DIM,currentChunk->chunkMask,3,currentChunk->w0);
copyNeighborsRaw(DIM,currentChunk->chunkMask,0,1,currentChunk->u);
copyNeighborsRaw(DIM,currentChunk->chunkMask,0,2,currentChunk->v);
copyNeighborsRaw(DIM,currentChunk->chunkMask,0,3,currentChunk->w);
copyNeighborsRaw(DIM,currentChunk->chunkMask,0,1,currentChunk->u0);
copyNeighborsRaw(DIM,currentChunk->chunkMask,0,2,currentChunk->v0);
copyNeighborsRaw(DIM,currentChunk->chunkMask,0,3,currentChunk->w0);
}
}
saveStep(chunks[0]->u[CENTER_LOC], "./chunks/projU");
saveStep(chunks[0]->v[CENTER_LOC], "./chunks/projV");
saveStep(chunks[0]->w[CENTER_LOC], "./chunks/projW");
saveStep(chunks[0]->u0[CENTER_LOC], "./chunks/projU0");
saveStep(chunks[0]->v0[CENTER_LOC], "./chunks/projV0");
saveStep(chunks[0]->w0[CENTER_LOC], "./chunks/projW0");
// exit(0);
//swap all vector fields
{
//swap vector fields
for(int i = 0; i < numChunks; i++){
Chunk * currentChunk = chunks[i];
float * tmpArr;
for(int j = 0; j < 27; j++){
tmpArr = currentChunk->u[j];
currentChunk->u[j] = currentChunk->u0[j];
currentChunk->u0[j] = tmpArr;
}
for(int j = 0; j < 27; j++){
tmpArr = currentChunk->v[j];
currentChunk->v[j] = currentChunk->v0[j];
currentChunk->v0[j] = tmpArr;
}
for(int j = 0; j < 27; j++){
tmpArr = currentChunk->w[j];
currentChunk->w[j] = currentChunk->w0[j];
currentChunk->w0[j] = tmpArr;
}
}
//copy neighbors
for(int i = 0; i < numChunks; i++){
Chunk * currentChunk = chunks[i];
copyNeighborsRaw(DIM,currentChunk->chunkMask,0,1,currentChunk->u);
copyNeighborsRaw(DIM,currentChunk->chunkMask,0,2,currentChunk->v);
copyNeighborsRaw(DIM,currentChunk->chunkMask,0,3,currentChunk->w);
copyNeighborsRaw(DIM,currentChunk->chunkMask,0,1,currentChunk->u0);
copyNeighborsRaw(DIM,currentChunk->chunkMask,0,2,currentChunk->v0);
copyNeighborsRaw(DIM,currentChunk->chunkMask,0,3,currentChunk->w0);
}
}
saveStep(chunks[0]->u[CENTER_LOC], "./chunks/swap2U");
saveStep(chunks[0]->v[CENTER_LOC], "./chunks/swap2V");
saveStep(chunks[0]->w[CENTER_LOC], "./chunks/swap2W");
saveStep(chunks[0]->u0[CENTER_LOC], "./chunks/swap2U0");
saveStep(chunks[0]->v0[CENTER_LOC], "./chunks/swap2V0");
saveStep(chunks[0]->w0[CENTER_LOC], "./chunks/swap2W0");
//advect vectors across boundaries
{
//update border arrs
for(int i = 0; i < numChunks; i++){
Chunk * currentChunk = chunks[i];
setBoundsToNeighborsRaw(DIM,currentChunk->chunkMask,1,currentChunk->u);
setBoundsToNeighborsRaw(DIM,currentChunk->chunkMask,2,currentChunk->v);
setBoundsToNeighborsRaw(DIM,currentChunk->chunkMask,3,currentChunk->w);
setBoundsToNeighborsRaw(DIM,currentChunk->chunkMask,1,currentChunk->u0);
setBoundsToNeighborsRaw(DIM,currentChunk->chunkMask,2,currentChunk->v0);
setBoundsToNeighborsRaw(DIM,currentChunk->chunkMask,3,currentChunk->w0);
copyNeighborsRaw(DIM,currentChunk->chunkMask,0,1,currentChunk->u);
copyNeighborsRaw(DIM,currentChunk->chunkMask,0,2,currentChunk->v);
copyNeighborsRaw(DIM,currentChunk->chunkMask,0,3,currentChunk->w);
copyNeighborsRaw(DIM,currentChunk->chunkMask,0,1,currentChunk->u0);
copyNeighborsRaw(DIM,currentChunk->chunkMask,0,2,currentChunk->v0);
copyNeighborsRaw(DIM,currentChunk->chunkMask,0,3,currentChunk->w0);
}
//advect
for(int i = 0; i < numChunks; i++){
Chunk * currentChunk = chunks[i];
advectVectors(DIM,currentChunk->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++){
Chunk * currentChunk = chunks[i];
setBoundsToNeighborsRaw(DIM,currentChunk->chunkMask,1,currentChunk->u);
setBoundsToNeighborsRaw(DIM,currentChunk->chunkMask,2,currentChunk->v);
setBoundsToNeighborsRaw(DIM,currentChunk->chunkMask,3,currentChunk->w);
copyNeighborsRaw(DIM,currentChunk->chunkMask,0,1,currentChunk->u);
copyNeighborsRaw(DIM,currentChunk->chunkMask,0,2,currentChunk->v);
copyNeighborsRaw(DIM,currentChunk->chunkMask,0,3,currentChunk->w);
}
}
saveStep(chunks[0]->u[CENTER_LOC], "./chunks/advectU");
saveStep(chunks[0]->v[CENTER_LOC], "./chunks/advectV");
saveStep(chunks[0]->w[CENTER_LOC], "./chunks/advectW");
saveStep(chunks[0]->u0[CENTER_LOC], "./chunks/advectU0");
saveStep(chunks[0]->v0[CENTER_LOC], "./chunks/advectV0");
saveStep(chunks[0]->w0[CENTER_LOC], "./chunks/advectW0");
//solve projection
{
//update array for vectors
for(int i = 0; i < numChunks; i++){
Chunk * currentChunk = chunks[i];
setBoundsToNeighborsRaw(DIM,currentChunk->chunkMask,1,currentChunk->u);
setBoundsToNeighborsRaw(DIM,currentChunk->chunkMask,2,currentChunk->v);
setBoundsToNeighborsRaw(DIM,currentChunk->chunkMask,3,currentChunk->w);
setBoundsToNeighborsRaw(DIM,currentChunk->chunkMask,1,currentChunk->u0);
setBoundsToNeighborsRaw(DIM,currentChunk->chunkMask,2,currentChunk->v0);
copyNeighborsRaw(DIM,currentChunk->chunkMask,0,1,currentChunk->u);
copyNeighborsRaw(DIM,currentChunk->chunkMask,0,2,currentChunk->v);
copyNeighborsRaw(DIM,currentChunk->chunkMask,0,3,currentChunk->w);
copyNeighborsRaw(DIM,currentChunk->chunkMask,0,1,currentChunk->u0);
copyNeighborsRaw(DIM,currentChunk->chunkMask,0,2,currentChunk->v0);
}
//setup projection
for(int i = 0; i < numChunks; i++){
Chunk * currentChunk = chunks[i];
setupProjection(DIM,currentChunk->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++){
Chunk * currentChunk = chunks[i];
setBoundsToNeighborsRaw(DIM,currentChunk->chunkMask,1,currentChunk->u0);
setBoundsToNeighborsRaw(DIM,currentChunk->chunkMask,2,currentChunk->v0);
copyNeighborsRaw(DIM,currentChunk->chunkMask,0,1,currentChunk->u0);
copyNeighborsRaw(DIM,currentChunk->chunkMask,0,2,currentChunk->v0);
}
//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++){
for(int i = 0; i < numChunks; i++){
Chunk * currentChunk = chunks[i];
solveProjection(DIM,currentChunk->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];
setBoundsToNeighborsRaw(DIM,currentChunk->chunkMask,0,currentChunk->u0);
copyNeighborsRaw(DIM,currentChunk->chunkMask,0,0,currentChunk->u0);
}
}
//samples u,v,w,u0
//sets u,v,w
//Finalize projection
for(int i = 0; i < numChunks; i++){
Chunk * currentChunk = chunks[i];
finalizeProjection(DIM,currentChunk->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
//...
for(int i = 0; i < numChunks; i++){
Chunk * currentChunk = chunks[i];
setBoundsToNeighborsRaw(DIM,currentChunk->chunkMask,1,currentChunk->u);
setBoundsToNeighborsRaw(DIM,currentChunk->chunkMask,2,currentChunk->v);
setBoundsToNeighborsRaw(DIM,currentChunk->chunkMask,3,currentChunk->w);
setBoundsToNeighborsRaw(DIM,currentChunk->chunkMask,1,currentChunk->u0);
setBoundsToNeighborsRaw(DIM,currentChunk->chunkMask,2,currentChunk->v0);
setBoundsToNeighborsRaw(DIM,currentChunk->chunkMask,3,currentChunk->w0);
copyNeighborsRaw(DIM,currentChunk->chunkMask,0,1,currentChunk->u);
copyNeighborsRaw(DIM,currentChunk->chunkMask,0,2,currentChunk->v);
copyNeighborsRaw(DIM,currentChunk->chunkMask,0,3,currentChunk->w);
copyNeighborsRaw(DIM,currentChunk->chunkMask,0,1,currentChunk->u0);
copyNeighborsRaw(DIM,currentChunk->chunkMask,0,2,currentChunk->v0);
copyNeighborsRaw(DIM,currentChunk->chunkMask,0,3,currentChunk->w0);
}
}
///------------------------------------------------------------------------------------------------------------------------------------------------------------------------
///------------------------------------------------------------------------------------------------------------------------------------------------------------------------
///------------------------------------------------------------------------------------------------------------------------------------------------------------------------
///------------------------------------------------------------------------------------------------------------------------------------------------------------------------
///------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//add density
{
for(int i = 0; i < numChunks; i++){
Chunk * currentChunk = chunks[i];
addDensity(DIM,currentChunk->chunkMask,currentChunk->d,currentChunk->d0,timestep);
}
}
//swap all density arrays
{
//swap vector fields
for(int i = 0; i < numChunks; i++){
Chunk * currentChunk = chunks[i];
float * tmpArr;
for(int j = 0; j < 27; j++){
tmpArr = currentChunk->d[j];
currentChunk->d[j] = currentChunk->d0[j];
currentChunk->d0[j] = tmpArr;
}
}
for(int i = 0; i < numChunks; i++){
Chunk * currentChunk = chunks[i];
copyNeighborsRaw(DIM,currentChunk->chunkMask,0,0,currentChunk->d);
copyNeighborsRaw(DIM,currentChunk->chunkMask,0,0,currentChunk->d0);
}
}
//diffuse density
{
for(int l = 0; l < LINEARSOLVERTIMES; l++){
for(int i = 0; i < numChunks; i++){
Chunk * currentChunk = chunks[i];
solveDiffuseDensity(DIM,currentChunk->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];
setBoundsToNeighborsRaw(DIM,currentChunk->chunkMask,0,currentChunk->d);
}
}
}
//swap all density arrays
{
//swap vector fields
for(int i = 0; i < numChunks; i++){
Chunk * currentChunk = chunks[i];
float * tmpArr;
for(int j = 0; j < 27; j++){
tmpArr = currentChunk->d[j];
currentChunk->d[j] = currentChunk->d0[j];
currentChunk->d0[j] = tmpArr;
}
}
for(int i = 0; i < numChunks; i++){
Chunk * currentChunk = chunks[i];
copyNeighborsRaw(DIM,currentChunk->chunkMask,0,0,currentChunk->d);
copyNeighborsRaw(DIM,currentChunk->chunkMask,0,0,currentChunk->d0);
}
}
//advect density
{
for(int i = 0; i < numChunks; i++){
Chunk * currentChunk = chunks[i];
advectDensity(currentChunk->chunkMask,DIM,currentChunk->d,currentChunk->d0,currentChunk->u,currentChunk->v,currentChunk->w,timestep);
}
}
//mirror densities
{
for(int i = 0; i < numChunks; i++){
Chunk * currentChunk = chunks[i];
setBoundsToNeighborsRaw(DIM,currentChunk->chunkMask,0,currentChunk->d);
}
}
}
static inline void saveStep(float * values, const char * name){
if(SAVE_STEPS){
FILE *fp;
int N = DIM;
// ... fill the array somehow ...
fp = fopen(name, "w");
// check for error here
for(int x = 0; x < DIM; x++){
for(int y = 0; y < DIM; y++){
for(int z = 0; z < DIM; z++){
float val = values[IX(x,y,z)];
if(val < REALLY_SMALL_VALUE && val > -REALLY_SMALL_VALUE){
val = 0;
}
fprintf(fp, "%f\t", val);
}
fprintf(fp, "\n");
}
fprintf(fp, "\n");
}
fclose(fp);
}
}

View File

@ -0,0 +1,170 @@
#include <jni.h>
//library includes
//include stb ds
#define STB_DS_IMPLEMENTATION
#include "../lib/stb/stb_ds.h"
//local includes
#include "../includes/chunk.h"
#include "../includes/chunkmask.h"
#include "../includes/utilities.h"
#include "../includes/simulation.h"
//defines
//function defines
#define getChunk(i) (*env)->CallObjectMethod(env,chunkList,jListGet,i)
#define getBuffArr(buffId) (*env)->GetObjectField(env,chunkJRaw,buffId)
#define setBuffArr(buffId,value) (*env)->SetObjectField(env,chunkJRaw,buffId,value)
#define GET_ARR(env,src,i) (*env)->GetDirectBufferAddress(env,(*env)->GetObjectArrayElement(env,src,i))
//declarations
void readInChunks(JNIEnv * env, jobject chunkList);
int calculateChunkMask(JNIEnv * env, jobjectArray jrx);
uint32_t matrix_transform(JNIEnv * env, jobjectArray jrx);
//the list of chunks
Chunk ** javaChunkView = NULL;
//the number of chunks
int numChunks = 0;
JNIEXPORT void JNICALL Java_electrosphere_FluidSim_simulate(
JNIEnv * env,
jclass fluidSimClass,
jobject chunkList,
jfloat dt
){
readInChunks(env,chunkList);
simulate(numChunks,javaChunkView,dt);
}
/**
* Reads chunks into the dynamic array
*/
void readInChunks(JNIEnv * env, jobject chunkList){
jclass listClass = (*env)->FindClass(env,"java/util/List");
jclass fluidSimClass = (*env)->FindClass(env,"electrosphere/FluidSim");
//JNIEnv *env, jclass clazz, const char *name, const char *sig
jmethodID jListSize = (*env)->GetMethodID(env, listClass, "size", "()I");
jmethodID jListGet = (*env)->GetMethodID(env, listClass, "get", "(I)Ljava/lang/Object;");
jmethodID jListAdd = (*env)->GetMethodID(env, listClass, "add", "(Ljava/lang/Object;)Z");
//ByteBuffer[]
jfieldID dJId = (*env)->GetFieldID(env,fluidSimClass,"density","[Ljava/nio/ByteBuffer;");
jfieldID d0JId = (*env)->GetFieldID(env,fluidSimClass,"densityAddition","[Ljava/nio/ByteBuffer;");
jfieldID uJId = (*env)->GetFieldID(env,fluidSimClass,"uVector","[Ljava/nio/ByteBuffer;");
jfieldID vJId = (*env)->GetFieldID(env,fluidSimClass,"vVector","[Ljava/nio/ByteBuffer;");
jfieldID wJId = (*env)->GetFieldID(env,fluidSimClass,"wVector","[Ljava/nio/ByteBuffer;");
jfieldID u0JId = (*env)->GetFieldID(env,fluidSimClass,"uAdditionVector","[Ljava/nio/ByteBuffer;");
jfieldID v0JId = (*env)->GetFieldID(env,fluidSimClass,"vAdditionVector","[Ljava/nio/ByteBuffer;");
jfieldID w0JId = (*env)->GetFieldID(env,fluidSimClass,"wAdditionVector","[Ljava/nio/ByteBuffer;");
jfieldID chunkmaskJId = (*env)->GetFieldID(env,fluidSimClass,"chunkMask","I");
//the number of chunks
numChunks = (*env)->CallIntMethod(env,chunkList,jListSize);
//current chunk (this)
jobject chunkJRaw;
//current chunk fields
jobjectArray jd;
jobjectArray jd0;
jobjectArray u;
jobjectArray v;
jobjectArray w;
jobjectArray u0;
jobjectArray v0;
jobjectArray w0;
int chunkMask;
//solve chunk mask
for(int i = 0; i < numChunks; i++){
chunkJRaw = getChunk(i);
chunkMask = calculateChunkMask(env,getBuffArr(dJId));
(*env)->SetIntField(env,chunkJRaw,chunkmaskJId,chunkMask);
Chunk * newChunk;
if(i >= stbds_arrlen(javaChunkView)){
// printf("allocate chunk %d\n",i);
// fflush(stdout);
newChunk = (Chunk *)malloc(sizeof(Chunk));
// printf("new chunk %p\n",newChunk);
// fflush(stdout);
stbds_arrput(javaChunkView,newChunk);
// printf("new chunk %p\n",chunks[i]);
// fflush(stdout);
} else {
newChunk = javaChunkView[i];
// printf("get chunk %d: %p\n",i,newChunk);
// fflush(stdout);
}
jd = (*env)->GetObjectField(env,chunkJRaw,dJId);
jd0 = (*env)->GetObjectField(env,chunkJRaw,d0JId);
u = (*env)->GetObjectField(env,chunkJRaw,uJId);
v = (*env)->GetObjectField(env,chunkJRaw,vJId);
w = (*env)->GetObjectField(env,chunkJRaw,wJId);
u0 = (*env)->GetObjectField(env,chunkJRaw,u0JId);
v0 = (*env)->GetObjectField(env,chunkJRaw,v0JId);
w0 = (*env)->GetObjectField(env,chunkJRaw,w0JId);
newChunk->chunkMask = chunkMask;
for(int j = 0; j < 27; j++){
if((chunkMask & CHUNK_INDEX_ARR[j]) > 0){
newChunk->d[j] = GET_ARR(env,jd,j);
newChunk->d0[j] = GET_ARR(env,jd0,j);
newChunk->u[j] = GET_ARR(env,u,j);
newChunk->v[j] = GET_ARR(env,v,j);
newChunk->w[j] = GET_ARR(env,w,j);
newChunk->u0[j] = GET_ARR(env,u0,j);
newChunk->v0[j] = GET_ARR(env,v0,j);
newChunk->w0[j] = GET_ARR(env,w0,j);
}
}
}
}
/**
* Calculates the bitmask for available chunks for the provided chunk's neighbor array
*/
int calculateChunkMask(JNIEnv * env, jobjectArray jrx){
return matrix_transform(env,jrx);
}
/**
* Calculates a mask that represents all nearby chunks that are actually accessible and exist
*/
uint32_t matrix_transform(JNIEnv * env, jobjectArray jrx){
//The returned value, an availability mask that contains the availability of each neighbor chunk
uint32_t rVal = 0;
//Add to maks for initial chunks
for(int i = 0; i < CENTER_LOC; i++){
if((*env)->GetObjectArrayElement(env,jrx,i)!=NULL){
rVal = rVal + 1;
}
rVal = rVal << 1;
}
//add 1 for center chunk because we already have that
rVal = rVal + 1;
rVal = rVal << 1;
//continue on for remaining chunks
for(int i = CENTER_LOC+1; i < 27; i++){
if((*env)->GetObjectArrayElement(env,jrx,i)!=NULL){
rVal = rVal + 1;
}
if(i < 26){
rVal = rVal << 1;
}
}
return rVal;
}

View File

@ -1,10 +1,9 @@
#include <jni.h>
#include <stdio.h>
#include <immintrin.h>
#include <stdint.h>
#include "includes/utilities.h"
#include "includes/chunkmask.h"
#include "../includes/utilities.h"
#include "../includes/chunkmask.h"
#define BOUND_NO_DIR 0
@ -15,33 +14,37 @@
#define SET_BOUND_IGNORE 0
#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, jobjectArray jrd0, float * u, float * v, float * w, float dt);
#define LINEARSOLVERTIMES 20
static inline void add_source(int N, float * x, float * s, float dt);
static inline void advect(uint32_t chunk_mask, int N, int b, float ** jrd, float ** jrd0, float * u, float * v, float * w, float dt);
/*
* Adds force to all vectors
*/
JNIEXPORT void JNICALL Java_electrosphere_FluidSim_addSourceToVectors
(JNIEnv * env,
jobject this,
jint N,
jint chunk_mask,
jobjectArray jru,
jobjectArray jrv,
jobjectArray jrw,
jobjectArray jru0,
jobjectArray jrv0,
jobjectArray jrw0,
jfloat DIFFUSION_CONST,
jfloat VISCOSITY_CONST,
jfloat dt){
add_source(N,GET_ARR(env,jru,CENTER_LOC),GET_ARR(env,jru0,CENTER_LOC),dt);
add_source(N,GET_ARR(env,jrv,CENTER_LOC),GET_ARR(env,jrv0,CENTER_LOC),dt);
add_source(N,GET_ARR(env,jrw,CENTER_LOC),GET_ARR(env,jrw0,CENTER_LOC),dt);
static inline void addSourceToVectors
(
int N,
int chunk_mask,
float ** jru,
float ** jrv,
float ** jrw,
float ** jru0,
float ** jrv0,
float ** jrw0,
float DIFFUSION_CONST,
float VISCOSITY_CONST,
float dt){
add_source(N,GET_ARR_RAW(jru,CENTER_LOC),GET_ARR_RAW(jru0,CENTER_LOC),dt);
add_source(N,GET_ARR_RAW(jrv,CENTER_LOC),GET_ARR_RAW(jrv0,CENTER_LOC),dt);
add_source(N,GET_ARR_RAW(jrw,CENTER_LOC),GET_ARR_RAW(jrw0,CENTER_LOC),dt);
}
void add_source(int N, float * x, float * s, float dt){
/**
* Adds from a source array to a destination array
*/
static inline void add_source(int N, float * x, float * s, float dt){
int i;
int size=N*N*N;
for(i=0; i<size; i++){
@ -52,29 +55,28 @@ void add_source(int N, float * x, float * s, float dt){
/*
* Solves vector diffusion along all axis
*/
JNIEXPORT void JNICALL Java_electrosphere_FluidSim_solveVectorDiffuse
(JNIEnv * env,
jobject this,
jint N,
jint chunk_mask,
jobjectArray jru,
jobjectArray jrv,
jobjectArray jrw,
jobjectArray jru0,
jobjectArray jrv0,
jobjectArray jrw0,
jfloat DIFFUSION_CONST,
jfloat VISCOSITY_CONST,
jfloat dt){
static inline void solveVectorDiffuse (
int N,
int chunk_mask,
float ** jru,
float ** jrv,
float ** jrw,
float ** jru0,
float ** jrv0,
float ** jrw0,
float DIFFUSION_CONST,
float VISCOSITY_CONST,
float dt
){
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);
float * v = GET_ARR(env,jrv,CENTER_LOC);
float * w = GET_ARR(env,jrw,CENTER_LOC);
float * u0 = GET_ARR(env,jru0,CENTER_LOC);
float * v0 = GET_ARR(env,jrv0,CENTER_LOC);
float * w0 = GET_ARR(env,jrw0,CENTER_LOC);
float * u = GET_ARR_RAW(jru,CENTER_LOC);
float * v = GET_ARR_RAW(jrv,CENTER_LOC);
float * w = GET_ARR_RAW(jrw,CENTER_LOC);
float * u0 = GET_ARR_RAW(jru0,CENTER_LOC);
float * v0 = GET_ARR_RAW(jrv0,CENTER_LOC);
float * w0 = GET_ARR_RAW(jrw0,CENTER_LOC);
__m256 aScalar = _mm256_set1_ps(a);
__m256 cScalar = _mm256_set1_ps(c);
@ -161,35 +163,31 @@ JNIEXPORT void JNICALL Java_electrosphere_FluidSim_solveVectorDiffuse
/*
* Sets up a projection system of equations
*/
JNIEXPORT void JNICALL Java_electrosphere_FluidSim_setupProjection
(JNIEnv * env,
jobject this,
jint N,
jint chunk_mask,
jobjectArray jru,
jobjectArray jrv,
jobjectArray jrw,
jobjectArray jru0,
jobjectArray jrv0,
jobjectArray jrw0,
jfloat DIFFUSION_CONST,
jfloat VISCOSITY_CONST,
jfloat dt){
static inline void setupProjection(
int N,
int chunk_mask,
float ** ur,
float ** vr,
float ** wr,
float ** pr,
float ** divr,
float DIFFUSION_CONST,
float VISCOSITY_CONST,
float dt
){
int i, j, k;
__m256 xVector = _mm256_set1_ps(N);
__m256 yVector = _mm256_set1_ps(N);
__m256 zVector = _mm256_set1_ps(N);
__m256 nVector = _mm256_set1_ps(N);
__m256 constScalar = _mm256_set1_ps(-1.0/3.0);
__m256 zeroVec = _mm256_set1_ps(0);
__m256 vector, vector2, vector3;
float * u = GET_ARR(env,jru,CENTER_LOC);
float * v = GET_ARR(env,jrv,CENTER_LOC);
float * w = GET_ARR(env,jrw,CENTER_LOC);
float * u = GET_ARR_RAW(ur,CENTER_LOC);
float * v = GET_ARR_RAW(vr,CENTER_LOC);
float * w = GET_ARR_RAW(wr,CENTER_LOC);
float * p = GET_ARR(env,jru0,CENTER_LOC);
float * div = GET_ARR(env,jrv0,CENTER_LOC);
float * p = GET_ARR_RAW(pr,CENTER_LOC);
float * div = GET_ARR_RAW(divr,CENTER_LOC);
float scalar = 1.0/3.0;
float h = 1.0/N;
@ -203,15 +201,15 @@ JNIEXPORT void JNICALL Java_electrosphere_FluidSim_setupProjection
//first part
vector = _mm256_loadu_ps(&u[IX(i+1,j,k)]);
vector = _mm256_sub_ps(vector,_mm256_loadu_ps(&u[IX(i-1,j,k)]));
vector = _mm256_div_ps(vector,xVector);
vector = _mm256_div_ps(vector,nVector);
//second part
vector2 = _mm256_loadu_ps(&v[IX(i,j+1,k)]);
vector2 = _mm256_sub_ps(vector2,_mm256_loadu_ps(&v[IX(i,j-1,k)]));
vector2 = _mm256_div_ps(vector2,yVector);
vector2 = _mm256_div_ps(vector2,nVector);
//third part
vector3 = _mm256_loadu_ps(&w[IX(i,j,k+1)]);
vector3 = _mm256_sub_ps(vector3,_mm256_loadu_ps(&w[IX(i,j,k-1)]));
vector3 = _mm256_div_ps(vector3,zVector);
vector3 = _mm256_div_ps(vector3,nVector);
//multiply and finalize
vector = _mm256_add_ps(vector,_mm256_add_ps(vector2,vector3));
vector = _mm256_mul_ps(vector,constScalar);
@ -225,15 +223,15 @@ JNIEXPORT void JNICALL Java_electrosphere_FluidSim_setupProjection
//first part
vector = _mm256_loadu_ps(&u[IX(i+1,j,k)]);
vector = _mm256_sub_ps(vector,_mm256_loadu_ps(&u[IX(i-1,j,k)]));
vector = _mm256_div_ps(vector,xVector);
vector = _mm256_div_ps(vector,nVector);
//second part
vector2 = _mm256_loadu_ps(&v[IX(i,j+1,k)]);
vector2 = _mm256_sub_ps(vector2,_mm256_loadu_ps(&v[IX(i,j-1,k)]));
vector2 = _mm256_div_ps(vector2,yVector);
vector2 = _mm256_div_ps(vector2,nVector);
//third part
vector3 = _mm256_loadu_ps(&w[IX(i,j,k+1)]);
vector3 = _mm256_sub_ps(vector3,_mm256_loadu_ps(&w[IX(i,j,k-1)]));
vector3 = _mm256_div_ps(vector3,zVector);
vector3 = _mm256_div_ps(vector3,nVector);
//multiply and finalize
vector = _mm256_add_ps(vector,_mm256_add_ps(vector2,vector3));
vector = _mm256_mul_ps(vector,constScalar);
@ -241,14 +239,6 @@ JNIEXPORT void JNICALL 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;
// }
}
}
}
@ -256,28 +246,27 @@ JNIEXPORT void JNICALL Java_electrosphere_FluidSim_setupProjection
/*
* Solves a projection system of equations
*/
JNIEXPORT void JNICALL Java_electrosphere_FluidSim_solveProjection
(JNIEnv * env,
jobject this,
jint N,
jint chunk_mask,
jobjectArray jru,
jobjectArray jrv,
jobjectArray jrw,
jobjectArray jru0,
jobjectArray jrv0,
jobjectArray jrw0,
jfloat DIFFUSION_CONST,
jfloat VISCOSITY_CONST,
jfloat dt){
static inline void solveProjection(
int N,
int chunk_mask,
float ** jru,
float ** jrv,
float ** jrw,
float ** jru0,
float ** jrv0,
float ** jrw0,
float DIFFUSION_CONST,
float VISCOSITY_CONST,
float dt
){
int a = 1;
int c = 6;
int i, j, k, l, m;
__m256 aScalar = _mm256_set1_ps(a);
__m256 cScalar = _mm256_set1_ps(c);
float * p = GET_ARR(env,jru0,CENTER_LOC);
float * div = GET_ARR(env,jrv0,CENTER_LOC);
float * p = GET_ARR_RAW(jru0,CENTER_LOC);
float * div = GET_ARR_RAW(jrv0,CENTER_LOC);
// update for each cell
for(k=1; k<N-1; k++){
for(j=1; j<N-1; j++){
@ -290,7 +279,7 @@ JNIEXPORT void JNICALL Java_electrosphere_FluidSim_solveProjection
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_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);
@ -301,9 +290,6 @@ JNIEXPORT void JNICALL 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;
// }
}
}
}
@ -311,35 +297,29 @@ JNIEXPORT void JNICALL Java_electrosphere_FluidSim_solveProjection
/*
* Finalizes a projection (subtract curl, set bounds, etc)
*/
JNIEXPORT void JNICALL Java_electrosphere_FluidSim_finalizeProjection
(JNIEnv * env,
jobject this,
jint N,
jint chunk_mask,
jobjectArray jru,
jobjectArray jrv,
jobjectArray jrw,
jobjectArray jru0,
jobjectArray jrv0,
jobjectArray jrw0,
jfloat DIFFUSION_CONST,
jfloat VISCOSITY_CONST,
jfloat dt){
static inline void finalizeProjection(
int N,
int chunk_mask,
float ** jru,
float ** jrv,
float ** jrw,
float ** jru0,
float ** jrv0,
float ** jrw0,
float DIFFUSION_CONST,
float VISCOSITY_CONST,
float dt
){
int i, j, k;
// __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 constScalar = _mm256_set1_ps(0.5f*N);
__m256 vector, vector2, vector3;
float * u = GET_ARR(env,jru,CENTER_LOC);
float * v = GET_ARR(env,jrv,CENTER_LOC);
float * w = GET_ARR(env,jrw,CENTER_LOC);
float * u = GET_ARR_RAW(jru,CENTER_LOC);
float * v = GET_ARR_RAW(jrv,CENTER_LOC);
float * w = GET_ARR_RAW(jrw,CENTER_LOC);
float * p = GET_ARR(env,jru0,CENTER_LOC);
float * div = GET_ARR(env,jrv0,CENTER_LOC);
float h = 1.0 / N;
float * p = GET_ARR_RAW(jru0,CENTER_LOC);
float * div = GET_ARR_RAW(jrv0,CENTER_LOC);
for ( k=1 ; k<N-1 ; k++ ) {
for ( j=1 ; j<N-1 ; j++ ) {
@ -350,14 +330,14 @@ JNIEXPORT void JNICALL Java_electrosphere_FluidSim_finalizeProjection
vector = _mm256_loadu_ps(&p[IX(1+1,j,k)]);
vector2 = _mm256_loadu_ps(&p[IX(1-1,j,k)]);
vector = _mm256_sub_ps(vector,vector2);
vector = _mm256_mul_ps(vector,xScalar);
vector = _mm256_mul_ps(vector,constScalar);
vector = _mm256_sub_ps(_mm256_loadu_ps(&u[IX(1,j,k)]),vector);
_mm256_storeu_ps(&u[IX(1,j,k)],vector);
//upper
vector = _mm256_loadu_ps(&p[IX(9+1,j,k)]);
vector2 = _mm256_loadu_ps(&p[IX(9-1,j,k)]);
vector = _mm256_sub_ps(vector,vector2);
vector = _mm256_mul_ps(vector,xScalar);
vector = _mm256_mul_ps(vector,constScalar);
vector = _mm256_sub_ps(_mm256_loadu_ps(&u[IX(9,j,k)]),vector);
_mm256_storeu_ps(&u[IX(9,j,k)],vector);
//
@ -367,14 +347,14 @@ JNIEXPORT void JNICALL Java_electrosphere_FluidSim_finalizeProjection
vector = _mm256_loadu_ps(&p[IX(1,j+1,k)]);
vector2 = _mm256_loadu_ps(&p[IX(1,j-1,k)]);
vector = _mm256_sub_ps(vector,vector2);
vector = _mm256_mul_ps(vector,yScalar);
vector = _mm256_mul_ps(vector,constScalar);
vector = _mm256_sub_ps(_mm256_loadu_ps(&v[IX(1,j,k)]),vector);
_mm256_storeu_ps(&v[IX(1,j,k)],vector);
//upper
vector = _mm256_loadu_ps(&p[IX(9,j+1,k)]);
vector2 = _mm256_loadu_ps(&p[IX(9,j-1,k)]);
vector = _mm256_sub_ps(vector,vector2);
vector = _mm256_mul_ps(vector,yScalar);
vector = _mm256_mul_ps(vector,constScalar);
vector = _mm256_sub_ps(_mm256_loadu_ps(&v[IX(9,j,k)]),vector);
_mm256_storeu_ps(&v[IX(9,j,k)],vector);
//
@ -384,21 +364,16 @@ JNIEXPORT void JNICALL Java_electrosphere_FluidSim_finalizeProjection
vector = _mm256_loadu_ps(&p[IX(1,j,k+1)]);
vector2 = _mm256_loadu_ps(&p[IX(1,j,k-1)]);
vector = _mm256_sub_ps(vector,vector2);
vector = _mm256_mul_ps(vector,zScalar);
vector = _mm256_mul_ps(vector,constScalar);
vector = _mm256_sub_ps(_mm256_loadu_ps(&w[IX(1,j,k)]),vector);
_mm256_storeu_ps(&w[IX(1,j,k)],vector);
//upper
vector = _mm256_loadu_ps(&p[IX(9,j,k+1)]);
vector2 = _mm256_loadu_ps(&p[IX(9,j,k-1)]);
vector = _mm256_sub_ps(vector,vector2);
vector = _mm256_mul_ps(vector,zScalar);
vector = _mm256_mul_ps(vector,constScalar);
vector = _mm256_sub_ps(_mm256_loadu_ps(&w[IX(9,j,k)]),vector);
_mm256_storeu_ps(&w[IX(9,j,k)],vector);
// for(i = 1; i < N-1; i++){
// u[IX(i,j,k)] = u[IX(i,j,k)] - 0.5 * (p[IX(i+1,j,k)] - p[IX(i-1,j,k)]) / h;
// v[IX(i,j,k)] = v[IX(i,j,k)] - 0.5 * (p[IX(i,j+1,k)] - p[IX(i,j-1,k)]) / h;
// w[IX(i,j,k)] = w[IX(i,j,k)] - 0.5 * (p[IX(i,j,k+1)] - p[IX(i,j,k-1)]) / h;
// }
}
}
}
@ -406,41 +381,42 @@ JNIEXPORT void JNICALL Java_electrosphere_FluidSim_finalizeProjection
/*
* Advects u, v, and w
*/
JNIEXPORT void JNICALL Java_electrosphere_FluidSim_advectVectors
(JNIEnv * env,
jobject this,
jint N,
jint chunk_mask,
jobjectArray jru,
jobjectArray jrv,
jobjectArray jrw,
jobjectArray jru0,
jobjectArray jrv0,
jobjectArray jrw0,
jfloat DIFFUSION_CONST,
jfloat VISCOSITY_CONST,
jfloat dt){
advect(env,chunk_mask,N,1,jru,jru0,GET_ARR(env,jru0,CENTER_LOC),GET_ARR(env,jrv0,CENTER_LOC),GET_ARR(env,jrw0,CENTER_LOC),dt);
advect(env,chunk_mask,N,2,jrv,jrv0,GET_ARR(env,jru0,CENTER_LOC),GET_ARR(env,jrv0,CENTER_LOC),GET_ARR(env,jrw0,CENTER_LOC),dt);
advect(env,chunk_mask,N,3,jrw,jrw0,GET_ARR(env,jru0,CENTER_LOC),GET_ARR(env,jrv0,CENTER_LOC),GET_ARR(env,jrw0,CENTER_LOC),dt);
static inline void advectVectors(
int N,
int chunk_mask,
float ** jru,
float ** jrv,
float ** jrw,
float ** jru0,
float ** jrv0,
float ** jrw0,
float DIFFUSION_CONST,
float VISCOSITY_CONST,
float dt
){
advect(chunk_mask,N,1,jru,jru0,GET_ARR_RAW(jru0,CENTER_LOC),GET_ARR_RAW(jrv0,CENTER_LOC),GET_ARR_RAW(jrw0,CENTER_LOC),dt);
advect(chunk_mask,N,2,jrv,jrv0,GET_ARR_RAW(jru0,CENTER_LOC),GET_ARR_RAW(jrv0,CENTER_LOC),GET_ARR_RAW(jrw0,CENTER_LOC),dt);
advect(chunk_mask,N,3,jrw,jrw0,GET_ARR_RAW(jru0,CENTER_LOC),GET_ARR_RAW(jrv0,CENTER_LOC),GET_ARR_RAW(jrw0,CENTER_LOC),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){
/**
* Actually performs the advection
*/
static inline 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;
float x, y, z, s0, t0, s1, t1, u1, u0, dtx,dty,dtz;
dtx=dty=dtz=dt*N;
float * d = GET_ARR(env,jrd,CENTER_LOC);
float * d = GET_ARR_RAW(jrd,CENTER_LOC);
float * d0 = GET_ARR(env,jrd0,CENTER_LOC);
float * d0 = GET_ARR_RAW(jrd0,CENTER_LOC);
for(k=1; k<N-1; k++){
for(j=1; j<N-1; j++){
for(i=1; i<N-1; i++){
d0 = GET_ARR(env,jrd0,CENTER_LOC);
d0 = GET_ARR_RAW(jrd0,CENTER_LOC);
//calculate location to pull from
x = i-dtx*u[IX(i,j,k)];
y = j-dty*v[IX(i,j,k)];
@ -506,7 +482,7 @@ void advect(JNIEnv * env, uint32_t chunk_mask, int N, int b, jobjectArray jrd, j
// printf("Hit other chunk\n");
d0 = GET_ARR(env,jrd0,CK(m,n,o));
d0 = GET_ARR_RAW(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);
@ -668,35 +644,31 @@ void advect(JNIEnv * env, uint32_t chunk_mask, int N, int b, jobjectArray jrd, j
}
}
JNIEXPORT void JNICALL Java_electrosphere_FluidSim_setBoundsToNeighbors
(JNIEnv * env,
jobject this,
jint N,
jint chunk_mask,
jint vector_dir,
jobjectArray neighborArray){
/**
* Sets the bounds of this cube to those of its neighbor
*/
static inline void setBoundsToNeighborsRaw(
int N,
int chunk_mask,
int vector_dir,
float ** neighborArray
){
int DIM = N;
float * target = GET_ARR(env,neighborArray,CENTER_LOC);
float * target = GET_ARR_RAW(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++){
//((x)+(DIM)*(y) + (DIM)*(DIM)*(z))
target[IX(0,x,y)] = vector_dir==BOUND_DIR_U ? -target[IX(1,x,y)] : target[IX(1,x,y)];
target[IX(DIM-1,x,y)] = vector_dir==BOUND_DIR_U ? -target[IX(DIM-2,x,y)] : target[IX(DIM-2,x,y)];
target[IX(x,0,y)] = vector_dir==BOUND_DIR_V ? -target[IX(x,1,y)] : target[IX(x,1,y)];
target[IX(x,DIM-1,y)] = vector_dir==BOUND_DIR_V ? -target[IX(x,DIM-2,y)] : target[IX(x,DIM-2,y)];
target[IX(x,y,0)] = vector_dir==BOUND_DIR_W ? -target[IX(x,y,1)] : target[IX(x,y,1)];
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)]));
@ -715,6 +687,7 @@ JNIEXPORT void JNICALL Java_electrosphere_FluidSim_setBoundsToNeighbors
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);
@ -725,19 +698,19 @@ JNIEXPORT void JNICALL Java_electrosphere_FluidSim_setBoundsToNeighbors
target[IX(DIM-1,DIM-1,DIM-1)] = (float)((target[IX(DIM-1,DIM-1,DIM-2)]+target[IX(DIM-1,DIM-2,DIM-1)]+target[IX(DIM-1,DIM-1,DIM-2)])/3.0);
}
/**
* This exclusively copies neighbors to make sure zeroing out stuff doesn't break sim
*/
JNIEXPORT void JNICALL Java_electrosphere_FluidSim_copyNeighbors
(JNIEnv * env,
jobject this,
jint N,
jint chunk_mask,
jint cx,
jint vector_dir,
jobjectArray neighborArray){
static inline void copyNeighborsRaw(
int N,
int chunk_mask,
int cx,
int vector_dir,
float ** neighborArray
){
int DIM = N;
float * target = GET_ARR(env,neighborArray,CENTER_LOC);
float * target = GET_ARR_RAW(neighborArray,CENTER_LOC);
float * source;
@ -746,9 +719,20 @@ JNIEXPORT void JNICALL Java_electrosphere_FluidSim_copyNeighbors
// PLANES
//
//
// __m512 transferVector;// = _mm512_set1_ps(0.5*N);
//__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)]));
//_mm256_storeu_ps(&p[IX(i,j,k)],vector);
//__m256
//_mm256_loadu_ps
//_mm256_storeu_ps
if(ARR_EXISTS(chunk_mask,0,1,1)){
source = GET_ARR(env,neighborArray,CK(0,1,1));
source = GET_ARR_RAW(neighborArray,CK(0,1,1));
for(int x=1; x < DIM-1; x++){
// transferVector = _mm512_loadu_ps(&source[IX(DIM-2,x,1)]);
// _mm512_storeu_ps(&target[IX(0,x,1)],_mm512_loadu_ps(&source[IX(DIM-2,x,1)]));
for(int y = 1; y < DIM-1; y++){
target[IX(0,x,y)] = source[IX(DIM-2,x,y)];
}
@ -756,8 +740,9 @@ JNIEXPORT void JNICALL Java_electrosphere_FluidSim_copyNeighbors
}
if(ARR_EXISTS(chunk_mask,2,1,1)){
source = GET_ARR(env,neighborArray,CK(2,1,1));
source = GET_ARR_RAW(neighborArray,CK(2,1,1));
for(int x=1; x < DIM-1; x++){
// _mm512_storeu_ps(&target[IX(DIM-1,x,1)],_mm512_loadu_ps(&source[IX(1,x,1)]));
for(int y = 1; y < DIM-1; y++){
target[IX(DIM-1,x,y)] = source[IX(1,x,y)];
}
@ -765,7 +750,7 @@ JNIEXPORT void JNICALL Java_electrosphere_FluidSim_copyNeighbors
}
if(ARR_EXISTS(chunk_mask,1,0,1)){
source = GET_ARR(env,neighborArray,CK(1,0,1));
source = GET_ARR_RAW(neighborArray,CK(1,0,1));
for(int x=1; x < DIM-1; x++){
for(int y = 1; y < DIM-1; y++){
target[IX(x,0,y)] = source[IX(x,DIM-2,y)];
@ -774,7 +759,7 @@ JNIEXPORT void JNICALL Java_electrosphere_FluidSim_copyNeighbors
}
if(ARR_EXISTS(chunk_mask,1,2,1)){
source = GET_ARR(env,neighborArray,CK(1,2,1));
source = GET_ARR_RAW(neighborArray,CK(1,2,1));
for(int x=1; x < DIM-1; x++){
for(int y = 1; y < DIM-1; y++){
target[IX(x,DIM-1,y)] = source[IX(x,1,y)];
@ -783,7 +768,7 @@ JNIEXPORT void JNICALL Java_electrosphere_FluidSim_copyNeighbors
}
if(ARR_EXISTS(chunk_mask,1,1,0)){
source = GET_ARR(env,neighborArray,CK(1,1,0));
source = GET_ARR_RAW(neighborArray,CK(1,1,0));
for(int x=1; x < DIM-1; x++){
for(int y = 1; y < DIM-1; y++){
target[IX(x,y,0)] = source[IX(x,y,DIM-2)];
@ -792,7 +777,7 @@ JNIEXPORT void JNICALL Java_electrosphere_FluidSim_copyNeighbors
}
if(ARR_EXISTS(chunk_mask,1,1,2)){
source = GET_ARR(env,neighborArray,CK(1,1,2));
source = GET_ARR_RAW(neighborArray,CK(1,1,2));
for(int x=1; x < DIM-1; x++){
for(int y = 1; y < DIM-1; y++){
target[IX(x,y,DIM-1)] = source[IX(x,y,1)];
@ -807,28 +792,28 @@ JNIEXPORT void JNICALL Java_electrosphere_FluidSim_copyNeighbors
//
//
if(ARR_EXISTS(chunk_mask,0,0,1)){
source = GET_ARR(env,neighborArray,CK(0,0,1));
source = GET_ARR_RAW(neighborArray,CK(0,0,1));
for(int x=1; x < DIM-1; x++){
target[IX(0,0,x)] = source[IX(DIM-2,DIM-2,x)];
}
}
if(ARR_EXISTS(chunk_mask,2,0,1)){
source = GET_ARR(env,neighborArray,CK(2,0,1));
source = GET_ARR_RAW(neighborArray,CK(2,0,1));
for(int x=1; x < DIM-1; x++){
target[IX(DIM-1,0,x)] = source[IX(1,DIM-2,x)];
}
}
if(ARR_EXISTS(chunk_mask,0,2,1)){
source = GET_ARR(env,neighborArray,CK(0,2,1));
source = GET_ARR_RAW(neighborArray,CK(0,2,1));
for(int x=1; x < DIM-1; x++){
target[IX(0,DIM-1,x)] = source[IX(DIM-2,1,x)];
}
}
if(ARR_EXISTS(chunk_mask,2,2,1)){
source = GET_ARR(env,neighborArray,CK(2,2,1));
source = GET_ARR_RAW(neighborArray,CK(2,2,1));
for(int x=1; x < DIM-1; x++){
target[IX(DIM-1,DIM-1,x)] = source[IX(1,1,x)];
}
@ -838,28 +823,28 @@ JNIEXPORT void JNICALL Java_electrosphere_FluidSim_copyNeighbors
//
if(ARR_EXISTS(chunk_mask,0,1,0)){
source = GET_ARR(env,neighborArray,CK(0,1,0));
source = GET_ARR_RAW(neighborArray,CK(0,1,0));
for(int x=1; x < DIM-1; x++){
target[IX(0,x,0)] = source[IX(DIM-2,x,DIM-2)];
}
}
if(ARR_EXISTS(chunk_mask,2,1,0)){
source = GET_ARR(env,neighborArray,CK(2,1,0));
source = GET_ARR_RAW(neighborArray,CK(2,1,0));
for(int x=1; x < DIM-1; x++){
target[IX(DIM-1,x,0)] = source[IX(1,x,DIM-2)];
}
}
if(ARR_EXISTS(chunk_mask,0,1,2)){
source = GET_ARR(env,neighborArray,CK(0,1,2));
source = GET_ARR_RAW(neighborArray,CK(0,1,2));
for(int x=1; x < DIM-1; x++){
target[IX(0,x,DIM-1)] = source[IX(DIM-2,x,1)];
}
}
if(ARR_EXISTS(chunk_mask,2,1,2)){
source = GET_ARR(env,neighborArray,CK(2,1,2));
source = GET_ARR_RAW(neighborArray,CK(2,1,2));
for(int x=1; x < DIM-1; x++){
target[IX(DIM-1,x,DIM-1)] = source[IX(1,x,1)];
}
@ -869,28 +854,28 @@ JNIEXPORT void JNICALL Java_electrosphere_FluidSim_copyNeighbors
//
if(ARR_EXISTS(chunk_mask,1,0,0)){
source = GET_ARR(env,neighborArray,CK(1,0,0));
source = GET_ARR_RAW(neighborArray,CK(1,0,0));
for(int x=1; x < DIM-1; x++){
target[IX(x,0,0)] = source[IX(x,DIM-2,DIM-2)];
}
}
if(ARR_EXISTS(chunk_mask,1,2,0)){
source = GET_ARR(env,neighborArray,CK(1,2,0));
source = GET_ARR_RAW(neighborArray,CK(1,2,0));
for(int x=1; x < DIM-1; x++){
target[IX(x,DIM-1,0)] = source[IX(x,1,DIM-2)];
}
}
if(ARR_EXISTS(chunk_mask,1,0,2)){
source = GET_ARR(env,neighborArray,CK(1,0,2));
source = GET_ARR_RAW(neighborArray,CK(1,0,2));
for(int x=1; x < DIM-1; x++){
target[IX(x,0,DIM-1)] = source[IX(x,DIM-2,1)];
}
}
if(ARR_EXISTS(chunk_mask,1,2,2)){
source = GET_ARR(env,neighborArray,CK(1,2,2));
source = GET_ARR_RAW(neighborArray,CK(1,2,2));
for(int x=1; x < DIM-1; x++){
target[IX(x,DIM-1,DIM-1)] = source[IX(x,1,1)];
}
@ -904,22 +889,22 @@ JNIEXPORT void JNICALL Java_electrosphere_FluidSim_copyNeighbors
//
if(ARR_EXISTS(chunk_mask,0,0,0)){
source = GET_ARR(env,neighborArray,CK(0,0,0));
source = GET_ARR_RAW(neighborArray,CK(0,0,0));
target[IX(0,0,0)] = source[IX(DIM-2,DIM-2,DIM-2)];
}
if(ARR_EXISTS(chunk_mask,2,0,0)){
source = GET_ARR(env,neighborArray,CK(2,0,0));
source = GET_ARR_RAW(neighborArray,CK(2,0,0));
target[IX(DIM-1,0,0)] = source[IX(1,DIM-2,DIM-2)];
}
if(ARR_EXISTS(chunk_mask,0,2,0)){
source = GET_ARR(env,neighborArray,CK(0,2,0));
source = GET_ARR_RAW(neighborArray,CK(0,2,0));
target[IX(0,DIM-1,0)] = source[IX(DIM-2,1,DIM-2)];
}
if(ARR_EXISTS(chunk_mask,2,2,0)){
source = GET_ARR(env,neighborArray,CK(2,2,0));
source = GET_ARR_RAW(neighborArray,CK(2,2,0));
target[IX(DIM-1,DIM-1,0)] = source[IX(1,1,DIM-2)];
}
@ -927,25 +912,25 @@ JNIEXPORT void JNICALL Java_electrosphere_FluidSim_copyNeighbors
//
if(ARR_EXISTS(chunk_mask,0,0,2)){
source = GET_ARR(env,neighborArray,CK(0,0,2));
source = GET_ARR_RAW(neighborArray,CK(0,0,2));
target[IX(0,0,DIM-1)] = source[IX(DIM-2,DIM-2,1)];
}
if(ARR_EXISTS(chunk_mask,2,0,2)){
source = GET_ARR(env,neighborArray,CK(2,0,2));
source = GET_ARR_RAW(neighborArray,CK(2,0,2));
target[IX(DIM-1,0,DIM-1)] = source[IX(1,DIM-2,1)];
}
if(ARR_EXISTS(chunk_mask,0,2,2)){
source = GET_ARR(env,neighborArray,CK(0,2,2));
source = GET_ARR_RAW(neighborArray,CK(0,2,2));
target[IX(0,DIM-1,DIM-1)] = source[IX(DIM-2,1,1)];
}
if(ARR_EXISTS(chunk_mask,2,2,2)){
source = GET_ARR(env,neighborArray,CK(2,2,2));
source = GET_ARR_RAW(neighborArray,CK(2,2,2));
target[IX(DIM-1,DIM-1,DIM-1)] = source[IX(1,1,1)];
}
}
}

View File

@ -2,29 +2,33 @@ package electrosphere;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.file.Files;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.Set;
import org.joml.Vector2i;
import org.joml.Vector3i;
import org.lwjgl.BufferUtils;
import org.lwjgl.PointerBuffer;
import org.lwjgl.system.MemoryUtil;
import org.lwjgl.glfw.GLFW;
/**
* Simulates a fluid via opencl
*/
public class FluidSim {
/**
* Load fluid sim library
*/
static {
System.out.println(System.getProperty("user.dir"));
System.load(System.getProperty("user.dir") + "/shared-folder/libfluidsim.dll");
String osName = System.getProperty("os.name").toLowerCase();
System.out.println(osName);
if(osName.contains("win")){
System.load(new File("./shared-folder/libfluidsim.dll").toPath().toAbsolutePath().toString());
} else {
System.load(new File("./shared-folder/libfluidsim.so").toPath().toAbsolutePath().toString());
}
}
public static final int DIM = 18;
@ -40,21 +44,21 @@ public class FluidSim {
// +-------------+ (2,0,0) +---------------> X
//Buffers that contain density for current frame
ByteBuffer[] density = new ByteBuffer[27];
public ByteBuffer[] density = new ByteBuffer[27];
//Buffers that contain new density to add to the simulation
ByteBuffer[] densityAddition = new ByteBuffer[27];
public ByteBuffer[] densityAddition = new ByteBuffer[27];
//Buffers that contain u vector directions
ByteBuffer[] uVector = new ByteBuffer[27];
public ByteBuffer[] uVector = new ByteBuffer[27];
//Buffers that contain v vector directions
ByteBuffer[] vVector = new ByteBuffer[27];
public ByteBuffer[] vVector = new ByteBuffer[27];
//Buffers that contain w vector directions
ByteBuffer[] wVector = new ByteBuffer[27];
public ByteBuffer[] wVector = new ByteBuffer[27];
//Buffers that contain u vector directions to add to the simulation
ByteBuffer[] uAdditionVector = new ByteBuffer[27];
public ByteBuffer[] uAdditionVector = new ByteBuffer[27];
//Buffers that contain v vector directions to add to the simulation
ByteBuffer[] vAdditionVector = new ByteBuffer[27];
public ByteBuffer[] vAdditionVector = new ByteBuffer[27];
//Buffers that contain w vector directions to add to the simulation
ByteBuffer[] wAdditionVector = new ByteBuffer[27];
public ByteBuffer[] wAdditionVector = new ByteBuffer[27];
//The densities for every voxel for the current frame
float[] densityArrayView = new float[DIM * DIM * DIM];
@ -69,15 +73,15 @@ public class FluidSim {
public float[] v0ArrayView = new float[DIM * DIM * DIM];
float[] w0ArrayView = new float[DIM * DIM * DIM];
int chunkMask = 0;
public int chunkMask = 0;
static final float DIFFUSION_CONSTANT = 0.0f;
static final float VISCOSITY_CONSTANT = 0.0f;
static final float DIFFUSION_CONSTANT = 0.00001f;
static final float VISCOSITY_CONSTANT = 0.00001f;
static final int LINEARSOLVERTIMES = 20;
static final float GRAVITY = -100f;
static final float GRAVITY = -1000f;
public void setup(Vector3i offset){
//allocate buffers for this chunk
@ -105,56 +109,66 @@ public class FluidSim {
FloatBuffer vf = vVector[13].asFloatBuffer();
FloatBuffer wf = wVector[13].asFloatBuffer();
Random rand = new Random(1);
//make a cube of water in the center
for(int i = 0; i < DIM; i++){
for(int j = 0; j < DIM; j++){
for(int k = 0; k < DIM; k++){
if(offset.x == 0 && offset.y == 0 && offset.z == 0){
// if(offset.x == 0 && offset.y == 0 && offset.z == 0){
if(
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
Math.abs(16 - i) < 4 &&
Math.abs(8 - j) < 4 &&
Math.abs(10 - k) < 4
// &&
// i < 17 && i > 0 &&
// j < 17 && j > 0 &&
// k < 17 && k > 0
){
xf.put(1);
uf.put(50);
vf.put(0);
wf.put(0);
} else {
uf.put(0.1f);
vf.put(-1);
wf.put(0.1f);
}
else {
xf.put(0);
uf.put(0);
vf.put(0);
wf.put(0);
}
} else {
if(
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);
} else {
xf.put(0);
uf.put(0);
vf.put(0);
wf.put(0);
}
}
// } else {
// if(
// 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);
// } else {
// xf.put(0);
// uf.put(0);
// vf.put(0);
// wf.put(0);
// }
// }
}
}
}
}
static int i = 0;
static double time = 0;
static double lastTime = 0;
public static void simChunks(FluidSim[][][] simArray, int step, float timestep){
// simArray[0][1][2].density0ArrayView[IX(10,10,3)] = 3.0f;
// simArray[2][1][2].density0ArrayView[IX(10,10,3)] = 3.0f;
// simArray[2][1][0].density0ArrayView[IX(10,10,3)] = 3.0f;
List<FluidSim> chunksToSim = new LinkedList<FluidSim>();
//
//init data for upcoming frame
for(int x = 0; x < simArray.length; x++){
@ -168,31 +182,24 @@ public class FluidSim {
//Performs main fluid simulation logic
//
simArray[x][y][z].writeNewStateIntoBuffers();
//
// add to queue
//
chunksToSim.add(simArray[x][y][z]);
}
}
}
lastTime = GLFW.glfwGetTime();
//
//Vector stage
solveChunkMask(simArray);
addVectorSources(simArray, timestep);
swapAllVectorFields(simArray, timestep);
solveVectorDiffusion(simArray, timestep);
solveProjection(simArray, step, timestep);
swapAllVectorFields(simArray, timestep);
advectVectorsAcrossBoundaries(simArray, timestep);
solveProjection(simArray, step, timestep);
//
//Density stage
addDensity(simArray, timestep);
swapAllDensityArrays(simArray, timestep);
diffuseDensity(simArray, timestep);
swapAllDensityArrays(simArray, timestep);
advectDensity(simArray, timestep);
// mirrorNeighborDensities(simArray, timestep);
//simulate
simulateWrapper(chunksToSim,0.01f);
//clock
time = time + (GLFW.glfwGetTime() - lastTime);
i++;
if(i == 100){
System.out.println(time / 100.0 * 1000.0);
}
//
@ -207,6 +214,100 @@ public class FluidSim {
}
}
}
if(Main.endStep == 0){
System.exit(0);
}
}
//
//
//Management functions
//
//
/**
* Creates a fluid simulation array based on a given dimension set
* @param dimx the x dimension
* @param dimy the y dimension
* @param dimz the z dimension
* @return The array of fluid sim chunks
*/
public static FluidSim[][][] initFluidSim(int dimx, int dimy, int dimz){
FluidSim[][][] simArray = new FluidSim[dimx][dimy][dimz];
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] = new FluidSim();
simArray[x][y][z].setup(new Vector3i(x,y,z));
}
}
}
//set sim adjacencies
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++){
FluidSim current = simArray[x][y][z];
for(int i = -1; i < 2; i++){
for(int j = -1; j < 2; j++){
for(int k = -1; k < 2; k++){
if(i == j && j == k && k == 0){
continue;
}
if(
0 <= x + i && x + i < simArray.length &&
0 <= y + j && y + j < simArray[0].length &&
0 <= z + k && z + k < simArray[0][0].length
){
current.setNeighbor(i+1,j+1,k+1,simArray[x+i][y+j][z+k]);
}
}
}
}
}
}
}
return simArray;
}
static void printLayer(FluidSim[][][] simArray, int step, int layer){
if(step == 0){
for(int x = 0; x < DIM; x++){
for(int y = 0; y < DIM; y++){
System.out.printf("%.2f\t",simArray[0][0][0].uArrayView[IX(x,layer,y)]);
}
System.out.println();
}
}
}
private static double sumAllDensity(FluidSim[][][] simArray){
@ -333,525 +434,36 @@ public class FluidSim {
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++){
for(int z = 0; z < simArray[0][0].length; z++){
simArray[x][y][z].calculateChunkMaskWrapper();
}
}
}
}
private static void addVectorSources(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++){
//Add source to all 3 vectors
// add_source(N, u, u0, dt);
// add_source(N, v, v0, dt);
// add_source(N, w, w0, dt);
simArray[x][y][z].addSourceToVectorsWrapper(timestep);
}
}
}
//swap
//u <=> u0 etc for u, v, and w
}
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++){
for(int z = 0; z < simArray[0][0].length; z++){
//lin_solve(env, chunk_mask, N, b, x, x0, a, 1+6*a);
//+
//set_bnd(env, chunk_mask, N, b, x);
//for u, v, and w all in 1 shot
simArray[x][y][z].solveVectorDiffuseWrapper(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].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, 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].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);
}
}
}
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
//...
simArray[x][y][z].setupProjectionWrapper(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(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);
}
}
}
//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++){
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++){
//lin_solve(env, chunk_mask, N, b, x, x0, a, 1+6*a);
//for u, v, and w all in 1 shot
simArray[x][y][z].solveProjectionWrapper(timestep);
}
}
}
//be sure to set boundaries to neighbor chunk values where appropriate
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].copyNeighborsWrapper(0, x, simArray[x][y][z].uAdditionVector);
}
}
}
}
//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++){
for(int z = 0; z < simArray[0][0].length; z++){
//Subtract curl field from current vector field
//...
simArray[x][y][z].finalizeProjectionWrapper(timestep);
}
}
}
//set boundaries a final time for 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].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);
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);
}
}
}
}
private static void mirrorNeighborDensities(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(0, simArray[x][y][z].density);
}
}
}
}
private static void swapAllVectorFields(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].swapVectorFields();
}
}
}
//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, 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);
}
}
}
}
private void swapVectorFields(){
ByteBuffer tmp;
//swap x0 <-> x
// tmp = densityAddition;
// densityAddition = density[13];
// density[13] = tmp;
//swap u0 <-> u
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);
}
}
}
//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++){
// advect(env, chunk_mask, N, 1, jru, u0, u0, v0, w0, dt);
// advect(env, chunk_mask, N, 2, jrv, v0, u0, v0, w0, dt);
// advect(env, chunk_mask, N, 3, jrw, w0, u0, v0, w0, dt);
//...
simArray[x][y][z].advectVectorsWrapper(timestep);
}
}
}
//mirror neighbor data
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].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 addDensity(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++){
//add_source(N, x, x0, dt);
simArray[x][y][z].addDensityWrapper(timestep);
//swap x <=> x0
//swap arrays in java side...
// simArray[x][y][z].swapDensityArrays();
}
}
}
}
private static 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, x, simArray[x][y][z].density);
simArray[x][y][z].copyNeighborsWrapper(0, x, simArray[x][y][z].densityAddition);
}
}
}
}
private void swapDensityArrays(){
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){
for(int l = 0; l < LINEARSOLVERTIMES; l++){
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++){
//lin_solve(env, chunk_mask, N, b, x, x0, a, 1+6*a);
//+
//set_bnd(env, chunk_mask, N, b, x);
simArray[x][y][z].solveDiffuseDensityWrapper(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(0, simArray[x][y][z].density);
}
}
}
}
}
private static void advectDensity(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++){
//swap x <=> x0 again
// simArray[x][y][z].swapDensityArrays();
//advect density
simArray[x][y][z].advectDensityWrapper(timestep);
}
}
}
}
/**
* The native function call to simulate a frame of fluid
* @param DIM_X
* @param DIM_Y
* @param DIM_Z
* @param x
* @param x0
* @param u
* @param v
* @param w
* @param u0
* @param v0
* @param w0
* @param DIFFUSION_CONSTANT
* @param VISCOSITY_CONSTANT
* Main simulation function
* @param timestep
*/
private native void simulate(
int DIM_X,
int chunkMask,
ByteBuffer[] x,
ByteBuffer x0,
ByteBuffer[] u,
ByteBuffer[] v,
ByteBuffer[] w,
ByteBuffer[] u0,
ByteBuffer[] v0,
ByteBuffer[] w0,
float DIFFUSION_CONSTANT,
float VISCOSITY_CONSTANT,
float timestep
);
private void calculateChunkMaskWrapper(){
this.chunkMask = this.calculateChunkMask(density);
private static void simulateWrapper(List<FluidSim> chunks, float timestep){
simulate(chunks,timestep);
}
/**
* Calculates the mask of chunk neighbors
* @param densityBuffers The neighbor array
* @return The mask
*/
private native int calculateChunkMask(ByteBuffer[] densityBuffers);
private static native void simulate(List<FluidSim> chunks, float timestep);
/**
* Add vector values to u, v, and w all at once
*/
private void addSourceToVectorsWrapper(float timestep){
addSourceToVectors(DIM, chunkMask, uVector, vVector, wVector, uAdditionVector, vAdditionVector, wAdditionVector, DIFFUSION_CONSTANT, VISCOSITY_CONSTANT, timestep);
}
private native void addSourceToVectors(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);
/**
* Solves u, v, and w diffusion systems of equations
*/
private void solveVectorDiffuseWrapper(float timestep){
solveVectorDiffuse(DIM, chunkMask, uVector, vVector, wVector, uAdditionVector, vAdditionVector, wAdditionVector, DIFFUSION_CONSTANT, VISCOSITY_CONSTANT, timestep);
}
private native void solveVectorDiffuse(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);
/**
* Setup projection system
*/
private void setupProjectionWrapper(float timestep){
setupProjection(DIM, chunkMask, uVector, vVector, wVector, uAdditionVector, vAdditionVector, wAdditionVector, DIFFUSION_CONSTANT, VISCOSITY_CONSTANT, timestep);
}
private native void setupProjection(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 solveProjectionWrapper(float timestep){
solveProjection(DIM, chunkMask, uVector, vVector, wVector, uAdditionVector, vAdditionVector, wAdditionVector, DIFFUSION_CONSTANT, VISCOSITY_CONSTANT, timestep);
}
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);
/**
* Does work like subtracting curl from vector field, setting boundaries, etc
*/
private void finalizeProjectionWrapper(float timestep){
finalizeProjection(DIM, chunkMask, uVector, vVector, wVector, uAdditionVector, vAdditionVector, wAdditionVector, DIFFUSION_CONSTANT, VISCOSITY_CONSTANT, timestep);
}
private native void finalizeProjection(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);
/**
* Advects vectors
*/
private void advectVectorsWrapper(float timestep){
advectVectors(DIM, chunkMask, uVector, vVector, wVector, uAdditionVector, vAdditionVector, wAdditionVector, DIFFUSION_CONSTANT, VISCOSITY_CONSTANT, timestep);
}
private native void advectVectors(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);
/**
* Adds density to the simulation
*/
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);
/**
* Solve density diffusion
*/
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);
/**
* Solve density diffusion
*/
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);
/**
* Sets the bounds of the neighbormap to neighbor values if available
* Dumps this chunk's density values to disk
* @param path the path to dump to
*/
private void setBoundsToNeighborsWrapper(int vectorDir, ByteBuffer[] neighborMap){
setBoundsToNeighbors(DIM, chunkMask, vectorDir, neighborMap);
public void dumpToDisk(String path){
OutputStream fileOS = null;
try {
fileOS = Files.newOutputStream(new File(path).toPath());
ByteBuffer density = this.density[13];
while(density.hasRemaining()){
fileOS.write(density.get());
}
fileOS.close();
} catch (IOException e) {
e.printStackTrace();
}
}
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, int x, ByteBuffer[] neighborMap){
copyNeighbors(DIM, chunkMask, x, vectorDir, neighborMap);
}
private native void copyNeighbors(int DIM_X, int chunkMask, int x, int vectorDir, ByteBuffer[] neighborMap);

View File

@ -2,9 +2,9 @@ package electrosphere;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Scanner;
import java.nio.file.Path;
import java.util.concurrent.TimeUnit;
import org.joml.Vector3i;
@ -17,30 +17,40 @@ import electrosphere.render.Mesh;
*/
public class Main {
static boolean render = false;
public static int endStep = -1;
public static final float TIMESTEP = 0.001f;
public static void main(String args[]){
int dim = 5;
int dim = 3;
int vdim = 3;
int i = 0;
long time = 0;
long lastTime = 0;
Scanner scan = new Scanner(System.in);
try {
GLFWContext.init();
if(render){
GLFWContext.init();
//init shader program
Mesh.initShaderProgram();
//init shader program
Mesh.initShaderProgram();
}
FluidSim[][][] simArray = initFluidSim(dim,1,dim);
FluidSim[][][] simArray = FluidSim.initFluidSim(dim,vdim,dim);
Mesh[][][] meshArray = initMeshes(dim,1,dim,simArray);
Mesh[][][] meshArray = null;
if(render){
meshArray = initMeshes(dim,vdim,dim,simArray);
}
//uncomment this to generate test data
// generateTestData();
while(true){
try {
@ -55,23 +65,27 @@ public class Main {
//
//Simulate
//
FluidSim.simChunks(simArray,i,0.01f);
time = time + (System.currentTimeMillis() - lastTime);
FluidSim.simChunks(simArray,i,TIMESTEP);
//
//Remesh
//
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++){
meshArray[x][y][z].remesh();
if(render){
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++){
meshArray[x][y][z].remesh();
}
}
}
}
time = time + (System.currentTimeMillis() - lastTime);
//redraw
GLFWContext.redraw(meshArray);
if(render){
GLFWContext.redraw(meshArray);
}
i++;
if(i == 100){
System.out.println(time / 100.0);
System.out.println("overall time: " + time / 100.0);
}
if(i > 3){
// scan.next();
@ -82,45 +96,6 @@ public class Main {
}
}
private static FluidSim[][][] initFluidSim(int dimx, int dimy, int dimz){
FluidSim[][][] simArray = new FluidSim[dimx][dimy][dimz];
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] = new FluidSim();
simArray[x][y][z].setup(new Vector3i(x,y,z));
}
}
}
//set sim adjacencies
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++){
FluidSim current = simArray[x][y][z];
for(int i = -1; i < 2; i++){
for(int j = -1; j < 2; j++){
for(int k = -1; k < 2; k++){
if(i == j && j == k && k == 0){
continue;
}
if(
0 <= x + i && x + i < simArray.length &&
0 <= y + j && y + j < simArray[0].length &&
0 <= z + k && z + k < simArray[0][0].length
){
current.setNeighbor(i+1,j+1,k+1,simArray[x+i][y+j][z+k]);
}
}
}
}
}
}
}
return simArray;
}
private static Mesh[][][] initMeshes(int dimx, int dimy, int dimz, FluidSim[][][] simArray){
Mesh[][][] meshArray = new Mesh[dimx][dimy][dimz];
for(int x = 0; x < simArray.length; x++){
@ -134,4 +109,126 @@ public class Main {
return meshArray;
}
/**
* Generates test data on disk in the test resources folder
*/
private static void generateTestData(){
//variables that will be used in generating data
int i = 0;
int dim = 1;
int length = 500;
FluidSim[][][] simArray;
int[] dims = new int[]{1,3,5};
int[] lengths = new int[]{1,50,100,500};
render = false;
//clean up existing data
//this is scary recursive file logic, but the innermost callback has a guard to guarantee the file is underneath the testdata path
while(Files.exists(new File("./src/test/resources/testdata").toPath())){
try {
Files.walk(new File("./src/test/resources/testdata").toPath()).forEach(path -> {
Path parent = new File("./src/test/resources/testdata").toPath().toAbsolutePath();
Path child = path.toAbsolutePath();
try {
TimeUnit.MILLISECONDS.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(child.startsWith(parent)){
try {
System.out.println("Deleting " + path);
Files.delete(path);
} catch (IOException e) {
System.out.println("Failed to delete " + path);
e.printStackTrace();
}
}
});
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//create folders to contain test data
try {
Files.createDirectory(new File("./src/test/resources/testdata").toPath());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(int dimIterator = 0; dimIterator < dims.length; dimIterator++){
dim = dims[dimIterator];
try {
Files.createDirectory(new File("./src/test/resources/testdata/" + dim + "by" + dim).toPath());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(int lengthIterator = 0; lengthIterator < lengths.length; lengthIterator++){
length = lengths[lengthIterator];
try {
Files.createDirectory(new File("./src/test/resources/testdata/" + dim + "by" + dim + "/" + length + "steps").toPath());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//main data generation loop
for(int dimIterator = 0; dimIterator < dims.length; dimIterator++){
for(int lengthIterator = 0; lengthIterator < lengths.length; lengthIterator++){
//actual main routine to generate data starts here
dim = dims[dimIterator];
length = lengths[lengthIterator];
System.out.println("Generating " + dim + "x" + dim + "x" + dim + " for " + length + " steps");
i = 0;
simArray = FluidSim.initFluidSim(dim,dim,dim);
for(i = 0; i < length; i++){
FluidSim.simChunks(simArray,i,TIMESTEP);
}
for(int x = 0; x < dim; x++){
for(int y = 0; y < dim; y++){
for(int z = 0; z < dim; z++){
simArray[x][y][z].dumpToDisk("./src/test/resources/testdata/" + dim + "by" + dim + "/" + length + "steps/chunk_" + x + "_" + y + "_" + z + "_" + dim + "by" + dim + "Chunk" + length + "Step.data");
}
}
}
for(int x = 0; x < dim; x++){
for(int y = 0; y < dim; y++){
for(int z = 0; z < dim; z++){
byte[] bytes;
try {
bytes = Files.readAllBytes(new File("./src/test/resources/testdata/" + dim + "by" + dim + "/" + length + "steps/chunk_" + x + "_" + y + "_" + z + "_" + dim + "by" + dim + "Chunk" + length + "Step.data").toPath());
ByteBuffer densityBytes = simArray[0][0][0].getDensityBuffer();
i = 0;
while(densityBytes.hasRemaining()){
boolean pass = bytes[i] == densityBytes.get();
if(!pass){
System.err.println("failed to pass!");
}
i++;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}
System.exit(0);
}
}

View File

@ -2,7 +2,6 @@ package electrosphere.render;
import java.nio.IntBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.opengl.GL;
import org.lwjgl.opengl.GL44;
@ -38,9 +37,6 @@ public class GLFWContext {
GLFW.glfwMakeContextCurrent(window);
//Maximize it
GLFW.glfwMaximizeWindow(window);
//grab actual framebuffer
int bufferWidth = 0;
int bufferHeight = 0;
try(MemoryStack stack = MemoryStack.stackPush()){
IntBuffer xBuffer = MemoryUtil.memAllocInt(1);
@ -48,9 +44,6 @@ public class GLFWContext {
GLFW.glfwGetFramebufferSize(window, xBuffer, yBuffer);
MemoryUtil.memFree(xBuffer);
MemoryUtil.memFree(yBuffer);
bufferWidth = xBuffer.get();
bufferHeight = yBuffer.get();
}
//

View File

@ -1,11 +1,8 @@
package electrosphere.render;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.file.Files;
@ -17,11 +14,8 @@ import java.util.Map;
import org.joml.Matrix4f;
import org.joml.Vector3f;
import org.joml.Vector3i;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL33;
import org.lwjgl.opengl.GL44;
import org.lwjgl.opengl.GL45;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.system.MemoryUtil;
import electrosphere.FluidSim;
@ -110,7 +104,6 @@ public class Mesh {
FloatBuffer NormalArrayBufferData;
if(normalCount > 0){
NormalArrayBufferData = MemoryUtil.memAllocFloat(normalCount * 3);
float[] temp = new float[3];
for(float normalValue : data.normals){
NormalArrayBufferData.put(normalValue);
}
@ -242,8 +235,7 @@ public class Mesh {
public static void initShaderProgram(){
String vsSrc = "";
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
try (BufferedReader is = new BufferedReader(Files.newBufferedReader(new File("C:\\Users\\satellite\\Documents\\fluid-sim\\src\\main\\resources\\shader.vs").toPath()))){
try (BufferedReader is = new BufferedReader(Files.newBufferedReader(new File("./src/main/resources/shader.vs").toPath()))){
String temp;
while((temp = is.readLine())!=null){
vsSrc = vsSrc + temp + "\n";
@ -254,7 +246,7 @@ public class Mesh {
}
String fsSrc = "";
try (BufferedReader is = new BufferedReader(Files.newBufferedReader(new File("C:\\Users\\satellite\\Documents\\fluid-sim\\src\\main\\resources\\shader.fs").toPath()))){
try (BufferedReader is = new BufferedReader(Files.newBufferedReader(new File("./src/main/resources/shader.fs").toPath()))){
String temp;
while((temp = is.readLine())!=null){
fsSrc = fsSrc + temp + "\n";

View File

@ -10,6 +10,7 @@ vec3 dLDiffuse = vec3(0.5,0.5,0.5);
in vec3 FragPos;
in vec3 Normal;
in vec3 rawPos;
uniform vec3 viewPos;
@ -26,7 +27,15 @@ void main(){
vec3 lightAmount = CalcDirLight(norm, viewDir);
vec3 color = vec3(0.3,0.7,0.9) * lightAmount;
vec3 color = vec3(0.3,0.7,0.9);
if(
rawPos.x < 2 || rawPos.x > 16 ||
rawPos.y < 2 || rawPos.y > 16 ||
rawPos.z < 2 || rawPos.z > 16
){
color = vec3(0.9,0.7,0.3);
}
color = color * lightAmount;
//this final calculation is for transparency
FragColor = vec4(color,1.0);

View File

@ -16,6 +16,7 @@ uniform mat4 projection;
//output buffers
out vec3 Normal;
out vec3 FragPos;
out vec3 rawPos;
@ -29,6 +30,7 @@ void main() {
//push frag, normal, and texture positions to fragment shader
FragPos = vec3(model * FinalVertex);
Normal = mat3(transpose(inverse(model))) * aNormal;
rawPos = aPos;
//set final position with opengl space

View File

@ -0,0 +1,185 @@
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import org.junit.Test;
import electrosphere.FluidSim;
import electrosphere.Main;
public class LongRunTests {
// @Test
// public void test5by5Chunk1Step(){
// int dim = 5;
// int maxTimestep = 1;
// System.out.println("TEST: " + dim + "x" + dim + "x" + dim + " for " + maxTimestep + " steps");
// //init chunk array
// FluidSim[][][] simArray = FluidSim.initFluidSim(dim,dim,dim);
// //simulate the chunk
// for(int i = 0; i < maxTimestep; i++){
// FluidSim.simChunks(simArray, i, Main.TIMESTEP);
// }
// for(int x = 0; x < dim; x++){
// for(int y = 0; y < dim; y++){
// for(int z = 0; z < dim; z++){
// InputStream testFileIS = this.getClass().getResourceAsStream("./testdata/" + dim + "by" + dim + "/" + maxTimestep + "steps/chunk_" + x + "_" + y + "_" + z + "_" + dim + "by" + dim + "Chunk" + maxTimestep + "Step.data");
// byte[] bytes;
// try {
// bytes = testFileIS.readAllBytes();
// ByteBuffer fromDiskBuffer = ByteBuffer.allocate(FluidSim.DIM * FluidSim.DIM * FluidSim.DIM * 4);
// fromDiskBuffer.order(ByteOrder.LITTLE_ENDIAN);
// fromDiskBuffer.put(bytes);
// fromDiskBuffer.flip();
// ByteBuffer densityBytes = simArray[x][y][z].getDensityBuffer();
// int i = 0;
// while(densityBytes.hasRemaining()){
// boolean pass = fromDiskBuffer.get() == densityBytes.get();
// assert(pass);
// i++;
// }
// } catch (IOException e) {
// e.printStackTrace();
// assert(false);
// }
// }
// }
// }
// System.out.println("PASSED");
// }
// @Test
// public void test5by5Chunk50Step(){
// int dim = 5;
// int maxTimestep = 50;
// System.out.println("TEST: " + dim + "x" + dim + "x" + dim + " for " + maxTimestep + " steps");
// //init chunk array
// FluidSim[][][] simArray = FluidSim.initFluidSim(dim,dim,dim);
// //simulate the chunk
// for(int i = 0; i < maxTimestep; i++){
// FluidSim.simChunks(simArray, i, Main.TIMESTEP);
// }
// for(int x = 0; x < dim; x++){
// for(int y = 0; y < dim; y++){
// for(int z = 0; z < dim; z++){
// InputStream testFileIS = this.getClass().getResourceAsStream("./testdata/" + dim + "by" + dim + "/" + maxTimestep + "steps/chunk_" + x + "_" + y + "_" + z + "_" + dim + "by" + dim + "Chunk" + maxTimestep + "Step.data");
// byte[] bytes;
// try {
// bytes = testFileIS.readAllBytes();
// ByteBuffer densityBytes = simArray[x][y][z].getDensityBuffer();
// int i = 0;
// while(densityBytes.hasRemaining()){
// boolean pass = bytes[i] == densityBytes.get();
// assert(pass);
// i++;
// }
// } catch (IOException e) {
// e.printStackTrace();
// assert(false);
// }
// }
// }
// }
// System.out.println("PASSED");
// }
// @Test
// public void test5by5Chunk100Step(){
// int dim = 5;
// int maxTimestep = 100;
// System.out.println("TEST: " + dim + "x" + dim + "x" + dim + " for " + maxTimestep + " steps");
// //init chunk array
// FluidSim[][][] simArray = FluidSim.initFluidSim(dim,dim,dim);
// //simulate the chunk
// for(int i = 0; i < maxTimestep; i++){
// FluidSim.simChunks(simArray, i, Main.TIMESTEP);
// }
// for(int x = 0; x < dim; x++){
// for(int y = 0; y < dim; y++){
// for(int z = 0; z < dim; z++){
// InputStream testFileIS = this.getClass().getResourceAsStream("./testdata/" + dim + "by" + dim + "/" + maxTimestep + "steps/chunk_" + x + "_" + y + "_" + z + "_" + dim + "by" + dim + "Chunk" + maxTimestep + "Step.data");
// byte[] bytes;
// try {
// bytes = testFileIS.readAllBytes();
// ByteBuffer densityBytes = simArray[x][y][z].getDensityBuffer();
// int i = 0;
// while(densityBytes.hasRemaining()){
// boolean pass = bytes[i] == densityBytes.get();
// assert(pass);
// i++;
// }
// } catch (IOException e) {
// e.printStackTrace();
// assert(false);
// }
// }
// }
// }
// System.out.println("PASSED");
// }
// @Test
// public void test5by5Chunk500Step(){
// int dim = 5;
// int maxTimestep = 500;
// System.out.println("TEST: " + dim + "x" + dim + "x" + dim + " for " + maxTimestep + " steps");
// //init chunk array
// FluidSim[][][] simArray = FluidSim.initFluidSim(dim,dim,dim);
// //simulate the chunk
// for(int i = 0; i < maxTimestep; i++){
// FluidSim.simChunks(simArray, i, Main.TIMESTEP);
// }
// for(int x = 0; x < dim; x++){
// for(int y = 0; y < dim; y++){
// for(int z = 0; z < dim; z++){
// InputStream testFileIS = this.getClass().getResourceAsStream("./testdata/" + dim + "by" + dim + "/" + maxTimestep + "steps/chunk_" + x + "_" + y + "_" + z + "_" + dim + "by" + dim + "Chunk" + maxTimestep + "Step.data");
// byte[] bytes;
// try {
// bytes = testFileIS.readAllBytes();
// ByteBuffer densityBytes = simArray[x][y][z].getDensityBuffer();
// int i = 0;
// while(densityBytes.hasRemaining()){
// boolean pass = bytes[i] == densityBytes.get();
// assert(pass);
// i++;
// }
// } catch (IOException e) {
// e.printStackTrace();
// assert(false);
// }
// }
// }
// }
// System.out.println("PASSED");
// }
}

View File

@ -0,0 +1,197 @@
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import org.junit.Test;
import electrosphere.FluidSim;
import electrosphere.Main;
public class MediumRunTests {
// @Test
// public void test3by3Chunk1Step(){
// int dim = 3;
// int maxTimestep = 1;
// System.out.println("TEST: " + dim + "x" + dim + "x" + dim + " for " + maxTimestep + " steps");
// //init chunk array
// FluidSim[][][] simArray = FluidSim.initFluidSim(dim,dim,dim);
// //simulate the chunk
// for(int i = 0; i < maxTimestep; i++){
// FluidSim.simChunks(simArray, i, Main.TIMESTEP);
// }
// for(int x = 0; x < dim; x++){
// for(int y = 0; y < dim; y++){
// for(int z = 0; z < dim; z++){
// InputStream testFileIS = this.getClass().getResourceAsStream("./testdata/" + dim + "by" + dim + "/" + maxTimestep + "steps/chunk_" + x + "_" + y + "_" + z + "_" + dim + "by" + dim + "Chunk" + maxTimestep + "Step.data");
// byte[] bytes;
// try {
// bytes = testFileIS.readAllBytes();
// ByteBuffer fromDiskBuffer = ByteBuffer.allocate(FluidSim.DIM * FluidSim.DIM * FluidSim.DIM * 4);
// fromDiskBuffer.order(ByteOrder.LITTLE_ENDIAN);
// fromDiskBuffer.put(bytes);
// fromDiskBuffer.flip();
// ByteBuffer densityBytes = simArray[x][y][z].getDensityBuffer();
// int i = 0;
// while(densityBytes.hasRemaining()){
// boolean pass = fromDiskBuffer.get() == densityBytes.get();
// assert(pass);
// i++;
// }
// } catch (IOException e) {
// e.printStackTrace();
// assert(false);
// }
// }
// }
// }
// System.out.println("PASSED");
// }
// @Test
// public void test3by3Chunk50Step(){
// int dim = 3;
// int maxTimestep = 50;
// System.out.println("TEST: " + dim + "x" + dim + "x" + dim + " for " + maxTimestep + " steps");
// //init chunk array
// FluidSim[][][] simArray = FluidSim.initFluidSim(dim,dim,dim);
// //simulate the chunk
// for(int i = 0; i < maxTimestep; i++){
// FluidSim.simChunks(simArray, i, Main.TIMESTEP);
// }
// for(int x = 0; x < dim; x++){
// for(int y = 0; y < dim; y++){
// for(int z = 0; z < dim; z++){
// InputStream testFileIS = this.getClass().getResourceAsStream("./testdata/" + dim + "by" + dim + "/" + maxTimestep + "steps/chunk_" + x + "_" + y + "_" + z + "_" + dim + "by" + dim + "Chunk" + maxTimestep + "Step.data");
// byte[] bytes;
// try {
// bytes = testFileIS.readAllBytes();
// ByteBuffer fromDiskBuffer = ByteBuffer.allocate(FluidSim.DIM * FluidSim.DIM * FluidSim.DIM * 4);
// fromDiskBuffer.order(ByteOrder.LITTLE_ENDIAN);
// fromDiskBuffer.put(bytes);
// fromDiskBuffer.flip();
// ByteBuffer densityBytes = simArray[x][y][z].getDensityBuffer();
// int i = 0;
// while(densityBytes.hasRemaining()){
// boolean pass = fromDiskBuffer.get() == densityBytes.get();
// assert(pass);
// i++;
// }
// } catch (IOException e) {
// e.printStackTrace();
// assert(false);
// }
// }
// }
// }
// System.out.println("PASSED");
// }
// @Test
// public void test3by3Chunk100Step(){
// int dim = 3;
// int maxTimestep = 100;
// System.out.println("TEST: " + dim + "x" + dim + "x" + dim + " for " + maxTimestep + " steps");
// //init chunk array
// FluidSim[][][] simArray = FluidSim.initFluidSim(dim,dim,dim);
// //simulate the chunk
// for(int i = 0; i < maxTimestep; i++){
// FluidSim.simChunks(simArray, i, Main.TIMESTEP);
// }
// for(int x = 0; x < dim; x++){
// for(int y = 0; y < dim; y++){
// for(int z = 0; z < dim; z++){
// InputStream testFileIS = this.getClass().getResourceAsStream("./testdata/" + dim + "by" + dim + "/" + maxTimestep + "steps/chunk_" + x + "_" + y + "_" + z + "_" + dim + "by" + dim + "Chunk" + maxTimestep + "Step.data");
// byte[] bytes;
// try {
// bytes = testFileIS.readAllBytes();
// ByteBuffer fromDiskBuffer = ByteBuffer.allocate(FluidSim.DIM * FluidSim.DIM * FluidSim.DIM * 4);
// fromDiskBuffer.order(ByteOrder.LITTLE_ENDIAN);
// fromDiskBuffer.put(bytes);
// fromDiskBuffer.flip();
// ByteBuffer densityBytes = simArray[x][y][z].getDensityBuffer();
// int i = 0;
// while(densityBytes.hasRemaining()){
// boolean pass = fromDiskBuffer.get() == densityBytes.get();
// assert(pass);
// i++;
// }
// } catch (IOException e) {
// e.printStackTrace();
// assert(false);
// }
// }
// }
// }
// System.out.println("PASSED");
// }
// @Test
// public void test3by3Chunk500Step(){
// int dim = 3;
// int maxTimestep = 500;
// System.out.println("TEST: " + dim + "x" + dim + "x" + dim + " for " + maxTimestep + " steps");
// //init chunk array
// FluidSim[][][] simArray = FluidSim.initFluidSim(dim,dim,dim);
// //simulate the chunk
// for(int i = 0; i < maxTimestep; i++){
// FluidSim.simChunks(simArray, i, Main.TIMESTEP);
// }
// for(int x = 0; x < dim; x++){
// for(int y = 0; y < dim; y++){
// for(int z = 0; z < dim; z++){
// InputStream testFileIS = this.getClass().getResourceAsStream("./testdata/" + dim + "by" + dim + "/" + maxTimestep + "steps/chunk_" + x + "_" + y + "_" + z + "_" + dim + "by" + dim + "Chunk" + maxTimestep + "Step.data");
// byte[] bytes;
// try {
// bytes = testFileIS.readAllBytes();
// ByteBuffer fromDiskBuffer = ByteBuffer.allocate(FluidSim.DIM * FluidSim.DIM * FluidSim.DIM * 4);
// fromDiskBuffer.order(ByteOrder.LITTLE_ENDIAN);
// fromDiskBuffer.put(bytes);
// fromDiskBuffer.flip();
// ByteBuffer densityBytes = simArray[x][y][z].getDensityBuffer();
// int i = 0;
// while(densityBytes.hasRemaining()){
// boolean pass = fromDiskBuffer.get() == densityBytes.get();
// assert(pass);
// i++;
// }
// } catch (IOException e) {
// e.printStackTrace();
// assert(false);
// }
// }
// }
// }
// System.out.println("PASSED");
// }
}

View File

@ -0,0 +1,202 @@
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import org.junit.Test;
import electrosphere.FluidSim;
import electrosphere.Main;
/**
* Tests stepping a single chunk by a single frame
*/
public class ShortRunTest {
// @Test
// public void test1by1Chunk1Step(){
// int dim = 1;
// int maxTimestep = 1;
// System.out.println("TEST: " + dim + "x" + dim + "x" + dim + " for " + maxTimestep + " steps");
// //init chunk array
// FluidSim[][][] simArray = FluidSim.initFluidSim(dim,dim,dim);
// //simulate the chunk
// for(int i = 0; i < maxTimestep; i++){
// FluidSim.simChunks(simArray, i, Main.TIMESTEP);
// }
// for(int x = 0; x < dim; x++){
// for(int y = 0; y < dim; y++){
// for(int z = 0; z < dim; z++){
// InputStream testFileIS = this.getClass().getResourceAsStream("./testdata/" + dim + "by" + dim + "/" + maxTimestep + "steps/chunk_" + x + "_" + y + "_" + z + "_" + dim + "by" + dim + "Chunk" + maxTimestep + "Step.data");
// byte[] bytes;
// try {
// bytes = testFileIS.readAllBytes();
// ByteBuffer fromDiskBuffer = ByteBuffer.allocate(FluidSim.DIM * FluidSim.DIM * FluidSim.DIM * 4);
// fromDiskBuffer.order(ByteOrder.LITTLE_ENDIAN);
// fromDiskBuffer.put(bytes);
// fromDiskBuffer.flip();
// ByteBuffer densityBytes = simArray[x][y][z].getDensityBuffer();
// int i = 0;
// while(densityBytes.hasRemaining()){
// boolean pass = fromDiskBuffer.get() == densityBytes.get();
// assert(pass);
// i++;
// }
// } catch (IOException e) {
// e.printStackTrace();
// assert(false);
// }
// }
// }
// }
// System.out.println("PASSED");
// }
// @Test
// public void test1by1Chunk50Step(){
// int dim = 1;
// int maxTimestep = 50;
// System.out.println("TEST: " + dim + "x" + dim + "x" + dim + " for " + maxTimestep + " steps");
// //init chunk array
// FluidSim[][][] simArray = FluidSim.initFluidSim(dim,dim,dim);
// //simulate the chunk
// for(int i = 0; i < maxTimestep; i++){
// FluidSim.simChunks(simArray, i, Main.TIMESTEP);
// }
// for(int x = 0; x < dim; x++){
// for(int y = 0; y < dim; y++){
// for(int z = 0; z < dim; z++){
// InputStream testFileIS = this.getClass().getResourceAsStream("./testdata/" + dim + "by" + dim + "/" + maxTimestep + "steps/chunk_" + x + "_" + y + "_" + z + "_" + dim + "by" + dim + "Chunk" + maxTimestep + "Step.data");
// byte[] bytes;
// try {
// bytes = testFileIS.readAllBytes();
// ByteBuffer fromDiskBuffer = ByteBuffer.allocate(FluidSim.DIM * FluidSim.DIM * FluidSim.DIM * 4);
// fromDiskBuffer.order(ByteOrder.LITTLE_ENDIAN);
// fromDiskBuffer.put(bytes);
// fromDiskBuffer.flip();
// ByteBuffer densityBytes = simArray[x][y][z].getDensityBuffer();
// int i = 0;
// while(densityBytes.hasRemaining()){
// boolean pass = fromDiskBuffer.get() == densityBytes.get();
// assert(pass);
// i++;
// }
// } catch (IOException e) {
// e.printStackTrace();
// assert(false);
// }
// }
// }
// }
// System.out.println("PASSED");
// }
// @Test
// public void test1by1Chunk100Step(){
// int dim = 1;
// int maxTimestep = 100;
// System.out.println("TEST: " + dim + "x" + dim + "x" + dim + " for " + maxTimestep + " steps");
// //init chunk array
// FluidSim[][][] simArray = FluidSim.initFluidSim(dim,dim,dim);
// //simulate the chunk
// for(int i = 0; i < maxTimestep; i++){
// FluidSim.simChunks(simArray, i, Main.TIMESTEP);
// }
// for(int x = 0; x < dim; x++){
// for(int y = 0; y < dim; y++){
// for(int z = 0; z < dim; z++){
// InputStream testFileIS = this.getClass().getResourceAsStream("./testdata/" + dim + "by" + dim + "/" + maxTimestep + "steps/chunk_" + x + "_" + y + "_" + z + "_" + dim + "by" + dim + "Chunk" + maxTimestep + "Step.data");
// byte[] bytes;
// try {
// bytes = testFileIS.readAllBytes();
// ByteBuffer fromDiskBuffer = ByteBuffer.allocate(FluidSim.DIM * FluidSim.DIM * FluidSim.DIM * 4);
// fromDiskBuffer.order(ByteOrder.LITTLE_ENDIAN);
// fromDiskBuffer.put(bytes);
// fromDiskBuffer.flip();
// ByteBuffer densityBytes = simArray[x][y][z].getDensityBuffer();
// int i = 0;
// while(densityBytes.hasRemaining()){
// boolean pass = fromDiskBuffer.get() == densityBytes.get();
// assert(pass);
// i++;
// }
// } catch (IOException e) {
// e.printStackTrace();
// assert(false);
// }
// }
// }
// }
// System.out.println("PASSED");
// }
// @Test
// public void test1by1Chunk500Step(){
// int dim = 1;
// int maxTimestep = 500;
// System.out.println("TEST: " + dim + "x" + dim + "x" + dim + " for " + maxTimestep + " steps");
// //init chunk array
// FluidSim[][][] simArray = FluidSim.initFluidSim(dim,dim,dim);
// //simulate the chunk
// for(int i = 0; i < maxTimestep; i++){
// FluidSim.simChunks(simArray, i, Main.TIMESTEP);
// }
// for(int x = 0; x < dim; x++){
// for(int y = 0; y < dim; y++){
// for(int z = 0; z < dim; z++){
// InputStream testFileIS = this.getClass().getResourceAsStream("./testdata/" + dim + "by" + dim + "/" + maxTimestep + "steps/chunk_" + x + "_" + y + "_" + z + "_" + dim + "by" + dim + "Chunk" + maxTimestep + "Step.data");
// byte[] bytes;
// try {
// bytes = testFileIS.readAllBytes();
// ByteBuffer fromDiskBuffer = ByteBuffer.allocate(FluidSim.DIM * FluidSim.DIM * FluidSim.DIM * 4);
// fromDiskBuffer.order(ByteOrder.LITTLE_ENDIAN);
// fromDiskBuffer.put(bytes);
// fromDiskBuffer.flip();
// ByteBuffer densityBytes = simArray[x][y][z].getDensityBuffer();
// int i = 0;
// while(densityBytes.hasRemaining()){
// boolean pass = fromDiskBuffer.get() == densityBytes.get();
// assert(pass);
// i++;
// }
// } catch (IOException e) {
// e.printStackTrace();
// assert(false);
// }
// }
// }
// }
// System.out.println("PASSED");
// }
}

View File

@ -0,0 +1,57 @@
#version 330 core
out vec4 FragColor;
vec3 color = vec3(0.3,0.7,0.9);
vec3 dLDirection = vec3(0.1,-0.9,0.1);
vec3 dLDiffuse = vec3(0.5,0.5,0.5);
in vec3 FragPos;
in vec3 Normal;
in vec3 rawPos;
uniform vec3 viewPos;
// uniform DirLight dirLight;
// uniform PointLight pointLights[NR_POINT_LIGHTS];
// uniform SpotLight spotLight;
// function prototypes
vec3 CalcDirLight(vec3 normal, vec3 viewDir);
void main(){
vec3 norm = normalize(Normal);
vec3 viewDir = normalize(viewPos - FragPos);
vec3 lightAmount = CalcDirLight(norm, viewDir);
vec3 color = vec3(0.3,0.7,0.9);
if(
rawPos.x < 2 || rawPos.x > 16 ||
rawPos.y < 2 || rawPos.y > 16 ||
rawPos.z < 2 || rawPos.z > 16
){
color = vec3(0.9,0.7,0.3);
}
color = color * lightAmount;
//this final calculation is for transparency
FragColor = vec4(color,1.0);
}
vec3 CalcDirLight(vec3 normal, vec3 viewDir){
vec3 lightDir = normalize(-dLDirection);
// diffuse shading
float diff = max(dot(normal, lightDir), 0.0);
// specular shading
vec3 reflectDir = reflect(-lightDir, normal);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 0.6);
// combine results
vec3 diffuse = dLDiffuse * diff;
vec3 specular = spec * color;
return diffuse + specular;
}

View File

@ -0,0 +1,38 @@
//Vertex Shader
#version 330 core
//input buffers
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormal;
//coordinate space transformation matrices
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
//output buffers
out vec3 Normal;
out vec3 FragPos;
out vec3 rawPos;
void main() {
//normalize posiiton and normal
vec4 FinalVertex = vec4(aPos, 1.0);
vec4 FinalNormal = vec4(aNormal, 1.0);
//push frag, normal, and texture positions to fragment shader
FragPos = vec3(model * FinalVertex);
Normal = mat3(transpose(inverse(model))) * aNormal;
rawPos = aPos;
//set final position with opengl space
gl_Position = projection * view * model * FinalVertex;
}

Some files were not shown because too many files have changed in this diff Show More