-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjkcm_mcnpx_rmesh.py
159 lines (135 loc) · 4.75 KB
/
jkcm_mcnpx_rmesh.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
# -*- coding: utf-8 -*-
"""
Spyder Editor
Use this class to read in and process rmesh data output by mcnpx.
"""
import numpy as np
import os
import re
from matplotlib.mlab import griddata
import matplotlib.pyplot as plt
class jkcm_mcnpx_rmesh:
"""This class represents an rmesh object output by mcnpx."""
def __init__(self):
self.tally_values = np.zeros([1])
self.unc_values = np.zeros([1])
self.xb = np.zeros([2])
self.yb = np.zeros([2])
self.zb = np.zeros([2])
self.nps = np.longlong(0)
def _calcCenter(self, arr):
return(0.5*arr[0:-1]+0.5*arr[1:])
def xc(self):
return self._calcCenter(self.xb)
def yc(self):
return self._calcCenter(self.yb)
def zc(self):
return self._calcCenter(self.zb)
def nx(self):
return len(self.xb) - 1
def ny(self):
return len(self.yb) - 1
def nz(self):
return len(self.zb) - 1
def nxyz(self):
return self.nx()*self.ny()*self.nz()
def griddata_yplane(self, xVec, zVec, yindex=0):
"""return the output from griddata at the y index supplied.
xVec: positions to plot the data (e.g. np.linspace(-10,10,200))
yVec: positions to plot the data"""
xc = self.xc()
zc = self.zc()
nx = self.nx()
ny = self.ny()
nz = self.nz()
e = np.reshape(np.repeat(xc, nz), [nx, nz])
e1 = np.reshape(np.repeat(zc, nx), [nx, nz], order='F')
imgPlane = griddata(e.flatten(), e1.flatten(), o.tally_values[:,yindex,:].flatten(), xVec, zVec, interp='linear')
return(imgPlane)
def import_from_mdata_ascii(self, filename):
""" Example
o = jkcm_mcnpx_rmesh()
filename = "/home/justin/001m"
o.import_from_mdata_ascii(filename)
"""
f = open(filename, 'r')
data = f.read()
f.close()
mylines = data.splitlines()
#get nps associated with mdata
self.nps = np.longlong(mylines[0].split()[5])
#find the nx,ny,nz line
p = re.compile('^f .*')
j=0
for i in np.arange(len(mylines)):
if(p.match(mylines[i])):
j=i
break
q = mylines[j].split()
nxyz = np.longlong(q[1])
nx = np.long(q[3])
ny = np.long(q[4])
nz = np.long(q[5])
#allocate your arrays
self.tally_values= np.zeros([nxyz])
self.unc_values=np.zeros([nxyz])
self.xb = np.zeros([nx+1])
self.yb = np.zeros([ny+1])
self.zb = np.zeros([nz+1])
#read in xbounds
t=0
for i in np.arange((j+1), len(mylines)):
t= t + len(mylines[i].split())
if(t == (nx+1)):
break
temp=''
for k in np.arange((j+1),i+1):
temp += mylines[k]
self.xb = np.array(temp.split(), dtype=np.double)
print("min/max xb: {0},{1}".format(min(self.xb),max(self.xb)))
#read in ybounds
j=i
t=0
for i in np.arange((j+1), len(mylines)):
t= t + len(mylines[i].split())
if(t == (ny+1)):
break
temp=''
for k in np.arange((j+1),i+1):
temp += mylines[k]
self.yb = np.array(temp.split(), dtype=np.double)
print("min/max yb: {0},{1}".format(min(self.yb),max(self.yb)))
#read in zbounds
j=i
t=0
for i in np.arange((j+1), len(mylines)):
t= t + len(mylines[i].split())
if(t == (nz+1)):
break
temp=''
for k in np.arange((j+1),i+1):
temp += mylines[k]
self.zb = np.array(temp.split(), dtype=np.double)
print("min/max zb: {0},{1}".format(min(self.zb),max(self.zb)))
#advance to tally values
p = re.compile('^vals.*')
j=0
for i in np.arange(len(mylines)):
if(p.match(mylines[i])):
j=i
break
#read everything into an array
tempArr = np.zeros([2*nxyz])
si=0
fi=0
for i in np.arange((j+1),len(mylines)):
temp = np.asarray(mylines[i].split(), dtype=np.double)
fi = si + len(temp)
tempArr[si:fi] = temp
si = fi
#separate absobred dose and uncertainty
self.unc_values = tempArr[1::2].reshape([nx,ny,nz],order='F')
self.tally_values = tempArr[0::2].reshape([nx,ny,nz], order='F')
#xc = 0.5*self.xb[0:-1] + 0.5*self.xb[1:]
#e= np.reshape(np.repeat(xc,nz),[nx,nz])
#e1=np.reshape(np.repeat(zc,nx), [nx,nz], order='F')