How to modify the following code?

  • Thread starter winiepu
  • Start date
  • Tags
    Code
In summary: If they do, then the code is working correctly. If the values displayed do not match, then there is a bug in the code.In summary, the conversation discusses a code that can be compiled but cannot be run on a computer. The person asks for help in changing the code and mentions an error message they received. They also mention that the code works when the subroutine is made internal. The expert suggests using WRITE statements to debug the code and check for any bugs.
  • #1
winiepu
3
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
  • #2
Can you be more specific when you say you can't run it on your computer?
 
  • #3
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
 
  • #4
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.
 

FAQ: How to modify the following code?

1. How do I modify the code to add a new variable?

To add a new variable, you will need to declare it by giving it a name and assigning a value to it. This can be done by using the "let" or "var" keyword, followed by the name of the variable, an equals sign, and the desired value. For example, let newVariable = 5;

2. Can I change the data type of a variable in the code?

Yes, you can change the data type of a variable by reassigning it with a new value that is of a different data type. JavaScript is a dynamically typed language, which means that variables can hold different types of data at different times.

3. How can I modify the code to loop through an array?

To loop through an array, you can use a for loop or a forEach loop. For a for loop, you will need to specify the starting index, the end condition, and the increment value. For a forEach loop, you can simply use the forEach() method and provide a function that will be executed on each element of the array.

4. Is it possible to modify the code to make it more efficient?

Yes, there are several ways to make code more efficient. One approach is to optimize algorithms and data structures. Another approach is to use built-in methods or functions that are specifically designed for the task at hand. Additionally, you can also consider refactoring the code to make it more concise and readable.

5. How do I modify the code to handle errors or exceptions?

To handle errors or exceptions, you can use a try-catch block. The "try" block contains the code that may throw an error, while the "catch" block contains the code that will handle the error if it occurs. You can also use the "throw" keyword to manually throw an error and specify a custom error message.

Similar threads

Replies
4
Views
2K
Replies
8
Views
2K
Replies
2
Views
1K
Replies
4
Views
2K
Replies
11
Views
1K
Replies
13
Views
21K
Back
Top