|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# encoding: utf-8 |
| 3 | + |
| 4 | +""" |
| 5 | +This module contains unit tests for the hydrogen bond structures around an ion. |
| 6 | +""" |
| 7 | + |
| 8 | +import copy |
| 9 | +import unittest |
| 10 | + |
| 11 | +import arc.scripts.hb_ion_structures as structures |
| 12 | + |
| 13 | + |
| 14 | +class TestHBStructures(unittest.TestCase): |
| 15 | + """ |
| 16 | + Contains unit tests for the hydrogen bond structures around an ion. |
| 17 | + """ |
| 18 | + |
| 19 | + @classmethod |
| 20 | + def setUpClass(cls): |
| 21 | + """ |
| 22 | + A method that is run before all unit tests in this class. |
| 23 | + """ |
| 24 | + cls.maxDiff = None |
| 25 | + cls.oh_xyz = {'symbols': ('O', 'H'), 'isotopes': (16, 1), |
| 26 | + 'coords': ((0.0, 0.0, 0.106631), (0.0, 0.0, -0.85305))} |
| 27 | + cls.oh_zmat = {'symbols': ('O', 'H'), |
| 28 | + 'coords': ((None, None, None), ('R_1_0', None, None)), |
| 29 | + 'vars': {'R_1_0': 0.9596809607593241}, |
| 30 | + 'map': {0: 0, 1: 1}} |
| 31 | + |
| 32 | + def test_check_args(self): |
| 33 | + """ |
| 34 | + Test the check_args function. |
| 35 | + """ |
| 36 | + with self.assertRaises(ValueError): # wrong ion |
| 37 | + structures.check_args(hydration_level=2, ion='Li+', e_threshold=1, min_hb_per_ion=1, t_list=[300]) |
| 38 | + with self.assertRaises(ValueError): # hydration_level < 1 |
| 39 | + structures.check_args(hydration_level=0, ion='OH-', e_threshold=1, min_hb_per_ion=1, t_list=[300]) |
| 40 | + with self.assertRaises(ValueError): # hydration_level > 8 |
| 41 | + structures.check_args(hydration_level=10, ion='OH-', e_threshold=1, min_hb_per_ion=1, t_list=[300]) |
| 42 | + with self.assertRaises(ValueError): # e_threshold < 1 |
| 43 | + structures.check_args(hydration_level=2, ion='OH-', e_threshold=0, min_hb_per_ion=1, t_list=[300]) |
| 44 | + with self.assertRaises(ValueError): # min_hb_per_ion > hydration_level |
| 45 | + structures.check_args(hydration_level=2, ion='OH-', e_threshold=1, min_hb_per_ion=3, t_list=[300]) |
| 46 | + with self.assertRaises(ValueError): # min_hb_per_ion < 1 |
| 47 | + structures.check_args(hydration_level=2, ion='OH-', e_threshold=1, min_hb_per_ion=0, t_list=[300]) |
| 48 | + with self.assertRaises(ValueError): # t_list not a list |
| 49 | + structures.check_args(hydration_level=2, ion='OH-', e_threshold=1, min_hb_per_ion=1, t_list=300) |
| 50 | + with self.assertRaises(ValueError): # t_list empty |
| 51 | + structures.check_args(hydration_level=2, ion='OH-', e_threshold=1, min_hb_per_ion=1, t_list=[]) |
| 52 | + with self.assertRaises(ValueError): # t_list has wrong T |
| 53 | + structures.check_args(hydration_level=2, ion='OH-', e_threshold=1, min_hb_per_ion=1, t_list=[-1]) |
| 54 | + |
| 55 | + def test_get_ion_zmat(self): |
| 56 | + """ |
| 57 | + Test the get_ion_zmat function. |
| 58 | + """ |
| 59 | + zmat = structures.get_ion_zmat('OH-') |
| 60 | + self.assertEqual(self.oh_zmat, zmat) |
| 61 | + |
| 62 | + |
| 63 | + def test_get_last_added_water_o_index(self): |
| 64 | + """ |
| 65 | + Test the get_last_added_water_o_index function. |
| 66 | + """ |
| 67 | + # dict xyz with OH and one water molecule in atom order OHHOH: |
| 68 | + xyz = {'symbols': ('O', 'H', 'H', 'O', 'H'), 'isotopes': (16, 1, 1, 16, 1), |
| 69 | + 'coords': ((0.0, 0.0, 0.106631), (0.0, 0.0, -0.85305), (0.0, 0.0, 1.106631), |
| 70 | + (0.0, 0.0, 2.106631), (0.0, 0.0, 3.106631))} |
| 71 | + last_o_index = structures.get_last_added_water_o_index(xyz, self.oh_zmat) |
| 72 | + self.assertEqual(last_o_index, 3) |
| 73 | + |
| 74 | + |
| 75 | + def test_add_water_molecule(self): |
| 76 | + """ |
| 77 | + Test the add_water_molecule function. |
| 78 | + """ |
| 79 | + new_zmat_1 = structures.add_water_molecule(zmat=copy.deepcopy(self.oh_zmat), ion_zmat=self.oh_zmat, dihedarl=0, beta=110) |
| 80 | + expected_zmat_1 = {'symbols': ('O', 'H', 'H', 'O', 'H'), |
| 81 | + 'coords': ((None, None, None), ('R_1_0', None, None), ('R_2_0', 'A_2_0_1', None), |
| 82 | + ('R_3_0', 'A_3_0_1', 'D_3_0_1_2'), ('R_4_3', 'A_4_3_2', 'D_4_3_2_1')), |
| 83 | + 'vars': {'R_1_0': 0.9596809607593241, 'R_2_0': 1.5, 'A_2_0_1': 110, 'R_3_0': 2.4699999999999998, |
| 84 | + 'A_3_0_1': 110, 'D_3_0_1_2': 0, 'R_4_3': 0.97, 'A_4_3_2': 109.5, 'D_4_3_2_1': 90}, |
| 85 | + 'map': {0: 0, 1: 1, 2: 2, 3: 3, 4: 4}} |
| 86 | + self.assertEqual(new_zmat_1, expected_zmat_1) |
| 87 | + |
| 88 | + new_zmat_2 = structures.add_water_molecule(zmat=expected_zmat_1, ion_zmat=self.oh_zmat, dihedarl=180, beta=110) |
| 89 | + expected_zmat_2 = {'symbols': ('O', 'H', 'H', 'O', 'H', 'H', 'O', 'H'), |
| 90 | + 'coords': ((None, None, None), ('R_1_0', None, None), ('R_2_0', 'A_2_0_1', None), |
| 91 | + ('R_3_0', 'A_3_0_1', 'D_3_0_1_2'), ('R_4_3', 'A_4_3_2', 'D_4_3_2_1'), |
| 92 | + ('R_5_0', 'A_5_0_1', 'D_5_0_1_3'), ('R_6_0', 'A_6_0_1', 'D_6_0_1_3'), |
| 93 | + ('R_7_6', 'A_7_6_5', 'D_7_6_5_1')), |
| 94 | + 'vars': {'R_1_0': 0.9596809607593241, 'R_2_0': 1.5, 'A_2_0_1': 110, 'R_3_0': 2.4699999999999998, |
| 95 | + 'A_3_0_1': 110, 'D_3_0_1_2': 0, 'R_4_3': 0.97, 'A_4_3_2': 109.5, 'D_4_3_2_1': 90, |
| 96 | + 'R_5_0': 1.5, 'A_5_0_1': 110, 'R_6_0': 2.4699999999999998, 'A_6_0_1': 110, 'D_6_0_1_3': 180, |
| 97 | + 'R_7_6': 0.97, 'A_7_6_5': 109.5, 'D_7_6_5_1': 90, 'D_5_0_1_3': 180}, |
| 98 | + 'map': {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7}} |
| 99 | + self.assertEqual(new_zmat_2, expected_zmat_2) |
| 100 | + |
| 101 | + def test_get_skeletal_structure(self): |
| 102 | + """ |
| 103 | + Test the get_skeletal_structure function. |
| 104 | + """ |
| 105 | + zmat_0 = structures.get_skeletal_structure(ion_zmat=self.oh_zmat, num_w_mols=0) |
| 106 | + expected_zmat_0 = {'symbols': ('O', 'H'), |
| 107 | + 'coords': ((None, None, None), ('R_1_0', None, None)), |
| 108 | + 'vars': {'R_1_0': 0.9596809607593241}, 'map': {0: 0, 1: 1}} |
| 109 | + self.assertEqual(zmat_0, expected_zmat_0) |
| 110 | + |
| 111 | + zmat_1 = structures.get_skeletal_structure(ion_zmat=self.oh_zmat, num_w_mols=1) |
| 112 | + expected_zmat_1 = {'symbols': ('O', 'H', 'H', 'O', 'H'), |
| 113 | + 'coords': ((None, None, None), ('R_1_0', None, None), ('R_2_0', 'A_2_0_1', None), |
| 114 | + ('R_3_0', 'A_3_0_1', 'D_3_0_1_2'), ('R_4_3', 'A_4_3_2', 'D_4_3_2_1')), |
| 115 | + 'vars': {'R_1_0': 0.9596809607593241, 'R_2_0': 1.5, 'A_2_0_1': 110, 'R_3_0': 2.4699999999999998, |
| 116 | + 'A_3_0_1': 110, 'D_3_0_1_2': 0.0, 'R_4_3': 0.97, 'A_4_3_2': 109.5, 'D_4_3_2_1': 90}, |
| 117 | + 'map': {0: 0, 1: 1, 2: 2, 3: 3, 4: 4}} |
| 118 | + self.assertEqual(zmat_1, expected_zmat_1) |
| 119 | + |
| 120 | + zmat_2 = structures.get_skeletal_structure(ion_zmat=self.oh_zmat, num_w_mols=2) |
| 121 | + expected_zmat_2 = {'symbols': ('O', 'H', 'H', 'O', 'H', 'H', 'O', 'H'), |
| 122 | + 'coords': ((None, None, None), ('R_1_0', None, None), ('R_2_0', 'A_2_0_1', None), |
| 123 | + ('R_3_0', 'A_3_0_1', 'D_3_0_1_2'), ('R_4_3', 'A_4_3_2', 'D_4_3_2_1'), |
| 124 | + ('R_5_0', 'A_5_0_1', 'D_5_0_1_3'), ('R_6_0', 'A_6_0_1', 'D_6_0_1_3'), |
| 125 | + ('R_7_6', 'A_7_6_5', 'D_7_6_5_1')), |
| 126 | + 'vars': {'R_1_0': 0.9596809607593241, 'R_2_0': 1.5, 'A_2_0_1': 110, 'R_3_0': 2.4699999999999998, |
| 127 | + 'A_3_0_1': 110, 'D_3_0_1_2': 0.0, 'R_4_3': 0.97, 'A_4_3_2': 109.5, 'D_4_3_2_1': 90, |
| 128 | + 'R_5_0': 1.5, 'A_5_0_1': 110, 'R_6_0': 2.4699999999999998, 'A_6_0_1': 110, 'D_6_0_1_3': 180.0, |
| 129 | + 'R_7_6': 0.97, 'A_7_6_5': 109.5, 'D_7_6_5_1': 90, 'D_5_0_1_3': 180.0}, |
| 130 | + 'map': {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7}} |
| 131 | + self.assertEqual(zmat_2, expected_zmat_2) |
| 132 | + |
| 133 | + zmat_3 = structures.get_skeletal_structure(ion_zmat=self.oh_zmat, num_w_mols=3) |
| 134 | + expected_zmat_3 = {'symbols': ('O', 'H', 'H', 'O', 'H', 'H', 'O', 'H', 'H', 'O', 'H'), |
| 135 | + 'coords': ((None, None, None), ('R_1_0', None, None), ('R_2_0', 'A_2_0_1', None), |
| 136 | + ('R_3_0', 'A_3_0_1', 'D_3_0_1_2'), ('R_4_3', 'A_4_3_2', 'D_4_3_2_1'), |
| 137 | + ('R_5_0', 'A_5_0_1', 'D_5_0_1_3'), ('R_6_0', 'A_6_0_1', 'D_6_0_1_3'), |
| 138 | + ('R_7_6', 'A_7_6_5', 'D_7_6_5_1'), ('R_8_0', 'A_8_0_1', 'D_8_0_1_3'), |
| 139 | + ('R_9_0', 'A_9_0_1', 'D_9_0_1_3'), ('R_10_9', 'A_10_9_8', 'D_10_9_8_1')), |
| 140 | + 'vars': {'R_1_0': 0.9596809607593241, 'R_2_0': 1.5, 'A_2_0_1': 110, 'R_3_0': 2.4699999999999998, |
| 141 | + 'A_3_0_1': 110, 'D_3_0_1_2': 0.0, 'R_4_3': 0.97, 'A_4_3_2': 109.5, 'D_4_3_2_1': 90, |
| 142 | + 'R_5_0': 1.5, 'A_5_0_1': 110, 'R_6_0': 2.4699999999999998, 'A_6_0_1': 110, 'D_6_0_1_3': 120.0, |
| 143 | + 'R_7_6': 0.97, 'A_7_6_5': 109.5, 'D_7_6_5_1': 90, 'D_5_0_1_3': 120.0, 'R_8_0': 1.5, 'A_8_0_1': 110, |
| 144 | + 'R_9_0': 2.4699999999999998, 'A_9_0_1': 110, 'D_9_0_1_3': 240.0, 'R_10_9': 0.97, 'A_10_9_8': 109.5, |
| 145 | + 'D_10_9_8_1': 90, 'D_8_0_1_3': 240.0}, |
| 146 | + 'map': {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9, 10: 10}} |
| 147 | + self.assertEqual(zmat_3, expected_zmat_3) |
| 148 | + |
| 149 | + zmat_4 = structures.get_skeletal_structure(ion_zmat=self.oh_zmat, num_w_mols=4) |
| 150 | + expected_zmat_4 = {'symbols': ('O', 'H', 'H', 'O', 'H', 'H', 'O', 'H', 'H', 'O', 'H', 'H', 'O', 'H'), |
| 151 | + 'coords': ((None, None, None), ('R_1_0', None, None), ('R_2_0', 'A_2_0_1', None), |
| 152 | + ('R_3_0', 'A_3_0_1', 'D_3_0_1_2'), ('R_4_3', 'A_4_3_2', 'D_4_3_2_1'), |
| 153 | + ('R_5_0', 'A_5_0_1', 'D_5_0_1_3'), ('R_6_0', 'A_6_0_1', 'D_6_0_1_3'), |
| 154 | + ('R_7_6', 'A_7_6_5', 'D_7_6_5_1'), ('R_8_0', 'A_8_0_1', 'D_8_0_1_3'), |
| 155 | + ('R_9_0', 'A_9_0_1', 'D_9_0_1_3'), ('R_10_9', 'A_10_9_8', 'D_10_9_8_1'), |
| 156 | + ('R_11_0', 'A_11_0_1', 'D_11_0_1_3'), ('R_12_0', 'A_12_0_1', 'D_12_0_1_3'), |
| 157 | + ('R_13_12', 'A_13_12_11', 'D_13_12_11_1')), |
| 158 | + 'vars': {'R_1_0': 0.9596809607593241, 'R_2_0': 1.5, 'A_2_0_1': 110, 'R_3_0': 2.4699999999999998, |
| 159 | + 'A_3_0_1': 110, 'D_3_0_1_2': 0.0, 'R_4_3': 0.97, 'A_4_3_2': 109.5, 'D_4_3_2_1': 90, |
| 160 | + 'R_5_0': 1.5, 'A_5_0_1': 110, 'R_6_0': 2.4699999999999998, 'A_6_0_1': 110, 'D_6_0_1_3': 90.0, |
| 161 | + 'R_7_6': 0.97, 'A_7_6_5': 109.5, 'D_7_6_5_1': 90, 'D_5_0_1_3': 90.0, 'R_8_0': 1.5, 'A_8_0_1': 110, |
| 162 | + 'R_9_0': 2.4699999999999998, 'A_9_0_1': 110, 'D_9_0_1_3': 180.0, 'R_10_9': 0.97, 'A_10_9_8': 109.5, |
| 163 | + 'D_10_9_8_1': 90, 'D_8_0_1_3': 180.0, 'R_11_0': 1.5, 'A_11_0_1': 110, 'R_12_0': 2.4699999999999998, |
| 164 | + 'A_12_0_1': 110, 'D_12_0_1_3': 270.0, 'R_13_12': 0.97, 'A_13_12_11': 109.5, 'D_13_12_11_1': 90, 'D_11_0_1_3': 270.0}, |
| 165 | + 'map': {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9, 10: 10, 11: 11, 12: 12, 13: 13}} |
| 166 | + self.assertEqual(zmat_4, expected_zmat_4) |
| 167 | + |
| 168 | + |
| 169 | +if __name__ == '__main__': |
| 170 | + unittest.main(testRunner=unittest.TextTestRunner(verbosity=2)) |
0 commit comments