<jupyter_start><jupyter_text>Functions to print blocks of QE inputs<jupyter_code>def makeCard(cardDict,cardKeys=None,optionKey='unit',T=False, endl = ' \n',eq = ' = ',stl = '  '):
    """Transform a Card block in dict form into a string."""
    from itertools import izip_longest
    from  tabulate import tabulate
    cardStr = ''
    if cardKeys is None:
        keys =  cardDict.keys()
        keys.remove(optionKey)
        cardKeys = cardDict[keys[0]]
        
    if optionKey in cardDict.keys():
        keys =  cardDict.keys()
        keys.remove(optionKey)
        for key in keys:
            cardStr += key + stl
        cardStr += cardDict[optionKey] + endl

        for key,val in cardDict.items():
            if isinstance(val, dict):
                l = [val[cardKey] for cardKey in cardKeys] 
                ll = map(list, izip_longest(*l)) if T else l
                cardStr += stl + tabulate(ll, tablefmt="plain").replace('[','').replace(']','').replace(',','').replace('\n',endl+stl) + endl
        
        return cardStr
    else:
        print 'cardDict has no key called: {} (optionKey)'.format(optionKey)
def makeNamelist(namelistName,namelistDict,namelistKeys=None, endl = ' \n',eq = ' = ',stl = '  '):
    """Transform a Name List block in dict form into a string."""
    if namelistKeys is None:
        namelistKeys = namelistDict.keys()
    namelistStr = namelistName + endl
    for key in namelistKeys:
        namelistStr += stl + key + eq + namelistDict[key] + endl
    namelistStr += '/' + endl
    return namelistStr<jupyter_output><empty_output><jupyter_text>Input file generator function<jupyter_code>def SG2ibrav(spaceGroupIdx,SGTable):
    ibrav = None
    sgInfo = SGTable[SGTable['Table No.'] == spaceGroupIdx]
    bravaisLattice = sgInfo['Crystal System'].values[0].encode('utf-8') + ' ' + sgInfo['Full notation'].values[0][0].encode('utf-8')
    bravaisLattice2ibrav = {'cubic P':1,'cubic F':2,'cubic I':3,'hexagonal P':4,'trigonal P':4,'trigonal R':5,'tetragonal P':6,'tetragonal I':7,
                            'orthorhombic P':8,'orthorhombic A':9,'orthorhombic B':9,'orthorhombic C':9,'orthorhombic F':10,'orthorhombic I':11,
                            'monoclinic P':12,'monoclinic A':13,'monoclinic B':13,'monoclinic C':13, 'triclinic P':14}
    ibrav = bravaisLattice2ibrav[bravaisLattice]
   
    return ibrav

def SG2wyckoff(spaceGroupIdx,wyckTable):
    highestMultiplicitySite = wyckTable[spaceGroupIdx].ix[0,:]
    return str(highestMultiplicitySite['Multiplicity']) + highestMultiplicitySite['Wyckoff letter']

def SG2Postion(spaceGroupIdx,crystals):
    generalPosition = crystals[spaceGroupIdx][0].get_scaled_positions()[0]
    return generalPosition

def SG2cellParam(spaceGroupIdx,crystals):
    cellParam = crystals[spaceGroupIdx][0].get_cell_lengths_and_angles()
    return cellParam

def makeQEInnput(crystals,WyckTable,SGTable,ElemTable,
                 zatom = 14,rhocutoff = 20 * 4,wfccutoff = 20,kpt = [2,2,2],kpt_offset = [0,0,0]):
    inputDic = {}
    
    elemInfo = ElemTable[ElemTable['z'] == zatom]
    species = [elemInfo['sym'].values[0],]*1
    mass = [elemInfo['mass'].values[0]] # in atomic unit
    PP = ['Si.pbe-n-rrkjus_psl.1.0.0.UPF']
    nvalence = elemInfo['val'].values[0]
    
    for SpaceGroupIdx in crystals:
        wyck = [SG2wyckoff(SpaceGroupIdx,WyckTable)]
        positions = [SG2Postion(SpaceGroupIdx,crystals)]
        cellParam = SG2cellParam(SpaceGroupIdx,crystals)
        ibrav = SG2ibrav(SpaceGroupIdx,SGTable)
        multipicity = WyckTable[SpaceGroupIdx]['Multiplicity'][0]

        if int(0.2*multipicity*nvalence/2.) > 4:
            nbnd = int(multipicity*nvalence/2.+int(0.2*multipicity*nvalence/2.))
        else:
            nbnd = int(multipicity*nvalence/2.+4)
        
        # define name list of QE
        control = {'calculation' : '"scf"',
                  'outdir' : '"./out/"',
                  'prefix' : '"qe"',
                  'pseudo_dir' : '"./pseudo"',
                  'restart_mode' : '"from_scratch"',
                  'verbosity' : '"high"',
                  'wf_collect' : '.false.',
            }
        controlKeys = ['calculation','outdir','prefix','pseudo_dir', 'restart_mode','verbosity' ,'wf_collect']
        system = {
                  'ecutrho' :   '{:.5f}'.format(rhocutoff),
                  'ecutwfc' :   '{:.5f}'.format(wfccutoff),
                  'ibrav' : str(ibrav),
                  'nat' : str(1),
                  'nbnd' : str(nbnd),
                  'ntyp' : str(1),
                  'occupations' : '"smearing"',
                  'smearing' : '"cold"',
                  'degauss' :   '1.0000000000d-02',
                  'space_group' : str(SpaceGroupIdx),
                  'A' : str(cellParam[0]),
                  'B' : str(cellParam[1]),
                  'C' : str(cellParam[2]),
                  'cosAB' : '{:.5f}'.format(np.cos(cellParam[5]*np.pi/180.)),
                  'cosAC' : '{:.5f}'.format(np.cos(cellParam[4]*np.pi/180.)),
                  'cosBC' : '{:.5f}'.format(np.cos(cellParam[3]*np.pi/180.)),
            }
        syskeys = [ 'ecutrho','ecutwfc','ibrav', 'nat','nbnd','ntyp' ,
                   'occupations', 'smearing','degauss','space_group','A','B' ,'C', 'cosAB', 'cosAC', 'cosBC' ]
        electrons = {'conv_thr':'1.0000000000d-06'}

        # define cards of QE
        optionkey = 'unit'
        atomic_sp = {'ATOMIC_SPECIES':{'species':np.unique(species), 'mass':mass,'PP':PP},'unit':''}
        atspkeys = ['species','mass','PP']
        atomic_pos = {'ATOMIC_POSITIONS':{'species':species, 'wickoffs':wyck,'positions':positions},'unit':'crystal_sg'}
        atposkeys = ['species', 'wickoffs','positions']
        kpoints = {'K_POINTS':{'kpoints':kpt+kpt_offset},'unit':'automatic'}
        kptkeys = ['kpoints']
        
        qeInput = ''

        qeInput += makeNamelist('&CONTROL',control)
        qeInput += makeNamelist( '&SYSTEM' ,system,syskeys)
        qeInput += makeNamelist( '&ELECTRONS',electrons)
        qeInput += makeCard(atomic_sp,cardKeys = atspkeys,optionKey = optionkey,T = True) 
        qeInput += makeCard(atomic_pos,cardKeys = atposkeys,optionKey = optionkey,T = True) 
        qeInput += makeCard(kpoints,cardKeys = kptkeys,optionKey = optionkey,T = False)
        
        inputDic[SpaceGroupIdx] = qeInput
        
    return inputDic<jupyter_output><empty_output><jupyter_text>Load general information<jupyter_code>fileNames = {}
dataDir = '../'
fileNames['crystals'] = 'Si-crystals-full-relax.pck'
fileNames['wyck'] = dataDir+'SpaceGroup-multiplicity-wickoff-info.pck'
fileNames['general info'] = dataDir+'SpaceGroup-general-info.pck'
fileNames['elements info'] = dataDir+'General-Info-Elements-fast.pck'
# give the SG 
spaceGroupIdxRange  = range(1,230+1)

Gen  = GenCrystalStructures(spaceGroupIdxRange=spaceGroupIdxRange)
Gen.setCrystals(fileNames['crystals'])
with open(fileNames['wyck'],'rb') as f:
    WyckTable = pck.load(f)
SGTable = pd.read_pickle(fileNames['general info'])
ElemTable = pd.read_pickle(fileNames['elements info'])
crystals = Gen.crystals
pck.dump?
with open('230spg-crystals.pck','wb') as f:
    pck.dump(crystals,f)
inputDic = makeQEInnput(Gen.crystals,WyckTable,SGTable,ElemTable)
print inputDic[50]
def make_dir(file_path):
    directory = os.path.abspath(file_path)
    if not os.path.exists(directory):
        os.makedirs(directory)
#'/Users/iMac/Documents/Workdir/QMAT/check-QE-crystal_sg'
dirNames = {sg:'./data/sg-test_'+str(sg) for sg in range(1,230+1)}
import subprocess as sp
os.environ['TMPDIR'] = "/tmp/"
exitState = {}
for sg in dirNames:
    make_dir(dirNames[sg])
    inputName = os.path.abspath(dirNames[sg]+'/qe.in')
    outputName = os.path.abspath(dirNames[sg]+'/qe.out')
    errName = os.path.abspath(dirNames[sg]+'/qe.err')
    with open(inputName,'w') as f:
        f.write(inputDic[sg])
    param = '-in '+ inputName
    print param
    # Set up the echo command and direct the output to a pipe
    exitState[sg] = sp.call('mpirun -np 2 pw.x ' + param , stdout=open(outputName, 'w'), 
                             stderr=open(errName, 'w'), shell=True)
    if exitState[sg]:
        print sg

prbSg = []
for sg in exitState:
    if exitState[sg]:
        prbSg.append(sg)
print prbSg<jupyter_output>[1, 4, 7, 9, 19, 29, 33, 38, 39, 40, 41, 76, 78, 138, 144, 145, 146, 148, 155, 160, 161, 166, 167, 169, 170, 182, 225, 226, 227, 228]
{'fraction_non_alphanumeric': 0.1584064500829974, 'fraction_numerical': 0.025373488261797486, 'mean_word_length': 2.9844119036372225, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '414080', 'license': 'MIT', 'n_tokens_mistral': 3174, 'n_tokens_neox': 2919, 'n_words': 511, 'path': 'make_input/check-qe-sg-format.ipynb', 'repo_name': 'felixmusil/run_qe', 'size': '8434'}