Debian Codegraph @

demo/kitchen-sink/docs/sac.sac

raw
1 /*****************************************************************************
2 *
3 * SAC demo program
4 *
5 * This SAC demo program implements 2-dimensional relaxation on double
6 * precision floating point numbers applying a 4-point stencil and fixed
7 * boundary conditions.
8 *
9 * The vertical (SIZE1) and the horizontal (SIZE2) array size as well as
10 * the number of iterations to be performed (LOOP) may be set at compile
11 * time.
12 *
13 *****************************************************************************/
14
15 #ifndef LOOP
16 #define LOOP 100
17 #endif
18
19 #ifndef SIZE1
20 #define SIZE1 1000
21 #endif
22
23 #ifndef SIZE2
24 #define SIZE2 1000
25 #endif
26
27 use Array: all;
28 use StdIO: all;
29
30 inline
31 double[+] onestep(double[+] B)
32 {
33 A = with {
34 ( . < x < . ) : 0.25*(B[x+[1,0]]
35 + B[x-[1,0]]
36 + B[x+[0,1]]
37 + B[x-[0,1]]);
38 } : modarray( B );
39
40 return(A);
41 }
42
43 inline
44 double[+] relax(double[+] A, int steps)
45 {
46 for (k=0; k<steps; k++) {
47 A = onestep(A);
48 }
49
50 return(A);
51 }
52
53 int main ()
54 {
55 A = with {
56 ( . <= x <= . ) : 0.0d;
57 } : genarray( [SIZE1,SIZE2] );
58
59 A = modarray(A, [0,1], 500.0d);
60 A = relax( A, LOOP);
61
62 z = with {
63 ( 0*shape(A) <= x < shape(A) ) : A[x];
64 } : fold( +, 0d );
65
66 #if 0
67 printf("%.10g\n", z);
68 return(0);
69 #else
70 print( z);
71 return( 0);
72 #endif
73 }
74
75