openmm / openmm-xtb

OpenMM plugin to interface with XTB
MIT License
10 stars 1 forks source link

Does not work with atom subset #4

Closed sef43 closed 7 months ago

sef43 commented 7 months ago

@peastman is this supposed to work when only a subset of atoms have their force computed with xtb?

I cannot get it to work without a segfault.

Here is a testcase I would expect to work. The system contains two water molecules. The xtb force is only used for the first one. It fails with a segfault.

import openmm as mm
import openmm.unit as unit
from openmmxtb import XtbForce
import unittest

class TestXtbForce(unittest.TestCase):

    def testForceSubset(self):

        # xtbforce on 1 water molecule

        force = XtbForce(XtbForce.GFNFF, 0.0, 1, False, [0, 1, 2], [8, 1, 1])

        # create system of 2 water molecules
        system = mm.System()
        system.addParticle(16.0)
        system.addParticle(1.0)
        system.addParticle(1.0)
        system.addParticle(16.0)
        system.addParticle(1.0)
        system.addParticle(1.0)
        system.addForce(force)
        positions = [mm.Vec3(0.1593, 0.7872, 0.5138),
                     mm.Vec3(0.1917, 0.7084, 0.4703),
                     mm.Vec3(0.2379, 0.8298, 0.5481),
                     mm.Vec3(0.1593+10.0, 0.7872, 0.5138),
                     mm.Vec3(0.1917+10.0, 0.7084, 0.4703),
                     mm.Vec3(0.2379+10.0, 0.8298, 0.5481),
                     ]
        integ = mm.LangevinIntegrator(300.0, 1.0, 0.001)
        context = mm.Context(system, integ, mm.Platform.getPlatformByName('Reference'))
        context.setPositions(positions)
        state = context.getState(getEnergy=True, getForces=True)

if __name__ == '__main__':
    unittest.main()
pytest TestXtbForce.py 

output:

platform linux -- Python 3.12.1, pytest-7.4.4, pluggy-1.4.0
rootdir: ...
collected 1 item                                                                                                                                                                                                                                       

TestXtbForce.py Fatal Python error: Segmentation fault

Extension modules: openmm._openmm, numpy.core._multiarray_umath, numpy.core._multiarray_tests, numpy.linalg._umath_linalg, numpy.fft._pocketfft_internal, numpy.random._common, numpy.random.bit_generator, numpy.random._bounded_integers, numpy.random._mt19937, numpy.random.mtrand, numpy.random._philox, numpy.random._pcg64, numpy.random._sfc64, numpy.random._generator, _openmmxtb (total: 15)
Segmentation fault (core dumped)
peastman commented 7 months ago

I just ran your test and it worked without problem. Perhaps it's a binary incompatibility of some sort in the way you built it?

sef43 commented 7 months ago

Hmm, what are you running on?

Valgrind is showing invalid writes on these lines: https://github.com/openmm/openmm-xtb/blob/ee5761f3880f3a556a95cf34a74e4ca087f73117/openmmapi/src/XtbForceImpl.cpp#L78-L83

is positions the vector of all atoms in the system?

while positionVec is a vector of length 3*number of length of indices passed to xtbforce ?

RaulPPelaez commented 7 months ago

Where is valgrind complaining? positionVec or positions?

peastman commented 7 months ago

I think you're right. Let me fix that.

peastman commented 7 months ago

The fix is in #5. Can you give it a try?

sef43 commented 7 months ago

Yes thanks #5 is working for me

peastman commented 4 weeks ago

Please open a new issue if you have a question. This one is already closed, and your question isn't related to it.