-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
"""A periodic Lennard Jones potential.""" | ||
|
||
|
||
import autograd.numpy as np | ||
from autograd import elementwise_grad | ||
from mlp.ag.neighborlist import get_distances | ||
|
||
def energy(params, positions, cell, strain=np.zeros((3, 3))): | ||
"Compute the energy of a Lennard-Jones system." | ||
|
||
sigma = params.get('sigma', 1.0) | ||
epsilon = params.get('epsilon', 1.0) | ||
|
||
rc = 3 * sigma | ||
|
||
e0 = 4 * epsilon * ((sigma / rc)**12 - (sigma / rc)**6) | ||
|
||
r2 = get_distances(positions, cell, rc, 0.01, strain)**2 | ||
|
||
zeros = np.equal(r2, 0.0) | ||
adjusted = np.where(zeros, np.ones_like(r2), r2) | ||
|
||
c6 = np.where((r2 <= rc**2) & (r2 > 0.0), (sigma**2 / adjusted)**3, np.zeros_like(r2)) | ||
c6 = np.where(zeros, np.zeros_like(r2), c6) | ||
energy = -e0 * (c6 != 0.0).sum() | ||
c12 = c6**2 | ||
energy += np.sum(4 * epsilon * (c12 - c6)) | ||
|
||
return energy / 2 | ||
|
||
|
||
def forces(params, positions, cell): | ||
dEdR = elementwise_grad(energy, 1) | ||
return -dEdR(params, positions, cell) | ||
|
||
|
||
def stress(params, positions, cell, strain=np.zeros((3, 3))): | ||
dEdst = elementwise_grad(energy, 3) | ||
|
||
volume = np.abs(np.linalg.det(cell)) | ||
|
||
der = dEdst(params, positions, cell, strain) | ||
result = (der + der.T) / 2 / volume | ||
return np.take(result, [0, 4, 8, 5, 2, 1]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters