Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

[FORTRAN] Code misassigning values

  1. Mar 11, 2015 #1

    Matterwave

    User Avatar
    Science Advisor
    Gold Member

    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 '
      icomments=1
      open(unit=20,file=trim(effilename))
      do while (icomments.eq.1)
      read(20,'(a1,a80)')first,rest
      if(elecfracname.eq.'') elecfracname=rest
      if (first.eq.'#') then
      write(6,'(a1,a80)') first,rest
      else
      icomments=0
      backspace 20
      endif
      enddo

    !  read data file
      iend=0
      iegrid=0
      do while (iend.eq.0)
      iegrid=iegrid+1
      read(20,*,iostat=ierr) ryegrid(iegrid), yegrid(iegrid)
      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. jcsd
  3. Mar 11, 2015 #2

    Mark44

    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.
     
  4. Mar 11, 2015 #3

    Matterwave

    User Avatar
    Science Advisor
    Gold Member

    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.
     
  5. Mar 11, 2015 #4

    Mark44

    Staff: Mentor

    It's easy enough to test. See if removing the '1p' makes a difference in your output.
     
  6. Mar 11, 2015 #5

    Matterwave

    User Avatar
    Science Advisor
    Gold Member

    Just tried doing that, it didn't change the output as far as I can tell:
    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
      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
     
  7. Mar 11, 2015 #6
    @Matterwave us code=fortran when using code blocks :)
     
  8. Mar 11, 2015 #7

    Mark44

    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.
     
  9. Mar 11, 2015 #8

    Matterwave

    User Avatar
    Science Advisor
    Gold Member

    Ah, ok I'll take note of that. Thanks. The output of the code is just text though.
     
  10. Mar 11, 2015 #9

    Matterwave

    User Avatar
    Science Advisor
    Gold Member

    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...:(
     
  11. Mar 11, 2015 #10

    Mark44

    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.
     
  12. Mar 11, 2015 #11

    Matterwave

    User Avatar
    Science Advisor
    Gold Member

    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
  13. Mar 11, 2015 #12

    Mark44

    Staff: Mentor

    It's kind of dangerous to copy and re-use code whose purpose you don't understand. Just sayin'.
     
  14. Mar 11, 2015 #13

    Matterwave

    User Avatar
    Science Advisor
    Gold Member

    Well, I know the purpose of the code, I just don't know the finer details...D:
     
  15. Mar 11, 2015 #14

    Mark44

    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.
     
  16. Mar 12, 2015 #15

    Matterwave

    User Avatar
    Science Advisor
    Gold Member

    Yeah...I suppose. But then I wouldn't get anything done because I'd be spending all my time learning Fortran. D:
     
  17. Mar 12, 2015 #16

    Mark44

    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.
     
  18. Mar 12, 2015 #17

    Matterwave

    User Avatar
    Science Advisor
    Gold Member

    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: [FORTRAN] Code misassigning values
  1. Running a Fortran code (Replies: 4)

  2. Help with fortran code (Replies: 17)

  3. Help with fortran code (Replies: 4)

Loading...