-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_test.py
1028 lines (922 loc) · 34.2 KB
/
run_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
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import asparagus
import os
import torch
import numpy as np
#==============================================================================
# Test Parameter
#==============================================================================
flag_dictionary_initialization = True
flag_database_sql = True
flag_database_npz = True
flag_database_hdf5 = True
flag_sampler_all = True
flag_sampler_shell = True
flag_sampler_slurm = False
flag_model_physnet = True
flag_train_physnet_sql = True
flag_train_physnet_npz = True
flag_ase_physnet = True
flag_model_painn = True
flag_train_painn = True
flag_transfer_learning = True
flag_train_cuda = False
#==============================================================================
# Test Asparagus Main Class Initialization
#==============================================================================
config_file = 'test/init.json'
config = {
'config_file': config_file}
device = 'cpu'
dtype = torch.float32
# Dictionary initialization
if flag_dictionary_initialization:
model = asparagus.Asparagus(config)
model = asparagus.Asparagus(config=config_file)
model = asparagus.Asparagus(config_file=config_file)
model = asparagus.Asparagus(
config,
model_device=device,
model_dtype=dtype)
model = asparagus.Asparagus(
config,
model_device=device,
model_dtype=dtype)
#==============================================================================
# Test Asparagus DataContainer Class Initialization
#==============================================================================
config_file = 'test/data.json'
config = {
'config_file': config_file}
# SQL
if flag_database_sql:
# Open DataBase file
model = asparagus.Asparagus(
config=config_file,
data_file='data/nms_nh3.db',
data_file_format='sql',
)
# Create new DataBase file
model.set_data_container(
config=config_file,
data_file='test/nms_nh3_test.db',
data_source='data/nms_nh3.db',
data_overwrite=True,
)
# Add same source to DataBase file, should be skipped
model.set_data_container(
config=config_file,
data_file='test/nms_nh3_test.db',
data_source='data/nms_nh3.db',
data_overwrite=False,
)
# Create new DataBase file with itself as source, should return error
try:
model.set_data_container(
config=config_file,
data_file='test/nms_nh3_test.db',
data_source='test/nms_nh3_test.db',
data_overwrite=True,
)
except SyntaxError:
print("\nSyntaxError as expected\n")
# Get DataContainer (Reset data source)
model = asparagus.Asparagus(
config=config_file,
data_file='test/nms_nh3_test.db',
data_file_format='sql',
data_source='data/nms_nh3.db',
)
data = model.get_data_container()
print("\nDatabase path: ", model.get_data_container(), "\n")
print("\nDatabase entry '0': ", data[0]['energy'])
print("\nDatabase Train entry '1': ", data.get_train(1)['atoms_number'])
print("\nDatabase Valid entry '2': ", data.get_valid(2)['cell'])
print("\nDatabase Valid entry '2': ", data.get_valid(2)['pbc'])
print("\nDatabase Test entry '3': ", data.get_test(3)['positions'])
print("\nDatabase Test entry '3': ", data.get_test(4)['forces'])
# Load Numpy .npz files
model.set_data_container(
config=config_file,
data_file='test/h2co_test.db',
data_source='data/h2co_B3LYP_cc-pVDZ_4001.npz',
data_overwrite=True,
)
data = model.get_data_container(data_file='test/h2co_test.db')
print("\nDatabase path: ", data, "\n")
print("\nDatabase entry '0': ", data[0]['energy'])
print("\nDatabase Train entry '1': ", data.get_train(1)['atomic_numbers'])
print("\nDatabase Valid entry '2': ", data.get_valid(2)['charge'])
print("\nDatabase Test entry '3': ", data.get_test(3)['pbc'])
# Load multiple source files files
model.set_data_container(
config=config_file,
data_file='test/nh3_h2co_test.db',
data_source=['data/h2co_B3LYP_cc-pVDZ_4001.npz', 'data/nms_nh3.db'],
data_overwrite=True,
)
# Check if repeated data source is skipped
model.set_data_container(
config=config_file,
data_file='test/nh3_h2co_test.db',
data_source=['data/nms_nh3.db'],
data_overwrite=False,
)
# Load ASE trajectory file
model.set_data_container(
config=config_file,
data_file='test/meta_nh3_test.db',
data_source='data/meta_nh3.traj',
data_overwrite=True,
)
# Load ASE trajectory file with different property units
model.set_data_container(
config=config_file,
data_file='test/meta_nh3_test_unit.db',
data_source='data/meta_nh3.traj',
data_load_properties=['energy', 'forces'],
data_unit_properties={
'positions': 'Bohr',
'energy': 'kcal/mol',
'forces': 'kcal/mol/Bohr'},
data_overwrite=True,
)
# Test training initialization
model.train(
trainer_max_epochs=0,
model_directory='test/test_model')
# Check automatic model property assignment from data properties
if os.path.exists(config_file):
os.remove(config_file)
model = asparagus.Asparagus(
config=config_file,
data_file='test/meta_nh3_test_unit.db',
data_source='data/meta_nh3.traj',
data_load_properties=['energy', 'forces'],
data_unit_properties={
'positions': 'Bohr',
'energy': 'kcal/mol',
'forces': 'kcal/mol/Bohr'},
data_overwrite=True,
)
# Test training initialization
model.train(
trainer_max_epochs=0,
model_directory='test/test_model')
# Numpy npz
if flag_database_npz:
model = asparagus.Asparagus(
config=config_file,
data_file='test/nms_nh3_test.db.npz',
data_file_format='npz',
data_source='data/nms_nh3.db',
data_source_format='db',
data_overwrite=True,
)
# Create new DataBase file
data = model.get_data_container(
config=config_file,
data_file='test/nms_nh3_test.db.npz',
data_file_format='npz',
data_source='data/nms_nh3.db',
data_source_format='db',
data_overwrite=False,
)
print("\nDatabase path: ", data, "\n")
print("\nDatabase entry '0': ", data[0]['energy'])
print("\nDatabase Train entry '1': ", data.get_train(1)['atoms_number'])
print("\nDatabase Valid entry '2': ", data.get_valid(2)['cell'])
print("\nDatabase Valid entry '2': ", data.get_valid(2)['pbc'])
print("\nDatabase Test entry '3': ", data.get_test(3)['positions'])
print("\nDatabase Test entry '3': ", data.get_test(4)['forces'])
# Test training initialization
model.train(
trainer_max_epochs=0,
model_directory='test/test_model')
# HDF5
if flag_database_hdf5:
# Create new DataBase file
model = asparagus.Asparagus(
config=config_file,
data_file='test/nms_nh3_test.db.h5',
data_file_format='hdf5',
data_source='data/nms_nh3.db',
data_source_format='sql',
data_overwrite=True,
)
data = model.get_data_container()
# Test training initialization
model.train(
trainer_max_epochs=0,
model_directory='test/test_model')
#==============================================================================
# Test Asparagus Sampler Methods
#==============================================================================
# Sampler - with XTB and ORCA
# Mind: XTB is not thread safe when using with ASE modules such as Optimizer
# or Vibrations, but simple Calculator call works
if flag_sampler_all:
from asparagus.sample import Sampler
# Load single system from xyz file and compute properties using XTB default
# calculator
sampler = Sampler(
config='test/smpl_nh3.json',
sample_directory='test',
sample_data_file='test/smpl_nh3.db',
sample_systems='data/nh3_c3v.xyz',
sample_systems_format='xyz',
sample_calculator='XTB',
sample_calculator_args = {
'charge': 0,
'directory': 'test/xtb'},
sample_num_threads=1,
)
sampler.run()
# Load two system from xyz file and compute properties using XTB default
# calculator in parallel (still works without using other ASE functions)
sampler = Sampler(
config='test/smpl_nh3.json',
sample_directory='test',
sample_data_file='test/smpl_nh3.db',
sample_systems=['data/nh3_c3v.xyz', 'data/nh3_d3h.xyz'],
sample_systems_format='xyz',
sample_calculator='XTB',
sample_calculator_args = {
'charge': 0,
'directory': 'test/xtb'},
sample_num_threads=2,
)
sampler.run()
# Load two systems from xyz file and a ASE trajectory file and compute
# properties using XTB default calculator
sampler = Sampler(
config='test/smpl_nh3.json',
sample_directory='test',
sample_data_file='test/smpl_nh3.db',
sample_systems=['data/nh3_c3v.xyz', 'data/meta_nh3.traj'],
sample_calculator='XTB',
sample_calculator_args = {
'charge': 0,
'directory': 'test/xtb'},
sample_num_threads=1,
)
sampler.run()
# Load a selection of sample system from an Asparagus data file and compute
# properties using XTB default calculator
sampler = Sampler(
config='test/smpl_nh3.json',
sample_directory='test',
sample_data_file='test/smpl_nh3.db',
sample_systems='data/nms_nh3.db',
sample_systems_format='db',
sample_systems_indices=[0, 1, 2, 3, -4, -3, -2, -1],
sample_calculator='XTB',
sample_calculator_args = {
'charge': 0,
'directory': 'test/xtb'},
sample_num_threads=1,
)
sampler.run()
from asparagus.sample import MCSampler
# Sample a single system loaded from a xyz file using the Monte-Carlo
# sampling method with the XTB calculator
sampler = MCSampler(
config='test/mc_nh3.json',
sample_directory='test',
sample_data_file='test/mc_nh3.db',
sample_systems='data/nh3_c3v.xyz',
sample_systems_format='xyz',
sample_calculator='XTB', # Not thread save when using ASE modules
sample_num_threads=1,
sample_systems_optimize=True,
sample_systems_optimize_fmax=0.001,
mc_temperature=300.0,
mc_steps=100,
mc_max_displacement=0.1,
mc_save_interval=1,
)
sampler.run()
# Sample two systems loaded from a xyz files in parallel using the
# Monte-Carlo sampling method and the ORCA calculator (thread safe)
sampler = MCSampler(
config='test/mc_nh3.json',
sample_directory='test',
sample_data_file='test/mc_nh3.db',
sample_systems=['data/nh3_c3v.xyz', 'data/nh3_d3h.xyz'],
sample_systems_format='xyz',
sample_calculator='ORCA',
sample_calculator_args = {
'charge': 0,
'mult': 1,
'orcasimpleinput': 'RI PBE D3BJ def2-SVP def2/J TightSCF',
'orcablocks': '%pal nprocs 1 end',
'directory': 'test/orca'},
sample_save_trajectory=True,
sample_num_threads=1,
sample_systems_optimize=True,
sample_systems_optimize_fmax=0.001,
mc_temperature=300.0,
mc_steps=10,
mc_max_displacement=0.1,
mc_save_interval=1,
)
sampler.run()
from asparagus.sample import MDSampler
# Sample a single system loaded from a xyz file using the Molecular
# Dynamics sampling method with the XTB calculator
sampler = MDSampler(
config='test/md_nh3.json',
sample_directory='test',
sample_data_file='test/md_nh3.db',
sample_systems='data/nh3_c3v.xyz',
sample_systems_format='xyz',
sample_calculator='XTB', # Not thread save when using ASE modules
sample_num_threads=1,
sample_systems_optimize=True,
sample_systems_optimize_fmax=0.001,
md_temperature=500,
md_time_step=1.0,
md_simulation_time=100.0,
md_save_interval=10,
md_langevin_friction=0.01,
md_equilibration_time=0,
md_initial_velocities=False,
)
sampler.run()
# Sample two systems loaded from a xyz files in parallel using the
# Molecular Dynamics sampling method and the ORCA calculator (thread safe)
sampler = MDSampler(
config='test/md_nh3.json',
sample_directory='test',
sample_data_file='test/md_nh3.db',
sample_systems=['data/nh3_c3v.xyz', 'data/nh3_d3h.xyz'],
sample_systems_format='xyz',
sample_calculator='ORCA',
sample_calculator_args = {
'charge': 0,
'mult': 1,
'orcasimpleinput': 'RI PBE D3BJ def2-SVP def2/J TightSCF',
'orcablocks': '%pal nprocs 1 end',
'directory': 'test/orca'},
sample_save_trajectory=True,
sample_num_threads=2,
sample_systems_optimize=True,
sample_systems_optimize_fmax=0.001,
md_temperature=500,
md_time_step=1.0,
md_simulation_time=20.0,
md_save_interval=10,
md_langevin_friction=0.01,
md_equilibration_time=0,
md_initial_velocities=True,
md_initial_temperature=300,
)
sampler.run()
from asparagus.sample import MetaSampler
# Sample a single system loaded from a xyz file using the Meta Dynamics
# sampling method with the XTB calculator
sampler = MetaSampler(
config='test/meta_nh3.json',
sample_directory='test',
sample_data_file='test/meta_nh3.db',
sample_systems='data/nh3_c3v.xyz',
sample_systems_format='xyz',
sample_calculator='XTB', # Not thread save when using ASE modules
sample_save_trajectory=True,
sample_num_threads=1,
sample_systems_optimize=True,
sample_systems_optimize_fmax=0.001,
meta_cv=[[0, 1], [0, 2], [0, 3]],
meta_gaussian_height=0.10,
meta_gaussian_widths=0.1,
meta_gaussian_interval=10,
meta_hookean=[[0, 1, 4.0], [0, 2, 4.0], [0, 3, 4.0]],
meta_temperature=500,
meta_time_step=1.0,
meta_simulation_time=10_0.0,
meta_save_interval=10,
)
sampler.run()
# Sample a system loaded from a xyz files in parallel using the Meta
# Dynamics sampling method and the ORCA calculator (thread safe)
# Here each of the multiple runs store the Gaussian add-potentials into
# in the same list, affecting the other runs as well and decrease the
# sample steps to reach higher potential areas.
# Not yet working as planned
sampler = MetaSampler(
config='test/meta_nh3.json',
sample_directory='test',
sample_data_file='test/meta_nh3.db',
sample_systems='data/nh3_c3v.xyz',
sample_systems_format='xyz',
sample_calculator='ORCA',
sample_calculator_args = {
'charge': 0,
'mult': 1,
'orcasimpleinput': 'RI PBE D3BJ def2-SVP def2/J TightSCF',
'orcablocks': '%pal nprocs 1 end',
'directory': 'test/orca'},
sample_save_trajectory=True,
sample_num_threads=4,
sample_systems_optimize=True,
sample_systems_optimize_fmax=0.001,
meta_cv=[[0, 1], [0, 2], [0, 3]],
meta_gaussian_height=0.10,
meta_gaussian_widths=0.1,
meta_gaussian_interval=10,
meta_hookean=[[0, 1, 4.0], [0, 2, 4.0], [0, 3, 4.0]],
meta_temperature=500,
meta_time_step=1.0,
meta_simulation_time=20.0,
meta_save_interval=10,
meta_parallel=True,
)
sampler.run()
# Sample two system loaded from a xyz files in parallel using the Meta
# Dynamics sampling method and the ORCA calculator (thread safe)
sampler = MetaSampler(
config='test/meta_nh3.json',
sample_directory='test',
sample_data_file='test/meta_nh3.db',
sample_systems=['data/nh3_c3v.xyz', 'data/nh3_d3h.xyz'],
sample_systems_format='xyz',
sample_calculator='ORCA',
sample_calculator_args = {
'charge': 0,
'mult': 1,
'orcasimpleinput': 'RI PBE D3BJ def2-SVP def2/J TightSCF',
'orcablocks': '%pal nprocs 1 end',
'directory': 'test/orca'},
sample_save_trajectory=True,
sample_num_threads=2,
sample_systems_optimize=True,
sample_systems_optimize_fmax=0.001,
meta_cv=[[0, 1], [0, 2], [0, 3]],
meta_gaussian_height=0.10,
meta_gaussian_widths=0.1,
meta_gaussian_interval=10,
meta_hookean=[[0, 1, 4.0], [0, 2, 4.0], [0, 3, 4.0]],
meta_temperature=500,
meta_time_step=1.0,
meta_simulation_time=20.0,
meta_save_interval=10,
)
sampler.run()
from asparagus.sample import NormalModeScanner
# Sample a single system loaded from a xyz file using the Normal Mode
# Scanner sampling method with the XTB calculator
sampler = NormalModeScanner(
config='test/nms_nh3.json',
sample_directory='test',
sample_data_file='test/nms_nh3.db',
sample_systems='data/nh3_c3v.xyz',
sample_systems_format='xyz',
sample_calculator='XTB', # Not thread save when using ASE modules
sample_num_threads=1,
sample_systems_optimize=True,
sample_systems_optimize_fmax=0.001,
nms_harmonic_energy_step=0.10,
nms_energy_limits=0.50,
nms_number_of_coupling=1,
nms_limit_of_steps=10,
nms_limit_com_shift=0.01,
nms_save_displacements=True,
)
sampler.run()
# Sample two systems loaded from a xyz file using the Normal Mode
# Scanner sampling method with the ORCA calculator (thread safe).
# Here it parallelize over the (1) system optimizations, (2) atom
# displacement calculations for numeric normal mode analysis and (3) the
# scans along single or combinations of normal modes. Step (2) and (3) will
# run in serial for each sample system.
sampler = NormalModeScanner(
config='test/nms_nh3.json',
sample_directory='test',
sample_data_file='test/nms_nh3.db',
sample_systems=['data/nh3_c3v.xyz', 'data/nh3_d3h.xyz'],
sample_systems_format='xyz',
sample_calculator='ORCA',
sample_calculator_args = {
'charge': 0,
'mult': 1,
'orcasimpleinput': 'RI PBE D3BJ def2-SVP def2/J TightSCF',
'orcablocks': '%pal nprocs 1 end',
'directory': 'test/orca'},
sample_num_threads=4,
sample_systems_optimize=True,
sample_systems_optimize_fmax=0.001,
nms_harmonic_energy_step=0.10,
nms_energy_limits=0.50,
nms_number_of_coupling=1,
nms_limit_of_steps=10,
nms_limit_com_shift=0.01,
nms_save_displacements=False,
)
sampler.run()
from asparagus.sample import NormalModeSampler
# Sample a single system loaded from a xyz file using the Normal Mode
# Sampler sampling method with the XTB calculator
sampler = NormalModeSampler(
config='test/nms_nh3.json',
sample_directory='test',
sample_data_file='test/nms_nh3.db',
sample_systems='data/nh3_c3v.xyz',
sample_systems_format='xyz',
sample_calculator='XTB', # Not thread save when using ASE modules
sample_num_threads=1,
sample_systems_optimize=True,
sample_systems_optimize_fmax=0.001,
nms_temperature=500.0,
nms_nsamples=100,
)
sampler.run()
# Sample a system loaded from a xyz file using the Normal Mode
# Sampler sampling method with the ORCA calculator (thread safe).
# Here it parallelize over the (1) system optimizations, (2) atom
# displacement calculations for numeric normal mode analysis and (3) the
# number of randomly sampled system conformations. Step (2) and (3) will
# run in serial for each potential sample system.
sampler = NormalModeSampler(
config='test/nms_nh3.json',
sample_directory='test',
sample_data_file='test/nms_nh3.db',
sample_systems='data/nh3_d3h.xyz',
sample_systems_format='xyz',
sample_calculator='ORCA',
sample_calculator_args = {
'charge': 0,
'mult': 1,
'orcasimpleinput': 'RI PBE D3BJ def2-SVP def2/J TightSCF',
'orcablocks': '%pal nprocs 1 end',
'directory': 'test/orca'},
sample_num_threads=4,
sample_systems_optimize=True,
sample_systems_optimize_fmax=0.001,
nms_temperature=500.0,
nms_nsamples=10,
)
sampler.run()
#==============================================================================
# Test Asparagus Calculators - Shell & Slurm
#==============================================================================
# Shell Calculator
if flag_sampler_shell:
from asparagus.sample import Sampler
# Calculate properties of a sample system with multiple conformations
# using the Shell calculator with template files for an ORCA calculation.
sampler = Sampler(
config='test/calc_nh3.json',
sample_directory='test',
sample_data_file='test/smpl_nh3.db',
sample_systems='data/nh3_c3v.xyz',
sample_systems_format='xyz',
sample_calculator='shell',
sample_calculator_args = {
'files': [
'data/template/shell/run_orca.sh',
'data/template/shell/run_orca.inp',
'data/template/shell/run_orca.py',
],
'files_replace': {
'%xyz%': '$xyz',
'%charge%': '$charge',
'%multiplicity%': '$multiplicity',
},
'execute_file': 'run_orca.sh', # or 'data/template/run_orca.sh'
'charge': 0,
'multiplicity': 1,
'directory': 'test/shell',
'result_properties': ['energy', 'forces', 'dipole']
},
sample_num_threads=1,
)
sampler.run()
# Calculate properties of a sample system with multiple conformations
# using the Shell calculator with template files for an ORCA calculation
# and in parallel.
sampler = Sampler(
config='test/calc_nh3.json',
sample_directory='test',
sample_data_file='test/smpl_nh3.db',
sample_systems='data/meta_nh3.traj',
sample_calculator='shell',
sample_calculator_args = {
'files': [
'data/template/shell/run_orca.sh',
'data/template/shell/run_orca.inp',
'data/template/shell/run_orca.py',
],
'files_replace': {
'%xyz%': '$xyz',
'%charge%': '$charge',
'%multiplicity%': '$multiplicity',
},
'execute_file': 'data/template/shell/run_orca.sh',
'charge': 0,
'multiplicity': 1,
'directory': 'test/shell',
'result_properties': ['energy', 'forces', 'dipole']
},
sample_num_threads=4,
sample_save_trajectory=True,
)
sampler.run()
# Slurm Calculator
if flag_sampler_slurm:
from asparagus.sample import Sampler
# Calculate properties of a sample system with multiple conformations
# using the Slurm calculator with template files for a MOLPRO calculation.
sampler = Sampler(
config='test/calc_nh3.json',
sample_directory='test',
sample_data_file='test/smpl_nh3.db',
sample_systems='data/nh3_c3v.xyz',
sample_systems_format='xyz',
sample_calculator='slurm',
sample_calculator_args = {
'files': [
'data/template/slurm/run_molpro.sh',
'data/template/slurm/run_molpro.inp',
'data/template/slurm/run_molpro.py',
],
'files_replace': {
'%xyz%': '$xyz',
'%charge%': '$charge',
'%spin2%': '$spin2',
},
'execute_file': 'run_molpro.sh',
'charge': 0,
'multiplicity': 1,
'directory': 'test/slurm',
'result_properties': ['energy', 'forces', 'dipole']
},
sample_num_threads=1,
)
sampler.run()
# Calculate properties of a sample system with multiple conformations
# using the Slurm calculator with template files for a MOLPRO calculation.
# Here, define own slurm task id catch and check function
import subprocess
def catch_id(
stdout: str,
) -> int:
"""
Catch slurm task id from the output when running:
subrocess.run([command, execute_file], capture_output=True)
(here [command, execute_file] -> 'sbatch run_molpro.sh')
Parameters
----------
stdout: str
Decoded output line (e.g. 'Submitted batch job 10937679')
Return
------
int
Task id
"""
return int(proc.stdout.decode().split()[-1])
def check_id(
slurm_id: int,
) -> bool:
"""
Check slurm task id with e.g. task id list extracted from squeue
Parameters
----------
slurm_id: int
Slurm task id of the submitted job
Return
------
bool
Answer if task is done:
False, if task is still running (task id is found in squeue)
True, if task is done (task id not found in squeue)
"""
proc = subprocess.run(
['squeue', '-u', os.environ['USER']],
capture_output=True)
active_id = [
int(tasks.split()[0])
for tasks in proc.stdout.decode().split('\n')[1:-1]]
return not slurm_id in active_id
sampler = Sampler(
config='test/calc_nh3.json',
sample_directory='test',
sample_data_file='test/smpl_nh3.db',
sample_systems='data/nh3_c3v.xyz',
sample_systems_format='xyz',
sample_calculator='slurm',
sample_calculator_args = {
'files': [
'data/template/slurm/run_molpro.sh',
'data/template/slurm/run_molpro.inp',
'data/template/slurm/run_molpro.py',
],
'files_replace': {
'%xyz%': '$xyz',
'%charge%': '$charge',
'%spin2%': '$spin2',
},
'execute_file': 'run_molpro.sh',
'charge': 0,
'multiplicity': 1,
'directory': 'test/slurm',
'result_properties': ['energy', 'forces', 'dipole']
},
sample_num_threads=1,
scan_interval=1,
scan_catch_id=catch_id,
scan_check_id=check_id,
)
sampler.run()
# Calculate properties of a sample system with multiple conformations
# using the Slurm calculator with template files for a MOLPRO calculation.
sampler = Sampler(
config='test/calc_nh3.json',
sample_directory='test',
sample_data_file='test/smpl_nh3.db',
sample_systems='data/nms_nh3.db',
sample_systems_format='db',
sample_systems_indices=[0, 1, 2, 3, -4, -3, -2, -1],
sample_calculator='slurm',
sample_calculator_args = {
'files': [
'data/template/slurm/run_molpro.sh',
'data/template/slurm/run_molpro.inp',
'data/template/slurm/run_molpro.py',
],
'files_replace': {
'%xyz%': '$xyz',
'%charge%': '$charge',
'%spin2%': '$spin2',
},
'execute_file': 'run_molpro.sh',
'charge': 0,
'multiplicity': 1,
'directory': 'test/slurm',
'result_properties': ['energy', 'forces', 'dipole']
},
sample_num_threads=4,
)
sampler.run()
#==============================================================================
# Test Asparagus Model Calculator - PhysNet
#==============================================================================
# Initialize PhysNet model calculator
config_file1 = 'test/model_physnet.json'
if flag_model_physnet:
model = asparagus.Asparagus(
config_file=config_file1,
model_type='physnet')
mcalc = model.get_model_calculator(
model_directory='test/physnet') # Default model type: 'PhysNet'
model.set_model_calculator(
model_directory='test/physnet')
model.set_model_calculator(
model_calculator=mcalc)
# Initialize PhysNet model training
if flag_train_physnet_sql:
config_file2 = 'test/train_physnet.json'
model = asparagus.Asparagus(
config=config_file1,
config_file=config_file2,
data_file='data/nms_nh3.db',
model_directory='test/physnet_sql',
model_num_threads=2,
trainer_max_epochs=10,
trainer_debug_mode=False,
)
trainer = model.get_trainer()
model.train()
model.test(test_directory='test/physnet_sql')
if flag_train_physnet_npz:
config_file2 = 'test/train_physnet_npz.json'
model = asparagus.Asparagus(
config=config_file1,
config_file=config_file2,
data_file='test/nms_nh3.db.npz',
data_source='data/nms_nh3.db',
data_source_format='db',
model_directory='test/physnet_npz',
model_num_threads=2,
trainer_max_epochs=10,
trainer_debug_mode=False,
)
trainer = model.get_trainer()
model.train()
model.test(test_directory='test/physnet_npz')
# Test ASE calculator
if flag_ase_physnet:
from ase import Atoms
# Get ASE model calculator
config_file = 'test/train_physnet.json'
model = asparagus.Asparagus(
config_file=config_file)
calc = model.get_ase_calculator()
# Get data container
data = model.get_data_container()
Ndata = len(data)
results_energy = np.zeros([Ndata, 2], dtype=float)
for idata, data_i in enumerate(data):
# Set system from data container
system = Atoms(
data_i['atomic_numbers'],
positions=data_i['positions'])
system_energy = data_i['energy'].numpy()
system_forces = data_i['forces'].numpy()
system_dipole = data_i['dipole'].numpy()
# Compute model properties
model_energy = calc.get_potential_energy(system)
model_forces = calc.get_forces(system)
model_dipole = calc.get_dipole_moment(system)
# Compare results
if False:
print(
"Reference and model energy (error): "
+ f"{system_energy:.4f} eV, {model_energy:.4f} eV "
+ f"({system_energy - model_energy:.4f} eV)"
)
print(
"Reference and model forces on nitrogen (mean error): "
+ f"{system_forces[0]} eV/Ang, {model_forces[0]} eV/Ang "
+ f"({np.mean(system_forces[0] - model_forces[0]):.4f} eV/Ang)"
)
print(
"Reference and model dipole (mean error): "
+ f"{system_dipole} eAng, {model_dipole} eAng "
+ f"({np.mean(system_dipole - model_dipole):.4f} eAng)"
)
# Append to result list
results_energy[idata, 0] = system_energy
results_energy[idata, 1] = model_energy
# Show RMSE
rmse_energy = np.sqrt(
np.mean((results_energy[:, 0] - results_energy[:, 1])**2))
print(f"RMSE(energy) = {rmse_energy:.4f} eV")
# Initialize PaiNN model calculator
config_file1 = 'test/model_painn.json'
if flag_model_painn:
model = asparagus.Asparagus(
config_file=config_file1,
model_type='painn')
mcalc = model.get_model_calculator(
model_directory='test/painn') # Default model type: 'PhysNet'
model.set_model_calculator(
model_directory='test/painn')
model.set_model_calculator(
model_calculator=mcalc)
# Initialize PaiNN model training
if flag_train_painn:
config_file2 = 'test/train_painn.json'
model = asparagus.Asparagus(
config=config_file1,
config_file=config_file2,
data_file='data/nms_nh3.db',
model_directory='test/painn',
model_num_threads=2,
trainer_max_epochs=10,
)
trainer = model.get_trainer()
model.train()
model.test(test_directory='test/painn')
#==============================================================================
# Test Transfer Learning
#==============================================================================
# Initialize PhysNet model, start training once, start training again but from
# best checkpoint file of first training.
if flag_transfer_learning:
# Base Model
config_file1 = 'test/trans_learn1.json'
model = asparagus.Asparagus(
config=config_file1,
data_file='data/nms_nh3.db',
model_directory='test/trans_learn/model_base',
trainer_max_epochs=10,
)
model.train()
model.test(test_directory='test/trans_learn/model_base')
config_file2 = 'test/trans_learn2.json'
model = asparagus.Asparagus(
config=config_file1,