-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbuilder_ligpargen_openmm_from_molality_multi_system.py
More file actions
404 lines (369 loc) · 11.9 KB
/
builder_ligpargen_openmm_from_molality_multi_system.py
File metadata and controls
404 lines (369 loc) · 11.9 KB
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
import argparse
import os
import ast
import numpy as np
from hitpoly.writers.box_builder import *
from hitpoly.utils.building_utils import salt_string_to_values, get_concentraiton_from_molality_multi_system
from hitpoly.simulations.openmm_scripts import (
equilibrate_system_1,
equilibrate_system_2,
prod_run_nvt,
prod_run_tg,
write_analysis_script,
equilibrate_system_liquid1,
equilibrate_system_liquid2,
)
import sys
sys.setrecursionlimit(5000)
def run(
save_path: str,
results_path: str,
smiles:list,
charge_scale:float,
salt_type:str,
molality:float,
charges:str,
system:str,
simu_temp:float,
atom_count:int,
ratios:list,
ratios_type:str,
simu_length:int,
md_save_time:int,
hitpoly_path:str,
timestep:float=0.002,
platform:str='local',
polymer_chain_length:int=None,
simu_type="conductivity",
htvs_env='htvs',
):
"""Run the MD simulation."""
cuda_device = "0"
packmol_path = os.environ["packmol"]
if not hitpoly_path:
hitpoly_path = f"{os.path.expanduser('~')}/HiTPoly"
if salt_type:
salt_smiles, salt_paths, salt_data_paths, ani_name_rdf, concentration = salt_string_to_values(
hitpoly_path, salt_type, 0)
salt = True
else:
salt = False
salt_paths = []
salt_data_paths = []
salt_smiles = []
ani_name_rdf = None
filename_list = []
long_smiles_list = []
atom_names_short_list = []
atom_names_long_list = []
atoms_short_list = []
atoms_long_list = []
param_dict_list = []
concentration, solvent_count, repeats, weight_prcnt, total_atoms, poly_name = get_concentraiton_from_molality_multi_system(
smiles=smiles,
molality=molality,
system=system,
atom_count=atom_count,
polymer_chain_length=polymer_chain_length,
ratios=ratios,
ratios_type=ratios_type,
salt_smiles='.'.join(salt_smiles),
)
with open(f"{save_path}/repeats.txt", "w") as f:
f.write(str(repeats))
for ind, i in enumerate(smiles):
if len(smiles) == 1:
extra_name = ""
name = "polymer_conformation.pdb"
filename_list.append(name)
else:
extra_name = f"_{ind}"
name = f"polymer_conformation{extra_name}.pdb"
filename_list.append(name)
ligpargen_path = f"{save_path}/ligpargen{extra_name}"
print(f"ligpargen path: {ligpargen_path}")
if not os.path.isdir(ligpargen_path):
os.makedirs(ligpargen_path)
long_smiles, _ = create_long_smiles(
i,
repeats=repeats[ind],
add_end_Cs=True,
)
long_smiles_list.append(long_smiles)
mol_long = Chem.MolFromSmiles(long_smiles)
mol_long = Chem.AddHs(mol_long)
r_long, atom_names_long, atoms_long, bonds_typed_long = generate_atom_types(
mol_long, 2
)
atom_names_long_list.append(atom_names_long)
atoms_long_list.append(atoms_long)
(
ligpargen_repeats,
smiles_initial,
mol_initial,
r,
atom_names,
atoms,
bonds_typed,
) = create_ligpargen_short_polymer(
i,
add_end_Cs=True,
reaction="[Cu][*:1].[*:2][Au]>>[*:1]-[*:2]",
product_index=0,
atom_names_long=atom_names_long,
)
mol_initial, _ = create_ligpargen(
smiles=i,
repeats=ligpargen_repeats,
add_end_Cs=True,
ligpargen_path=ligpargen_path,
hitpoly_path=hitpoly_path,
platform=platform,
)
print(f"Created ligpargen files at {ligpargen_path}")
r, atom_names, atoms, bonds_typed = generate_atom_types(mol_initial, 2)
atom_names_short_list.append(atom_names)
atoms_short_list.append(atoms)
param_dict = generate_parameter_dict(ligpargen_path, atom_names, atoms, bonds_typed)
param_dict_list.append(param_dict)
minimize = create_conformer_pdb(
save_path,
long_smiles,
name=name,
)
# minimize = False
print(f"Saved conformer pdb.")
if minimize:
minimize_polymer(
save_path=save_path,
long_smiles=long_smiles,
atoms_long=[atoms_long],
atoms_short=[atoms],
atom_names_short=[atom_names],
atom_names_long=[atom_names_long],
param_dict=[param_dict],
lit_charges_save_path=None,
charges=charges,
name=name,
cuda_device=cuda_device,
)
if system == "gel":
box_multiplier = 0.2
elif system == "liquid":
box_multiplier = 10
elif system == "polymer":
box_multiplier = 1
else:
raise ValueError("System must be either gel, liquid or polymer")
create_box_and_ff_files_openmm(
save_path=save_path,
long_smiles=long_smiles_list,
filename=filename_list,
concentration=concentration,
solvent_count=solvent_count,
packmol_path=packmol_path,
atoms_long=atoms_long_list,
atoms_short=atoms_short_list,
atom_names_short=atom_names_short_list,
atom_names_long=atom_names_long_list,
param_dict=param_dict_list,
lit_charges_save_path=None,
charges=charges,
charge_scale=charge_scale,
salt_smiles=salt_smiles,
salt_paths=salt_paths,
salt_data_paths=salt_data_paths,
box_multiplier=box_multiplier,
salt=salt,
)
final_save_path = f"{save_path}/openmm_saver"
if not os.path.isdir(final_save_path):
os.makedirs(final_save_path)
if system == "polymer" or system == "gel":
equilibrate_system_1(
save_path=save_path,
final_save_path=final_save_path,
cuda_device=cuda_device,
)
equilibrate_system_2(
save_path=save_path,
final_save_path=final_save_path,
cuda_device=cuda_device,
)
else:
equilibrate_system_liquid1(
save_path=save_path,
final_save_path=final_save_path,
simu_temp=simu_temp,
cuda_device=cuda_device,
)
equilibrate_system_liquid2(
save_path=save_path,
final_save_path=final_save_path,
simu_temp=simu_temp,
cuda_device=cuda_device,
)
if simu_type == "conductivity":
prod_run_nvt(
save_path=save_path,
final_save_path=final_save_path,
simu_temp=simu_temp,
mdOutputTime=md_save_time,
simu_time=simu_length,
cuda_device=cuda_device,
timestep=timestep,
)
write_analysis_script(
save_path=save_path,
results_path=results_path,
platform=platform,
repeat_units=repeats,
cation=salt_type.split(".")[0],
anion=ani_name_rdf.split(",")[0],
simu_temperature=simu_temp,
prod_run_time=simu_length,
ani_name_rdf=ani_name_rdf,
poly_name=','.join(poly_name),
hitpoly_path=hitpoly_path,
htvs_env=htvs_env,
xyz_output=int(md_save_time*timestep),
)
elif simu_type.lower() == "tg":
prod_run_tg(
save_path=save_path,
final_save_path=final_save_path,
simu_time=10,
start_temperature=500,
end_temperature=100,
temperature_step=20,
)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Boxbuilder for OpenMM simulations")
parser.add_argument(
"-p", "--save_path", help="Path to the where the new directory to be created"
)
parser.add_argument(
"-pr",
"--results_path",
help="Path to the where the new directory to be created",
)
parser.add_argument(
"-s",
"--smiles_path",
help="Path of the file that contains the smiles strings to be created, if more than one, have them '.' separated, and the second line should be the mole fraction ratios of the smiles strings, ex, 'C1.C2.C3', '0.5,0.3,0.2'",
)
parser.add_argument(
"--salt_type",
help="Type of the salt to be added to the simulation",
default="Li.TFSI",
)
parser.add_argument(
"-M",
"--molality_salt",
help="Molality of salt in mol/kg",
default="1",
)
parser.add_argument(
"-cs",
"--charge_scale",
help="To what value the charges of the salts be scaled",
default="0.75",
)
parser.add_argument(
"-ct", "--charge_type", help="What type of charges to select", default="LPG"
)
parser.add_argument(
"-f",
"--hitpoly_path",
help="Path towards the HiTPoly folder",
default="None",
)
parser.add_argument("--temperature", help="Simulation temperature", default="430")
parser.add_argument("--simu_length", help="Simulation length, ns", default="100")
parser.add_argument(
"--platform", help="For which platform to build the files for", default="local"
)
parser.add_argument(
"-system",
"--system",
help="System to be used for the simulation",
default="polymer",
)
parser.add_argument(
"-atom_count",
"--atom_count",
help="Atom count of the system",
default="None",
)
parser.add_argument(
"-md_save_time",
"--md_save_time",
help="MD save time",
default="10000",
)
parser.add_argument(
"-polymer_chain_length",
"--polymer_chain_length",
help="Polymer chain length",
default="None",
)
parser.add_argument(
"--simu_type",
help="What type of simulation to perform, options [conductivity, tg]}",
default="conductivity",
)
parser.add_argument(
"--htvs_env",
help="HTVS environment",
default="htvs",
)
args = parser.parse_args()
if args.hitpoly_path == "None":
args.hitpoly_path = None
# The file from which the smiles strings are read should have the following format:
# [smiles_string_1].[smiles_string_2].[smiles_string_3]
# [ratio_1],[ratio_2],[ratio_3]
# [ratios_type] # either 'mol' or 'weight', default is 'mol'
with open(args.smiles_path, "r") as f:
lines = f.readlines()
smiles = lines[0].split(".")
if len(smiles) > 1:
ratios = np.array(ast.literal_eval(lines[1]))
ratios = (ratios / ratios.sum()).tolist()
if len(lines) > 2:
ratios_type = lines[2].strip()
else:
ratios_type = 'mol'
assert len(smiles) == len(ratios)
else:
ratios = None
ratios_type = None
if args.atom_count == "None":
atom_count = None
else:
atom_count = int(args.atom_count)
if args.polymer_chain_length == "None":
polymer_chain_length = None
else:
polymer_chain_length = int(args.polymer_chain_length)
run(
save_path=args.save_path,
results_path=args.results_path,
smiles=smiles,
charge_scale=float(args.charge_scale),
salt_type=args.salt_type,
molality=float(args.molality_salt),
charges=args.charge_type,
system=args.system,
atom_count=atom_count,
md_save_time=int(args.md_save_time),
simu_temp=int(args.temperature),
simu_length=int(args.simu_length),
platform=args.platform,
polymer_chain_length=polymer_chain_length,
ratios=ratios,
ratios_type=ratios_type,
hitpoly_path=args.hitpoly_path,
simu_type=args.simu_type,
htvs_env=args.htvs_env,
)