#!/usr/local/bin/perl
# perl prog to find temp / m curves for mf ising
# 14/02/98
#
# for simple example
# mft.p | gnuplot
# for book
# mft.p H=3 hmax=0.8 file=mft3 | gnuplot
# for input to next computation
# mft.p H=1 mmin=0.0 mmax=1.001 continuum=20 file=mft1 M=501 | gnuplot
#
$mmin = -1.001 ; $mmax = - $mmin ; $M = 1001 ;
$mclip = 0.999999999 ;
$hmin = 0.0 ; $hmax = 0.1 ; $H = 2 ;
$ymax = 1.1 ; 
$gnu = 1 ; # whether to gnuplot
$ps = 1 ; # whether to write ps when gnuplotting.
$sizex=0.6; $sizey=0.6;
$CJ = 4.0 ; # number of neighbours times J
$continuum = 1 ; # if this is 1 then the line h=0 is created by just two endpoints. If it is bigger then extra points are plastered in between.
$betacrit = 0.25 ; 
$betamaxmax = 1000.0 ; # approximation to infinity.
$betaminmin = 0.0001 ; # approximation to zero.
$file = "mft" ;
# process command line
eval "\$$1=\$2" while @ARGV && $ARGV[0]=~ /^(\w+)=(.*)/ && shift;
$firstVFE = 1 ; 
#
$dm = ($mmax-$mmin)*1.0/($M-1) ;
if ( $H > 1 ) {
    $dh = ($hmax-$hmin)*1.0/($H-1) ;
} else {
    $dh = 2.0 *( $hmax-$hmin) ; 
}
$betamax = 4.0  * $betacrit ; # define view ranges
$betamin = 0.5  * $betacrit ;
$files = 1 ; 
if ( $justonefile ) {
    open( FI , ">$file" ) ;
    $filename[$files] = $file ;
    $keystring[$files] = "-" ; 
}
# outer loop through m values
$firstneg = 0 ;
for ( $h= $hmin ; $h <= $hmax ; $h += $dh , $files++ ) {
    if ( !$justonefile ) {	# 
	$thisfile = sprintf ( "%s%.2f" , $file , $h ) ;
	$thisfile =~ s/\s*//g ;	# 
	print STDERR "writing to $thisfile\n" ; 
	open( FI , ">$thisfile" ) ; # 
	$filename[$files] = $thisfile ;
	$keystring[$files] = sprintf ( "h = %.2f" , $h ) ;
    }				# 
    printf FI ("# written my mft.p\n\n# h = $h\n\n") ;
    printf FI "# %-8s %-8s %-8s %-8s %-8s %-10s %-8s %-8s %-10s %-8s %-10s \n", # 
    "beta", "T", "h", "m" , "log Z" , "VFE" , "E" , "S" , "C" , "varE" , "varEbb" ; 
    for ( $um = $mmin ; $um <= $mmax ; $um += $dm ) {
	$m = $um ; 
	if ( $m < -$mclip ) { $m = - $mclip ;  }
	if ( $m > $mclip ) { $m = $mclip ;  }
# find beta value  to match this.
	# the VFE is
	# 
# eqn to satisfy is:
# 1/2 log[(1+m)/(1-m)] = beta(CJ m+h)
	$TINY = 1e-10 ;
	$denom = $CJ * $m + $h ;
	if(($denom<$TINY)&&($denom>-$TINY)) { # special case
	    if ($h==0.0) {	# show whole line of beta values for case h=0.
		# $continuum defines the number of points to do on this line
		# if continuum > 1 then we skip the unstable branch
		$dbeta = - ( $betaminmin - $betacrit ) / $continuum ;
		$con = 0 ;
		if ($continuum<=1) {
		    printf FI ("# critical point\n") ;
		    $beta=$betacrit; &doit() ;
		}
		printf FI ("# stable branch\n") ;
		for ( $beta=$betaminmin ; $con <= $continuum ;
		     $beta += $dbeta , $con ++ ) {
		    &doit() ;
		}
		if ($continuum<=1) {
		    printf FI ("# unstable branch\n") ;
		    $beta=$betamaxmax ; &doit() ;
		}
		printf FI ("# critical point\n") ;
		$beta=$betacrit; &doit() ;
		printf FI ("# continue to m not 0\n") ;
	    } else {
		$beta=$betamaxmax ;  &doit() ;
	    }
	} else { # general case
	    $beta = 0.5 * log((1.0+$m)/(1.0-$m)) / ($denom) ;  &doit() ;
	}
    }
    printf FI ("\n") ;
}				# 
# write gnuplot stuff.
if ( $gnu ) {
	$Tmax = 1.0/$betamin ;
	print "# gnuplot\n" ;
	print "set yrange [-$ymax:$ymax]\n" ;
	if ( $justonefile ) { print "set nokey \n" ; }
	else {  print "set key \n" ; }
	print "set size $sizex,$sizey\n" ;
	print "set xrange [0.0:$betamax] \n" ;
	if ($ps) {
		print "set term post \n" ;
		print "set output '$file.b.ps' \n" ;
	}
	$Files = ($justonefile) ? 1 : $files - 1 ;
	$command = "plot " ;
	for ( $files = 1 ; $files <= $Files ; $files ++ ) {
	    $tfile = $filename[$files] ;
	    $key = $keystring[$files] ;
	    if ( $files > 1 ) { $command .= ", " ; }
	    $command .= "'$tfile' u 1:4 t '$key' w l" ; # 
	}
	print "$command \n" ;
	print "# gnuplot\n" ;
	print "set yrange [-$ymax:$ymax]\n" ;
	print "set size $sizex,$sizey\n" ;
	print "set xrange [0.0:$Tmax] \n" ;
	if ($ps) {
		print "set term post \n" ;
		print "set output '$file.T.ps' \n" ;
	} else {
		print "pause -1 'ready'\n" ;
	}
	$command = "plot " ;
	for ( $files = 1 ; $files <= $Files ; $files ++ ) {
	    $tfile = $filename[$files] ;
	    $key = $keystring[$files] ;
	    if ( $files > 1 ) { $command .= ", " ; }
	    $command .= "'$tfile' u 2:4 t '$key' w l" ; # 
	}
	print "$command \n" ;
	print " \n" ;
}
# end ############################################################
# 
# print to file
#
sub doit {
    if ( $beta <= 0.0 ) { 
	if ( $firstneg ) { 
	    printf FI "\n" ; 
	    $firstneg = 0 ; 
	}
	printf FI "# " ;
    } else { 
	printf FI "  " ; $firstneg = 1 ;  
    } 
    $T = 1.0 / $beta ;
    &VFE();
    printf FI "%-8.5g %-8.5g %-8.5g %-8.5g %-8.5g %-10.5g %-8.5g %-8.5g %-10.5g %-8.5g %-10.5g \n",
    $beta, $T, $h, $m , $logz , $vfe ,  $E , $S , $realCap , $varE , $Cap ;
    # 
}

sub VFE {
    
    # finds variational free energy
    if (!$firstVFE) {
    }
    $ans = $beta * ( - 0.5 * $CJ * $m * $m - $h * $m )
	- &H2( 0.5*($m + 1.0 ) ) ;
    $E = ( - 0.5 * $CJ * $m * $m - $h * $m ) ;
    $S =  &H2( 0.5*($m + 1.0 ) ) ;
    $q = 0.5*($m + 1.0 ) ;
    # var(E) per spin = C / 2  * J^2 * (1-m^2 ) # NEGLECTING h term!!!!
    $varE = $CJ * ( 1.0 - $m * $m ) * 0.5 ; 
    $Cap = $varE * $beta * $beta ; 
    # not clear whether the varE and the C will agree with each other.
    # in fact, no, this "Cap" is not the derivative of E at all?
    # this is beta F: 
    $logz = - $ans ;
    $vfe  = $ans / $beta ; 
    $realCap = 0.0 ; 
    if (!$firstVFE) {
	$dE = $E - $oldE ;
	$deltabeta = $oldbeta - $beta ;
	$dT = -($oldT - $T) ;
	if (( $dT > $TINY ) || ($dT < - TINY ) ) {
	    $realCap = $dE / $dT ;
	} else {
	    $realCap = 0.0 ; 
	}			# 
	#
	$oldT = $T ;
	$oldbeta = $beta ;
        $oldE = $E ;
    }
    $firstVFE = 0 ; 
}

sub H2 {
    # binary entropy in natural units
    # usage ( $f ) 
    local ( $f ) = @_ ; 
    local ( $ans ) = - (  $f * &safelog ( $f ) + (1.0 - $f) * &safelog (1.0 - $f ) )  ;
    return $ans ; 
}

sub safelog {
    local ($f) = @_ ; 
    if ( $f < 0 ) { print STDERR "Can't take log of $f\n" ; }
    return ( $f > 0 ? log ( $f) : 0.0 ) ;
}
