Modeling heat flow in Processing? I have a bad bug.
in
Programming Questions
•
2 years ago
So I got this weird idea that I would write a Processing sketch that simulates a closed 2d system (perhaps a sheet of metal totally insulated from the outside world) where some regions start out hotter than others and by heat being conducted the whole system comes to equilibrium. So I wrote this code (every frame the code determines the average temperature of the system and prints it out).
- final int windowSize = 500;
int[][] heatarray = new int[windowSize][windowSize];
int totalheat = 0;
void setup() {
size(windowSize, windowSize);
frameRate(30);
for (int iii=0; iii<=(windowSize-1); iii++) { //windowSize-2 makes it go 0-499
for (int jjj=0; jjj<=(windowSize-1); jjj++) {
heatarray[iii][jjj] = round((noise(iii/200,jjj/200)*200));
}
}
}
void draw() {
totalheat = 0;
for (int iii=0; iii<=(windowSize-1); iii++) {
for (int jjj=0; jjj<=(windowSize-1); jjj++) {
stroke(heatarray[iii][jjj],heatarray[iii][jjj],heatarray[iii][jjj]);
totalheat = totalheat+heatarray[iii][jjj];
point(iii,jjj);
htransfer(iii,jjj);
}
}
println(totalheat/250000);
}
void htransfer(int iii,int jjj) {
if ((iii>0) && (iii<499)) {
if ((jjj>0) && (jjj<499)) {
haverage(iii,jjj,iii+1,jjj+1);
haverage(iii,jjj,iii+1,jjj-1);
haverage(iii,jjj,iii-1,jjj+1);
haverage(iii,jjj,iii-1,jjj-1);
}
}
}
void haverage(int x1,int y1,int x2, int y2) {
int average = (heatarray[x1][y1]+heatarray[x2][y2])/2;
heatarray[x1][y1] = average;
heatarray[x2][y2] = average;
}
1