for ii in np.arange(0, end, step): #modify density mcnpcontent = mh.readContent(mcnpinp, '4') density = mcnpcontent.strip().split()[2] mh.modifyinp( mcnpinp, '4', mcnpcontent.replace(density, '{:.6e}'.format(float(density) + ii))) #modify m card mcnpcontent = mh.readContent(mcnpinp, 'm1', section='data') mcnplist = mcnpcontent.strip().split() indx = mcnplist.index('90232.90c') mcnplist[indx + 1] = '{:.6e}'.format(float(mcnplist[indx + 1]) + ii) mcnpline = ' '.join(mcnplist) mh.modifyinp(mcnpinp, 'm1', mcnpline, section='data') os.system('mpirun -r ssh -np ' + str(int(node * ppn)) + ' /home/daiye/bin/mcnp5.mpi n=' + mcnpinp) if os.path.isfile(mcnpinp + 'o'): print('MCNP5 run finished!') keff = mtr.readKeff(mcnpinp + 'o')['keff'] oldfilename = mcnpinp + 'o' newfilename = mcnpinp + 'o_' + '{:.4e}'.format(ii) mh.deleteFiles(newfilename) os.rename(oldfilename, newfilename) mh.cleanup(mcnpinp) else: print('error!!!,MCNP5 run failed!') exit(0) with open(resultfile, 'a') as fid: fid.write("{:^.4e} {:^.5f}\n".format(ii, keff))
def maxPowerPeakFactorSearch(mcnpinp, node, ppn, trforrod, mode, rodstep=10): """ Function: under different rod position, compute the power distribution, power peak factor and find the maximum power peak factor, finally write all results to a filename of inp+'results.out'. Parameters: inp: the name of mcnp output file. node: the node for parallel computing. ppn: the core for parallel computing. trforrod: dict for rod, key is 'tr' card, value is rod num. mode: mode for rod withdrawal accident or rod drop accident, 'w' stands for withdrawal and 'd' stands for drop. rodstep: rod move step everytime. Return: none """ cr = {} rodlists = {} powerfactorresults = {} mh = McnpinpHandler() # read initial rod position of burnup mcnp input for key, value in trforrod.items(): rodmessage = mh.readContent(mcnpinp, key, 'data') lists = rodmessage.strip().split() rodxcoordinate = float(lists[1]) rodycoordinate = float(lists[2]) rodinsertpostion = float(lists[3]) cr[value] = ControlRod(rod=value, trCard=key, rodRange=180.0, rodXCoordinate=rodxcoordinate, rodYCoordinate=rodycoordinate) cr[value].setInsertPosition(rodinsertpostion) rodlists[value] = key powerfactorresults[value] = [] print(rodlists) print(powerfactorresults) mh.cleanup(mcnpinp) if re.match('w', mode, re.I) is not None: limit = 180. factor = 1 elif re.match('d', mode, re.I) is not None: limit = 0 factor = -1 else: print("Mode set error! Should be w or d!") exit(0) for rod in rodlists: ii = 0 initinsertposition = cr[rod].getInsertPosition() while (cr[rod].getInsertPosition() * factor < limit): instertposition = initinsertposition + rodstep * ii * factor if instertposition * factor > limit: instertposition = limit cr[rod].setInsertPosition(instertposition) ### modify mcnp inp mh.modifyinp(mcnpinp, cr[rod].getTrCardNo(), cr[rod].ouputforMcnpinp(), 'data') ii = ii + 1 ### run mcnp print(' mpirun -r ssh -np ' + str(int(node * ppn)) + ' /home/daiye/bin/mcnp5.mpi n=' + mcnpinp) os.system(' mpirun -r ssh -np ' + str(int(node * ppn)) + ' /home/daiye/bin/mcnp5.mpi n=' + mcnpinp) if os.path.isfile(mcnpinp + 'o'): print('MCNP5 run finished!') else: print('error!!!,MCNP5 run failed!') exit(0) ### read results and write to results file keff = readKeff(mcnpinp + 'o') meshfilename = mcnpinp + '_mesh_' + rod + '_' + str( instertposition) original_meshfilename = getMeshFilename(mcnpinp + 'o') if os.path.isfile(original_meshfilename): mh.deleteFiles(meshfilename) os.rename(original_meshfilename, meshfilename) print("Rename meshtal to {:}\n".format(meshfilename)) resultsfilename = mcnpinp + rod + '_' + str( instertposition) + '.csv' uncertainty = 1.1 * 1.1 radialPowerPeakFactor, axialPowerPeakFactor, totPowerPeakFactor = computePowerDesityDistribution( meshfilename, resultsfilename, uncertainty) powerfactorresults[rod].append( (instertposition, keff[0], radialPowerPeakFactor, axialPowerPeakFactor, totPowerPeakFactor)) mh.cleanup(mcnpinp) ## set rod insertposition to inital cr[rod].setInsertPosition(initinsertposition) mh.modifyinp(mcnpinp, cr[rod].getTrCardNo(), cr[rod].ouputforMcnpinp(), 'data') maxradialPowerPeakFactor = 0 maxaxialPowerPeakFactor = 0 maxtotPowerPeakFactor = 0 maxrod1 = '' maxrod2 = '' maxrod3 = '' #print(powerfactorresults) with open(mcnpinp + 'results.out', 'w') as fid: fid.write('{:^5}{:^20}{:^8}{:^20}{:^20}{:^20}\n'.format\ ('Rod', 'Insert position', 'Keff', 'Radial peak factor', 'Axial peak factor', 'Tot peak factor')) for rod in powerfactorresults: for ii in range(len(powerfactorresults[rod])): radialpowerfactor = powerfactorresults[rod][ii][2] axialpowerfactor = powerfactorresults[rod][ii][3] totpowerfactor = powerfactorresults[rod][ii][4] instertposition = powerfactorresults[rod][ii][0] keff = powerfactorresults[rod][ii][1] if maxradialPowerPeakFactor < radialpowerfactor: maxrod1 = rod maxradialPowerPeakFactor = radialpowerfactor if maxaxialPowerPeakFactor < axialpowerfactor: maxrod2 = rod maxaxialPowerPeakFactor = axialpowerfactor if maxtotPowerPeakFactor < totpowerfactor: maxrod3 = rod maxtotPowerPeakFactor = totpowerfactor fid.write('{:^5}{:^20.3f}{:^8.5f}{:^20.4f}{:^20.4f}{:^20.4f}\n'.format\ (rod, instertposition, keff, radialpowerfactor, axialpowerfactor, totpowerfactor)) fid.write('{:}: {:}\n'.format('Rod', maxrod1)) fid.write('{:}: {:.4}\n'.format('Max radial power peak factor', maxradialPowerPeakFactor)) fid.write('{:}: {:}\n'.format('Rod', maxrod2)) fid.write('{:}: {:.4}\n'.format('Max axial power peak factor', maxaxialPowerPeakFactor)) fid.write('{:}: {:}\n'.format('Rod', maxrod3)) fid.write('{:}: {:.4}\n'.format('Max total power peak factor', maxtotPowerPeakFactor))
class SearchRodCriticalPos(object): def __init__(self): self.mh = McnpinpHandler() self.mtr = McnpTallyReader() def preProcessing(self, mcnpfile, rod_postion, rods): self.mh.cleanup(mcnpfile) for key, rod in rods.items(): print(key) print(type(rod)) rod.setInsertPosition(rod_postion) self.mh.modifyinp(mcnpfile, rod.getTrCardNo(), rod.ouputforMcnpinp(), section='data') def postProcessing(self, mcnpfile, outfilename): outputfile = ''.join([mcnpfile, 'o']) if os.path.isfile(outputfile): print('MCNP5 run finished!') keff = self.mtr.readKeff(outputfile)['keff'] self.mh.deleteFiles(outfilename) os.rename(outputfile, outfilename) else: raise CustomError('MCNP5 did not run successful!') return keff def startSearch(self, mcnpfile, num_node=1, num_processor=16, reference_keff=1.0, eps=1e-4, **rods): inputfile = mcnpfile for key, rod in rods.items(): if type(rod) != ControlRod: raise TypeError( 'The function parameter requires an instance of class ControlRod!' ) else: rod_range = rod.getRodRange() low = 0 high = rod_range with open('results.out', 'w') as fid: fid.write('{:^12} {:^10}\n'.format('Rod position', 'keff')) while high - low > 0.1: mid = (low + high) / 2.0 self.preProcessing(inputfile, mid, rods) os.system(' mpirun -r ssh -np ' + str(int(num_node * num_processor)) + ' /home/daiye/bin/mcnp5.mpi n=' + inputfile) # os.system(' mcnp5 '+' n='+inputfile) keff = self.postProcessing(inputfile, ''.join([inputfile, 'o', str(mid)])) with open('results.out', 'a') as fid: fid.write('{:^12.6f} {:^10.5f}\n'.format(mid, keff)) if abs(keff - reference_keff) < eps: return mid if keff > reference_keff: high = mid else: low = mid return -1
class Postprocesser(object): def __init__(self): self.mcnptallyreader = McnpTallyReader() self.mcnpinphandler = McnpinpHandler() def outputResults(self, filename, content, writeingmode='a'): with open(filename, writeingmode) as fid: fid.write(content) def renameFile(self, oldfilename, newfilename): self.mcnpinphandler.deleteFiles(newfilename) os.rename(oldfilename, newfilename) def readVolume(self, filename): volume = None with open(filename, 'r') as fid: context = fid.readlines() context = [line for line in context if len(line.strip().split()) == 1] line = re.findall(" \d\.\d{5}E[+-]\d{2}", ''.join(context))[0] volume = float(line) return volume def getKeff(self, mcnpout): if os.path.isfile(mcnpout): print('MCNP5 run finished!') return self.mcnptallyreader.readKeff(mcnpout)['keff'] else: raise FileNotFoundError('No such file: {:}!'.format(mcnpout)) def getEscape(self, mcnpout, mode='fixed'): if os.path.isfile(mcnpout): print('MCNP5 run finished!') if re.fullmatch('kcode', mode, re.I): k_totrate = self.mcnptallyreader.readNeutronActivity(mcnpout) escaperate = k_totrate['escape']/(k_totrate['escape']\ +k_totrate['lossfission']+k_totrate['capture']) elif re.fullmatch('fixed', mode, re.I): f_totrate = self.mcnptallyreader.readNeutronActivity(mcnpout) escaperate = f_totrate['escape'] / (f_totrate['escape'] + f_totrate['lossfission'] + f_totrate['capture']) else: raise NameError('No such mode: {:}'.format(mode)) return escaperate # escape of kcode mode else: raise FileNotFoundError('No such file: {:}'.format(mcnpout)) def getCR(self, mcnpout, mode='kcode', tallydic=None, cell=None, matnum=None): if os.path.isfile(mcnpout): print('MCNP5 run finished!') if re.fullmatch('kcode', mode, re.I): cr = self.mcnptallyreader.getCR(mcnpout) elif re.fullmatch('fixed', mode, re.I): cr = self.mcnptallyreader.getCR( PurePath.joinpath(Path(os.getcwd()), mcnpout), mode='fixed', tallydic=tallydic, cell=cell, matnum=matnum, volume=self.readVolume(mcnpout)) else: raise NameError('No such mode: {:}'.format(mode)) return cr # escape of kcode mode else: raise FileNotFoundError('No such file: {:}'.format(mcnpout))