Skip to content

Instantly share code, notes, and snippets.

@drewsberry
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save drewsberry/bab9f99a48444aeca191 to your computer and use it in GitHub Desktop.
Save drewsberry/bab9f99a48444aeca191 to your computer and use it in GitHub Desktop.
Harry's Waves in Processing
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(20);
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.setDim(400, 400);
plot.setYLim(-10, 50);
plot.setXLim(0, 200);
plot.setTitleText("Harry's waves.");
for (int i = 0; i < LEN; i++) {
points.add(i, mesh[0][i]);
}
plot.setPoints(points);
myDraw(plot);
}
void myDraw(GPlot plotobj) {
plotobj.beginDraw();
plotobj.drawBackground();
plotobj.drawBox();
plotobj.drawXAxis();
plotobj.drawYAxis();
plotobj.drawTitle();
plotobj.drawLines();
plotobj.endDraw();
}
void draw() {
// Continuously run loop
if (frameCount == mesh.length) {
frameCount = 1;
}
GPointsArray points = new GPointsArray(nPoints);
for (int i = 0; i < LEN; i++) {
points.add(i, mesh[frameCount][i]);
}
plot.setPoints(points);
myDraw(plot);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment