[FORTRAN] Code misassigning values

  • Fortran
  • Thread starter Matterwave
  • Start date
  • Tags
    Code Fortran
In summary: E+00 50000.0000000 4.000000000E+00As 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?
  • #1
Matterwave
Science Advisor
Gold Member
3,971
328
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:

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:
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:
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:
Technology news on Phys.org
  • #2
I believe that what's happening is the 1P descriptor you have in this line:
Code:
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.
 
  • Like
Likes DrClaude
  • #3
Mark44 said:
I believe that what's happening is the 1P descriptor you have in this line:
Code:
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.

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
It's easy enough to test. See if removing the '1p' makes a difference in your output.
 
  • #5
Just tried doing that, it didn't change the output as far as I can tell:
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
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
Greg Bernhardt said:
@Matterwave us code=fortran when using code blocks :)

Ah, ok I'll take note of that. Thanks. The output of the code is just text though.
 
  • #9
Mark44 said:
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.

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
Greg Bernhardt said:
@Matterwave us code=fortran when using code blocks :)
Matterwave said:
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...:(
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
Mark44 said:
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.

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:
  • #12
Matterwave said:
I copied this code from a chunk of pre-existing code so I'm not too familiar with that either.
It's kind of dangerous to copy and re-use code whose purpose you don't understand. Just sayin'.
 
  • #13
Mark44 said:
It's kind of dangerous to copy and re-use code whose purpose you don't understand. Just sayin'.

Well, I know the purpose of the code, I just don't know the finer details...D:
 
  • #14
Let me rephrase what I said: It's kind of dangerous to copy and re-use code whose details you don't understand.
 
  • #15
Yeah...I suppose. But then I wouldn't get anything done because I'd be spending all my time learning Fortran. D:
 
  • #16
Matterwave said:
Yeah...I suppose. But then I wouldn't get anything done because I'd be spending all my time learning Fortran. D:
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
Mark44 said:
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.

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.
 

Related to [FORTRAN] Code misassigning values

1. What is FORTRAN code misassigning values?

FORTRAN code misassigning values refers to when a programmer unintentionally assigns incorrect or incorrect data values to variables within their FORTRAN code. This can happen due to human error, lack of understanding of the code, or other factors.

2. How does FORTRAN code misassigning values affect the code?

Misassigned values in FORTRAN code can lead to unexpected results, incorrect calculations, and potentially cause the code to crash or produce incorrect outputs. It can also make the code difficult to debug and troubleshoot.

3. How can I prevent FORTRAN code misassigning values?

To prevent FORTRAN code misassigning values, it is important to thoroughly test and debug your code before using it. This includes double-checking all variable assignments and using proper data types. It is also important to have a good understanding of the code and its purpose.

4. Can I use any tools or techniques to identify FORTRAN code misassigning values?

Yes, there are various tools and techniques that can help identify FORTRAN code misassigning values. These include debugging tools, code analyzers, and manual code review. It is also helpful to follow best practices and coding standards to minimize the chances of misassigning values.

5. How can I fix FORTRAN code misassigning values?

Fixing FORTRAN code misassigning values involves identifying and correcting the specific lines of code where the incorrect values are being assigned. This may require rewriting or restructuring parts of the code. It is also important to thoroughly test the code after making any changes to ensure the issue has been resolved.

Similar threads

  • Programming and Computer Science
2
Replies
54
Views
4K
  • Programming and Computer Science
Replies
4
Views
693
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
16
Views
2K
  • Programming and Computer Science
Replies
19
Views
5K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
10
Views
2K
  • Programming and Computer Science
Replies
4
Views
2K
Back
Top