#!/bin/perl -w

use strict ;

my $VERSION = '1.0' ;

=head1 NAME

moments - batch execution of transci on multiple symmetries and roots.

=head1 SYNOPSIS

moments number_of_roots list_of_symmetries

=head1 DESCRIPTION

For each nonredundant pair of symmetries execute the COLUMBUS transition
moment program, transci, for each nonredundant pair of roots.
In addition to the files F<moints> and F<cisrtif>, required by transci,
this script creates symbolic links from the files F<cidrtfl.sym>,
F<civfl.sym>, and F<civout.sym> for each sym in the list_of_symmetries.
Each individual transci output file, F<trncils>, is renamed to
F<trncils.first_sym.root_m.second_sym.root_n>.
The transci execution command is `F<transci.x>`.

=head1 EXAMPLE

moments 3 au b1u b2u

Assuming the files F<moints>, F<cisrtif>, F<cidrtfl.au>, F<cidrtfl.b1u>, F<cidrtfl.b2u>,
F<civfl.au>, F<civfl.b1u>, F<civfl.b2u>, F<civout.au>, F<civout.b1u>, and F<civout.b2u>
exist, and that F<transci.x> executes the proper program then
the following transition moments will be calculated:
  au  root 1  with  au  root 1, 
  au  root 1  with  au  root 2,
  au  root 1  with  au  root 3,
  au  root 2  with  au  root 2,
  au  root 2  with  au  root 3,
  au  root 3  with  au  root 3,
  au  root 1  with  b1u root 1,
  au  root 1  with  b1u root 2,
  au  root 1  with  b1u root 3,
  au  root 2  with  b1u root 1,
  au  root 2  with  b1u root 2,
  au  root 2  with  b1u root 3,
  au  root 3  with  b1u root 1,
  au  root 3  with  b1u root 2,
  au  root 3  with  b1u root 3,
  au  root 1  with  b2u root 1,
  au  root 1  with  b2u root 2,
  au  root 1  with  b2u root 3,
  au  root 2  with  b2u root 1,
  au  root 2  with  b2u root 2,
  au  root 2  with  b2u root 3,
  au  root 3  with  b2u root 1,
  au  root 3  with  b2u root 2,
  au  root 3  with  b2u root 3,
  b1u root 1  with  b1u root 1,
  b1u root 1  with  b1u root 2,
  b1u root 1  with  b1u root 3,
  b1u root 2  with  b1u root 2,
  b1u root 2  with  b1u root 3,
  b1u root 3  with  b1u root 3,
  b1u root 1  with  b2u root 1,
  b1u root 1  with  b2u root 2,
  b1u root 1  with  b2u root 3,
  b1u root 2  with  b2u root 1,
  b1u root 2  with  b2u root 2,
  b1u root 2  with  b2u root 3,
  b1u root 3  with  b2u root 1,
  b1u root 3  with  b2u root 2,
  b1u root 3  with  b2u root 3,
  b2u root 1  with  b2u root 1,
  b2u root 1  with  b2u root 2,
  b2u root 1  with  b2u root 3,
  b2u root 2  with  b2u root 2,
  b2u root 2  with  b2u root 3,
  b2u root 3  with  b2u root 3,

The output files created will be 
F<trncils.au.root1.au.root1>,
F<trncils.au.root1.au.root2>,
etc., through 
F<trncils.b2u.root3.b2u.root3>.

=head1 FILES

=over 4

=item 

F<cidrtfl1>
temporary symbolic link to F<cidrtfl.sym>.

=item 

F<cidrtfl2>
temporary symbolic link to F<cidrtfl.sym>.

=item 

F<cidrtfl.sym>
the DRT files for each sym in the list of symmetries.

=item 

F<cisrtif>
the sort-specific information file.

=item 

F<civfl1>
temporary symbolic link to F<civfl.sym>.

=item 

F<civfl2>
temporary symbolic link to F<civfl.sym>.

=item 

F<civfl.sym>
the CI vector files for each sym in the list of symmetries.

=item 

F<civout1>
temporary symbolic link to F<civout.sym>.

=item 

F<civout2>
temporary symbolic link to F<civout.sym>.

=item 

F<civout.sym>
the CI vector header information files for each sym in the list of symmetries.

=item 

F<moints>
the MO integral file.

=item 

F<transci.x>
the COLUMBUS transition moment program.

=item 

F<trnciin>
the transition moment program input temporary file.

=item 

F<trncils>
the transition moment program output temporary file.

=back

=head1 PREREQUISITES

This script requires the C<strict> module.

=head1 COREQUISITES

None.

=head1 OSNAMES

Any.

=head1 SCRIPT CATEGORIES

  CPAN/Unknown
  Science/Quantum Chemistry

=head1 SEE ALSO

COLUMBUS
(http://www.itc.univie.ac.at/~hans/Columbus/columbus.html)

=head1 AUTHOR

Scott Brozell <brozell.1@osu.edu>

=head1 HISTORY

Based on runtranmom.perl and runmmom.perl from Spiridoula Matsika.

=head1 COPYRIGHT

Copyright (c) 2000 Scott Brozell. All rights reserved. This program is
free software; you can redistribute it and/or modify it under the same
terms as Perl itself.

=head1 DISCLAIMER

This software is distributed in the hope that it will be useful, but
is provided "AS IS" WITHOUT WARRANTY OF ANY KIND, either expressed or
implied, INCLUDING, without limitation, the implied warranties of
MERCHANTABILITY and FITNESS FOR A PARTICULAR PURPOSE.

The ENTIRE RISK as to the quality and performance of the software
IS WITH YOU (the holder of the software).  Should the software prove
defective, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
CORRECTION.

IN NO EVENT WILL ANY COPYRIGHT HOLDER OR ANY OTHER PARTY WHO MAY CREATE,
MODIFY, OR DISTRIBUTE THE SOFTWARE BE LIABLE OR RESPONSIBLE TO YOU OR TO
ANY OTHER ENTITY FOR ANY KIND OF DAMAGES (no matter how awful - not even
if they arise from known or unknown flaws in the software).

=cut


my $number_roots = shift
  || die "usage: $0 number_of_roots list_of_symmetries\n" ;
my @symmetries = @ARGV ;
my $number_symmetries = @symmetries ;

print "Program Batch Calculation of Transition Moments:  Version $VERSION\n\n" ;
print "Number of roots = $number_roots\n" ;
print "$number_symmetries symmetries read: @symmetries\n" ;
print "\n" ;
# For non UNIX systems. Obvious workarounds exist.
my $can_symlink = ( eval { symlink( "", "" ); }, $@ eq "" ) ;
$can_symlink || die "Can Not Create Symbolic Links!\n" ;

my @basenames     = qw( cidrtfl civfl civout ) ;
my $first_suffix  = '1' ;
my $second_suffix = '2' ;
my $trnciin       = 'trnciin' ;
my $trncils       = 'trncils' ;

for ( my $i = 0; $i < $number_symmetries; ++ $i ) {
    foreach ( @basenames ) {
        my $tmp_file = "$_$first_suffix" ;
        unlink $tmp_file ;
        symlink "$_.$symmetries[ $i ]", $tmp_file
          or die "Can Not Create Symbolic Link from $_.$symmetries[ $i ]!\n" ;
    }
    for ( my $j = $i; $j < $number_symmetries; ++ $j ) {
        foreach ( @basenames ) {
            my $tmp_file = "$_$second_suffix" ;
            unlink $tmp_file ;
            symlink "$_.$symmetries[ $j ]", $tmp_file
              or die "Can Not Create Symbolic Link from $_.$symmetries[ $j]!\n";
        }
        for ( my $first_root = 1; $first_root <= $number_roots; ++ $first_root){
            my $second_root = 1 ;
            if ( $symmetries[ $i ] eq $symmetries[ $j ] ) {
                $second_root = $first_root ;
            }
            for ( ; $second_root <= $number_roots; ++ $second_root ) {
                open( TRCIIN, ">$trnciin" ) || die "Can Not Open $trnciin!\n" ;
                print TRCIIN " &input \n",
                             "  lvlprt=3, \n",
                             "  nroot1=$first_root, \n",
                             "  nroot2=$second_root \n",
                             " &end \n" ;
                close TRCIIN ;
                print "Calculating transition moment for ",
                      $symmetries[ $i ],
                      " root ",
                      $first_root,
                      " -- ",
                      $symmetries[ $j ],
                      " root ",
                      $second_root,
                      ".\n" ;
                `transci.x`;
                rename $trncils, join "", $trncils,
                                          ".",
                                          $symmetries[ $i ],
                                          ".root",
                                          $first_root,
                                          ".",
                                          $symmetries[ $j ],
                                          ".root",
                                          $second_root
                  or warn "Can Not Rename $trncils!\n" ;
            }
        }
    }
}

