-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdumptools.pl
1606 lines (1468 loc) · 44.9 KB
/
dumptools.pl
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
# Author: Otello M Roscioni
# email [email protected]
# Last revision: 9 December 2022
#
# Unauthorized copying of this file, via any medium is strictly prohibited.
# Proprietary and confidential.
#
# Copyright (C) MaterialX LTD - All Rights Reserved.
use Time::Piece;
use File::Basename;
# Default values
$start=localtime(time);
$frame="";
$stype=0;
$deg2rad=3.14159265358979/180.;
$half2rad=3.14159265358979/360.;
# numerical tolerance for zero.
$tol=1e-6;
%mode=(
polymer => 0,
count => 0,
save => 0,
wrap => 0,
fix => 0,
merge => 0,
supercell => 0,
external => 0,
select => 0,
delete => 0,
do => 0,
debug => 0
);
$help="Usage: dumptools [options] file.dump [file2.dump]\n
Perform a series of operations on a DUMP file of the MOLC model.
Spatial operations can be performed on the whole sample or on a
subset of atoms or molecules, selected with the options -m.
The selection interval can be specified as individual
numbers separated by commas (such as 1,2,5) or with the
syntax start:end:(step). The interval sintax allows mixing,
e.g. 1:4,7,10 will select the molecules: 1 2 3 4 7 10.\n
Different tasks are available. To avoid unwanted results, just select
one of the following tasks:\n
*** Polymer Patch ***
Transform the first and last monomer's atom type into a new type, to be
used as a capping residue. If an atom type i is selected to become a
capping residue, the patched atom types will be:
FIRST N+2*i-1
END N+2*i
Where N is the number of atom types in the sample.
Example:
dumptools -p 1 -p 2 -p 3 file.dump
Here, 3 atom types are present in the input system and all of them can be
on the polymer chain.
For each type, 2 more types will be created depending if the monomer is
the first (left) or last (right) of the chain. Conversion Table:
***********************
* TYPE * FIRST * LAST *
***********************
* 1 * 4 * 5 *
* 2 * 6 * 7 *
* 3 * 8 * 9 *
***********************
INPUT:
1-1-1-1-1-1-1 2-1-2-1-2-1-2 2-1-1-2-1-1-1 1-3-1-3-1-2-1 3-3-1-1-2-2
OUTPUT:
4-1-1-1-1-1-5 6-1-2-1-2-1-7 6-1-1-2-1-1-5 4-3-1-3-1-2-5 8-3-1-1-2-7\n
NOTE: When using the polymer patch mode, the program won't perform any
transformation on the coordinates such as wrapping, translation or
rotation.\n
*** Frame Count ***
Print the number of frames in the input system and other statistics.
Example:
dumptools -c file.dump\n
*** Post-processing ***
Perform one of the following tasks on the selected frames:
* distance
Example:
dumptools -a distance file.dump\n
*** Spatial operations ***
Translate, rotate, wrap specific molecules or the whole sample. These
operations are available when a frame is selected to be saved. Multiple
translations and rotations can be specified and will be executed in
that order.
Examples:
dumptools -r 90,0,1,0 -t 10,10,0 -m 200:500 -f 12000 file.dump
dumptools -w file.dump
Expression selection is applied after the spatial operations. Logic
operators and mathematical operations are applied to the Cartesian
coordinates to extract a subset of molecules. The original molid is
maintained, and the number of atoms updated. It worls only for single
configurations! Example:
dumptools file.dump -e \"(x**2+y**2)<500\"\n
*** Merge ***
Two or more files will be merged together. The input file must all have
the same data structure, i.e. same number of records in the ATOMS section.
Spatial operations can be used as well, but special care should be paid
when selecting specific molecules, as their value will refer to the
merged value. The option -f is not available in this mode.
The box vectors are taken from the first DUMP file provided.
Example:
dumptools -g s -o merged file1.dump file2.dump file3.dump\n
*** Supercell ***
Create a supercell. Example:
dumptools -s 1,2,2 -f 1 file.dump\n
Option Description
------------------------------------------------------------------------
-h, --help Print this help.
-a [task] Apply one of the following post-processing tasks on the
selected frames (default=use all frames):
distance Compute the distance between atom types using
the minimum image convention.
-o [basename] Name of the output file. Default=basename(input)_f\%i.dump
-p [integer] Patch the atom types belonging to a polymer. This option
can be repeated multiple times.
-c Print the number of frames and atoms in the input file.
This mode disables the writing of a new file.
-f [interval] Save the selected frame. An interval can also be provided,
extracting a sub-trajectory. Input values are flexible:
\"last\", \"first\", and \"all\" are valid keywords;
-1 stands for the last frame, 0 for all frames.
-m [interval] Apply the operation to the specific molecule id(s).
Default=all.
-x Delete the selected molecules.
-t [x,y,z] Translate using the input vector.
-r [a,x,y,z] Rotate by an angle a around the vector x,y,z.
-s [a,b,c] Create a supercell. Each dimension must be at least 1.
-e \"condition\" Expression select: print only the molecules or atoms
satisfying a logic operation on coordinates or atom types.
Examples: \"x > -10 & y < 7 & z >= 0\" or \"(x**2 + (y-4)**2) < 40\"
\"t < 3 || t == 5\" or \"x<300 && t == 1\".
-w Wrap molecules to the bounding box without splitting them.
It supports the atom styles:
* hybrid full ellipsoid
* hybrid molecular ellipsoid
* ellipsoid
-u Fix wrapped coordinates of multi-bead molecules. For some
cases,this option needs to be used twice, i.e. wrap again
the resulting dump file.
-g [s|a] Merge two or more files in a single dump. For this task,
only the first frame is used. Arguments are:
* s Sequence the atom types, e.g. 1 2 3 1 2 to 1 2 3 4 5.
* a Append the atom type without changes (default).
-d Debug mode.\n";
# Check the input arguments
for my $i(0 .. $#ARGV){
if (-f $ARGV[$i]){push @files, $ARGV[$i];}
elsif ($ARGV[$i] eq "-o"){
$basename=$ARGV[$i+1];
$mode{external}=1;
}
elsif ($ARGV[$i] eq "-p"){
$mode{polymer}=1;
push @type, $ARGV[$i+1];
}
elsif ($ARGV[$i] eq "-f"){$frame=$ARGV[$i+1];}
elsif ($ARGV[$i] eq "-m"){@molecule=&decomma($ARGV[$i+1]);}
elsif ($ARGV[$i] eq "-t"){
my @translate=&decomma($ARGV[$i+1]);
unshift (@translate, "T"); # Make the array 4 elements long.
push @operations, @translate if $#translate == 3;
$mode{save}=1;
}
elsif ($ARGV[$i] eq "-r"){
my @rotate=&decomma($ARGV[$i+1]);
push @operations, @rotate if $#rotate == 3;
$mode{save}=1;
}
elsif ($ARGV[$i] eq "-s"){
@replica=&decomma($ARGV[$i+1]);
$replica[$_]-=1 for 0 .. 2;
$mode{supercell}=1;
$mode{save}=1;
if ($replica[0] < 0 || $replica[1] < 0 || $replica[2] < 0 ){
die "ERROR: A valid supercell must be greater than 1,1,1\n";
}
}
elsif ($ARGV[$i] eq "-e"){
$expression=$ARGV[$i+1];
$expression =~ s/x/\$v[0]/gi;
$expression =~ s/y/\$v[1]/gi;
$expression =~ s/z/\$v[2]/gi;
$expression =~ s/t/\$v[3]/gi;
$expression =~ s/\\//g;
$mode{select}=1;
$mode{save}=1;
}
elsif ($ARGV[$i] eq "-c"){$mode{count}=1;}
elsif ($ARGV[$i] eq "-w"){
$mode{wrap}=1;
$mode{save}=1;
}
elsif ($ARGV[$i] eq "-u"){
$mode{wrap}=1;
$mode{fix}=1;
$mode{save}=1;
}
elsif ($ARGV[$i] eq "-g"){
$mode{merge}=1;
$stype=1 if $ARGV[$i+1] =~ /^s$/i;
}
elsif ($ARGV[$i] eq "-x"){
$mode{delete}=1;
$mode{save}=1;
}
elsif ($ARGV[$i] eq "-a"){
$mode{do}=$ARGV[$i+1];
$mode{save}=0;
}
elsif ($ARGV[$i] eq "-d"){$mode{debug}=1;}
elsif ($ARGV[$i] eq "-h"||$ARGV[$i] eq "--help"){die $help;}
}
$mode{save}=1 if ($frame && !$mode{polymer});
$mode{save}=0 if ($mode{merge}); # Overwrite inconsistent input.
$frame=1 if ($mode{count} && !$frame);
if ($mode{do} =~ /distance/i){
$mode{save}=0;
$frame=0 if !$frame;
}
# Sort the array containing the atom types for polymers.
@type = sort {$a <=> $b} @type if $#type > -1;
# State which molecules will be analysed.
if ($mode{debug} && $#molecule > -1){
print STDERR "Molecules selected: ";
foreach (@molecule){print STDERR "$_ "};
print STDERR "\n";
}
print "\nExpression Selection: $expression\n" if ($mode{debug} && $mode{select});
# Verify that the input file has been specified.
if ($#files<0){
print "ERROR: input file not specified.\n";
exit;
}
# Compute the rotation matrix and quaternion.
if ($#operations>-1){
my $op=($#operations+1)/4 - 1;
for my $i(0 .. $op){
my $j=4*$i;
if ($operations[$j] ne "T"){
my @rotate;
$rotate[$_]=$operations[$j+$_] for 0 .. 3;
my @rm=&rotmat(\@rotate);
my @rq=&rotquat(\@rotate);
push @rm_g, @rm;
push @rq_g, @rq;
}
}
}
# Read the header of the DUMP and store it into a hash.
# The hash is shared also for the $mode{merge}, assuming that
# all the DUMP files have the same structure.
$input=$files[0];
@item_atoms=&field(`awk '/^ITEM: ATOMS/{print; exit}' $input`);
shift @item_atoms for 0 .. 1;
$record{$item_atoms[$_]}=$_ for 0 .. $#item_atoms;
# Process the data header.
@pos=(-1,-1,-1);
for (0 .. $#item_atoms){
if ($item_atoms[$_] =~ /^x/i){
$pos[0]=$_;
} elsif ($item_atoms[$_] =~ /^qw/i || $item_atoms[$_] =~ /^quatw/i || $item_atoms[$_] eq "c_q[1]" || $item_atoms[$_] eq "c_orient[4]"){
$pos[1]=$_;
$pos_quat[0]=$_;
} elsif ($item_atoms[$_] =~ /^qx/i || $item_atoms[$_] =~ /^quatx/i || $item_atoms[$_] eq "c_q[2]" || $item_atoms[$_] =~ /^quati/i || $item_atoms[$_] eq "c_orient[1]"){
$pos_quat[1]=$_;
} elsif ($item_atoms[$_] =~ /^qy/i || $item_atoms[$_] =~ /^quaty/i || $item_atoms[$_] eq "c_q[3]" || $item_atoms[$_] =~ /^quatj/i || $item_atoms[$_] eq "c_orient[2]"){
$pos_quat[2]=$_;
} elsif ($item_atoms[$_] =~ /^qx/i || $item_atoms[$_] =~ /^quatz/i || $item_atoms[$_] eq "c_q[4]" || $item_atoms[$_] =~ /^quatk/i || $item_atoms[$_] eq "c_orient[3]"){
$pos_quat[3]=$_;
} elsif ($item_atoms[$_] =~ /^type/i){
$pos[2]=$_;
}
}
if ($mode{debug}){
print STDERR "\nRecords in the ATOMS section:\n";
printf STDERR "%2i %s\n",$_,$item_atoms[$_] for 0 .. $#item_atoms;
printf STDERR "X column: %i\n",$pos[0];
printf STDERR "QW column: %i\n\n",$pos[1];
}
# Read the timestep of the first last frame.
$time[0]=`awk '{if(e==1){print \$1; exit};if(/^ITEM: TIMESTEP/){e=1}}' $input`;
chomp $time[0];
$tail=`awk '{if(e==1){print \$1+9; exit};if(/^ITEM: NUMBER OF ATOMS/){e=1}}' $input`;
chomp $tail;
$time[1]=0;
$stop=0;
while($time[1]==0 && $stop==0){
my $last_frame=`tail -$tail $input | awk '{if(e==1){print \$1; exit};if(/^ITEM: TIMESTEP/){e=1}}'`;
chomp $last_frame;
$stop=`tail -$tail $input | awk '/^ITEM: TIMESTEP/{print 1; exit}'`;
chomp $stop;
if ($last_frame){
$time[1]=$last_frame;
} else {
# Take care of gran-canonical simulations.
$tail*=2;
}
}
# Process the input frames. Use @trj to identify the frame number,
# and $frame the first or last timestep, which we need to locate
# while reading the DUMP file.
if ($frame =~ /[:,]+/i){
@trj=&decomma($frame);
if ($mode{debug}){
printf STDERR "%i frames were selected for saving:\n",$#trj+1;
printf STDERR "%i ",$trj[$_] for 0 .. $#trj;
print STDERR "\n";
}
$frame=-2;
} elsif ($frame eq -1 || $frame=~/^last/i){
$frame=$time[1];
$trj[0]=-1;
} elsif ($frame eq 0 || $frame=~/^all/i){
$trj[0]=0;
$frame=-3;
} elsif ($frame eq 1 || $frame=~/^first/i){
$frame=$time[0];
$trj[0]=-1;
} elsif ($frame>1){
$trj[0]=$frame;
$frame=-1;
} else {
printf "ERROR: Invalid frame specified: %s\n\n",$frame;
exit;
}
# Open the required files for writing the output.
if ($mode{save} && !$mode{count}){
if ($basename eq ""){
$output=basename($input);
$output =~ s/\.dump[\w]*+$//;
my $suff;
if ($frame == -3 && $trj[0]==0){
$suff="all";
} elsif ($frame == -2 && $#trj>0) {
$suff=sprintf("f%i-%i",$trj[0],$trj[$#trj]);
} elsif ($frame == -1 && $trj[0]>1){
$suff=sprintf("f%i",$trj[0]);
} elsif ($frame >= 0 && $trj[0] == -1){
$suff=sprintf("t%i",$frame);
}
$output=sprintf("%s_%s.dump",$output,$suff);
} else {
$output=$basename.".dump";
}
open(OUT,">$output");
}
if ($mode{polymer} && !$mode{count}){
my $output=$basename;
$output=basename($input) if $basename eq "";
$output =~ s/\.dump[\w]*+$//;
if ($mode{external}){
$output .= ".dump";
} else {
$output .= "_polypatch.dump";
}
open(OUT,">$output");
}
# Read the dump file and process the desired frame(s),
# storing the data into an array-of-arrays.
$mode{save}=0 if ($mode{supercell});
if (!$mode{merge}){
open(DUMP,"<$input") || die "File $input not found.\n";
$fc=0; # Frame counter.
$fi=0; # @trj frame index.
$ac=0; # Atoms counter.
$go=0; # Switch for processing the current frame.
$box=0; # Switch for reading the box vectors.
$stop=-1;
my $noa=4; # line index in the DUMP telling the number of atoms (in case of grancanonical).
while(my $line=<DUMP>){
# Timestep interval
if ($tsc){
$tsc=0;
chomp $line;
push @timestep, $line;
}
if ($line =~ /^ITEM: TIMESTEP/){
$tsc++;
# Frame counter: from 1 to last.
$fc++;
}
# Number of Atoms. It allows working with gran-canonical simulations,
# where the number of particles changes over time.
if ($atc){
$atc=0;
chomp $line;
push @natm, $line;
}
$atc++ if $line =~ /^ITEM: NUMBER OF ATOMS/;
# Read the current frame.
# When $frame is used for the first or last frame, the eq operator must be used:
# print "You should have not seen this message!\n" if $frame == $timestep[$fc-1]; # true when @timestep is undefined.
if ($trj[$fi]==$fc || $frame eq $timestep[$fc-1] || $trj[0]==0){
my @data=&field($line);
#print STDERR "DEBUG $line \n";
# Store the header.
if (!$go){
push @header, $line;
$go=1 if $line =~ /^ITEM: ATOMS/;
# PBC vectors.
push @bb,$line if ($box && !$go);
$box=1 if ($line =~ /^ITEM: BOX BOUNDS/);
$stop=$natm[$fc-1] if $go;
} else {
# Store the atoms section.
push @coord, \@data;
$ac++;
# Store the number of atom types in the current frame.
@ftype=&unique(\@ftype,$data[$record{type}]);
}
# Process the current frame.
if ($ac==$stop && $go){
$go=0;
$ac=0;
$box=0;
$fi++;
# Sort the AoA containing the coordinates w.r.t. the index column.
@coord = sort {$a->[0] <=> $b->[0]} @coord;
# do the same for a simple array.
@ftype = sort {$a <=> $b} @ftype;
# Check that the frame has been read correctly.
if ($mode{debug}){
printf STDERR "\n";
printf STDERR "Current line: %s\n",$line;
printf STDERR "Current frame: %i\n",$fc;
printf STDERR "Total atom types found: %i\nTypes: ",$#ftype+1;
printf STDERR "%i ",$ftype[$_] for 0 .. $#ftype;
printf STDERR "\n";
printf STDERR "Bounding Box: ";
printf STDERR "%s ",$bb[$_] for 0 .. 2;
printf STDERR "\n";
printf STDERR "First entry of \@coord: ";
my @test=@{$coord[0]};
printf STDERR "%s ",$test[$_] for 0 .. $#test;
printf STDERR "\n";
printf STDERR "Last entry of \@coord: ";
@test=@{$coord[$#coord]};
printf STDERR "%s ",$test[$_] for 0 .. $#test;
printf STDERR "\n";
printf STDERR "Number of molecules: %i\n",$test[$record{mol}] if $record{mol};
}
# Print min and max coordinates, only if one frame is being analysed.
if ($mode{count} && $trj[0] != 0){
# Find min-max coordinates.
my @min=(1e10,1e10,1e10);
my @max=(-1e10,-1e10,-1e10);
for my $i(0 .. $#coord){
for my $j(0 .. 2){
$min[$j]=$coord[$i][$pos[0]+$j] if $coord[$i][$pos[0]+$j]<$min[$j];
$max[$j]=$coord[$i][$pos[0]+$j] if $coord[$i][$pos[0]+$j]>$max[$j];
}
}
printf "Current frame: %i\n",$fc;
print "Coordinates: MIN MAX\n";
printf "X %15g %15g\n",$min[0],$max[0];
printf "Y %15g %15g\n",$min[1],$max[1];
printf "Z %15g %15g\n",$min[2],$max[2];
}
# This line was skipped and must be reintroduced (only when saving the first and last frame).
unshift(@header, "ITEM: TIMESTEP") if $frame > -1;
@bb=&box(\@bb);
my $atoms=&frame_save(\@header,\@bb,\@coord) if ($mode{save} && !$mode{count});
&patch_polymer(\@header,\@bb,\@coord,\@ftype) if ($mode{polymer} && !$mode{count});
&replica(\@header,\@bb,\@coord) if ($mode{supercell} && !$mode{count});
# Patch the number of atoms, if a selection has been made.
if ( !$mode{count} && ($mode{select}||$mode{delete}) ){
close(OUT);
system("sed -i '${noa}s/^$header[3]/$atoms/' $output");
print STDERR "Executing: sed -i '${noa}s/^$header[3]/$atoms/' $output\n" if $mode{debug};
open(OUT,">>$output");
$noa+=$atoms+9;
}
# Post-processing.
if ($mode{do} =~ /distance/i){
my @test=&distance(\@bb,\@coord,\@ftype);
push @distance, @test;
@types=@ftype;
}
# Reset the frame storage arrays.
undef @header;
undef @bb;
undef @coord;
undef @ftype;
# Break out the while loop if the job is done.
last if ($trj[$#trj]==$fc || $frame==$timestep[$fc-1]) && (!$mode{count});
}
}
}
close(DUMP);
close(OUT) if ($mode{save} || $mode{polymer} || $mode{supercell});
}
# Print stuff.
if ($mode{count}){
printf "\nNumber of frames read: %i\n",$fc;
printf "Timespan: %i-%i\n",$time[0],$time[1];
my $df=$fc;
$df=($timestep[$#timestep]-$timestep[0])/($fc-1) if $fc>1;
printf "Dumping Frequency: %i\n",$df;
printf "Atoms in the first frame: %i\n",$natm[0];
printf "Atoms in the last frame: %i\n",$natm[$#natm] if $fc>1;
}
# Print more stuff.
if ($mode{do}){
my $output=$basename;
$output=basename($input) if $basename eq "";
$output =~ s/\.dump[\w]*+$//;
$output .= ".postprocess";
open(OUT,">$output");
# Print the header.
printf OUT "# Distance: ";
my $c=0;
for my $i(0 .. $#types){
for my $j($i .. $#types){
printf OUT "%i-%i ",$types[$i],$types[$j];
$c++;
}
}
$c--;
printf OUT "\n";
# Print the table.
for my $i(0 .. $#distance){
for my $j(0 .. $c){
if ($distance[$i][$j] =~ /\d/){
printf OUT "%8g ",$distance[$i][$j];
} else {
printf OUT "-%7s","";
}
}
printf OUT "\n";
}
close(OUT);
}
# Merge two or more DUMP files.
if ($mode{merge}){
my $output=$basename;
my @header;
my @bb;
my @tcoord;
if ($basename eq ""){
$output .= basename($files[$_]) for 0 .. $#files;
$output =~ s/\.dump[\w]*+$/_/g;
}
if ($mode{external}){
$output .= ".dump";
} else {
$output .= "_merged.dump";
}
open(OUT,">$output");
# Read all the input files and accumulate the different sections.
# Each file should contain only one frame, otherwise the program will be sad :(
for my $i(0 .. $#files){
# Open the current file.
open(DUMP,"<$files[$i]") || die "File $files[$i] not found.\n";
my $go=1;
my $box=0;
my $natm=0;
while(my $line=<DUMP>){
# Store the header.
if ($go){
# Check that the number of recors does not change.
if ($line =~ /^ITEM: ATOMS/){
chomp $line;
push @header, $line;
$go=0;
}
# PBC vectors.
push @bb,$line if ($box && $go);
$box=1 if ($line =~ /^ITEM: BOX BOUNDS/);
} else {
# Stop reading after the first frame.
if ($line =~ /^ITEM: TIMESTEP/){
printf STDERR "WARNING: only the first frame of file %s was used.\n",$files[$i];
last;
}
# Store the atoms section.
my @data=&field($line);
push @tcoord, \@data;
$natm++;
# Store the number of atom types in the current frame.
@ftype=&unique(\@ftype,$data[$record{type}]) if $stype;
}
}
# Sort the AoA containing the coordinates w.r.t. the index column.
@tcoord = sort {$a->[0] <=> $b->[0]} @tcoord;
push @coord, @tcoord;
undef @tcoord;
close(DUMP);
push @natm_g, $natm;
if ($stype){
# Save the atom types into a unique sequence.
@stype=&append(\@stype,\@ftype);
push @tindex, $#ftype;
# Reset the frame storage arrays.
undef @ftype;
}
}
# Check that the DUMP files were written with the same number of fields.
for (1 .. $#header){
printf STDERR "WARNING: ATOMS section of file %s has differ from %s\n",
$files[$_],$files[0] if $header[$_]!=$header[0];
}
if ($mode{debug} && $stype){
printf STDERR "\nOutput atom types: ";
printf STDERR "%i ",$stype[$_] for 0 .. $#stype;
printf STDERR "\n";
}
# 1. Process the header.
my $natm=0;
$natm+=$natm_g[$_] for 0 .. $#natm_g;
my $volume=0;
my $prev=0;
my $big=0;
if ($mode{debug}){
print STDERR "Bounding Boxes\n";
printf STDERR "%i %s",$_,$bb[$_] for 0 .. $#bb;
print STDERR "File Volume\n";
}
# Find the biggest bounding box. (DISCARDED).
#for my $i(0 .. $#files){
# my $j=$i*3;
# my @box;
# $box[$_]=$bb[$j+$_] for 0 .. 2;
# @box=&box(\@box);
# $volume=&volume(\@box);
# printf STDERR "%4i %g\n",$i,$volume if $mode{debug};
# if ($volume>$prev){
# $prev=$volume;
# $big=$j;
# }
#}
my @header2=(
"ITEM: TIMESTEP",
0,
"ITEM: NUMBER OF ATOMS",
$natm,
"ITEM: BOX BOUNDS pp pp pp"
);
my @bb2;
#$bb2[$_]=$bb[$big+$_] for 0 .. 2;
# Use the bounding box of the first DUMP file.
$bb2[$_]=$bb[$_] for 0 .. 2;
@bb2=&box(\@bb2);
push @header2, "dummy" for 0 .. 2;
push @header2, $header[0];
# 2. Process the coordinates.
my @coord2;
if (!$stype){
@coord2=&merge1(\@coord);
} else {
@coord2=&merge2(\@coord,\@stype,\@tindex,\@natm_g);
}
# 3. Write the output (easy!).
&frame_save(\@header2,\@bb2,\@coord2);
close(OUT);
}
# Let us know how slow perl is.
&endtime;
# Save the selected frame.
sub frame_save {
# Dereference the input arrays.
my @header=@{ $_[0] };
my @bb=@{ $_[1] };
my @coord=@{ $_[2] };
# Print the header.
printf OUT "%s\n",$header[$_] for 0 .. $#header-4;
# Print the bounding box.
&cm_format(\@bb);
printf OUT "%s\n",$header[$#header];
# Process the frame.
my $next=0; # select the current molecule.
my $old=$coord[0][$record{mol}]; # if the system has holes in the molecule index, e.g. if the dump contains a selection of the whole system.
my $aid=1;
my $mid=1;
my @coord2;
for my $i(0 .. $#coord){
# Dereference the AoA into a separate array.
my @data=@{ $coord[$i] };
$next++ if $molecule[$next+1] == $data[$record{mol}];
# Perform additional operations on the coordinates.
my $go=0;
$go=1 if (!$record{mol} || $#molecule == -1 || $molecule[$next] == $data[$record{mol}]);
if ($#operations>-1 && $go){
my $op=($#operations+1)/4 - 1;
my $rot=0; # Counter for rotations.
for my $j(0 .. $op){
my $start=4*$j;
if ($operations[$start] eq "T"){
# Translation.
$data[ $pos[0]+$_ ] += $operations[$start+$_+1] for 0 .. 2;
} else {
# Rotation.
@data=&rot_body(\@data,$rot);
$rot++;
}
}
}
# Print the data.
if (!$record{mol}){
# simple wrap for single ellipsoids.
@data=&wrap_single(\@data,\@bb) if $mode{wrap};
# Expression Select.
if ($mode{select}){
my @v;
$v[$_]=$data[$pos[0]+$_] for 0 .. 2;
$v[3]=$data[$pos[2]];
if (eval($expression)){
printf OUT "%g ",$aid;
printf OUT "%g ",$data[$_] for 1 .. $#data;
print OUT "\n";
$aid++;
}
} else {
printf OUT "%g ",$aid;
printf OUT "%g ",$data[$_] for 1 .. $#data;
print OUT "\n";
$aid++;
}
} elsif ($data[$record{mol}] != $old){
# process the whole molecule.
@coord2=&wrap(\@coord2,\@bb) if $mode{wrap};
# Delete only selected molecules.
my $check = 1;
#$check = 0 if ($molecule[$next-1]==$old && $mode{delete} && $#molecule > -1 ); # Apparently is a bug.
$check = 0 if ($molecule[$next]==$old && $mode{delete} && $#molecule > -1 );
# Expression Select.
if ($mode{select}){
# Use the first ellipsoid for the expression selection.
my @v;
$v[$_]=$coord2[0][$pos[0]+$_] for 0 .. 2;
$v[3]=$coord2[0][$pos[2]];
if (eval($expression) && $check){
#printf STDERR "Molecule selected: %s Position: %.2f %.2f %.2f\n",
#$data[$record{mol}],$v[0],$v[1],$v[2] if ($mode{debug});
for my $j(0 .. $#coord2){
my @data2=@{ $coord2[$j] };
# Overwrite the old molecular index with a sequential one.
# Maybe there are cases where we want to keep the original ones:
# if this necessity arises, make this substitution optional.
$data2[$record{mol}]=$mid;
printf OUT "%g ",$aid;
printf OUT "%g ",$data2[$_] for 1 .. $#data2;
print OUT "\n";
$aid++;
}
$mid++;
}
} elsif($check) {
for my $j(0 .. $#coord2){
my @data2=@{ $coord2[$j] };
printf OUT "%g ",$aid;
printf OUT "%g ",$data2[$_] for 1 .. $#data2;
print OUT "\n";
$aid++;
}
}
# Update the molecule.
$old=$data[$record{mol}];
undef @coord2;
}
# Store the molecule, if the record is present.
push @coord2, \@data if $record{mol};
}
# Print the last molecule.
@coord2=&wrap(\@coord2,\@bb) if $mode{wrap};
# Delete only selected molecules.
my $check = 1;
$check = 0 if ($molecule[$next]==$old && $mode{delete} && $#molecule > -1 );
# Expression Select.
if ($mode{select} && $record{mol}){
# Use the first ellipsoid for the expression selection.
my @v;
$v[$_]=$coord2[0][$pos[0]+$_] for 0 .. 2;
$v[3]=$coord2[0][$pos[2]];
if (eval($expression) && $check){
#printf STDERR "Molecule selected: %s\nPosition: %.2f %.2f %.2f\n",
#$data[$record{mol}],$v[0],$v[1],$v[2] if ($mode{debug});
for my $j(0 .. $#coord2){
my @data2=@{ $coord2[$j] };
$data2[$record{mol}]=$mid;
printf OUT "%g ",$aid;
printf OUT "%g ",$data2[$_] for 1 .. $#data2;
print OUT "\n";
$aid++;
}
}
} elsif($check) {
for my $j(0 .. $#coord2){
my @data2=@{ $coord2[$j] };
printf OUT "%g ",$aid;
printf OUT "%g ",$data2[$_] for 1 .. $#data2;
print OUT "\n";
$aid++;
}
}
$aid--;
return $aid;
}
# Patch the atom types for a polymer.
sub patch_polymer {
# Dereference the input arrays.
my @header=@{ $_[0] };
my @bb=@{ $_[1] };
my @coord=@{ $_[2] };
my @ftype=@{ $_[3] };
my $aid=1;
# Annoy the user if inconsistent options were selected :D
print STDERR "WARNING: translations and rotations are not available in polymer-patch mode.\n" if $#operations>-1;
print STDERR "WARNING: wrapping is not available in polymer-patch mode.\n" if $mode{wrap};
print STDERR "WARNING: no fixing wrapped coordinates in polymer-patch mode!\n" if $mode{fix};
# Determine the new atom types to be created.
my $new=2;
for my $i (0 .. $#type){
$ntype[ $type[$i] ]{first}=$#ftype+$new;
$new++;
$ntype[ $type[$i] ]{last}=$#ftype+$new;
$new++;
}
# Print a summary table
print "***********************\n";
print "* TYPE * FIRST * LAST *\n";
print "***********************\n";
printf "* %4i * %4i * %4i *\n",
$type[$_],$ntype[ $type[$_] ]{first},$ntype[ $type[$_] ]{last} for 0 .. $#type;
print "***********************\n";
# Print the header.
printf OUT "%s\n",$header[$_] for 0 .. $#header-4;
# Print the bounding box.
&cm_format(\@bb);
printf OUT "%s\n",$header[$#header];
my $mol=0;
for my $i(0 .. $#coord){
my @data=@{ $coord[$i] };
my @next=@{ $coord[$i+1] };
# check when the molecule changes:
my $go=&patch($data[$record{type}]);
if ($mol ne $data[$record{mol}] && $go eq 1){
# Patch the FIRST monomer of the polymer chain.
$data[$record{type}]=$ntype[ $data[$record{type}] ]{first};
$mol=$data[$record{mol}];
} elsif ($mol ne $next[$record{mol}] && $go eq 1){
# Patch the LAST monomer of the polymer chain.
$data[$record{type}]=$ntype[ $data[$record{type}] ]{last};
}
# Print the line after modifications.
printf OUT "%g ",$aid;
printf OUT "%g ",$data[$_] for 1 .. $#data;
print OUT "\n";
$aid++;
}
}
# Append new atom type to the current array.
sub append {
# Dereference the input array.
my @out=@{ $_[0] };
my @inp=@{ $_[1] };
my @tmp;
my $last=$out[$#out];
for my $i (0 .. $#inp){
my $go=1;
for my $j (0 .. $#out){
$go=0 if $out[$j] == $inp[$i];
}
if ($go){
push @tmp, $inp[$i];
} else {
$last++;
push @tmp, $last;
}
}
if ($mode{debug}){
printf STDERR "Input types: ";
printf STDERR "%i ",$out[$_] for 0 .. $#out;
printf STDERR "\n";
printf STDERR "Output types: ";
printf STDERR "%i ",$tmp[$_] for 0 .. $#tmp;
printf STDERR "\n";
}
push @out, @tmp;
return @out;
}
# Merge the record ID and MOL.
sub merge1 {
# Dereference the input array-of-arrays.
my @coord=@{ $_[0] };
# Process the frame.
my $id=0;
my $mol=0;
my $old=-1;
for my $i(0 .. $#coord){
$id++;
$coord[$i][ $record{id} ]=$id;
if ($coord[$i][ $record{mol} ] != $old){
$old=$coord[$i][ $record{mol} ];
$mol++;
}
$coord[$i][ $record{mol} ]=$mol;
}
return @coord;
}
# Merge the record ID, TYPE, and MOL.
sub merge2 {
# Dereference the input array-of-arrays.
my @coord=@{ $_[0] };
my @stype=@{ $_[1] };
my @tindex=@{ $_[2] };
my @natm=@{ $_[3] };
# Process the frame.
my $id=0;
my $mol=0;
my $old=-1;
my $next=$natm[0]-1;
my $d=0;
my $shift=-1;
for my $i(0 .. $#coord){
$id++;
$coord[$i][ $record{id} ]=$id;
if ($coord[$i][ $record{mol} ] != $old){
$old=$coord[$i][ $record{mol} ];
$mol++;
}
$coord[$i][ $record{mol} ]=$mol;
# Transform the old TYPE to the STYPE.
if ($i>$next){
$shift+=$tindex[$d]+1;