def run_kinetic(): """ :return: """ from read_inp import read_inp from rmc.RMC import RMC from optparse import OptionParser # command line parsing usage = """usage: %prog [options]""" parser = OptionParser(usage=usage) parser.add_option('-n', '--mpi', type='int', dest='n_procs', help="number of MPI parallel processes.") parser.add_option('-i', '--input', type='string', dest='input_name', help="name of the input file.") parser.add_option( '-p', '--platform', type='string', dest='platform', help="name of the platform that the code is running on.\n" "OPTIONS: linux, windows, tianhe, tansuo.") parser.add_option( '-c', '--continue', type='int', dest='n_continue', help='step number which the program will continue calculating at.') parser.add_option('-s', '--stop', type='int', dest='n_stop', help="step number which the program will stop at.") (options, args) = parser.parse_args() if options.n_procs is not None: n_parallel = options.n_procs else: n_parallel = 1 if options.input_name is not None: s_inp_name = options.input_name else: s_inp_name = 'inp' if options.platform is not None: s_platform = options.platform else: s_platform = 'LINUX' if options.n_continue is not None: n_continue = options.n_continue else: n_continue = 0 if options.n_stop is not None: n_stop = options.n_stop else: n_stop = -1 # pre-condition os.chdir(sys.path[0]) if not os.path.exists('results'): os.mkdir('results') if os.path.exists('RMC.log'): os.remove('RMC.log') l_s_sub_inp_name = read_inp(s_inp_name) # continue calculation if n_continue >= 1: shutil.copyfile( os.path.join(sys.path[0], 'results', l_s_sub_inp_name[n_continue - 1] + '.source'), os.path.join(sys.path[0], l_s_sub_inp_name[n_continue] + '.source')) for i_sub_inp_name in (range(n_continue, len(l_s_sub_inp_name)) if n_stop < 0 else range(n_continue, n_stop + 1)): s_sub_inp_name = l_s_sub_inp_name[i_sub_inp_name] rmc = RMC(s_inp_name=s_sub_inp_name) rmc.run(n_parallel, s_platform) rmc.archive(s_sub_inp_name, os.path.join(sys.path[0], 'results')) if i_sub_inp_name != len(l_s_sub_inp_name) - 1: os.rename( os.path.join(sys.path[0], s_sub_inp_name + '.source'), os.path.join(sys.path[0], l_s_sub_inp_name[i_sub_inp_name + 1] + '.source'))
path="/tmp/SOLblobXZ/data_"+sim_key # inp = read_inp(path=path,boutinp='BOUT.inp') # inp = parse_inp(inp) # inp2 = {} # for k, v in inp.items(): # inp2[k] = v # print inp2 #read the data and process t1= 0 t2 = 200 try: inp = read_inp(path=path,boutinp='BOUT.inp') inp = parse_inp(inp) dx = np.double(inp['[mesh]']['dx']) #print 'dx',dx zmax = np.double(inp['[main]']['ZMAX']) print dx,zmax n = (np.squeeze(collect("n",path=path,tind =[t1,t2]))) print dx,zmax n0 = (np.squeeze(collect("n0",path=path,tind =[t1,t2]))) u = np.squeeze(collect("u",path=path,tind =[t1,t2])) phi = np.squeeze(collect("phi",path=path,tind =[t1,t2])) time = np.squeeze(collect("t_array",path=path,xind=[0,0])) #dx = np.squeeze(collect("dx",path=path)) #zmax = np.squeeze(collect("ZMAX",path=path))
#key='movie' cache=path+'_movie' nfile = path+ 'nfile.dat' ufile = path+ 'ufile.dat' phifile = path+ 'phifile.dat' Akfiile = path+'Akfile.dat' from boutdata import collect from boutdata import collect2 from collect2 import collect2 from boututils import savemovie import numpy as np inp = read_inp(path=path) meta = parse_inp(inp) #print meta nz = np.double(meta['[main]']['MZ']) nxpe = np.double(meta['[main]']['NXPE']) nype = np.double(meta['[main]']['NYPE']) mxg = np.double(meta['[main]']['MXG']) dx = np.double(meta['[mesh]']['dx']) zmax = np.double(meta['[main]']['ZMAX']) #mxsub = meta['[main]'][''] #nz = np.squeeze(collect("MZ",xind=[0,0],path=path,info=False)) #nxpe= np.squeeze(collect("NXPE",xind=[0,0],path=path,info=False)) mxsub = 15 #mxsub = np.squeeze(collect("MXSUB",xind=[0,0],path=path,info=True)) #without gaurds #mxg = np.squeeze(collect("MXG",xind=[0,0],path=path,info=False))
def __init__(self,path,field="n",meta=None,fast_center=True,get_Xc=True, get_lambda=True,debug=False,fast = True): self.path=path self.field=field #print 'read the inp' f=open(path+'/BOUT.log.0', 'r') self.md5 = hashlib.md5(f.read()).hexdigest() f.close() defaults = {'dx':1,'x0':0,'dy':1,'y0':0,'log_n':False} for key,val in defaults.items(): if not hasattr(self,key): #print 'setting: ',key,val setattr(self, key, val) #read all metadata from BOUT.inp #print 'read the inp' inp = read_inp(path=path,boutinp='BOUT.inp') inp = parse_inp(inp) #inp file for k, v in inp.items(): setattr(self, k, v) for k,v in (getattr(self,'[main]')).items(): #print k,v try: setattr(self, str.lower(k), float(v)) except: setattr(self, str.lower(k), v) for k,v in (getattr(self,'[physics]')).items(): #print k,v try: setattr(self, str.lower(k), float(v)) except: try: setattr(self, str.bool(k), v) except: setattr(self, str.lower(k), v) if meta is not None: for k, v in meta.items(): setattr(self, k, v) self.mz = self.mz-1 self.nz = self.mz #self.nz = self.np.squeeze(collect("MZ",xind=[0,0],path=path,info=False))-1 nxpe = self.nxpe# np.squeeze(collect("NXPE",xind=[0,0],path=path,info=False)) mxg = self.mxg #np.squeeze(collect("MXG",xind=[0,0],path=path,info=False)) self.mxsub = np.squeeze(collect("MXSUB",xind=[0,0],path=path,info=False)) #gaurds self.nx = int(self.mxsub*nxpe + 2*mxg) #including gaurds nx,ny,nz = self.nx,self.nz,self.nz self.dx = np.squeeze(collect("dx",path=path,xind=[0,0])) self.dy = np.squeeze(collect("dz",path=path,xind=[0,0])) self.zmax = np.squeeze(collect("ZMAX",path=path)) self.time = np.array(np.squeeze(collect("t_array",path=path,xind=[0,0]))) nt = self.nt = len(self.time) try: self.nave = np.squeeze(collect("nave",path=path)) self.nave = self.nave/nt have_nave = True except: have_nave = False try: self.G = np.squeeze(collect("gamma",path=path)) self.G = self.G/nt have_flux = True except: havex_flux = False self.debug = debug if debug: t_chunk = 20 t_stop = np.max(self.nt) t1 = np.int(self.nt*.90) else: t_chunk = 30 t_stop = np.max(self.nt) t1 = 0 #self.time[0] self.t_chunk = t_chunk self.t1 = t1 self.t_stop = t_stop tarray = np.array(np.arange(nt - nt/2)) t2 = np.min([t1+t_chunk,t_stop]) self.xmoment = {'1':[],'2':[]} self.ymoment = {'1':[],'2':[]} self.max_val = [] y0,x0 = (self.y0,self.x0) dx,dy = (self.dx,self.dy) ymin = y0 xmin = x0 xmax = nx*dx + xmin ymax = ny*dy + ymin self.kx_max = nx self.ky_max = ny kxmax,kymax = nx,ny kxmin = 0. kymin = 0. dky = self.dky = 1.0/self.zmax dkx = self.dkx = 2.*np.pi/self.dx self.kx = np.arange(0,nx)*self.dkx self.ky = np.arange(0,ny)*self.dky #print nx,nx*self.mxsub self.pos_i = np.mgrid[0:nx:1,0:ny:1] self.x = np.mgrid[xmin:xmax:self.dx] self.y = np.mgrid[xmin:xmax:self.dy] sys.stdout = mystdout = StringIO() a = np.squeeze(collect("alpha",path=path)) sys.stdout = old_stdout #self.a_ave a_ave = self.a_ave = np.average(a,axis=1) # popt, pcov= curve_fit(fit_tanh,xnew,a_ave,p0=p0) # w = run['w'] = popt[0]*dx try: xstart = np.min(np.where((a_ave-.1*a_ave.max()>0)) ) self.xstart = xstart xstop = np.int(xstart+ nx/2.) if xstop > nx: xstop = nx -1; except: xstart= np.int(nx/3.) xstop = np.int(xstart+ nx/2.) self.fast = fast #will return 'real n' not 'log n' statistics and profiles, spasly sampled in x, dense in t moving_min_n, moving_max_n, moving_min, moving_max, nave_net, nrms_net ,nmin_net,nmax_net = self.compute_profile() pdf_x = [] df_x = [] self.nbins = nbins = 300 for x in xrange(nx): #print moving_min_n[x] pdf_x.append(np.mgrid[moving_min_n[x]-.01:moving_max_n[x] +.01:complex(0,nbins)]) df_x.append(np.mgrid[moving_min[x]-.01:moving_max[x] +.01:complex(0,nbins)]) self.pdf_x = pdf_x self.df_x = df_x pdf_y=nx*[None] df_y=nx*[None] flux_pdf_y=nx*[None] flux_df_y =nx*[None] pdf_y_net = 0 df_y_net = 0 # self.moving_min_n= moving_min_n # self.moving_max_n= moving_max_n # self.moving_min= moving_min # self.moving_max= moving_max for i in range(nx): #print "some unique object %d" % ( i, ) pdf_y[i]= 0*pdf_x[i] df_y[i]= 0*pdf_x[i] flux_pdf_y[i]= 0*pdf_x[i] flux_df_y[i]= 0*pdf_x[i] xstart = np.min(np.where(a_ave - 0.1 * a_ave.max() > 0)) xstop = np.int(xstart + nx / 2.0) x1 = np.int(xstart) x2 = np.int(xstart + nx/5.0) # x1 = np.min(np.where((a_ave-.1*a_ave.max()>0)) ) # x2 = np.min(np.where((a_ave-.9*a_ave.max()>0)) ) df_x_start = np.array(df_x[x1:x2]).min() df_x_stop = np.array(df_x[x1:x2]).max() pdf_x_start = np.array(pdf_x[x1:x2]).min() pdf_x_stop = np.array(pdf_x[x1:x2]).max() self.df_x_start = df_x_start self.df_x_stop = df_x_stop self.pdf_x_start = pdf_x_start self.pdf_x_stop = pdf_x_stop if xstop > nx: xstop = nx - 1 j = 0 print 't2,t_stop:', t2, t_stop self.nave = [] self.flux = [] self.v = [] while t2 < t_stop: print 'computing statistical measures' (nave, dn), (flux, flux_max, d_flux), (vx,vy),data = self.dens_and_flux(t1, t2) self.save_linlam(np.log(nave), np.log(dn)) self.nave.append([nave, dn]) self.flux.append([flux, flux_max, d_flux]) self.v.append([vx,vy]) #print len(self.flux) ntt, nxx, nyy = data.shape #print 'data.shape: ', data.shape, t1, t_stop if t1 > np.int(2*t_stop)/3: j = j + 1 for x in xrange(nx): if not fast: p = np.polyfit(np.arange(ntt), np.squeeze(data[:, x, :]).mean(axis=1), 1) nave2d = p[1] + p[0] * np.arange(ntt) p = np.polyfit(np.arange(ntt), np.squeeze(flux[:, x, :]).mean(axis=1), 1) flux2d_x = p[1] + p[0] * np.arange(ntt) if self.log_n: cond = np.exp(data[:, x, :]) - np.exp(np.reshape(np.repeat(nave2d, ny), [ntt, ny])) cond_flux = np.exp(data[:, x, :])*vy - np.reshape(np.repeat(flux2d_x, ny), [ntt, ny]) else: cond = data[:, x, :] - np.reshape(np.repeat(nave2d, ny), [ntt, ny]) elif self.log_n: #print nave.shape,data.shape cond = np.exp(data[:, x, :]) - nave[x] else: cond = data[:, x, :] - nave[x] nrms = cond.std() datamax = data[:, x, :].max() if np.isfinite(datamax): dfadd = np.squeeze(np.histogram(cond, bins=nbins, normed=False, range=(df_x[x].min(), df_x[x].max()))[0]) pdfadd = np.squeeze(np.histogram(cond / nrms, bins=nbins, normed=False, range=(pdf_x[x].min(), pdf_x[x].max()))[0]) if np.isfinite(np.sum(pdfadd)): pdf_y[x] = pdf_y[x] + pdfadd df_y[x] = df_y[x] + dfadd if abs(x - xstart) < nx / 20.0: pdf_y_net = pdf_y_net + np.squeeze(np.histogram(cond / nrms, bins=nbins, normed=False, range=(pdf_x_start, pdf_x_stop))[0]) #print x1, x2 df_y_net = df_y_net + np.squeeze(np.histogram(cond, bins=nbins, normed=False, range=(df_x_start, df_x_stop))[0]) self.pdf_y = pdf_y self.pdf_y_net = pdf_y_net self.df_y = df_y self.df_y_net = df_y_net self.t.append(t1 + 0.5 * (t2 - t1)) t1 = t2 + 1 t2 = np.min([t1 + t_chunk - 1, t_stop + 1]) self.nave = np.array(self.nave) self.flux = np.array(self.flux) self.pdf_stats = [] self.df_stats = [] print 'almost done' for x in xrange(nx): if np.mean(nrms_net[:, x]) != 0: self.pdf_y[x] = pdf_y[x] / sum(pdf_y[x]) self.df_y[x] = df_y[x] / sum(df_y[x]) else: self.pdf_x[x] = pdf_x[x - 1] if hasattr(self, 'pdf_y_net'): self.pdf_y_net = pdf_y_net / np.float(sum(pdf_y_net)) self.df_y_net = df_y_net / np.float(sum(df_y_net))
def post_dynamic(l_n_mesh_num): """ Post-process the result of the kinetic calculation. :param l_n_mesh_num: :return: VOID """ from read_inp import read_inp from read_inp import format_inp from read_inp import read_kinetic_para from post.get_dynamic_results import get_power from post.get_dynamic_results import get_power_fit from post.get_dynamic_results import get_reactivity from post.get_dynamic_results import get_reactivity_fit from post.get_dynamic_results import get_keff from post.get_dynamic_results import get_beta from post.get_dynamic_results import get_beta_fit from post.get_dynamic_results import get_gentime from post.get_dynamic_results import get_gentime_fit from post.get_dynamic_results import get_power_mesh from post.get_dynamic_results import get_power_assem from post.get_dynamic_results import nmz_power from post.get_dynamic_results import nmz_power_mesh from post.get_dynamic_results import nmz_power_assem from post.output_to_file import save_array_1D from post.output_to_file import save_array_4D from optparse import OptionParser # # l_n_time: # time list, in which every time corresponds to a single Monte Carlo calculation # l_n_reactivity: # reactivity list, in which every reactivity is calculated by Monte Carlo directly # l_n_keff: # keff list, in which every keff is calculated by Monte Carlo directly # l_n_power: # fission rate list, in which every fission rate is calculated by Monte Carlo # l_n_beta: # delayed neutron fraction list, in which every fraction is calcualted by Monte Carlo # l_n_gentime: # generation time(prompt neutron lifetime) list, in which every gentime is calculated by Monte Carlo # # l_n_time_fit: # time list, in which every time is used in fitting calculation # l_n_reactivity_fit: # reactivity list, obtained after a n-order fitting calculation using l_n_reactivity # l_n_power_fit: # fission rate list, in which everyone is calculated using the fitted kinetic parameters # l_n_beta_fit: # delayed neutron fraction list, obtained after a n-order fitting calculation using l_n_beta # l_n_gentime_fit:# generation time(prompt neutron lifetime) list, obtained after a n-order fitting calculation using l_n_gentime # l4_n_power_mesh_nmz # pin power distribution after normalization # l4_n_power_assem_nmz # assembly power distribution after normalization # # command line parsing usage = """usage: %prog [options]""" parser = OptionParser(usage=usage) parser.add_option('-i', '--input', type='string', dest='input_name', help="name of the input file.") parser.add_option( '-p', '--platform', type='string', dest='platform', help='name of the platform that the code is running on.\n' 'OPTIONS: linux, windows, tianhe, tansuo.') (options, args) = parser.parse_args() if options.input_name is not None: s_inp_name = options.input_name else: s_inp_name = 'inp' if options.platform is not None: s_platform = options.platform.upper() else: s_platform = 'LINUX' # pre-condition os.chdir(sys.path[0]) with open(s_inp_name, 'r') as f_inp: s_inp = f_inp.read() s_inp = format_inp(s_inp) d_var_time = read_kinetic_para(s_inp) l_inp_name = read_inp(s_inp_name) for s_subinp_name in l_inp_name: os.remove(os.path.join(sys.path[0], s_subinp_name) ) # remove inp files generated by the read_inp() function l_inner_name = [ '%s.innerproduct' % s_subinp_name for s_subinp_name in l_inp_name ] l_out_name = ['%s.out' % s_subinp_name for s_subinp_name in l_inp_name] os.chdir(os.path.join(sys.path[0], 'results')) # output original kinetic results l_s_time = d_var_time['TIMESTEP'] l_n_time = [float(s_time) for s_time in l_s_time] save_array_1D(l_n_time, 'time', '%s.time' % s_inp_name) l_n_reactivity = get_reactivity(l_inner_name) # kinetic reactivity save_array_1D(l_n_reactivity, 'Reactivity', '%s.reactivity' % s_inp_name) l_n_keff = get_keff(l_out_name) # keff save_array_1D(l_n_keff, 'Keff', '%s.keff' % s_inp_name) l_n_power = get_power( l_inner_name) # total fission rate of the point reactor save_array_1D(l_n_power, 'Power', '%s.power' % s_inp_name) l_n_beta = get_beta(l_inner_name) # delayed neutron fraction save_array_1D(l_n_beta, 'Beta', '%s.beta' % s_inp_name) l_n_gentime = get_gentime( l_inner_name) # generation time: prompt neutron lifetime save_array_1D(l_n_gentime, 'generation time', '%s.gentime' % s_inp_name) # output fine time points and other kinetic parameters for fit case if 'FIT' not in d_var_time: return d_fitinfo = d_var_time['FIT'] """ d_fitinfo structure example: {'FITSTEP':['5', '10', '15'] 'SAMPLESTEP':[['0', '1', '2', '4'], ['5', '6', '7', '8'], ['11','12','14','15']] 'SUBSTEP':['100', '100', '100'] } """ l_s_fitstep = d_fitinfo['FITSTEP'] l2_s_samplestep = d_fitinfo['SAMPLESTEP'] l_s_substep = d_fitinfo['SUBSTEP'] l_samplestep_min = [] l_samplestep_max = [] l_samplestep_num = [] for l_s_1 in l2_s_samplestep: min = int(l_s_1[0]) for s_1 in l_s_1[1:]: if min > int(s_1): min = int(s_1) l_samplestep_min.append(min) for s_1 in l_s_fitstep: l_samplestep_max.append(int(s_1)) for s_1 in l_s_substep: l_samplestep_num.append(int(s_1)) # # calculate time value in fitting calculation l_n_time_fit = [] n_index = 0 while n_index < len(l_n_time): if n_index in l_samplestep_min: n_index_next = l_samplestep_max[l_samplestep_min.index(n_index)] n_substep_num = l_samplestep_num[l_samplestep_min.index(n_index)] for i in range(n_substep_num): time = l_n_time[n_index] + float(i) / n_substep_num * ( l_n_time[n_index_next] - l_n_time[n_index]) l_n_time_fit.append(time) n_index = n_index_next else: l_n_time_fit.append(l_n_time[n_index]) n_index += 1 save_array_1D(l_n_time_fit, 'time for fitting', '%s.time.fit' % s_inp_name) # # read power and reactivity value calculated by fitting method l_n_power_fit = [] # total fission rate of the point reactor l_n_reactivity_fit = [] # kinetic reactivity l_n_beta_fit = [] # delayed neutron fraction l_n_gentime_fit = [] # generation time: prompt neutron lifetime for s_time in l_s_time: b_is_fitstep = False b_is_samplestep = False i_time = l_s_time.index(s_time) if '%s' % i_time in l_s_fitstep: b_is_fitstep = True for i_samplestep_min in range(len(l_samplestep_min)): i_samplestep_max = i_samplestep_min if l_samplestep_min[i_samplestep_min] <= i_time <= l_samplestep_max[ i_samplestep_max]: b_is_samplestep = True if i_time == l_samplestep_min[i_samplestep_min]: if '%s' % l_samplestep_min[ i_samplestep_min] not in l_s_fitstep: b_is_samplestep = False break s_inner_name = '%s.innerproduct' % l_inp_name[i_time] if (not b_is_samplestep): l_n_power_fit += get_power([s_inner_name]) l_n_reactivity_fit += get_reactivity([s_inner_name]) l_n_beta_fit += get_beta([s_inner_name]) l_n_gentime_fit += get_gentime([s_inner_name]) elif b_is_fitstep: l_n_power_fit += get_power_fit([s_inner_name]) l_n_reactivity_fit += get_reactivity_fit([s_inner_name]) l_n_beta_fit += get_beta_fit([s_inner_name]) l_n_gentime_fit += get_gentime_fit([s_inner_name]) save_array_1D(l_n_power_fit, 'power after fitting', '%s.power.fit' % s_inp_name) # # normalize the fission rate l_n_power_fit_nmz = nmz_power(l_n_power_fit) save_array_1D(l_n_power_fit_nmz, 'power after normalization & fitting', '%s.power.fit.nmz' % s_inp_name) save_array_1D(l_n_reactivity_fit, 'reactivity after fitting', '%s.reactivity.fit' % s_inp_name) save_array_1D(l_n_beta_fit, 'delayed neutron fraction after fitting', '%s.beta.fit' % s_inp_name) save_array_1D(l_n_gentime_fit, 'generation time after fitting', '%s.gentime.fit' % s_inp_name) # from the fission rate results (a very fine, large list) calculated by fitting method, # find the fission rates corresponding to every Monte Carlo calculation. """ l_n_time & l_n_power l_n_time_fit & l_n_power_fit """ l_n_power_fixed = [ ] # total fission rates of every monte carlo calculation, # but replaced by the corresponding fitting calculation results for i_time in range(len(l_n_time)): for i_power_fit in range(len(l_n_power_fit)): i_time_fit = i_power_fit if abs(l_n_time[i_time] - l_n_time_fit[i_time_fit]) < TRUNCATION_ERROR: l_n_power_fixed.append(l_n_power_fit[i_power_fit]) l_n_power_fixed_nmz = nmz_power(l_n_power_fixed) # read the pin power distribution and transform it to assembly power distribution l4_n_power_mesh = [] n_x_count = l_n_mesh_num[0] n_y_count = l_n_mesh_num[1] n_z_count = l_n_mesh_num[2] l_tally_name = [ '%s.%s' % (s_inner_name, 'Tally' if s_platform is not 'WINDOWS' else 'tally') for s_inner_name in l_inp_name ] for s_tally_name in l_tally_name: l4_n_power_mesh.append( get_power_mesh(s_tally_name, n_x_count, n_y_count, n_z_count)) l4_n_power_assem = get_power_assem( l4_n_power_mesh) # Warning: this function is a temporary function. l4_n_power_mesh_nmz = nmz_power_mesh(l4_n_power_mesh, l_n_power_fixed_nmz) save_array_4D(l4_n_power_mesh_nmz, 'power of every mesh after normalization', '%s.power.mesh' % s_inp_name) l4_n_power_assem_nmz = nmz_power_assem(l4_n_power_assem, l_n_power_fixed_nmz) save_array_4D(l4_n_power_assem_nmz, 'power of every assem after normalization', '%s.power.assem' % s_inp_name)