def _open(self, filename, mode): import ase.io.ulm as ulm if mode not in 'aw': raise ValueError('mode must be "w" or "a".') if self.master: self.backend = ulm.open(filename, mode, tag='ASE-Trajectory') if len(self.backend) > 0: r = ulm.open(filename) self.numbers = r.numbers self.pbc = r.pbc else: self.backend = ulm.DummyWriter()
def test_ulm_copy(ulmfile): path = ulmfile.with_name('c.ulm') ulm.copy(ulmfile, path, exclude={'.a'}) with ulm.open(path) as r: assert 'a' not in r assert 'y' in r
def par_load(self, filename, name): import ase.io.ulm as ulm if world.rank == 0: r = ulm.open(filename, 'r') if name == 'v_TS': self.w_T = r.w_T self.rhoG0_S = r.rhoG0_S self.df_S = r.df_S A_XS = r.A_XS r.close() else: if name == 'v_TS': self.w_T = np.zeros((self.nS), dtype=float) self.rhoG0_S = np.zeros((self.nS), dtype=complex) self.df_S = np.zeros((self.nS), dtype=float) A_XS = None world.broadcast(self.rhoG0_S, 0) world.broadcast(self.df_S, 0) if name == 'H_SS': self.H_sS = self.distribute_A_SS(A_XS) if name == 'v_TS': world.broadcast(self.w_T, 0) self.v_St = self.distribute_A_SS(A_XS, transpose=True)
def test_ulm(ulmfile): with ulm.open(ulmfile) as r: assert r.y == 9 assert r.s == 'abc' assert (A.read(r.a).x == np.ones((2, 3))).all() assert (r.a.x == np.ones((2, 3))).all() assert r[1].s == 'abc2' assert r[2].s == 'abc3' assert (r[2].z == np.ones(7)).all()
def test_append(ulmfile): path = ulmfile.with_name('b.ulm') path.write_bytes(ulmfile.read_bytes()) with ulm.open(path, 'a') as w: assert w.nitems == 3 w.write(d={'h': [1, 'asdf']}) w.add_array('psi', (4, 2)) w.fill(np.ones((1, 2))) w.fill(np.ones((1, 2)) * 2) w.fill(np.ones((2, 2)) * 3) with ulm.open(path, 'r', 3) as r: assert r.d['h'] == [1, 'asdf'] with ulm.open(path) as r: assert (r[2].z == np.ones(7)).all() with ulm.open(path, index=3) as r: psi = r.proxy('psi')[0:3] assert (psi == [[1, 1], [2, 2], [3, 3]]).all()
def _open(self, filename, mode): import ase.io.ulm as ulm if mode not in 'aw': raise ValueError('mode must be "w" or "a".') if self.master: self.backend = ulm.open(filename, mode, tag='ASE-Trajectory') if len(self.backend) > 0 and mode == 'a': atoms = Trajectory(filename)[0] self.header_data = get_header_data(atoms) else: self.backend = ulm.DummyWriter()
def _open(self, filename): import ase.io.ulm as ulm try: self.backend = ulm.open(filename, 'r') except ulm.InvalidULMFileError: raise RuntimeError('This is not a valid ASE trajectory file. ' 'If this is an old-format (version <3.9) ' 'PickleTrajectory file you can convert it ' 'with ase.io.trajectory.convert("%s") ' 'or:\n\n $ python -m ase.io.trajectory %s' % (filename, filename)) self._read_header()
def ulmfile(tmp_path): path = tmp_path / 'a.ulm' with ulm.open(path, 'w') as w: w.write(a=A(), y=9) w.write(s='abc') w.sync() w.write(s='abc2') w.sync() w.write(s='abc3', z=np.ones(7, int)) return path
def run(args): if args.remove_wave_functions: import ase.io.ulm as ulm reader = ulm.open(args.gpw) if 'values' not in reader.wave_functions: print('No wave functions in', args.gpw) else: ulm.copy(reader, args.gpw + '.temp', exclude={'.wave_functions.values'}) reader.close() os.rename(args.gpw + '.temp', args.gpw) else: from gpaw import GPAW GPAW(args.gpw)
def par_save(self, filename, name, A_sS): import ase.io.ulm as ulm if world.size == 1: A_XS = A_sS else: A_XS = self.collect_A_SS(A_sS) if world.rank == 0: w = ulm.open(filename, 'w') if name == 'v_TS': w.write(w_T=self.w_T) # w.write(nS=self.nS) w.write(rhoG0_S=self.rhoG0_S) w.write(df_S=self.df_S) w.write(A_XS=A_XS) w.close() world.barrier()
def test_ulm(): import numpy as np import ase.io.ulm as ulm class A: def write(self, writer): writer.write(x=np.ones((2, 3))) @staticmethod def read(reader): a = A() a.x = reader.x return a w = ulm.open('a.ulm', 'w') w.write(a=A(), y=9) w.write(s='abc') w.sync() w.write(s='abc2') w.sync() w.write(s='abc3', z=np.ones(7, int)) w.close() print(w.data) r = ulm.open('a.ulm') print(r.y, r.s) print(A.read(r.a).x) print(r.a.x) print(r[1].s) print(r[2].s) print(r[2].z) with ulm.open('a.ulm', 'a') as w: print(w.nitems, w.offsets) w.write(d={'h': [1, 'asdf']}) w.add_array('psi', (4, 3)) w.fill(np.ones((1, 3))) w.fill(np.ones((1, 3)) * 2) w.fill(np.ones((2, 3)) * 3) print(ulm.open('a.ulm', 'r', 3).d) print(ulm.open('a.ulm')[2].z) print(ulm.open('a.ulm', index=3).proxy('psi')[0:3]) for d in ulm.open('a.ulm'): print(d)
def read_gpw(filename): try: reader = ulm.open(filename) except ulm.InvalidULMFileError: return read_old_gpw(filename) atoms = read_atoms(reader.atoms, _try_except=False) wfs = reader.wave_functions kpts = wfs.get('kpts') if kpts is None: ibzkpts = None bzkpts = None bz2ibz = None else: ibzkpts = kpts.ibzkpts bzkpts = kpts.get('bzkpts') bz2ibz = kpts.get('bz2ibz') if reader.version >= 3: efermi = reader.wave_functions.fermi_levels.mean() else: efermi = reader.occupations.fermilevel atoms.calc = SinglePointDFTCalculator(atoms, efermi=efermi, ibzkpts=ibzkpts, bzkpts=bzkpts, bz2ibz=bz2ibz, **reader.results.asdict()) if kpts is not None: atoms.calc.kpts = [] spin = 0 for eps_kn, f_kn in zip(wfs.eigenvalues, wfs.occupations): kpt = 0 for weight, eps_n, f_n in zip(kpts.weights, eps_kn, f_kn): atoms.calc.kpts.append( SinglePointKPoint(weight, spin, kpt, eps_n, f_n)) kpt += 1 spin += 1 return atoms
def _open(self, filename): import ase.io.ulm as ulm self.backend = ulm.open(filename, 'r') self._read_header()
def ibz2bz(input_gpw, output_gpw=None): """Unfold IBZ to full BZ and write new gpw-file. Example:: ibz2bz('abc.gpw') This will create a 'abc-bz.gpw' file. Works also from the command line:: $ python3 -m gpaw.utilities.ibz2bz abc.gpw """ if world.rank > 0: return calc = GPAW(input_gpw, txt=None) spos_ac = calc.atoms.get_scaled_positions() kd = calc.wfs.kd nK = kd.N_c.prod() nbands = calc.wfs.bd.nbands ns = calc.wfs.nspins I_a = None for K, k in enumerate(kd.bz2ibz_k): a_a, U_aii, time_rev = construct_symmetry_operators(calc.wfs, spos_ac, K) if I_a is None: # First time: I1 = 0 I_a = [0] for U_ii in U_aii: I2 = I1 + len(U_ii) I_a.append(I2) I1 = I2 # Allocate arrays: P_sKnI = np.empty((ns, nK, nbands, I2), calc.wfs.dtype) e_sKn = np.empty((ns, nK, nbands)) f_sKn = np.empty((ns, nK, nbands)) for s in range(ns): e_sKn[s, K] = calc.get_eigenvalues(k, s) f_sKn[s, K] = calc.get_occupation_numbers(k, s) P_nI = calc.wfs.collect_projections(k, s) P2_nI = P_sKnI[s, K] a = 0 for b, U_ii in zip(a_a, U_aii): P_ni = np.dot(P_nI[:, I_a[b]:I_a[b + 1]], U_ii) if time_rev: P_ni = P_ni.conj() P2_nI[:, I_a[a]:I_a[a + 1]] = P_ni a += 1 # Write new gpw-file: parameters = calc.todict() parameters['symmetry'] = 'off' if output_gpw is None: assert input_gpw.endswith('.gpw') output_gpw = input_gpw[:-4] + '-bz.gpw' out = ulm.open(output_gpw, 'w') ulm.copy(input_gpw, out, exclude={'.wave_functions', '.parameters'}) out.child('parameters').write(**parameters) wfs = out.child('wave_functions') wfs.write(eigenvalues=e_sKn, occupations=f_sKn, projections=P_sKnI) wfs.child('kpts').write(bz2ibz=np.arange(nK), bzkpts=kd.bzk_kc, ibzkpts=kd.bzk_kc, weights=np.ones(nK) / nK) out.close()
import numpy as np from ase.io.ulm import open class A: def write(self, writer): writer.write(x=np.ones((2, 3))) @staticmethod def read(reader): a = A() a.x = reader.x return a w = open('a.ulm', 'w') w.write(a=A(), y=9) w.write(s='abc') w.sync() w.write(s='abc2') w.sync() w.write(s='abc3', z=np.ones(7, int)) w.close() print(w.data) r = open('a.ulm') print(r.y, r.s) print(A.read(r.a).x) print(r.a.x) print(r[1].s) print(r[2].s)
def test_open_anypathlike(): # File does not exist, but we still want to test that it is correctly # converted into a (nonexistent) path with pytest.raises(FileNotFoundError): ulm.open(MyFile())
import numpy as np import ase.io.ulm as ulm class A: def write(self, writer): writer.write(x=np.ones((2, 3))) @staticmethod def read(reader): a = A() a.x = reader.x return a w = ulm.open('a.ulm', 'w') w.write(a=A(), y=9) w.write(s='abc') w.sync() w.write(s='abc2') w.sync() w.write(s='abc3', z=np.ones(7, int)) w.close() print(w.data) r = ulm.open('a.ulm') print(r.y, r.s) print(A.read(r.a).x) print(r.a.x) print(r[1].s) print(r[2].s)
import numpy as np from ase.io.ulm import ulmopen as open class A: def write(self, writer): writer.write(x=np.ones((2, 3))) @staticmethod def read(reader): a = A() a.x = reader.x return a w = open('a.ulm', 'w') w.write(a=A(), y=9) w.write(s='abc') w.sync() w.write(s='abc2') w.sync() w.write(s='abc3', z=np.ones(7, int)) w.close() print(w.data) r = open('a.ulm') print(r.y, r.s) print(A.read(r.a).x) print(r.a.x) print(r[1].s) print(r[2].s) print(r[2].z)