コード例 #1
0
ファイル: search_generation.py プロジェクト: thomasyp/project
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))
コード例 #2
0
 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')
     k_totrate = mtr.readNeutronActivity(inp + 'o')
     results['kescape'] = k_totrate['escape']/(k_totrate['escape']\
             +k_totrate['lossfission']+k_totrate['capture']) # escape of kcode mode
     if os.path.isfile(inp + 'o'):
         oldfilename = inp + 'o'
         newfilename = inp+'ko_'+str(mm)+'_'+str(kk)+'_'+'{:.4f}'.format(matdict[nacl])\
             +'_'+'{:.4f}'.format(matdict[ucl3])+'_'+'{:.4f}'.format(matdict[thcl4])
         mh.deleteFiles(newfilename)
         os.rename(oldfilename, newfilename)
     mh.cleanup(inp)
 else:
     print('error!!!,MCNP5 run failed!')
     exit(0)
 if results['keff'] < 0.998:
コード例 #3
0
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
コード例 #4
0
ファイル: search_CR.py プロジェクト: thomasyp/project
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))