-
Notifications
You must be signed in to change notification settings - Fork 2
/
rjsub
1064 lines (958 loc) · 32.3 KB
/
rjsub
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
#! /usr/bin/perl
# Modified by Naomi Haworth 17/3/2015
if (@ARGV < 1) { die "Syntax: rjsub filename [optional flags]
Optional Flags (order doesn't matter):
-rproj [f81/x69] choose project to run on
-time [xh] max runtime in hours
-mem [xmb] max virtual memory in mb or gb
-progmem [xmb] max program memory in mb or gb
-disk [xgb] max disk space in mb or gb
-nproc [x] number of processors
-e submit to express queue
-q queue to submit to: normal, broadwell (bw), hugemem (hm), knightslanding (knl)
-ver program version eg. use 09c01 to use Gaussian 09, Revision C.01 (often resolves Internal Consistency Failure problem for MP2/GTMP2Large and can also help avoid geometry optimisations getting stuck in unproductive cycles)
-ichk copy initial gaussian formatted checkpoint file to raijin - filename must match .com file.
-ochk copy gaussian formatted checkpoint file back from raijin after calculation. \n\n" }
$|++;
$machine = "raijin";
chomp($ARGV[0]);
$scanjob=0;
$proj[0]=$ENV{PROJECT};
$proj[1]=$ENV{PROJECT2};
$home=$ENV{HOME};
if (!($ENV{PROJ1GRANT})) {
die "Please edit your .bashrc to include the following lines and source: \nexport PROJECT=f81\nexport PROJECT2=x69\nexport PROJ1GRANT=400\nexport PROJ2GRANT=750\n";
}
else {
$grant{$proj[0]} = $ENV{PROJ1GRANT};
$grant{$proj[1]} = $ENV{PROJ2GRANT};
}
$queue = "normal";
$express = "n";
$RJ = "RJ";
$ruser = $ENV{REMOTE_USER};
$rproj = $proj[0];
$definerproj = "n";
$utime = "";
$umem = "";
$upmem = "";
$pmem = "";
$ujobfs = "";
$unproc = "";
$ichk = "";
$ochk = "";
$cs = "";
$maxQ{x69} = 690; # These limits are outdated
$maxQ{f81} = 690; # Feel free to change if needed
$maxU{x69} = 650; # however make sure you don't run so many jobs
$maxU{f81} = 650; # that others are blocked out.
$sleep_queue = 360;
$sqtest = "no";
for ($i = 0; $i <= 30; $i++) {
if ($ARGV[$i]){
if (($ARGV[$i] =~ /^-rproj/) || ($ARGV[$i] =~ /^-proj/)) {
$rproj = $ARGV[$i+1];
$definerproj = "y";
}
if ($ARGV[$i] =~ /^-ver/){
$ver = "-ver $ARGV[$i+1]";
}
if ($ARGV[$i] =~ /^-uq/){
$m = $ARGV[$i+1];
$sqtest = "yes";
}
if ($ARGV[$i] =~ /^-tq/){
$n = $ARGV[$i+1];
$sqtest = "yes";
}
if (($ARGV[$i] =~ /^-lt/) || ($ARGV[$i] =~ /^-time/)) {
$utime = $ARGV[$i+1];
}
if (($ARGV[$i] =~ /^-lw/) || ($ARGV[$i] =~ /^-mem/)) {
$umem = $ARGV[$i+1];
}
if (($ARGV[$i] =~ /^-pm/) || ($ARGV[$i] =~ /^-progmem/)) {
$upmem = $ARGV[$i+1];
}
if (($ARGV[$i] =~ /^-lf/) || ($ARGV[$i] =~ /^-jobfs/) || ($ARGV[$i] =~ /^-disk/)) {
$ujobfs = $ARGV[$i+1];
}
if ($ARGV[$i] =~ /^-nproc/) {
$unproc = $ARGV[$i+1];
}
if ($ARGV[$i] =~/^-e/){
$express = "y";
}
if ($ARGV[$i] =~/^-q/){
$queue = $ARGV[$i+1];
if ($queue =~ /normal/) {
$RJ = "RJ";
}
elsif (($queue =~ /broadwell/) || ($queue =~ /bw/)) {
$RJ = "BW";
}
elsif (($queue =~ /hugemem/) || ($queue =~ /hm/)) {
$RJ = "HM";
}
elsif ($queue =~ /kn/) {
$RJ = "KNL";
}
}
if ($ARGV[$i] =~/^-ichk/){
$ichk = "-ichk";
}
if ($ARGV[$i] =~/^-ochk/){
$ochk = "-ochk";
}
if ($ARGV[$i] =~/^-cs/){
$cs = "yes";
}
}
}
if (($RJ =~ /RJ/) || ($RJ =~ /BW/)) {
if ($express =~ /y/) {
$RJ = $RJ."E";
}
else {
$RJ = $RJ."N";
}
}
else {
if ($express =~ /y/) {
print "Express is only available for normal and Broadwell nodes.\n";
}
$RJ = $RJ."N";
}
if (($sqtest =~ "no") && ($cs !~ "yes") && ($definerproj =~ "n")) {
@alloctime = split(/\s+/,`ssh $machine -l \$REMOTE_USER nci_account -P $proj[0] | grep 'Overall' `);
if ($alloctime[6]/1000 > $grant{$proj[0]}) {
$rproj = $proj[1];
print "No SU available on project $proj[0] - submit to $rproj.\n";
}
}
if($ARGV[0] =~/inp/){
# GAMESS INPUT
if ($ARGV[0] =~ /uhf/){
$vmem = "1gb";
$jobfs = "1gb";
$time = "3";
$nproc = "4";
}
elsif ($ARGV[0] =~/sad/ or $ARGV[0] =~/opt/){
$vmem = "5gb";
$jobfs = "6gb";
$time = "3";
$nproc = "4";
}
elsif ($ARGV[0] =~/sp/){
$vmem = "3gb";
$jobfs = "5gb";
$time = "1";
$nproc = "4";
}
else {
$vmem = "3gb";
$jobfs = "5gb";
$time = "20";
$nproc = "4";
}
if ($RJ =~/RJE/){
$time = "1";
$vmem = "3gb";
$jobfs = "5gb";
$nproc = "4";
}
}
elsif ($ARGV[0] =~/ini/){
# MOLPRO INPUT
open INI, "$ARGV[0]", or die $!;
while (<INI>){
if (/geometry/){
$natom = readline(INI);
}
}
close INI;
if ($natom <=10){
$vmem = "5gb";
$nproc = "4";
$jobfs = "10gb";
$time = "48";
}
elsif($natom <=20){
$vmem = "8500mb";
$nproc = "4";
$jobfs = "20gb";
$time = "50";
}
elsif($natom <= 40 ){
$vmem = "28gb";
$nproc = "4";
$jobfs = "100gb";
$time = "150";
}
else{
$vmem = "58gb";
$nproc = "4";
$jobfs = "200gb";
$time = "200";
}
if ($RJ =~/RJE/){
# $time = "1";
# $vmem = "4gb";
# $jobfs = "2gb";
# $nproc = "4";
if($time >= 24){
$time = 24;
}
}
}
elsif ($ARGV[0] =~/\.in/){
# Q-Chem INPUT
if ($RJ =~/RJE/){
$time = "1";
$vmem = "2gb";
$jobfs = "2gb";
$nproc = "1";
}
else{
$vmem = "2200mb";
$nproc = "1";
$jobfs = "10gb";
$time = "30";
}
}
elsif ($ARGV[0] =~/qz/){
$vmem = "28gb";
$nproc = "8";
$jobfs = "400gb";
$time = "400";
$ini_mem = "2000";
}
else {
if ($ichk !~ "chk") {
$cominput=file2string($ARGV[0]);
stripcom($cominput);
$hydrogens=0;
$heavyatoms=0;
if ($iszmat){
my $molecule = $ARGV[0];
zmtCom2xyz($molecule, \@atom, \@X, \@Y, \@Z);
for ($i = 0; $i < $natoms; $i++){
if($atom[$i] =~ /^H$/i){
$hydrogens++;
}
else{
$heavyatoms++;
}
}
}
else{
@atom1= split(/\n/,$cominput);
for ($i = 0; $i <= $#cominput; $i++){
@atom1[$i] =~ s/^\s+//;
@atom[$i] = split(/\s+/,@atom1[$i]);
if($atom[$i] =~ /^H$/i){
$hydrogens++;
}
else{
$heavyatoms++;
}
}
}
print "$ARGV[0]\t has $heavyatoms heavy atoms\n";
}
else {
print "Input data will be taken from checkpoint file\n";
}
#print "natoms=",$hydrogens+$heavyatoms,"\n";
#print $cominput;
if ($ARGV[0] =~/run/){
# ADF INPUT
$vmem = "1gb";
$jobfs = "1gb";
$time = "1";
$nproc = "4";
}
request();
if (!$ARGV[1]){
}
else{
if ($ARGV[1] eq "dog3m"){
print "usarj $RJ $ARGV[0] -lf $jobfs -lw $vmem -lt $time -nproc $nproc -rproj $rproj -g3 mp2\n";
system("usarj $RJ $ARGV[0] -lf $jobfs -lw $vmem -lt $time -nproc $nproc -rproj $rproj -g3 mp2");
}
elsif ($ARGV[1] eq "dog3mdiff"){
print "usarj $RJ $ARGV[0] -lf $jobfs -lw $vmem -lt $time -nproc $nproc -rproj $rproj -g3 mp2-diff\n";
system("usarj $RJ $ARGV[0] -lf $jobfs -lw $vmem -lt $time -nproc $nproc -rproj $rproj -g3 mp2-diff");
}
elsif ($ARGV[1] eq "dog3"){
print "usarj $RJ $ARGV[0] -lf $jobfs -lw $vmem -lt $time -nproc $nproc -rproj $rproj -g3 mp2rad\n";
system("usarj $RJ $ARGV[0] -lf $jobfs -lw $vmem -lt $time -nproc $nproc -rproj $rproj -g3 mp2rad");
}
elsif ($ARGV[1] eq "dog3diff"){
print "usarj $RJ $ARGV[0] -lf $jobfs -lw $vmem -lt $time -nproc $nproc -rproj $rproj -g3 mp2rad-diff\n";
system("usarj $RJ $ARGV[0] -lf $jobfs -lw $vmem -lt $time -nproc $nproc -rproj $rproj -g3 mp2rad-diff");
}
elsif ($ARGV[1] eq "dog3full"){
print "usarj $RJ $ARGV[0] -lf $jobfs -lw $vmem -lt $time -nproc $nproc -rproj $rproj -g3 full\n";
system("usarj $RJ $ARGV[0] -lf $jobfs -lw $vmem -lt $time -nproc $nproc -rproj $rproj -g3 full");
}
elsif ($ARGV[1] eq "dog3core"){
print "usarj $RJ $ARGV[0] -lf $jobfs -lw $vmem -lt $time -nproc $nproc -rproj $rproj -g3 core\n";
system("usarj $RJ $ARGV[0] -lf $jobfs -lw $vmem -lt $time -nproc $nproc -rproj $rproj -g3 core");
}
elsif ($ARGV[1] eq "dow1"){
print "usarj $RJ $ARGV[0] -lf $jobfs -lw $vmem -lt $time -nproc $nproc -rproj $rproj -g3 w1\n";
system("usarj $RJ $ARGV[0] -lf $jobfs -lw $vmem -lt $time -nproc $nproc -rproj $rproj -g3 w1");
}
else{
}
}
}
if ($utime =~ /\d/) { $time = $utime }
if ($umem =~ /\d/) { $vmem = $umem }
if ($upmem =~ /\d/) { $pmem = "-pm $upmem" }
if ($ujobfs =~ /\d/) { $jobfs = $ujobfs }
if ($unproc =~ /\d/) { $nproc = $unproc }
if ((($RJ =~ /BW/) || ($RJ =~ /HM/)) && ($unproc !~ /\d/)) { $nproc = int($nproc/4)*7 }
if (($RJ =~ /HM/) && ($vmem =~ /(\d+)(\S)\S/)) {
$val = $1;
$lt = $2;
if (($lt !~ /[gG]/) || ($val < 256)) { $vmem = "260gb" }
}
if (($RJ =~ /E/) && ($time > "24")) {
$time = "24";
}
if (($RJ =~ /BW/) && ($time > "48")) {
$time = "48";
}
if (($RJ =~ /HM/) && ($time > "24")) {
$time = "24";
}
$time = ${time}."h";
# print "Attempting to submit on project $rproj\n";
if (($sqtest =~ "yes") && (($m >= $maxU{$rproj}) || ($n+1 >= $maxQ{$rproj}))){
while (!($queue = freeQ($rproj,$m,$n))){
print color 'red';
print "Queue on $machine is full. Waiting...\n\n";
print color 'reset';
sleep $sleep_queue;
}
}
print "usarj $RJ $ARGV[0] -lf $jobfs -lw $vmem $pmem -lt $time -nproc $nproc -rproj $rproj $ichk $ochk $ver\n";
$jobid = `usarj $RJ $ARGV[0] -lf $jobfs -lw $vmem $pmem -lt $time -nproc $nproc -rproj $rproj $ichk $ochk $ver`;
chomp ($jobid);
$jobid =~ s/\.r-man2//;
$jobid =~ s/\n//;
printf "Jobid: %-10s\n",$jobid;
$recfile = "$home/JOB/recordfile";
open RECORD, ">>$recfile" or die "Couldn't open file $recfile for appending\n";
printf RECORD "%-10s %-100s\n",$jobid, $ARGV[0];
close (RECORD);
######################################################################################################
sub request{
if ($theory =~/pm6/i ){
$vmem = "1500mb";
$nproc = "4";
$jobfs = "1";
$time = "0.2";
}
elsif (($theory =~/lyp/i or $theory =~/bmk/i or $theory =~/M05/i or $theory =~/M06/i) and ($scanjob==1 or $ARGV[0] =~/scan/i) ){
$vmem = "1500mb";
$nproc = "4";
$jobfs = int($heavyatoms/4)*4;
$time = 5*int($heavyatoms/10*6);
}
elsif (($theory =~/lyp/i or $theory =~/bmk/i or $theory =~/M05/i or $theory =~/M06/i) and $scanjob==0 ){
$vmem = "1500mb";
$nproc = "4";
$jobfs = int($heavyatoms/4)*4;
$time = int($heavyatoms)*2;
}
elsif ($theory =~/ccsd/i ){
$vmem = "40gb";
$nproc = "4";
$jobfs = "40";
$time = int($heavyatoms/10*30);
}
elsif ($theory =~/MP2\/gen/i ){
$vmem = "8500mb";
$nproc = "4";
$jobfs = $heavyatoms*2;
$time = $heavyatoms*5;
}
elsif ($theory =~/6-311/i ){
$vmem = "6500mb";
$nproc = "4";
$jobfs = "10";
$time = int($heavyatoms*5);
}
elsif ($theory =~/GTMP2LARGE/i ){
$vmem = "8500mb";
$nproc = "4";
$jobfs = $heavyatoms*2;
$time = $heavyatoms*5;
}
elsif ($theory =~/hf/i ){
$vmem = "2500mb";
$nproc = "4";
$jobfs = "5";
$time = int($heavyatoms/10*5);
}
elsif ($theory =~/g3mp2b3/i ){
$vmem = "8500mb";
$nproc = "4";
$jobfs = "50";
$time = $heavyatoms*5;
}
else{
$vmem = "1500mb";
$nproc = "4";
$jobfs = "20";
$time = int($heavyatoms/10*14);
}
if ($heavyatoms > 20) {
$nproc = "8";
}
if ($jobfs<5){
$jobfs=5;
}
elsif ($jobfs > 26){
$jobfs=26;
}
$jobfs = ${jobfs}."gb";
if ($time eq "0"){
$time = "48";
}
if ($time > "200"){
$time = "200";
}
}
################################################################################
sub file2string {
# file2string("filename") : gets a txt file, turns it into a string.
my ($i,$file,$temp);
local $/;
open(INPUTFILE, $_[0]) ||die "Can't open $_[0] for reading.\n";
undef $/;
$file = <INPUTFILE>;
close(INPUTFILE)|| die "Can't close $_[0].\n";
return $file;
}
sub getcm {
#getcm("geometry") : gets charge/multiplicity line from "geometry" string.
my ($geometry, @geometry, $cm);
$geometry = $_[0];
@geometry = split(/\n/, $geometry);
$cm = $geometry[0];
return $cm;
}
sub stripcom{
#stripcom("geometry_string"), returns stripped geometry string.
my($geometry, @file, $cm_flag, $bk, $cm_index, $i);
my($temp2, @temp);
$geometry = $_[0];
@file = split(/\n/,$geometry);
$cm_flag = 0; # whether we've hit the (first) charge/multiplicity section or not.
$bk = 0; #blank line counter for zmat section
@cominput = ();
$cm_index = 9999999;
for($i = 0; $i <= $#file; $i++){
#match the charge/multiplicity section.
if ($file[$i] =~/#/){
$theory =${theory}.${file[$i]};
}
if ($file[$i] =~/^[ \t]*-*\+*[ \t]*[0-9]{1,2}[ \t]*\+*[ \t]*[0-9]{1,2}[ \t]*$/){
$cm_flag +=1; #means you only get the top geometry.
if ($cm_flag == 1){
# push(@cominput, $file[$i]); #put cm into array
$cm_index = $i; #mark where cm is
$temp2 = $file[$i+1]; #get next line
$temp2 =~s/^[ \t]*//; #strip leading whitespace
$temp2 =~s/[ \t]*$//; #strip trailing whitespace
@temp = split(/[ \t]+/,$temp2); #split
if ($temp[0] =~ /-?\b[a-z]{1,2}\b/i){ #see if zmat
if (@temp == 1){
$iszmat = 1;
}else{
if (@temp == 4) {
$iszmat = 0;
}
elsif (@temp == 5) {
$iszmat = 1;
}
else{
die "$temp2 : Not xyz or xmat!\n";
}
}
}else{
die "$temp[0] : not an atom!!\n";
}
}
}
if ($i > $cm_index && $cm_flag == 1){ #discard useless header and footer
if($iszmat == 0 && $file[$i] =~/^[ \t]*[a-zA-Z]{1,2}[ \t]{1,}-?\+? *\.?[0-9]{1,}\.?[0-9]*[ \t]{1,}-?\+? *\.?[0-9]{1,}\.?[0-9]*[ \t]{1,}-?\+? *\.?[0-9]{1,}\.?[0-9]*[ \t]*$/){ #if zmat and a coordinate
push(@cominput, $file[$i]);
}
if($iszmat == 1){
if($file[$i] =~ /^[ \t]*$/){
$bk+=1; #count blank lines in zmat
}
if ($bk < 2){ # if zmat and before the end of it, then
push(@cominput, $file[$i]);
}
}
}
}
$cominput = join("\n",@cominput);
return $cominput;
}
sub check_geometry_type{
# check_geometry_type("cominput") : checks to see if the cominput is xyz or zmat.
my($cominput,@cominput, $i, $temp2, $temp, @temp, $iszmat);
$cominput = $_[0];
@cominput = split(/\n/,$cominput);
for($i = 0; $i <= $#cominput; $i++){
if ($cominput[$i] =~/^[ \t]*-*\+*[ \t]*[0-9]{1,2}[ \t]*\+*[ \t]*[0-9]{1,2}[ \t]*$/){
$temp2 = $cominput[$i+1]; #get next line
$temp2 =~s/^[ \t]*//; #get rid of leading whitespace
$temp2 =~s/[ \t]*$//; #get rid of trailing whitespace
@temp = split(/[ \t]+/,$temp2); #split
if ($temp[0] =~ /\b[a-z]{1,2}\b/i){ #see if zmat
if (@temp == 1){
$iszmat = 1;
}else{
if (@temp == 4){
$iszmat = 0;
}else{
die "$temp2 : Not xyz or zmat!\n";
}
}
}else{
die "$temp[0] : not an atom!!\n";
}
}
}
return $iszmat;
}
# usage: zmtCom2xyz($molecule, \@atom, \@X, \@Y, \@Z);
sub zmtCom2xyz {
my $mol = shift;
$atom = shift;
my ($x,$y,$z) = @_;
open COMFILE, "< $mol" or die "Couldn't open file $mol\n$!";
# this takes Gaussian input file with geometry given as
# Zmat, and converts it to the XYZ format (cartesians)
# This is an educational script, hence many comments.
# The script is a filter, i.e., should be used as
# cat name.inp | g9xinp2xyz.pl > name.xyz
# where name.inp is the Gaussian input file and
# name.xyz is a file with cartesian coordinates a la XMol
# It is assumed that G9x input file has a format:
# % part (e.g., checkpoint specs)
# # keywords/links part
# empty line
# title lines
# empty line
# charge multiplicity line
# zmatrix lines
# empty line or "Variables"
# symbolic variables for Z-matrix (if ZMAT has symbolic parameters)
# empty line or "Constants"
# symbolic constants for Z-matriz (if all references were not resolved yet)
# empty line or line starting with "-"
#
# Written by Jan Labanowski in July 1997 <[email protected]>
#
$keywords = "";
while($line = <COMFILE>) { # read next lines of keywords (route section)
last if ($line =~ /\s*\d\s+\d\s*/);
}
$natoms = 0; #number of atoms found initialized
while ($line = <COMFILE>) { # read molecule description lines
chop($line);
$line =~ tr/a-z/A-Z/;
$line =~ s/^\s+//; # get rid of front and back spaces
$line =~ s/\s+$//;
if(($line !~ /\S/) ||
($line =~ /variab/i) ||
($line =~ /consta/i)) { # if blank line or end of Zmat
last; # exit the loop
}
(@fields) = split(/\s+|,/, $line); # split line into fields
if (($natoms == 0) && ($fields[1] !~ /0/)) { # first atom is only an atomic symbol
$symbol[$natoms] = $line;
($bf[$natoms], $af[$natoms], $tf[$natoms]) = (1, 1, 1);
}
elsif ($natoms == 0) { # first atom is only an atomic symbol
$symbol[$natoms] = $fields[0];
$blen[$natoms] = $fields[2];
$angle[$natoms] = $fields[3];
$tors[$natoms] = $fields[4];
($bf[$natoms], $af[$natoms], $tf[$natoms]) = (0, 0, 0);
if(($blen[$natoms] =~ /^[-+]?[0-9]*\.?[0-9]*$/) &&
($blen[$natoms] =~ /[0-9]+/)) {
$bf[$natoms] = 1;
}
if(($angle[$natoms] =~ /^[-+]?[0-9]*\.?[0-9]*$/) &&
($angle[$natoms] =~ /[0-9]+/)) {
$af[$natoms] = 1;
}
if(($tors[$natoms] =~ /^[-+]?[0-9]*\.?[0-9]*$/) &&
($tors[$natoms] =~ /[0-9]+/)) {
$tf[$natoms] = 1;
}
}
else {
# split the line into individual fields:
# assumed most standard z-matrix form, so check number of entries
# if((($#fields != 2) && ($natoms == 1)) &&
# (($#fields != 4) && ($natoms == 2)) &&
# (($#fields != 6) && ($natoms >= 3))) {
# printf STDERR "Error in Z-Matrix for atom %d\n", $natoms+1;
# exit(2); # abort
# }
for($i = $#fields+1; $i <8; $i++) { # fill up with empty strings
$fields[$i] = "";
}
# parse fields symbol_i j d_ij k theta_ijk l phi_ijkl
($symbol[$natoms], # atom symbol
$bondat[$natoms], # bonded atom
$blen[$natoms], # bond length
$angat[$natoms], # atom in an angle
$angle[$natoms], # angle
$torat[$natoms], # last atom in torsion angle
$tors[$natoms]) # torsion angle
= (@fields);
# initialize flags for required parameters to "NotFound" status (i.e., 0)
if($natoms == 1) {
($bf[$natoms], $af[$natoms], $tf[$natoms]) = (0, 1, 1);
}
elsif($natoms == 2) {
($bf[$natoms], $af[$natoms], $tf[$natoms]) = (0, 0, 1);
}
else {
($bf[$natoms], $af[$natoms], $tf[$natoms]) = (0, 0, 0);
}
# check if parameters are given as numbers (i.e., sign, possibly followed
# by digits, possibly followed by period, possibly followed by digits
# and containing at least one digit total. Set flag if number.
if(($blen[$natoms] =~ /^[-+]?[0-9]*\.?[0-9]*$/) &&
($blen[$natoms] =~ /[0-9]+/)) {
$bf[$natoms] = 1;
}
if(($angle[$natoms] =~ /^[-+]?[0-9]*\.?[0-9]*$/) &&
($angle[$natoms] =~ /[0-9]+/)) {
$af[$natoms] = 1;
}
if(($tors[$natoms] =~ /^[-+]?[0-9]*\.?[0-9]*$/) &&
($tors[$natoms] =~ /[0-9]+/)) {
$tf[$natoms] = 1;
}
# Atom numbers must be all digits
if(($bondat[$natoms] !~ /^[0-9]+$/) ||
(($angat[$natoms] !~ /^[0-9]+$/) && ($natoms > 1)) ||
(($torat[$natoms] !~ /^[0-9]+$/) && ($natoms > 2))) {
printf STDERR "Error in atom definition for %d\n", $i+1;
exit(2);
}
}
$natoms++;
}
# check any symbolic parameters are given in the Z-mat
$parms_given = 0;
for($i = 0; $i < $natoms; $i++) {
if(($bf[$i] == 0) ||
($af[$i] == 0) ||
($tf[$i] == 0)) {
$parms_given = 1; # symbolic parameters present
last;
}
}
if($parms_given == 1) { # read in symbolic parameters (variables) when present
while($line = <COMFILE>) {
$line =~ s/^\s+//; # trailing blanks out!
$line =~ s/\s+$//;
$line =~ tr/a-z/A-Z/; # convert to uppercase
if(($line !~ /\S/) ||
($line =~ /const/i)) { # if blank line or end of Variables
last; # exit the loop
}
($name, $value) = @fields = split(/[ =,]+/, $line); # retrieve fields
if($#fields != 1) {
if($fields[2]=~ /s/i ){
$scanjob=1;
}
}
if(($value !~ /^[-+]?[0-9]*\.?[0-9]*$/) ||
($value !~ /[0-9]+/)) {
die "Wrong number format of Variables assignment line\n";
}
# substitute parameter in Z-Matrix. The symbolic parameter name is
# allowed to be preceded with a sign, so take this into account
$parameter_found = 0;
for($i = 0; $i < $natoms; $i++) {
if($bf[$i] == 0) {
if($blen[$i] =~ /^([-+]?)$name$/) {
$sign = $1 . "1.0";
$blen[$i] = $sign * $value;
$parameter_found = 1;
$bf[$i] = 1;
}
}
if($af[$i] == 0) {
if($angle[$i] =~ /^([-+]?)$name$/) {
$sign = $1 . "1.0";
$angle[$i] = $sign * $value;
$parameter_found = 1;
$af[$i] = 1;
}
}
if($tf[$i] == 0) {
if($tors[$i] =~ /^([-+]?)$name$/) {
$sign = $1 . "1.0";
$tors[$i] = $sign * $value;
$parameter_found = 1;
$tf[$i] = 1;
}
}
}
# parameter, if given, must appear at least once in Z-Matrix
if($parameter_found == 0) {
die "Parameter $name = $value does not appear in Z-Matrix\n";
}
}
}
# check if all symbolic paramemters were reslolved
$parms_given = 0;
for($i = 0; $i < $natoms; $i++) {
if(($bf[$i] == 0) ||
($af[$i] == 0) ||
($tf[$i] == 0)) {
$parms_given = 1; # symbolic parameters present
last;
}
}
# if all parameters are not resloved, read in the Constants section
if($parms_given == 1) { # read in symbolic parameters (variables) when present
while($line = <COMFILE>) {
$line =~ s/^\s//; # trailing blanks out!
$line =~ s/\s$//;
$line =~ tr/a-z/A-Z/; # convert to uppercase
if($line =~ /const/i) {
next;
}
if(($line !~ /\S/) ||
($line =~ /^\s*\-/)) { # if blank line or - at the front
last; # exit the loop
}
($name, $value) = @fields = split(/[ =,]+/, $line); # retrieve fields
# print "#fields is $#fields\n";
# if($#fields != 1) {
# print "fields0 is $fields[0]\n";
# print "fields1 is $fields[1]\n";
# die "Wrong format of Constants assignment line\n";
# }
if(($value !~ /^[-+]?[0-9]*\.?[0-9]*$/) ||
($value !~ /[0-9]+/)) {
die "Wrong number format on Constants assignment line\n";
}
# substitute parameter in Z-Matrix. The symbolic parameter name is
# allowed to be preceded with a sign, so take this into account
$parameter_found = 0;
for($i = 0; $i < $natoms; $i++) {
if($bf[$i] == 0) {
if($blen[$i] =~ /^([-+]?)$name$/) {
$sign = $1 . "1.0";
$blen[$i] = $sign * $value;
$parameter_found = 1;
$bf[$i] = 1;
}
}
if($af[$i] == 0) {
if($angle[$i] =~ /^([-+]?)$name$/) {
$sign = $1 . "1.0";
$angle[$i] = $sign * $value;
$parameter_found = 1;
$af[$i] = 1;
}
}
if($tf[$i] == 0) {
if($tors[$i] =~ /^([-+]?)$name$/) {
$sign = $1 . "1.0";
$tors[$i] = $sign * $value;
$parameter_found = 1;
$tf[$i] = 1;
}
}
}
# parameter, if given, must appear at least once in Z-Matrix
if($parameter_found == 0) {
die "Parameter $name = $value does not appear in Z-Matrix\n";
}
}
}
# check if all symbolic paramemters were reslolved
$parms_given = 0;
for($i = 0; $i < $natoms; $i++) {
if(($bf[$i] == 0) ||
($af[$i] == 0) ||
($tf[$i] == 0)) {
$parms_given = 1; # symbolic parameters present
last;
}
}
if($parms_given == 1) {
print STDERR
"The follwing symbolic parameters are not resolved in Z-matrix:\n";
for($i = 0; $i < $natoms; $i++) {
if($bf[$i] == 0) {
printf STDERR " %s for bond length for atom %d\n", $blen[$i], $i;
}
if($af[$i] == 0) {
printf STDERR " %s for angle value for atom %d\n", $angle[$i], $i;
}
if($tf[$i] == 0) {
printf STDERR " %s for torsion for atom %d\n", $tors[$i], $i;
}
}
exit(2);
}
# output cartesian coordinates in XYZ XMol format
for ($i = 0; $i < $natoms; $i++) {
oneatom($i+1, $bondat[$i], $blen[$i], $angat[$i], $angle[$i],
$torat[$i], $tors[$i]);
$$atom[$i] = $symbol[$i];
$$z[$i] = $Zcoor[$i+1];
$$x[$i] = $Xcoor[$i+1];
$$y[$i] = $Ycoor[$i+1];
#printf STDOUT "%-3s %12.6f %12.6f %12.6f\n",$$atom[$i],$$z[$i],$$x[$i],$$y[$i];
}
close COMFILE;
}
# -- Takes Z-mat line, i.e.,
# atomnumber, ibond, bondlen, iangle, bondangle, itors, torsangle
# and updates NONLOCAL arrays Xcoor, Ycoor, Zcoor which hold cartesian
# coordinates. It is assumed that atom numbering starts from 1
# (not from 0 as usually in C and perl)
# USES nonlocal array Xcoor, Ycoor, Zcoor where it stores coordinates
# It is assumed that angles are given in DEGREEs and bondlen in the same
# units in which cartesian coordinates will be given.
#
# ibond -- iangle
# / \
# atnum itors
#
# CAUTIONS -- contrary to the UNIX/PERL/C tradition, the
# arrays XYZ start from subscript 1 (not 0), and XYZ[0] are left unused,
# in other words, array element numbers correspond to atom numbers.
sub oneatom {
# variables are declared local, so they do not interfere with
# variables outside this subroutine
local ($atnum,
$ibond, $bondlen,
$iangle, $bondangle,
$itors, $torsangle);
local ($deg2r);
local ($rinv, $aux);
local ($xa, $ya, $za);
local ($xb, $yb, $zb);
local ($xc, $yc, $zc);
local ($xx, $yy, $zz);
$atnum = $_[0];
if($atnum == 2) {
($atnum, $ibond, $bondlen) = @_;
}
elsif ($atnum == 3) {
($atnum, $ibond, $bondlen, $iangle, $bondangle) = @_;
}
elsif ($atnum > 3) {
($atnum, $ibond, $bondlen, $iangle, $bondangle, $itors, $torsangle) = @_;
}
$deg2r = atan2(1.0, 1.0)*4.0/180.0;
# first atom in the origin of coordinate system
if($atnum == 1) {
$Xcoor[1] = 0.0;
$Ycoor[1] = 0.0;
$Zcoor[1] = 0.0;
return;
}
# second atom on Z-axis, bondlen from the first atom
if($atnum == 2) {
$Xcoor[2] = 0.0;
$Ycoor[2] = 0.0;
$Zcoor[2] = $bondlen;
return;
}
# bonded atom coordinates
$x0 = $Xcoor[$ibond];
$y0 = $Ycoor[$ibond];
$z0 = $Zcoor[$ibond];
if($atnum == 3) { # third atom placed on the Oxz plane
$Xcoor[3] = $x0+$bondlen*sin($bondangle*$deg2r);
$Ycoor[3] = 0.0;
$aux = $bondlen*cos($bondangle*$deg2r);
if($ibond == 1) {
$Zcoor[3] = $z0+$aux;
}
elsif ($ibond == 2) {
$Zcoor[3] = $z0-$aux;
}
return;
}
# vector from j-->k
$xx = $Xcoor[$iangle] - $Xcoor[$ibond];
$yy = $Ycoor[$iangle] - $Ycoor[$ibond];
$zz = $Zcoor[$iangle] - $Zcoor[$ibond];
# inverted vector length
$rinv = 1.0/sqrt($xx*$xx + $yy*$yy + $zz*$zz);
# unit vector ibond--->iangle