def test_molecule(): Mol = Molecule(name="Test Mol", steps=0) assert Mol.name == "Test Mol" assert len(Mol) == 0 Mol.newStep() assert len(Mol) == 1 Mol.newAtom('C', (1, 1, 1)) assert Mol.nat == 1 assert atom_equal(Mol.getAtom(0, fmt='crystal'), ['C', (1, 1, 1)]) Mol.copyStep() assert len(Mol) == 2 for s in Mol.steps: assert s.nat == 1 assert s.getCellDim() == 1 assert Mol.curStep == 1 Mol.changeStep(0) assert Mol.curStep == 0 Mol.setCellDimAll(1, fmt='angstrom', scale=True) for s in Mol.steps: assert float_equal(s.getCellDim(fmt='angstrom'), 1) assert vec_equal(s.getVec(), ((1, 0, 0), (0, 1, 0), (0, 0, 1))) assert atom_equal(s.getAtom(0, fmt='angstrom'), ['C', (1, 1, 1)]) Mol.setVecAll(((2, 0, 0), (0, 2, 0), (0, 0, 2)), scale=True) for s in Mol.steps: assert float_equal(s.getCellDim(fmt='angstrom'), 1) assert vec_equal(s.getVec(), ((2, 0, 0), (0, 2, 0), (0, 0, 2))) assert atom_equal(s.getAtom(0, fmt='angstrom'), ['C', (2, 2, 2)]) Mol.setVecAll(((1, 0, 0), (0, 1, 0), (0, 0, 1))) for s in Mol.steps: assert float_equal(s.getCellDim(fmt='angstrom'), 1) assert vec_equal(s.getVec(), ((1, 0, 0), (0, 1, 0), (0, 0, 1))) assert atom_equal(s.getAtom(0, fmt='crystal'), ['C', (2, 2, 2)]) assert Mol.getKpoints('active') == 'gamma' assert Mol.getKpoints('mpg') == ['1', '1', '1', '0', '0', '0'] assert Mol.getKpoints('discrete') == [] assert Mol.getKpoints('options') == {'crystal': False, 'bands': False} Mol.setKpoints('active', 'mpg') assert Mol.getKpoints('active') == 'mpg' Mol1 = Mol.copy() assert len(Mol1) == 1 assert Mol1.name == "Copy of Test Mol" Mol2 = Mol.copyAll() assert len(Mol2) == 2 assert Mol2.name == "Copy of Test Mol"
def test_pwi_write_kpoints(): body = "&control\n"\ "/\n"\ "\n"\ "&system\n"\ " nat=2\n"\ " ntyp=1\n"\ " celldm(1)=2.0\n"\ " ibrav=0\n"\ " ecutwfc=30.0\n"\ "/\n"\ "\n"\ "&electrons\n"\ "/\n"\ "\n"\ "ATOMIC_SPECIES\n"\ "C 12.0107 C.uspp736.pbe.UPF\n"\ "\n"\ "ATOMIC_POSITIONS alat\n"\ "C 0.00000 0.00000 0.00000 1 0 1\n"\ "C 0.50000 7.50000 7.50000\n"\ "\n" tail = "\n"\ "CELL_PARAMETERS\n"\ " 1.00000 2.00000 0.00000\n"\ " 0.00000 5.00000 6.00000\n"\ " 0.00000 8.00000 9.00000\n" Param = pwInput.param['default'].copy() Mol = Molecule() Mol.setFmt('alat') Mol.setCellDim(2) Mol.setVec(((1, 2, 0), (0, 5, 6), (0, 8, 9))) Mol.newAtom('C', (0, 0, 0), fix=[False, True]) Mol.newAtom('C', [0.5, 0.5, 0.5], fmt='crystal') def assertKpoint(): f = StringIO() pwInput.writer(Mol, f, Param) assert f.getvalue() == target Mol.setKpoints('active', 'gamma') target = body + "K_POINTS gamma\n" + tail assertKpoint() Mol.setKpoints('active', 'mpg') Mol.setKpoints('mpg', ('2', '0', '0', '0', '0', '0')) target = body +\ "K_POINTS automatic\n2 0 0 0 0 0 \n" +\ tail assertKpoint() Mol.setKpoints('active', 'discrete') Mol.setKpoints('discrete', [['0.0', '0.0', '0.0', '0.75'], ['0.5', '0.5', '0.5', '0.25']]) Mol.setKpoints('options', {'crystal': False, 'bands': False}) target = body +\ "K_POINTS tpiba\n2\n0.0 0.0 0.0 0.75\n0.5 0.5 0.5 0.25\n" +\ tail assertKpoint() Mol.setKpoints('options', {'crystal': False, 'bands': True}) target = body +\ "K_POINTS tpiba_b\n2\n0.0 0.0 0.0 0.75\n0.5 0.5 0.5 0.25\n" +\ tail assertKpoint() Mol.setKpoints('options', {'crystal': True, 'bands': False}) target = body +\ "K_POINTS crystal\n2\n0.0 0.0 0.0 0.75\n0.5 0.5 0.5 0.25\n" +\ tail assertKpoint() Mol.setKpoints('options', {'crystal': True, 'bands': True}) target = body +\ "K_POINTS crystal_b\n2\n0.0 0.0 0.0 0.75\n0.5 0.5 0.5 0.25\n" +\ tail assertKpoint()
def test_cpmd_write_kpoints_constraints(): Mol = Molecule() Mol.newAtom('Na', (0, 0, 0), fix=[True]) Mol.newAtom('Na', (1, 1, 1), fix=[True, True, True]) p = newParam('cpmd') p["&ATOMS"] = "CONSTRAINTS\n"\ "FIX COM\n"\ "END CONSTRAINTS\n" head = "&CPMD\n"\ "&END\n"\ "&SYSTEM\n"\ " CELL VECTORS\n"\ " 1.0000 0.0000 0.0000\n"\ " 0.0000 1.0000 0.0000\n"\ " 0.0000 0.0000 1.0000\n" tail = "&END\n"\ "&ATOMS\n"\ "*Na.uspp736.pbe BINARY\n"\ " LMAX=F\n"\ " 2\n"\ " 0.0000 0.0000 0.0000\n"\ " 1.0000 1.0000 1.0000\n"\ "CONSTRAINTS\n"\ "FIX ATOMS\n"\ "1\n"\ " 2\n"\ "FIX COORDINATES\n"\ "1\n"\ "1 0 1 1\n"\ "\n"\ "FIX COM\n"\ "END CONSTRAINTS\n"\ "ISOTOPE\n"\ "22.99\n"\ "&END\n" def assertKpoint(): f = StringIO() cpmd.writer(Mol, f, p) f = f.getvalue() assert f == target Mol.setKpoints('active', 'gamma') target = head + tail assertKpoint() Mol.setKpoints('active', 'mpg') Mol.setKpoints('mpg', ('2', '0', '0', '0', '0', '0')) target = head +\ " KPOINTS MONKHORST-PACK\n 2 0 0 \n" +\ tail assertKpoint() Mol.setKpoints('mpg', ('2', '0', '0', '0', '0', '0.5')) target = head +\ " KPOINTS MONKHORST-PACK SHIFT=0 0 0.5 \n" +\ " 2 0 0 \n" +\ tail assertKpoint() Mol.setKpoints('active', 'discrete') Mol.setKpoints('discrete', [['0.0', '0.0', '0.0', '0.75'], ['0.5', '0.5', '0.5', '0.25']]) Mol.setKpoints('options', {'crystal': False, 'bands': False}) target = head +\ " KPOINTS\n 2\n 0.0 0.0 0.0 0.75\n 0.5 0.5 0.5 0.25\n" +\ tail assertKpoint() Mol.setKpoints('options', {'crystal': True, 'bands': False}) target = head +\ " KPOINTS SCALED\n 2\n 0.0 0.0 0.0 0.75\n"\ " 0.5 0.5 0.5 0.25\n" +\ tail assertKpoint() Mol.setKpoints('discrete', [['0.0', '0.0', '0.0', '2'], ['0.5', '0.5', '0.5', '0']]) Mol.setKpoints('options', {'crystal': False, 'bands': True}) target = head +\ " KPOINTS BANDS\n 2 0.0 0.0 0.0 0.5 0.5 0.5 \n"\ " 0 0. 0. 0. 0. 0. 0.\n" +\ tail assertKpoint() Mol.setKpoints('options', {'crystal': True, 'bands': True}) target = head +\ " KPOINTS SCALED BANDS\n 2 0.0 0.0 0.0 0.5 0.5 0.5 \n"\ " 0 0. 0. 0. 0. 0. 0.\n" +\ tail assertKpoint() Mol.newAtom('Na', (2, 1, 1), fix=[True, True, True]) Mol.newAtom('Na', (3, 1, 1), fix=[True, True, True]) Mol.newAtom('Na', (4, 1, 1), fix=[True, True, True]) Mol.newAtom('Na', (5, 1, 1), fix=[True, True, True]) Mol.newAtom('Na', (6, 1, 1), fix=[True, True, True]) Mol.newAtom('Na', (7, 1, 1), fix=[True, True, True]) Mol.newAtom('Na', (8, 1, 1), fix=[True, True, True]) Mol.newAtom('Na', (9, 1, 1), fix=[True, True, True]) Mol.newAtom('Na', (1, 2, 1), fix=[True, True, True]) Mol.newAtom('Na', (1, 3, 1), fix=[True, True, True]) Mol.setKpoints('active', 'gamma') tail2 = "&END\n"\ "&ATOMS\n"\ "*Na.uspp736.pbe BINARY\n"\ " LMAX=F\n"\ " 12\n"\ " 0.0000 0.0000 0.0000\n"\ " 1.0000 1.0000 1.0000\n"\ " 2.0000 1.0000 1.0000\n"\ " 3.0000 1.0000 1.0000\n"\ " 4.0000 1.0000 1.0000\n"\ " 5.0000 1.0000 1.0000\n"\ " 6.0000 1.0000 1.0000\n"\ " 7.0000 1.0000 1.0000\n"\ " 8.0000 1.0000 1.0000\n"\ " 9.0000 1.0000 1.0000\n"\ " 1.0000 2.0000 1.0000\n"\ " 1.0000 3.0000 1.0000\n"\ "CONSTRAINTS\n"\ "FIX ATOMS\n"\ "11\n"\ " 2 3 4 5 6 7 8 9 10 11\n"\ " 12\n"\ "FIX COORDINATES\n"\ "1\n"\ "1 0 1 1\n"\ "END CONSTRAINTS\n"\ "ISOTOPE\n"\ "22.99\n"\ "&END\n" target = head + tail2 f = StringIO() cpmd.writer(Mol, f, newParam('cpmd')) for i in range(len(target.split('\n'))): print(f.getvalue().split('\n')[i], target.split('\n')[i]) assert f.getvalue().split('\n')[i] == target.split('\n')[i] assert f.getvalue() == target
def test_pwi_write_calc(): calcs = ["'scf'", "'relax'", "'vc-relax'"] adnl = ["", "&ions\n ion_dynamics='bfgs'\n/\n\n", "&ions\n ion_dynamics='bfgs'\n/\n\n" "&cell\n cell_dynamics='bfgs'\n/\n\n"] head = "&control\n calculation=" body = "\n/\n\n"\ "&system\n"\ " nat=2\n"\ " ntyp=1\n"\ " celldm(1)=2.0\n"\ " ibrav=0\n"\ " ecutwfc=30.0\n"\ "/\n"\ "\n"\ "&electrons\n"\ " diagonalization='david'\n"\ " conv_thr=1.0e-8\n"\ "/\n"\ "\n" tail = "ATOMIC_SPECIES\n"\ "C 12.0107 C.uspp736.pbe.UPF\n"\ "\n"\ "ATOMIC_POSITIONS crystal\n"\ "C 0.00000 0.00000 0.00000 1 0 1\n"\ "C 0.50000 0.50000 0.50000\n"\ "\n"\ "K_POINTS automatic\n"\ "2 0 0 0 0 0 \n"\ "\n"\ "CELL_PARAMETERS\n"\ " 1.00000 2.00000 0.00000\n"\ " 0.00000 5.00000 6.00000\n"\ " 0.00000 8.00000 9.00000\n" Mol = Molecule() Mol.setFmt('crystal') Mol.setCellDim(2) Mol.setVec(((1, 2, 0), (0, 5, 6), (0, 8, 9))) Mol.newAtom('C', (0, 0, 0), fix=[False, True]) Mol.newAtom('C', [0.5, 0.5, 0.5], fmt='crystal') Mol.setKpoints('active', 'mpg') Mol.setKpoints('mpg', ('2', '0', '0', '0', '0', '0')) def failWrite(p): try: pwInput.writer(Mol, f, p) except KeyError: return True else: return False for c in range(3): for a in range(3): Param = newParam('pwi') Param['&control']['calculation'] = calcs[c] Param['&electrons']['diagonalization'] = "'david'" Param['&electrons']['conv_thr'] = "1.0e-8" if a > 0: Param['&ions'] = {'ion_dynamics': "'bfgs'"} if a > 1: Param['&cell'] = {'cell_dynamics': "'bfgs'"} f = StringIO() if a < c: failWrite(Param) else: pwInput.writer(Mol, f, Param) target = head + calcs[c] + body + adnl[a] + tail assert f.getvalue() == target # default to scf when nothing present: Param = newParam('pwi') Param['&electrons']['diagonalization'] = "'david'" Param['&electrons']['conv_thr'] = "1.0e-8" f = StringIO() pwInput.writer(Mol, f, Param) target = "&control" + body + tail assert f.getvalue() == target