-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfree_energy.py
270 lines (231 loc) · 11.5 KB
/
free_energy.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import os
from pathlib import Path
import random
import lammpsio
from utils import *
import re
from templates import *
def write_non_bonded_lj(mof,out_dir, mol_name):
if mol_name is not None:
pairij = read_param(f'{out_dir}/data.emin_{mof}_{mol_name}')['PairIJ Coeffs']
else:
pairij = read_param(f'{out_dir}/data.emin_{mof}')['PairIJ Coeffs']
pairij_pairs = [entry[:-1] for entry in pairij]
with open(f'{out_dir}/emin_{mof}.param', 'r') as f:
lines = f.readlines()
filtered_lines = [line for line in lines if not line.startswith('pair_coeff')]
cross_term_lines=[f"pair_coeff {' '.join([f'{value}' for value in entry])} 1 12.5\n" for entry in pairij_pairs]
filtered_lines.extend(cross_term_lines)
with open(f'{out_dir}/non_bonded_{mof}_lj.param', 'w') as f:
f.writelines(filtered_lines)
def write_non_bonded(mof, out_dir, mol_name):
if mol_name is not None:
pairij = read_param(f'{out_dir}/data.emin_{mof}_{mol_name}')['PairIJ Coeffs']
else:
pairij = read_param(f'{out_dir}/data.emin_{mof}')['PairIJ Coeffs']
pairij_pairs = [entry[:-1] for entry in pairij]
with open(f'{out_dir}/emin_{mof}.param', 'r') as f:
lines = f.readlines()
filtered_lines = [line for line in lines if not line.startswith('pair_coeff')]
cross_term_lines=[f"pair_coeff {' '.join([f'{value}' for value in entry])}\n" for entry in pairij_pairs]
filtered_lines.extend(cross_term_lines)
with open(f'{out_dir}/non_bonded_{mof}.param', 'w') as f:
f.writelines(filtered_lines)
def is_number(s):
try:
float(s)
return True
except ValueError:
return False
def process_coeff_line(line, var_prefix):
parts = line.split()
if is_number(parts[2]):
energy_index = 2
else:
energy_index = 3
energy_term = parts[energy_index]
var_name = f'{var_prefix}{parts[1]}'
variable_assignment = f'variable {var_name} equal ${{lambda}}*{energy_term}'
parts[energy_index] = f'${{{var_name}}}'
modified_line = ' '.join(parts)
return variable_assignment, modified_line
def write_bonded(mof,out_dir):
input_filename = f'{out_dir}/emin_{mof}.param'
output_filename = f'{out_dir}/bonded_{mof}.param'
with open(input_filename, 'r') as file:
lines = file.readlines()
output_lines = []
in_mof_section = False
var_prefixes = {
'bond_coeff': 'b',
'angle_coeff': 'a',
'dihedral_coeff': 'd',
'improper_coeff': 'i'
}
for line in lines:
line = line.strip()
if "MOF" in line:
in_mof_section = True
elif "Molecule" in line:
in_mof_section = False
if line.startswith('pair_coeff'):
continue
if in_mof_section and line.startswith(('bond_coeff', 'angle_coeff', 'dihedral_coeff', 'improper_coeff')):
coeff_type = line.split()[0]
var_prefix = var_prefixes[coeff_type]
variable_assignment, modified_line = process_coeff_line(line, var_prefix)
output_lines.append(variable_assignment)
output_lines.append(modified_line)
else:
output_lines.append(line)
output_lines.append('pair_coeff * *')
# Write the output file
with open(output_filename, 'w') as file:
for line in output_lines:
file.write(line + '\n')
def write_in_fe(mof, out_dir, mol_name=None, center_atom=None, temp=300, pressure=0.0):
## read the input file
mof_styles = read_styles(f'{out_dir}/in.{mof}')
if mol_name is not None:
mol_styles = read_styles(f'{out_dir}/in.{mol_name}')
merged_styles = merge_styles(mol_styles, mof_styles)
name = f'{mof}_{mol_name}'
else:
merged_styles = mof_styles
name = f'{mof}'
mof_param = read_param(f'{out_dir}/data.{mof}')
mof_atm_num = len(mof_param['Masses'])
if 'Bond Coeffs' in mof_param.keys():
mof_b_num = len(mof_param['Bond Coeffs'])
else:
mof_b_num = 0
if 'Angle Coeffs' in mof_param.keys():
mof_a_num = len(mof_param['Angle Coeffs'])
else:
mof_a_num = 0
if 'Dihedral Coeffs' in mof_param.keys():
mof_d_num = len(mof_param['Dihedral Coeffs'])
else:
mof_d_num = 0
if 'Improper Coeffs' in mof_param.keys():
mof_im_num = len(mof_param['Improper Coeffs'])
else:
mof_im_num = 0
if mol_name is not None:
mol_param = read_param(f'{out_dir}/data.{mol_name}')
mol_atm_num = len(mol_param['Masses'])
if 'Bond Coeffs' in mol_param.keys():
mol_b_num = len(mol_param['Bond Coeffs'])
else:
mol_b_num = 0
if 'Angle Coeffs' in mol_param.keys():
mol_a_num = len(mol_param['Angle Coeffs'])
else:
mol_a_num = 0
if 'Dihedral Coeffs' in mol_param.keys():
mol_d_num = len(mol_param['Dihedral Coeffs'])
else:
mol_d_num = 0
if 'Improper Coeffs' in mol_param.keys():
mol_im_num = len(mol_param['Improper Coeffs'])
else:
mol_im_num = 0
total_atm_num = mof_atm_num + mol_atm_num
total_b_num = mof_b_num + mol_b_num
total_a_num = mof_a_num + mol_a_num
total_d_num = mof_d_num + mol_d_num
total_im_num = mof_im_num + mol_im_num
job=mol_name+'_fe'
modify_mof_data(f'{out_dir}/data.emin_{mof}_{mol_name}', total_atm_num, total_b_num, total_a_num, total_d_num, total_im_num, 'fe')
else:
total_atm_num = mof_atm_num
total_b_num = mof_b_num
total_a_num = mof_a_num
total_d_num = mof_d_num
total_im_num = mof_im_num
job='fe'
modify_mof_data(f'{out_dir}/data.emin_{mof}', total_atm_num, total_b_num, total_a_num, total_d_num, total_im_num, 'fe')
random_seed = random.randint(1, 100000)
os.makedirs(f'{out_dir}/bonded/', exist_ok=True)
with open(f'{out_dir}/bonded/in.BONDED', 'w') as f:
f.write(bonded_part1_template.format(seed = random_seed, mof = mof,
temp = temp, pressure = pressure,
pstyle = 'zero 12.5 nocoeff',
bstyle = merged_styles.get('bond_style'),
astyle = merged_styles.get('angle_style'),
dstyle = merged_styles.get('dihedral_style'),
istyle = merged_styles.get('improper_style'),
fram_atoms = mof_atm_num+1,
job=job
))
if mol_name is not None:
f.write(f'group cation type > {mof_atm_num}\n')
f.write(f'group center type {center_atom}\n')
f.write(f'fix SPRING2 center spring/self 1\n\n')
f.write(bonded_part2_template.format(mof=mof))
os.makedirs(f'{out_dir}/lj/', exist_ok=True)
with open(f'{out_dir}/lj/in.LJ', 'w') as f:
f.write(lj_part1_template.format(seed = random_seed, mof = mof,
temp = temp, pressure = pressure,
pstyle = 'lj/cut/soft 1 0.5 12.5',
bstyle = merged_styles.get('bond_style'),
astyle = merged_styles.get('angle_style'),
dstyle = merged_styles.get('dihedral_style'),
istyle = merged_styles.get('improper_style'),
fram_atoms = mof_atm_num+1,
job=job
))
if mol_name is not None:
f.write(f'group cation type > {mof_atm_num}\n')
f.write(f'group center type {center_atom}\n')
f.write(f'fix SPRING2 center spring/self 1\n\n')
f.write(lj_part2_template.format(mof=mof))
if 'coul' in merged_styles.get('pair_style'):
os.makedirs(f'{out_dir}/q/', exist_ok=True)
with open(f'{out_dir}/q/in.Q', 'w') as f:
f.write(q_part1_template.format(seed = random_seed, mof = mof,
temp = temp, pressure = pressure,
pstyle = merged_styles.get('pair_style'),
bstyle = merged_styles.get('bond_style'),
astyle = merged_styles.get('angle_style'),
dstyle = merged_styles.get('dihedral_style'),
istyle = merged_styles.get('improper_style'),
kstyle = merged_styles.get('kspace_style'),
fram_atoms = mof_atm_num+1,
job=job
))
if mol_name is not None:
f.write(f'group cation type > {mof_atm_num}\n')
f.write(f'group center type {center_atom}\n')
f.write(f'fix SPRING2 center spring/self 1\n\n')
f.write(q_part2_template.format(mof=mof))
os.makedirs(f'{out_dir}/hr/', exist_ok=True)
if mol_name is not None:
with open(f'{out_dir}/hr/in.HR', 'w') as f:
f.write(hr_with_mol_template.format(seed = random_seed, mof = mof,
temp = temp, pressure = pressure,
pstyle = merged_styles.get('pair_style'),
bstyle = merged_styles.get('bond_style'),
astyle = merged_styles.get('angle_style'),
dstyle = merged_styles.get('dihedral_style'),
istyle = merged_styles.get('improper_style'),
kstyle = merged_styles.get('kspace_style'),
fram_atoms = mof_atm_num+1,
cation_atoms=mof_atm_num,
center_atom=center_atom,
job=job
))
else:
with open(f'{out_dir}/hr/in.HR', 'w') as f:
f.write(hr_no_mol_template.format(seed = random_seed, mof = mof,
temp = temp, pressure = pressure,
pstyle = merged_styles.get('pair_style'),
bstyle = merged_styles.get('bond_style'),
astyle = merged_styles.get('angle_style'),
dstyle = merged_styles.get('dihedral_style'),
istyle = merged_styles.get('improper_style'),
kstyle = merged_styles.get('kspace_style'),
fram_atoms = mof_atm_num+1,
job=job
))
print('Inputs for Free energies has been generated.')