glotzerlab / hoomd-blue

Molecular dynamics and Monte Carlo soft matter simulation on GPUs.
http://glotzerlab.engin.umich.edu/hoomd-blue
BSD 3-Clause "New" or "Revised" License
329 stars 127 forks source link

Support futures module used in jobscripts #107

Closed joaander closed 8 years ago

joaander commented 8 years ago

Original report by Åsmund Ervik (Bitbucket: asmunder, GitHub: asmunder).


Trying to write a Python 2/3 agnostic jobscript for HOOMD-blue, using the futures module, there are two sanity checks in hoomd_script/init.py that break (abbreviated below):

    if type(polymers) != type([]) or len(polymers) == 0:
        raise RuntimeError();

    if type(separation) != type(dict()) or len(separation) == 0:
        raise RuntimeError();

Both of these tests raise false errors when given correct data structures for polymers and separation.

The problem is that when running on Python2 with futures, the types of list and dict are, respectively,

<class 'future.types.newlist.newlist'>
<class 'future.types.newdict.newdict'>

which doesn't match the types of [] and dict() in init.py, since that does not import future.

In raaSAFT I use future to make the framework Python 2/3 universal, so the problem occurs for all raaSAFT-based jobscripts running under Python 2, even when the jobscript does not import future.

joaander commented 8 years ago

Original comment by Åsmund Ervik (Bitbucket: asmunder, GitHub: asmunder).


Proposal for fix:

I tested this now, and the following appears to solve the problem:

#!python
    if not isinstance(polymers,list) or len(polymers) == 0:
        raise RuntimeError();

    if not isinstance(separation,dict) or len(separation) == 0:
        raise RuntimeError();

And it looks more pythonic to boot.

joaander commented 8 years ago

Removes type checks for polymers list and separation dict in create_random_polymers() in init.py

This resolves #107 and makes HOOMD-blue compatible with jobscripts using the "future" module. I initially thought to use "isinstance" as outlined in #107, but Joshua suggested to remove the type checks altogether, and I agree this makes more sense (then it fails later with more specific error messages).

joaander commented 8 years ago

Merged in asmunder/hoomd-blue-my-prs/remove-polymers-separation-typecheck-2 (pull request #112)

fixes #107 Removes type checks for polymers list and separation dict in create_random_polymers() in init.py