1 | #!/usr/bin/env python |
---|
2 | ######################################################### |
---|
3 | # |
---|
4 | # |
---|
5 | # Calculate and print the norms of the domain |
---|
6 | # |
---|
7 | # |
---|
8 | # The routines defined here are intended for debugging |
---|
9 | # use. They print the norms of the quantities in the |
---|
10 | # domain. As opposed to the definitions given |
---|
11 | # in utiltites.norms these calculations take a |
---|
12 | # parallel domain into account. |
---|
13 | # |
---|
14 | # Authors: Linda Stals and April 2006 |
---|
15 | # |
---|
16 | # |
---|
17 | ######################################################### |
---|
18 | |
---|
19 | import sys |
---|
20 | |
---|
21 | import pypar |
---|
22 | |
---|
23 | from Numeric import array, Int8, zeros, ones, take, nonzero, Float |
---|
24 | from anuga.utilities.norms import l1_norm, l2_norm, linf_norm |
---|
25 | |
---|
26 | ######################################################### |
---|
27 | # |
---|
28 | # Find out which triangles are full triangles (only these |
---|
29 | # triangles should be included in the norm calculations) |
---|
30 | # |
---|
31 | # *) |
---|
32 | # |
---|
33 | # ------------------------------------------------------- |
---|
34 | # |
---|
35 | # *) A 1-D array, tri_full_flag, is returned. |
---|
36 | # |
---|
37 | # *) The size of tri_full_flag is the same as the number |
---|
38 | # of vertices in the domain |
---|
39 | # |
---|
40 | # *) If tri_full_flag[i] = 1, then triangle number i is |
---|
41 | # a full triangle, if tri_full_flag[i] = 0 the triangle |
---|
42 | # is a ghost triangle |
---|
43 | # |
---|
44 | ######################################################### |
---|
45 | |
---|
46 | def build_full_flag(domain, ghost_recv_dict): |
---|
47 | |
---|
48 | tri_full_flag = ones(len(domain.get_triangles()), Int8) |
---|
49 | for i in ghost_recv_dict.keys(): |
---|
50 | for id in ghost_recv_dict[i][0]: |
---|
51 | tri_full_flag[id] = 0 |
---|
52 | |
---|
53 | return tri_full_flag |
---|
54 | |
---|
55 | ######################################################### |
---|
56 | # |
---|
57 | # Print the l1 norm of the given quantity |
---|
58 | # |
---|
59 | # *) The quantity is an array containing three columns |
---|
60 | # |
---|
61 | # ------------------------------------------------------- |
---|
62 | # |
---|
63 | # *) The l1 norm is calculated along each axis |
---|
64 | # |
---|
65 | # *) The l1 norm is printed |
---|
66 | # |
---|
67 | # *) Processor 0 prints the results |
---|
68 | # |
---|
69 | # |
---|
70 | ######################################################### |
---|
71 | def print_l1_stats(full_edge): |
---|
72 | |
---|
73 | numprocs = pypar.size() |
---|
74 | myid = pypar.rank() |
---|
75 | |
---|
76 | tri_norm = zeros(3, Float) |
---|
77 | recv_norm = zeros(3, Float) |
---|
78 | tri_norm[0] = l1_norm(full_edge[:, 0]) |
---|
79 | tri_norm[1] = l1_norm(full_edge[:, 1]) |
---|
80 | tri_norm[2] = l1_norm(full_edge[:, 2]) |
---|
81 | if myid == 0: |
---|
82 | for p in range(numprocs-1): |
---|
83 | pypar.receive(p+1, recv_norm) |
---|
84 | tri_norm[0] = tri_norm[0]+recv_norm[0] |
---|
85 | tri_norm[1] = tri_norm[1]+recv_norm[1] |
---|
86 | tri_norm[2] = tri_norm[2]+recv_norm[2] |
---|
87 | print 'l1_norm along each axis : [', tri_norm[0],', ', tri_norm[1], ', ', tri_norm[2], ']' |
---|
88 | |
---|
89 | else: |
---|
90 | pypar.send(tri_norm, 0) |
---|
91 | |
---|
92 | ######################################################### |
---|
93 | # |
---|
94 | # Print the l2 norm of the given quantity |
---|
95 | # |
---|
96 | # *) The quantity is an array containing three columns |
---|
97 | # |
---|
98 | # ------------------------------------------------------- |
---|
99 | # |
---|
100 | # *) The l2 norm is calculated along each axis |
---|
101 | # |
---|
102 | # *) The l2 norm is printed |
---|
103 | # |
---|
104 | # *) Processor 0 prints the results |
---|
105 | # |
---|
106 | # |
---|
107 | ######################################################### |
---|
108 | def print_l2_stats(full_edge): |
---|
109 | |
---|
110 | numprocs = pypar.size() |
---|
111 | myid = pypar.rank() |
---|
112 | |
---|
113 | tri_norm = zeros(3, Float) |
---|
114 | recv_norm = zeros(3, Float) |
---|
115 | tri_norm[0] = pow(l2_norm(full_edge[:, 0]), 2) |
---|
116 | tri_norm[1] = pow(l2_norm(full_edge[:, 1]), 2) |
---|
117 | tri_norm[2] = pow(l2_norm(full_edge[:, 2]), 2) |
---|
118 | if myid == 0: |
---|
119 | for p in range(numprocs-1): |
---|
120 | pypar.receive(p+1, recv_norm) |
---|
121 | tri_norm[0] = tri_norm[0]+recv_norm[0] |
---|
122 | tri_norm[1] = tri_norm[1]+recv_norm[1] |
---|
123 | tri_norm[2] = tri_norm[2]+recv_norm[2] |
---|
124 | print 'l2_norm along each axis : [', pow(tri_norm[0], 0.5),', ', pow(tri_norm[1], 0.5), \ |
---|
125 | ', ', pow(tri_norm[2], 0.5), ']' |
---|
126 | else: |
---|
127 | pypar.send(tri_norm, 0) |
---|
128 | |
---|
129 | |
---|
130 | ######################################################### |
---|
131 | # |
---|
132 | # Print the linf norm of the given quantity |
---|
133 | # |
---|
134 | # *) The quantity is an array containing three columns |
---|
135 | # |
---|
136 | # ------------------------------------------------------- |
---|
137 | # |
---|
138 | # *) The linf norm is calculated along each axis |
---|
139 | # |
---|
140 | # *) The linf norm is printed |
---|
141 | # |
---|
142 | # *) Processor 0 prints the results |
---|
143 | # |
---|
144 | # |
---|
145 | ######################################################### |
---|
146 | def print_linf_stats(full_edge): |
---|
147 | |
---|
148 | numprocs = pypar.size() |
---|
149 | myid = pypar.rank() |
---|
150 | |
---|
151 | tri_norm = zeros(3, Float) |
---|
152 | recv_norm = zeros(3, Float) |
---|
153 | |
---|
154 | tri_norm[0] = linf_norm(full_edge[:, 0]) |
---|
155 | tri_norm[1] = linf_norm(full_edge[:, 1]) |
---|
156 | tri_norm[2] = linf_norm(full_edge[:, 2]) |
---|
157 | if myid == 0: |
---|
158 | for p in range(numprocs-1): |
---|
159 | pypar.receive(p+1, recv_norm) |
---|
160 | tri_norm[0] = max(tri_norm[0], recv_norm[0]) |
---|
161 | tri_norm[1] = max(tri_norm[1], recv_norm[1]) |
---|
162 | tri_norm[2] = max(tri_norm[2], recv_norm[2]) |
---|
163 | print 'linf_norm along each axis : [', tri_norm[0],', ', tri_norm[1], ', ', tri_norm[2], ']' |
---|
164 | else: |
---|
165 | pypar.send(tri_norm, 0) |
---|
166 | |
---|
167 | |
---|
168 | ######################################################### |
---|
169 | # |
---|
170 | # Print the norms of the quantites assigned to the domain |
---|
171 | # (this is useful for checking the numerical results |
---|
172 | # in the parallel computation) |
---|
173 | # |
---|
174 | # *) tri_full_flag states which of the triangles are |
---|
175 | # full triangles |
---|
176 | # |
---|
177 | # |
---|
178 | # ------------------------------------------------------- |
---|
179 | # |
---|
180 | # *) For each quantity, the l1, l2 and linf norms are |
---|
181 | # printed |
---|
182 | # |
---|
183 | # *) The size of tri_full_flag is the same as the number |
---|
184 | # of vertices in the domain |
---|
185 | # |
---|
186 | # *) Only the full triangles are used in the norm |
---|
187 | # calculations |
---|
188 | # |
---|
189 | # *) Processor 0 prints the results |
---|
190 | # |
---|
191 | ######################################################### |
---|
192 | def print_test_stats(domain, tri_full_flag): |
---|
193 | |
---|
194 | myid = pypar.rank() |
---|
195 | |
---|
196 | for k in domain.quantities.keys(): |
---|
197 | TestStage = domain.quantities[k] |
---|
198 | if myid == 0: |
---|
199 | print " ===== ", k, " ===== " |
---|
200 | full_edge = take(TestStage.edge_values, nonzero(tri_full_flag)) |
---|
201 | print_l1_stats(full_edge) |
---|
202 | print_l2_stats(full_edge) |
---|
203 | print_linf_stats(full_edge) |
---|
204 | |
---|