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
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))
## set initials results['kCR'] = 0 # CR of kcode mode results['fCR'] = 0 # CR of fixed mode results['kescape'] = 0 # escape of kcode mode results['fescape'] = 0 # escape of fixed mode matdict[ucl3] = jj matdict[thcl4] = 100 - ii - jj mat = Material('mat1', matdict, 900) # print(uf4.getActomicMass()) # print(thf4.getActomicMass()) density = mat.getDensity() print(density) # print(mat.toMcnpCard().strip()) line = mat.toMcnpCard().strip() mh.modifyinp(inp, 'm1', 'm1 ' + line, 'data') line = mh.readContent(inp, '4') print(line) # print(line.strip().split()[2]) newline = line.replace(line.strip().split()[2], '-{:.4f}'.format(density)) print(newline) mh.modifyinp(inp, '4', newline) changeMode(inp, mode='kcode') # os.system('mcnp5'+ ' n=' + inp) os.system('mpirun -r ssh -np ' + str(int(node * ppn)) + ' /home/daiye/bin/mcnp5.mpi n=' + inp) if os.path.isfile(inp + 'o'): print('MCNP5 run finished!') results['keff'] = mtr.readKeff(inp + 'o')['keff'] results['kCR'] = mtr.getCR(inp + 'o')
class Preprocesser(object): def __init__(self): self._cardlist = [ 'Naclsets', 'Pucl3sets', 'coresizesets', 'reflectorsets', 'Thcl4sets', 'Ucl3sets', 'Ucl4sets', 'Na', 'Cl', 'U', 'Th' ] self.mcnpinphandler = McnpinpHandler() def getParameter(self, mcnpinp, materialcard): paralists = [] newcardlist = [] for card in self._cardlist: line = self.mcnpinphandler.readContent(mcnpinp, card, section='data') if line: lists = line.strip().split() newcardlist.append(card) paralists.append([ int(x) if type(eval(x)) == int else float(x) for x in lists[1:] ]) parameter = {card: para for card, para in zip(newcardlist, paralists)} line = self.mcnpinphandler.readContent(mcnpinp, materialcard, section='data') linelist = line.strip().split() parameter['mcardcontent'] = linelist return parameter def copyInitalMcnpinp(self, initalinp, mcnpinp): with open(initalinp, 'r') as fid1, open(mcnpinp, 'w') as fid2: for line in fid1: fid2.write(line) def deleteNonMcnpCard(self, mcnpinp): shadow = False with open(mcnpinp, 'r') as fid, open(mcnpinp + 'bp', 'w') as fid2: for line in fid: shadow = False lists = line.strip().split() if bool(lists): for card in self._cardlist: if re.fullmatch(card, lists[0], re.I) is not None: shadow = True break if not shadow: fid2.write(line) os.remove(mcnpinp) os.rename(mcnpinp + 'bp', mcnpinp) def cleanupFolder(self, mcnpinp): self.mcnpinphandler.cleanup(mcnpinp) def modfiyMaterial(self, mcnpinp, cellnum, newdensity, mcard, newmcardcontent): line = self.mcnpinphandler.readContent(mcnpinp, cellnum) newline = line.replace(line.strip().split()[2], '-{:.5f}'.format(newdensity)) self.mcnpinphandler.modifyinp(mcnpinp, cellnum, newline) self.mcnpinphandler.modifyinp(mcnpinp, mcard, newmcardcontent, 'data') def changeMode(self, mcnpinp, mode, test=False): with open(mcnpinp, 'r', encoding="utf-8") as fid: content = fid.readlines() if test: fixedSource = 'sdef axs=0 0 1 pos=0 0 0 ext=d1 rad=d2 erg=d3 par=1\ \nsi1 -10 10\nsp1 0 1\nsi2 0 10\nsp2 -21 1\nSI3 L \ 0.151 0.248 0.410 0.675 1.11 1.84 3.03 4.99 19.64\nSP3 \ 0.0 5.45e-2 5.0e-2 8.0e-2 0.122 0.165 0.178 0.157 0.1985\nnps 50\n' kcodeSource = 'kcode 200 1.0 5 50\nksrc 50. 0. 0. -50 0 0 -0 \ 0 0 0 0 20\n' else: fixedSource = 'sdef axs=0 0 1 pos=0 0 0 ext=d1 rad=d2 erg=d3 par=1\ \nsi1 -10 10\nsp1 0 1\nsi2 0 10\nsp2 -21 1\nSI3 L \ 0.151 0.248 0.410 0.675 1.11 1.84 3.03 4.99 19.64\nSP3 \ 0.0 5.45e-2 5.0e-2 8.0e-2 0.122 0.165 0.178 0.157 0.1985\nnps 50000\n' kcodeSource = 'kcode 20000 1.0 30 250\nksrc 50. 0. 0. -50 0 0 -0 \ 0 0 0 0 20\n' if re.fullmatch('fixed', mode, re.I): with open(mcnpinp, 'w', encoding="utf-8") as f: for line in content: lists = line.strip().split() if lists and re.fullmatch('kcode', lists[0], re.I) is not None: f.write(fixedSource) elif lists and re.fullmatch('ksrc', lists[0], re.I) is not None: pass else: f.write(line) elif re.fullmatch('kcode', mode, re.I): with open(mcnpinp, 'w', encoding="utf-8") as f: for line in content: lists = line.strip().split() if lists and re.fullmatch('sdef', lists[0], re.I) is not None: f.write(kcodeSource) elif lists and re.fullmatch('si|sp|sc|ds[0-9]{1,3}', lists[0], \ re.I) is not None: pass elif lists and re.fullmatch('nps', lists[0], re.I) is not None: pass else: f.write(line) else: raise NameError('No such mode!') def changeMcnpLine(self, inp, increment, designator, section='cell'): line = self.mcnpinphandler.readContent(inp, designator, section) data = line.strip().split()[2] if float(data) > 0: replacedata = str(float(data) + increment) else: replacedata = str(float(data) - increment) newline = line.replace(data, replacedata) self.mcnpinphandler.modifyinp(inp, designator, newline, section)