Permalink
Browse files

mock testing

  • Loading branch information...
1 parent 7fa4cb7 commit 4c8e6426af28166368caee9cb824a6cbde2e87f5 @Ilink committed Apr 25, 2012
Showing with 74 additions and 19 deletions.
  1. +19 −2 density.js
  2. +16 −4 fluid.js
  3. +14 −8 fluid_core.js
  4. +22 −3 main.js
  5. +3 −2 velocity.js
View
@@ -5,16 +5,33 @@ var Density = function(args){
// x is density
// u, v are velocity vectors
this.step = function(u, v, x, x0, diff, dt){
- self.increase(size, x, x0, dt);
+// self.increase(size, x, x0, dt);
self.swap(x, x0);
+ render_numbers("#container",x0);
+
self.diffuse(size, 1, x, x0, diff, dt);
self.swap(x, x0);
+
self.advect(size, 1, x, x0, u, v, dt);
+// console.log(size, x, x0, u, v, dt);
+
}
}
Density.prototype.set_boundary = Fluid_core.prototype.set_boundary;
Density.prototype.diffuse = Fluid_core.prototype.diffuse;
Density.prototype.advect = Fluid_core.prototype.advect;
Density.prototype.increase = Fluid_core.prototype.increase;
-Density.prototype.swap = Util.prototype.swap;
+Density.prototype.swap = Util.prototype.swap;
+
+var render_numbers = function(selector, arr){
+ var size = arr.length;
+ selector = $(selector);
+ selector.empty();
+ for(var i = 0; i < size; i++){
+ for(var j = 0; j < size; j++){
+ selector.append(arr[i][j]);
+ }
+ selector.append('<br/>')
+ }
+}
View
@@ -2,17 +2,23 @@ var Fluid = function(args){
var self = this;
var size, dim, dens, dens0, u, v, u0, v0, visc, diff;
dens = [], dens0 = [], u = [], v = [], v0 = [], u0 = [];
+ diff = 2;
+ visc = 2;
dim = (args.size + 2) * (args.size + 2);
- size = args.size;
+ var N = args.size;
+ size = args.size + 2;
visc = 1;
- var velocity = new Velocity({size: size});
- var density = new Density({size: size});
+ var velocity = new Velocity({size: N});
+ var density = new Density({size: N});
var solve = function(dens0, u0, v0, dt){
+// console.log('dens before vel', dens0);
velocity.step(u, v, u0, v0, visc, dt);
+// console.log('dens after vel', dens0);
density.step(u, v, dens, dens0, diff, dt);
+ console.log('dens after dens', dens);
return dens; // i think? the renderer will handle it just fine
}
@@ -42,7 +48,13 @@ var Fluid = function(args){
// does it need to be scaled by time like add_source?
}
- this.add_velocity = function(){
+ this.add_velocity = function(new_u, new_v){
+ for(var i = 0; i < size; i++){
+ for(var j = 0; j < size; j++){
+ u[i][j] += new_u[i][j];
+ v[i][j] += new_v[i][j];
+ }
+ }
// idk, add velocity to u and v here
// add helpers to make it easier to produce u and v values from velocity
}
View
@@ -8,6 +8,7 @@ var Fluid_core = function(){}
// todo: create a strategy pattern for various boundary condition types
Fluid_core.prototype.set_boundary = function(size, bound, matrix){
+// console.log(size, bound, matrix);
for (var i=1; i < size; i++ ) {
matrix[0][i] = (bound==1) ? -matrix[1][i] : matrix[1][i];
matrix[size-1][i] = bound==1 ? -matrix[size-1][i] : matrix[size-1][i];
@@ -16,9 +17,9 @@ Fluid_core.prototype.set_boundary = function(size, bound, matrix){
}
// corner cases (literally)
matrix[0][0] = 0.5*(matrix[1][0] + matrix[0][1]);
-// matrix[0][size-1] = 0.5*(matrix[i][size-1]+ matrix[0][size-1]);
- matrix[size-1][0] = 0.5*(matrix[size-1][0]+matrix[size-1][1]);
- matrix[size-1][size-1] = 0.5*(matrix[size-1][size-1]+matrix[size-1][size-1]);
+ matrix[0][size+1] = 0.5*(matrix[i][size+1]+ matrix[0][size]);
+// matrix[size+1][0] = 0.5*(matrix[size][0]+matrix[size+1][1]);
+// matrix[size+1][size+1] = 0.5*(matrix[size][size+1]+matrix[size+1][size]);
}
Fluid_core.prototype.infinite_boundary = function(size, bound, matrix){
@@ -38,8 +39,9 @@ Fluid_core.prototype.infinite_boundary = function(size, bound, matrix){
Fluid_core.prototype.advect = function(size, bound, field, field0, u, v, dt){
var i, j, i0, j0, i1, j1, x, y, s0, t0, s1, t1, dt0;
dt0 = dt * size;
- for (i=1; i < size; i++) {
- for (j=1; j < size; j++) {
+ size -= 1;
+ for (i=1; i < size-1; i++) {
+ for (j=1; j < size-1; j++) {
// find the new position for our particles
x = i - dt0 * u[i][j];
y = j - dt0 * v[i][j];
@@ -55,8 +57,11 @@ Fluid_core.prototype.advect = function(size, bound, field, field0, u, v, dt){
t1 = y-j0; t0 = 1-t1;
// carry over the values (densities, velocities)
- field[i][j] = s0 * (t0*field0[i0][j0] + t1*field0[i0][j1])+
- s1 * (t0*field0[i1][j0] + t1*field0[i1][j1]);
+// field[i1][j0] and field[i1][j1] are out of bounds
+
+ field[i][j] = s0 * (t0*field0[i0][j0] + t1*field0[i0][j1]) +
+ s1 * (t0*field0[i1][j0] + t1*field0[i1][j1]);
+ if(isNaN(field0[i][j]))console.log(i0, j0, i1, j1);
}
}
Fluid_core.prototype.set_boundary ( size, bound, field );
@@ -83,8 +88,9 @@ Fluid_core.prototype.increase = function(size, dens, new_dens, dt){
Fluid_core.prototype.diffuse = function(size, bound, dens, dens0, rate, dt){
// var a = dt * rate * size;
var a = dt * rate * size * size;
+// var a = dt * 2 * size * size;
for (var k = 0; k < 20; k++ ) {
- for (var i = 1; i < size-1; i++ ) {
+ for (var i = 1; i <= size-1; i++ ) {
for (var j = 1; j < size-1; j++ ) {
// i think it is using the previous density (d0) because this process is iterative (see outermost loop)
// could dens0 be part of the function instead of an argument?
View
25 main.js
@@ -53,6 +53,7 @@ var simple_interaction = function(canvas, bodies){
var render_numbers = function(selector, arr){
var size = arr.length;
selector = $(selector);
+ selector.empty();
for(var i = 0; i < size; i++){
for(var j = 0; j < size; j++){
selector.append(arr[i][j]);
@@ -61,13 +62,25 @@ var render_numbers = function(selector, arr){
}
}
+var populate_array = function(arr, min, max){
+ for(var i = 0; i < arr.length; i++){
+ for(var j = 0; j < arr[i].length; j++){
+ arr[i][j] = Math.floor(Math.random() * max) + min
+ }
+ }
+}
+
$(document).ready(function(){
var size = 10;
var mock_dens = [];
- mock_dens = Util.prototype.init_2d_arr(size, mock_dens);
+ mock_dens = Util.prototype.init_2d_arr(size+2, mock_dens);
mock_dens[0][5] = 5;
- console.log(mock_dens);
+
+ var mock_u = [];
+ var mock_v = [];
+ mock_u = Util.prototype.init_2d_arr(size+2, mock_u);
+ mock_v = Util.prototype.init_2d_arr(size+2, mock_v);
var screen_width = $(window).width();
var screen_height = $(window).height();
@@ -100,14 +113,20 @@ $(document).ready(function(){
var timeline = new Timeline({
tickrate: 10,
callback: function(dt){ // render loop goes here
+ populate_array(mock_u, 0, 3);
+ populate_array(mock_v, 0, 3);
+ populate_array(mock_dens, 0, 5);
+
fluid.add_density(mock_dens);
+ fluid.add_velocity(mock_u, mock_v);
fluid.tick(dt);
- render_numbers("#container", fluid.get_dens());
+// render_numbers("#container", fluid.get_dens());
}
});
$("#start").click(function(){
timeline.start();
+ timeline.stop();
});
$("#stop").click(function(){
View
@@ -52,12 +52,13 @@ var Velocity = function(args){
self.swap(v, v0); self.diffuse(size, 2, v, v0, visc, dt);
project(u, v, u0, v0);
self.swap(u, u0); self.swap(v, v0);
- self.advect(size, 1, u, u0, u0, v0, dt); self.advect(size, 1, v, v0, u0, v0, dt); // self-advection along each velocity vector
+ self.advect(size, 1, u, u0, u0, v0, dt);
+ self.advect(size, 1, v, v0, u0, v0, dt); // self-advection along each velocity vector
project(u, v, u0, v0);
}
}
Velocity.prototype.advect = Fluid_core.prototype.advect;
Velocity.prototype.diffuse = Fluid_core.prototype.diffuse;
Velocity.prototype.increase = Fluid_core.prototype.increase;
Velocity.prototype.swap = Util.prototype.swap;
-Velocity.prototype.set_boundary = Util.prototype.set_boundary;
+Velocity.prototype.set_boundary = Fluid_core.prototype.set_boundary;

0 comments on commit 4c8e642

Please sign in to comment.