-
Notifications
You must be signed in to change notification settings - Fork 1
/
end_to_end.py
56 lines (43 loc) · 1.41 KB
/
end_to_end.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import jax
import jax.numpy as jnp
from glp.graph import system_to_graph
from glp.neighborlist import neighbor_list
from .calculator import Calculator
from .utils import strain_graph, get_strain
def calculator(
potential,
system,
skin=0.0,
capacity_multiplier=1.25,
):
cutoff = potential.cutoff
state, update_neighbors = neighbor_list(
system, cutoff=cutoff, skin=skin, capacity_multiplier=capacity_multiplier
)
def energy_fn(system, strain, state):
graph = system_to_graph(system, state)
graph = strain_graph(graph, strain)
return jnp.sum(potential(graph))
energies_and_derivatives_fn = jax.value_and_grad(
energy_fn, argnums=(0, 1), allow_int=True
)
def calculator_fn(system, state, velocities=None, masses=None):
state = update_neighbors(system, state)
strain = get_strain(dtype=system.R.dtype)
energy, grads_and_stress = energies_and_derivatives_fn(system, strain, state)
grads, stress = grads_and_stress
forces = -grads.R
output = {"energy": energy, "forces": forces, "stress": stress}
return output, state
return (
Calculator(
calculator_fn,
lambda system: calculator(
potential,
system,
skin=skin,
capacity_multiplier=capacity_multiplier,
),
),
state,
)