-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SHELLG loop index #72
Comments
I wrote to CCMC at [email protected] about this:
|
The CCMC people report:
|
Interesting, I wonder what the significance of this "magic number" 3333 is. That a large array to put on the stack. when I make the change in my version I get a warning:
|
…size it's unclear if this could ever happen in practice. see: nasa/radbelt#72 made it an allocatable in the class since it would be a large array on the stack and was getting warnings from gfortran with -Wsurprising
If the loop variable N exceeds 100, it will result in an out-of-bounds error for the array P, which is dimensioned as P(8,100). To improve the code and ensure it does not run into array bounds issues, you can add a check within the loop to exit if N exceeds the upper bound of the array: SUBROUTINE SHELLG(LAT, LON, HEIGHT, DIMO, XL, ICODE, BAB1)
REAL LAT, LON, HEIGHT, DIMO, XL, BAB1
INTEGER CODE
REAL P(8,100)
INTEGER N
REAL STEP12
! Initialize STEP12 (Assuming it's calculated or assigned earlier in the code)
STEP12 = ...
! Main loop with added bounds check
DO 3 N = 3, 3333
! Exit the loop if N exceeds the bounds of the P array
IF (N > 100) THEN
PRINT *, 'Warning: Loop variable N exceeds the bounds of array P. Exiting loop.'
EXIT
END IF
! Corrector (field line tracing)
P(1, N) = P(1, N-1) + STEP12 * (5. * P(4, N) + 8. * P(4, N-1) - P(4, N-2))
P(2, N) = P(2, N-1) + STEP12 * (5. * P(5, N) + 8. * P(5, N-1) - P(5, N-2))
! (Include other operations as necessary)
CONTINUE
END DO 3
END SUBROUTINE SHELLING This ensures that the array bounds are respected, preventing potential runtime errors or undefined behavior due to accessing out-of-bounds array elements. If the logic of the code ensures that the loop variable N never exceeds 100, this check will never trigger. However, it's good practice to include such safeguards, especially in scientific and engineering code, where array-based issues can lead to significant problems. I know you're semi-fluent in FORTRAN; what do you think, @Montana? |
Hi @xawpaw, Thanks for thinking of me, so what I've done here is added an Declared the Removed the unnecessary label 3 from the I lastly vectorized the corrector operations using array slicing to improve performance and readability. Added a check after the loop to determine if the loop completed successfully. If This is my updated version of this SHELLG Loop Index fix: SUBROUTINE SHELLG(LAT, LON, HEIGHT, DIMO, XL, ICODE, BAB1)
IMPLICIT NONE
REAL, INTENT(IN) :: LAT, LON, HEIGHT, DIMO, XL
INTEGER, INTENT(IN) :: ICODE
REAL, INTENT(OUT) :: BAB1
REAL, PARAMETER :: MAX_N = 3333
REAL, DIMENSION(8, MAX_N) :: P
INTEGER :: N
REAL :: STEP12
! Initialize STEP12 (Assuming it's calculated or assigned earlier in the code)
STEP12 = ...
! Main loop with added bounds check and vectorized operations
DO N = 3, MAX_N
! Corrector (field line tracing) using vector operations
P(1, N) = P(1, N-1) + STEP12 * (5. * P(4, N) + 8. * P(4, N-1) - P(4, N-2))
P(2, N) = P(2, N-1) + STEP12 * (5. * P(5, N) + 8. * P(5, N-1) - P(5, N-2))
! (Include other operations as necessary)
END DO
! Check if the loop completed successfully
IF (N > MAX_N) THEN
PRINT *, 'Warning: Loop variable N exceeds the maximum allowed value. Exiting subroutine.'
RETURN
END IF
! (Perform any necessary post-loop calculations or assignments)
END SUBROUTINE SHELLG Thanks again for thinking of me @xawpaw! |
Were either of the last two posts generated by an AI chat bot? |
In
SHELLG
we have a do loop that goes from 3 to 3333:Note that
P(1,N)
, but P is dimensioned asP(8,100)
. So, if N ever gets to be > 100, it will go off the end of this array. Maybe the code logic some prevents this from happening (i.e., it always exits the loop before this happens), but it seems suspicious.The text was updated successfully, but these errors were encountered: