Skip to content

Commit

Permalink
torch ff unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
marjanalbooyeh committed Sep 1, 2023
1 parent 80562bc commit 99be3c6
Showing 1 changed file with 19 additions and 63 deletions.
82 changes: 19 additions & 63 deletions hoomd_organics/tests/library/test_forcefields.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import os

import hoomd
import torch
import torch.nn as nn

from hoomd_organics import Molecule, Pack, Simulation
from hoomd_organics.library import (
GAFF,
OPLS_AA,
Expand All @@ -13,52 +10,10 @@
OPLS_AA_PPS,
BeadSpring,
FF_from_file,
TorchCustomForce,
)
from hoomd_organics.tests.base_test import ASSETS_DIR, BaseTest


class NN(nn.Module):
def __init__(
self, hidden_dim, out_dim, n_layers, act_fn="Tanh", dropout=0.5
):
super(NN, self).__init__()
self.in_dim = 4 # (relative position vector, center-to-center distance)
self.out_dim = 1 # predicted energy
self.hidden_dim = hidden_dim

self.n_layers = n_layers
self.act_fn = act_fn
self.dropout = dropout

self.net = nn.Sequential(*self._get_net())

def _get_act_fn(self):
act = getattr(nn, self.act_fn)
return act()

def _get_net(self):
layers = [nn.Linear(self.in_dim, self.hidden_dim), self._get_act_fn()]
for i in range(self.n_layers - 1):
layers.append(nn.Linear(self.hidden_dim, self.hidden_dim))
layers.append(self._get_act_fn())
layers.append(nn.Dropout(p=self.dropout))
layers.append(nn.Linear(self.hidden_dim, self.out_dim))
return layers

def forward(self, pos_1, pos_2):
pos_1.requires_grad = True
x = torch.tensor(pos_1 - pos_2).to(pos_1.device)
x = torch.cat(
(x, torch.norm(x, dim=1, keepdim=True).to(x.device)), dim=1
).to(x.device)
energy = self.net(x)
force = (-1.0) * torch.autograd.grad(
energy, pos_1, retain_graph=True, create_graph=True
)[0].to(energy.device)
return force


class TestXMLForceFields:
def test_GAFF(self):
ff = GAFF()
Expand Down Expand Up @@ -138,21 +93,22 @@ def test_BeadSpring(self):
assert ff.hoomd_forcefield[3].params[param]["n"] == 1

def test_TorchCustomForce(self, benzene_smiles):
molecule = Molecule(num_mols=2, smiles=benzene_smiles)
molecule.coarse_grain(beads={"A": benzene_smiles})
system = Pack(
molecules=[molecule],
density=0.5,
r_cut=2.5,
)
model = NN(
hidden_dim=32, out_dim=1, n_layers=2, act_fn="Tanh", dropout=0.5
)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model.load_state_dict(torch.load("best_model.pth", map_location=device))
custom_force = TorchCustomForce(model)
sim = Simulation(
initial_state=system.hoomd_snapshot,
forcefield=[custom_force],
)
sim.run_NVT(n_steps=10, kT=1.0, tau_kt=1.0)
pass
# molecule = Molecule(num_mols=2, smiles=benzene_smiles)
# molecule.coarse_grain(beads={"A": benzene_smiles})
# system = Pack(
# molecules=[molecule],
# density=0.8,
# r_cut=2.5,
# )
# model = NN(hidden_dim=32, n_layers=2, act_fn="Tanh", dropout=0.5)
# device = torch.device("cuda:0" if torch.cuda.is_available()
# else "cpu")
# model.load_state_dict(torch.load("best_model.pth",
# map_location=device))
# custom_force = TorchCustomForce(model)
# sim = Simulation(
# initial_state=system.hoomd_snapshot,
# forcefield=[custom_force],
# )
# sim.run_NVT(n_steps=10, kT=1.0, tau_kt=1.0)

0 comments on commit 99be3c6

Please sign in to comment.