Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quick fix to reallocation with nd.array-type box #249

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

arturtoshev
Copy link
Contributor

@arturtoshev arturtoshev commented Jan 3, 2023

Minimal working example:

import jax.numpy as jnp
from jax_md import space, partition

pos = jnp.array([[0.0, 0.0]])
box_size = jnp.array([1.0,  1.0])
cell_size = 0.1

displacement_fn, shift_fn = space.periodic(box_size)
neighbor_list_fn = partition.neighbor_list(
    displacement_fn, box_size, cell_size)

print('First iteration neighbor allocation')
_ = neighbor_list_fn.allocate(pos)
print('Second iteration neighbor allocation')
_ = neighbor_list_fn.allocate(pos)

leads to

First iteration neighbor allocation
Second iteration neighbor allocation
Traceback (most recent call last):
  File "/home/artur/code/jaxmdQF/test.py", line 15, in <module>
    _ = neighbor_list_fn.allocate(pos)
  File "/home/artur/code/jaxmdQF/jax-md/jax_md/partition.py", line 981, in allocate_fn
    return neighbor_list_fn(position, extra_capacity=extra_capacity, **kwargs)
  File "/home/artur/code/jaxmdQF/jax-md/jax_md/partition.py", line 953, in neighbor_list_fn
    return neighbor_fn((position, PartitionError(jnp.zeros((), jnp.uint8))))
  File "/home/artur/code/jaxmdQF/jax-md/jax_md/partition.py", line 910, in neighbor_fn
    idx = cell_list_candidate_fn(cl, position)
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

I'm not really sure why this happens, but my quick fix is the only way I could get around the error message. I backtracked the error and it originates from this commit: 511eb19

Note: this bug happens only when box_size is an array (both np.ndarray and jnp.ndarray).

Minimal working example:

```bash
import jax.numpy as jnp
from jax_md import space, partition

pos = jnp.array([[0.0, 0.0]])
box_size = jnp.array([1.0,  1.0])
cell_size = 0.1

displacement_fn, shift_fn = space.periodic(box_size)
neighbor_list_fn = partition.neighbor_list(
    displacement_fn, box_size, cell_size, format=partition.Sparse)

print('First iteration neighbor allocation')
_ = neighbor_list_fn.allocate(pos)
print('Second iteration neighbor allocation')
_ = neighbor_list_fn.allocate(pos)
```

leads to 

```bash
First iteration neighbor allocation
Second iteration neighbor allocation
Traceback (most recent call last):
  File "/home/artur/code/jaxmdQF/test.py", line 15, in <module>
    _ = neighbor_list_fn.allocate(pos)
  File "/home/artur/code/jaxmdQF/jax-md/jax_md/partition.py", line 981, in allocate_fn
    return neighbor_list_fn(position, extra_capacity=extra_capacity, **kwargs)
  File "/home/artur/code/jaxmdQF/jax-md/jax_md/partition.py", line 953, in neighbor_list_fn
    return neighbor_fn((position, PartitionError(jnp.zeros((), jnp.uint8))))
  File "/home/artur/code/jaxmdQF/jax-md/jax_md/partition.py", line 910, in neighbor_fn
    idx = cell_list_candidate_fn(cl, position)
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
```

I'm not really sure why this happens, but my quick fix is the only way I could get around the error message. I backtracked the error and it originates from this commit: jax-md@511eb19

Note: this bug happens only when `box_size` is an array (both np.array and jnp.array).
@arturtoshev
Copy link
Contributor Author

One can also safely use an older jax-md version. The latest version before the above-mentioned commit is 0.2.3.

PS: Of course, I mean ndarray in the title.

@MarcBerneman MarcBerneman mentioned this pull request Jul 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant