-
Notifications
You must be signed in to change notification settings - Fork 17
/
sigma.py
executable file
·187 lines (160 loc) · 6.01 KB
/
sigma.py
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
#!/usr/bin/env python3
import os
import sys
import glob
import argparse
import tempfile
import numpy as np
from mctools.fluka import line
from math import log10
from shutil import which
from contextlib import redirect_stdout
import ROOT
ROOT.PyConfig.IgnoreCommandLineOptions = True
def inp(projectile, material, energy, include):
""" Prints a single input file """
print("TITLE")
print("FLUKA cross section generator")
line("DEFAULTS",sdum="PRECISIO")
line("BEAM",w1=f"-{energy:.4f}",sdum=projectile)
line("BEAMPOS",w2="5.0e4",w5="1.0")
line("GEOBEGIN",sdum="COMBNAME")
print("""0 0 Sphere
SPH sphere 0.0 0.0 0.0 1.0e-8
END
Target 1 +sphere
Out 3 -sphere
END
GEOEND
ASSIGNMA BLCKHOLE Out""")
line("ASSIGNMA",material,"Target")
if include:
with open(include) as f:
for l in f:
l = l.strip()
if len(l)>0:
print(l)
line("RANDOMIZ", w1=1.0, w2=1.0)
line("START", w1=1.0)
line("STOP")
def generateInputs(args,tmp):
""" Generate input files """
step = (log10(args.emax) - log10(args.emin))/(args.npoints-1)
elist = np.arange(log10(args.emin), log10(args.emax), step).tolist()
elist.append(log10(args.emax))
for i, e in enumerate(elist):
E = pow(10, e)
fname = f"sigma-{i:0>3}.inp"
fname = os.path.join(tmp,fname);
with open(fname, "w") as f:
with redirect_stdout(f):
inp(args.projectile, args.material, E, args.include)
def run(tmp):
assert which("parallel"), "GNU parallel is not installed"
command = f"cd {tmp} && parallel --bar $FLUPRO/flutil/rfluka -N0 -M1 ::: sigma*inp"
return_value = os.system(command)
def parse(fname, material):
""" Parse the given FLUKA output file """
E0 = -1.0 # negative = not set
inTable = False
d = {}
with open(fname) as f:
for line in f:
line = line.strip()
if E0<0.0 and line.find("***** BEAM ")>0:
w = line.split()
E0 = -float(w[6])
if line.find("== Material composition")>0:
inTable = True
if inTable and line.find(material)>0:
w = line.split()
d[str(E0)] = w[2:]
return d
def fixEmin(args):
emin = args.emin
if args.projectile == "PROTON":
emin = max(args.emin, 1e-4) # 100 keV
elif args.projectile == "ELECTRON":
emin = max(args.emin, 7e-5) # 70 keV
elif args.projectile == "PHOTON":
emin = max(args.emin, 1e-6) # 1 keV
if emin>args.emin:
print(f"Warning: emin has been reduced to the min primary energy of {emin} GeV",file=sys.stderr)
print(" Press Enter to continue",file=sys.stderr)
input()
return emin
def main():
""" A simple FLUKA cross section plotter """
parser = argparse.ArgumentParser(description=main.__doc__,
epilog="Homepage: https://github.com/kbat/mc-tools")
parser.add_argument('projectile', type=str, help='Incident particle')
parser.add_argument('material', type=str, help='Material')
parser.add_argument('npoints', type=int, help='Number of energy evaluation points (log. equal intervals between Emin and Emax are assumed)')
parser.add_argument('emin', type=float, help='Min energy [GeV]')
parser.add_argument('emax', type=float, help='Max energy [GeV]')
parser.add_argument('-include', default=None, dest='include', help='FLUKA deck to include (e.g. with custom material definition)', required=False)
parser.add_argument('-o', default=None, dest='out', help='Output file name (root, xml, txt, tex, pdf, png, etc)', required=False)
args = parser.parse_args()
args.emin = fixEmin(args)
assert args.emin < args.emax, "emin > emax"
assert args.npoints > 1, "Number of energy points is too small"
data = {}
with tempfile.TemporaryDirectory(prefix="sigma.") as tmp:
generateInputs(args,tmp)
run(tmp)
outputs = os.path.join(tmp,"*.out")
for out in glob.glob(outputs):
d = parse(out, args.material)
data.update(d)
energies = list(data.keys())
energies.sort()
d = {e: data[e] for e in energies}
grIn = ROOT.TGraph()
grIn.SetTitle("Inelastic;Energy [GeV]")
grIn.SetMarkerStyle(ROOT.kFullCircle)
grIn.SetMarkerColor(ROOT.kGreen+1)
grEl = ROOT.TGraph()
grEl.SetTitle("Elelastic;Energy [GeV]")
grEl.SetMarkerStyle(ROOT.kFullCircle)
grEl.SetMarkerColor(ROOT.kRed)
grX0 = ROOT.TGraph()
grX0.SetTitle("X0;Energy [GeV]")
grX0.SetMarkerStyle(ROOT.kFullCircle)
grX0.SetMarkerColor(ROOT.kBlue)
grTotal = ROOT.TGraph()
grTotal.SetTitle("Inel+El;Energy [GeV]")
grTotal.SetMarkerStyle(ROOT.kFullCircle)
grTotal.SetMarkerColor(ROOT.kBlack)
for e in d:
E0 = float(e)
data = d[e]
A,rho = map(float, data[1:3])
sigma_in, sigma_el, sigma_X0 = (A/(float(data[i])*ROOT.TMath.Na()*rho) * 1.0e24 for i in (3,4,5))
grIn.AddPoint(E0, sigma_in);
grEl.AddPoint(E0, sigma_el);
grX0.AddPoint(E0, sigma_X0);
grTotal.AddPoint(E0, sigma_in+sigma_el);
mg = ROOT.TMultiGraph("mg", f"{args.projectile} on {args.material};Energy [GeV];#sigma [barn]")
leg = ROOT.TLegend(0.903, 0.78, 0.99, 0.9)
mg.Add(grTotal, "p")
mg.Add(grIn, "p")
mg.Add(grEl, "p")
leg.AddEntry(grTotal, grTotal.GetTitle())
leg.AddEntry(grIn, grIn.GetTitle())
leg.AddEntry(grEl, grEl.GetTitle())
mg.Draw("ap")
leg.Draw()
ROOT.gPad.SetGrid()
ROOT.gPad.Update()
if args.out:
if args.out.endswith(".root") or args.out.endswith(".xml") or args.out.endswith(".C"):
mg.SaveAs(args.out)
elif args.out.endswith(".txt"):
with open(args.out, "w") as f:
for e in d:
print(e, " ".join(map(str, d[e])), file=f)
else:
ROOT.gPad.Print(args.out)
input()
if __name__ == "__main__":
sys.exit(main())