# Running a Fortran program and getting the results

I'm trying to figure how to make Mathematica interact with a Fortran program that takes 2 parameters in a first input line, 3 parameters in a second input line and produces two lines as outputs. Below a simple example to illustrate my point:

program par
implicit none
integer            :: i,j
integer            :: k,l,m

write (*,*) (i+j)*k, (i+j)-2*l
write (*,*) 2*l, 3*m, i-j

end program par


When I run the code below in Mathematica, I get "EndOfFile" as a result.

pr = StartProcess["par"];
g[i_, j_, k_, l_, m_] := (Write[pr, ToString[i], ToString[j]];
g[1, 2, 3, 4, 5]


Any help would be highly appreciated. Cheers.

• Can you clarify a bit? What should Mathematica take as an input, and return exactly? Nov 15, 2016 at 0:25
• related: mathematica.stackexchange.com/a/77063/2079.. I suspect your Write may be running the numbers together with no spaces but cant test here..try adding ," ", to the lists Nov 15, 2016 at 1:00
• In the Fortran code given above as an example, the "read" statements are the inputs (variables i and j in the first, and k, l and m in the second), and the "write" are the outputs.
– lgcm
Nov 15, 2016 at 1:42

This worked for me

g[i_, j_, k_, l_, m_] := (
WriteLine[pr, ToString[i] <> " " <> ToString[j]];
WriteLine[pr,
ToString[k] <> " " <> ToString[l] <>
" " <> ToString[m]];

pr = StartProcess["/home/louis/prgms/tmp/par"];
g[1, 2, 3, 4, 5] // ColumnForm


(* " 9 -5", " 8 15 -1" *)

For numeric output, one could use this

g[i_, j_, k_, l_, m_] := (
WriteLine[pr, ToString[i] <> " " <> ToString[j]];
WriteLine[pr,
ToString[k] <> " " <> ToString[l] <>
" " <> ToString[m]];

pr = StartProcess["/home/louis/prgms/tmp/par"];
output = g[1, 2, 3, 4, 5]


(* {{9, -5}, {8, 15, -1}} *)

The above works for integer input to and output from the external program, which is okay for specified Fortran program. If the external program, however, were reading and writing floating point numbers, scientific notation and/or text, we would want to do things a little differently.

• ReadList[pr,Number,2] may do Nov 15, 2016 at 2:37
• This also works for numeric output: {Read[pr, {Number, Number}], Read[pr, {Number, Number, Number}]} Nov 15, 2016 at 2:42