Programs for Programmers

Running a Fortran Application on the Web

Porting applications to run on the web poses the same challenges whether they are written in Fortran, C, Delphi or any other similar non-web-aware language.  The actual running is not the problem, but communicating with the user is!

A typical web-based scenario is a browser on the user’s machine (client) interacts with a web-based program on a server.  This program, typically written in HTML, PHP or another scripting language can spawn another program on the server and this could be a Fortran application.  [The PHP ‘exec’ command is used to execute an external program but be aware that due to security risks, this may be locked down by your ISP]

Now what about getting input into the program and output out?  This example uses good old file-handling to pass large amounts of data and passing parameters on the command-line for individual values.

<html><body><form method="POST" action="callplusfort.php">
<table border="0" width="30%">
<tr>
<td width="400"><textarea rows="14" name="F77" cols="70"></textarea></td>
<td>Run Type:  <br><select size="1" name="runtype">
<option >Beautify only</option>
<option >Beautify and Relabel</option>
<option >Full Restructure</option>
</select></td>
</tr>
<tr>
<td width="400"><p align="center"><input type="submit" name="submit" value="Submit">
<input type="submit" name="reset" value="Reset"></td>
<td>&nbsp;</td>
</tr>
<tr>
<td width="400"><textarea rows="14" name="F90" cols="70"></textarea></td>
<td>&nbsp;</td>
</tr>
</table>
</form>
</body></html>

The above code shows some straightforward HTML code containing a form with two text areas and one pull-down.  We want to pass the contents of the top text area and the pull-down to our Fortran application and display the results from the Fortran in the bottom text area.
When the user presses the submit button, the contents of the top text area is contained in the variable ‘F77’ and the pulldown contents is contained in the variable ‘runtype’.  These are then passed to the server-based PHP program.

<?php
$submit = $_POST['submit'];
if ($submit == "Submit")
{
// store the contents of the user form field containing the f77 code
$f77code = $_POST['F77'];
// save the user-selected pull-down options
$run = $_POST['runtype'];  // 1 = 1 (beautify) = 2 (and relabel) = 3 (restructure)
// set numeric equivalents to pull-down option ready for passing to Fortran program.
switch($run) {
case "Beautify only":
   $runnum = 1;
   break;
case "Beautify and Relabel":
   $runnum = 2;
   break;
case "Full Restructure":
   $runnum = 3;
   break;
}
// copy the f77 code into a local file
$numbytes = file_put_contents("test.for",$f77code);
// run Fortran program ?spag’ with user-options on command-line and default local source file //TEST.FOR
exec("spag 1=$runnum");
// return back to PLUSFORTONLINE.PHP with pull-down options to retain state
header ("Location: http://www.polyhedron.com/plusfortonline.php?id=140&id1=$runnum");
}
?>

The above code receives the text-area contents into local variable ‘f77code’ and saves it as a local file called ‘test.for’, and it receives the pulldown option into local variable ‘run’ and converts it to its numerical equivalent.
Running the Fortran program is done with the PHP ‘exec’ command which passes the numeric variable ‘runnum’ as a command-line argument.  (The program can take many command-line arguments and the 1= signifies which argument is being passed).  The program that is run automatically reads its input from the file ‘test.for’ and produces its output to a file called ‘test.spg’.

The PHP code then returns to the original PHP/HTML file which can then pick up the newly generated file with the command:

$f90block = file_get_contents('http://00.000.00.000//plusfortonline/test.spg');

and display it accordingly.

This example uses PHP and file-handling to exchange the data between the Fortran application and the web.  Fortran READ’s and WRITE’s to the standard input/output channel haven’t been explored here, but look at the article below regarding a native Fortran-77 CGI interface.

 References:

A Native Fortran-77 CGI Interface

Online syntax highlighting (useful web-site for colour-highlighting your code, as used above!)