How to modify the following code?

  • Thread starter Thread starter winiepu
  • Start date Start date
  • Tags Tags
    Code
Click For Summary

Discussion Overview

The discussion revolves around issues related to running a Fortran code snippet that includes a module and subroutines. Participants explore potential modifications to the code to resolve runtime errors and improve functionality.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Exploratory

Main Points Raised

  • One participant reports being able to compile the code but unable to run it, prompting a request for modifications.
  • Another participant asks for clarification on the specific error encountered when running the code.
  • A participant mentions receiving a "handle exception: Error while dumping state" message and notes that making the subroutine internal allows the code to compile and run successfully, raising questions about the behavior of external subroutines.
  • One suggestion involves adding WRITE statements to display variable values for debugging purposes, with an emphasis on predicting values before running the code.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the cause of the runtime error or the best approach to modify the code. Multiple perspectives on debugging and code structure are presented.

Contextual Notes

There are unresolved issues regarding the handling of external versus internal subroutines and the specific nature of the runtime error. The discussion reflects varying levels of understanding and approaches to debugging in Fortran.

winiepu
Messages
3
Reaction score
0
I can compile the following code, but I can not run it in my computer. How do change it? Thanks.


module nrtype
INTEGER, PARAMETER :: SP = KIND(1.0)
INTEGER, PARAMETER :: DP = KIND(1.0D0)
INTEGER, PARAMETER :: I4B = SELECTED_INT_KIND(9)
INTEGER, PARAMETER :: I2B = SELECTED_INT_KIND(4)
INTEGER, PARAMETER :: I1B = SELECTED_INT_KIND(2)
END module

program mainp
use nrtype
implicit none
call xlocate
end program

subroutine xlocate
USE nrtype
IMPLICIT NONE
!INTEGER(I4B), PARAMETER :: n=3
INTEGER(I4B) :: i,j
REAL(DP) :: x
REAL(DP), DIMENSION(4) :: xx
REAL(DP), DIMENSION(2):: xlim,ab
!real(DP), external:: mylocate1
xx=(/1.0_DP,2.0_DP,3.0_DP,4.0_DP/)
x=1.5_DP
CALL mylocate(xx,x,xlim,ab)
write(*,*) ab

END subroutine xlocate

SUBROUTINE mylocate(xx,x,xlim,ab)
USE nrtype
IMPLICIT NONE
REAL(DP), DIMENSION(2), INTENT(OUT):: xlim, ab
REAL(DP), DIMENSION(:), INTENT(IN) :: xx
REAL(DP), INTENT(IN) :: x
INTEGER(I4B) :: k
INTEGER(I4B) :: n,jl,jm,ju
LOGICAL(I4B) :: ascnd
REAL(DP)::h
n=SIZE(xx)
ascnd = (xx(n) >= xx(1))
jl=0
ju=n+1
DO
IF (ju-jl <= 1) EXIT
jm=(ju+jl)/2
IF (ascnd .EQV. (x >= xx(jm))) THEN
jl=jm
ELSE
ju=jm
END IF
END DO
IF (x == xx(1)) THEN
k=1
ELSE IF (x == xx(n)) THEN
k=n-1
ELSE
k=jl
END IF
h=xx(k+1)-xx(k);

ab(1)=(xx(k+1)-x)/h;
ab(2)=(x-xx(k))/h;

xlim(1)=k;
xlim(2)=k+1;
END SUBROUTINE mylocate
 
Technology news on Phys.org
Can you be more specific when you say you can't run it on your computer?
 
The compiler said handle exception: Error while dumping state. If I make mylocate internal subroutine by using contain, I can compile and run it just like the following. It is so weird. Can someone explain this to me? Thanks.

module nrtype
INTEGER, PARAMETER :: SP = KIND(1.0)
INTEGER, PARAMETER :: DP = KIND(1.0D0)
INTEGER, PARAMETER :: I4B = SELECTED_INT_KIND(9)
INTEGER, PARAMETER :: I2B = SELECTED_INT_KIND(4)
INTEGER, PARAMETER :: I1B = SELECTED_INT_KIND(2)
END module

program mainp
use nrtype
implicit none
call xlocate
end program mainp

subroutine xlocate
USE nrtype
! USE nr
IMPLICIT NONE
!INTEGER(I4B), PARAMETER :: n=3
INTEGER(I4B) :: i,j
REAL(DP) :: x
REAL(DP), DIMENSION(4) :: xx
REAL(DP), DIMENSION(2):: xlim,ab
!real(DP), external:: mylocate1
xx=(/1.0_DP,2.0_DP,3.0_DP,4.0_DP/)
x=1.5_DP
CALL mylocate(xx,x,xlim,ab)
write(*,*) ab
!j=mylocate1(xx,x)
!write(*,*) j
Contains
SUBROUTINE mylocate(xx,x,xlim,ab)
USE nrtype
IMPLICIT NONE
REAL(DP), DIMENSION(2), INTENT(OUT):: xlim, ab
REAL(DP), DIMENSION(:), INTENT(IN) :: xx
REAL(DP), INTENT(IN) :: x
INTEGER(I4B) :: k
INTEGER(I4B) :: n,jl,jm,ju
LOGICAL(I4B) :: ascnd
REAL(DP)::h
n=SIZE(xx)
ascnd = (xx(n) >= xx(1))
jl=0
ju=n+1
DO
IF (ju-jl <= 1) EXIT
jm=(ju+jl)/2
IF (ascnd .EQV. (x >= xx(jm))) THEN
jl=jm
ELSE
ju=jm
END IF
END DO
IF (x == xx(1)) THEN
k=1
ELSE IF (x == xx(n)) THEN
k=n-1
ELSE
k=jl
END IF
h=xx(k+1)-xx(k);

ab(1)=(xx(k+1)-x)/h;
ab(2)=(x-xx(k))/h;

xlim(1)=k;
xlim(2)=k+1;
END SUBROUTINE mylocate

END subroutine xlocate
 
What I would do is put a bunch of WRITE *,* statements in xlocate and mylocate to display the value of variables. (I'm assuming you want to get the code in post 1 to work.) Before you run this code, go through and predict the values of all variables. After that run the code with the WRITE statements, and see if the values displayed match what your hand calculations predicted.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 20 ·
Replies
20
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 13 ·
Replies
13
Views
22K
Replies
1
Views
2K
Replies
1
Views
3K