Example #1
0
def test_same_interactions(left, right, expected):
    """
    Test that Molecule.same_interactions works as expected.
    """
    left_mol = Molecule()
    left_mol.interactions = left
    right_mol = Molecule()
    right_mol.interactions = right
    assert left_mol.same_interactions(right_mol) == expected
    assert right_mol.same_interactions(left_mol) == expected
def test_pre_post_section_lines(pre_meta, post_meta):
    """
    :func:`vermouth.gmx.itp.write_molecule_itp` writes pre and post section lines.

    If `pre_meta` or `post_meta` is `True`, then "pre_section_lines" or
    "post_section_lines", respectively, is read from `molecule.meta`, otherwise
    it is passed as an argument.
    """
    molecule = Molecule(nrexcl=1)
    molecule.add_nodes_from((
        (0, {
            'atype': 'Q5',
            'resid': 1,
            'resname': 'VAL',
            'atomname': 'BB',
            'charge_group': 1,
            'charge': 1,
        }),
        (1, {
            'atype': 'Q5',
            'resid': 1,
            'resname': 'VAL',
            'atomname': 'BB',
            'charge_group': 1,
            'charge': 1,
        }),
        (2, {
            'atype': 'Q5',
            'resid': 1,
            'resname': 'VAL',
            'atomname': 'BB',
            'charge_group': 1,
            'charge': 1,
        }),
    ))
    # The molecule has 6 interaction sections. The sections are names
    # "interaction_<joint>_<part>", where <joint> can be "with" or "only", and
    # <part> can be "pre", "post", or "pre_post". The <part> indicates what
    # pre_section_lines and post_section_lines are defined for the section.
    # When <joint> in "with", then the section contains 2 interactions in
    # addition to the pre- and post- lines. When <joint> is "only", then the
    # section only has the pre/post lines defined, and no interactions.
    parts = ('pre', 'post', 'pre_post')
    molecule.interactions = {}
    for part in parts:
        name = 'interaction_with_{}'.format(part)
        molecule.interactions[name] = [
            Interaction(atoms=[0, 1], parameters=[name], meta={}),
            Interaction(atoms=[1, 2], parameters=[name], meta={}),
        ]

    pre_section_lines = {
        'atoms': ['test_atoms_0', 'test_atoms_1'],
        'interaction_with_pre': ['test_pre_0', 'test_pre_1', 'test_pre_2'],
        'interaction_with_pre_post': ['test_pre_post_0'],
        'interaction_only_pre': ['test_pre_only_0', 'test_pre_only_1'],
        'interaction_only_pre_post':
        ['test_prepost_only_0', 'test_prepost_only_1'],
    }
    post_section_lines = {
        'atoms': ['after_atoms_0'],
        'interaction_with_post': ['after_post_0', 'after_post_1'],
        'interaction_with_pre_post': ['after_pre_post_0'],
        'interaction_only_post': ['after_post_only_0', 'after_post_only_1'],
        'interaction_only_pre_post': ['after_prepost_only_0'],
    }
    expected_segments = [
        """
        [ atoms ]
        test_atoms_0
        test_atoms_1
        1 Q5 1 VAL BB 1 1 
        2 Q5 1 VAL BB 1 1 
        3 Q5 1 VAL BB 1 1 
        after_atoms_0
        """,
        """
        [ interaction_with_pre ]
        test_pre_0
        test_pre_1
        test_pre_2
        1 2 interaction_with_pre
        2 3 interaction_with_pre
        """,
        """
        [ interaction_with_post ]
        1 2 interaction_with_post
        2 3 interaction_with_post
        """,
        """
        [ interaction_with_pre_post ]
        test_pre_post_0
        1 2 interaction_with_pre_post
        2 3 interaction_with_pre_post
        after_pre_post_0
        """,
        """
        [ interaction_only_pre ]
        test_pre_only_0
        test_pre_only_1
        """,
        """
        [ interaction_only_post ]
        after_post_only_0
        after_post_only_1
        """,
        """
        [ interaction_only_pre_post ]
        test_prepost_only_0
        test_prepost_only_1
        after_prepost_only_0
        """,
    ]

    if post_meta:
        arg_post = post_section_lines
        molecule.meta['post_section_lines'] = 'invalid'
    else:
        arg_post = None
        molecule.meta['post_section_lines'] = post_section_lines
    if pre_meta:
        arg_pre = pre_section_lines
        molecule.meta['pre_section_lines'] = 'invalid'
    else:
        arg_pre = None
        molecule.meta['pre_section_lines'] = pre_section_lines

    outfile = io.StringIO()
    write_molecule_itp(molecule,
                       outfile,
                       moltype='test',
                       post_section_lines=arg_post,
                       pre_section_lines=arg_pre)
    itp_content = outfile.getvalue()
    # This could be a assert all(...), but it makes it more difficult to
    # understant what is happening in case of a failure.
    for segment in expected_segments:
        assert textwrap.dedent(segment)[:-1] in itp_content