#!/usr/bin/perl -wT ################################################################################ # # # WebGasEOS.CGI # # Matthew T. Reagan # # LBL # # # ################################################################################ # # Version 1.07 # # January 23, 2006 use strict; use CGI; use Fcntl; use POSIX qw(tmpnam); #use GD::Graph; # Set this to root server my $SERVER = "http://esd.lbl.gov/people/mtreagan"; # Secure the run environment $ENV{"PATH"} = ""; delete @ENV{ 'IFS','CDPATH','ENV','BASH_ENV'}; #Use CGI.pm my $q = new CGI; my $i; # Check cookies my $cookie = $q->cookie("GasEOS"); my $remote_id; my $remote_agent; my $use_time; if (!$cookie) { #Create cookie $cookie = $q->cookie( -name => "GasEOS", -value => "Visitor", -expires => "+1y" ); # Log the new guy $remote_id = $ENV{REMOTE_HOST} || $ENV{REMOTE_ADDR}; $remote_agent = $ENV{HTTP_USER_AGENT}; $use_time = localtime; } #Extract input parameters from the form my $eos = $q->param("EOS"); my $T = $q->param("T"); my $P = $q->param("P"); my $Punits = $q->param("Punits"); my $Tunits = $q->param("Tunits"); my $Pseries = $q->param("Pseries"); my $Tseries = $q->param("Tseries"); my $Pend = $q->param("Pend"); my $Pstep = $q->param("Pstep"); my $Tend = $q->param("Tend"); my $Tstep = $q->param("Tstep"); my @species = $q->param("Species"); #Parameters fpr salts my $Molal = $q->param("Molal"); my $IonStr = $q->param("IonStr"); my $NIons = $q->param("NIons"); my $Salt = $q->param("SaltType"); #Output units my $Dunits = $q->param("Dunits"); my $Eunits = $q->param("Eunits"); my $Vunits = $q->param("Vunits"); #Extra features my $Diff = $q->param("Diff"); my $ThermC = $q->param("ThermC"); my $Visc = $q->param("Visc"); my $Fug = $q->param("Fug"); my $DepF = $q->param("DepF"); my $Format = $q->param("OutForm"); my $nspec = 0; #Concentration hash variable my %x; #Assemble concentration hash, create param references x???? on the fly foreach (@species) { $nspec++; $x{$_} = $q->param("x" . $_); } #Initialize flags my $PsatFlag = 0; my $iH2O = 0; my $Xmax = 1.0; #Check units if ($Punits eq 'MPa') { $P=$P*1_000_000; if ($Pstep) { $Pstep=$Pstep*1_000_000;} if ($Pend) {$Pend=$Pend*1000000; } } if ($Punits eq 'bar') { $P=$P*100_000; if ($Pstep) { $Pstep=$Pstep*100_000.0; } if ($Pend) { $Pend=$Pend*100_000.0; } } if ($Punits eq 'atm') { $P=$P*101_325; if ($Pstep) { $Pstep=$Pstep*101_325; } if ($Pend) { $Pend=$Pend*101_325; } } if ($Tunits eq 'C') { $T=$T+273.15; if ($Tend) { $Tend=$Tend+273.15; } } #Check bounds if ($P < 0.0) {$P=0.0} if ($Tunits eq 'K' && $T < 0.0) { $T = 0.0 } ############################################################# # # Create GasEOS input file in /tmp # ############################################################# my $tmpfile; do {$tmpfile = tmpnam() } until sysopen (INFILE, $tmpfile, O_RDWR|O_CREAT|O_EXCL ); print INFILE "$eos\n"; print INFILE "$nspec G\n"; foreach (@species) { print INFILE "$_ $x{$_}\n"; if ($_ eq "H2O") { print INFILE "$Molal $IonStr $NIons\n"; $iH2O = 1; } } print INFILE "$P $T\n"; #Series my $points; if ($Pseries eq 'Series') { my $Px = $P; $points = int( ($Pend-$P)/$Pstep ); print INFILE "$points\n"; for(1..$points) { $Px = $Px + $Pstep; print INFILE "$Px $T\n"; } } elsif ($Tseries eq 'Series') { my $Tx = $T; $points = int( ($Tend-$T)/$Tstep ); print INFILE "$points\n"; for(1..$points) { $Tx = $Tx + $Tstep; print INFILE "$P $Tx\n"; } } else { $points=0; print INFILE "0\n"; } close INFILE; #Name the output file my $outfile = $tmpfile . ".out"; ############################################################# # # Output a web page # ############################################################# #Turn on the border to see the layout my $TB = 0; # Border width my $TS = 0; # Cellspacing my $TP = 2; # Cellpadding # Create HTML document, output header print "Content-type: text/html\n"; print "Set-Cookie: $cookie\n"; print < GasEOS END_OF_HEADER print "\n
"; print "\n"; print <



WebGasEOS Home

WebGasEOS Documentation


Hydrogeology
Home


ESD Home



Feedback

Disclaimer
END_OF_SIDEBAR #Beginning of output DIV print "\n

"; #Read and print input file for debugging purposes #print "

"; #open TEST, $tmpfile; #print ""; #while () { # print "$_
"; # } #close TEST; #print "


"; ############################################################# # # Execute WebGasEOS # ############################################################# #(This should probably move to a secure executables directory) system "/usr/local/bin/webgaseos $tmpfile $outfile"; #Debug dump of output file #open OUT, $outfile or die "webgaseos output file not openable: $!"; #print ""; #while () { # print "$_
"; # } #print "
"; #close OUT; #printf("\n
"); open OUT, $outfile or die "webgaseos output file not openable: $!"; #Possible graphing code #if ($points gt 0.0) { # # print "\n
"; # print "\n Plot a property: "; # print "\n "; # print "\n"; # print "\n
"; # # print "\n
"; #} #Read sequential output fields, parse and output $eos = ; printf("\nEOS: %s

",$eos); $nspec = ; printf("\nComposition:
"); printf("\n"); foreach (@species) { printf("\n",$_,$x{$_}); if ($_ eq "H2O") { if ($Molal > 0.0) { printf("",$Salt,$Molal); } } printf(""); } print "\n
x%s = %gm%s = %g
"; print "\n
\n"; if ($Format eq 'cols') { print "\n
";
    print "\n# GasEOS output";
    print "\n#";
    foreach (@species) {
        print " x($_)=$x{$_}";
    }
#    print "
"; print "\n# T0=$T K P0=$P Pa"; if ($Tseries eq 'Series') { printf ("\n# T(%1s) Zgas ",$Tunits); } elsif ($Pseries eq 'Series') { printf ("\n# P(%-3s) Zgas ",$Punits); } else { printf("\n# Zgas"); } printf (" Zliq Rg(%-6s) Rl(%-6s) ",$Dunits,$Dunits); for (0..$nspec-1) { printf ("Phi_%-6s ",$species[$_]); } printf ("Visc(%-4s) K(W/m-K) ",$Vunits); if ($nspec == 2) { print "Dij(m^2/s) Dlp(m^2/s)"; } print "\n"; } #Clean up units if ($Tunits eq 'C') { $Tunits = 'oC'} if ($Dunits eq 'g/cm^3') { $Dunits = "g/cm3" } else { $Dunits = "kg/m3" } my $Xmaxmin = 1.0; my $Txmax; my @Xliq; #Cycle over all T or P points for (1..$points+1) { if ($Format eq 'text') { $P = ; $T = ; if ($Tunits eq 'oC') { $T = $T - 273.15 }; if ($Punits eq 'MPa') { $P = $P / 1_000_000 }; if ($Punits eq 'bar') { $P = $P / 100_000 }; if ($Punits eq 'atm') { $P = $P / 101_325 }; if ($points+1 > 1) { print "\nState #$_:
" } printf("\n"); printf("\n",$T,$Tunits); printf("\n",$P,$Punits); printf("\n
T = %g %s    P = %g %s
\n"); my $Zliq = ; my $Zgas = ; my $Rhol = ; my $Rhog = ; # Liquid water phase? if ($iH2O) { for (0..$nspec-1) { $Xliq[$_] = ; } $Xmax = ; if ($Xmax < 1.0 && $x{"H2O"} > $Xmax ) { my $Psat=$Xmax*$P; printf("\nWarning: At %g %s, Psat(H2O) = %.3g %s",$T,$Tunits,$Psat,$Punits); printf("\n
xmax(H2O) ~ %.3g in the gaseous phase

",$Xmax); } } if ($Dunits eq 'g/cm3') { $Rhol = $Rhol / 1000.0 ; $Rhog = $Rhog / 1000.0 ; } if ($Rhol != $Rhog) { printf("\n"); printf("\n",$Zgas,$Rhog,$Dunits); printf("\n",$Zliq,$Rhol,$Dunits); printf("\n
Zgas= %g    Gas density= %g %s
Zliq= %g    Liquid density= %g %s
\n"); } else { printf("\n"); printf("\n",$Zliq,$Rhol,$Dunits); printf("\n
Z= %g    Density= %g %s
\n"); } my @Phi; for (0..$nspec-1) { $Phi[$_] = ; } if ($Fug) { printf("\n
\n"); for (0..$nspec-1) { printf("\n",$species[$_],$Phi[$_]); } print "\n
Φ%s= %g
\n"; } my $GVisc = ; my $ThermK = ; if ($Vunits eq 'cP') { $GVisc = $ GVisc * 1000.0; } my $BinDiff; my $HiPDiff; #Binary diffusivity for binaries only if ($nspec == 2) { $BinDiff = ; $HiPDiff = ; } else { $Diff = ''; } if ($Diff || $ThermC || $Visc) { printf("\n
\n"); printf("\n"); if ($Diff) { printf("\n",$BinDiff); } if ($Visc) { printf("",$GVisc,$Vunits); } printf("\n"); printf("\n"); if ($Diff) { printf("\n",$HiPDiff); } if ($ThermC) { printf("",$ThermK); } printf("\n"); printf("\n
Low-pressure diffusivity= %g m2/s    Gas viscosity= %g %s
High-pressure diffusivity= %g m2/s    Thermal conductivity= %g W/(m K)
\n"); } #More liquid water options if ($iH2O) { if ($Rhol != $Rhog && $Xmax == 1.0) { print "\n
Note: Psat(H2O) > P"; print "\n
Liquid water not indicated
"; } #We only trust solubility for CO2-Water cases if ($Rhol != $Rhog && $Xmax < 1.0 && $species[0] eq "CO2" && $nspec==2) { printf("\n
\n"); printf("\n"); for (0..$nspec-1) { printf("\n",$species[$_],$Xliq[$_]); } printf("\n
Mole fractions in the aqueous liquid phase:
y%s= %g
\n"); } } #Departures for series input if ($_ > 1) { my $DH = ; my $DU = ; my $HDep = ; my $UDep = ; my $DHt = ; my $DUt = ; if ($Eunits eq 'J/mol') { $DH = $DH / 1000.0; $DU = $DU / 1000.0; $HDep = $HDep / 1000.0; $UDep = $UDep / 1000.0; $DHt = $DHt / 1000.0; $DUt = $DUt / 1000.0; } if ($DepF) { printf("\n
\n"); printf("\n"); printf("\n",$DH,$Eunits,$DU,$Eunits); printf("\n",$HDep,$Eunits,$UDep,$Eunits); printf("\n",$DHt,$Eunits,$DUt,$Eunits); printf("\n
Between State #1 and State #$_:
Ideal gas ΔH= %g %s    Ideal gas ΔU= %g %s
Net H departure= %g %s    Net U departure= %g %s
Total ΔH= %g %s    Total ΔU= %g %s
\n"); } } printf("\n
\n"); } #End of if 'text' #For columnar output: else { $P = ; $T = ; if ($Tunits eq 'oC') { $T = $T - 273.15 }; if ($Punits eq 'MPa') { $P = $P / 1_000_000 }; if ($Punits eq 'bar') { $P = $P / 100_000 }; if ($Punits eq 'atm') { $P = $P / 101_325 }; if ($Pseries eq 'Series') { printf("%10.4e ",$P) } if ($Tseries eq 'Series') { printf("%10.4e ",$T) } my $Zliq = ; my $Zgas = ; my $Rhol = ; my $Rhog = ; if ($iH2O) { for (0..$nspec-1) { $Xliq[$_] = ; } $Xmax = ; if ($Xmax < 1.0 && $x{"H2O"} > $Xmax) { $PsatFlag = 1; if ($Xmax < $Xmaxmin) { $Xmaxmin = $Xmax; $Txmax = $T } } } if ($Dunits eq 'g/cm3') { $Rhol = $Rhol / 1000.0 ; $Rhog = $Rhog / 1000.0 ; } printf("%10.4e %10.4e %10.4e %10.4e ",$Zgas,$Zliq,$Rhog,$Rhol); my $Phi; for (0..$nspec-1) { $Phi = ; printf("%10.4e ",$Phi); } my $GVisc = ; my $ThermK = ; if ($Vunits eq 'cP') { $GVisc = $ GVisc / 10.0; } printf("%10.4e ",$GVisc); printf("%10.4e ",$ThermK); my $BinDiff; my $HiPDiff; #Binary diffusivity for binaries only if ($nspec == 2) { $BinDiff = ; $HiPDiff = ; printf("%10.4e %10.4e ",$BinDiff,$HiPDiff); } if ($_ > 1) { my $DH = ; my $DU = ; my $HDep = ; my $UDep = ; my $DHt = ; my $DUt = ; } print "\n"; } #End of else 'cols' } #End of T/P loop #Warning for cols users if ($Format eq 'cols') { print "\n

"; if ($PsatFlag == 1) { my $Psat=$Xmax*$P; printf("\n
Warning: at %g %s, Psat(H2O) = %g %s",$Txmax,$Tunits,$Psat,$Punits); printf("\n
Maximum xH2O = %.3g in the gaseous phase.

",$Xmaxmin); } } close OUT; printf("\n"); printf("\n"); printf("\n"); #End of output DIV print "\n
"; print < END_OF_FOOTER #Handler insures tempfiles are deleted at program exit END { unlink $tmpfile or die "Couldn't unlink $tmpfile: $!" }; END { unlink $outfile or die "Couldn't unlink $outfile: $!" }; #Close HTML document print $q->end_html; #Log new users if ($remote_id) { open OUT, ">>/tmp/users.log" or die "Can't open users.log!"; print OUT "$use_time $remote_id $remote_agent\n"; close OUT; } #Done