source: CIVL/examples/arithmetic/laplace.cvl@ ac5660f

1.23 2.0 main test-branch
Last change on this file since ac5660f was 9bcb8b8e, checked in by Tim Zirkel <zirkeltk@…>, 13 years ago

Moved declarations to make laplace.cvl a little more natural.

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@182 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 1.2 KB
RevLine 
[95a2a9e]1int TIME_BOUND = 2;
[56e078f]2#define nx 3 // number of x coordinates (including boundary)
3#define ny 3 // number of rows including boundary
[95a2a9e]4double 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
9double grid[ny][nx]; // holds values of current iteration
10
11double square(double x) { return x * x; }
12
13void init() {
[9bcb8b8e]14 for (int row=0; row<ny; row++)
15 for (int col=0; col<nx; col++)
[95a2a9e]16 grid[row][col] = initialValues[row][col];
17}
18
[56e078f]19void write_result(int time, double data[ny][nx]) {
[95a2a9e]20 t = time;
[9bcb8b8e]21 for (int row=ny-1; row>=0; row--)
22 for (int col=0; col<nx; col++)
[95a2a9e]23 out[row][col] = data[row][col];
24}
25
26void 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;
[9bcb8b8e]35 for (int row=1; row<ny-1; row++) {
36 for (int col=1; col<nx-1; col++) {
[95a2a9e]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 }
[9bcb8b8e]43 for (int row=1; row<ny-1; row++)
44 for (int col=1; col<nx-1; col++)
[95a2a9e]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.