CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Main CFD Forum

Fortran issue (write statement modifying variables??)

Register Blogs Members List Search Today's Posts Mark Forums Read

Like Tree1Likes
  • 1 Post By cfdnewbie

Reply
 
LinkBack Thread Tools Display Modes
Old   January 18, 2014, 22:11
Default Fortran issue (write statement modifying variables??)
  #1
Senior Member
 
Joachim
Join Date: Mar 2012
Location: Atlanta
Posts: 140
Rep Power: 7
Joachim is on a distinguished road
Hi everyone!

I have been having an issue with my fortran code, which really does not make sense to me. If somebody could give me his opinion, I would greatly appreciate!

At one point, my code generates a mistake (a variable = NaN). I used write statements to spot the source of the error.

if I use

print *, Mbar -> answer: 2.802

if I use

print *, Mbar, Cavg -> answer: NaN, 0,354

I did not change anything in the code, but the value of the variable Mbar changed to NaN!!!
Why would such a thing happen??

Thank you very much for your help!

Joachim
Joachim is offline   Reply With Quote

Old   January 19, 2014, 04:56
Default
  #2
Senior Member
 
Filippo Maria Denaro
Join Date: Jul 2010
Posts: 2,516
Rep Power: 31
FMDenaro will become famous soon enoughFMDenaro will become famous soon enough
Quote:
Originally Posted by Joachim View Post
Hi everyone!

I have been having an issue with my fortran code, which really does not make sense to me. If somebody could give me his opinion, I would greatly appreciate!

At one point, my code generates a mistake (a variable = NaN). I used write statements to spot the source of the error.

if I use

print *, Mbar -> answer: 2.802

if I use

print *, Mbar, Cavg -> answer: NaN, 0,354

I did not change anything in the code, but the value of the variable Mbar changed to NaN!!!
Why would such a thing happen??

Thank you very much for your help!

Joachim

what option you added in compiling your code? Mbar should be an integer? Often this kind of error is just an indication that your source code has some error like mismatch of type or some error in the bound of array.
Use the debug settings..
FMDenaro is online now   Reply With Quote

Old   January 19, 2014, 06:29
Default
  #3
Senior Member
 
cfdnewbie
Join Date: Mar 2010
Posts: 557
Rep Power: 12
cfdnewbie is on a distinguished road
I would agree, Debug flags are your best option. I would assume some memory allocation / out of bounds array things, because when an error seems to "jump" when you add a line, that usually means that a different part of the memory becomes corrupted.

Just out of curiosity: what type of code is it? f77 still around?
FMDenaro likes this.
cfdnewbie is offline   Reply With Quote

Old   January 19, 2014, 11:44
Default
  #4
Senior Member
 
Joachim
Join Date: Mar 2012
Location: Atlanta
Posts: 140
Rep Power: 7
Joachim is on a distinguished road
Hey! thanks to both of you.
I am not very familiar with makefiles however. What kind of line should I had to spot the mistake?

So far, that is what I have in my Makefile

-------------------------------------------------------------------------------------
FC = gfortran

# List of executables to be built within the package
PROGRAMS = main_euler_v1

-fcheck=all -fbacktrace -ffpe-trap=invalid,zero,overflow

# "make" builds all
all: $(PROGRAMS)

# Dependency rules
main_euler_v1.o: data_package_v1.o
main_euler_v1: data_package_v1.o
main_euler_v1.o: grid_jacobian_v1.o
main_euler_v1: grid_jacobian_v1.o
main_euler_v1.o: euler_solver_v1.o
main_euler_v1: euler_solver_v1.o
main_euler_v1.o: fluxes_v1.o
main_euler_v1: fluxes_v1.o

grid_jacobian_v1.o: data_package_v1.o
euler_solver_v1.o: data_package_v1.o
fluxes_v1.o: data_package_v1.o

# General rules for building the program
%: %.o
$(FC) $(FCFLAGS) -o $@ $^ $(LDFLAGS)

%.o: %.f90
$(FC) $(FCFLAGS) -c $<

%.o: %.F90
$(FC) $(FCFLAGS) -c $<

# Utility targets
.PHONY: clean veryclean

clean:
rm -f *.o *.mod *.MOD

veryclean: clean
-----------------------------------------------------------------------------------

Thank you once again!

Joachim

ps: I am using f90
Joachim is offline   Reply With Quote

Old   January 19, 2014, 12:16
Default
  #5
New Member
 
Patrick
Join Date: Apr 2012
Posts: 28
Rep Power: 6
Patrick1 is on a distinguished road
Quote:
Originally Posted by Joachim View Post
Hi everyone!

I have been having an issue with my fortran code, which really does not make sense to me. If somebody could give me his opinion, I would greatly appreciate!

At one point, my code generates a mistake (a variable = NaN). I used write statements to spot the source of the error.

if I use

print *, Mbar -> answer: 2.802

if I use

print *, Mbar, Cavg -> answer: NaN, 0,354

I did not change anything in the code, but the value of the variable Mbar changed to NaN!!!
Why would such a thing happen??

Thank you very much for your help!

Joachim
Try using a format specifier:

http://www.fortran.com/F77_std/rjcnf0001-sh-13.html

here is an old piece of code of mine. First I define the format with ID 91:

91 format (3X,I4,4X,3(6x,f6.1),2(6x,F10.6))

Then I write onto the screen (6) using the 91 format:

write (6,91) turbno,x_mat(dum_turbno),y_mat(dum_turbno),height, (U(dum_turbno,j)),(TI(j))

turbno etc are just the names of my variables.

The format specifiers can be found on the web but the basic ones are:

Integers of field width w: Iw
Real numbers of field width w and n decimal places: Fw.n
n blank spaces: nX

So in my format 91 I write 3 blank spaces, and integer of field width 4, 3 times 6 blank spaces then a real number of field width 6 and one decimal place..etc
Patrick1 is offline   Reply With Quote

Old   January 19, 2014, 12:29
Default
  #6
Senior Member
 
Filippo Maria Denaro
Join Date: Jul 2010
Posts: 2,516
Rep Power: 31
FMDenaro will become famous soon enoughFMDenaro will become famous soon enough
Quote:
Originally Posted by Joachim View Post
Hey! thanks to both of you.
I am not very familiar with makefiles however. What kind of line should I had to spot the mistake?

So far, that is what I have in my Makefile

-------------------------------------------------------------------------------------
FC = gfortran

# List of executables to be built within the package
PROGRAMS = main_euler_v1

-fcheck=all -fbacktrace -ffpe-trap=invalid,zero,overflow

# "make" builds all
all: $(PROGRAMS)

# Dependency rules
main_euler_v1.o: data_package_v1.o
main_euler_v1: data_package_v1.o
main_euler_v1.o: grid_jacobian_v1.o
main_euler_v1: grid_jacobian_v1.o
main_euler_v1.o: euler_solver_v1.o
main_euler_v1: euler_solver_v1.o
main_euler_v1.o: fluxes_v1.o
main_euler_v1: fluxes_v1.o

grid_jacobian_v1.o: data_package_v1.o
euler_solver_v1.o: data_package_v1.o
fluxes_v1.o: data_package_v1.o

# General rules for building the program
%: %.o
$(FC) $(FCFLAGS) -o $@ $^ $(LDFLAGS)

%.o: %.f90
$(FC) $(FCFLAGS) -c $<

%.o: %.F90
$(FC) $(FCFLAGS) -c $<

# Utility targets
.PHONY: clean veryclean

clean:
rm -f *.o *.mod *.MOD

veryclean: clean
-----------------------------------------------------------------------------------

Thank you once again!

Joachim

ps: I am using f90

but what about FCFLAGS?
FMDenaro is online now   Reply With Quote

Old   January 19, 2014, 13:58
Default
  #7
Senior Member
 
Joachim
Join Date: Mar 2012
Location: Atlanta
Posts: 140
Rep Power: 7
Joachim is on a distinguished road
thanks everyone! Actually, I cheated for that makefile. I just took a template and adapted it for my purpose...

I think I found something interesting, but I still don't fully understand it.

In my code, I use a special type called flow, in which I store most of the variables (--> flow(i,j)%u, etc).

1.At one point in the code, I checked and flow(i,j)%u is properly defined.

2. It is then called by a subroutine to compute the fluxes. The flow pointer is a global variable (use simulation_data at the beginning of the subroutine), but for some reason, if I do print *, flow(i,j)%u, I obtain NaN.

3. Pretty weird, if I add a print statement at the very beginning of the subroutine, then everything works just fine!! (if I don't, the flux calculation fails because u = NaN).

I think there is a problem when communicating the flow pointer, but I really don't understand why!! Would anyone have an idea??
Thanks very much for your help!
Joachim is offline   Reply With Quote

Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Setting environment variables: bash issue travonz OpenFOAM Installation 2 July 31, 2012 03:17
Issue with write interval haze_1986 OpenFOAM 0 July 30, 2012 02:24
Comparison between C/C++ and Fortran? rick Main CFD Forum 45 September 6, 2011 00:52
CFX11 + Fortran compiler ? Mohan CFX 20 March 30, 2011 18:56
about USER FORTRAN compiling issue CXL CFX 1 October 9, 2003 21:00


All times are GMT -4. The time now is 11:39.