| 1 |
import numpy as np |
| 2 |
import time |
| 3 |
|
| 4 |
np.random.seed(0) |
| 5 |
|
| 6 |
import gau2grid as gg |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
npoints = int(1.e5) |
| 12 |
|
| 13 |
L = 2 |
| 14 |
nprim = 1 |
| 15 |
|
| 16 |
spherical = False |
| 17 |
spherical = True |
| 18 |
|
| 19 |
do_transpose = False |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
xyz = np.random.rand(3, npoints) |
| 25 |
|
| 26 |
grad_inds = ["PHI_X", "PHI_Y", "PHI_Z"] |
| 27 |
hess_inds = ["PHI_XX", "PHI_XY", "PHI_XZ", "PHI_YY", "PHI_YZ", "PHI_ZZ"] |
| 28 |
|
| 29 |
|
| 30 |
def compare(test, ref, grad): |
| 31 |
""" |
| 32 |
Compares two results |
| 33 |
""" |
| 34 |
print("%-6s %s" % ("PHI", np.allclose(test["PHI"], ref["PHI"]))) |
| 35 |
if grad > 0: |
| 36 |
print('--') |
| 37 |
for key in grad_inds: |
| 38 |
print("%-6s %s" % (key, np.allclose(test[key], ref[key]))) |
| 39 |
if grad > 1: |
| 40 |
print('--') |
| 41 |
for key in hess_inds: |
| 42 |
print("%-6s %s" % (key, np.allclose(test[key], ref[key]))) |
| 43 |
|
| 44 |
|
| 45 |
def transpose_dict(inp, out): |
| 46 |
return |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
def build_out(nvals, npoints, grad): |
| 52 |
""" |
| 53 |
Builds output zeros to prevent cost effecting timings |
| 54 |
""" |
| 55 |
inds = ["PHI"] |
| 56 |
if grad > 0: |
| 57 |
inds += grad_inds |
| 58 |
if grad > 1: |
| 59 |
inds += hess_inds |
| 60 |
|
| 61 |
return {k: np.zeros((nvals, npoints)) for k in inds} |
| 62 |
|
| 63 |
|
| 64 |
ncart = int((L + 1) * (L + 2) / 2) |
| 65 |
nsph = L * 2 + 1 |
| 66 |
|
| 67 |
nvals = ncart |
| 68 |
if spherical: |
| 69 |
nvals = nsph |
| 70 |
|
| 71 |
coefs = np.arange(nprim, dtype=np.double) + 1 |
| 72 |
exps = np.arange(nprim, dtype=np.double) + 2 |
| 73 |
|
| 74 |
center = np.array([0, 0, 0], dtype=np.double) |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
gg_out = build_out(nvals, npoints, 0) |
| 80 |
tran_out = build_out(npoints, nvals, 0) |
| 81 |
t = time.time() |
| 82 |
if do_transpose: |
| 83 |
transpose_dict(gg_out, tran_out) |
| 84 |
|
| 85 |
gg.collocation(xyz, L, coefs, exps, center, grad=0, spherical=spherical, out=gg_out) |
| 86 |
|
| 87 |
ctime = (time.time() - t) |
| 88 |
|
| 89 |
|
| 90 |
t = time.time() |
| 91 |
np_out = gg.ref.collocation(xyz, L, coefs, exps, center, grad=0, spherical=spherical, cartesian_order="row") |
| 92 |
pytime = (time.time() - t) |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
print("PHI") |
| 97 |
compare(gg_out, np_out, 0) |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
print("C time %12.6f" % ctime) |
| 102 |
print("Py time %12.6f" % pytime) |
| 103 |
print("Ratio %12.6f" % (pytime / ctime)) |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
print("\nDerivative") |
| 108 |
|
| 109 |
gg_out = build_out(nvals, npoints, 1) |
| 110 |
tran_out = build_out(npoints, nvals, 1) |
| 111 |
t = time.time() |
| 112 |
if do_transpose: |
| 113 |
transpose_dict(gg_out, tran_out) |
| 114 |
gg.collocation(xyz, L, coefs, exps, center, grad=1, spherical=spherical, out=gg_out) |
| 115 |
ctime = (time.time() - t) |
| 116 |
|
| 117 |
|
| 118 |
t = time.time() |
| 119 |
np_out = gg.ref.collocation(xyz, L, coefs, exps, center, grad=1, spherical=spherical, cartesian_order="row") |
| 120 |
pytime = (time.time() - t) |
| 121 |
|
| 122 |
compare(gg_out, np_out, 1) |
| 123 |
|
| 124 |
print("C time %12.6f" % ctime) |
| 125 |
print("Py time %12.6f" % pytime) |
| 126 |
print("Ratio %12.6f" % (pytime / ctime)) |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
print("\nHessian") |
| 131 |
gg_out = build_out(nvals, npoints, 2) |
| 132 |
tran_out = build_out(npoints, nvals, 2) |
| 133 |
t = time.time() |
| 134 |
gg.collocation(xyz, L, coefs, exps, center, grad=2, spherical=spherical, out=gg_out) |
| 135 |
if do_transpose: |
| 136 |
transpose_dict(gg_out, tran_out) |
| 137 |
ctime = (time.time() - t) |
| 138 |
|
| 139 |
|
| 140 |
t = time.time() |
| 141 |
np_out = gg.ref.collocation(xyz, L, coefs, exps, center, grad=2, spherical=spherical, cartesian_order="row") |
| 142 |
pytime = (time.time() - t) |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
compare(gg_out, np_out, 2) |
| 148 |
|
| 149 |
print("C time %12.6f" % ctime) |
| 150 |
print("Py time %12.6f" % pytime) |
| 151 |
print("Ratio %12.6f" % (pytime / ctime)) |