• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

APN-Pucky / feynamp / 9267239762

28 May 2024 09:53AM UTC coverage: 80.215% (-2.1%) from 82.348%
9267239762

Pull #67

github

APN-Pucky
fix: PREFACTOR
Pull Request #67: add: spin correlation

13 of 43 new or added lines in 5 files covered. (30.23%)

31 existing lines in 3 files now uncovered.

896 of 1117 relevant lines covered (80.21%)

0.8 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

64.13
/feynamp/form/form.py
1
import os
1✔
2
import re
1✔
3
import subprocess
1✔
4
import tempfile
1✔
5

6
import form
1✔
7
from pqdm.threads import pqdm
1✔
8

9
count = 0
1✔
10
dummy = 0
1✔
11
# TODO auto generate symbols
12
init = """
1✔
13
Symbols Pi,G,ZERO,Tr,Nc,Cf,CA,MC,ee,realpart,PREFACTOR;
14
AutoDeclare Index Mu,Spin,Pol,Propagator;
15
AutoDeclare Symbol Mass,fd,mss,mst,msu;
16
AutoDeclare Vector Mom;
17
Tensors colorcorrelation;
18
Tensors Metric(symmetric),df(symmetric),da(symmetric),Identity(symmetric);
19
Function ProjM,ProjP,VF,xg,xgi,P,dg,dgi,xeg,xegi;
20
CFunctions Den,Denom,P,Gamma,u,v,ubar,vbar,eps,epsstar,VC,VA,VPol,GammaId, GammaCollect, GammaIdCollect;
21
Indices a,o,n,m,tm,tn,beta,b,m,betap,alphap,a,alpha,ind,delta,k,j,l,c,d,e;
22
"""
23

24

25
def get_dummy_index(underscore=True, questionmark=True):
1✔
26
    global dummy
27
    dummy = dummy + 1
1✔
28
    return f"N{dummy}" + ("_" if underscore else "") + ("?" if questionmark else "")
1✔
29

30

31
def string_to_form(s):
1✔
32
    try:
1✔
33
        s = re.sub(r"complex\((.*?),(.*?)\)", r"(\1+i_*(\2))", s)
1✔
34
        # s = s.replace("complex(0,1)", "i_")  # form uses i_ for imaginary unit
35
        s = s.replace("Gamma_Id", "GammaId")
1✔
36
        s = s.replace("u_bar", "ubar")
1✔
37
        s = s.replace("v_bar", "vbar")
1✔
38
        s = s.replace("eps_star", "epsstar")
1✔
39
        s = s.replace(
1✔
40
            "Identity", "df"
41
        )  # TODO check if this holds or also happens for anti
42
        s = s.replace("ZERO", "0")
1✔
43
        s = s.replace(".*", "*")  # handle decimals
1✔
44
        s = s.replace(".)", ")")  # handle decimals
1✔
UNCOV
45
    except Exception as e:
×
UNCOV
46
        print("Error in string_to_form", e)
×
UNCOV
47
        print(s)
×
UNCOV
48
        raise e
×
49
    return s
1✔
50

51

52
def run_parallel(*args, **kwargs):
1✔
53
    # return run_parallel_v1(*args, **kwargs)
54
    return run_parallel_new(*args, **kwargs)
1✔
55

56

57
def run_parallel_new(
1✔
58
    init, cmds, variables, show=False, keep_form_file=True, threads=None
59
):
60
    global count
61
    count = count + 1
1✔
62
    rets = []
1✔
63
    if threads is None:
1✔
64
        threads = os.cpu_count()
1✔
65
    return pqdm(
1✔
66
        ["" + init + f"Local TMP = {var};" + cmds for var in variables],
67
        run_bare,
68
        n_jobs=threads,
69
    )
70

71

72
def run_parallel_v1(
1✔
73
    init, cmds, variables, show=False, keep_form_file=True, threads=None
74
):
75
    global count
UNCOV
76
    count = count + 1
×
UNCOV
77
    rets = []
×
UNCOV
78
    if threads is None:
×
UNCOV
79
        threads = os.cpu_count()
×
UNCOV
80
    with form.open(keep_log=1000, args=["tform", f"-w{threads}"]) as f:
×
UNCOV
81
        txt = "" + init
×
82
        for i, s in enumerate(variables):
×
83
            txt += f"Local TMP{i} = {s};\n"
×
84
        txt += cmds
×
85
        for i, s in enumerate(variables):
×
86
            # Not sure why sort is needed, but it is needed
87
            txt += f"print TMP{i};.sort;"
×
88
        if keep_form_file:
×
89
            with open("form" + str(count) + ".frm", "w") as frm:
×
90
                frm.write(txt)
×
91
        f.write(txt)
×
UNCOV
92
        for i, s in enumerate(variables):
×
93
            rets.append(f.read(f"TMP{i}"))
×
94
        # What is this ?
95
        # r = re.sub(r"\+factor_\^?[0-9]*", r"", r).strip("*")
96
        if show:
×
97
            for r in rets:
×
98
                print(r + "\n")
×
99
        assert len(rets) == len(variables)
×
UNCOV
100
        return rets
×
101

102

103
def run_bare(s, show=False, keep_form_file=True):
1✔
104
    """Run it just as a subprocess"""
105
    # print("Running bare form")
106
    # make temporary file
107
    with tempfile.NamedTemporaryFile(
1✔
108
        "w", suffix=".frm", delete=not keep_form_file
109
    ) as f:
110
        local = s.split("Local")[1].split("=")[0].strip()
1✔
111
        txt = s + "print " + local + ";.sort;"
1✔
112
        f.write(txt)
1✔
113
        # flush it
114
        f.flush()
1✔
115
        # run form on file and capture output
116
        out = subprocess.check_output(["form", f.name])
1✔
117
        res = re.findall(local + r"\s+=(.*?);", out.decode(), re.DOTALL)
1✔
118
        if len(res) != 2:
1✔
UNCOV
119
            raise ValueError(
×
120
                f"Error in form output. found {len(res)} in {out.decode()}"
121
            )
122
        # print("bare form output", res.group(2))
123
        # print("Finished bare form", res)
124
        ret = res[1].replace("\n", "").replace(" ", "")
1✔
125
        return ret
1✔
126

127

128
def run(s, show=False, keep_form_file=True, threads=1):
1✔
129
    global count
130
    count = count + 1
1✔
131
    if threads is None:
1✔
UNCOV
132
        threads = os.cpu_count()
×
133
    with form.open(keep_log=1000, args=["tform", f"-w{threads}"]) as f:
1✔
134
        local = s.split("Local")[1].split("=")[0].strip()
1✔
135
        # no clue why sort
136
        txt = s + "print " + local + ";.sort;"
1✔
137
        if keep_form_file:
1✔
138
            with open("form" + str(count) + ".frm", "w") as frm:
1✔
139
                frm.write(txt)
1✔
140
        f.write(txt)
1✔
141
        # frm.write(txt)
142
        r = f.read("" + local)
1✔
143
        r = re.sub(r"\+factor_\^?[0-9]*", r"", r).strip("*")
1✔
144
        if show:
1✔
UNCOV
145
            print(r + "\n")
×
146
        return r
1✔
147

148

149
def sympyfy(string_expr):
1✔
UNCOV
150
    from sympy import simplify
×
151
    from sympy.parsing.sympy_parser import parse_expr
×
152

UNCOV
153
    ret = simplify(
×
154
        parse_expr(
155
            string_expr.replace("Mom_", "")
156
            .replace(".", "_")
157
            .replace("^", "**")
158
            .replace("mss", "s")
159
            .replace("msu", "u")
160
            .replace("mst", "t")
161
        )
162
    )
163
    return simplify(ret.subs("Nc", "3").subs("Cf", "4/3"))
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc