def test_onemember(): """ Test the one-central-member of pdfflow """ # Check the central member pdf = mkPDF(f"{PDFNAME}/0") pdfflow_tester(pdf) # Try a non-central member, but trace first # Ensure it is not running eagerly run_eager(False) pdf = mkPDF(f"{PDFNAME}/1") pdf.trace() pdfflow_tester(pdf)
def test_accuracy(atol=1e-6): """ Check the accuracy for all PDF sets for all members in the lists LIST_PDF and MEMBERS for all defined ranges of Q for all flavours is better than atol. This test doesnt care about Q extrapolation """ import tensorflow as tf tf.config.experimental_run_functions_eagerly(True) for setname in LIST_PDF: for member in range(MEMBERS): pdfset = f"{setname}/{member}" logger.info(" > Checking %s", pdfset) pdfflow = pdf.mkPDF(pdfset, f"{DIRNAME}/") for qi, qf in QS: # Dont test extrapolation qi = max(qi, pdfflow.q2min) qf = min(qf, pdfflow.q2max) q2arr = gen_q2(qi, qf) logger.info(" Q2 from %f to %f", qi, qf) flow_values = pdfflow.py_xfxQ2(FLAVS, XARR, q2arr) lhapdf_values = get_pdfvals(XARR, q2arr, pdfset) for i, f in enumerate(FLAVS): np.testing.assert_allclose(flow_values[:, i], lhapdf_values[f], atol=atol) tf.config.experimental_run_functions_eagerly(False)
def test_one_multi(): """ Test that the multimember-I is indeed the same as just the Ith instance """ run_eager(True) pdf = mkPDF(f"{PDFNAME}/0") multi_pdf = mkPDFs(PDFNAME, [4, 0, 6]) grid_size = 4 x = np.random.rand(grid_size) q2 = np.random.rand(grid_size) * 1000.0 res_1 = pdf.py_xfxQ2_allpid(x, q2) res_2 = multi_pdf.py_xfxQ2_allpid(x, q2) np.testing.assert_allclose(res_1, res_2[1])
def test_alpha_trace(): """ Check that the alpha_s can be traced and then instantiated """ # Ensure the functions are not run eagerly run_eager(False) setname = LIST_PDF[0] # Do it for one single replica pdfset = f"{setname}/0" pex = pdf.mkPDF(pdfset, f"{DIRNAME}/") pex.alphas_trace() # Do it for many replicas pex2 = pdf.mkPDFs(setname, [0, 1, 2]) pex2.alphas_trace()
def test_alphas_q2(atol=1e-6): """ Check the accuracy for all PDF sets for all members given when computing alpha_s given Q is compatible within atol between pdfflow and LHAPDF This test does not run eagerly """ for setname in LIST_PDF: for member in range(MEMBERS): pdfset = f"{setname}/{member}" logger.info(" > Checking %s", pdfset) pdfflow = pdf.mkPDF(pdfset, f"{DIRNAME}/") for qi, qf in QS: qi = max(qi, pdfflow.q2min) qf = min(qf, pdfflow.q2max) q2arr = gen_q2(qi, qf) logger.info(" Q2 from %f to %f", qi, qf) flow_values = pdfflow.py_alphasQ2(q2arr) lhapdf_values = get_alphavals(q2arr, pdfset, sq2=True) np.testing.assert_allclose(flow_values, lhapdf_values, atol=atol)
def compare_alphas(pdfname, ax): """ Computes the alphas difference pdfflow vs lhapdf and returns axes for plots Parameters: pdfname: string ax: matplotlib.axes.Axes object Return: matplotlib.axes.Axes object """ p = pdf.mkPDF(pdfname, DIRNAME) name = '\_'.join(pdfname.split('_')) s = time.time() p.alphas_trace() print("".join([ f"\nPDFflow alphas, pdf set:{pdfname}", f"\n\tBuilding graph time: {time.time()-s}\n" ])) if p is None: p = pdf.mkPDF(pdfname, DIRNAME) l_pdf = lhapdf.mkPDF(pdfname) name = '\_'.join(pdfname.split('_')) q = np.logspace(-3, 9, 10000, dtype=float) s_time = time.time() vl = np.array([l_pdf.alphasQ(iq) for iq in q]) l_time = time.time() vp = p.py_alphasQ(q) p_time = time.time() ax.plot(q, np.abs(vp - vl) / (np.abs(vl) + EPS)) ax.hlines(1e-3, plt.xlim()[0], plt.xlim()[1], linestyles='dotted', color='red') ax.set_xscale('log') ax.set_yscale('log') ax.set_xlim([1e-3, 1e9]) ax.set_ylim([EPS, 0.01]) ax = set_ticks(ax, -3, 9, 13, 'x', 4) ax.tick_params(axis='x', which='both', direction='in', bottom=True, labelbottom=True, top=True, labeltop=False) ax = set_ticks(ax, -15, -3, 16, 'y') ax.title.set_text(r'%s, $\alpha_s(Q)$' % name) ax.set_xlabel(r'$Q$', fontsize=17) print("\nDry run time comparison for pdf %s:" % pdfname) print("{:>10}:{:>15.8f}".format("lhapdf", l_time - s_time)) print("{:>10}:{:>15.8f}".format("pdfflow", p_time - l_time)) return ax
def mkpdf(fname, dirname): """Generate a PDF givena PDF name and a directory.""" pdfset = ffi.string(fname).decode('utf-8') path = ffi.string(dirname).decode('utf-8') global pdf pdf = pflow.mkPDF(pdfset, path)
def accumulate_times(pdfname, dev0, dev1, no_lhapdf): """ Computes performance times Parameters: dev0: str, device name over which run pdfflow dev1: str, device name over which run pdfflow """ with tf.device(dev0): p0 = pdf.mkPDF(pdfname, DIRNAME) p0.trace() with tf.device(dev1): p1 = pdf.mkPDF(pdfname, DIRNAME) p1.trace() if no_lhapdf: l_pdf = lhapdf.mkPDF(pdfname) else: l_pdf = None xmin = np.exp(p0.grids[0][0].log_xmin) xmax = np.exp(p0.grids[0][0].log_xmax) q2min = np.sqrt(np.exp(p0.grids[0][0].log_q2min)) q2max = np.sqrt(np.exp(p0.grids[0][-1].log_q2max)) t_pdf0 = [] t_pdf1 = [] t_lha = [] n = np.linspace(1e5, 1e6, 20) for j in range(10): t0 = [] t1 = [] t2 = [] for i in tqdm.tqdm(n): a_x = np.random.uniform(xmin, xmax, [ int(i), ]) a_q2 = np.exp( np.random.uniform(np.log(q2min), np.log(q2max), [ int(i), ])) with tf.device(dev0): t_ = test_pdfflow(p0, a_x, a_q2) t0 += [t_] with tf.device(dev1): t_ = test_pdfflow(p1, a_x, a_q2) t1 += [t_] t_ = test_lhapdf(l_pdf, a_x, a_q2) if no_lhapdf else [] t2 += [t_] t_pdf0 += [t0] t_pdf1 += [t1] t_lha += [t2] t_pdf0 = np.stack(t_pdf0) t_pdf1 = np.stack(t_pdf1) t_lha = np.stack(t_lha) return n, t_pdf0, t_pdf1, t_lha
def main(pdfname, pid, no_tex=True): """Testing PDFflow vs LHAPDF performance.""" mpl.rcParams['text.usetex'] = no_tex mpl.rcParams['savefig.format'] = 'pdf' mpl.rcParams['figure.figsize'] = [11, 5.5] mpl.rcParams['axes.titlesize'] = 20 mpl.rcParams['ytick.labelsize'] = 17 mpl.rcParams['xtick.labelsize'] = 17 mpl.rcParams['legend.fontsize'] = 14 import pdfflow.pflow as pdf p = pdf.mkPDF(pdfname, DIRNAME) l_pdf = lhapdf.mkPDF(pdfname) name = '\_'.join(pdfname.split('_')) s = time.time() p.trace() print("\nPDFflow\n\tBuilding graph time: %f\n" % (time.time() - s)) fig = plt.figure() gs = fig.add_gridspec(nrows=1, ncols=2, wspace=0.05) ax = fig.add_subplot(gs[0]) x = np.logspace(-12, 0, 10000, dtype=float) q2 = np.array([1.65, 1.7, 4.92, 1e2, 1e3, 1e4, 1e5, 1e6, 2e6], dtype=float)**2 for iq2 in q2: vl = np.array([l_pdf.xfxQ2(pid, ix, iq2) for ix in x]) vp = p.py_xfxQ2(pid, float_me(x), float_me([iq2] * len(x))) ax.plot(x, np.abs(vp - vl) / (np.abs(vl) + EPS), label=r'$Q=%s$' % sci_notation(iq2**0.5, 2)) ax.hlines(1e-3, plt.xlim()[0], plt.xlim()[1], linestyles='dotted', color='red') ax.set_xscale('log') ax.set_yscale('log') ax.set_xlim([1e-12, 1.]) ax.set_ylim([EPS, .01]) ax = set_ticks(ax, -12, 0, 13, 'x', 4) ax.tick_params(axis='x', which='both', direction='in', bottom=True, labelbottom=True, top=True, labeltop=False) ax = set_ticks(ax, -15, -3, 16, 'y') ax.tick_params(axis='y', which='both', direction='in', left=True, labelleft=True, right=True, labelright=False) ax.set_title(r'%s, flav = %d' % (name, pid)) ylabel = r'$\displaystyle{r_{i}(x,Q)}$' if no_tex else '$r_{i}$(x,Q)' ax.set_ylabel(ylabel, fontsize=20) ax.set_xlabel(r'$x$', fontsize=17) ax.legend(frameon=False, ncol=2, loc='upper right', bbox_to_anchor=(1.02, 0.9)) x = np.array([1e-10, 1e-9, 1.1e-9, 5e-7, 1e-6, 1e-4, 1e-2, 0.5, 0.99], dtype=float) q2 = np.logspace(1, 7, 10000, dtype=float)**2 ax = fig.add_subplot(gs[1]) for ix in x: s_time = time.time() vl = np.array([l_pdf.xfxQ2(pid, ix, iq2) for iq2 in q2]) l_time = time.time() vp = p.py_xfxQ2(pid, float_me([ix] * len(q2)), float_me(q2)) p_time = time.time() ax.plot(q2**0.5, np.abs(vp - vl) / (np.abs(vl) + EPS), label=r'$x=%s$' % sci_notation(ix, 1)) ax.hlines(1e-3, plt.xlim()[0], plt.xlim()[1], linestyles='dotted', color='red') ax.set_xscale('log') ax.set_yscale('log') ax.set_xlim([1, 1e7]) ax.set_ylim([EPS, .01]) ax = set_ticks(ax, 1, 7, 9, 'x') ax.tick_params(axis='x', which='both', direction='in', top=True, labeltop=False, bottom=True, labelbottom=True) ax = set_ticks(ax, -15, -3, 16, 'y') ax.tick_params(axis='y', which='both', direction='in', right=True, labelright=False, left=True, labelleft=False) ax.set_title(r'%s, flav = %d' % (name, pid)) ax.set_xlabel(r'$Q$', fontsize=17) ax.legend(frameon=False, ncol=2, loc='upper right', bbox_to_anchor=(1.02, 0.9)) plt.savefig('diff_%s_flav%d.pdf' % (pdfname.replace('/', '-'), pid), bbox_inches='tight', dpi=250) plt.close() print("\nDry run time comparison:") print("{:>10}:{:>15.8f}".format("lhapdf", l_time - s_time)) print("{:>10}:{:>15.8f}".format("pdfflow", p_time - l_time))
mt = tf.constant(173.2, dtype=DTYPE) # center of mass energy sqrts = tf.constant(8000, dtype=DTYPE) # minimum allowed center of mass energy sqrtsmin = tf.constant(173.2, dtype=DTYPE) # W-boson mass mw = tf.constant(80.419, dtype=DTYPE) # gaw gaw = tf.constant(2.1054, dtype=DTYPE) # GF gf = tf.constant(1.16639e-5, dtype=DTYPE) # load pdf DIRNAME = sp.run(['lhapdf-config', '--datadir'], stdout=sp.PIPE, universal_newlines=True).stdout.strip('\n') + '/' pdf = mkPDF(pdfset, DIRNAME) # nx = 100 # examplex = np.random.rand(nx) # exampleq = np.linspace(5,nx, nx) # r = pdf.xfxQ2([5,4,5], examplex, exampleq) # import ipdb # ipdb.set_trace() # auxiliary variables colf_bt = tf.constant(9, dtype=DTYPE) mt2 = tf.square(mt) s = tf.square(sqrts) s2 = tf.square(s) smin = tf.square(sqrtsmin) bmax = tf.sqrt(1 - smin / s)
# wait until pineappl has filled the grids properly pool.close() pool.join() end = time.time() print(f"Pool took: time (s): {end-start}") if args['pineappl']: # write the grid to disk filename = 'DY-LO-AA.pineappl' print(f'Writing PineAPPL grid to disk: {filename}') grid.write(filename) # check convolution # load pdf for testing pdf = mkPDF('NNPDF31_nlo_as_0118_luxqed/0') def xfx(id, x, q2, p): return pdf.py_xfxQ2([id], [x], [q2]) def alphas(q2, p): return pdf.py_alphasQ2([q2]) # perform convolution dxsec = grid.convolute(xfx, xfx, alphas, None, None, 1.0, 1.0) for i in range(len(dxsec)): print(f'{bins[i]:.1f} {bins[i + 1]:.1f} {dxsec[i]:.3e}') end = time.time() print(f"Total time (s): {end-start}")