Skip to content

Commit 9a92d01

Browse files
committed
Add add_atom method to PosCar.
1 parent 44c34e0 commit 9a92d01

File tree

2 files changed

+58
-4
lines changed

2 files changed

+58
-4
lines changed

tests/poscar_test.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ class PosCarTest(unittest.TestCase):
1313

1414
def setUp(self):
1515
self.maxDiff = True
16+
self.filename = path + "/POSCAR"
1617

1718
def test_get_poscar_content(self):
1819
" Make sure we can get the correct poscar content. "
19-
filename = path + "/POSCAR"
20-
poscar = PosCar(filename)
20+
poscar = PosCar(self.filename)
2121

2222
ref_content = """Created by VASPy\n 1.000000000\n 7.29321435 -4.21073927 0.00000000\n 0.00000000 8.42147853 0.00000000\n -0.00000000 0.00000000 16.87610843\nPt \n 36\nSelective Dynamics\nDirect\n 0.244666665792 0.223999996980 0.135815443038F F F \n 0.022444443570 0.112888885869 0.271630886077T T T \n 0.133555554681 0.001777774758 0.000000000000F F F \n 0.133555554681 0.001777774758 0.407446329115T T T \n 0.577999999126 0.223999996980 0.135815443038F F F \n 0.355777776904 0.112888885869 0.271630886077T T T \n 0.466888888015 0.001777774758 0.000000000000F F F \n 0.466888888015 0.001777774758 0.407446329115T T T \n 0.911333332459 0.223999996980 0.135815443038F F F \n 0.689111110237 0.112888885869 0.271630886077T T T \n 0.800222221348 0.001777774758 0.000000000000F F F \n 0.800222221348 0.001777774758 0.407446329115T T T \n 0.244666665792 0.557333330313 0.135815443038F F F \n 0.022444443570 0.446222219202 0.271630886077T T T \n 0.133555554681 0.335111108091 0.000000000000F F F \n 0.133555554681 0.335111108091 0.407446329115T T T \n 0.577999999126 0.557333330313 0.135815443038F F F \n 0.355777776904 0.446222219202 0.271630886077T T T \n 0.466888888015 0.335111108091 0.000000000000F F F \n 0.466888888015 0.335111108091 0.407446329115T T T \n 0.911333332459 0.557333330313 0.135815443038F F F \n 0.689111110237 0.446222219202 0.271630886077T T T \n 0.800222221348 0.335111108091 0.000000000000F F F \n 0.800222221348 0.335111108091 0.407446329115T T T \n 0.244666665792 0.890666663647 0.135815443038F F F \n 0.022444443570 0.779555552536 0.271630886077T T T \n 0.133555554681 0.668444441424 0.000000000000F F F \n 0.133555554681 0.668444441424 0.407446329115T T T \n 0.577999999126 0.890666663647 0.135815443038F F F \n 0.355777776904 0.779555552536 0.271630886077T T T \n 0.466888888015 0.668444441424 0.000000000000F F F \n 0.466888888015 0.668444441424 0.407446329115T T T \n 0.911333332459 0.890666663647 0.135815443038F F F \n 0.689111110237 0.779555552536 0.271630886077T T T \n 0.800222221348 0.668444441424 0.000000000000F F F \n 0.800222221348 0.668444441424 0.407446329115T T T \n"""
2323

@@ -27,15 +27,29 @@ def test_get_poscar_content(self):
2727

2828
def test_get_xyz_content(self):
2929
" Make sure we can get correct xyz file content from poscar. "
30-
filename = path + "/POSCAR"
31-
poscar = PosCar(filename)
30+
poscar = PosCar(self.filename)
3231

3332
ref_content = """ 36\nSTEP = 1\nPt 0.244666665792 0.22399999698 0.135815443038\nPt 0.02244444357 0.112888885869 0.271630886077\nPt 0.133555554681 0.001777774758 0.0\nPt 0.133555554681 0.001777774758 0.407446329115\nPt 0.577999999126 0.22399999698 0.135815443038\nPt 0.355777776904 0.112888885869 0.271630886077\nPt 0.466888888015 0.001777774758 0.0\nPt 0.466888888015 0.001777774758 0.407446329115\nPt 0.911333332459 0.22399999698 0.135815443038\nPt 0.689111110237 0.112888885869 0.271630886077\nPt 0.800222221348 0.001777774758 0.0\nPt 0.800222221348 0.001777774758 0.407446329115\nPt 0.244666665792 0.557333330313 0.135815443038\nPt 0.02244444357 0.446222219202 0.271630886077\nPt 0.133555554681 0.335111108091 0.0\nPt 0.133555554681 0.335111108091 0.407446329115\nPt 0.577999999126 0.557333330313 0.135815443038\nPt 0.355777776904 0.446222219202 0.271630886077\nPt 0.466888888015 0.335111108091 0.0\nPt 0.466888888015 0.335111108091 0.407446329115\nPt 0.911333332459 0.557333330313 0.135815443038\nPt 0.689111110237 0.446222219202 0.271630886077\nPt 0.800222221348 0.335111108091 0.0\nPt 0.800222221348 0.335111108091 0.407446329115\nPt 0.244666665792 0.890666663647 0.135815443038\nPt 0.02244444357 0.779555552536 0.271630886077\nPt 0.133555554681 0.668444441424 0.0\nPt 0.133555554681 0.668444441424 0.407446329115\nPt 0.577999999126 0.890666663647 0.135815443038\nPt 0.355777776904 0.779555552536 0.271630886077\nPt 0.466888888015 0.668444441424 0.0\nPt 0.466888888015 0.668444441424 0.407446329115\nPt 0.911333332459 0.890666663647 0.135815443038\nPt 0.689111110237 0.779555552536 0.271630886077\nPt 0.800222221348 0.668444441424 0.0\nPt 0.800222221348 0.668444441424 0.407446329115\n"""
3433

3534
ret_content = poscar.get_xyz_content()
3635

3736
self.assertEqual(ref_content, ret_content)
3837

38+
def test_add_atom(self):
39+
"Make sure we can add a new atom to current poscar. "
40+
poscar = PosCar(self.filename)
41+
ori_data = poscar.data
42+
ori_tf = poscar.tf
43+
44+
poscar.add_atom('O', [0.5, 0.5, 0.5])
45+
46+
self.assertListEqual(poscar.atom_types, ['Pt', 'O'])
47+
self.assertListEqual(poscar.atom_numbers, [36, 1])
48+
self.assertListEqual(ori_data.tolist() + [[0.5, 0.5, 0.5]],
49+
poscar.data.tolist())
50+
self.assertListEqual(ori_tf.tolist() + [['T', 'T', 'T']],
51+
poscar.tf.tolist())
52+
3953
if "__main__" == __name__:
4054
suite = unittest.TestLoader().loadTestsFromTestCase(PosCarTest)
4155
unittest.TextTestRunner(verbosity=2).run(suite)

vaspy/atomco.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,46 @@ def get_content(self):
437437
content = self.get_poscar_content()
438438
return content
439439

440+
def add_atom(self, atom_type, coordinate, fix=['T', 'T', 'T']):
441+
"""
442+
Add a new atom to coordinate file.
443+
444+
Parameters:
445+
-----------
446+
atom_type: element type of the atom, str.
447+
coordinate: position of the added atom, list of float.
448+
fix: flags for fixed atom in three directions, list of str.
449+
450+
Example:
451+
--------
452+
>>> poscar.add_atom('C', [0.5, 0.5, 0.3])
453+
"""
454+
atomco_dict = self.atomco_dict
455+
tf_dict = self.tf_dict
456+
457+
self.natom += 1
458+
self.totline += 1
459+
460+
if atom_type in self.atom_types:
461+
atomco_dict[atom_type].append(coordinate)
462+
tf_dict[atom_type].append(fix)
463+
idx = self.atom_types.index(atom_type)
464+
self.atom_numbers[idx] += 1
465+
else:
466+
self.atom_types.append(atom_type)
467+
atomco_dict[atom_type] = [coordinate]
468+
tf_dict[atom_type] = [fix]
469+
self.atom_numbers.append(1)
470+
471+
# New data and fix info.
472+
data, tf = [], []
473+
for atom_type in self.atom_types:
474+
data += atomco_dict[atom_type]
475+
tf += tf_dict[atom_type]
476+
477+
self.data = np.float64(np.array(data))
478+
self.tf = np.array(tf)
479+
440480
def tofile(self, filename='POSCAR_c'):
441481
"生成文件"
442482
"PosCar object to POSCAR or CONTCAR."

0 commit comments

Comments
 (0)