CustomExternalForce should use periodicdistance to avoid major artifacts when heavy atoms are restrained to fixed points in space
cc: https://github.com/pandegroup/openmm/issues/1854#issuecomment-346202857

In order to avoid major artifacts when using restraints to fixed points in space via `CustomExternalForce` (in [`restrain_atoms`](https://github.com/choderalab/openmmtools/blob/f50f0844b28106978973e6374613bc8d24a3c4b9/openmmtools/forcefactories.py#L101)), for periodic systems, we should replace
```python
restraint_force = mm.CustomExternalForce('(K/2)*((x-x0)^2 + (y-y0)^2 + (z-z0)^2)')
```
with
```python
restraint_force = mm.CustomExternalForce('(K/2)*periodicdistance(x, y, z, x0, y0, z0)^2')
```
as suggested in [this comment](https://github.com/pandegroup/openmm/issues/1854#issuecomment-346493353).

The relevant code is [here](https://github.com/choderalab/openmmtools/blob/f50f0844b28106978973e6374613bc8d24a3c4b9/openmmtools/forcefactories.py#L158).
diff --git a/openmmtools/forcefactories.py b/openmmtools/forcefactories.py
index 8c59aef..3ebca36 100644
--- a/openmmtools/forcefactories.py
+++ b/openmmtools/forcefactories.py
@@ -155,7 +155,11 @@ def restrain_atoms(thermodynamic_state, sampler_state, restrained_atoms, sigma=3
         sampler_state.positions -= centroid * distance_unit
 
     # Create a CustomExternalForce to restrain all atoms.
-    restraint_force = openmm.CustomExternalForce('(K/2)*((x-x0)^2 + (y-y0)^2 + (z-z0)^2)')
+    if thermodynamic_state.is_periodic:
+        energy_expression = '(K/2)*periodicdistance(x, y, z, x0, y0, z0)^2' # periodic distance
+    else:
+        energy_expression = '(K/2)*((x-x0)^2 + (y-y0)^2 + (z-z0)^2)' # non-periodic distance
+    restraint_force = openmm.CustomExternalForce(energy_expression)
     # Adding the spring constant as a global parameter allows us to turn it off if desired
     restraint_force.addGlobalParameter('K', K)
     restraint_force.addPerParticleParameter('x0')
['openmmtools/forcefactories.py']