# Fortran [FORTRAN] Code misassigning values

1. Mar 11, 2015

### Matterwave

Hi guys, I can't figure this one out. Here I am working with a piece of code which should read in a grid of 2 arrays, a radius array and an electron fraction array. The code looks like this:

Code (Fortran):

subroutine varelec_init(varelecfrac,elecprofile,effilename,elecfracname)
integer :: varelecfrac,elecprofile
character*80 effilename
character*80 elecfracname
character*80 rest
character*1 first

elecfracname = ''
!  varelecfrac = 0 means constant elecfrac, 1 means analytic profile, 2 means read from file
!  elecprofile = 1 means linear electron fraction
if(varelecfrac.eq.0) then
elecfracname = 'Constant electron fraction'
elseif(varelecfrac.eq.1) then
elecfracname = 'Analytic profile '

elseif(varelecfrac.eq.2) then

if(myid.eq.0) then
write(6,*) ' Reading electron fraction from ',effilename
write(6,*) ' Beginning of file '
open(unit=20,file=trim(effilename))
if(elecfracname.eq.'') elecfracname=rest
if (first.eq.'#') then
write(6,'(a1,a80)') first,rest
else
backspace 20
endif
enddo

iend=0
iegrid=0
do while (iend.eq.0)
iegrid=iegrid+1
if(ierr.ne.0) then
iend=1
iegrid=iegrid-1
close(20)
endif
enddo
write(6,*) ' Electron fraction grid contains ',iegrid,'values'
write(6,*) ' Beginning of Y_e grid '
do i=1,min(5,iegrid)
write(6,'(1p,f15.7,e18.9)') ryegrid(i),yegrid(i)
enddo
write(6,*) ' End of Y_e grid '
do i=max(1,iegrid-4),iegrid
write(6,'(1p,f15.7,e18.9)') ryegrid(i),yegrid(i)
enddo

!  end of if for myid = 0
endif

!  end of if for varelecfrac=0,1,2
endif

return
end subroutine varelec_init
varelecfrac has been set to 2. It reads the array without giving me any runtime error or anything, but it adds a 0 to end of all the radius values (ryegrid). The file that I fed it looks like this:
Code (Fortran):

# Electron fraction testing
# (0) r/km  (1) Ye
#$13 1 500 .1 550 .105 600 .11 650 .115 700 .12 800 .13 900 .14 1000 .15 1500 .2 2000 .25 3000 .3 4000 .35 5000 .4 But the output looks like this: Code (Fortran): Reading electron fraction from test.dat Beginning of file # Electron fraction testing # (0) r/km (1) Ye #$ 13 1
Electron fraction grid contains  13 values
Beginning of Y_e grid
5000.0000000  1.000000000E-01
5500.0000000  1.050000000E-01
6000.0000000  1.100000000E-01
6500.0000000  1.150000000E-01
7000.0000000  1.200000000E-01
End of Y_e grid
15000.0000000  2.000000000E-01
20000.0000000  2.500000000E-01
30000.0000000  3.000000000E-01
40000.0000000  3.500000000E-01
50000.0000000  4.000000000E-01
As you can see, all the radius values are a factor of 10 larger for some reason. Anyone have any ideas why FORTRAN is doing this? I have tried writing "500.d0" in my test.dat file but it didn't change anything. It's not simply a write error either since I made it write out values for the electron fraction later (when the code is actually calculating something) and they were all wrong because the radii read from the file were all wrong. Thanks for any help!

Last edited by a moderator: Mar 11, 2015
2. Mar 11, 2015

### Staff: Mentor

I believe that what's happening is the 1P descriptor you have in this line:
Code (Text):
write(6,'(1p,f15.7,e18.9)') ryegrid(i),yegrid(i)
Reference http://www.fortran.com/F77_std/rjcnf0001-sh-13.html, 13.5.7 P Editing.
This is the only think I can think of that would be causing what you see. The P descriptor is a new one on me - I had to look at about a dozen sites to find it.

3. Mar 11, 2015

### Matterwave

I copied this code from a chunk of pre-existing code so I'm not too familiar with that either. However, I don't think it's just a problem with the "write" statement because it's literally using values of ryegrid that is a factor of 10 too high. Later on in the code, I use this grid and linear regression to figure out the electron fractions at distances ~1000km. Because the ryegrid values are so large, and the electron fraction is a growing function, the electron fractions that the code is giving me at my distances are tiny. So I think the values themselves in ryegrid are wrong.

4. Mar 11, 2015

### Staff: Mentor

It's easy enough to test. See if removing the '1p' makes a difference in your output.

5. Mar 11, 2015

### Matterwave

Just tried doing that, it didn't change the output as far as I can tell:
Code (Fortran):

Beginning of file
# Electron fraction testing
# (0) r/km  (1) Ye
#\$ 13 1
Electron fraction grid contains  13 values
Beginning of Y_e grid
5000.0000000  1.000000000E-01
5500.0000000  1.050000000E-01
6000.0000000  1.100000000E-01
6500.0000000  1.150000000E-01
7000.0000000  1.200000000E-01
End of Y_e grid
1500.0000000  0.200000000E+00
2000.0000000  0.250000000E+00
3000.0000000  0.300000000E+00
4000.0000000  0.350000000E+00
5000.0000000  0.400000000E+00

6. Mar 11, 2015

### Greg Bernhardt

@Matterwave us code=fortran when using code blocks :)

7. Mar 11, 2015

### Staff: Mentor

Actually, it did change your output. Notice that the 2nd group is now what it should be. You need to remove both occurrences of '1P'. You removed the 2nd occurrence, but not the first.

8. Mar 11, 2015

### Matterwave

Ah, ok I'll take note of that. Thanks. The output of the code is just text though.

9. Mar 11, 2015

### Matterwave

hmmm ok, let me try.

EDIT: you are right, now the values show up correctly there...

So I must have screwed up some other part of the code to make it find the wrong electron fraction values...:(

10. Mar 11, 2015

### Staff: Mentor

Maybe or maybe not, and you know your program better than I do. What I found out was that you were reading in 500 and printing 5000, for example, all as a result of that print specifier 1P. Removing it made it so that what got printed out was what was read in.

11. Mar 11, 2015

### Matterwave

That's such a weird print specifier...I wonder why the original coder used it in the first place. It just adds a 0 to the first number in the grid...I only just noticed that this code has been doing that for the electron density file its reading in as well...this makes no sense to me... O.O

EDIT: I figured out where I went wrong...I am so dumb, I wrote a linear function y=m+b instead of y=mx+b... -_______-

EDIT2: As I have no idea the utility of the "1p" specifier I have removed all instances of it in my code...

Last edited: Mar 11, 2015
12. Mar 11, 2015

### Staff: Mentor

It's kind of dangerous to copy and re-use code whose purpose you don't understand. Just sayin'.

13. Mar 11, 2015

### Matterwave

Well, I know the purpose of the code, I just don't know the finer details...D:

14. Mar 11, 2015

### Staff: Mentor

Let me rephrase what I said: It's kind of dangerous to copy and re-use code whose details you don't understand.

15. Mar 12, 2015

### Matterwave

Yeah...I suppose. But then I wouldn't get anything done because I'd be spending all my time learning Fortran. D:

16. Mar 12, 2015

### Staff: Mentor

So, if I'm understanding you correctly, you're saying it's a better use of your time to maintain code that you don't understand than it is to spend some time understanding what each piece of the code does? That seems like a false economy to me.

17. Mar 12, 2015

### Matterwave

Well, the code is written in like 5 different styles because I think 5 different coders has worked on the code in the past. Some of it is written as FORTRAN 90, some as FORTRAN 77, some as FORTRAN 95. I understand basically what each piece of the code does, but not every detail.