Subversion Repositories wimsdev

Rev

Rev 13418 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13403 bpr 1
#!/bin/sh
2
 
3
exec perl <<'EOF'
4
 
5
use strict "vars";
6
use warnings;
7
 
8
push (@ARGV,split(' ', $ENV{'wims_exec_parm'})) if ($ENV{'wims_exec_parm'});
9
my ($file)=shift @ARGV;
10
 
11
my @valence1=('Li','Na','K','F','Cl','Br','I','At');
12
my @valence2=('Be','Mg','Ca','Sr','Ba','O','Se','Te','Mn','Hg','Zn','Cu','S');
13
my @valence3=('B','Al','N','P','As','Fe');
14
my @valence4=('C','Si','Ge','Sn','A','Q');
15
 
16
my ($nbatom, $nbbond);
17
my $cnt=1; my $nH=0;
18
my %hash = ();
19
my $hash = \%hash;
20
my %atom = ();
21
my $atom = \%atom;
22
my $cntmax=10000;
23
my @a=();
24
open(IN, "$file");
25
while(<IN>) {
26
  @a=split(" ", $_);
27
  if($cnt==4){
28
    $nbatom=$a[0]; $nbbond=$a[1];$cntmax=4+$nbatom;
29
  };
30
  if($cnt>4 && $cnt <= $cntmax) {
31
    $hash->{$cnt-4}{name}=$a[3];
32
    if(!$atom->{$a[3]}) { $atom->{$a[3]}=1} else { $atom->{$a[3]} ++}
33
    if ( grep { $_ eq $a[3] } @valence4 ){ $hash->{$cnt-4}{valence}=4}
34
    elsif ( grep { $_ eq $a[3] } @valence1 ){ $hash->{$cnt-4}{valence}=1}
35
    elsif ( grep { $_ eq $a[3] } @valence2 ){ $hash->{$cnt-4}{valence}=2}
36
    elsif ( grep { $_ eq $a[3] } @valence3 ){ $hash->{$cnt-4}{valence}=3}
37
    else {
38
    #die( "$a[3]");
39
    }
40
  }
41
  if ($cnt > $cntmax && $cnt <= $cntmax + $nbbond) {
42
    if (!($hash->{$a[0]}{name} =~ /H\b/) && !($hash->{$a[1]}{name} =~ /H\b/)) {
43
      if(!(defined $hash->{$a[0]}{hydrogen})) {
44
        $hash->{$a[0]}{hydrogen}=$hash->{$a[0]}{valence}-$a[2];
45
    } else {
46
        $hash->{$a[0]}{hydrogen}-= $a[2]
47
    }
48
      if(!(defined $hash->{$a[1]}{hydrogen})) {
49
        $hash->{$a[1]}{hydrogen}=$hash->{$a[1]}{valence}-$a[2]
50
    } else {
51
        $hash->{$a[1]}{hydrogen} -= $a[2]
52
    }
53
  }
54
  }
55
  if ($_ =~s /M  CHG//) {
56
    @a=split(" ", $_);
57
    my $cc=1;
58
    for my $ion (@a) {
59
      if ($cc%2==0) {
60
        $hash->{$ion}{hydrogen} += $a[$cc]
61
      }
62
      $cc++;
63
    }
64
  };
65
  $cnt ++;
66
}
67
close IN;
68
for my $at (sort keys %hash){
69
  $hash{$at}{hydrogen}=0 if (!(defined $hash{$at}{hydrogen}) || $hash{$at}{hydrogen}<0);
70
  $nH = $nH+ $hash{$at}{hydrogen};
71
}
72
$atom{'H'}=$nH;
73
my $formula=$atom{'C'} > 0 ? 'C' : '' ;
74
$formula .= $atom{'C'} if $atom{'C'} > 1;
75
$formula .= 'H' if $atom{'H'} > 0;
76
$formula .= $atom{'H'} if $atom{'H'} > 1;
77
 
78
for my $at (sort keys %atom){
79
  next if (!(defined ($atom{$at})) || $atom{$at}==0 );
80
  next if ($at=~'C\b' || $at=~'H\b');
81
   $formula .= $atom{$at}==1? $at : $at . $atom{$at} ;
82
}
83
print $formula . "\n";
84
 
85
EOF