#  Copyright (c) 1997-2021
#  Ewgenij Gawrilow, Michael Joswig, and the polymake team
#  Technische Universität Berlin, Germany
#  https://polymake.org
#
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by the
#  Free Software Foundation; either version 2, or (at your option) any
#  later version: http://www.gnu.org/licenses/gpl.txt.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#-------------------------------------------------------------------------------

CREDIT mptopcom
   mptopcom is a software developed at TU Berlin and Hokkaido University for
   computing triangulations of point configurations in parallel.
   Copyright by Charles Jordan, Michael Joswig, Lars Kastner.
   https://polymake.org/mptopcom

custom $mptopcom;

custom $mpirun;

CONFIGURE {
   $mptopcom =~ s{(?<=\S)$}{/mptopcom1};
   my $path=find_program($mptopcom, "mptopcom1", { 
      prompt => "the `mptopcom1' program from the mptopcom package", 
   }) or return;
   my $version = `echo bla | $mptopcom --version 2>&1`;
   if($version !~ m/mptopcom version/){
      die <<"---";
Your version of mptopcom is not supported anymore. Please upgrade.
---
   }
   find_program($mpirun, "mpirun", { prompt => "`mpirun' to call mptopcom's parallel version" });
   ($mptopcom) = $path =~ $directory_re;
}

# @category Triangulations, subdivisions and volume
# Use [[wiki:external_software#mptopcom|mptopcom]] for computing triangulations of point configurations in parallel.
label mptopcom

    
# @category Triangulations, subdivisions and volume
# Computes triangulations of a point configuration.
# @param Matrix points as rows.
# @param Array<Array<Int>> generators of group acting on the points.
# @option Bool regular compute only regular triangulations.
# @option Bool fine compute only fine triangulations.
# @return Array<Pair<Array<Set<Int>>, Vector<Int>>> Array of pairs of the form (Triangulation, GKZ vector)
user_function mptopcom1($ ;  Array<Array<Int>>, { regular=>0, fine=>0 }) {
   my ($A, $G, $options) = @_;
   my $cmd = "$mptopcom/mptopcom1";
   return call_mptopcom($cmd, $A, $G, $options);
}

# @category Triangulations, subdivisions and volume
# Computes triangulations of a point configuration.
# @param Matrix points as rows.
# @param Array<Array<Int>> generators of group acting on the points.
# @option Bool regular compute only regular triangulations.
# @option Bool fine compute only fine triangulations.
# @option Int np number of slots for parallel computation.
# @return Array<Pair<Array<Set<Int>>, Vector<Int>>> Array of pairs of the form (Triangulation, GKZ vector)
user_function mptopcom($ ;  Array<Array<Int>>, { regular=>0, fine=>0, np=>3 }) {
   my ($A, $G, $options) = @_;
   my $np = $options->{np};
   $mpirun or die "mpirun not found.\n";
   my $cmd = "$mpirun --oversubscribe -np $np $mptopcom/mptopcom";
   return call_mptopcom($cmd, $A, $G, $options);
}

sub build_mptopcom_command {
   my ($cmd, $options) = @_;
   my $result = $cmd;
   if ($options->{regular}){
      $result .= " --regular";
   }
   if ($options->{fine}){
      $result .= " -F";
   }
   unless ($DebugLevel) {
      $result .= " 2>/dev/null";
   }
   return $result;
}

sub decompose_line {
   my ($line) = @_;
   my ($triang, $gkz) = $line =~ m/(\{\{.*\}\})\]\s*gkz: (\[[^\]]*\])/;
   $triang =~ s/\{/\[/g;
   $triang =~ s/\}/\]/g;
   return new Pair<Array<Set<Int>>, Vector<Int>>(new Array<Set<Int>>(eval $triang), new Vector<Int>(eval $gkz));
}

sub call_mptopcom {
   my ($cmd, $A, $G, $options) = @_;
   my $input = new TempTextFile;
   print $input "[[", join("],[", map(join(",", @$_), @$A)) . "]]\n";
   if (defined($G)) {
      print $input "[[", join("],[", map(join(",", @$_), @$G)) . "]]\n";
   }
   my $mptopcom_command = build_mptopcom_command($cmd, $options) . " < $input";
   if ($Verbose::external) {
        dbg_print( "running mptopcom: $mptopcom_command" );
    }
   open my $output,  "$mptopcom_command |"
     or die "can't start $cmd: $!\n";
   my @result;
   while (my $line = <$output>) {
      push @result, decompose_line($line);
   }
   return new Array<Pair<Array<Set<Int>>, Vector<Int>>>(\@result);
}

# Local Variables:
# mode: perl
# cperl-indent-level:3
# indent-tabs-mode:nil
# End:
