1.23
2.0
main
test-branch
| Line | |
|---|
| 1 | int TIME_BOUND = 2;
|
|---|
| 2 | #define nx 3 // number of x coordinates (including boundary)
|
|---|
| 3 | #define ny 3 // number of rows including boundary
|
|---|
| 4 | double epsilon = 0.01; // total error tolerance
|
|---|
| 5 | $input double initialValues[ny][nx]; // initial values
|
|---|
| 6 | $output int t;
|
|---|
| 7 | $output double out[ny][nx];
|
|---|
| 8 |
|
|---|
| 9 | double grid[ny][nx]; // holds values of current iteration
|
|---|
| 10 |
|
|---|
| 11 | double square(double x) { return x * x; }
|
|---|
| 12 |
|
|---|
| 13 | void init() {
|
|---|
| 14 | for (int row=0; row<ny; row++)
|
|---|
| 15 | for (int col=0; col<nx; col++)
|
|---|
| 16 | grid[row][col] = initialValues[row][col];
|
|---|
| 17 | }
|
|---|
| 18 |
|
|---|
| 19 | void write_result(int time, double data[ny][nx]) {
|
|---|
| 20 | t = time;
|
|---|
| 21 | for (int row=ny-1; row>=0; row--)
|
|---|
| 22 | for (int col=0; col<nx; col++)
|
|---|
| 23 | out[row][col] = data[row][col];
|
|---|
| 24 | }
|
|---|
| 25 |
|
|---|
| 26 | void main() {
|
|---|
| 27 | double error = epsilon;
|
|---|
| 28 | int time = 0;
|
|---|
| 29 | double result;
|
|---|
| 30 | double tmp[ny][nx];
|
|---|
| 31 |
|
|---|
| 32 | init();
|
|---|
| 33 | while (error >= epsilon && time < TIME_BOUND) {
|
|---|
| 34 | error = 0.0;
|
|---|
| 35 | for (int row=1; row<ny-1; row++) {
|
|---|
| 36 | for (int col=1; col<nx-1; col++) {
|
|---|
| 37 | tmp[row][col] = (grid[row-1][col]+grid[row+1][col]+
|
|---|
| 38 | grid[row][col-1]+grid[row][col+1])/4.0;
|
|---|
| 39 | result = square(grid[row][col] - tmp[row][col]);
|
|---|
| 40 | error += result;
|
|---|
| 41 | }
|
|---|
| 42 | }
|
|---|
| 43 | for (int row=1; row<ny-1; row++)
|
|---|
| 44 | for (int col=1; col<nx-1; col++)
|
|---|
| 45 | grid[row][col] = tmp[row][col];
|
|---|
| 46 | time++;
|
|---|
| 47 | }
|
|---|
| 48 | write_result(time, grid);
|
|---|
| 49 | }
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.