コード例 #1
0
ファイル: universal.py プロジェクト: andersx/qctoolkit
def runCode(self, parrent, name, **kwargs):
    worker, name = \
      super(parrent, self).run(kwargs['program'], name, **kwargs)

    if 'no_subfolder' not in kwargs or not kwargs['no_subfolder']:
        self.setting['root_dir'] = name

    def run():
        if 'charge' in kwargs:
            self.setChargeMultiplicity(kwargs['charge'], 1)
        inp = self.write(name, **kwargs)
        new_name = None
        if 'new_name' in kwargs:
            new_name = kwargs['new_name']
        return worker.start(inp, new_name)

    if not os.path.exists(name):
        return run()
    elif 'rename_if_exist' in self.setting\
    and self.setting['rename_if_exist']:
        n_name = len(glob.glob("%s_[0-9]" % name)) + 1
        new_name = name + '_%d' % n_name
        while os.path.exists(new_name):
            n_name = n_name + 1
            new_name = name + '_%d' % n_name
        kwargs['root_dir'] = new_name
        qtk.warning("folder %s exists, running under %s" % (name, new_name))
        return run()
    elif self.setting['overwrite']:
        qtk.warning("Overwrite existing folder %s" % name)
        return run()
    else:
        qtk.report("QMInp.run", "%s exists" % name)
コード例 #2
0
ファイル: pseudo.py プロジェクト: andersx/qctoolkit
 def PPCheck(xc, element, pp_file_str, **kwargs):
   if xc == 'lda':
     xc = 'pade'
   ne = qtk.n2ve(element)
   try:
     if 'dcacp' in kwargs and kwargs['dcacp']:
       pp_path = os.path.join(xc.upper(), "%s_DCACP_%s" %\
                 (element, xc.upper()))
       if element in qtk.setting.dcacp_dict:
         pp_path = pp_path + "_%s" % qtk.setting.dcacp_dict[element]
       pp_file = os.path.join(qtk.setting.cpmd_dcacp_url, pp_path)
     else:
       pp_path = os.path.join(xc,
         element + '-q' + str(qtk.n2ve(element)))
       pp_file = os.path.join(qtk.setting.cpmd_pp_url, pp_path)
     saved_pp_path = os.path.join(qtk.setting.cpmd_pp, pp_file_str)
     if not os.path.exists(saved_pp_path) \
     and qtk.setting.download_pp:
       if pp_file:
         new_pp = os.path.join(qtk.setting.cpmd_pp, pp_file_str)
         pp_content = urllib2.urlopen(pp_file).read()
         qtk.report('PPCheck', 'pp file %s not found in %s, ' \
                    % (pp_file_str, qtk.setting.cpmd_pp) + \
                    'but found on internet, download now...')
         new_pp_file = open(new_pp, 'w')
         new_pp_file.write(pp_content)
         new_pp_file.close()
         pp_file = new_pp
     return saved_pp_path
   except:
     qtk.warning('something wrong with pseudopotential')
コード例 #3
0
ファイル: cube.py プロジェクト: andersx/qctoolkit
    def __call__(self, *coord, **kwargs):
        if 'unit' not in kwargs:
            unit = 'angstrom'
        else:
            unit = kwargs['unit'].lower()
            if unit not in ['angstrom', 'bohr']:
                qtk.warning('unit %s not reconized, set to Bohr' % unit)
                unit = 'bohr'

        R = np.atleast_2d(np.array(coord))
        if len(coord) == 3:
            try:
                x, y, z = [float(c) for c in coord]
                R = np.atleast_2d(np.array([x, y, z]))
            except TypeError:
                pass

        if unit == 'angstrom':
            R = [r / 0.529177249 for r in R]

        if not self.interp:
            self.interpolate()

        if len(R) == 1:
            return self.interp(R)[0]
        else:
            return self.interp(R)
コード例 #4
0
ファイル: general_io.py プロジェクト: andersx/qctoolkit
    def start(self, InpClass, new_name=None, root_dir=None):
        self.inp = InpClass
        if not new_name:
            self.name = self.inp.final_name
        else:
            self.name = new_name
        if self.inp.finalized:
            cwd = os.getcwd()
            os.chdir(self.inp.final_path)
            run_setup = copy.deepcopy(self.setting)
            del run_setup['program']

            if 'debug' in self.setting and self.setting['debug']:
                out = qmjob.QMRun(self.name, self.qmcode, **run_setup)
                os.chdir(cwd)
            else:
                try:
                    out = qmjob.QMRun(self.name, self.qmcode, **run_setup)
                except Exception, err:
                    qtk.warning("qmjob finished unexpectedly for '" + \
                                self.name + "'" + ", with error message:\n"\
                                + str(err))
                    out = GenericQMOutput()
                finally:
                    os.chdir(cwd)
コード例 #5
0
ファイル: pseudo.py プロジェクト: andersx/qctoolkit
 def resize(self, cn, hn):
   too_small = False
   if cn < self.param['Cn']:
     too_small = True
   else:
     self.param['Cn'] = cn
     tmp = [0 for i in range(cn)]
     tmp[:len(self.param['Ci'])] = self.param['Ci']
     self.param['Ci'] = copy.deepcopy(tmp)
   if len(hn) < len(self.param['h_ij']):
     too_small = True
   else:
     n = len(hn)
     tmp = [0 for i in range(n)]
     tmp[:len(self.param['r_nl'])] = self.param['r_nl']
     self.param['r_nl'] = copy.deepcopy(tmp)
     tmp = [np.zeros((0,0)) for i in range(len(hn))]
     tmp[:len(self.param['h_ij'])] = self.param['h_ij']
     self.param['h_ij'] = copy.deepcopy(tmp)
   if not too_small:
     for i in range(len(hn)):
       n = hn[i]
       h = self.param['h_ij'][i]
       if n > len(h):
         tmp = np.zeros((n,n))
         tmp[:len(h), :len(h)] = h
         self.param['h_ij'][i] = tmp
   else:
     qtk.warning('PP dimension too small')
   self.dim = self.getDim()
   return self
コード例 #6
0
ファイル: general_io.py プロジェクト: SamKChang/qctoolkit
  def start(self, InpClass, new_name=None):
    self.inp = InpClass
    if not new_name:
      self.name = self.inp.final_name
    else:
      self.name = new_name
    if self.inp.finalized:
      cwd = os.getcwd()
      os.chdir(self.inp.final_path)
      run_setup = copy.deepcopy(self.setting)
      del run_setup['program']

      if 'debug' in self.setting and self.setting['debug']:
        out = qmjob.QMRun(self.name, self.qmcode, **run_setup)
        os.chdir(cwd)
      else:
        try:
          out = qmjob.QMRun(self.name, self.qmcode, **run_setup)
        except Exception, err:
          qtk.warning("qmjob finished unexpectedly for '" + \
                      self.name + "'" + ", with error message:\n"\
                      + str(err))
          out = GenericQMOutput()
        finally:
          os.chdir(cwd)
コード例 #7
0
ファイル: parallizer.py プロジェクト: SamKChang/qctoolkit
def qmRunAll(inp_list, root=None,**kwargs):
  if 'block_size' not in kwargs:
    kwargs['block_size'] = 1
  job = []
  for inp in inp_list:
    job.append([inp, inp.molecule.name])
  inp = inp_list[0]
  if inp.setting['threads'] != 1 and 'threads' not in kwargs:
    kwargs['threads'] = setting.cpu_count / inp.setting['threads']
  if root is None:
    qtk.parallelize(qtk.qmRunJob, job, **kwargs)
  else:
    if os.path.exists(root):
      if 'overwrite' in kwargs and kwargs['overwrite']:
        qtk.warning("overwrite existing folder %s" % root)
        shutil.rmtree(root)
        os.makedirs(root)
      else:
        qtk.warning("%s exists, " % root +\
          "joining calculations with other threads")
    else:
      os.makedirs(root)
    cwd = os.getcwd()
    os.chdir(root)
    qtk.parallelize(qtk.qmRunJob, job, **kwargs)
    os.chdir(cwd)
コード例 #8
0
def qmRunAll(inp_list, root=None, **kwargs):
    if 'block_size' not in kwargs:
        kwargs['block_size'] = 1
    job = []
    for inp in inp_list:
        job.append([inp, inp.molecule.name])
    inp = inp_list[0]
    if inp.setting['threads'] != 1 and 'threads' not in kwargs:
        kwargs['threads'] = setting.cpu_count / inp.setting['threads']
    if root is None:
        qtk.parallelize(qtk.qmRunJob, job, **kwargs)
    else:
        if os.path.exists(root):
            if 'overwrite' in kwargs and kwargs['overwrite']:
                qtk.warning("overwrite existing folder %s" % root)
                shutil.rmtree(root)
                os.makedirs(root)
            else:
                qtk.warning("%s exists, " % root +\
                  "joining calculations with other threads")
        else:
            os.makedirs(root)
        cwd = os.getcwd()
        os.chdir(root)
        qtk.parallelize(qtk.qmRunJob, job, **kwargs)
        os.chdir(cwd)
コード例 #9
0
ファイル: molecule.py プロジェクト: SamKChang/qctoolkit
  def write_xyz(self, name=None, **kwargs):

    def listStr(data_list):
      out = str(data_list)
      out = out.replace('[', '')
      out = out.replace(']', '')
      return out.replace(',', '')

    out = sys.stdout if not name else open(name,"w")

    if 'fractional' in kwargs and kwargs['fractional']:
      if self.celldm and self.scale:
        out.write(str(self.N)+"\n\n")
        for I in xrange(0, self.N):
          out.write("%-2s " % self.type_list[I])
          out.write(" ".join("% 8.4f" % i \
            for i in self.R_scale[I][:]))
          out.write('\n')
        out.write("\ncelldm: %s\n" % listStr(self.celldm))
        out.write('scale: %s\n' % listStr(self.scale))
        if self.symmetry:
          out.write('symmetry: %s\n' % self.symmetry)
      else:
        del kwargs['fractional']
        qtk.warning('celldm or scale not set, print cartician')
        self.write(name, **kwargs)
    else:
      out.write(str(self.N)+"\n\n")
      for I in xrange(0, self.N):
        out.write("%-2s " % self.type_list[I])
        out.write(" ".join("% 8.4f" % i for i in self.R[I][:]))
        out.write("\n")
  
      if name:
        out.close()
コード例 #10
0
ファイル: cube.py プロジェクト: SamKChang/qctoolkit
  def __call__(self, *coord, **kwargs):
    if 'unit' not in kwargs:
      unit = 'angstrom'
    else:
      unit = kwargs['unit'].lower()
      if unit not in ['angstrom', 'bohr']:
        qtk.warning('unit %s not reconized, set to Bohr' % unit)
        unit = 'bohr'

    R = np.atleast_2d(np.array(coord))
    if len(coord) == 3:
      try:
        x, y, z = [float(c) for c in coord]
        R = np.atleast_2d(np.array([x,y,z]))
      except TypeError:
        pass

    if unit == 'angstrom':
      R = [r / 0.529177249 for r in R]

    if not self.interp:
      self.interpolate()

    if len(R) == 1:
      return self.interp(R)[0]
    else:
      return self.interp(R)
コード例 #11
0
ファイル: general_io.py プロジェクト: andersx/qctoolkit
 def cm_check(self, mol):
     ve = mol.getValenceElectrons()
     if (ve % 2) == (mol.multiplicity % 2):
         msg = "Multiplicity %d " % mol.multiplicity + \
               "and %d valence electrons " % ve +\
               "\n(with charge %3.1f) " % float(mol.charge) +\
               "are not compatible"
         qtk.warning(msg)
コード例 #12
0
def pdump(obj, name):
    try:
        with open(name, 'wb') as pfile:
            pickle.dump(obj, pfile)
    except TypeError:
        qtk.warning('possible issues with ctypes extensions')
        obj = copy.deepcopy(obj)
        structure = qtk.get_nested_structure(obj)
コード例 #13
0
ファイル: element_tool.py プロジェクト: SamKChang/qctoolkit
def n2Z(Zn):
    match = [m for m in z_list.iterkeys() if m in Zn]
    mlen = [len(s) for s in match]
    if len(match) > 0:
        ind = np.argmax(mlen)
        return float(z_list[match[ind]])
    else:
        qtk.warning("n2Z: element type " + str(Zn) + " is not defined, returning nuclear charge 0")
        return 0
コード例 #14
0
    def run(self, name=None, **kwargs):

        self.setting.update(kwargs)

        if self.setting['theory'] in ['hf']:
            optimizer = PlainSCFSolver
            opt_arg = [
                self.ht_ham, self.ht_olp, self.ht_occ_model, self.ht_exp_alpha
            ]
        elif is_gga(self) or is_mgga(self):
            optimizer = CDIISSCFSolver
            opt_arg = [
                self.ht_ham, self.ht_olp, self.ht_occ_model, self.ht_dm_alpha
            ]
        elif is_hgga(self) or is_mhgga(self):
            optimizer = EDIIS2SCFSolver
            opt_arg = [
                self.ht_ham, self.ht_olp, self.ht_occ_model, self.ht_dm_alpha
            ]

        scf_solver = optimizer(threshold=self.setting['wf_convergence'],
                               maxiter=self.setting['scf_step'])
        scf_solver(*opt_arg)

        if self.setting['theory'] is not 'hf':
            fock_alpha = np.zeros(self.ht_olp.shape)
            self.ht_ham.reset(self.ht_dm_alpha)
            self.ht_ham.compute_energy()
            self.ht_ham.compute_fock(fock_alpha)
            self.ht_exp_alpha.from_fock_and_dm(fock_alpha, self.ht_dm_alpha,
                                               self.ht_olp)

        self.ht_solver = scf_solver

        try:
            self.Et = self.ht_ham.cache['energy']
            self.mov = self.ht_exp_alpha.coeffs.__array__()
            self.mo_vectors = self.mov
            self.mo_eigenvalues = self.ht_exp_alpha.energies

        except Exception as err:
            qtk.warning('SCF did not converged: %s' % err)
            self.Et = np.nan
        self.mol = self.molecule

        out = qtk.QM.gaussianbasis_io.GaussianBasisOutput()

        for attr in dir(out):
            if not hasattr(self, attr):
                setattr(self, attr, getattr(out, attr))

        for attr in dir(self):
            if not attr.startswith('_'):
                setattr(out, attr, getattr(self, attr))

        return out
コード例 #15
0
def n2Z(Zn):
  match = [m for m in z_list.iterkeys() if m in Zn]
  mlen = [len(s) for s in match]
  if len(match) > 0:
    ind = np.argmax(mlen)
    return float(z_list[match[ind]])
  else:
    qtk.warning("n2Z: element type " + str(Zn) +\
                " is not defined, returning nuclear charge 0")
    return 0
コード例 #16
0
ファイル: molecule.py プロジェクト: SamKChang/qctoolkit
  def read_xyz(self, name, **kwargs):
    xyz = open(name, 'r')
    content = xyz.readlines()
    xyz.close()
    content = [line.replace('\t', ' ') for line in content]

    prop_list = ['charge', 'celldm', 'scale', 'symmetry']
    for prop in prop_list:
      try:
        prop_str = filter(lambda x: prop in x, content)[0]
        prop_str = re.sub('.*:', '', prop_str)
        prop_data = prop_str.split(' ')
        prop_data = filter(None, prop_data)
        if len(prop_data) == 1:
          try:
            setattr(self, prop, float(prop_data[0]))
          except Exception as exc:
            if prop == 'symmetry':
              setattr(self, prop, prop_data[0].strip())
        elif len(prop_data) > 1:
          setattr(self, prop, [float(_) for _ in prop_data])
      except ValueError as exc:
        setattr(self, prop, False)
        qtk.warning("setting attribute %s with error: %s" % \
          (prop, exc))
      except:
        setattr(self, prop, False)
    
    if not self.charge:
      self.charge = 0
    if self.celldm or self.scale:
      self.periodic = True
    if self.celldm:
       assert len(self.celldm) == 6

    self.N = int(content[0])
    coord_list = content[2 : self.N + 2]
    coord = [filter(None,[a for a in entry.split(' ')]) 
             for entry in coord_list]
    type_list = list(np.array(coord)[:,0])
    self.type_list = [str(elem) for elem in type_list]
    self.Z = [qtk.n2Z(elem) for elem in self.type_list]
    self.Z = np.array(self.Z)
    self.R = np.array(coord)[:,1:4].astype(float)

    self.box = False
    if self.celldm:
      self.periodic = True
      angle = self.celldm[3:]
      angle_sum = sum([abs(entry) for entry in angle])
      if angle_sum == 0:
        self.box = self.celldm[:3]

      self.R_scale = copy.deepcopy(self.R)
      self.R = qtk.fractional2xyz(self.R_scale, self.celldm)
コード例 #17
0
ファイル: element_tool.py プロジェクト: SamKChang/qctoolkit
def Z2n(Z):
    if type_list.has_key(Z):
        return type_list[Z]
    elif type(Z) is float:
        return "ATOM_%4.2f" % Z
        qtk.warning("Z2n: atomic number not defined, return HETATM")
    elif type(Z) is int:
        return "ATOM_%d" % Z
        qtk.warning("Z2n: atomic number not defined, return HETATM")
    else:
        qtk.exit("Z2n: atomic number " + str(Z) + " is not defined")
コード例 #18
0
def Z2n(Z):
  if type_list.has_key(Z):
    return type_list[Z]
  elif type(Z) is float:
    return 'ATOM_%4.2f' % Z
    qtk.warning('Z2n: atomic number not defined, return HETATM')
  elif type(Z) is int:
    return 'ATOM_%d' % Z
    qtk.warning('Z2n: atomic number not defined, return HETATM')
  else:
    qtk.exit("Z2n: atomic number " + str(Z) + " is not defined")
コード例 #19
0
def PPCheck(xc, pp_theory, pp_path, element):

    theory_dict = {
        'lda': 'pade',
        'pbe0': 'pbe',
        'pbesol': 'pbe',
        'hse06': 'pbe',
    }

    name = '%s_%s_%s' % (element, xc, pp_theory)
    pp_file = os.path.join(pp_path, name)
    if not os.path.exists(pp_file) and qtk.setting.download_pp:
        if pp_theory != 'nlcc':
            if pp_theory in theory_dict.keys():
                pp_theory = theory_dict[pp_theory]
            url_root = qtk.setting.bigdft_pp_url
            element_str = element + '-q%d' % qtk.n2ve(element)
            url = url_root + '%s/%s' % (pp_theory, element_str)
            page = False
            try:
                page = urllib2.urlopen(url).readlines()
                pattern = re.compile(r'^.*</*pre>.*$')
                pp_se = filter(pattern.match, page)
                pp_start = page.index(pp_se[0])
                pp_end = page.index(pp_se[1])
                page = page[pp_start:pp_end]
                page[0] = page[0].split('>')[-1]
            except:
                qtk.warning('something wrong with url:%s' % url)
            pp = ''.join(page)
        else:
            url = qtk.setting.bigdft_pp_nlcc_url
            page = urllib2.urlopen(url).readlines()
            string = filter(lambda x: '"psppar.%s' % element in x, page)[-1]
            index = page.index(string) + 2
            pp = []
            itr = 0
            while '</pre>' not in page[index + itr] \
            and index + itr < len(page)\
            and itr < 20:
                pp.append(page[index + itr])
                itr = itr + 1
            pp = ''.join(pp)
        if pp:
            qtk.report('', 'pp file %s not found in %s.' %\
              (name, pp_path) +\
              ' But found in cp2k page, download now...')
            new_pp_file = open(pp_file, 'w')
            new_pp_file.write(pp)
            new_pp_file.close()

    return pp_file
コード例 #20
0
    def push(self, content=None, data=None, comment=None, date=None):
        if date is None:
            date = dt.now()
        entry = Entry(date=date, content=content, comment=comment, data=data)

        qtk.progress("DB", "adding entry", content, data, comment)
        self.session.add(entry)
        try:
            qtk.progress("DB", "attempt to commit...")
            self.session.commit()
            qtk.progress("DB", "done")
        except Exception as err:
            qtk.warning('can not commit, error: %s' % err)
コード例 #21
0
    def __init__(self, qmout, **kwargs):
        WaveletOutput.__init__(self, qmout, **kwargs)
        self.info = ''

        try:
            with open(qmout, 'r') as raw_yml:
                self.detail = yaml.load(raw_yml)
        except Exception as err:
            qtk.warning("error when loading yaml file: %s" % err)

        if 'Last Iteration' in self.detail.keys():
            self.Et = self.detail['Last Iteration']['EKS']
        else:
            qtk.warning("Entry Last Iteration not found")
コード例 #22
0
 def sys_run(cmd_str, log_file=None):
     if log_file:
         log = open(log_file, 'w')
     try:
         qtk.progress("QMRun", cmd_str)
         if log_file:
             run = sp.Popen(cmd_str, shell=True, stdout=log)
         else:
             run = sp.Popen(cmd_str, shell=True)
         run.wait()
     except Exception as err:
         qtk.warning('%s failed with error: %s' % (cmd_str, err))
     if log_file:
         log.close()
コード例 #23
0
ファイル: bigdft.py プロジェクト: SamKChang/qctoolkit
def PPCheck(xc, pp_theory, pp_path, element):

  theory_dict = {
    'lda': 'pade',
  }

  name = '%s_%s_%s' % (element, xc, pp_theory)
  pp_file = os.path.join(pp_path, name)
  if not os.path.exists(pp_file) and qtk.setting.download_pp:
    if pp_theory != 'nlcc':
      if pp_theory in theory_dict.keys():
        pp_theory = theory_dict[pp_theory]
      url_root = qtk.setting.bigdft_pp_url
      element_str = element + '-q%d' % qtk.n2ve(element)
      url = url_root + '%s/%s' % (pp_theory, element_str)
      page = False
      try:
        page = urllib2.urlopen(url).readlines()
        pattern = re.compile(r'^.*</*pre>.*$')
        pp_se = filter(pattern.match, page)
        pp_start = page.index(pp_se[0])
        pp_end = page.index(pp_se[1])
        page = page[pp_start:pp_end]
        page[0] = page[0].split('>')[-1]
      except:
        qtk.warning('something wrong with url:%s' % url)
      pp = ''.join(page)
    else:
      url = qtk.setting.bigdft_pp_nlcc_url
      page = urllib2.urlopen(url).readlines()
      string = filter(lambda x: '"psppar.%s' % element in x, page)[-1]
      index = page.index(string) + 2
      pp = []
      itr = 0
      while '</pre>' not in page[index + itr] \
      and index + itr < len(page)\
      and itr < 20:
        pp.append(page[index + itr])
        itr = itr + 1
      pp = ''.join(pp)
    if pp:
      qtk.report('', 'pp file %s not found in %s.' %\
        (name, pp_path) +\
        ' But found in cp2k page, download now...')
      new_pp_file = open(pp_file, 'w')
      new_pp_file.write(pp)
      new_pp_file.close()
      
  return pp_file
コード例 #24
0
ファイル: general_io.py プロジェクト: SamKChang/qctoolkit
 def cleanPath(self, path, force=False):
   if os.path.exists(path):
     if force:
       try:
         os.remove(path)
       except OSError:
         shutil.rmtree(path)
     else:
       qtk.prompt(path + ' exists, overwrite?')
       try:
         os.remove(path)
       except OSError:
         shutil.rmtree(path)
       except:
         qtk.warning("can not remove file: " + path)
コード例 #25
0
ファイル: general_io.py プロジェクト: andersx/qctoolkit
 def cleanPath(self, path, force=False):
     if os.path.exists(path):
         if force:
             try:
                 os.remove(path)
             except OSError:
                 shutil.rmtree(path)
         else:
             qtk.prompt(path + ' exists, overwrite?')
             try:
                 os.remove(path)
             except OSError:
                 shutil.rmtree(path)
             except:
                 qtk.warning("can not remove file: " + path)
コード例 #26
0
ファイル: espresso.py プロジェクト: SamKChang/qctoolkit
def PPCheck(xc, element, pp_file_str, **kwargs):
  ne = qtk.n2ve(element)
  saved_pp_path = os.path.join(qtk.setting.espresso_pp, pp_file_str)
  if not os.path.exists(saved_pp_path) and qtk.setting.download_pp:
    qtk.status("PPCheck", pp_file_str)
    url = os.path.join(qtk.setting.espresso_pp_url, pp_file_str)
    try:
      pp_content = urllib2.urlopen(url).read()
      qtk.report('', 'pp file %s not found in %s. ' \
                 % (pp_file_str, qtk.setting.espresso_pp) + \
                 'but found in espresso page, download now...')
      new_pp_file = open(saved_pp_path, 'w')
      new_pp_file.write(pp_content)
      new_pp_file.close()
    except:
      qtk.warning('something wrong with pseudopotential')
コード例 #27
0
ファイル: parallizer.py プロジェクト: SamKChang/qctoolkit
def qmWriteAll(inp_list, root, **kwargs):
  if os.path.exists(root):
    if 'overwrite' in kwargs and kwargs['overwrite']:
      qtk.warning("overwrite existing folder %s" % root)
      shutil.rmtree(root)
      os.makedirs(root)
    else:
      qtk.warning("%s exists, " % root +\
        "joining calculations with other threads")
  else:
    os.makedirs(root)
  cwd = os.getcwd()
  os.chdir(root)
  for inp in inp_list:
    inp.write(inp.molecule.name)
  os.chdir(cwd)
コード例 #28
0
def primitiveCell(symmetry):
    if symmetry == 'fcc':
        return 0.5 * np.array([
            [0, 1, 1],
            [1, 0, 1],
            [1, 1, 0],
        ])
    elif symmetry == 'bcc':
        return 0.5 * np.array([
            [-1, 1, 1],
            [1, -1, 1],
            [1, 1, -1],
        ])
    else:
        qtk.warning('symmetry %s not found' % symmetry)
        return np.ones(3)
コード例 #29
0
ファイル: molecule.py プロジェクト: SamKChang/qctoolkit
 def removeAtoms(self, indices):
   if type(indices) is int:
     indices = [indices]
   indices.sort()
   i_max = indices[-1]
   if i_max <= self.N - 1:
     for i in range(len(indices)):
       index = indices[len(indices) - 1 - i]
       self.N -= 1
       self.R = np.delete(self.R, index, 0)
       self.Z = np.delete(self.Z, index)
       self.type_list = list(np.delete(self.type_list, index))
   else:
     msg = "index:%d out of range:%d, nothing has happend"\
           % (index, self.N)
     qtk.warning(msg)
コード例 #30
0
ファイル: espresso.py プロジェクト: andersx/qctoolkit
def PPCheck(xc, element, pp_file_str, **kwargs):
    ne = qtk.n2ve(element)
    saved_pp_path = os.path.join(qtk.setting.espresso_pp, pp_file_str)
    if not os.path.exists(saved_pp_path) and qtk.setting.download_pp:
        qtk.status("PPCheck", pp_file_str)
        url = os.path.join(qtk.setting.espresso_pp_url, pp_file_str)
        try:
            pp_content = urllib2.urlopen(url).read()
            qtk.report('', 'pp file %s not found in %s. ' \
                       % (pp_file_str, qtk.setting.espresso_pp) + \
                       'but found in espresso page, download now...')
            new_pp_file = open(saved_pp_path, 'w')
            new_pp_file.write(pp_content)
            new_pp_file.close()
        except:
            qtk.warning('something wrong with pseudopotential')
コード例 #31
0
def qmWriteAll(inp_list, root, **kwargs):
    if os.path.exists(root):
        if 'overwrite' in kwargs and kwargs['overwrite']:
            qtk.warning("overwrite existing folder %s" % root)
            shutil.rmtree(root)
            os.makedirs(root)
        else:
            qtk.warning("%s exists, " % root +\
              "joining calculations with other threads")
    else:
        os.makedirs(root)
    cwd = os.getcwd()
    os.chdir(root)
    for inp in inp_list:
        inp.write(inp.molecule.name)
    os.chdir(cwd)
コード例 #32
0
 def removeAtoms(self, indices):
   if type(indices) is int:
     indices = [indices]
   indices.sort()
   i_max = indices[-1]
   if i_max <= self.N - 1:
     for i in range(len(indices)):
       index = indices[len(indices) - 1 - i]
       self.N -= 1
       self.R = np.delete(self.R, index, 0)
       self.Z = np.delete(self.Z, index)
       self.type_list = list(np.delete(self.type_list, index))
   else:
     msg = "index:%d out of range:%d, nothing has happend"\
           % (index, self.N)
     qtk.warning(msg)
コード例 #33
0
ファイル: parallizer.py プロジェクト: SamKChang/qctoolkit
 def run_jobs(q_in, q_out):
   for inps in iter(q_in.get, None):
     ind = inps[-1]    # index of job
     inps = inps[:-1]  # actual input sequence
     out = []
     try:
       for args in inps:
         if type(args[-1]) == dict: # check known args input
           kwargs = args[-1]
           args = args[:-1]  
           out.append(target_function(*args, **kwargs))
         else:
           out.append(target_function(*args))
       q_out.put([out, ind]) # output result with index
     except: 
       qtk.warning('job failed!')
       q_out.put([np.nan, ind])
コード例 #34
0
    def __init__(self, pattern, qm_property='Et', **kwargs):

        if 'program' not in kwargs:
            kwargs['program'] = qtk.setting.qmcode
        if 'unit' not in kwargs:
            kwargs['unit'] = 'Eh'

        out_files = sorted(glob.glob(pattern))
        if not out_files:
            qtk.warning('no output files matched %s' % pattern)

        files = []
        values = []
        self.qmout = []
        self.names = []
        for out in out_files:
            out_file = os.path.split(out)[1]
            out_file = re.sub('\.out', '', out_file)
            qmout = qtk.QMOut(out, program=kwargs['program'])
            qmout.inUnit(kwargs['unit'])
            try:
                value = getattr(qmout, qm_property)
            except:
                qtk.warning('%s not found for file %s' %\
                  (qm_property, out_file))
                value = np.nan
            files.append(out_file)
            values.append(value)
            self.names.append(out_file)
            self.qmout.append(qmout)
        self.data = pd.Series(values, index=files)

        method_strlist = [m for m in dir(self.data)]
        method_list = []
        for m in method_strlist:
            try:
                if callable(getattr(self.data, m)):
                    method_list.append(m)
            except:
                pass

        p = re.compile('^[a-z].*$')
        method_list = filter(p.match, method_list)
        for m in method_list:
            if m not in dir(self):
                setattr(self, m, getattr(self.data, m))
コード例 #35
0
 def run_jobs(q_in, q_out):
     for inps in iter(q_in.get, None):
         ind = inps[-1]  # index of job
         inps = inps[:-1]  # actual input sequence
         out = []
         try:
             for args in inps:
                 if type(args[-1]) == dict:  # check known args input
                     kwargs = args[-1]
                     args = args[:-1]
                     out.append(target_function(*args, **kwargs))
                 else:
                     out.append(target_function(*args))
             q_out.put([out, ind])  # output result with index
         except:
             qtk.warning('job failed!')
             q_out.put([np.nan, ind])
コード例 #36
0
 def update(self, inp_list, out_list, info_list):
     print len(inp_list), len(out_list), len(info_list)
     print inp_list, out_list
     for i in range(len(inp_list)):
         content = str(inp_list[i])
         comment = str(info_list[i])
         data = out_list[i]
         q = self.log.list(content)
         if len(q) == 0:
             qtk.warning("no entry found with %s" % content)
             self.log.push(content, data, comment)
         else:
             if len(q) > 1:
                 qtk.warning("found multiple entry with %s" % content)
             entry = q[-1]
             entry.data = out_list[i]
             entry.comment = info_list[i]
             self.log.commit()
コード例 #37
0
ファイル: cube.py プロジェクト: SamKChang/qctoolkit
def read_gaussian(fchk, **kwargs):
  if 'name' in kwargs:
    cube = kwargs['name']
    root, ext = os.path.splitext(cube)
    if ext != '.cube':
      print ext
      qtk.warning("extension .cube is required for many " + \
                  "visualization programs")
  else:
    root, ext = os.path.splitext(fchk)
    cube = root + '.cube'
  qtk.progress("CUBE", "writing file %s\n" % cube)
  if 'flag' not in kwargs:
    flag = 'density=scf'
  else:
    flag = kwargs['flag']
  if 'grid' in kwargs:
    grid = kwargs['grid']
    cmd = '%s 1 %s %s %s -1' % (qtk.gaussian_cubegen_exe, 
                                flag, fchk, cube)
    run = sp.Popen(cmd, shell=True, stdin=sp.PIPE)
    for i in range(len(grid)):
      vec = grid[i]
      if i == 0:
        # for formated output
        msg = '-1 %f %f %f\n' % (vec[1], vec[2], vec[3])
      elif i == 1:
        # for Bohr as grid unit
        msg = '%d %f %f %f\n' % (-vec[0], vec[1], vec[2], vec[3])
      else:
        msg = '%d %f %f %f\n' % (vec[0], vec[1], vec[2], vec[3])
      run.stdin.write(msg)
  else:
    cmd = '%s 1 %s %s %s' % (qtk.gaussian_cubegen_exe, 
                             flag, fchk, cube)
    run = sp.Popen(cmd, shell=True, stdin=sp.PIPE)
  run.stdin.flush()
  run.communicate()
  run.wait()
  q = qtk.CUBE(cube, format='cube')
  zcoord = np.hstack([q.molecule.Z[:, np.newaxis], q.molecule.R])
  zcoord[:,1:4] = zcoord[:,1:4] / 0.529177249
  return q.data, zcoord, q.grid
コード例 #38
0
ファイル: ccs.py プロジェクト: andersx/qctoolkit
 def get_coord(self, mol):
     qtk.warning(
         "temporary implementation for special cases of cubic AlGaAs")
     coord = self.random()[1]
     mut_list = coord['mutation'][0]
     ref_list = self.mutation_list[0]
     scale = np.asarray(self.structure.scale)
     for ind_i in range(len(ref_list)):
         i = ref_list[ind_i]
         base_R = self.structure.R_scale[i]
         for ind_j in range(len(ref_list)):
             j = ref_list[ind_j]
             diff = np.linalg.norm(base_R - mol.R_scale[j] * scale)
             print diff
             if diff < 1E-5:
                 print 'yo'
                 mut_list[ind_j] = mol.Z[j]
     print mut_list
     print coord
     return coord
コード例 #39
0
ファイル: universal.py プロジェクト: andersx/qctoolkit
def PPCheck(xc, element, pp_file_str, **kwargs):
    ne = qtk.n2ve(element)
    try:
        pp_path = os.path.join(xc, element + '-q' + str(qtk.n2ve(element)))
        pp_file = os.path.join(qtk.setting.cpmd_pp_url, pp_path)
        saved_pp_path = os.path.join(qtk.setting.cpmd_pp, pp_file_str)
        if not os.path.exists(saved_pp_path) and qtk.setting.download_pp:
            if pp_file:
                new_pp = os.path.join(qtk.setting.cpmd_pp, pp_file_str)
                pp_content = urllib2.urlopen(pp_file).read()
                qtk.report('', 'pp file %s not found in %s. ' \
                           % (pp_file_str, qtk.setting.cpmd_pp) + \
                           'but found in cp2k page, download now...')
                new_pp_file = open(new_pp, 'w')
                new_pp_file.write(pp_content)
                new_pp_file.close()
                pp_file = new_pp
        return saved_pp_path
    except:
        qtk.warning('something wrong with pseudopotential')
コード例 #40
0
ファイル: universal.py プロジェクト: SamKChang/qctoolkit
def PPCheck(xc, element, pp_file_str, **kwargs):
  ne = qtk.n2ve(element)
  try:
    pp_path = os.path.join(xc, element + '-q' + str(qtk.n2ve(element)))
    pp_file = os.path.join(qtk.setting.cpmd_pp_url, pp_path)
    saved_pp_path = os.path.join(qtk.setting.cpmd_pp, pp_file_str)
    if not os.path.exists(saved_pp_path) and qtk.setting.download_pp:
      if pp_file:
        new_pp = os.path.join(qtk.setting.cpmd_pp, pp_file_str)
        pp_content = urllib2.urlopen(pp_file).read()
        qtk.report('', 'pp file %s not found in %s. ' \
                   % (pp_file_str, qtk.setting.cpmd_pp) + \
                   'but found in cp2k page, download now...')
        new_pp_file = open(new_pp, 'w')
        new_pp_file.write(pp_content)
        new_pp_file.close()
        pp_file = new_pp
    return saved_pp_path
  except:
    qtk.warning('something wrong with pseudopotential')
コード例 #41
0
def Molecules(file_name, **kwargs):
    """read nested xyz file and return molecule list"""
    xyz = open(file_name, 'r')
    content = xyz.readlines()
    content = [line.replace('\t', ' ') for line in content]
    xyz.close()

    itr = 0
    more_data = True
    mols = []

    while more_data:
        try:
            N = int(content[itr])
            prop_list = content[itr + 1]
            try:
                prop_list = np.array(prop_list.split(' ')).astype(float)
            except Exception as err:
                qtk.warning(str(err))
                prop_list = prop_list
            coord_list = content[itr + 2:itr + N + 2]
            coord = [
                filter(None, [a for a in entry.split(' ')])
                for entry in coord_list
            ]
            type_list = list(np.array(coord)[:, 0])
            type_list = [str(elem) for elem in type_list]
            Z = np.array([qtk.n2Z(elem) for elem in type_list])
            R = np.array(coord)[:, 1:4].astype(float)
            mol_data = {}
            for var in ['N', 'type_list', 'Z', 'R']:
                mol_data[str(var)] = eval(var)
            itr += N + 2
            mols.append(qtk.Molecule(molecule_data=mol_data))
        except Exception as err:
            qtk.progress(
                "Molecules", "%d molecules have been loaded with message %s." %
                (len(mols), str(err)))
            more_data = False

    return mols
コード例 #42
0
  def write_xyz(self, name=None, **kwargs):

    def listStr(data_list):
      out = str(data_list)
      out = out.replace('[', '')
      out = out.replace(']', '')
      return out.replace(',', '')

    out = sys.stdout if not name else open(name,"w")

    if 'fractional' in kwargs and kwargs['fractional']:
      if self.celldm and self.scale:
        if 'comment' in kwargs:
          out.write(str(self.N)+"\n" + str(kwargs['comment']) + "\n")
        else:
          out.write(str(self.N)+"\n\n")
        for I in xrange(0, self.N):
          out.write("%-2s " % self.type_list[I])
          out.write(" ".join("% 8.4f" % i \
            for i in self.R_scale[I][:]))
          out.write('\n')
        out.write("\ncelldm: %s\n" % listStr(self.celldm))
        out.write('scale: %s\n' % listStr(self.scale))
        if self.symmetry:
          out.write('symmetry: %s\n' % self.symmetry)
      else:
        del kwargs['fractional']
        qtk.warning('celldm or scale not set, print cartician')
        self.write(name, **kwargs)
    else:
      if 'comment' in kwargs:
        out.write(str(self.N)+"\n" + str(kwargs['comment']) + "\n")
      else:
        out.write(str(self.N)+"\n\n")
      for I in xrange(0, self.N):
        out.write("%-2s " % self.type_list[I])
        out.write(" ".join("% 8.4f" % i for i in self.R[I][:]))
        out.write("\n")
  
      if name:
        out.close()
コード例 #43
0
ファイル: cube.py プロジェクト: andersx/qctoolkit
def read_gaussian(fchk, **kwargs):
    if 'name' in kwargs:
        cube = kwargs['name']
        root, ext = os.path.splitext(cube)
        if ext != '.cube':
            print ext
            qtk.warning("extension .cube is required for many " + \
                        "visualization programs")
    else:
        root, ext = os.path.splitext(fchk)
        cube = root + '.cube'
    qtk.progress("CUBE", "writing file %s\n" % cube)
    if 'flag' not in kwargs:
        flag = 'density=scf'
    else:
        flag = kwargs['flag']
    if 'grid' in kwargs:
        grid = kwargs['grid']
        cmd = '%s 1 %s %s %s -1' % (qtk.gaussian_cubegen_exe, flag, fchk, cube)
        run = sp.Popen(cmd, shell=True, stdin=sp.PIPE)
        for i in range(len(grid)):
            vec = grid[i]
            if i == 0:
                # for formated output
                msg = '-1 %f %f %f\n' % (vec[1], vec[2], vec[3])
            elif i == 1:
                # for Bohr as grid unit
                msg = '%d %f %f %f\n' % (-vec[0], vec[1], vec[2], vec[3])
            else:
                msg = '%d %f %f %f\n' % (vec[0], vec[1], vec[2], vec[3])
            run.stdin.write(msg)
    else:
        cmd = '%s 1 %s %s %s' % (qtk.gaussian_cubegen_exe, flag, fchk, cube)
        run = sp.Popen(cmd, shell=True, stdin=sp.PIPE)
    run.stdin.flush()
    run.communicate()
    run.wait()
    q = qtk.CUBE(cube, format='cube')
    zcoord = np.hstack([q.molecule.Z[:, np.newaxis], q.molecule.R])
    zcoord[:, 1:4] = zcoord[:, 1:4] / 0.529177249
    return q.data, zcoord, q.grid
コード例 #44
0
def write(self, cpmd_name, espresso_name):
    if not os.path.exists(cpmd_name):
        qtk.report("PP", "writing cpmd PP file")
        cpmd_write(self, cpmd_name)
        cpmd_exists = False
    else:
        cpmd_exists = True
        qtk.prompt('cpmd pp path:%s exist' % cpmd_name)
    if (cpmd_name == espresso_name and not cpmd_exists)\
    or not os.path.exists(espresso_name):
        qtk.report("PP", 'start converting Goedecker PP')
        conv_pp = sp.Popen("%s %s" % \
          (qtk.setting.espresso_cpmd2upf_exe, cpmd_name),
          shell=True)
        conv_pp.wait()
        if conv_pp.returncode != 0:
            qtk.warning('conversion failed...')
        else:
            os.rename(cpmd_name + '.UPF', espresso_name)
    else:
        qtk.prompt('espresso pp path:%s exist' % espresso_name)
コード例 #45
0
ファイル: dataset.py プロジェクト: SamKChang/qctoolkit
  def setDescriptor(self, descriptor, **kwargs):
    self.descriptor = descriptor
    if 'threads' in kwargs:
      self.threads = int(kwargs['threads'])
    else:
      self.threads = 1
    qtk.report("DataSet", "reading folder", self.path)
    qtk.report("Descriptor", self.descriptor)
    if descriptor == 'CoulombMatrix':
      if 'matrix_size' not in kwargs:
        qtk.warning("matrix size not assigend, " + \
                    "using default value")
        qtk.warning("matrix size WILL CHANGE " + \
                    "according to numer of atoms in the molecule")
        self.matrix_size = 0
      else:
        self.matrix_size = kwargs['matrix_size']
    else:
      qtk.exit("descriptor" + descriptor + "is not implemented")

    if self.threads > 1:
      data_list = []
      for data in sorted(\
                    glob.glob(self.path + '/' + self.pattern)):
        data_list.append([descriptor, 
                          self.matrix_size, 
                          {'xyz':data}])
      self.data = qtk.parallelize(DataPoint,
                                  data_list,
                                  threads=self.threads)
    else:
      for data in sorted(\
                    glob.glob(self.path + '/' + self.pattern)):
        self.data.append(\
          DataPoint(descriptor, self.matrix_size, xyz=data)\
        )

    self.data_size = len(self.data)
コード例 #46
0
ファイル: universal.py プロジェクト: SamKChang/qctoolkit
def runCode(self, parrent, name, **kwargs):
  worker, name = \
    super(parrent, self).run(kwargs['program'], name, **kwargs)

  if 'no_subfolder' not in kwargs or not kwargs['no_subfolder']:
    self.setting['root_dir'] = name

  def run():
    if 'charge' in kwargs:
      self.setChargeMultiplicity(kwargs['charge'], 1)
    inp = self.write(name, **kwargs)
    new_name = None
    if 'new_name' in kwargs:
      new_name = kwargs['new_name']
    return worker.start(inp, new_name)

  if not os.path.exists(name):
    return run()
  elif self.setting['overwrite']:
    qtk.warning("Overwrite existing folder %s" % name)
    return run()
  else:
    qtk.report("QMInp.run", "%s exists" % name)
コード例 #47
0
    def plot_band(self, path, dk=0.1, n_cb=2, n_vb=2, krange=1, ax=None):

        if not hasattr(self, 'kpoints_symmetrized'):
            qtk.warning(
                'Make sure full kmesh is available. Brillouinize might help')

        p, ticks = self._kPath(path, dk)
        x = np.linspace(0, len(p) - 1, 100)
        tick_pos, tick_txt = ticks[0], ticks[1]
        if ax is None:
            fig, ax = plt.subplots()
        bands = []
        bands_out = []
        occ = np.array(self.occupation)
        vb_top = np.where(occ < 1)[0][0]
        for b in range(vb_top - n_vb, vb_top + n_cb):
            bout, cb = self._interpolate(b, krange=krange)
            band_points = bout(p)
            bands_out.append(bout)
            interb = interp1d(range(len(p)), band_points, kind='linear')
            if b < vb_top:
                color = 'r'
            else:
                color = 'b'
            if b == vb_top or b == vb_top + 1:
                bands.append(bout)
            plt.plot(x, interb(x), color=color)
            plt.plot(band_points, ls='', marker='x', color=color)
        for pos in tick_pos:
            plt.axvline(pos, color='k')
        plt.axhline(0, color='r', ls='--')
        plt.axhline(cb, color='b', ls='--')
        ax.set_xticks(tick_pos)
        ax.set_xticklabels(tick_txt)
        ax.set_xlim([0, len(p) - 1])

        return ax
コード例 #48
0
ファイル: planewave_io.py プロジェクト: SamKChang/qctoolkit
  def plot_band(self, path, dk=0.1, n_cb=2, n_vb=2, krange=1, ax=None):

    if not hasattr(self, 'kpoints_symmetrized'):
      qtk.warning('Make sure full kmesh is available. Brillouinize might help')

    p, ticks = self._kPath(path, dk)
    x = np.linspace(0, len(p)-1, 100)
    tick_pos, tick_txt = ticks[0], ticks[1]
    if ax is None:
      fig, ax = plt.subplots()
    bands = []
    bands_out = []
    occ = np.array(self.occupation)
    vb_top = np.where(occ < 1)[0][0]
    for b in range(vb_top - n_vb, vb_top + n_cb):
      bout, cb = self._interpolate(b, krange=krange)
      band_points = bout(p)
      bands_out.append(bout)
      interb = interp1d(range(len(p)), band_points, kind='linear')
      if b < vb_top:
        color = 'r'
      else:
        color = 'b'
      if b == vb_top or b == vb_top + 1:
        bands.append(bout)
      plt.plot(x,interb(x), color=color)
      plt.plot(band_points, ls='', marker='x', color=color)
    for pos in tick_pos:
      plt.axvline(pos, color='k')
    plt.axhline(0, color='r', ls='--')
    plt.axhline(cb, color='b', ls='--')
    ax.set_xticks(tick_pos)
    ax.set_xticklabels(tick_txt)
    ax.set_xlim([0, len(p)-1])

    return ax
コード例 #49
0
ファイル: qmInterface.py プロジェクト: SamKChang/qctoolkit
def QMOut(out=None, **kwargs):
    out_dict = {
        "cpmd": cpmd.out,
        "vasp": vasp.out,
        "abinit": abinit.out,
        "espresso": espresso.out,
        "nwchem": nwchem.out,
        "horton": horton.out,
        "gaussian": gaussian.out,
        "bigdft": bigdft.out,
    }

    if "unit" in kwargs:
        unit = kwargs["unit"]
    else:
        unit = "Ha"

    output = qtk.QMOutput()

    if out is not None and not os.path.exists(out):
        qtk.warning("%s not found" % out)
        # return qtk.QMOut()
    else:
        if "program" in kwargs:
            p_str = kwargs["program"]
            if "debug" in kwargs and kwargs["debug"]:
                # return out_dict[p_str](out).inUnit(unit)
                output = out_dict[p_str](out)
            else:
                try:
                    # return out_dict[p_str](out)
                    output = out_dict[p_str](out)
                except Exception as e:
                    qtk.warning("%s failed with message: %s" % (out, e))
                    # qout = qtk.QMOut()
                    output.path, output.name = os.path.split(out)
                    # return output.inUnit(unit)
        else:
            for p in out_dict.itervalues():
                try:
                    output = p(out)
                except:
                    pass
            qtk.warning("something wrong with output file, " + "pass 'program' eplicitly")
    return output.inUnit(unit)
コード例 #50
0
ファイル: cube.py プロジェクト: SamKChang/qctoolkit
  def ESP(self, coord=None, **kwargs):
    """
    method for electron density
    Note: CUBE file is assumed to be orthorohmbic
    """
    data = self.data
    grid = self.grid
    if 'molecule' not in kwargs:
      mol = self.molecule
    else:
      try:
        mol = copy.deepcopy(qtk.toMolecule(kwargs['molecule']))
      except:
        qtk.exit("error when loading molecule:%s" % \
                 str(kwargs['molecule']))
    N = mol.N

    Q = self.integrate()
    Z_sum = sum(mol.Z)
    ne_diff = abs(Q - Z_sum)
    ve_diff = abs(Q - mol.getValenceElectrons())
    if min(ne_diff, ve_diff) > 1E-2:
      qtk.warning("charge not conserved... ESP is wrong!")
      qtk.warning("charge integrate to %.2f, " % Q + \
                  "while nuclear charge adds to %.2f" % Z_sum)
    if ve_diff < ne_diff:
      Z = [qtk.n2ve(qtk.Z2n(z)) for z in mol.Z]
      Z = np.array(Z).reshape(N, 1)
    else:
      Z = mol.Z.reshape(N, 1)
    structure = np.hstack([Z, mol.R * 1.889725989])
    if coord is not None:
      x, y, z = np.array(coord) * 1.889725989
      V = ESP_c.esp_point(grid, structure, data, x, y, z)
      return V
    else:
      qtk.warning("known issue: unidentifed memory leak")
      out = copy.deepcopy(self)
      out.molecule = mol
      out.data = np.nan_to_num(
        ESP_cube_c.esp_cube(grid, structure, data)
      )
      return out
コード例 #51
0
ファイル: vasp.py プロジェクト: SamKChang/qctoolkit
  def __init__(self, qmoutXML, **kwargs):
    PlanewaveOutput.__init__(self, qmoutXML, **kwargs)
    if qmoutXML:
      #xml_file = open(qmoutXML)
      tree = ET.parse(qmoutXML)
      #xml_file.close()
      self.xml = tree.getroot()
      self.Et, self.unit = qtk.convE(float(self.xml[-2][-5][1].text),
                                     'eV-Eh', '-')
      self.info = self.xml[0][1].text
      self.SCFStep = len(self.xml[-2])-9

      try:
        # kpoints and band structure data
        kpoints = []
        band = []
        occ = []
  
        for i in range(len(self.xml[2][1])):
          k_str = self.xml[2][1][i].text
          weight = float(self.xml[2][2][i].text)
          band_k = []
          occ_k = []
          bk_xml = self.xml[-2][-3][0][5][0][i]
          for b_xml in bk_xml:
            b, o = [float(c) for c in b_xml.text.split()]
            band_k.append(b)
            occ_k.append(o)
          coord = [float(c) for c in k_str.split()]
          coord.append(weight)
          kpoints.append(coord)
          band.append(band_k)
          occ.append(occ_k)
        self.mo_eigenvalues = copy.deepcopy(band[0])
        self.kpoints = np.array(kpoints)
        self.band = np.array(band)
        self.occupation = occ[0]
  
        diff = np.diff(occ[0])
        pos = diff[np.where(abs(diff) > 0.5)]
        mask = np.in1d(diff, pos)
        ind = np.array(range(len(diff)))
        if len(ind[mask]) > 0:
          N_state = ind[mask][0]
          vb = max(self.band[:, N_state])
          cb = min(self.band[:, N_state + 1])
          vb_pos = np.argmax(self.band[:, N_state])
          cb_pos = np.argmin(self.band[:, N_state + 1])
          self.Eg = cb - vb
          if vb_pos == cb_pos:
            self.Eg_direct = True
          else:
            self.Eg_direct = False
  
        # DOS data
        dos = []
        self.E_fermi = float(self.xml[-2][-1][0].text) 
        print self.E_fermi
        for dos_xml in self.xml[-2][-1][1][0][5][0]:
          dos_lst = filter(None, dos_xml.text.split(' '))
          dos_E = [float(d) for d in dos_lst]
          dos.append(dos_E)
        self.dos = np.array(dos)

      except:
        qtk.warning("error when accessing kpoint data for %s"\
                    % self.name)
コード例 #52
0
ファイル: vasp.py プロジェクト: SamKChang/qctoolkit
  def write(self, name=None, **kwargs):
    self.setting.update(kwargs)
    self.setting['root_dir'] = name
    self.setting['no_molecule'] = False
    if name:
      self.setting['output'] = True
      name = os.path.splitext(name)[0]
    else: 
      self.setting['output'] = False
    incar, molecule = \
      super(PlanewaveInput, self).write('INCAR', **self.setting)
    self.setting['no_molecule'] = True
    kpoints = \
      super(PlanewaveInput, self).write('KPOINTS', **self.setting)
    poscar = \
      super(PlanewaveInput, self).write('POSCAR', **self.setting)
    potcar = \
      super(PlanewaveInput, self).write('POTCAR', **self.setting)

    # !!!!!!!!!!! TODO !!!!!!!!!!!
    # Center molecule
    # charge multiplicity
    # optimizer
    # 
    # write CPMD to modulize structure manipulation?

    PPPath = []
    n_list = []
    R_list = []

    def catPOTCAR(path):
      if os.path.exists(path):
        PPPath.append(path)
      else:
        qtk.exit("PP file: " + path + " not found")

    def getNlist(atom_number):
      n_list.append(atom_number)

    def getRlist(coord):
      R_list.append(coord)

    # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    # construct to POSCAR and POTCAR data
    molecule.sort()
    type_index = molecule.index
    type_list = molecule.type_list
    Z = molecule.Z

    self.pp_path = None
    if 'pp_path' not in self.setting:
      if 'pp_theory' not in self.setting:
        theory_dict = {
          'pbe': 'pbe',
          'pbe0': 'pbe',
          'hse06': 'pbe',
          'hse03': 'pbe',
          'lda': 'lda',
        }
        if self.setting['theory'] not in theory_dict:
          qtk.warning("%s is not supported, change theory to LDA" \
                      % (self.setting['theory']))
          self.setting['theory'] = 'lda'
        theory = theory_dict[self.setting['theory']]
        if theory.lower() not in ['pbe', 'lda']:
          qtk.warning('xc: %s is not supported, using LDA PP' % \
                      theory.upper())
          theory = 'LDA'
        self.pp_path = qtk.setting.vasp_pp + '_%s_%s' % \
                       (theory.upper(), 
                        self.setting['pp_type'].upper())
      else:
        self.pp_path = qtk.setting.vasp_pp + '_%s_%s' % \
                       (self.setting['pp_theory'].upper(), 
                        self.setting['pp_type'].upper())
    else:
      self.pp_path = self.setting['pp_path']

    for atom_type in xrange(0,len(type_index)-1):
      type_n = type_index[atom_type+1] - type_index[atom_type]
      # check special PP folder
        # not yet implemented
      # default PP path
      type_name = type_list[type_index[atom_type]]
      AtomPP = os.path.join(self.pp_path, type_name, 'POTCAR')
      catPOTCAR(AtomPP)
      getNlist(type_n)
      for I in\
        xrange(type_index[atom_type],type_index[atom_type+1]):
        getRlist(molecule.R[I][:])

    # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    # write to INCAR and generate POTCAR
    qtk.report("vasp.inp", "writing", "INCAR")
    incar.write("SYSTEM = %s\n" % self.setting['info'])
    incar.write("ISMEAR = 0\n")
    incar.write("IBRION = 2\n")
    if 'restart' in self.setting and self.setting['restart']:
      incar.write("ISTART = 1\n")
    if 'cutoff' in self.setting:
      cutoff = self.setting['cutoff']
      incar.write("ENCUT = %.2f" % (cutoff * 13.605698066))
      incar.write(" # in eV, that is %.1f Ry\n" % cutoff)
    if 'scf_step' in self.setting:
      incar.write('NELM = %d\n' % self.setting['scf_step'])
    if 'vdw' in self.setting:
      vdw = self.setting['vdw'].lower()
      if vdw != 'none':
        if vdw=='d2':
          incar.write("IVDW = 10\n")
        elif vdw=='d3':
          incar.write("IVDW = 11\n")
        elif vdw=='d3-bj':
          incar.write("IVDW = 12\n")
        elif vdw=='mbd':
          incar.write("IVDW = 202\n")
        elif vdw=='mbd_iter':
          incar.write("IVDW = 212\n")
        else:
          qtk.exit("VDW '%s' is not supported for VASP" % vdw)
    if 'ks_states' in self.setting:
      vs = int(round(self.molecule.getValenceElectrons() / 2.0))
      nbnd = vs + self.setting['ks_states']
      incar.write("NBANDS = %d\n" % nbnd)
    if 'full_kmesh' in self.setting and self.setting['full_kmesh']:
      incar.write("ISYM = -1\n")
    if self.setting['theory'] == 'pbe0':
      incar.write("LHFCALC = .TRUE.\n")
      incar.write("GGA = PE\n")
    elif self.setting['theory'] == 'hse06':
      incar.write("GGA = PE\n")
      incar.write("\n##HSE setting\n")
      incar.write("LHFCALC = .TRUE.\n")
      incar.write("HFSCREEN = 0.2 \n")
      incar.write("ALGO = D\n")

    if molecule.charge != 0:
      nve = molecule.getValenceElectrons()
      incar.write("NELECT = %d\n" % (nve))
    if 'save_density'not in self.setting\
    or not self.setting['save_density']:
      incar.write("LCHARG = .FALSE.\n")
    if 'scalapack' not in self.setting:
      incar.write("LSCALAPACK = .FALSE.\n")
    elif not self.setting['scalapack']:
      incar.write("LSCALAPACK = .FALSE.\n")
    incar.close()

    # !!!!!!!!!!!!!!!!
    # write to KPOINTS
    qtk.report("vasp.inp", "writing", "KPOINTS")
    if 'kmesh' not in self.setting:
      kpoints.write("Gamma-point only\n")
      kpoints.write(" 1       ! one k-point\n")
      kpoints.write("rec      ! in units of reciprocal vector\n")
      kpoints.write(" 0 0 0 1 ! coordinates and weight\n")
    else:
      k1, k2, k3 = self.setting['kmesh']
      kpoints.write("Automatic mesh\n")
      kpoints.write(" 0       ! number of k-points = 0")
      kpoints.write(" ->automatic generation scheme\n")
      kpoints.write("Gamma    ! generate a Gamma centered grid\n")
      kpoints.write(" %d %d %d   ! number of k grids\n" % (k1, k2, k3))
    kpoints.close(no_cleanup=True)

    # !!!!!!!!!!!!!!!
    # write to POSCAR
    qtk.report("vasp.inp", "writing", "POSCAR")
    poscar.write(self.setting['info'] + '\n')
    poscar.write("1.0\n")
    self.celldm2lattice()
    for i in range(3):
      for j in range(3):
        poscar.write(" %7.4f" % self.setting['lattice'][i,j])
      poscar.write(" ! lattic vector a(%d)\n" %i)
    for n in n_list:
      poscar.write(str(n) + ' ')
    poscar.write("! number of atoms in order of POTCAR\n")
    poscar.write("cart ! cartesian coordinates\n")
    for R in R_list:
      for X in R:
        poscar.write(" %7.4f" % X)
      poscar.write("\n")
    poscar.close(no_cleanup=True)

    # !!!!!!!!!!!!!!!
    # write to POTCAR
    qtk.report("vasp.inp", "writing", "POTCAR")
    for PP_file in PPPath:
      qtk.report("vasp.inp.POTCAR", PP_file)
      if name:
        with open(PP_file) as PP:
          for line in PP:
            potcar.write(str(line))
      else:
        potcar.write("cat %s\n" % PP_file)
    potcar.close(no_cleanup=True)

    return incar
コード例 #53
0
ファイル: abinit.py プロジェクト: SamKChang/qctoolkit
  def __init__(self, qmout, **kwargs):
    PlanewaveOutput.__init__(self, qmout, **kwargs)
    out_file = open(qmout)
    data = out_file.readlines()
    out_file.close()

    EStrList = filter(lambda x: 'Etotal' in x, data)
    EList = filter(lambda x: 'ETOT' in x, data)
    self.scf_step = len(EList)
    if self.scf_step > 0:
      Et_list = [float(filter(None, s.split(' '))[2]) for s in EList]
      self.Et = Et_list[-1]
    elif len(EStrList) > 0:
      EStr = EStrList[-1]
      self.Et = float(EStr.split(' ')[-1])

    if len(EStrList) > 0:
      EStr = EStrList[-1]
      detailInd = data.index(EStr)
      self.detail = data[detailInd-7:detailInd]
  
    xangst = filter(lambda x: 'xangst' in x, data)[-1]
    angst_n = len(data) - data[::-1].index(xangst) - 1
    xcart = filter(lambda x: 'xcart' in x, data)[-1]
    cart_n = len(data) - data[::-1].index(xcart) - 1
    Rstr = copy.deepcopy(data[angst_n:cart_n])
    Rstr[0] = Rstr[0].replace('xangst', '')
    R = [[float(r) for r in filter(None, s.split(' '))] for s in Rstr]
    N = len(R)
    ZstrOriginal = filter(lambda x: ' typat' in x, data)[-1]
    Zstr = ZstrOriginal.replace('typat', '')
    Zind = [int(z) for z in filter(None, Zstr.split(' '))]
    ZindItr = data.index(ZstrOriginal)
    while len(Zind) != N:
      ZindItr += 1
      ZindNewStr = filter(None, data[ZindItr].split(' '))
      ZindNew = [int(z) for z in ZindNewStr]
      Zind.extend(ZindNew)
    Znuc = filter(lambda x: 'znucl ' in x, data)[-1]
    Znuc = filter(None, Znuc.replace('znucl', '').split(' '))
    Znuc = [float(z) for z in Znuc]
    build = []
    for i in range(N):
      Z = [Znuc[Zind[i]-1]]
      Z.extend(R[i])
      build.append(Z)
    self.molecule = qtk.Molecule()
    self.molecule.build(build)

    if self.scf_step > 0:
      fStr = filter(lambda x: 'tesian forces (hartree/b' in x, data)[-1]
      fInd = data.index(fStr)
      fData = data[fInd+1:fInd+1+N]
      force = []
      for f in fData:
        fStr = filter(None, f.split(' '))[1:]
        force.append([float(fs) for fs in fStr])
      self.force = np.array(force)

    self.occupation = []
    try:
      r1p = re.compile(r'^[ a-z]{17} +[ 0-9.E+-]+$')
      r2p = re.compile(r'^ +[a-z]+ +.*$')
      report = filter(r2p.match, filter(r1p.match, data))
      occ_pattern = filter(lambda x: ' occ ' in x, report)[-1]
      occ_pattern_ind = len(report) - report[::-1].index(occ_pattern)
      occ_pattern_end = report[occ_pattern_ind]
      occ_ind_start = len(data) - data[::-1].index(occ_pattern) - 1
      occ_ind_end = len(data) - data[::-1].index(occ_pattern_end) - 1
      for i in range(occ_ind_start, occ_ind_end):
        for occ in filter(None, data[i].split(' ')):
          try:
            self.occupation.append(float(occ))
          except:
            pass
    except Exception as err:
      qtk.warning("error when extracting occupation number with" +\
        " error message: %s" % str(err))

    cell_pattern = re.compile(r'^ R.*=.* G.*=.*$')
    cell_list = filter(cell_pattern.match, data)
    cell = []
    for cstr in cell_list:
      cell.append(
        [float(c) for c in filter(None, cstr.split(' '))[1:4]])
    self.lattice = np.array(cell) / 1.889726124993
    self.celldm = qtk.lattice2celldm(self.lattice)
    self.molecule.R_scale = qtk.xyz2fractional(
      self.molecule.R, self.celldm)

    eigStr = os.path.join(os.path.split(qmout)[0], '*_EIG')
    eigFileList = glob.glob(eigStr)
    if len(eigFileList) != 0:
      if len(eigFileList) > 1:
        qtk.warning("more than one o_EIG files found")
      eigFile = open(eigFileList[0])
      eigData = eigFile.readlines()
      eigFile.close()
      spinList = filter(lambda x: 'SPIN' in x, eigData)
      if len(spinList) != 0:
        spinFactor = 2
        maxInd = eigData.index(spinList[-1])
      else:
        spinFactor = 1
        maxInd = len(eigData)
      ind = []
      for kStr in filter(lambda x: 'kpt#' in x, eigData):
        ind.append(eigData.index(kStr))
      band = []
      kpoints = []
      if spinFactor == 1:
        for i in range(len(ind)):
          wcoord = eigData[ind[i]].split('wtk=')[-1].split(', kpt=')
          weight = float(wcoord[0])
          cStr = filter(None, wcoord[1].split('(')[0].split(' '))
          coord = [float(c) for c in cStr]
          coord.append(weight)
          kpoints.append(coord)
          s = ind[i] + 1
          if i < len(ind) - 1:
            e = ind[i+1]
          else:
            e = len(eigData)
          eig_i = filter(None, ''.join(eigData[s:e]).split(' '))
          band.append([qtk.convE(float(ew), 'Eh-eV')[0]
                       for ew in eig_i])
  
        self.band = np.array(band)
        self.kpoints = np.array(kpoints)
        self.mo_eigenvalues = np.array(band[0]).copy()
        if len(self.occupation) > 0:
          diff = np.diff(self.occupation)
          ind = np.array(range(len(diff)))
          pos = diff[np.where(abs(diff) > 0.5)]
          mask = np.in1d(diff, pos)
          if len(ind[mask]) > 0:
            N_state = ind[mask][0]
            vb = max(self.band[:, N_state])
            cb = min(self.band[:, N_state + 1])
            vb_pos = np.argmax(self.band[:, N_state])
            cb_pos = np.argmin(self.band[:, N_state + 1])
            self.Eg = cb - vb
            if vb_pos == cb_pos:
              self.Eg_direct = True
            else:
              self.Eg_direct = False
  
      else:
        qtk.warning("spin polarized band data " +\
                    "extraction is not yet implemented")
    else:
      qtk.warning('no k-point information (o_EIG file) found')
コード例 #54
0
ファイル: espresso.py プロジェクト: SamKChang/qctoolkit
  def __init__(self, qmout, **kwargs):
    PlanewaveOutput.__init__(self, qmout, **kwargs)
    out_file = open(qmout)
    data = out_file.readlines()
    out_file.close()
    Et_pattern = re.compile("^.*total energy *=.*$")
    f_pattern = re.compile("^.*atom.*type.*force.*=.*$")
    Et_list = filter(Et_pattern.match, data)
    f_list = filter(f_pattern.match, data)
    if len(Et_list) > 0:
      Et_str = filter(Et_pattern.match, data)[-1]
      Et = float(Et_str.split()[-2])
      self.Et, self.unit = qtk.convE(Et, 'Ry-Eh')
      out_folder = os.path.split(os.path.abspath(qmout))[0]
      save = glob.glob(os.path.join(out_folder, '*.save'))

      # extract force information
      if len(f_list) > 0:
        fstr = [filter(None, fstr.split('=')[-1].split(' ')) 
                for fstr in f_list]
        # atomic unit force, HF/au, converted from Ry/au
        self.force = 0.5 * np.array(
          [[float(comp) for comp in atm] for atm in fstr]
        )

      # extract band structure from xml files
      if save:
        save = save[0]
        try:
          data_xml = os.path.join(save, 'data-file.xml')
          xml_file = open(data_xml)
          tree = ET.parse(xml_file)
          xml_file.close()
          self.xml = tree.getroot()
          kpoints = []
          band = []

          # extract celldm
          celldm = []
          cellVec = []
          for i in range(1, 4):
            cellvStr = filter(None, 
              self.xml[2][4][i].text.replace('\n', '').split(' ')
            )
            cellVec.append([float(v) for v in cellvStr])
          self.celldm = qtk.cellVec2celldm(cellVec)
    
          # extract structure
          R = []
          N = int(self.xml[3][0].text.replace('\n', ''))
          Nsp = int(self.xml[3][1].text.replace('\n', ''))
          for i in range(N):
            RiStr = self.xml[3][5+Nsp+i].get('tau')
            Ri = [float(r) * 0.529177249 for r in RiStr.split(' ')]
            ni = self.xml[3][5+Nsp+i].get('SPECIES')
            Z = [qtk.n2Z(ni)]
            Z.extend(Ri)
            R.append(Z)
          self.molecule = qtk.Molecule()
          self.molecule.build(R)

          # access data for each kpoint
          for k in self.xml[-2]:
            k_str = k[0].text
            coord = [float(c) for c in k_str.split()]
            weight = float(k[1].text.split()[0])
            coord.append(weight)
            kpoints.append(coord)
            ev_file = os.path.join(save, k[2].attrib['iotk_link'])
            k_xml_file = open(ev_file)
            k_xml = ET.parse(k_xml_file)
            k_xml_file.close()
            ev_k = k_xml.getroot()
            ev_str = ev_k[2].text.split()
            ev = [qtk.convE(float(entry), 'Eh-eV')[0]\
                  for entry in ev_str]
            band.append(ev)
            occ_str = ev_k[3].text.split()
            occ = [float(entry) for entry in occ_str]
          self.kpoints = np.array(kpoints)
          self.mo_eigenvalues = copy.deepcopy(band[0])
          self.band = np.array(band)
          self.occupation = occ
          diff = np.diff(occ)
          pos = diff[np.where(abs(diff) > 0.5)]
          mask = np.in1d(diff, pos)
          ind = np.array(range(len(diff)))
          if len(ind[mask]) > 0:
            N_state = ind[mask][0]
            vb = max(self.band[:, N_state])
            cb = min(self.band[:, N_state + 1])
            vb_pos = np.argmax(self.band[:, N_state])
            cb_pos = np.argmin(self.band[:, N_state + 1])
            self.Eg = cb - vb
            if vb_pos == cb_pos:
              self.Eg_direct = True
            else:
              self.Eg_direct = False

          cell = []
          for i in range(1, 4):
            vec = filter(
              None, 
              self.xml[2][4][i].text.replace('\n', '').split(' '))
            cell.append([float(v) for v in vec])
          self.lattice = np.array(cell) / 1.889726124993
          self.celldm = qtk.lattice2celldm(self.lattice)
          self.molecule.R_scale = qtk.xyz2fractional(
            self.molecule.R, self.celldm)
            
        except IOError:
          qtk.warning('xml file of job %s not found' % qmout)
    else:
      qtk.warning('job %s not finished' % qmout)
コード例 #55
0
ファイル: nwchem.py プロジェクト: SamKChang/qctoolkit
  def __init__(self, qmout=None, **kwargs):
    GaussianBasisOutput.__init__(self, qmout, **kwargs)
    if qmout:
      self.program = 'nwchem'
      outfile = open(qmout, 'r')
      data = outfile.readlines()
      outfile.close()
      Et = filter(lambda x: 'Total' in x and 'energy' in x, data)
      try:
        self.Et = float(Et[-1].split( )[-1])
      except:
        self.Et = np.nan
      try:
        _Et = filter(lambda x: 'total' in x and 'energy' in x, data)
        _Et = float(_Et[-1].split( )[-1])
        self.Et = _Et
      except:
        pass
      # necessary for opening *.movecs file
      n_basis = filter(lambda x: 'functions' in x, data)
      if ':' in n_basis[-1]:
        try:
          self.n_basis = int(n_basis[-1].split(':')[1])
        except:
          self.n_basis = np.nan
      elif '=' in n_basis[-1]:
        try:
          self.n_basis = int(n_basis[-1].split('=')[1])
        except:
          self.n_basis = np.nan

      nuclear = filter(lambda x: 'repulsion' in x, data)[-1]
      self.nuclear_repulsion = float(nuclear.split(' ')[-1])

      def getBasis():
        ######################################
        # extract basis function information #
        ######################################
      
        basis_dict = {"S":0, "P":1, "D":2, "F":3, "G":4, "H":5}
  
        basis_P = re.compile(r" *[0-9]+ [A-Z]  [0-9]\.[0-9]{8}")
        batom_P = re.compile(r"^  [0-9A-Za-z\-_\.]+ *\([A-Z][a-z]*\)")
        bname_P = re.compile(r"\((.*)\)")
        coord_P = re.compile(r"^ [0-9A-Za-z\.\-_]+ +[- ][0-9\.]{9,}")
        basisStr = filter(basis_P.match, data)
        batomStr = filter(batom_P.match, data)
        coordStr = filter(coord_P.match, data)
  
        # change 'Sulphur' to 'Sulfur' for NWChem format
        # 'Sulphur' 'Sulfur'
        def atomNameConv(old, new):
          _matched = filter(lambda x: old in x, batomStr)
          if _matched:
            _matched = _matched[0]
            _s = batomStr.index(_matched)
            batomStr[_s] = re.sub(old, new, batomStr[_s])
            _s = data.index(_matched)
            data[_s] = re.sub(old, new, data[_s])
        atomNameConv('Sulphur', 'Sulfur')
        atomNameConv('Aluminium', 'Aluminum')
  
        _exponents = [float(filter(None, s.split(' '))\
          [2]) for s in basisStr]
        _coefficients = [float(filter(None, s.split(' '))\
          [3]) for s in basisStr]
        _N = [int(filter(None, s.split(' '))[0])\
          for s in basisStr]
        _type = [filter(None, s.split(' '))[1]\
          for s in basisStr]
        _bfnInd = [data.index(batom) for batom in batomStr]
        _bfnEndPtn = re.compile(r" Summary of \"")
        _bfnEndStr = filter(_bfnEndPtn.match, data)[0]
        _bfnInd.append(data.index(_bfnEndStr))
  
        _ao_keys = [0]
        for ind in range(len(_bfnInd)-1):
          _key = _ao_keys[-1]
          for i in range(_bfnInd[ind]+4, _bfnInd[ind+1]):
            if len(data[i]) > 1:
              _key = _key + 1
          _ao_keys.append(_key)
        _atoms = [getattr(pt, bname_P.match(
          filter(None, s.split(' '))[1]).group(1).lower()).symbol\
          for s in batomStr]
        self.type_list = [re.split(r'[\._]',
          filter(None, s.split(' '))[0])[0].title()\
          for s in coordStr]
        self.type_list_unique = list(
          collections.OrderedDict.fromkeys(self.type_list)
        )
        self.R = np.array([filter(None, s.split(' '))[1:4]\
          for s in coordStr]).astype(float)
        self.N = len(self.R)
        self.Z = [qtk.n2Z(e) for e in self.type_list]
        self.R_bohr = 1.889725989 * self.R
        ZR = []
        for i in range(self.N):
          vec = [self.Z[i]]
          vec.extend(self.R[i])
          ZR.append(vec)
        self.molecule = qtk.Molecule()
        self.molecule.build(ZR)
  
        _N.append(0)
        self.basis = []
        for i in range(len(self.type_list)):
          e = self.type_list[i]
          center = self.R_bohr[i]
          ind = self.type_list_unique.index(e)
          bfn_base = {}
          bfn_base['atom'] = e
          bfn_base['center'] = center
          bfn_base['index'] = i
          exp = []
          cef = []
          for g in range(_ao_keys[ind], _ao_keys[ind+1]):
            exp.append(_exponents[g])
            cef.append(_coefficients[g])
            if _N[g] != _N[g+1] or g+1 >= _ao_keys[ind+1]:
              bfn = copy.deepcopy(bfn_base)
              bfn['exponents'] = copy.deepcopy(exp)
              bfn['coefficients'] = copy.deepcopy(cef)
              if _type[g] in basis_dict:
                _bfnList = self.basisList(basis_dict[_type[g]])
                for bStr in _bfnList:
                  bfn['type'] = _type[g].lower() + bStr
                  self.basis.append(copy.deepcopy(bfn))
              exp = []
              cef = []
  
      try:
        getBasis()
      except AttributeError as err:
        qtk.warning('failed to get basis information with error: %s.'\
                    % err + ' Weird atom names?')

      movecs = os.path.join(self.path, self.stem) + '.modat'
      if os.path.exists(movecs):
        self.getMO(movecs)
コード例 #56
0
ファイル: espresso.py プロジェクト: SamKChang/qctoolkit
def PPString(inp, mol, i, n, outFile):
  """
  append PP file names to inp.pp_files
  """
  alchemy = re.compile('^\w*2\w*_\d\d\d$')
  ppstr = re.sub('\*', '', mol.string[i])
  if ppstr:
    PPStr = ppstr
    pp_root, pp_ext = os.path.split(ppstr)
  else:
    if inp.setting['pp_type'] == 'geodecker':
      element = mol.type_list[i].title()
      if 'd_shell' in inp.setting:
        if type(inp.setting['d_shell']) is not list:
          inp.setting['d_shell'] = [inp.setting['d_shell']]
      if qtk.n2ve(mol.type_list[i].title()) > 10:
        shell = '-d'
      elif 'd_shell' in inp.setting \
      and element in inp.setting['d_shell']:
        shell = '-d'
      else:
        element = qtk.element[mol.type_list[i].title()]
        if element.group < 3 and mol.Z[i] > 1:
          if mol.Z[i] != 3:
            shell = '-sp'
          else:
            shell = '-s'
        else:
          shell = ''
      pp_xc_dict = {
        'lda': 'pz',
        'pbe0': 'pbe',
        'b3lyp': 'blyp',
      }
      pp_xc = inp.setting['pp_theory'].lower()
      if pp_xc in pp_xc_dict:
        pp_xc = pp_xc_dict[pp_xc]
      PPStr = ''.join([c for c in mol.type_list[i] if not c.isdigit()])\
              + '.' + pp_xc + shell + '-hgh.UPF'
    elif inp.setting['pp_type'] == 'cpmd':
      PPStr = PPName(inp, mol, i, n)
  xc = inp.setting['pp_theory'].lower()
  if not mol.string[i]:
    if inp.setting['pp_type'] == 'geodecker':
      PPCheck(pp_xc, mol.type_list[i].title(), PPStr)
    elif inp.setting['pp_type'] == 'cpmd':
      saved_pp = PPCheck_cpmd(pp_xc, mol.type_list[i].title(), PPStr)
      new_pp1 = saved_pp + '.UPF'
      conv_pp = sp.Popen("%s %s" % \
        (qtk.setting.espresso_cpmd2upf_exe, saved_pp),
        shell=True)
      conv_pp.wait()
      new_pp1_file = os.path.split(new_pp1)[1]
      new_pp1_trg = os.path.join(qtk.setting.espresso_pp, new_pp1_file)
      if not os.path.exists(new_pp1_trg):
        shutil.copy(new_pp1, qtk.setting.espresso_pp)
      PPStr = PPStr + '.UPF'

  elif alchemy.match(mol.string[i]):
    cpmd_pp = alchemyPP(xc, PPStr)
    new_pp1 = cpmd_pp + '.UPF'
    if not os.path.exists(new_pp1):
      qtk.report('espresso', "rewrite Goedecker's PP to UPF")
      conv_pp = sp.Popen("%s %s" % \
        (qtk.setting.espresso_cpmd2upf_exe, cpmd_pp),
        shell=True)
      conv_pp.wait()
      if conv_pp.returncode != 0:
        # dirty fix for espresso alchemy conversion routine
        qtk.warning('conversion failed..., trying path end points')
        root, _ = os.path.splitext(PPStr)
        element_str = re.sub('_.*', '', root)
        element1 = re.sub('2.*', '', element_str)
        element2 = re.sub('.*2', '', element_str)
        fraction = float(re.sub('.*_', '', root))/100
        if fraction == 0.0:
          strpp = element1 + "_q" + str(qtk.n2ve(element1)) +\
                  "_" + xc + '.psp'
        elif fraction == 1.0:
          strpp = element2 + "_q" + str(qtk.n2ve(element2)) +\
                  "_" + xc + '.psp'
        else:
          qtk.exit("PP conversion failed for intermediate lambda")
        strpp = os.path.join(qtk.setting.cpmd_pp, strpp)
        conv_pp = sp.Popen("%s %s" % \
          (qtk.setting.espresso_cpmd2upf_exe, strpp),
          shell=True)
        conv_pp.wait()
        os.rename(strpp + '.UPF', new_pp1)
    new_pp1_file = os.path.split(new_pp1)[1]
    new_pp1_trg = os.path.join(qtk.setting.espresso_pp, new_pp1_file)
    if not os.path.exists(new_pp1_trg):
      shutil.copy(new_pp1, qtk.setting.espresso_pp)
    PPStr = PPStr + '.UPF'

  return PPStr
コード例 #57
0
ファイル: cpmd.py プロジェクト: SamKChang/qctoolkit
def PPCheck(xc, element, pp_file_str, **kwargs):
  pp_file = None
  pp_content = None
  if xc == 'lda':
    xc = 'pade'
  elif xc == 'pbe0':
    xc = 'pbe'
  ne = qtk.n2ve(element)
  try:
    if 'dcacp' in kwargs and kwargs['dcacp']\
    and element in qtk.setting.dcscp_list:
      pp_path = os.path.join(xc.upper(), "%s_DCACP_%s" %\
                (element, xc.upper()))
      if element in qtk.setting.dcacp_dict:
        pp_path = pp_path + "_%s" % qtk.setting.dcacp_dict[element]
      #pp_file = os.path.join(qtk.setting.cpmd_dcacp_url, pp_path)
    else:
      pp_path = os.path.join(xc, 
        element + '-q' + str(qtk.n2ve(element)))
    pp_file = os.path.join(qtk.setting.cpmd_pp_url, pp_path)
    saved_pp_path = os.path.join(qtk.setting.cpmd_pp, pp_file_str)
    if not os.path.exists(saved_pp_path) and qtk.setting.download_pp:
      new_pp = os.path.join(qtk.setting.cpmd_pp, pp_file_str)

      if 'dcacp' in kwargs and kwargs['dcacp']\
      and element in qtk.setting.dcscp_list:
        root_list = filter(None, qtk.setting.cpmd_dcacp_url.split('/'))
        root = '//'.join(root_list[:2])
        url = qtk.setting.cpmd_dcacp_url
        html = ''.join(urllib2.urlopen(url).readlines())
        pp_links = BeautifulSoup(html).body.findAll(
          'a', attrs={'class': 'table'}
        )
        if kwargs['pp_type'].title() == 'Goedecker':
          pp_flag = r'/SG/'
        elif kwargs['pp_type'].upper() == 'MT':
          pp_flag = r'/MT/'
        pp_path = filter(lambda x: xc.upper() in x and pp_flag in x, 
          [l['href'] for l in pp_links if l.text == element.title()])
        pp_content = urllib2.urlopen(root + pp_path[0]).readlines()

      elif pp_file:
        pp_content = urllib2.urlopen(pp_file).readlines()
        pattern = re.compile(r'^.*</*pre>.*$')
        pp_se = filter(pattern.match, pp_content)
        pp_start = pp_content.index(pp_se[0])
        pp_end = pp_content.index(pp_se[1])
        pp_content = pp_content[pp_start:pp_end]
        pp_content[0] = pp_content[0].split('>')[-1]
      if pp_content:
        for i in range(len(pp_content)):
           pp_str = pp_content[i]
           pp_content[i] = pp_str.replace('&amp;', '&')
        qtk.report('PPCheck', 'pp file %s not found in %s, ' \
                   % (pp_file_str, qtk.setting.cpmd_pp) + \
                   'download now...')
        new_pp_file = open(new_pp, 'w')
        new_pp_file.write(''.join(pp_content))
        new_pp_file.close()
        pp_file = new_pp
    return saved_pp_path
  except Exception as e:
    qtk.warning('something wrong with pseudopotential with error: '+\
      str(e))
コード例 #58
0
ファイル: parallizer.py プロジェクト: SamKChang/qctoolkit
def parallelize(target_function, 
                input_list, 
                **kwargs):
  """
  target_function is implemented in a general way
    supposely any function would work
    But it could break down if target_function assumes some convoluted data structure
    
  input_list is a list of list. 
    Each input entry should be wrapped properly as a list 
    **kwargs can be passed py passing dictionary
    
  Example:
    # a toy target function
    def f(a, b, **kwargs):
      if 'factor' in kwargs:
        factor = kwargs['factor']
      else:
        factor = 1
      return a + b*factor
      
    input_list = [[i,j,{'factor':3}] for i in range(10) for j in range(10)]
    
    out_list = parallelize(f, input_list, block_size=10)
  """

  if 'threads' in kwargs:
    threads = kwargs['threads']
  else:
    threads = setting.cpu_count
  if 'block_size' in kwargs:
    block_size = kwargs['block_size']
  else:
    if len(input_list) > threads*3:
      block_size = len(input_list)/(threads*3)
    else:
      block_size = 1

  #############################################
  # runing target function of a single thread #
  #############################################
  def run_jobs(q_in, q_out):
    for inps in iter(q_in.get, None):
      ind = inps[-1]    # index of job
      inps = inps[:-1]  # actual input sequence
      out = []
      try:
        for args in inps:
          if type(args[-1]) == dict: # check known args input
            kwargs = args[-1]
            args = args[:-1]  
            out.append(target_function(*args, **kwargs))
          else:
            out.append(target_function(*args))
        q_out.put([out, ind]) # output result with index
      except: 
        qtk.warning('job failed!')
        q_out.put([np.nan, ind])
  ###### end of single thread definition ######

  # devide input_list into chunks according to block_size
  def chunks(_list, _size):
    for i in range(0, len(_list), _size):
      yield _list[i:i+_size]
  input_block = list(chunks(input_list, block_size))

  # setup empty queue
  output_stack = []
  output = []
  qinp = mp.Queue()
  qout = mp.Queue()

  # start process with empty queue
  jobs = []
  for thread in range(threads):
    p =  mp.Process(target=run_jobs, args=(qinp, qout))
    p.start()
    jobs.append(p)

  # put I/O data into queue for parallel processing
  index = range(len(input_block))
  for ind, inps in zip(index, input_block):
    inps.append(ind) # append inp index
    qinp.put(inps)   # put inp to input queue

  for thread in jobs:
    qinp.put(None)

  # while not queue.empty' is NOT reliable
  for i in range(len(input_block)):
    # collect output from each subprocess
    try:
      output_stack.append(qout.get())
    # check keyboard interrupt and terminate subprocess
    except KeyboardInterrupt:
      qtk.warning('jobs terminated by keyboard interrupt')
      for p in jobs:
        p.terminate()
      try:
        sys.exit(0)
      except SystemExit:
        os._exit(0)

  for thread in jobs:
    thread.join()

  # clean up queues
  while not qinp.empty():
    qinp.get()
  while not qout.empty():
    qout.get()

  if len(output_stack)>0:
    # sort/restructure output according to input order
    output_stack = sorted(output_stack, key=operator.itemgetter(1))
    # loop though all input for corresponding output
    for data_out in output_stack: 
      # if output is list of class, in-line iteration doesn't work
      output.append(data_out[0])
  return flatten(output)
コード例 #59
0
ファイル: submit.py プロジェクト: SamKChang/qctoolkit
def submit(inp_list, root, **remote_settings):
  necessary_list = [
    'ip',
    'submission_script',
  ]
  default_dict = {
    'username': None,
    'password': None,
    'flags': None,
    'timeout': 40,
  }
    
  for k, v in default_dict.iteritems():
    exec "%s = %s" % (k, v)
  if len(inp_list) * 5 > 40:
    timeout = len(inp_list) * 5
  if 'password' in remote_settings:
    password = remote_settings['password']
  if 'username' in remote_settings:
    username = remote_settings['username']
  if 'remote_path' not in remote_settings:
    remote_path = './%s' % root
  else:
    remote_path = remote_settings['remote_path']
  if 'timeout' in remote_settings:
    timeout = remote_settings['timeout']
  if 'prefix' in remote_settings:
    prefix = remote_settings['prefix']
  else:
    prefix = ''
  if 'flags' in remote_settings:
    flags = remote_settings['flags']
  if 'threads' not in remote_settings:
    threads = inp_list[0].setting['threads']
  else:
    threads = remote_settings['threads']
    if threads != inp_list[0].setting['threads']:
      qtk.report('submit', 'reset job threads to %d' % threads)
      for inp in inp_list:
        inp.setting['threads'] = threads
  if 'qthreads' not in remote_settings:
    qthreads = threads
  else:
    qthreads = remote_settings['qthreads']
  for s in necessary_list:
    if s not in remote_settings:
      qtk.exit('cluster setting:%s not defined' % s)
    else:
      exec "%s = '%s'" % (s, remote_settings[s])

  if type(inp_list) is not list:
    inp_list = [inp_list]
  program = inp_list[0].setting['program']

  if os.path.exists(root):
    if 'overwrite' in remote_settings \
    and remote_settings['overwrite']:
      qtk.warning("root directory %s exist, overwrite..." % root)
      shutil.rmtree(root)
      cwd = os.getcwd()
      os.makedirs(root)
      os.chdir(root)
      for inp in inp_list:
        inp.write(inp.molecule.name)
      os.chdir(cwd)
    else:
      qtk.warning("root directory %s exist, uploading existing folder"\
                  % root)
  else:
    cwd = os.getcwd()
    os.makedirs(root)
    os.chdir(root)
    for inp in inp_list:
      inp.write(inp.molecule.name)
    os.chdir(cwd)

  if 'compress' not in remote_settings:
    remote_settings['compress'] = False
    if len(inp_list) > 5:
      remote_settings['compress'] = True

  if remote_settings['compress']:
    qtk.report("submit", "compressing input files")
    cmd = 'tar -zcf %s %s' % (root + '.tar.gz', root)
    run = sp.Popen(cmd, shell=True, stdin=sp.PIPE)
    run.stdin.flush()
    run.communicate()
    run.wait()
    rootToSend = root + '.tar.gz'
    remote_dest = remote_path + '.tar.gz'
    qtk.report("submit", "compression completed")
  else:
    rootToSend = root
    remote_dest = remote_path

  paramiko_kwargs = {}
  if username:
    paramiko_kwargs['username'] = username
  if password:
    paramiko_kwargs['password'] = password
  ssh = paramiko.SSHClient()
  ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  ssh.load_system_host_keys()
  ssh.connect(ip, **paramiko_kwargs)
  ssh_stdin, ssh_stdout, ssh_stderr = \
    ssh.exec_command('ls %s' % remote_path)
  sshout = ssh_stdout.read()
  if len(sshout) > 0:
    if 'overwrite' in remote_settings \
    and remote_settings['overwrite']:
      status = 'remote path %s exists, overwrite...' % remote_path
      cmd = 'rm -r %s' % remote_path
      remoteRun(cmd, status, ssh)
    else:
      qtk.exit('remote path %s exists' % remote_path)

  ssh_newkey = 'Are you sure you want to continue connecting'
  patterns = [ssh_newkey, '[Pp]assword:', pexpect.EOF]
  if username:
    userStr = username + '@'
  else:
    userStr = ''

  cmd = 'scp -qr %s %s%s:%s' % (rootToSend, userStr, ip, remote_dest)
  qtk.report('submit', 'scp input files...')
  qtk.report('submit-remote_command', cmd)

  p = pexpect.spawn(cmd)
  i = p.expect(patterns, timeout=timeout)
  if i == 0:
    qtk.report('submit', 'adding %s to known_hosts' % ip)
    p.sendline('yes')
    i = p.expect(patterns, timeout=timeout)
  if i == 1:
    p.sendline(password)
    i = p.expect(patterns, timeout=timeout)
  if i == 2:
    if not p.before:
      qtk.report('submit', 'scp completed')
    else:
      qtk.warning('scp message: %s' % p.before)

  if remote_settings['compress']:
    status = "decompress remote input files"
    cmd = 'tar xf %s' % rootToSend
    remoteRun(cmd, status, ssh)
    status = "remove remote tar file"
    cmd = 'rm %s' % rootToSend
    remoteRun(cmd, status, ssh)
    qtk.report('submit', 'done')

  exe = qtk.setting.program_dict[program]
  cmd = "%s \"%s\" %s %d %d '%s' %s" % (submission_script, exe,
    remote_path, threads, qthreads, flags, prefix)
  status = 'submitting jobs...'
  remoteRun(cmd, status, ssh)
  ssh.exec_command("echo %s > %s/cmd.log" % (cmd, remote_path))
  qtk.report('submit', 'submission completed')

  ssh.close()

  if 'debug' in remote_settings and remote_settings['debug']:
    pass
  else:
    qtk.report('submit', 'cleanup local files')
    shutil.rmtree(root)
    if os.path.exists(root + '.tar.gz'):
      os.remove(root + '.tar.gz')
コード例 #60
0
ファイル: aljob.py プロジェクト: SamKChang/qctoolkit
def Al1st(qminp, **setting):
  assert 'ref_dir' in setting
  assert os.path.exists(setting['ref_dir'])

  if 'runjob' not in setting:
    setting['runjob'] = True

  qminp = copy.deepcopy(univ.toInp(qminp, **setting))

  name = qminp.molecule.name
  if 'out_dir' in setting:
    name = setting['out_dir']
    del setting['out_dir']
    qminp.molecule.name = name
    qminp.setting['root_dir'] = name

  if qminp.setting['program'] == 'cpmd':
    setting['restart'] = True
    setting['scf_step'] = 1
    rst = os.path.join(setting['ref_dir'], 'RESTART')
    assert os.path.exists(rst)
    rst = os.path.abspath(rst)
    if 'dependent_files' in setting:
      setting['dependent_files'].append(rst)
    else:
      setting['dependent_files'] = [rst]

  elif qminp.setting['program'] == 'abinit':
    setting['restart'] = True
    setting['scf_step'] = 0
    rstList = glob.glob(os.path.join(setting['ref_dir'], '*o_WFK'))
    assert len(rstList) > 0
    rstSrc = rstList[-1]
    rstTar = rstSrc.replace('o_WFK', 'i_WFK')
    rstTar = os.path.split(rstTar)[-1]
    if 'dependent_file' in setting:
      setting['dependent_files'].append([rstSrc, name+'i_WFK'])
    else:
      setting['dependent_files'] = [[rstSrc, name+'i_WFK']]

  elif qminp.setting['program'] == 'espresso':
    wfn = glob.glob(setting['ref_dir'] + '/*.wfc[0-9]*')
    if 'threads' not in setting or setting['threads'] != len(wfn):
      qtk.warning('threads must be the same as ref_dir, reset to %d'\
                  % len(wfn))
      setting['threads'] = len(wfn)
    setting['restart'] = True
    setting['scf_step'] = 1
    rst = glob.glob(setting['ref_dir'] + '/*.restart*')
    save = glob.glob(setting['ref_dir'] + '/*.save')
    if 'dependent_files' in setting:
      for lst in [wfn, rst, save]:
        setting['dependent_files'].extend(lst)
    else:
      setting['dependent_files'] = wfn
      for lst in [rst, save]:
        setting['dependent_files'].extend(lst)

  elif qminp.setting['program'] == 'vasp':
    setting['restart'] = True
    setting['scf_step'] = 1
    wfn_list = glob.glob(setting['ref_dir'] + '/WAVECAR')
    assert len(wfn_list) == 1
    wfn = wfn_list[0]
    if 'dependent_files' in setting:
      setting['dependent_files'].append(wfn)
    else:
      setting['dependent_files'] = [wfn]

  elif qminp.setting['program'] == 'bigdft':
    pass

  elif qminp.setting['program'] == 'nwchem':
    pass

  if setting['runjob']:
    qmout = qminp.run(name, **setting)
    return qmout
  else:
    qminp.molecule.name = name
    qminp.setting.update(setting)
    new_inp = qtk.QMInp(qminp.molecule, **qminp.setting)
    return new_inp