-
Notifications
You must be signed in to change notification settings - Fork 0
/
pn.py
executable file
·297 lines (246 loc) · 9.48 KB
/
pn.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/usr/bin/env ipython2
import numpy as np
import scipy.special as ss
import scipy.interpolate as sint
from statepoint import StatePoint
from matplotlib import pyplot as plt
from uncertainties import ufloat
from gen_mgxs import mgxs
import pickle
from bisect import bisect
import os
import sys
def Pn_solve(sigtn, sigsn, Qn, deriv_term):
# d/dx[(n/(2n+1))*psinm1+(n+1)/(2n+1)*psip1]+sigtn*psin=sigsn*psin+Qn
# deriv_term+sigtn*psin=sigsn*psin+Qn
# psin = (Qn-deriv_term)/(sigtn-sigsn)
# psin = (Qn - deriv_term) / (sigtn - np.sum(sigsn,axis=0))
psin = (Qn - deriv_term) / (sigtn)
return psin[:]
def solve1g(N, sigtns1g, sigsns1g, Qnsg, psinsg, x, invdx, n_ratios):
# Loops through each of the n orders, sets up the derivitave term,
# and calls Pn_solve on it.
# n_ratios is [(n/(2n+1),(n+1)/(2n+1)) for n in range(N+1)]
for n in xrange(N+1):
# N+1 so we get an n==N in this loop
# Set up the deriv_term.
# Due to assumed reflective BC, deriv_term will always be 0 for
# ix ==0 and ix == last one, so we can skip those
if n > 0:
nm1_interp = sint.KroghInterpolator(x, psinsg[n - 1])
else:
nm1_interp = sint.KroghInterpolator([x[0],x[-1]],[0.0, 0.0])
if n < N:
np1_interp = sint.KroghInterpolator(x, psinsg[n + 1])
else:
np1_interp = sint.KroghInterpolator([x[0],x[-1]],[0.0, 0.0])
deriv_term = n_ratios[n][0] * nm1_interp.derivative(x) + \
n_ratios[n][1] * np1_interp.derivative(x)
# Now adjust for BC
deriv_term[0] = 0.0
deriv_term[-1] = 0.0
# Now we can pass this to Pn_solve to get our new psin values
psinsg[n,:] = Pn_solve(sigtns1g[n], sigsns1g[n], Qnsg[n], deriv_term)
return psinsg[:,:]
def fixedsrc(N, G, sigtns, sigsns, Qns, psins, x, invdx, n_ratios, eps_psi, max_inner):
# Not implemented yet. This wll be the MG solver.
eps = 1.0E4
iter = 0
newQns = np.zeros_like(Qns[:,0,:])
# import pdb; pdb.set_trace()
while ((eps > eps_psi) and (iter <= max_inner)):
# Develop scattering source
for g in range(G):
for n in range(N):
for ix in range(len(invdx) + 1):
newQns[n,ix] = Qns[g,n,ix] + \
np.dot(sigsns[:,n,g,ix], psins[:,n,ix])
# Run fixed src solver
psins[g,:,:] = solve1g(N, sigtns[g,:,:], sigsns[g,:,:,:], newQns,
psins[g,:,:], x, invdx, n_ratios)
# eps =
iter += 1
for g in xrange(G):
plt.plot(x,psins[g,0,:],label='Pn')
plt.plot(x,omcflux[g,0,:],label='OMC')
plt.legend(loc='best')
plt.show()
plt.close()
print "Inner Iterations = " + str(iter)
def init(x, G, N, flux_guess):
invdx = np.zeros(len(x) - 1)
for ix in xrange(len(invdx)):
invdx[ix] = 1.0 / (x[ix + 1] - x[ix])
n_ratios = [(float(n)/float(2 * n + 1), float(n + 1)/float(2 * n + 1))
for n in range(N + 1)]
psins = np.ones(shape=(G, N + 1, len(x)))
for g in xrange(G):
for n in xrange(N + 1):
psins[g,n,:] = flux_guess[g,n,:] / np.sum(flux_guess[g,n,:])
return invdx, n_ratios, psins
def get_openmc_mesh(spFile, tid, sid, G, N, extent):
sp = StatePoint(spFile)
sp.read_results()
sp.generate_stdev()
keff = ufloat(sp.k_combined[0], sp.k_combined[1])
GN = [[0.0 for n in xrange(N)] for g in xrange(G)]
data = np.array(GN[:][:])
dx = extent / float(N)
x = [(float(i) + 0.5) * dx for i in xrange(N)]
for g in xrange(G):
myg = G - g - 1
for n in xrange(N):
m, u = sp.get_value(tid, [('mesh',(1,1,n+1)),('energyin',g)], sid)
data[myg,n] = m
return x, data[:,:], keff
def get_openmc_mesh_matrix(spFile, tid, sid, G, N, extent):
sp = StatePoint(spFile)
sp.read_results()
sp.generate_stdev()
keff = ufloat(sp.k_combined[0], sp.k_combined[1])
GGN = [[[0.0 for n in xrange(N)] for go in xrange(G)] for g in xrange(G)]
data = np.array(GGN[:][:][:])
dx = extent / float(N)
x = [(float(i) + 0.5) * dx for i in xrange(N)]
for g in xrange(G):
myg = G - g - 1
for go in xrange(G):
mygo = G - go - 1
for n in xrange(N):
m, u = sp.get_value(tid, [('mesh',(1,1,n+1)),('energyin',g),
('energyout',go)], sid)
data[myg,mygo,n] = m
return x, data[:,:,:], keff
def get_omc_mgxs(sp, mesh_tids, mesh_sids, order, G, Nmesh, extent, xstype):
# Get flux-yN
fluxyn = np.zeros(shape=(order, G, Nmesh))
for l in range(order):
tid = mesh_tids[0]
sid = mesh_sids[0][l]
x, fluxyn[l,:,:], omck = get_openmc_mesh(sp,tid,sid,G,Nmesh,extent)
# Get scatt-pN
scattpn = np.zeros(shape=(order, G, G, Nmesh))
for l in range(order):
tid = mesh_tids[1]
sid = mesh_sids[1][l]
x, scattpn[l,:, :, :], omck = get_openmc_mesh_matrix(sp,tid,sid,G,Nmesh,extent)
# Get scatt-yN
scattyn = np.zeros(shape=(order, G, G, Nmesh))
for l in range(order):
tid = mesh_tids[2]
sid = mesh_sids[2][l]
x, scattyn[l,:,:,:], omck = get_openmc_mesh_matrix(sp,tid,sid,G,Nmesh,extent)
# Get total-yN
totalyn = np.zeros(shape=(order, G, Nmesh))
for l in range(order):
tid = mesh_tids[3]
sid = mesh_sids[3][l]
x, totalyn[l,:,:], omck = get_openmc_mesh(sp,tid,sid,G,Nmesh,extent)
# Get nu-fission (right now only doing iso weighting)
nusigfns = np.zeros(shape=(order, G, G, Nmesh))
tid = mesh_tids[4]
sid = mesh_sids[4][0]
# Now only doing iso weighting so l=0
x, nusigfns[0,:,:,:], omck = get_openmc_mesh_matrix(sp,tid,sid,G,Nmesh,extent)
Qns = np.zeros(shape=(order, G, Nmesh))
# put Q in nusigfns, leave as isotropic now
l = 0
Qsum = 0.0
for go in range(G):
for n in range(Nmesh):
Qns[l,go,n] = 0.0
for g in range(G):
Qns[l,go,n] += nusigfns[0,g,go,n]
Qsum += Qns[l,go,n]
Qns[l,:,:] /= Qsum
for l in range(1,order):
for g in range(G):
for n in range(Nmesh):
Qns[l,g,n] = 0.0
totaliso = totalyn[0,:,:]
for l in range(order):
for g in range(G):
for n in range(Nmesh):
# Nmeshormalize by flux
flux = fluxyn[l,g,n]
flux0 = fluxyn[0,g,n]
if flux0 != 0.0:
for go in range(G):
scattpn[l,g,go,n] /= flux0
if l == 0:
totaliso[g,n] /= flux0
if flux != 0.0:
for go in range(G):
scattyn[l,g,go,n] /= flux
nusigfns[l,g,go,n] /= flux
totalyn[l,g,n] /= flux
# Apply correction
if xstype == 'consP':
corr = totaliso[g,n] - totalyn[l,g,n]
for go in range(G):
scattyn[l,g,go,n] += corr
if xstype == 'iso':
sigtns = [totaliso for l in range(order)]
sigsns = scattpn[:]
elif xstype == 'consP':
sigtns = [totaliso for l in range(order)]
sigsns = scattyn[:]
elif xstype == 'yN':
sigtns = totalyn[:]
sigsns = scattyn[:]
return omck, np.swapaxes(fluxyn,0,1), x, np.swapaxes(sigtns,0,1), \
np.swapaxes(sigsns,0,1), np.swapaxes(nusigfns,0,1), np.swapaxes(Qns,0,1)
if __name__ == "__main__":
rcdef = plt.rcParams.copy
newparams = {'savefig.dpi': 100, 'figure.figsize': (24, 13.5),
'font.size': 16}
plt.rcParams.update(newparams)
if len(sys.argv) != 3:
raise ValueError("Must Provide Cross-Section Type [consP, iso, yN] & " +
"Run Type [FS, k]!")
else:
xstype = sys.argv[1]
if xstype not in ["consP", "iso", "yN"]:
raise ValueError("Invalid Cross-Section Type!")
runtype = sys.argv[2]
if runtype not in ["FS", "k"]:
raise ValueError("Invalid Run Type!")
show = False
save = True
G = 4
N = 1
Nmesh = 16
extent = 0.64
sp = './statepoint.08000.binary'
eps_psi = 1.0E-6
max_inner = 2
# First get the mgxs data and create x/s
if xstype == 'iso':
momWgt = False
trcorr = None
elif xstype == 'consP':
momWgt = True
trcorr = 'consP'
elif xstype == 'yN':
momWgt = True
trcorr = None
mesh_tids = [0, 1, 1, 0, 2]
mesh_sids = [[0,2,6,12], [0,1,2,3], [4,6,10,16], [16,18,22,27], [0]]
omck, omcflux, x, sigtns, sigsns, nusigfns, Qns = \
get_omc_mgxs(sp, mesh_tids, mesh_sids, N+1, G, Nmesh, extent, xstype)
print 'OpenMC k_eff=' + "{:12.5E}".format(omck)
# Set up some of our data we will use during the sweep
invdx, n_ratios, psins = init(x, G, N, omcflux)
if runtype == 'FS':
fixedsrc(N, G, sigtns, sigsns, Qns, psins, x, invdx, n_ratios, eps_psi, max_inner)
# Estimate k to compare with the openMC k
pnk = 0.0
for g in xrange(G):
for ix in xrange(Nmesh):
if Qns[g,0,ix] > 0.0:
pnk += np.sum(nusigfns[g,0,:,ix])*psins[g,0,ix] / Qns[g,0,ix]
else:
print "k-eigenvalue solver not yet implemented!"
pcm = 1.0E5*(pnk-omck)/omck
print "Pn k_eff = " + "{:12.5E}".format(pnk)
print "pcm = " + "{:12.5E}".format(pcm)