def test_gbsa_handler():

    patterns = [['[*:1]', 99., 99.], ['[#1:1]', 99., 99.],
                ['[#1:1]~[#7]', 99., 99.], ['[#6:1]', 0.1, 0.2],
                ['[#7:1]', 0.3, 0.4], ['[#8:1]', 0.5, 0.6],
                ['[#9:1]', 0.7, 0.8], ['[#14:1]', 99., 99.],
                ['[#15:1]', 99., 99.], ['[#16:1]', 99., 99.],
                ['[#17:1]', 99., 99.]]

    props = {
        'solvent_dielectric': 78.3,  # matches OBC2,
        'solute_dielectric': 1.0,
        'probe_radius': 0.14,
        'surface_tension': 28.3919551,
        'dielectric_offset': 0.009,
        # GBOBC1
        'alpha': 0.8,
        'beta': 0.0,
        'gamma': 2.909125
    }

    smirks = [x[0] for x in patterns]
    params = np.array([[x[1], x[2]] for x in patterns])

    gbh = nonbonded.GBSAHandler(smirks, params, props)

    obj = gbh.serialize()
    all_handlers = deserialize_handlers(bin_to_str(obj))

    assert len(all_handlers) == 1

    new_gbh = all_handlers[0]
    np.testing.assert_equal(new_gbh.smirks, gbh.smirks)
    np.testing.assert_equal(new_gbh.params, gbh.params)
    assert new_gbh.props == gbh.props
def test_gbsa_handler():

    patterns = [
       ['[*:1]', 99., 99.],
       ['[#1:1]', 99., 99.],
       ['[#1:1]~[#7]', 99., 99.],
       ['[#6:1]', 0.1, 0.2],
       ['[#7:1]', 0.3, 0.4],
       ['[#8:1]', 0.5, 0.6],
       ['[#9:1]', 0.7, 0.8],
       ['[#14:1]', 99., 99.],
       ['[#15:1]', 99., 99.],
       ['[#16:1]', 99., 99.],
       ['[#17:1]', 99., 99.]
    ]

    smirks = [x[0] for x in patterns]
    params = np.array([[x[1], x[2]] for x in patterns])
    props = {'foo': 'bar'}
    gbh = nonbonded.GBSAHandler(smirks, params, props)

    mol = Chem.MolFromSmiles("C1CNCOC1F")

    gb_params, gb_vjp_fn = gbh.parameterize(mol)

    ligand_params = np.array([
        [0.1, 0.2], # C
        [0.1, 0.2], # C
        [0.3, 0.4], # N
        [0.1, 0.2], # C
        [0.5, 0.6], # O
        [0.1, 0.2], # C
        [0.7, 0.8]  # F
    ])

    np.testing.assert_almost_equal(gb_params, ligand_params)

    gb_params_adjoints = np.random.randn(*gb_params.shape)

    # test that we can use the adjoints
    adjoints = gb_vjp_fn(gb_params_adjoints)[0]

    # if a parameter is > 99 then its adjoint should be zero (converse isn't necessarily true since)
    mask = np.argwhere(params > 90)
    assert np.all(adjoints[mask] == 0.0) == True