IMP logo
IMP Reference Guide  develop.266d43d110,2026/09/24
The Integrative Modeling Platform
jax.py
1 """@namespace IMP.jax
2  @brief Support for the JAX Python library.
3 
4  IMP currently has rudimentary support for running on a graphics
5  processing unit (GPU) or similar systems such as
6  Tensor Processing Units (TPUs). This support uses the
7  [JAX](https://docs.jax.dev/) Python library.
8 """
9 
10 import jax.numpy as jnp
11 
12 
13 class Space:
14  """The space in which restraints are evaluated. See FreeSpace for the
15  default unbounded space, or PeriodicSpace for a space that implements
16  periodic boundary conditions."""
17 
18  def distance(dr):
19  """If given an array of particle-particle vectors, return an array
20  of distances. If given a single particle-particle vector, return
21  a single distance."""
22  pass
23 
24  def shift(r, dr):
25  """Shift r by dr and return new r"""
26  pass
27 
28  def shift_indexes(r, indexes, dr):
29  """Modify r[indexes] in place by adding dr"""
30  pass
31 
32 
33 class FreeSpace(Space):
34  """An unbounded space with no periodic boundary conditions."""
35 
36  @staticmethod
37  def distance(dr):
38  return jnp.linalg.norm(dr, axis=-1)
39 
40  @staticmethod
41  def shift(r, dr):
42  return r + dr
43 
44  @staticmethod
45  def shift_indexes(r, indexes, dr):
46  return r.at[indexes].add(dr)
47 
48 
50  """A space with periodic boundary conditions.
51 
52  @param side A 3D vector of the periodic boundary box dimensions.
53  """
54 
55  def __init__(self, side):
56  self.side = jnp.asarray(side)
57 
58  def distance(self, dr):
59  p_dr = jnp.mod(dr + self.side * 0.5, self.side) - 0.5 * self.side
60  return jnp.linalg.norm(p_dr, axis=-1)
61 
62  def shift(self, r, dr):
63  return jnp.mod(r + dr, self.side)
64 
65  def shift_indexes(self, r, indexes, dr):
66  newr = jnp.mod(r[indexes] + dr, self.side)
67  return r.at[indexes].set(newr)
def shift_indexes
Modify r[indexes] in place by adding dr.
Definition: jax.py:28
A space with periodic boundary conditions.
Definition: jax.py:49
The space in which restraints are evaluated.
Definition: jax.py:14
def shift
Shift r by dr and return new r.
Definition: jax.py:24
def distance
If given an array of particle-particle vectors, return an array of distances.
Definition: jax.py:18