Debugging Fortran Program: Getting Fluxconv Value

Click For Summary

Discussion Overview

The discussion revolves around debugging a Fortran program that calculates a value called "fluxconv" from data read from a file. Participants are addressing issues related to variable scope and data handling within loops, as well as seeking general feedback on programming practices in Fortran.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant expresses confusion about why the variable "fluxconv" only retains the last value assigned to it after a loop, suggesting that it is being treated as a simple variable without retaining previous values.
  • Another participant requests better formatting of the code for readability, indicating that proper presentation can aid in understanding the problem.
  • A separate post introduces a different Fortran program related to heat conduction, inviting comments and suggestions for improvements, indicating a broader interest in learning and refining programming skills.

Areas of Agreement / Disagreement

There is no consensus on the specific issue of variable retention in the Fortran program, as participants are still exploring the problem. Additionally, the introduction of a different program suggests varying levels of experience and focus among participants.

Contextual Notes

Participants have not fully resolved the issue of variable scope and retention, and there may be assumptions about the understanding of Fortran programming that are not explicitly stated. The discussion includes both debugging specific code and general programming practices.

Who May Find This Useful

This discussion may be useful for individuals learning Fortran programming, particularly those interested in debugging techniques and best practices in coding structure.

big man
Messages
242
Reaction score
0
Hey,

I wasn't sure whether or not to put this in the homework section or not because I need to complete this code for an assignment, but I thought that someone with fortran knowledge might have a better chance of seeing it in this section.

PROGRAM testing

! Variable declarations
CHARACTER input*100 ! buffer for holding a line of data from a file
INTEGER, PARAMETER :: DATAFILE = 30, STDIN = 5, STDOUT = 6
INTEGER numRecords, & ! number of records for each star
starNum, &
flag, &
numRepetitions
REAL (KIND=8) :: barycentricJD, bMag, deltaBMag, backgroundB, &
vMag, deltaVMag, backgroundV, fluxconv, mean, std, zeromean

REAL (KIND=8), PARAMETER :: F0=0.0000000216

! Program Start

! Read information from the keyboard
WRITE(STDOUT,*) "Enter number of stars to read in: "
READ(STDIN,*) numRepetitions

! Open the .dat file
OPEN( FILE = 'TYC_EP.dat', UNIT = DATAFILE, STATUS = 'OLD', IOSTAT = IOStat )

DO I = 1, numRepetitions ! read in numRepetitions stars
READ (DATAFILE, "(a)") input ! read header information for current star
READ(input(6:10), FMT="(I5)") starNum ! extract star number from header info
READ(input(14:16), FMT="(I3)") numRecords ! extract number of transits for current star

WRITE (STDOUT,*) "Star number is: ", starNum
WRITE (STDOUT, *) "Number of records is: ", numRecords

fluxconvsum=0

DO J = 1, numRecords
READ (DATAFILE,"(a)") input
READ(input(1:50), "(F10.1,1X,F6.3,1X,F5.3,1X,F6.2,1X,F6.3,1X, F5.3,1X, F6.2)") &
barycentricJD, bMag, deltaBMag, backgroundB, &
vMag, deltaVMag, backgroundV
READ(input(83:84), "(I2)") flag
fluxconv=F0*(10**(-bMag/2.5))
fluxconvsum=fluxconvsum+fluxconv
WRITE(STDOUT,*) barycentricJD," ", bMag, " ", deltaBMag," ",&
backgroundB," ", vMag," ", deltaVMag," ",&
" ", backgroundV, " ",flag, " ", fluxconv

END DO ! End of loop for reading in each record
mean=(fluxconvsum)/numRecords
WRITE(STDOUT, *) "MEAN", mean, fluxconv

END DO ! End of loop for reading in each starCLOSE(DATAFILE)

END PROGRAM testing

I'm no expert in fortran (well actually I've never done programming ever and I'm really bad at it) and this program has essentially been created by trial and error from my lecture notes and I've been lucky up until this moment. In the loop for the "reading in each record" I will get 111 individual entries for fluxconv (for the first star), but then I write fluxconv outside of the loop it only returns the very last value of fluxconv. All I want to know is why this is? If I know this then I should be able to sort it out and then proceed to zero mean the data.

I'd appreciate any help at all
Thanks
 
Last edited:
Technology news on Phys.org
I don't mean to be a bother, but could you put [ code ] and [ /code ] (without the spaces) around your code to make it more readable, so that whatever indenting you do isn't ignored by the forums software?
 
In the loop for the "reading in each record" I will get 111 individual entries for fluxconv (for the first star), but then I write fluxconv outside of the loop it only returns the very last value of fluxconv.

What are you expecting, or, what is it you're hoping for? -- right now, you are using "fluxconv" as a simple variable and thus, it will only retain the last value assigned to it (the last value at the end of the J cycle for each iteration of I).
 
How can I open, read, write multiple filenames in fortran90 using linux?
 
this is my first fortran program and i tried to learn fortran myself i would like anyone please to comment on this program and if there is an improvement can be done
i appreciate your help and thank you very much





PROGRAM FIRST
! FINITE VOLUME METHOD FOR ONE DIMENSIONAL UNSTEADY STATE HEAT CONDUCTION
! EXPLICIT SCHEME
! EXAMPLE 8.1
!
! THE ONE DIMENSIONAL TRANSIENT HEAT CONDUCTION EQUATION IS D*dT/dt=d/dx(k*dT/dx)
! THE INITIAL CONDITIONS ARE T=200 AT t=0
!
! THE BOUNDARY CONDITIONS ARE: dT/dx=0 at x=0, t>0 and T=0 at x=L, t>0
! THE NUMERICAL SOLUTION WITH THE EXPLICIT METHOD IS GENERATED BY DIVIDING THE
! DOMAIN INTO FIVE EQUAL CONTROL VOLUMES WITH (DELTA X = 0.004 m)
! !
!P*TP=W*TW+E*TE+[P-(W+E)]*TP+Su
!
REAL, DIMENSION (0:50,0:50):: T
INTEGER :: j
REAL :: E,P,W

PRINT*, ' '
!
! AS THE PLATE INITIALLY AT A UNIFORM TEMPERATURE OF 200
! !
DATA T(0,1),T(0,2),T(0,3),T(0,4),T(0,5) /5*200/
!
PRINT*,' '
CALL ONE(E,P,W)
!
PRINT*, T(0,1),T(0,2),T(0,3),T(0,4),T(0,5)
!
DO j=0,19
!
! NODE (1) EQUATION
T(j+1,1)=E/P*T(j,2)+(1-(E/P))*T(j,1)
!
! NODE(2-4)EQUATIONS
T(j+1,2)=W/P*T(j,1)+E/P*T(j,3)+(1-((W/P)+(E/P)))*T(j,2)
T(j+1,3)=W/P*T(j,2)+E/P*T(j,4)+(1-((W/P)+(E/P)))*T(j,3)
T(j+1,4)=W/P*T(j,3)+E/P*T(j,5)+(1-((W/P)+(E/P)))*T(j,4)
!
! NODE (5)EQUATION
T(j+1,5)=W/P*T(j,4)+((1-(3*W/P)))*T(j,5)
!
! THE RESULTS OF 40 SECONDS
PRINT*, T(j+1,1),T(j+1,2),T(j+1,3),T(j+1,4),T(j+1,5)
!
END DO
!
END PROGRAM FIRST

!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

SUBROUTINE ONE(E,P,W)
! K : IS THE THERMAL CONDACTIVITY (W/m/K)
! D : IS THE MATERIAL DINSITY TIMES THE SPECIFIC HEAT (J/m3/K)
! DELTAX: IS THE CONTROL VOLUME WIDTH (m)
! DELTAT: IS THE TIME STEP (SECONDS)
REAL :: K, D, DELTAX, DEALTAT
REAL :: E,P,W
!
K=10.00
D=10.00E6
DELTAX=0.004
DELTAT=(D*DELTAX**2)/(2*K)
!
!THE TIME STEP FOR THE EXPLICIT METHOD IS SUBJECT TO THE CONDITION : DELTAT < D*DELTAX**2/2**K
!
PRINT*, 'THE TIME STEP SHOULD BE LESS THAN ',DELTAT, 'SEC'
PRINT*, ' '
PRINT*, 'LET THE TIME STEP '
READ*, DELTAT
E=K/DELTAX
P=(D*DELTAX)/DELTAT
W=E
END SUBROUTINE ONE
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 32 ·
2
Replies
32
Views
4K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 13 ·
Replies
13
Views
4K