forked from BlauGroup/HiPRGen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
634 lines (488 loc) · 19 KB
/
test.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
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
import os
import sys
import subprocess
import sqlite3
import pickle
from HiPRGen.network_loader import NetworkLoader
from HiPRGen.initial_state import find_mol_entry_from_xyz_and_charge
from monty.serialization import loadfn, dumpfn
from HiPRGen.species_filter import species_filter
from HiPRGen.bucketing import bucket
from HiPRGen.report_generator import ReportGenerator
from HiPRGen.initial_state import insert_initial_state
from HiPRGen.constants import ROOM_TEMP, Terminal
from HiPRGen.reaction_filter_payloads import (
DispatcherPayload,
WorkerPayload
)
from HiPRGen.species_questions import (
mg_species_decision_tree,
li_species_decision_tree,
positive_penalty,
species_default_true
)
from HiPRGen.reaction_questions import (
default_reaction_decision_tree,
)
from HiPRGen.mc_analysis import (
reaction_tally_report,
species_report,
Pathfinding,
SimulationReplayer,
generate_pathway_report,
sink_report,
consumption_report,
redox_report,
coordination_report,
decoordination_report
)
# Since HiPRGen uses an end-to-end testing approach rather than testing
# each individual function, we have decided to use the tests as
# documentation, by explaining every single line through the first test.
# The first thing you need to consider when using HiPRGen is how many
# worker threads do you want to run. HiPRGen can be run with a single
# thread or thousands distrubuted across several nodes. For reaction
# networks with between ~5000 and ~10000 species, we have found that the
# optimal number of worker threads is between 1000 and 2000. If you try
# and use more than that, the worker threads are going to spend lots of
# time waiting for the dispatcher to get through all of the reactions it
# is being sent, which slows everything down. Fixing this would require
# a more complex distrubuted system, but it hasn't been an issue even
# for very large reaction networks.
if len(sys.argv) != 2:
print("usage: python test.py number_of_threads")
quit()
number_of_threads = sys.argv[1]
class bcolors:
PASS = '\u001b[32;1m'
FAIL = '\u001b[31;1m'
ENDC = '\u001b[0m'
# HiPRGen is organized as a pipeline, where all the relevent data is
# stored in a sqlite database between phases. For this reason, during
# a single run of the full pipeline, it makes sense to store all the
# relevent files in a single directory. We have two test sets, a lithium
# set and a magnesium set. Since the lithium test set is older, we shall
# document that instead of the mg test set.
if os.path.isdir('./scratch'):
subprocess.run(['rm', '-r', './scratch'])
subprocess.run(['mkdir', './scratch'])
def li_test():
# folder is the where we store all our intermediate databases
folder = './scratch/li_test'
subprocess.run(['mkdir', folder ])
# The initial input to the pipeline is a list of LIBE or MADEIRA
# dataset entries. We provide two examples in the data foloder.
mol_json = './data/ronald_LIBE.json'
database_entries = loadfn(mol_json)
# The first step of the HiPRGen pipeline is passing the input molecules
# through the species decision tree to discard molecules.
species_decision_tree = li_species_decision_tree
# There is one non-local part of species filtering: we consider two
# molecules to be equivalent if they have the same total charge,
# composition, and covalent bonds, even if they have different metal
# coordination, and we choose one such molecule in each "coordimer"
# class using the coodimer weight function. Since most of our logging
# later on is defined in terms of a fixed molecule set, logging for
# the species filtering phase is messy, so ignore the species_report
# argument for now. The second argument is where we store a pickle of
# the filtered molecule entries for use in later phases.
mol_entries = species_filter(
database_entries,
mol_entries_pickle_location=folder + '/mol_entries.pickle',
species_report=folder + '/unfiltered_species_report.tex',
species_decision_tree=species_decision_tree,
coordimer_weight=lambda mol: (mol.penalty, mol.solvation_free_energy),
)
# Once we have generated our molecule list, we generate the bucket database
# which is how we break up the reaction filtering amongst all avaliable workers.
# It gets stored in the buckets.sqlite database.
bucket(mol_entries, folder + '/buckets.sqlite')
# Reaction filtering is paralellized using MPI, so we need to spawn
# an MPI instance to run it. This is why we can't just start
# reaction filtering by calling a python function. We pass the
# reaction decision tree, the logging decision tree, and the electron
# free energy as strings across this barrier. Every possible
# reaction gets passed through both the reaction decision tree and
# the logging decision tree. If a reaction passes the reaction
# decision tree, it gets written to the network. If a reaction
# passes the logging decision tree, it gets logged to the reaction
# report along with what happened to it in reaction_decision_tree.
# The reaction decision trees are constructed in
# HiPRGen.reaction_questions
params = {
'temperature' : ROOM_TEMP,
'electron_free_energy' : -1.4
}
dispatcher_payload = DispatcherPayload(
folder + '/buckets.sqlite',
folder + '/rn.sqlite',
folder + '/reaction_report.tex'
)
worker_payload = WorkerPayload(
folder + '/buckets.sqlite',
default_reaction_decision_tree,
params,
Terminal.DISCARD
)
# The dispatcher and worker payloads are passed through the MPI barrier
# as JSON blobs dispatcher_payload and worker_payload
dumpfn(dispatcher_payload, folder + '/dispatcher_payload.json')
dumpfn(worker_payload, folder + '/worker_payload.json')
subprocess.run(
[
'mpirun',
'--use-hwthread-cpus',
'-n',
number_of_threads,
'python',
'run_network_generation.py',
folder + '/mol_entries.pickle',
folder + '/dispatcher_payload.json',
folder + '/worker_payload.json'
]
)
# After we have generated the mol_entries, we refer to molecules by
# their index. The function find_mol_entry_from_xyz_and_charge can
# help find the indices of specific species to be used in the initial
# condition for propagating trajectories and/or trajectory analysis.
Li_plus_id = find_mol_entry_from_xyz_and_charge(
mol_entries,
'./xyz_files/Li.xyz',
1)
EC_id = find_mol_entry_from_xyz_and_charge(
mol_entries,
'./xyz_files/EC.xyz',
0)
LEDC_id = find_mol_entry_from_xyz_and_charge(
mol_entries,
'./xyz_files/LEDC.xyz',
0)
# After generating a reaction network, it is stored in rn.sqlite. We
# use Monte Carlo simulation to interrogate the network, and for that
# we need to define an initial condition.
initial_state = {
Li_plus_id : 30,
EC_id : 30
}
# The initial state and the trajectories (after simulation) are stored in
# a seperate database from the network, here called initial_state.sqlite.
# This facilitates running multiple independent simulations of the same
# network with different initial conditions at the same time, if desired.
insert_initial_state(initial_state, mol_entries, folder + '/initial_state.sqlite')
# GMC is a high performance reaction network Monte Carlo simulator using the
# Gillespie algorithm: https://github.com/BlauGroup/RNMC. Here we run 1000
# trajectories each of 200 steps.
subprocess.run([
'GMC',
'--reaction_database=' + folder + '/rn.sqlite',
'--initial_state_database=' + folder + '/initial_state.sqlite',
'--number_of_simulations=1000',
'--base_seed=1000',
'--thread_count=' + number_of_threads,
'--step_cutoff=200'
])
# The network loader builds a python object around a reaction network
# and the molecules to make it easier to use them.
network_loader = NetworkLoader(
folder + '/rn.sqlite',
folder + '/mol_entries.pickle',
folder + '/initial_state.sqlite'
)
network_loader.load_trajectories()
network_loader.load_initial_state()
# HiPRGen has analysis tools to understand what happened in our simulation.
# The output files are written into the same folder in which the reaction
# network is stored.
# This report is empty, but we use it to generate the molecule pictures.
# This is an expensive operation, so we only want do do it once.
report_generator = ReportGenerator(
network_loader.mol_entries,
folder + '/dummy.tex',
rebuild_mol_pictures=True)
# The tally report shows reactions sorted by the number of times fired.
reaction_tally_report(
network_loader,
folder + '/reaction_tally.tex'
)
# Run `pdflatex reaction_tally.tex` in `scratch/li_test` to generate
# the tally report PDF.
# The species report shows every specie in the network and their IDs.
species_report(network_loader, folder + '/species_report.tex')
# Run `pdflatex species_report.tex` in `scratch/li_test` to generate
# the species report PDF.
# Pathfinding is a central goal of HiPRGen / GMC. See mc_analysis.py for
# further documentation of the Pathfinding class.
pathfinding = Pathfinding(network_loader)
# The pathway report shows all the ways that a target species was
# produced in the simulation trajectories, where each simulation only
# contributes the shortest path responsible for the first formation
# of the target species to the report. The report can be sorted by
# pathway frequency, but instead here we sort by pathway cost. Note
# that the test network has ~5000 reactions while production networks
# have between 50-100 million reactions.
generate_pathway_report(
pathfinding,
LEDC_id,
folder + '/LEDC_pathways.tex',
sort_by_frequency=False
)
# Run `pdflatex LEDC_pathways.tex` in `scratch/li_test` to generate
# the LEDC pathway report PDF.
# The simulation replayer sweeps through all trajectories in order
# to extract additional information that is used for consumption
# reports and sink reports.
simulation_replayer = SimulationReplayer(network_loader)
# The consumption report shows reactions which consumed a target
# species, sorted by the number of times the reaction fired.
consumption_report(simulation_replayer,
LEDC_id,
folder + '/LEDC_consumption_report.tex')
# Run `pdflatex LEDC_consumption_report.tex` in `scratch/li_test`
# to generate the LEDC consumption report PDF.
# The sink report shows species which have a production to
# consumption ratio of greater than 3/2 and which have an expected
# value above 0.1. These are two of the three heuristic criteria
# that we use to identify network products. The third criteria is
# that each network product must have a shortest path with cost
# less than 10. This can be checked by generating pathway reports
# to each species shown in the sink report. For the curious reader,
# we note that generating pathway reports to the six species in the
# sink report will show that only Li2CO3, C2H4, LiEDC-, and DLEMC
# have sufficiently low-cost paths to pass the third criteria and
# thus to be considered products of the test network used here.
sink_report(simulation_replayer, folder + '/sink_report.tex')
# Run `pdflatex sink_report.tex` in `scratch/li_test` to generate
# the sink report PDF.
tests_passed = True
if network_loader.number_of_species == 190:
print(bcolors.PASS +
"li_test: correct number of species" +
bcolors.ENDC)
else:
print(bcolors.FAIL +
"li_test: correct number of species" +
bcolors.ENDC)
tests_passed = False
if network_loader.number_of_reactions == 4921:
print(bcolors.PASS +
"li_test: correct number of reactions" +
bcolors.ENDC)
else:
print(bcolors.FAIL +
"li_test: correct number of reactions" +
bcolors.ENDC)
tests_passed = False
return tests_passed
def mg_test():
folder = './scratch/mg_test'
subprocess.run(['mkdir', folder ])
mol_json = './data/sam_G2.json'
species_decision_tree = mg_species_decision_tree
database_entries = loadfn(mol_json)
mol_entries = species_filter(
database_entries,
folder + '/mol_entries.pickle',
folder + '/unfiltered_species_report.tex',
species_decision_tree,
coordimer_weight=lambda mol: (mol.penalty, mol.solvation_free_energy)
)
bucket(mol_entries, folder + '/buckets.sqlite')
dispatcher_payload = DispatcherPayload(
folder + '/buckets.sqlite',
folder + '/rn.sqlite',
folder + '/reaction_report.tex'
)
worker_payload = WorkerPayload(
folder + '/buckets.sqlite',
default_reaction_decision_tree,
{
'temperature' : ROOM_TEMP,
'electron_free_energy' : -2.06
},
Terminal.DISCARD
)
dumpfn(dispatcher_payload, folder + '/dispatcher_payload.json')
dumpfn(worker_payload, folder + '/worker_payload.json')
subprocess.run(
[
'mpiexec',
'--use-hwthread-cpus',
'-n',
number_of_threads,
'python',
'run_network_generation.py',
folder + '/mol_entries.pickle',
folder + '/dispatcher_payload.json',
folder + '/worker_payload.json'
]
)
mg_g2_plus_plus_id = find_mol_entry_from_xyz_and_charge(
mol_entries,
'./xyz_files/mgg2.xyz',
2)
c2h4_id = find_mol_entry_from_xyz_and_charge(
mol_entries,
'./xyz_files/c2h4.xyz',
0)
c2h6_id = find_mol_entry_from_xyz_and_charge(
mol_entries,
'./xyz_files/c2h6.xyz',
0)
initial_state = {
33 : 30,
81 : 30
}
insert_initial_state(initial_state, mol_entries, folder + '/initial_state.sqlite')
subprocess.run([
'GMC',
'--reaction_database=' + folder + '/rn.sqlite',
'--initial_state_database=' + folder + '/initial_state.sqlite',
'--number_of_simulations=1000',
'--base_seed=1000',
'--thread_count=' + number_of_threads,
'--step_cutoff=200'
])
network_loader = NetworkLoader(
folder + '/rn.sqlite',
folder + '/mol_entries.pickle',
folder + '/initial_state.sqlite'
)
network_loader.load_trajectories()
network_loader.load_initial_state()
report_generator = ReportGenerator(
network_loader.mol_entries,
folder + '/dummy.tex',
rebuild_mol_pictures=True)
reaction_tally_report(
network_loader,
folder + '/reaction_tally.tex'
)
pathfinding = Pathfinding(network_loader)
generate_pathway_report(
pathfinding,
c2h6_id,
folder + '/C2H6_pathways.tex',
sort_by_frequency=False
)
generate_pathway_report(
pathfinding,
c2h4_id,
folder + '/C2H4_pathways.tex',
sort_by_frequency=False
)
species_report(network_loader, folder + '/species_report.tex')
tests_passed = True
if network_loader.number_of_species == 83:
print(bcolors.PASS +
"mg_test: correct number of species" +
bcolors.ENDC)
else:
print(bcolors.FAIL +
"mg_test: correct number of species" +
bcolors.ENDC)
tests_passed = False
if network_loader.number_of_reactions == 788:
print(bcolors.PASS +
"mg_test: correct number of reactions" +
bcolors.ENDC)
else:
print(bcolors.FAIL +
"mg_test: correct number of reactions" +
bcolors.ENDC)
tests_passed = False
return tests_passed
def flicho_test():
folder = './scratch/flicho_test'
subprocess.run(['mkdir', folder ])
mol_json = './data/flicho_test.json'
database_entries = loadfn(mol_json)
species_decision_tree = li_species_decision_tree
mol_entries = species_filter(
database_entries,
mol_entries_pickle_location=folder + '/mol_entries.pickle',
species_report=folder + '/unfiltered_species_report.tex',
species_decision_tree=species_decision_tree,
coordimer_weight=lambda mol: (mol.penalty, mol.solvation_free_energy),
)
bucket(mol_entries, folder + '/buckets.sqlite')
params = {
'temperature' : ROOM_TEMP,
'electron_free_energy' : -1.4
}
dispatcher_payload = DispatcherPayload(
folder + '/buckets.sqlite',
folder + '/rn.sqlite',
folder + '/reaction_report.tex'
)
worker_payload = WorkerPayload(
folder + '/buckets.sqlite',
default_reaction_decision_tree,
params,
Terminal.DISCARD
)
dumpfn(dispatcher_payload, folder + '/dispatcher_payload.json')
dumpfn(worker_payload, folder + '/worker_payload.json')
subprocess.run(
[
'mpirun',
'--use-hwthread-cpus',
'-n',
number_of_threads,
'python',
'run_network_generation.py',
folder + '/mol_entries.pickle',
folder + '/dispatcher_payload.json',
folder + '/worker_payload.json'
]
)
Li_plus_id = find_mol_entry_from_xyz_and_charge(
mol_entries,
'./xyz_files/Li.xyz',
1)
EC_id = find_mol_entry_from_xyz_and_charge(
mol_entries,
'./xyz_files/EC.xyz',
0)
initial_state = {
Li_plus_id : 30,
EC_id : 30
}
insert_initial_state(initial_state, mol_entries, folder + '/initial_state.sqlite')
subprocess.run([
'GMC',
'--reaction_database=' + folder + '/rn.sqlite',
'--initial_state_database=' + folder + '/initial_state.sqlite',
'--number_of_simulations=1000',
'--base_seed=1000',
'--thread_count=' + number_of_threads,
'--step_cutoff=200'
])
network_loader = NetworkLoader(
folder + '/rn.sqlite',
folder + '/mol_entries.pickle',
folder + '/initial_state.sqlite'
)
network_loader.load_trajectories()
network_loader.load_initial_state()
report_generator = ReportGenerator(
network_loader.mol_entries,
folder + '/dummy.tex',
rebuild_mol_pictures=True)
coordination_report(
network_loader,
folder + '/coodination_report.tex',
'Li1',
1)
decoordination_report(
network_loader,
folder + '/decoodination_report.tex',
'Li1',
1)
tests = [
mg_test,
li_test,
# flicho_test
]
for test in tests:
if not test():
exit(1)