| [89b55f5] | 1 | /* Commandline execution:
|
|---|
| 2 | * civl verify laplace.cvl
|
|---|
| 3 | */
|
|---|
| [95a2a9e] | 4 | int TIME_BOUND = 2;
|
|---|
| [56e078f] | 5 | #define nx 3 // number of x coordinates (including boundary)
|
|---|
| 6 | #define ny 3 // number of rows including boundary
|
|---|
| [95a2a9e] | 7 | double epsilon = 0.01; // total error tolerance
|
|---|
| 8 | $input double initialValues[ny][nx]; // initial values
|
|---|
| 9 | $output int t;
|
|---|
| 10 | $output double out[ny][nx];
|
|---|
| 11 |
|
|---|
| 12 | double grid[ny][nx]; // holds values of current iteration
|
|---|
| 13 |
|
|---|
| 14 | double square(double x) { return x * x; }
|
|---|
| 15 |
|
|---|
| 16 | void init() {
|
|---|
| [9bcb8b8e] | 17 | for (int row=0; row<ny; row++)
|
|---|
| 18 | for (int col=0; col<nx; col++)
|
|---|
| [95a2a9e] | 19 | grid[row][col] = initialValues[row][col];
|
|---|
| 20 | }
|
|---|
| 21 |
|
|---|
| [56e078f] | 22 | void write_result(int time, double data[ny][nx]) {
|
|---|
| [95a2a9e] | 23 | t = time;
|
|---|
| [9bcb8b8e] | 24 | for (int row=ny-1; row>=0; row--)
|
|---|
| 25 | for (int col=0; col<nx; col++)
|
|---|
| [95a2a9e] | 26 | out[row][col] = data[row][col];
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | void main() {
|
|---|
| 30 | double error = epsilon;
|
|---|
| 31 | int time = 0;
|
|---|
| 32 | double result;
|
|---|
| 33 | double tmp[ny][nx];
|
|---|
| 34 |
|
|---|
| 35 | init();
|
|---|
| 36 | while (error >= epsilon && time < TIME_BOUND) {
|
|---|
| 37 | error = 0.0;
|
|---|
| [9bcb8b8e] | 38 | for (int row=1; row<ny-1; row++) {
|
|---|
| 39 | for (int col=1; col<nx-1; col++) {
|
|---|
| [95a2a9e] | 40 | tmp[row][col] = (grid[row-1][col]+grid[row+1][col]+
|
|---|
| 41 | grid[row][col-1]+grid[row][col+1])/4.0;
|
|---|
| 42 | result = square(grid[row][col] - tmp[row][col]);
|
|---|
| 43 | error += result;
|
|---|
| 44 | }
|
|---|
| 45 | }
|
|---|
| [9bcb8b8e] | 46 | for (int row=1; row<ny-1; row++)
|
|---|
| 47 | for (int col=1; col<nx-1; col++)
|
|---|
| [95a2a9e] | 48 | grid[row][col] = tmp[row][col];
|
|---|
| 49 | time++;
|
|---|
| 50 | }
|
|---|
| 51 | write_result(time, grid);
|
|---|
| 52 | }
|
|---|