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
|
import numpy as np
import time
np.random.seed(0)
import gau2grid as gg
### Options
#npoints = int(5)
npoints = int(1.e5)
L = 2
nprim = 1
spherical = False
spherical = True
do_transpose = False
#do_transpose = True
### Test
xyz = np.random.rand(3, npoints)
grad_inds = ["PHI_X", "PHI_Y", "PHI_Z"]
hess_inds = ["PHI_XX", "PHI_XY", "PHI_XZ", "PHI_YY", "PHI_YZ", "PHI_ZZ"]
def compare(test, ref, grad):
"""
Compares two results
"""
print("%-6s %s" % ("PHI", np.allclose(test["PHI"], ref["PHI"])))
if grad > 0:
print('--')
for key in grad_inds:
print("%-6s %s" % (key, np.allclose(test[key], ref[key])))
if grad > 1:
print('--')
for key in hess_inds:
print("%-6s %s" % (key, np.allclose(test[key], ref[key])))
def transpose_dict(inp, out):
return
#pygg.fast_transpose(inp[k], out[k])
def build_out(nvals, npoints, grad):
"""
Builds output zeros to prevent cost effecting timings
"""
inds = ["PHI"]
if grad > 0:
inds += grad_inds
if grad > 1:
inds += hess_inds
return {k: np.zeros((nvals, npoints)) for k in inds}
ncart = int((L + 1) * (L + 2) / 2)
nsph = L * 2 + 1
nvals = ncart
if spherical:
nvals = nsph
coefs = np.arange(nprim, dtype=np.double) + 1
exps = np.arange(nprim, dtype=np.double) + 2
#center = np.array([5, 5, 5])
center = np.array([0, 0, 0], dtype=np.double)
### Points
# Call pyGG
gg_out = build_out(nvals, npoints, 0)
tran_out = build_out(npoints, nvals, 0)
t = time.time()
if do_transpose:
transpose_dict(gg_out, tran_out)
gg.collocation(xyz, L, coefs, exps, center, grad=0, spherical=spherical, out=gg_out)
#gg_out["PHI"] = gg_out["PHI"].copy().reshape(npoints, nvals).T
ctime = (time.time() - t)
# Call NP GG
t = time.time()
np_out = gg.ref.collocation(xyz, L, coefs, exps, center, grad=0, spherical=spherical, cartesian_order="row")
pytime = (time.time() - t)
# print(c_out.shape)
# print(np_out["PHI"].shape)
print("PHI")
compare(gg_out, np_out, 0)
#print(np_out["PHI"])
#print(gg_out["PHI"])
print("C time %12.6f" % ctime)
print("Py time %12.6f" % pytime)
print("Ratio %12.6f" % (pytime / ctime))
### Derivatives
print("\nDerivative")
# Call pyGG
gg_out = build_out(nvals, npoints, 1)
tran_out = build_out(npoints, nvals, 1)
t = time.time()
if do_transpose:
transpose_dict(gg_out, tran_out)
gg.collocation(xyz, L, coefs, exps, center, grad=1, spherical=spherical, out=gg_out)
ctime = (time.time() - t)
# Call NP GG
t = time.time()
np_out = gg.ref.collocation(xyz, L, coefs, exps, center, grad=1, spherical=spherical, cartesian_order="row")
pytime = (time.time() - t)
compare(gg_out, np_out, 1)
print("C time %12.6f" % ctime)
print("Py time %12.6f" % pytime)
print("Ratio %12.6f" % (pytime / ctime))
### Hessian
print("\nHessian")
gg_out = build_out(nvals, npoints, 2)
tran_out = build_out(npoints, nvals, 2)
t = time.time()
gg.collocation(xyz, L, coefs, exps, center, grad=2, spherical=spherical, out=gg_out)
if do_transpose:
transpose_dict(gg_out, tran_out)
ctime = (time.time() - t)
# Call NP GG
t = time.time()
np_out = gg.ref.collocation(xyz, L, coefs, exps, center, grad=2, spherical=spherical, cartesian_order="row")
pytime = (time.time() - t)
#print(np_out["PHI_X"])
#print(np_out["PHI_Y"])
#print(np_out["PHI_Z"])
compare(gg_out, np_out, 2)
print("C time %12.6f" % ctime)
print("Py time %12.6f" % pytime)
print("Ratio %12.6f" % (pytime / ctime))
|