示例#1
0
def test_build_geometric_constraint_string():
    """
    Test build_geometric_constraint_string() function

    Notes
    -----
    Atom indices in constraits_string is 1-indexed
    Atom indices in dihedral_idxs is 0-indexed
    """
    constraints_dict = {
        'freeze': [{
            'type': 'xyz',
            'indices': [0, 1, 2, 5, 6],
        }, {
            'type': 'distance',
            'indices': [0, 4],
        }],
        'set': [
            {
                'type': 'distance',
                'indices': [0, 3],
                'value': 1.0,
            },
            {
                'type': 'angle',
                'indices': [1, 0, 3],
                'value': 30.0,
            },
            {
                'type': 'dihedral',
                'indices': [0, 1, 2, 3],
                'value': 60.0,
            },
        ]
    }
    constraints_string = build_geometric_constraint_string(constraints_dict)
    # validate the constraints string
    assert constraints_string.strip() == '\n'.join([
        '$freeze', 'xyz 1-3,6-7', 'distance 1 5', '$set', 'distance 1 4 1.0',
        'angle 2 1 4 30.0', 'dihedral 1 2 3 4 60.0'
    ])
    # test with dihedral_idx_values
    dihedral_idx_values = [(1, 2, 3, 4, 90.0), (2, 3, 4, 5, 120.0)]
    constraints_string2 = build_geometric_constraint_string(
        constraints_dict, dihedral_idx_values=dihedral_idx_values)
    assert constraints_string2.strip() == '\n'.join([
        '$freeze', 'xyz 1-3,6-7', 'distance 1 5', '$set', 'distance 1 4 1.0',
        'angle 2 1 4 30.0', 'dihedral 1 2 3 4 60.0', 'dihedral 2 3 4 5 90.0',
        'dihedral 3 4 5 6 120.0'
    ])
示例#2
0
 def write_constraints_txt(self):
     """ write a constraints.txt file for geomeTRIC """
     if self.extra_constraints is None:
         constraints_string = "$set\n"
         for d1, d2, d3, d4, v in self.dihedral_idx_values:
             # geomeTRIC use atomic index starting from 1
             constraints_string += f"dihedral {d1 + 1} {d2 + 1} {d3 + 1} {d4 + 1} {float(v)}\n"
     else:
         constraints_string = build_geometric_constraint_string(
             self.extra_constraints, self.dihedral_idx_values)
     with open('constraints.txt', 'w') as outfile:
         outfile.write(constraints_string)