1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
|
##############################################################################
#
# Copyright (c) 2003-2020 by The University of Queensland
# http://www.uq.edu.au
#
# Primary Business: Queensland, Australia
# Licensed under the Apache License, version 2.0
# http://www.apache.org/licenses/LICENSE-2.0
#
# Development until 2012 by Earth Systems Science Computational Center (ESSCC)
# Development 2012-2013 by School of Earth Sciences
# Development from 2014 by Centre for Geoscience Computing (GeoComp)
# Development from 2019 by School of Earth and Environmental Sciences
#
##############################################################################
from __future__ import print_function, division
import lamesource
import sys
def buildTempAndSummation(dim, ids, temps, summations, forced_substitutions = []):
declarations = {}
sumStatements = {}
for k in range(dim):
for m in range(dim):
declarations[(k,m)] = []
sumStatements[(k,m)] = []
zeroes = [] #tracks tmpvars that will always be zero
nonzeroes = [] #tracks tmpvars that will possibly be non-zero
use_counts = {} #tmp var usage counts, used later for culling
#temp declarations
#hold on to the building expression for every identifier
var_expressions = {}
for line in temps:
newline = line.format(k,m)
insert = False
for index in ids.iterkeys():
if index in newline:
insert = True
break
if not insert:
value = newline.split("=")[1]
for tmp in nonzeroes:
if tmp in value:
x = use_counts.get(tmp, 0)
use_counts[tmp] = x+1
insert = True
i = newline.index("tmp")
identifier = newline[i:].split()[0]
expression = newline.split("= ")[1][:-1]
if insert:
nonzeroes.append(identifier)
var_expressions[identifier] = expression
else:
zeroes.append(identifier)
#summations
with_nonzero = [] #only those with some non-zero temp var
for line in summations:
newline = replaceZeroes(line.format(k,m),zeroes)
for nz in nonzeroes:
if nz in newline:
with_nonzero.append(newline)
components = newline[:-1].split("+=")[1].lstrip().replace(" + ", "|").replace(" - ", "|")
if components[0] == "-":
components = components[1:]
components = components.split("|")
for var in components:
x = use_counts.get(var, 0)
use_counts[var] = x+1
break
for fs in forced_substitutions:
if fs in nonzeroes:
use_counts[fs] = 1
for z in zeroes:
use_counts[z] = 0
#only interested in keeping variables declarations with 2 or more uses
for var in nonzeroes:
#0 we don't care and 1 we substitute the expression
if use_counts.get(var, 0) > 1:
declarations[(k,m)].append(" const double %s = %s;"%(var, var_expressions[var]))
#remove zeroes and replace single use tmpvars with their expression
for line in with_nonzero:
for key in use_counts.iterkeys():
if use_counts[key] == 0:
line = line.replace("%s;"%key, ";")
line = line.replace("%s "%key, " ")
elif use_counts[key] < 2:
s = var_expressions[key]
line = line.replace("%s;"%key, s+";")
line = line.replace("%s "%key, s+" ")
#print only if there's a right-hand-side of the expression
if "+=;" not in line and "+=;" not in line:
sumStatements[(k,m)].append(" "+line)
return declarations, sumStatements
def replaceZeroes(line, zeroes):
for zero in zeroes:
if zero+"|" in line:
line = line.replace(" + %s|"%zero, "")
line = line.replace("%s| + "%zero, "0 + ") #there's a better solution
line = line.replace(" - %s|"%zero, "") #it's just to stop
line = line.replace("%s| - "%zero, "0 - ") # a - b -> b instead of -b
line = line.replace("=%s|;"%zero,"=;")
if line:
return line.replace("|","").replace(" - 0", "").replace(" + 0", "").replace("+= 0 +", "+=").replace("+= 0 - ", "+=-")
return line
def print2DAExpanded():
dim = 2
quads = 2**dim
ids = {}
for i in range(dim):
for j in range(dim):
ids["{0}{0}{1}{1}".format(i,j)] = None
ids["{0}{1}{1}{0}".format(i,j)] = None
ids["{0}{1}{0}{1}".format(i,j)] = None
for name in sorted(ids.iterkeys()):
print("double A_{0}[{1}] =".format(name,quads), "{0};")
# ijji += mu
# ijij += mu
# iijj += lambda
print("if (!mu.isEmpty()) {\n const double *mu_p = mu.getSampleDataRO(e);")
completed = {}
for i in range(dim):
for j in range(dim):
for q in range(quads):
if i == j:
print(" A_{0}{0}{0}{0}[{1}] += 2*mu_p[{1}];".format(i,q))
else:
print(" A_{0}{1}{1}{0}[{2}] += mu_p[{2}];".format(i,j,q))
print(" A_{0}{1}{0}{1}[{2}] += mu_p[{2}];".format(i,j,q))
print("}\nif (!lambda.isEmpty()) {\n const double *lambda_p = lambda.getSampleDataRO(e);")
for i in range(dim):
for j in range(dim):
for q in range(quads):
print(" A_{0}{0}{1}{1}[{2}] += lambda_p[{2}];".format(i,j,q))
print("}")
decl, sums = buildTempAndSummation(dim, ids, lamesource.expanded2Dtemps, lamesource.expanded2Dsummations)
for k in range(dim):
for m in range(dim):
print("{")
print("\n".join(decl[(k,m)]))
print("\n".join(sums[(k,m)]))
print("}")
def print2DAReduced():
dim = 2
quads = 2**dim
ids = {}
for i in range(dim):
for j in range(dim):
ids["{0}{0}{1}{1}".format(i,j)] = None
ids["{0}{1}{1}{0}".format(i,j)] = None
ids["{0}{1}{0}{1}".format(i,j)] = None
for name in sorted(ids.iterkeys()):
print("double A_{0} =".format(name,quads), "0;")
# ijji += mu
# ijij += mu
# iijj += lambda
print("if (!mu.isEmpty()) {\n const double *mu_p = mu.getSampleDataRO(e);")
for i in range(dim):
for j in range(dim):
if i == j:
print(" A_{0}{0}{0}{0} += 2*mu_p[0];".format(i))
else:
print(" A_{0}{1}{1}{0} += mu_p[0];".format(i,j))
print(" A_{0}{1}{0}{1} += mu_p[0];".format(i,j))
print("}")
print("if (!lambda.isEmpty()) {\n const double *lambda_p = lambda.getSampleDataRO(e);")
for i in range(dim):
for j in range(dim):
print(" A_{0}{0}{1}{1} += lambda_p[0];".format(i,j))
print("}")
lines = [
"const double tmp_0 = 6*w1*(A_{0}0{1}1 - A_{0}1{1}0);",
"const double tmp_1 = 6*w1*(A_{0}0{1}1 + A_{0}1{1}0);",
"const double tmp_2 = 6*w1*(-A_{0}0{1}1 - A_{0}1{1}0);",
"const double tmp_3 = 6*w1*(-A_{0}0{1}1 + A_{0}1{1}0);"
]
zeroes = []
nonzeroes = []
for k in range(dim):
for m in range(dim):
if k == m:
for q in range(quads):
zeroes.append("tmp{0}{0}_{1}".format(m, q))
continue
for line in lines:
newline = line.format(k,m)
insert = False
for index in ids.iterkeys():
if index in newline:
insert = True
break
i = newline.index("tmp")
if insert:
print(newline)
nonzeroes.append(newline[i:].split()[0].rstrip())
else:
zeroes.append(newline[i:].split()[0].rstrip())
for k in range(dim):
for m in range(dim):
for line in lamesource.reduced2Dsummations:
newline = replaceZeroes(line.format(k,m),zeroes)
if not newline:
continue
if k != m:
st = "A_{0}0{1}0".format(k,m)
if st in newline:
i = newline.index(st)
newline = newline[:i-3] + newline[i+12:]
st = "A_{0}1{1}1".format(k,m)
if st in newline:
i = newline.index(st)
newline = newline[:i-2] + newline[i+11:]
print(newline)
def print3DAExpanded():
dim = 3
quads = 2**dim
ids = {}
for i in range(dim):
for j in range(dim):
ids["{0}{0}{1}{1}".format(i,j)] = None
ids["{0}{1}{1}{0}".format(i,j)] = None
ids["{0}{1}{0}{1}".format(i,j)] = None
for name in sorted(ids.iterkeys()):
print("double A_{0}[{1}] =".format(name,quads), "{0};")
# ijji += mu
# ijij += mu
# iijj += lambda
print("if (!mu.isEmpty()) {\n const double *mu_p = mu.getSampleDataRO(e);")
for i in range(dim):
for j in range(dim):
for q in range(quads):
if i == j:
print(" A_{0}{0}{0}{0}[{1}] += 2*mu_p[{1}];".format(i,q))
else:
print(" A_{0}{1}{1}{0}[{2}] += mu_p[{2}];".format(i,j,q))
print(" A_{0}{1}{0}{1}[{2}] += mu_p[{2}];".format(i,j,q))
print("}")
print("if (!lambda.isEmpty()) {\n const double *lambda_p = lambda.getSampleDataRO(e);")
for i in range(dim):
for j in range(dim):
for q in range(quads):
print(" A_{0}{0}{1}{1}[{2}] += lambda_p[{2}];".format(i,j,q))
print("}")
decl, sums = buildTempAndSummation(dim, ids, lamesource.expanded3Dtemps, lamesource.expanded3Dsummations)
for k in range(dim):
for m in range(dim):
print("{")
print("\n".join(decl[(k,m)]))
print("\n".join(sums[(k,m)]))
print("}")
def print3DAReduced():
dim = 3
ids = {}
for i in range(dim):
for j in range(dim):
ids["{0}{0}{1}{1}".format(i,j)] = None
ids["{0}{1}{1}{0}".format(i,j)] = None
ids["{0}{1}{0}{1}".format(i,j)] = None
for i in sorted(ids.iterkeys()):
print("double Aw%s = 0;"%i)
print("if (!mu.isEmpty()) {\n const double *mu_p = mu.getSampleDataRO(e);")
for i in range(dim):
for j in range(dim):
if i == j:
print(" Aw{0}{0}{0}{0} += 2*mu_p[0];".format(i))
else:
print(" Aw{0}{1}{1}{0} += mu_p[0];".format(i,j))
print(" Aw{0}{1}{0}{1} += mu_p[0];".format(i,j))
print("}\nif (!lambda.isEmpty()) {\n const double *lambda_p = lambda.getSampleDataRO(e);")
for i in range(dim):
for j in range(dim):
print(" Aw{0}{0}{1}{1} += lambda_p[0];".format(i,j))
print("}")
for k in range(dim):
for m in range(dim):
for line in ["Aw{0}0{1}0 *= 8*w27;","Aw{0}0{1}1 *= 12*w8;","Aw{0}0{1}2 *= 12*w11;",
"Aw{0}1{1}0 *= 12*w8;","Aw{0}1{1}1 *= 8*w22;","Aw{0}1{1}2 *= 12*w10;",
"Aw{0}2{1}0 *= 12*w11;","Aw{0}2{1}1 *= 12*w10;","Aw{0}2{1}2 *= 8*w13;"]:
newline = line.format(k,m)
found = False
for ident in ids.iterkeys():
if ident in newline:
found = True
break
if found:
print(newline)
decs, sums = buildTempAndSummation(dim, ids, lamesource.reduced3Dtemps,
lamesource.reduced3Dsummations,
forced_substitutions = ["tmp12","tmp13","tmp14","tmp21","tmp22","tmp23"])
for k in range(dim):
for m in range(dim):
print("{")
print("\n".join(decs[(k,m)]))
print("\n".join(sums[(k,m)]))
print("}")
def printAReduced(dim):
if dim == 2:
return print2DAReduced()
elif dim == 3:
return print3DAReduced()
else:
raise
def printAExpanded(dim):
if dim == 2:
return print2DAExpanded()
elif dim == 3:
return print3DAExpanded()
else:
raise
if __name__ == "__main__":
if len(sys.argv) < 3 or sys.argv[1] not in ["R", "E"] or sys.argv[2] not in ["2","3"]:
print("Usage: {0} [R]educed/[E]xpanded dimensions\nE.g. {0} R 3")
exit(1)
dim = int(sys.argv[2])
if sys.argv[1] == "R":
printAReduced(dim)
else:
printAExpanded(dim)
|