choderalab / openmmtools

A batteries-included toolkit for the GPU-accelerated OpenMM molecular simulation engine.
http://openmmtools.readthedocs.io
MIT License
250 stars 77 forks source link

`reduced_potential` raising exception? #293

Closed pgrinaway closed 7 years ago

pgrinaway commented 7 years ago

I tried this code:

import simtk.openmm as openmm
import openmmtools.testsystems as ts
import openmmtools.cache as cache
import openmmtools.states as states
import simtk.unit as unit

alanine_ts = ts.AlanineDipeptideExplicit()

thermodynamic_state = states.ThermodynamicState(alanine_ts.system, temperature = 300.0*unit.kelvin)
sampler_state = states.SamplerState(alanine_ts.positions, box_vectors=alanine_ts.system.getDefaultPeriodicBoxVectors())

context, integrator = cache.global_context_cache.get_context(thermodynamic_state)
sampler_state.apply_to_context(context, ignore_velocities=True)
print(thermodynamic_state.reduced_potential(sampler_state))

And I get the following exception:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/Users/grinawap/anaconda/lib/python3.5/site-packages/simtk/unit/quantity.py in _change_units_with_factor(self, new_unit, factor, post_multiply)
    675                 if post_multiply:
--> 676                     value = self._value * factor # works for number, numpy.array, or vec3, e.g.
    677                 else:

TypeError: unsupported operand type(s) for *: 'float' and 'NoneType'

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
/Users/grinawap/anaconda/lib/python3.5/site-packages/simtk/unit/quantity.py in _scale_sequence(self, value, factor, post_multiply)
    690             if post_multiply:
--> 691                 value = value*factor
    692             else:

TypeError: unsupported operand type(s) for *: 'float' and 'NoneType'

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
/Users/grinawap/anaconda/lib/python3.5/site-packages/simtk/unit/quantity.py in _scale_sequence(self, value, factor, post_multiply)
    699                     else:
--> 700                         for i in range(len(value)):
    701                             value[i] = value[i]*factor

TypeError: object of type 'float' has no len()

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
<ipython-input-1-8d4437b36778> in <module>()
     12 context, integrator = cache.global_context_cache.get_context(thermodynamic_state)
     13 sampler_state.apply_to_context(context, ignore_velocities=True)
---> 14 print(thermodynamic_state.reduced_potential(sampler_state))

/Users/grinawap/anaconda/lib/python3.5/site-packages/openmmtools-0.13.1-py3.5.egg/openmmtools/states.py in reduced_potential(self, context_state)
    635         beta = 1.0 / (unit.BOLTZMANN_CONSTANT_kB * self.temperature)
    636         reduced_potential = potential_energy
--> 637         reduced_potential = reduced_potential / unit.AVOGADRO_CONSTANT_NA
    638         pressure = self.pressure
    639         if pressure is not None:

/Users/grinawap/anaconda/lib/python3.5/site-packages/simtk/unit/quantity.py in __rtruediv__(self, other)
    424         else:
    425             # print "R scalar / quantity"
--> 426             return other * pow(self, -1.0)
    427             # return Quantity(other / self._value, pow(self.unit, -1.0))
    428 

/Users/grinawap/anaconda/lib/python3.5/site-packages/simtk/unit/quantity.py in __rmul__(self, other)
    387         else:
    388             # print "scalar * quantity"
--> 389             return self._change_units_with_factor(self.unit, other, post_multiply=True)
    390             # return Quantity(other * self._value, self.unit)
    391 

/Users/grinawap/anaconda/lib/python3.5/site-packages/simtk/unit/quantity.py in _change_units_with_factor(self, new_unit, factor, post_multiply)
    680             except TypeError:
    681                 value = copy.deepcopy(self._value)
--> 682                 result = Quantity(self._scale_sequence(value, factor, post_multiply), new_unit)
    683         if (new_unit.is_dimensionless()):
    684             return result._value

/Users/grinawap/anaconda/lib/python3.5/site-packages/simtk/unit/quantity.py in _scale_sequence(self, value, factor, post_multiply)
    710                     value = tuple([self._scale_sequence(x, factor, post_multiply) for x in value])
    711                 else:
--> 712                     for i in range(len(value)):
    713                         value[i] = self._scale_sequence(value[i], factor, post_multiply)
    714         return value

TypeError: object of type 'float' has no len()
pgrinaway commented 7 years ago

Any idea what might be going wrong here?

pgrinaway commented 7 years ago

(I should add that adding a pressure argument makes no difference)

andrrizzi commented 7 years ago

Hmm. Let me see if I can reproduce it!

andrrizzi commented 7 years ago

Ah! You need to use either this

context, integrator = cache.global_context_cache.get_context(thermodynamic_state)
sampler_state.apply_to_context(context, ignore_velocities=True)
print(thermodynamic_state.reduced_potential(context))

Or this

context, integrator = cache.global_context_cache.get_context(thermodynamic_state)
sampler_state.apply_to_context(context, ignore_velocities=True)
sampler_state.update_from_context(context)
print(thermodynamic_state.reduced_potential(sampler_state))
pgrinaway commented 7 years ago

Oh, whoops

pgrinaway commented 7 years ago

Thanks! That seems to work!

andrrizzi commented 7 years ago

No problem!