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