Re: Fortran code
- To: mathgroup at yoda.physics.unc.edu
- Subject: Re: Fortran code
- From: rubin at msu.edu (Paul A. Rubin)
- Date: Tue, 28 Jun 94 11:02:52 EDT
>Dear MMA users,
>
>I wish to pass some parameter values generated as a list by MMA to a Fortran
>code. The following is an example.
>
>In[12]:= FortranForm[{s1 -> 1., s2 -> 2.}]
>
>Out[12]//FortranForm= List(Rule(s1,1.),Rule(s2,2.))
>
>VAX Fortran does not understand "List" and "Rule". How does one circumvent
>this ?
>
>Abbey.
>
It depends on whether you want to put the parameters into a data file your
program will read, or embed them directly into the source code. To create a
data file, use PutAppend, and map it across the list of values resulting
from the substitutions. For instance:
In[]:= r = {s1 -> 1., s2 -> 2. 10^9}; (* creates the substitution rules *)
In[]:= PutAppend[ FortranForm[ # ], "g:\\out" ]& /@ ({s1, s2} /. r);
The result is a text file ("out") containing the values in FORTRAN format:
1.
2.e9
Note that: (a) you have to apply the substitution rules to a list {s1, s2} to
extract the values; and (b) you have to Map PutAppend across the list (if
you just Put the list, I'm pretty sure you get the braces and all). BTW, I
multiplied your s2 value by 10^9 to make sure the exponent was being
converted to a format suitable for FORTRAN.
The easiest way to embed the numbers in the source code is probably to use
Splice[]. Write the source code, and where you want the numbers to go, put
a Mma expression to generate them, bracketing the expression with "<*" and
"*>" (these can be changed using an option to Splice if they present a
problem. Example:
=== Mma code ===
In[]:= r = {s1 -> 1., s2 -> 2. 10^9}; (* creates the substitution rules *)
In[]:= Splice[ "g:\\test", "g:\\test.2", FormatType -> FortranForm ];
=== FORTRAN source file before splicing ("test") ===
PROGRAM TEST
REAL S1, S2
S1 = <* s1 /. r *>
S2 = <* s2 /. r *>
PRINT *, S1, S2
END
=== FORTRAN source file after splicing ("test.2") ===
PROGRAM TEST
REAL S1, S2
S1 = 1.
S2 = 2.e9
PRINT *, S1, S2
END
Paul
**************************************************************************
* Paul A. Rubin Phone: (517) 336-3509 *
* Department of Management Fax: (517) 336-1111 *
* Eli Broad Graduate School of Management Net: RUBIN at MSU.EDU *
* Michigan State University *
* East Lansing, MI 48824-1122 (USA) *
**************************************************************************
Mathematicians are like Frenchmen: whenever you say something to them,
they translate it into their own language, and at once it is something
entirely different. J. W. v. GOETHE