Permalink
Browse files

SPH not working yet, debugging densities and forces

  • Loading branch information...
1 parent 765d256 commit 149002ec6b492cee62c4e9e309598453359a0ee9 @hrvthzs committed Nov 21, 2011
View
@@ -46,6 +46,8 @@ CUFILES := \
# CUDA dependency files
CU_DEPS := \
+ src/boundary.cu \
+ src/boundary_walls.cu \
src/grid_utils.cu \
src/grid_kernel.cu \
src/sph_density.cu \
View
@@ -1,9 +1,26 @@
#version 150
in vec4 color;
+in vec2 vTextCoord;
out vec4 fragColor;
void main()
{
- fragColor = vec4(0.09, 0.31, 0.98, 1.0);
+ vec4 c = vec4(0.09, 0.31, 0.98, 1.0);
+
+ const vec3 lightDir = vec3(0.577, 0.577, 0.577);
+
+ // calculate normal from texture coordinates
+ //vec3 N;
+ //N.xy = vTextCoord.xy*vec2(2.0, -2.0) + vec2(-1.0, 1.0);
+ //float mag = dot(N.xy, N.xy);
+ //if (mag > 1.0) discard; // kill pixels outside circle
+ //N.z = sqrt(1.0-mag);
+
+ // calculate lighting
+ //float diffuse = max(1.0, dot(lightDir, N));
+
+
+ //fragColor = c * diffuse;
+ fragColor = c;
}
View
@@ -1,12 +1,18 @@
-#version 150
-
-
+#version 130
in vec4 position;
uniform mat4 mvp;
+uniform mat4 mv;
+uniform float pointRadius;
+uniform float pointScale;
+out vec2 vTexCoord;
void main()
{
+ vec3 posEye = vec3(mv * position);
+ float dist = length(posEye);
+ //vTexCoord = gl_MultiTexCoord0.xy;
+ gl_PointSize = pointScale / (pointRadius * dist);
gl_Position = mvp * vec4(position);
}
View
@@ -0,0 +1,83 @@
+#ifndef __BOUNDARY_CU__
+#define __BOUNDARY_CU__
+
+namespace Boundary {
+
+ //for collision detection
+ #define EPSILON 0.00001f
+
+
+ __device__ float3 calculateRepulsionForce(
+ float3 const& vel,
+ float3 const& normal,
+ float const& boundary_distance,
+ float const& boundary_dampening,
+ float const& boundary_stiffness
+ ) {
+
+ // from ama06
+ return (boundary_stiffness * boundary_distance - boundary_dampening * dot(normal, vel)) * normal;
+ }
+
+
+ /*
+ COLLISION RESPONSE
+ SIMPLE REFLECTION
+ ................
+ ....^...........
+ Vn..|.../.V.....
+ ....|../........
+ ....|./.........
+ ....|/________>.
+ ........Vt......
+ ................
+
+ Vn = (Vbc * n)Vbc
+ Vt = Vbc –vn
+ Vbc = velocity before collision
+ Vn = normal component of velocity
+ Vt == tangential component of velocity
+ V = (1-u)Vt – eVn
+ u = dynamic friction (affects tangent velocity)
+ e = resilience (affects normal velocity)
+ */
+
+ __device__ float3 calculateFrictionForce(
+ float3 const& vel,
+ float3 const& force,
+ float3 const& normal,
+ float const& friction_kinetic,
+ float const& friction_static_limit
+ )
+ {
+ float3 friction_force = make_float3(0,0,0);
+
+ // the normal part of the force vector (ie, the part that is going "towards" the boundary
+ float3 f_n = force * dot(normal, force);
+ // tangent on the terrain along the force direction (unit vector of tangential force)
+ float3 f_t = force - f_n;
+
+ // the normal part of the velocity vector (ie, the part that is going "towards" the boundary
+ float3 v_n = vel * dot(normal, vel);
+ // tangent on the terrain along the velocity direction (unit vector of tangential velocity)
+ float3 v_t = vel - v_n;
+
+ if((v_t.x + v_t.y + v_t.z)/3.0f > friction_static_limit)
+ friction_force = -v_t;
+ else
+ friction_force = friction_kinetic * -v_t;
+
+ // above static friction limit?
+ // friction_force.x = f_t.x > friction_static_limit ? friction_kinetic * -v_t.x : -v_t.x;
+ // friction_force.y = f_t.y > friction_static_limit ? friction_kinetic * -v_t.y : -v_t.y;
+ // friction_force.z = f_t.z > friction_static_limit ? friction_kinetic * -v_t.z : -v_t.z;
+
+ //TODO; friction should cause energy/heat in contact particles!
+ friction_force = friction_kinetic * -v_t;
+
+ return friction_force;
+ }
+
+};
+
+#endif // __BOUNDARY_CU__
View
@@ -0,0 +1,132 @@
+#ifndef __BOUNDARY_WALLS_CU__
+#define __BOUNDARY_WALLS_CU__
+
+#include "boundary.cu"
+
+namespace Boundary {
+
+ namespace Walls {
+
+ __device__ float3 calculateWallsNoPenetrationForce(
+ float3 const& pos,
+ float3 const& vel,
+ float3 const& grid_min,
+ float3 const& grid_max,
+ float const& boundary_distance,
+ float const& boundary_stiffness,
+ float const& boundary_dampening,
+ float const& scale_to_simulation)
+ {
+ float3 repulsion_force = make_float3(0,0,0);
+ float diff;
+
+ // simple limit for "wall" in Y direction (min of simulated volume)
+ diff = boundary_distance - ((pos.y - grid_min.y ) * scale_to_simulation);
+ if (diff > EPSILON) {
+ float3 normal = make_float3(0,1,0);
+ repulsion_force += calculateRepulsionForce(vel, normal, diff, boundary_dampening, boundary_stiffness);
+ }
+
+ // simple limit for "wall" in Y direction (max of simulated volume)
+ diff = boundary_distance - ((grid_max.y - pos.y ) * scale_to_simulation);
+ if (diff > EPSILON) {
+ float3 normal = make_float3(0,-1,0);
+ repulsion_force += calculateRepulsionForce(vel, normal, diff, boundary_dampening, boundary_stiffness);
+ }
+
+ // simple limit for "wall" in Z direction (min of simulated volume)
+ diff = boundary_distance - ((pos.z - grid_min.z ) * scale_to_simulation);
+ if (diff > EPSILON ) {
+ float3 normal = make_float3(0,0,1);
+ repulsion_force += calculateRepulsionForce(vel, normal, diff, boundary_dampening, boundary_stiffness);
+ }
+
+ // simple limit for "wall" in Z direction (max of simulated volume)
+ diff = boundary_distance - ((grid_max.z - pos.z ) * scale_to_simulation);
+ if (diff > EPSILON) {
+ float3 normal = make_float3(0,0,-1);
+ float adj = boundary_stiffness * diff - boundary_dampening * dot(normal, vel);
+ repulsion_force += adj * normal;
+ }
+
+ // simple limit for "wall" in X direction (min of simulated volume)
+ diff = boundary_distance - ((pos.x - grid_min.x ) * scale_to_simulation);
+ if (diff > EPSILON ) {
+ float3 normal = make_float3(1,0,0);
+ repulsion_force += calculateRepulsionForce(vel, normal, diff, boundary_dampening, boundary_stiffness);
+ }
+
+ // simple limit for "wall" in X direction (max of simulated volume)
+ diff = boundary_distance - ((grid_max.x - pos.x ) * scale_to_simulation);
+ if (diff > EPSILON) {
+ float3 normal = make_float3(-1,0,0);
+ repulsion_force += calculateRepulsionForce(vel, normal, diff, boundary_dampening, boundary_stiffness);
+ }
+
+ return repulsion_force;
+ }
+
+
+ __device__ float3 calculateWallsNoSlipForce(
+ float3 const& pos,
+ float3 const& vel,
+ float3 const& force,
+ float3 const& grid_min,
+ float3 const& grid_max,
+ float const& boundary_distance,
+ float const& friction_kinetic,
+ float const& friction_static_limit,
+ float const& scale_to_simulation)
+ {
+ float3 friction_force = make_float3(0,0,0);
+ float diff;
+
+ // simple limit for "wall" in Y direction (min of simulated volume)
+ diff = boundary_distance - ((pos.y - grid_min.y ) * scale_to_simulation);
+ if (diff > EPSILON) {
+ float3 normal = make_float3(0,1,0);
+ friction_force += calculateFrictionForce(vel, force, normal, friction_kinetic, friction_static_limit);
+ }
+
+ // simple limit for "wall" in Y direction (max of simulated volume)
+ diff = boundary_distance - ((grid_max.y - pos.y ) * scale_to_simulation);
+ if (diff > EPSILON) {
+ float3 normal = make_float3(0,-1,0);
+ friction_force += calculateFrictionForce(vel, force, normal, friction_kinetic, friction_static_limit);
+ }
+
+ // simple limit for "wall" in Z direction (min of simulated volume)
+ diff = boundary_distance - ((pos.z - grid_min.z ) * scale_to_simulation);
+ if (diff > EPSILON ) {
+ float3 normal = make_float3(0,0,1);
+ friction_force += calculateFrictionForce(vel, force, normal, friction_kinetic, friction_static_limit);
+ }
+
+ // simple limit for "wall" in Z direction (max of simulated volume)
+ diff = boundary_distance - ((grid_max.z - pos.z ) * scale_to_simulation);
+ if (diff > EPSILON) {
+ float3 normal = make_float3(0,0,-1);
+ friction_force += calculateFrictionForce(vel, force, normal, friction_kinetic, friction_static_limit);
+ }
+
+ // simple limit for "wall" in X direction (min of simulated volume)
+ diff = boundary_distance - ((pos.x - grid_min.x ) * scale_to_simulation);
+ if (diff > EPSILON ) {
+ float3 normal = make_float3(1,0,0);
+ friction_force += calculateFrictionForce(vel, force, normal, friction_kinetic, friction_static_limit);
+ }
+
+ // simple limit for "wall" in X direction (max of simulated volume)
+ diff = boundary_distance - ((grid_max.x - pos.x ) * scale_to_simulation);
+ if (diff > EPSILON) {
+ float3 normal = make_float3(-1,0,0);
+ friction_force += calculateFrictionForce(vel, force, normal, friction_kinetic, friction_static_limit);
+ }
+
+ return friction_force;
+ }
+
+ };
+};
+
+#endif // __BOUNDARY_WALLS_CU__
View
@@ -28,12 +28,11 @@
typedef struct sGridData GridData;
typedef struct sGridParams GridParams;
- __constant__ GridParams cudaGridParams;
-
/**
* If cell is empty in grid this value idicates it
*/
#define EMPTY_CELL_VALUE 0xffffffff
+ __constant__ GridParams cudaGridParams;
#endif // __GRID_H__
View
@@ -185,7 +185,7 @@ namespace Grid {
////////////////////////////////////////////////////////////////////////////
void Uniform::_calcParams(float cellSize, float gridSize) {
- this->_params.min = make_float3(0.0, 0.0, 0.0);
+ this->_params.min = make_float3(-1.0f, -1.0f, -1.0f);
this->_params.max =
make_float3(
this->_params.min.x + gridSize,
@@ -266,9 +266,14 @@ namespace Grid {
std::cout
<< "Delta["
- << _params.delta.x << ", "
- << _params.delta.y << ", "
- << _params.delta.z << "]"
+ << this->_params.delta.x << ", "
+ << this->_params.delta.y << ", "
+ << this->_params.delta.z << "]"
+ << std::endl;
+
+ std::cout
+ << "Num cells: "
+ << this->_numCells
<< std::endl;
}
View
@@ -13,7 +13,8 @@ __global__ void kernel(float4* pos, unsigned int width, unsigned int height, flo
float v = y / (float) height;
u = u*2.0f - 1.0f;
v = v*2.0f - 1.0f;
-;
+ //u *= 0.5f;
+ //v *= 0.5f;
// calculate simple sine wave pattern
float freq = 4.0f;
float w = sinf(u*freq + time) * cosf(v*freq + time) * 0.5f;
View
@@ -69,7 +69,7 @@ struct integrate_functor
pos = normalize(pos) * 0.99;
float velL = length(vel);
vel -= (pos / (len / velL));
- vel *= boundaryDamping;
+ vel *= -boundaryDamping;
}
// store new position and velocity
Oops, something went wrong.

0 comments on commit 149002e

Please sign in to comment.