Page 1 of 1

Malloc error

Posted: Thu May 30, 2019 9:07 am
by gideonsimpson
I've been playing with coulomb gases, and I got the following (mysterious) error when performing simulation.minimizeEnergy():

Code: Select all

python(7187,0x1093785c0) malloc: *** error for object 0x7fe32d3871f8: pointer being freed was not allocated
python(7187,0x1093785c0) malloc: *** set a breakpoint in malloc_error_break to debug
Abort trap: 6
What makes this "mysterious" is that it is sensitive to the initial configuration of my atoms. I get crashes in some cases, and it works fine in others.

My full code is:

Code: Select all


from simtk import openmm, unit
from simtk.openmm import app
import numpy as np


nparticles = 7
mass = 1.0 * unit.amu
charge = 0.1 * unit.elementary_charge
sigma = 1.0 * unit.nanometer
epsilon = 0.0 * unit.kilojoule_per_mole
dt = 0.001 * unit.picosecond
gamma = 1/unit.picosecond
temp =300 * unit.kelvin

system = openmm.System()
for index in range(nparticles):
    system.addParticle(mass)

coulomb_force = openmm.NonbondedForce()
coulomb_force.setNonbondedMethod(openmm.NonbondedForce.NoCutoff)
for index in range(nparticles):
    coulomb_force.addParticle(charge, sigma, epsilon)
coulomb_force_index = system.addForce(coulomb_force)

trapping_force = openmm.CustomExternalForce("10 * r^2; r=sqrt(x*x+y*y+z*z)");
for i in range(nparticles):
    trapping_force.addParticle(i, [])
trapping_force_index = system.addForce(trapping_force)

# this causes an malloc error
center = np.array([[0.0,0.0,0.0]])
# this works fine
#center = np.array([[0.01,0.01,0.0]])
positions = np.copy(center)
for j in range(nparticles-1):
    positions = np.append(positions, center
        + 2.0 * np.array([[np.cos(2*j * np.pi/(nparticles-1)),
        np.sin(2*j * np.pi/(nparticles-1)), 0.0]]),axis=0)

topology = app.Topology()
element = app.Element.getBySymbol('H')
chain = topology.addChain()
for particle in range(system.getNumParticles()):
    residue = topology.addResidue('H', chain)
    topology.addAtom('H', element, residue)

# this is arbitrary
integrator = openmm.BrownianIntegrator(temp,gamma, dt)

simulation = app.Simulation(topology, system, integrator)
simulation.context.setPositions(positions)

state = simulation.context.getState(getEnergy=True)
potential_energy =state.getPotentialEnergy()
print('Intitial potential energy: %s' % potential_energy)

simulation.minimizeEnergy()
state = simulation.context.getState(getEnergy=True)
potential_energy =state.getPotentialEnergy()
print('Minimized potential energy: %s' % potential_energy)

Re: Malloc error

Posted: Thu May 30, 2019 10:12 am
by peastman
The first thing I'd try is uninstalling and reinstalling OpenMM. That's the sort of error I see when I've accidentally messed something up in my installation, for example if I recompiled the C++ libraries but forgot to also recompile the Python interface.

A few other questions.

How did you install OpenMM? From source? With conda? Some other way?

What OS are you using?

Peter

Re: Malloc error

Posted: Thu May 30, 2019 10:31 am
by gideonsimpson
I'm on MacOS 10.14.5 with an anaconda 2019.3 installation. OpenMM and associated libraries were freshly installed with conda install -c omnia openmm.

Re: Malloc error

Posted: Thu May 30, 2019 10:56 am
by peastman
Did you try uninstalling and reinstalling like I suggested?

Re: Malloc error

Posted: Thu May 30, 2019 12:01 pm
by gideonsimpson
Did a conda uninstall followed by a conda install -c omnia, which installed:

Code: Select all

The following NEW packages will be INSTALLED:

  openmm             omnia/osx-64::openmm-7.3.1-py37_cuda92_rc_2

Same results

Re: Malloc error

Posted: Thu May 30, 2019 3:50 pm
by egallicc
I have been getting malloc() errors due to singularities in the forces. See for example https://github.com/pandegroup/openmm/issues/2244

The fact that the crashes are dependent on atomic configurations suggests that the same thing might be happening here ...

Re: Malloc error

Posted: Fri May 31, 2019 5:33 pm
by peastman
I managed to reproduce this. The malloc error is a red herring: that's happening in the SWIG generated wrapper code. The real problem is that the energy minimizer is throwing an exception with the message, "Particle coordinate is nan". SWIG is supposed to automatically translate that into a Python exception, but for some reason it's crashing instead.

So why are you getting nan positions? The problem is in the way you define the CustomExternalForce:

Code: Select all

trapping_force = openmm.CustomExternalForce("10 * r^2; r=sqrt(x*x+y*y+z*z)");
Notice how you've made it a bit more complicated than it needs to be: you first take a square root, then immediately square it again. By doing that, you've created a trap for yourself, because the derivative of sqrt() is singular at 0. When a particle is exactly at the origin, the force on it becomes 0/0 = nan. Fortunately, this is very easy to fix. Just define it as

Code: Select all

trapping_force = openmm.CustomExternalForce("10 * r2; r2=x*x+y*y+z*z")
and the error goes away.

Re: Malloc error

Posted: Sat Jun 01, 2019 10:14 am
by gideonsimpson
Yup, fixed it. Thanks.

Re: Malloc error

Posted: Sat Jun 01, 2019 4:47 pm
by peastman
I've added some algebraic optimizations to help it handle this case automatically. The expression optimizer will now make the replacements sqrt(x)^2 = x and sqrt(x^2) = abs(x). https://github.com/pandegroup/openmm/pull/2312

Re: Malloc error

Posted: Tue Jun 11, 2019 12:27 pm
by dkonstan
I read somewhere that this is a MacOS problem has something to do with permissions, hard to fix (https://stackoverflow.com/questions/223 ... ing-termin).