def readisordk(filename="AOONEINT"): """Read data under label ISORDK in AOONEINT""" header = readhead(filename) INT = header["int_fmt"] FLOAT = header["float_fmt"] aooneint = FortranBinary(filename) aooneint.find("ISORDK") aooneint.next() aooneint.next() sizeofi = struct.calcsize(INT) sizeofd = struct.calcsize(FLOAT) mxcent_ = (len(aooneint.data) - sizeofi) // (4 * sizeofd) chrn_ = aooneint.readbuf(mxcent_, FLOAT) nucdep = aooneint.readbuf(1, INT)[0] cooo_ = aooneint.readbuf(3 * mxcent_, FLOAT) isordk_ = { # "table":table1, "chrn": chrn_, "nucdep": nucdep, "cooo": cooo_, } aooneint.close() return isordk_
def test_1(self): """Read int, float integer, parameter :: n = 3 double precision x(n) x = (/ 1.0D0, 2.0D0, 3.0D0 /) open(1, file='fort.1', status='new', form='unformatted') write(1) n write(1) x close(1) end """ ffile = os.path.join(self.tdir, 'fort.1') fb = FortranBinary(ffile) # first record is int 3 next(fb) n = fb.readbuf(1, 'i')[0] self.assertEqual(n, 3) # first record is float 1. 2. 3. next(fb) xref = (1., 2., 3.) x = fb.readbuf(n, 'd') np.testing.assert_allclose(x, xref) fb.close()
def test_3b(self): """Read vecs integer, parameter :: nx = 3, ny=3 double precision x(nx), y(ny) x = (/ 1.0D0, 2.0D0, 3.0D0 /) y = (/ 5.0D0, 6.0D0, 7.0D0 /) open(3, file='fort.3', status='new', form='unformatted') write(3) nx, ny write(3) x write(3) y close(3) end """ ffile = os.path.join(self.tdir, 'fort.3') fb = FortranBinary(ffile) # first record is int 3 fb.next() x = [] for rec in fb: x += list(fb.readbuf(3, 'd')) xref = (1., 2., 3., 5., 6., 7.) np.testing.assert_allclose(x, xref) fb.close()
def test_3b(self): """Read vecs integer, parameter :: nx = 3, ny=3 double precision x(nx), y(ny) x = (/ 1.0D0, 2.0D0, 3.0D0 /) y = (/ 5.0D0, 6.0D0, 7.0D0 /) open(3, file='fort.3', status='new', form='unformatted') write(3) nx, ny write(3) x write(3) y close(3) end """ ffile = os.path.join(self.tdir, 'fort.3') fb = FortranBinary(ffile) # first record is int 3 fb.next() x=[] for rec in fb: x += list(fb.readbuf(3, 'd')) xref = (1., 2., 3., 5., 6., 7.) np.testing.assert_allclose(x, xref) fb.close()
def read(*args, **kwargs): """Read response vector given property""" if "freqs" in kwargs: # linear response/one frequency bfreqs = kwargs.get("freqs") else: # non-linear response/two frequencies bfreqs = kwargs.get("bfreqs", (0.0, )) cfreqs = kwargs.get("cfreqs", (0.0, )) propfile = kwargs.get("propfile", "RSPVEC") lr_vecs = kwargs.get("lr_vecs", False) rspvec = FortranBinary(propfile) vecs = {} for rec in rspvec: for lab in args: try: lab1 = bytes(lab.ljust(16), "utf-8") except TypeError: lab1 = bytes(lab.ljust(16)) # alternative label with permuted labels lab2 = lab1[8:] + lab1[:8] if lab1 in rec or lab2 in rec: if lab1 in rec: rec.read(16, "c") bfreq, cfreq = rec.read(2, "d") elif lab2 in rec: rec.read(16, "c") cfreq, bfreq = rec.read(2, "d") if bfreq in bfreqs and cfreq in cfreqs: _rec = next(rspvec) kzyvar = len(_rec) // 8 buffer_ = rspvec.readbuf(kzyvar, "d") vecs[(lab, bfreq, cfreq)] = \ np.array(buffer_).view(full.matrix) vecs[(lab1, bfreq, bfreq)] = vecs[(lab, bfreq, cfreq)] vecs[(lab2, cfreq, bfreq)] = vecs[(lab, bfreq, cfreq)] # now check that all required vectors are saved for _l in args: for b in bfreqs: for c in cfreqs: if (_l, b, c) not in vecs: raise RspVecError( "Response vector N(%s,%g,%g) not found on file %s" % (_l, b, c, propfile)) # complement dict with lr pointers if cfreqs == (0.0, ): vecs.update({(_l, b): vecs[(_l, b, 0.0)] for _l in args for b in bfreqs}) if bfreqs == (0.0, ): vecs.update({_l: vecs[(_l, 0.0, 0.0)] for _l in args}) if lr_vecs: keys = ((a, w) for a in args for w in bfreqs) return {k: vecs[k] for k in keys} else: return vecs
def readall(property_label, propfile="RSPVEC"): """Read response all vectors given property""" import numpy _rspvec = FortranBinary(propfile) found = [] while _rspvec.find(property_label) is not None: _rspvec.rec.read(16, "c") vfreq = _rspvec.rec.read(1, "d")[0] _rec = next(_rspvec) kzyvar = len(_rec) // 8 buffer_ = _rspvec.readbuf(kzyvar, "d") mat = numpy.array(buffer_).view(full.matrix) found.append((mat, vfreq)) return found
def readscfinp(filename="AOONEINT"): """Read data under labl SCFINP in AOONEINT""" header = readhead(filename) INT = header["int_fmt"] FLOAT = header["float_fmt"] aooneint = FortranBinary(filename) aooneint.find("SCFINP") import collections scfinp_ = collections.OrderedDict() aooneint.next() title = aooneint.data.decode() aooneint.next() scfinp_["ttitle"] = title nsym = aooneint.readbuf(1, INT)[0] scfinp_["nsym"] = nsym scfinp_["naos"] = aooneint.readbuf(nsym, INT) scfinp_["potnuc"] = aooneint.readbuf(1, FLOAT)[0] kmax = aooneint.readbuf(1, INT)[0] scfinp_["kmax"] = kmax scfinp_["ncent"] = aooneint.readbuf(kmax, INT) nbasis = aooneint.readbuf(1, INT)[0] scfinp_["nbasis"] = nbasis scfinp_["jtran"] = aooneint.readbuf(nbasis, INT) scfinp_["itran"] = aooneint.readbuf(8 * nbasis, INT) scfinp_["ctran"] = aooneint.readbuf(8 * nbasis, FLOAT) scfinp_["nbasis"] = aooneint.readbuf(1, INT)[0] scfinp_["inamn"] = aooneint.readbuf(nbasis, INT) scfinp_["iptyp"] = aooneint.readbuf(nbasis, INT) scfinp_["dpnuc"] = aooneint.readbuf(3, FLOAT) nucdep = aooneint.readbuf(1, INT)[0] scfinp_["nucdep"] = nucdep scfinp_["cooo"] = aooneint.readbuf(3 * nucdep, FLOAT) scfinp_["ifxyz"] = aooneint.readbuf(3, INT) scfinp_["dummy"] = aooneint.readbuf(1, FLOAT)[0] scfinp_["qpol"] = aooneint.readbuf(6, FLOAT) scfinp_["qq"] = aooneint.readbuf(3, FLOAT) scfinp_["jfxyz"] = aooneint.readbuf(3, INT) aooneint.close() return scfinp_
class SirIfc: """Read data from dalton interface file""" ifclabel = "SIR IPH " def __init__(self, name="SIRIFC"): self.name = name self.file = FortranBinary(name) self._cmo = None self._dv = None self._pv = None self._fock = None self._fc = None self._fv = None self._orbdiag = None if not self.file.find(self.ifclabel): raise RuntimeError("Label %s not found on %s" % (self.ifclabel, name)) rec = next(self.file) # Integer size from first record, 4 floats, 5 ints # 1) POTNUC,EMY,EACTIV,EMCSCF,ISTATE,ISPIN,NACTEL,LSYM,MS2 try: self.INT = get_intcode(len(rec), 4, 5) except KeyError: # pragma no cover # Allow Dalton 2013 version self.INT = get_intcode(len(rec), 4, 4) self.FLOAT = "d" self.potnuc, self.emy, self.eactive, self.emcscf = self.file.readbuf( 4, self.FLOAT) self.istate, self.ispin, self.nactel, self.lsym = \ self.file.readbuf(4, self.INT) self.file.next() self.nisht, self.nasht, self.nocct, self.norbt, self.nbast,\ self.nconf, self.nwopt, self.nwoph, self.ncdets,\ self.ncmot, self.nnashx, self.nnashy, self.nnorbt,\ self.n2orbt, self.nsym = \ self.file.readbuf(15, self.INT) self.muld2h = numpy.array(self.file.readbuf(64, self.INT)).reshape( (8, 8)) self.nrhf = numpy.array(self.file.readbuf(8, self.INT)) self.nfro = numpy.array(self.file.readbuf(8, self.INT)) self.nish = numpy.array(self.file.readbuf(8, self.INT)) self.nash = numpy.array(self.file.readbuf(8, self.INT)) self.norb = numpy.array(self.file.readbuf(8, self.INT)) self.nbas = numpy.array(self.file.readbuf(8, self.INT)) self.nelmn1, self.nelmx1, self.nelmn3, self.nelmx3, self.mctype = \ self.file.readbuf(5, self.INT) self.nas1 = numpy.array(self.file.readbuf(8, self.INT)) self.nas2 = numpy.array(self.file.readbuf(8, self.INT)) self.nas3 = numpy.array(self.file.readbuf(8, self.INT)) self.file.close() return @property def cmo(self): """Read MO coefficients""" if self._cmo is None: with FortranBinary(self.name) as fb: fb.find(self.ifclabel) for _ in range(3): fb.next() ncmot4 = max(self.ncmot, 4) dbl = fb.readbuf(ncmot4, self.FLOAT) n = 0 self._cmo = blocked.BlockDiagonalMatrix(self.nbas, self.norb) for isym in range(8): for mo in range(self.norb[isym]): for ao in range(self.nbas[isym]): self._cmo.subblock[isym][ao, mo] = dbl[n] n += 1 assert n == self.ncmot return self._cmo @property def dv(self): """Get active density matrix""" if self._dv is None: with FortranBinary(self.name) as fb: fb.find(self.ifclabel) for _ in range(5): fb.next() # mmashx = max(self.nnashx, 4) dbl = fb.readbuf(self.nnashx, self.FLOAT) self._dv = full.triangular.init(dbl) return self._dv @property def pv(self): """Get two-electron density""" if self._pv is None: with FortranBinary(self.name) as fb: fb.find(self.ifclabel) for i in range(7): fb.next() m2ashy = max(self.nnashx**2, 4) dbl = fb.readbuf(m2ashy, self.FLOAT) self._pv = full.matrix((self.nnashx, self.nnashx)) n = 0 for i in range(self.nnashx): for j in range(self.nnashx): self._pv[j, i] = dbl[n] n += 1 assert n == self.nnashx**2 return self._pv @property def fock(self): """Read Fock matrix (MO)""" if self._fock is None: with FortranBinary(self.name) as fb: fb.find(self.ifclabel) for i in range(6): fb.next() m2orbt = max(self.n2orbt, 4) dbl = fb.readbuf(m2orbt, self.FLOAT) self._fock = blocked.BlockDiagonalMatrix(self.norb, self.norb) n = 0 for isym in range(8): for i in range(self.norb[isym]): for j in range(self.norb[isym]): self._fock.subblock[isym][j, i] = dbl[n] n += 1 assert n == self.n2orbt return self._fock @property def fc(self): """Read inactive Fock matrix (MO)""" if self._fc is None: with FortranBinary(self.name) as fb: fb.find(self.ifclabel) for i in range(8): fb.next() mmorbt = max(self.nnorbt, 4) dbl = fb.readbuf(mmorbt, self.FLOAT) self._fc = blocked.triangular(self.norb) n = 0 for isym in range(8): ij = 0 for i in range(self.norb[isym]): for j in range(i + 1): self._fc.subblock[isym][i, j] = dbl[ij] ij += 1 n += ij assert n == self.nnorbt return self._fc @property def fv(self): """Get active Fock matrix""" if self._fv is None: with FortranBinary(self.name) as fb: fb.find(self.ifclabel) for i in range(9): fb.next() mmorbt = max(self.nnorbt, 4) dbl = fb.readbuf(mmorbt, self.FLOAT) self._fv = blocked.triangular(self.norb) n = 0 for isym in range(8): ij = 0 for i in range(self.norb[isym]): for j in range(i + 1): self._fv.subblock[isym][i, j] = dbl[ij] ij += 1 n += ij assert n == self.nnorbt return self._fv @property def orbdiag(self): """Get orbital Hessian diagonal""" if self._orbdiag is None: with FortranBinary(self.name) as fb: if fb.find("ORBDIAG"): rec = next(fb) self._orbdiag = rec.read(self.nwopt, self.FLOAT) else: raise LabelNotFound("ORBDIAG not found") return numpy.array(self._orbdiag) def __str__(self): retstr = "" retstr += "Nuclear Potential Energy: %12.6f\n" % self.potnuc retstr += "Electronic energy : %12.6f\n" % self.emy retstr += "Active energy : %12.6f\n" % self.eactive retstr += "MCSCF energy : %12.6f\n" % self.emcscf retstr += "State : %d\n" % self.istate retstr += "Spin : %d\n" % self.ispin retstr += "Active electrons : %d\n" % self.nactel retstr += "Symmetry : %d\n" % self.lsym retstr += "NISHT : %d\n" % self.nisht retstr += "NASHT : %d\n" % self.nasht retstr += "NOCCT : %d\n" % self.nocct retstr += "NORBT : %d\n" % self.norbt retstr += "NBAST : %d\n" % self.nbast retstr += "NCONF : %d\n" % self.nconf retstr += "NWOPT : %d\n" % self.nwopt retstr += "NWOPH : %d\n" % self.nwoph retstr += "NCDETS : %d\n" % self.ncdets retstr += "NCMOT : %d\n" % self.ncmot retstr += "NNASHX : %d\n" % self.nnashx retstr += "NNASHY : %d\n" % self.nnashy retstr += "NNORBT : %d\n" % self.nnorbt retstr += "N2ORBT : %d\n" % self.n2orbt retstr += "NSYM : %d\n" % self.nsym retstr += "MULD2H:\n" for i in range(8): retstr += " " for j in range(8): retstr += " %d" % self.muld2h[i, j] retstr += "\n" def strvec(lab, vec): """ String representation of vector""" locstr = lab + ":" for i in range(8): locstr += " %d" % vec[i] return locstr + "\n" retstr += strvec("NRHF", self.nrhf) retstr += strvec("NFRO", self.nfro) retstr += strvec("NISH", self.nish) retstr += strvec("NASH", self.nash) retstr += strvec("NORB", self.norb) retstr += strvec("NBAS", self.nbas) retstr += "NELMN1 : %d\n" % self.nelmn1 retstr += "NELMX1 : %d\n" % self.nelmx1 retstr += "NELMN3 : %d\n" % self.nelmn3 retstr += "NELMX3 : %d\n" % self.nelmx3 retstr += "MCTYPE : %d\n" % self.mctype retstr += strvec("NAS1", self.nas1) retstr += strvec("NAS2", self.nas2) retstr += strvec("NAS3", self.nas3) retstr += "CMO" + str(self.cmo) retstr += "DV\n" + str(self.dv) + "\n" retstr += "FOCK" + str(self.fock) retstr += "PV\n" + str(self.pv) + "\n" retstr += "FC" + str(self.fc) retstr += "FV" + str(self.fv) return retstr def xindx(self): from itertools import combinations ms2 = self.ispin - 1 na = (self.nactel + ms2) // 2 nb = (self.nactel - ms2) // 2 astrings = tuple(combinations(range(self.nasht)[::-1], na))[::-1] bstrings = tuple(combinations(range(self.nasht)[::-1], nb))[::-1] return ((adet[::-1], bdet[::-1]) for bdet in bstrings for adet in astrings)