-
Notifications
You must be signed in to change notification settings - Fork 2
/
asciiFluidSimulation.js
174 lines (150 loc) · 7.45 KB
/
asciiFluidSimulation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Generated by CoffeeScript 1.10.0
var CONSOLE_HEIGHT, CONSOLE_WIDTH, Particle, calculateDensities, calculateForces, gravity, loadExample, particles, pressure, simulationNextStep, totalOfParticles, viscosity;
CONSOLE_WIDTH = 80;
CONSOLE_HEIGHT = 24;
particles = [];
totalOfParticles = 0;
gravity = 1;
pressure = 4;
viscosity = 7;
Particle = (function() {
function Particle() {}
Particle.prototype.density = 0.0;
Particle.prototype.xForce = 0.0;
Particle.prototype.xVelocity = 0.0;
Particle.prototype.yForce = 0.0;
Particle.prototype.yVelocity = 0.0;
Particle.prototype.wallflag = 0;
Particle.prototype.dead = false;
Particle.prototype.xPos = 0.0;
Particle.prototype.yPos = 0.0;
return Particle;
})();
loadExample = function(selectedExample) {
var i, len, particlesCounter, ref, results, x, xSandboxAreaScan, ySandboxAreaScan;
if (selectedExample == null) {
selectedExample = document.getElementById("exampleSelection");
selectedExample = selectedExample.options[selectedExample.selectedIndex].value;
}
particles = [];
totalOfParticles = 0;
xSandboxAreaScan = 0;
ySandboxAreaScan = 0;
particlesCounter = 0;
ref = examples[selectedExample].data;
results = [];
for (i = 0, len = ref.length; i < len; i++) {
x = ref[i];
if (x === '\n') {
ySandboxAreaScan += 2;
xSandboxAreaScan = -1;
} else if (x !== ' ') {
particles.push(new Particle());
particles.push(new Particle());
if (x === "#") {
particles[particlesCounter].wallflag = particles[particlesCounter + 1].wallflag = 1;
}
particles[particlesCounter].xPos = xSandboxAreaScan;
particles[particlesCounter].yPos = ySandboxAreaScan;
particles[particlesCounter + 1].xPos = xSandboxAreaScan;
particles[particlesCounter + 1].yPos = ySandboxAreaScan + 1;
particlesCounter += 2;
totalOfParticles = particlesCounter;
}
results.push(xSandboxAreaScan += 1);
}
return results;
};
calculateDensities = function(particles, totalOfParticles) {
var i, j, particlesCursor, particlesCursor2, particlesDistance, particlesInteraction, ref, ref1, xParticleDistance, yParticleDistance;
particlesCursor = 0;
particlesCursor2 = 0;
for (particlesCursor = i = 0, ref = totalOfParticles; 0 <= ref ? i < ref : i > ref; particlesCursor = 0 <= ref ? ++i : --i) {
if (particles[particlesCursor].dead) {
continue;
}
particles[particlesCursor].density = particles[particlesCursor].wallflag * 9;
for (particlesCursor2 = j = 0, ref1 = totalOfParticles; 0 <= ref1 ? j < ref1 : j > ref1; particlesCursor2 = 0 <= ref1 ? ++j : --j) {
if (particles[particlesCursor2].dead) {
continue;
}
xParticleDistance = particles[particlesCursor].xPos - particles[particlesCursor2].xPos;
yParticleDistance = particles[particlesCursor].yPos - particles[particlesCursor2].yPos;
particlesDistance = Math.sqrt(xParticleDistance * xParticleDistance + yParticleDistance * yParticleDistance);
particlesInteraction = particlesDistance / 2.0 - 1.0;
if (Math.floor(1.0 - particlesInteraction) > 0) {
particles[particlesCursor].density += particlesInteraction * particlesInteraction;
}
}
}
};
calculateForces = function(particles, totalOfParticles) {
var i, j, particlesCursor, particlesCursor2, particlesDistance, particlesInteraction, ref, ref1, xParticleDistance, yParticleDistance;
particlesCursor = 0;
particlesCursor2 = 0;
for (particlesCursor = i = 0, ref = totalOfParticles; 0 <= ref ? i < ref : i > ref; particlesCursor = 0 <= ref ? ++i : --i) {
if (particles[particlesCursor].wallflag === 1 || particles[particlesCursor].dead) {
continue;
}
particles[particlesCursor].yForce = gravity;
particles[particlesCursor].xForce = 0;
for (particlesCursor2 = j = 0, ref1 = totalOfParticles; 0 <= ref1 ? j < ref1 : j > ref1; particlesCursor2 = 0 <= ref1 ? ++j : --j) {
if (particles[particlesCursor2].dead) {
continue;
}
xParticleDistance = particles[particlesCursor].xPos - particles[particlesCursor2].xPos;
yParticleDistance = particles[particlesCursor].yPos - particles[particlesCursor2].yPos;
particlesDistance = Math.sqrt(xParticleDistance * xParticleDistance + yParticleDistance * yParticleDistance);
particlesInteraction = particlesDistance / 2.0 - 1.0;
if (Math.floor(1.0 - particlesInteraction) > 0) {
particles[particlesCursor].xForce += particlesInteraction * (xParticleDistance * (3 - particles[particlesCursor].density - particles[particlesCursor2].density) * pressure + particles[particlesCursor].xVelocity * viscosity - particles[particlesCursor2].xVelocity * viscosity) / particles[particlesCursor].density;
particles[particlesCursor].yForce += particlesInteraction * (yParticleDistance * (3 - particles[particlesCursor].density - particles[particlesCursor2].density) * pressure + particles[particlesCursor].yVelocity * viscosity - particles[particlesCursor2].yVelocity * viscosity) / particles[particlesCursor].density;
}
}
}
};
simulationNextStep = function() {
var i, j, k, particlesCursor, ref, ref1, ref2, screenBuffer, screenBufferIndex, screenBufferString, x, y;
calculateDensities(particles, totalOfParticles);
calculateForces(particles, totalOfParticles);
screenBuffer = [];
screenBufferIndex = 0;
for (screenBufferIndex = i = 0, ref = CONSOLE_WIDTH * CONSOLE_HEIGHT; 0 <= ref ? i < ref : i > ref; screenBufferIndex = 0 <= ref ? ++i : --i) {
screenBuffer[screenBufferIndex] = 0;
}
for (particlesCursor = j = 0, ref1 = totalOfParticles; 0 <= ref1 ? j < ref1 : j > ref1; particlesCursor = 0 <= ref1 ? ++j : --j) {
if (particles[particlesCursor].wallflag === 0) {
if (Math.sqrt(particles[particlesCursor].xForce * particles[particlesCursor].xForce + particles[particlesCursor].yForce * particles[particlesCursor].yForce) < 4.2) {
particles[particlesCursor].xVelocity += particles[particlesCursor].xForce / 10;
particles[particlesCursor].yVelocity += particles[particlesCursor].yForce / 10;
} else {
particles[particlesCursor].xVelocity += particles[particlesCursor].xForce / 11;
particles[particlesCursor].yVelocity += particles[particlesCursor].yForce / 11;
}
particles[particlesCursor].xPos += particles[particlesCursor].xVelocity;
particles[particlesCursor].yPos += particles[particlesCursor].yVelocity;
}
x = Math.round(particles[particlesCursor].xPos);
y = Math.round(particles[particlesCursor].yPos / 2);
screenBufferIndex = Math.round(x + CONSOLE_WIDTH * y);
if (y >= 0 && y < CONSOLE_HEIGHT - 1 && x >= 0 && x < CONSOLE_WIDTH - 1) {
screenBuffer[screenBufferIndex] |= 8;
screenBuffer[screenBufferIndex + 1] |= 4;
screenBuffer[screenBufferIndex + CONSOLE_WIDTH] |= 2;
screenBuffer[screenBufferIndex + CONSOLE_WIDTH + 1] |= 1;
} else {
particles[particlesCursor].dead = true;
}
}
screenBufferString = '';
for (screenBufferIndex = k = 0, ref2 = CONSOLE_WIDTH * CONSOLE_HEIGHT; 0 <= ref2 ? k < ref2 : k > ref2; screenBufferIndex = 0 <= ref2 ? ++k : --k) {
if (screenBufferIndex % CONSOLE_WIDTH === CONSOLE_WIDTH - 1) {
screenBufferString += '<br>';
} else {
screenBufferString += " '`-.|//,\\|\\_\\/#"[screenBuffer[screenBufferIndex]];
}
}
document.getElementById("simulationOutput").innerHTML = screenBufferString.replace(/ /g, " ");
return window.requestAnimationFrame(simulationNextStep);
};
//# sourceMappingURL=asciiFluidSimulation.js.map