-
-
Save hmac/ae6091124d6651d3121a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import grafica.*; | |
GPlot plot; | |
GPointsArray points; | |
int nPoints = 200; | |
int LEN = 200; | |
float[][] mesh = new float[1080][LEN]; | |
float gaussian(float x, float mean, float stddev) { | |
float val = (1.0/(stddev*sqrt(2*PI))) * exp((-pow((x-mean),2))/(2.0*pow(stddev,2))); | |
return val; | |
} | |
float initial_conditions(int index) { | |
float[] vals = new float[200]; | |
for (int i = 0; i < 30; i++) { | |
vals[52 + i] = 50 * gaussian(i, 15, 5); | |
} | |
for (int i = 0; i < 30; i++) { | |
vals[107 + i] = 100 * gaussian(i, 15, 5); | |
} | |
return vals[index]; | |
} | |
void setup() { | |
// Setup the graph | |
frameRate(10); | |
size(500, 500); | |
background(150); | |
GPointsArray points = new GPointsArray(nPoints); | |
for (int j = 0; j < LEN; j++) { | |
mesh[0][j] = initial_conditions(j); | |
mesh[1][j] = initial_conditions(j); | |
} | |
// Stability requires dt/dx^2 <= 1/2 | |
float dt = 1.0; | |
float dx = sqrt(2); | |
float f = 6.0; | |
float M = 5.0; | |
float[] l = new float[LEN]; | |
float[] m = new float[LEN]; | |
float[] n = new float[LEN]; | |
for (int i = 2; i < mesh.length - 1; i++) { | |
for (int j = 0; j < LEN; j++) { | |
l[j] = mesh[i-2][j]; | |
m[j] = mesh[i-1][j]; | |
n[j] = mesh[i][j]; | |
} | |
for (int j = 1; j < LEN-1; j++) { | |
float v = m[j] - l[j]; | |
float dv = dt * (f/(M*dx)) * (m[j+1] + m[j-1] - 2.0*m[j]); | |
float du = dt * (v + dv); | |
mesh[i][j] = m[j] + du; | |
} | |
} | |
plot = new GPlot(this); | |
plot.setTitleText("Harry's waves."); | |
for (int i = 0; i < LEN; i++) { | |
points.add(i, pow(mesh[0][i], 2)); | |
} | |
plot.setPoints(points); | |
plot.defaultDraw(); | |
} | |
void draw() { | |
// Continuously run loop | |
if (frameCount == 1080) { | |
return; | |
} | |
GPointsArray points = new GPointsArray(nPoints); | |
for (int i = 0; i < LEN; i++) { | |
points.add(i, pow(mesh[2*frameCount][i], 2)); | |
} | |
plot.setPoints(points); | |
plot.defaultDraw(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment