473,378 Members | 1,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

fprintf() C variables into FORTRAN input file format ???

first, a little background...

i have a C program which preprocesses some data, and then outputs the
results into a text file. that text file, in turn, is used as input
to a FORTRAN computational fluid dynamics (CFD) program. the FORTRAN
program goes to work on the provided data, and outputs a text file
which i read back in with another C program to postprocess the data
and analyze the results of the CFD run.

now on to the problem. the input file to the FORTRAN CFD program is
in the following format (which may look familiar to folks well versed
in FORTRAN, which i'm not):
(ronin)$ cat boundedmatrix.in
1.0D0 ! Ratio
1.5D0 ! AXMAX
1 ! NPNAX
1D-1 ! B
0.5D0 ! GAM
18 ! NKMAX
1.7D0 ! EPS
-1 ! NP
0.6283D0 ! LAM (wavelength)
1.50D0 ! MRR (real part of the refractive index)
0.02D0 ! MRI (imag part of the refractive index)
0.00001D0 ! DDELT
181 ! NPNA (number of angles)
2 ! NDGS

the format of the FORTRAN input parameters, as you can see, carries
the precision of the value with it. e.g., 1.0D0 is a double precision
value X 10^0. and, 1D-1 is a double precision value X 10^-1.

now then, i'm a complete novice when it comes to FORTRAN, and denoting
the value precision in the input file in this way seems, to this C
programmer, to be a little odd. but nevertheless that's the way it
is.

does anyone have any ideas on how i might get my fprintf()'s to output
these kinds of values without a lot of gymnastics? i've taken a
couple of simplistic runs at it but i haven't gotten anywhere except
really-frustrated-ville. it appears that it should be easy to do but
i haven't found a clever way yet. of course i know ahead of time
which of my C variables are double precision and so forth. for that
matter i know a priori which variables the FORTRAN program are
expected to be in double precision.

note that it is no way possible for me to modify the FORTRAN program,
it's a NSF-sourced CFD code and if i touch it i can look forward to
about 10 years of verification tests. each CFD run is taking about 6
hours on a 512 processor SGI Origin box as it is. so the solution can
not include "tweaking" the FORTRAN code in any way. i have to treat
the CFD program as an immutable black box.

ideas, thoughts, catalysts, FORTRAN-conversion.c source, hate mail,
etc are all welcome.

thanks
jim
Nov 14 '05 #1
5 1471

"google" <go****@losdos.dyndns.org> wrote in message

1.0D0 is a double precision value X 10^0. and, 1D-1 is a double
precision value X 10^-1.

does anyone have any ideas on how i might get my fprintf()'s to
output these kinds of values without a lot of gymnastics?

All you need is a function

void dbltofortan(FILE *fpout, double x)
{
double lg = log10(x);
fprintf(fp, "%gD%d", x / pow(10, floor(lg)), floor(lg));
}

Hope this is right (untested).
Nov 14 '05 #2

On Sat, 10 Apr 2004, google wrote:

now on to the problem. the input file to the FORTRAN CFD program is
in the following format (which may look familiar to folks well versed
in FORTRAN, which i'm not):
(ronin)$ cat boundedmatrix.in
1.0D0 ! Ratio
1.5D0 ! AXMAX
1 ! NPNAX
1D-1 ! B
0.5D0 ! GAM <snip>
the format of the FORTRAN input parameters, as you can see, carries
the precision of the value with it. e.g., 1.0D0 is a double precision
value X 10^0. and, 1D-1 is a double precision value X 10^-1.


The 'D', I suppose, represents significant figures somehow? That is,
is the value 0.1D0 different from the value 1D-1? If so, then I think
you'll have to cope with the fact that C's "float" and "double" values
don't have any concept of "significant figures," so you're going to
lose some information somewhere.
If 0.1D0 and 1D-1 are in fact the same value, then you should just
be able to write something like this, to append "D0" to the end of
each floating-point value you print:

#include <stdio.h>

int main(void)
{
double fieldval = 4.52;
const char *fieldname = "FOO";

char buffer[20];
sprintf(buffer, "%gD0", fieldval);
printf("%-20s! %s\n", buffer, fieldname);
return 0;
}

Does this solve your problem?

HTH,
-Arthur

Nov 14 '05 #3
On Sat, 10 Apr 2004 19:40:40 -0400 (EDT), "Arthur J. O'Dwyer"
<aj*@nospam.andrew.cmu.edu> wrote:

On Sat, 10 Apr 2004, google wrote:

now on to the problem. the input file to the FORTRAN CFD program is
in the following format (which may look familiar to folks well versed
in FORTRAN, which i'm not):
(ronin)$ cat boundedmatrix.in
1.0D0 ! Ratio
1.5D0 ! AXMAX
1 ! NPNAX
1D-1 ! B
0.5D0 ! GAM

<snip>
the format of the FORTRAN input parameters, as you can see, carries
the precision of the value with it. e.g., 1.0D0 is a double precision
value X 10^0. and, 1D-1 is a double precision value X 10^-1.


The 'D', I suppose, represents significant figures somehow? That is,
is the value 0.1D0 different from the value 1D-1? If so, then I think
you'll have to cope with the fact that C's "float" and "double" values
don't have any concept of "significant figures," so you're going to
lose some information somewhere.
If 0.1D0 and 1D-1 are in fact the same value, then you should just
be able to write something like this, to append "D0" to the end of
each floating-point value you print:

#include <stdio.h>

int main(void)
{
double fieldval = 4.52;
const char *fieldname = "FOO";

char buffer[20];
sprintf(buffer, "%gD0", fieldval);
printf("%-20s! %s\n", buffer, fieldname);
return 0;
}

Does this solve your problem?

HTH,
-Arthur

If your values will have a large range of exponents (rather than the
small range of -1 to 0 in the sample), use the %E format specification
with sprintf and then change the 'E' to 'D' in the string before
writing it to the file. The width and precision modifiers can provide
some additional control.
<<Remove the del for email>>
Nov 14 '05 #4

"google" <go****@losdos.dyndns.org> wrote in message
news:95**************************@posting.google.c om...
first, a little background...

i have a C program which preprocesses some data, and then outputs the
results into a text file. that text file, in turn, is used as input
to a FORTRAN computational fluid dynamics (CFD) program. the FORTRAN
program goes to work on the provided data, and outputs a text file
which i read back in with another C program to postprocess the data
and analyze the results of the CFD run.

now on to the problem. the input file to the FORTRAN CFD program is
in the following format (which may look familiar to folks well versed
in FORTRAN, which i'm not):
(ronin)$ cat boundedmatrix.in
1.0D0 ! Ratio
1.5D0 ! AXMAX
1 ! NPNAX
1D-1 ! B
0.5D0 ! GAM
18 ! NKMAX
1.7D0 ! EPS
-1 ! NP
0.6283D0 ! LAM (wavelength)
1.50D0 ! MRR (real part of the refractive index)
0.02D0 ! MRI (imag part of the refractive index)
0.00001D0 ! DDELT
181 ! NPNA (number of angles)
2 ! NDGS

the format of the FORTRAN input parameters, as you can see, carries
the precision of the value with it. e.g., 1.0D0 is a double precision
value X 10^0. and, 1D-1 is a double precision value X 10^-1.

now then, i'm a complete novice when it comes to FORTRAN, and denoting
the value precision in the input file in this way seems, to this C
programmer, to be a little odd. but nevertheless that's the way it
is.

does anyone have any ideas on how i might get my fprintf()'s to output
these kinds of values without a lot of gymnastics? i've taken a
couple of simplistic runs at it but i haven't gotten anywhere except
really-frustrated-ville. it appears that it should be easy to do but
i haven't found a clever way yet. of course i know ahead of time
which of my C variables are double precision and so forth. for that
matter i know a priori which variables the FORTRAN program are
expected to be in double precision.

note that it is no way possible for me to modify the FORTRAN program,
it's a NSF-sourced CFD code and if i touch it i can look forward to
about 10 years of verification tests. each CFD run is taking about 6
hours on a 512 processor SGI Origin box as it is. so the solution can
not include "tweaking" the FORTRAN code in any way. i have to treat
the CFD program as an immutable black box.

You don't need to go to any trouble, any g or f format will work, as Fortran
reads accept [DEe] as equivalent. As in C, the format of the input doesn't
affect whether it is read as single (float) or double precision. The
comments are probably optional.
Nov 14 '05 #5
In <95**************************@posting.google.com > go****@losdos.dyndns.org (google) writes:
now on to the problem. the input file to the FORTRAN CFD program is
in the following format (which may look familiar to folks well versed
in FORTRAN, which i'm not):
(ronin)$ cat boundedmatrix.in
1.0D0 ! Ratio
1.5D0 ! AXMAX
1 ! NPNAX
1D-1 ! B

the format of the FORTRAN input parameters, as you can see, carries
the precision of the value with it. e.g., 1.0D0 is a double precision
value X 10^0. and, 1D-1 is a double precision value X 10^-1.
As Tim Prince mentioned, this is true in Fortran source code, where
1.0D0 is a double precision constant, while 1.0E0 is a single precision
constant. In READ statements, the precision is already known from the
way the variable was declared, so both D and E are accepted.
now then, i'm a complete novice when it comes to FORTRAN, and denoting
the value precision in the input file in this way seems, to this C
programmer, to be a little odd. but nevertheless that's the way it
is.

does anyone have any ideas on how i might get my fprintf()'s to output
these kinds of values without a lot of gymnastics?


You only need a minimal amount of gymnastics, if you *really* want to
use 'D' instead of 'E': use sprintf with the %E format, then use strchr
to find the first 'E' character and replace it by 'D'. Output the
string to the file with fputs.

char line[LINESIZE], *p;
double ratio;
FILE *fp;
....
sprintf(line, "%-20E! Ratio\n", ratio);
if ((p = strchr(line, 'E')) != NULL) *p = 'D';
fputs(line, fp);

I used %-20E instead of a plain %E so that your comments get nicely
aligned, as in your example above. You may also want to use a precision
specification, if the values are computed in your program with more
precision than the default six digits implied by the absence of a
precision specification.

In the pathological case when the value is a NaN, my if statement may
replace an 'E' inside a comment by a 'D' ;-) If NaN's in the input
file are a real option, you can avoid this by adding an extra check:

if (ratio == ratio && (p = strchr(line, 'E')) != NULL) *p = 'D';

NaNs don't compare equal, so a NaN-aware compiler cannot assume that
ratio == ratio always evaluates to true. And this test is more portable
than the isnan macro (which need not be supported by C89 implementations).

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mitch | last post by:
Hi all, I hope this is the right place for this post, apologies if it is not. I am trying to write a program that creates an input file for a dispersion model. The model is expecting a Fortran...
7
by: Anonymous | last post by:
I have a mixed-language pgm (Fortran and C++) which needs to pass a few hundred values from C++ to Fortran. One way to do this is to have a Fortran module and a C++ structure (or class) with an...
13
by: NM | last post by:
Sometimes ago I was having a problem in linking between C++ and Fortran program. That was solved (using input from this newsgroup) using the Fortran keyword "sequence" with the derived types (to...
2
by: hpy_awad | last post by:
formatting float variables to fprintf has error to my writing to output file called rental and I do not the reason that a rabish is written to the file instead of the actual input screen values ? ...
5
by: google | last post by:
first, a little background... i have a C program which preprocesses some data, and then outputs the results into a text file. that text file, in turn, is used as input to a FORTRAN...
6
by: hpy_awad | last post by:
I am writing stings ((*cust).name),((*cust).address)to a file using fgets but rabish is being wrote to that file ? Look to my source please and help me finding the reason why this rabish is being...
3
by: Andrew Fabbro | last post by:
I have code with stuff like this all over it: sprintf(errmsg,"somefunc(): %s has illegal character %c",somestring,somechar); fatal_error(errmsg); where fatal_error() just fprintf's to stderr...
52
by: Nomad.C | last post by:
Hi I've been thinking of learning Fortran as number crunching kinda language for my Physics degree......but then looking around the internet, people are saying that the libraries/ Algorithms once...
11
by: David Mathog | last post by:
In the beginning (Kernighan & Ritchie 1978) there was fprintf, and unix write, but no fwrite. That is, no portable C method for writing binary data, only system calls which were OS specific. At...
5
by: imailz | last post by:
Hi all, since I'm forced to switch from Fortran to C I wonder if there is posibility in C: 1) to use implicit loops 2) to parse several variables which number is determined at runtime. ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.