3
3
import itertools as it
4
4
from collections import defaultdict
5
5
6
- from cmeutils .geometry import angle_between_vectors
7
- from mbuild import Compound , Particle , load
6
+ import gsd .hoomd
8
7
import mbuild as mb
9
8
import numpy as np
10
- import gsd .hoomd
9
+ from cmeutils .geometry import angle_between_vectors
10
+ from mbuild import Compound , Particle , load
11
11
12
12
from grits .utils import align , get_hydrogen , get_index , reactant_dict
13
13
14
+
14
15
def backmap_snapshot_to_compound (
15
16
snapshot ,
16
17
bead_mapping = None ,
@@ -20,7 +21,7 @@ def backmap_snapshot_to_compound(
20
21
ref_distance = None ,
21
22
energy_minimize = False
22
23
):
23
- #TODO
24
+ # TODO
24
25
# assert all 3 dicts have the same keys
25
26
if (bead_mapping is None and bond_head_index is None and bond_tail_index is None ) == (library_key is None ):
26
27
raise ValueError (
@@ -37,12 +38,11 @@ def backmap_snapshot_to_compound(
37
38
box = cg_snap .configuration .box * ref_distance
38
39
pos_adjust = np .array ([box [0 ] / 2 , box [1 ] / 2 , box [2 ] / 2 ])
39
40
mb_box = mb .box .Box .from_lengths_angles (
40
- lengths = [box [0 ], box [1 ], box [2 ]],
41
- angles = [90.0 , 90.0 , 90.0 ]
41
+ lengths = [box [0 ], box [1 ], box [2 ]], angles = [90.0 , 90.0 , 90.0 ]
42
42
)
43
43
fg_compound .box = mb_box
44
44
# Create atomistic compounds, remove hydrogens in the way of bonds
45
- compounds = dict ()
45
+ compounds = dict ()
46
46
anchor_dict = dict ()
47
47
for mapping in bead_mapping :
48
48
comp = mb .load (bead_mapping [mapping ], smiles = True )
@@ -68,9 +68,9 @@ def backmap_snapshot_to_compound(
68
68
for i in [bond_tail_index [mapping ][0 ], bond_head_index [mapping ][0 ]]:
69
69
for j , particle in enumerate (comp .particles ()):
70
70
if j == i :
71
- remove_atoms .append (particle )
71
+ remove_atoms .append (particle )
72
72
anchor = [p for p in particle .direct_bonds ()][0 ]
73
- anchor_particles .append (anchor )
73
+ anchor_particles .append (anchor )
74
74
for particle in remove_atoms :
75
75
comp .remove (particle )
76
76
# List of length 2 [tail particle index, head particle index]
@@ -88,46 +88,48 @@ def backmap_snapshot_to_compound(
88
88
mb_compounds = []
89
89
for group in cg_snap .bonds .group :
90
90
cg_bond_vec = (
91
- cg_snap .particles .position [group [1 ]] -
92
- cg_snap .particles .position [group [0 ]]
91
+ cg_snap .particles .position [group [1 ]]
92
+ - cg_snap .particles .position [group [0 ]]
93
93
)
94
94
cg_bond_vec = cg_bond_vec / np .linalg .norm (cg_bond_vec )
95
95
for bead_index in group :
96
96
if bead_index not in finished_beads :
97
97
bead_type = cg_snap .particles .types [
98
- cg_snap .particles .typeid [bead_index ]
98
+ cg_snap .particles .typeid [bead_index ]
99
99
]
100
100
bead_pos = cg_snap .particles .position [bead_index ] * ref_distance
101
101
comp = mb .clone (compounds [bead_type ])
102
102
tail_pos = comp [anchor_particle_indices [1 ]].xyz [0 ]
103
103
head_pos = comp [anchor_particle_indices [0 ]].xyz [0 ]
104
- head_tail_vec = tail_pos - head_pos
104
+ head_tail_vec = tail_pos - head_pos
105
105
head_tail_vec = head_tail_vec / np .linalg .norm (head_tail_vec )
106
106
normal_vec = np .cross (head_tail_vec , cg_bond_vec )
107
107
angle = angle_between_vectors (
108
- head_tail_vec ,
109
- cg_bond_vec ,
110
- degrees = False
111
- )
108
+ head_tail_vec , cg_bond_vec , degrees = False
109
+ )
112
110
comp .rotate (around = normal_vec , theta = angle )
113
111
comp .translate_to (bead_pos + pos_adjust )
114
112
mb_compounds .append (comp )
115
113
bead_to_comp_dict [bead_index ] = comp
116
114
finished_beads .add (bead_index )
117
115
fg_compound .add (comp )
118
-
116
+
119
117
tail_comp = bead_to_comp_dict [group [0 ]]
120
118
tail_comp_particle = tail_comp [anchor_particle_indices [1 ]]
121
119
head_comp = bead_to_comp_dict [group [1 ]]
122
120
head_comp_particle = head_comp [anchor_particle_indices [0 ]]
123
- fg_compound .add_bond (particle_pair = [tail_comp_particle , head_comp_particle ])
121
+ fg_compound .add_bond (
122
+ particle_pair = [tail_comp_particle , head_comp_particle ]
123
+ )
124
124
if energy_minimize :
125
125
temp_head = mb .clone (head_comp )
126
126
temp_tail = mb .clone (tail_comp )
127
127
temp_comp = mb .Compound (subcompounds = [temp_tail , temp_head ])
128
128
tail_comp_particle = temp_tail [anchor_particle_indices [1 ]]
129
129
head_comp_particle = temp_head [anchor_particle_indices [0 ]]
130
- temp_comp .add_bond (particle_pair = [tail_comp_particle , head_comp_particle ])
130
+ temp_comp .add_bond (
131
+ particle_pair = [tail_comp_particle , head_comp_particle ]
132
+ )
131
133
print ("Running energy minimization" )
132
134
temp_comp .energy_minimize (steps = 500 )
133
135
temp_tail = temp_comp .children [0 ]
@@ -139,8 +141,6 @@ def backmap_snapshot_to_compound(
139
141
return fg_compound
140
142
141
143
142
-
143
-
144
144
def backmap_compound (cg_compound ):
145
145
"""Backmap a fine-grained representation onto a coarse one.
146
146
0 commit comments