This question already has answers here:
segmentation fault created by fortran if tests
(1 answer)
Why does the second if-clause even execute?
(2 answers)
Closed 4 years ago.
Just look at the following simple codes:
program hello
call foo()
contains
subroutine foo(bar)
logical, intent(in), optional :: bar
if (present(bar) .and. bar) then
print *, 'hello'
end if
end subroutine foo
end program hello
When compiled it by pgf90, I got Segmentation fault: 11 error, but it is ok when use gfortran. The problem should be at present(bar) .and. bar, but when bar is not given, present(bar) will be evaluated as .false., and the following bar should not be evaluted, which is null in this case.
Related
This question already has answers here:
Preserve bounds in allocation in intrinsic assignment
(2 answers)
Fortran doesn't keep lower/upper array bounds after copy to another allocatable array
(2 answers)
Closed 4 months ago.
I am trying to test the Fortran 2003 feature that allows to add an element to an array. Consider the following code:
program test
implicit none
integer :: i
integer, parameter :: n=6, j=2
integer, allocatable :: inx(:)
open(3, file='check.dat')
allocate(inx(0:1)); inx=0
do i=0,1
write(3,*) i, inx(i)
end do
do i=1,n
inx=[inx,i+j]
end do
do i=0,n
write(3,*) i, inx(i)
end do
end program test
I get the following runtime error:
At line 22 of file test.f90
Fortran runtime error: Index '0' of dimension 1 of array 'inx' below lower bound of 1
I explicitly set inx(0:1), so that inx's index starts from zero ( I want inx(0:some number)) but it seems that's it's reallocating from 1?
This question already has answers here:
Using matrices as arguments in functions and as output in subroutines in Fortran
(2 answers)
Function ‘array’ has no IMPLICIT type
(1 answer)
Closed 1 year ago.
I have a main function which creates an array and passes the array to a subroutine, the subroutine should then read in values to the array, my array has a size of 5 so the subroutine should have 5 numbers input into the array.
Here is my code currently:
program main
real arr(5)
c
call readData(arr)
stop
end
subroutine readData(arr)
c
do i=1, 5
read (*,*) arr(i)
end do
return
end
I need a special library for Fortran so that I can code and visualize on the fly, instead of writing to a text file then use either Python or Matlab to plot. I followed this:
https://people.sc.fsu.edu/~jburkardt/f_src/gnufor/gnufor.html
The instructions are not cleared so I did not use their examples, so I wrote my own code. There are 2 files: gnu.f90 from website, and myplot.f90, which I wrote like this:
program myplot
!Declare data types
implicit none
integer, parameter :: N1 = 50
real(kind=8) :: x1(N1),x2(N1)
real(kind=8) :: y1(N1)
real(kind=8) :: y2(N1)
integer :: i
!Generate 2D plot
do i = 1,N1
x1(i) = i
x2(i) = i
end do
y1 = x1**2
y2 = x2**3
!print *, 'Plotting'
!call plot(x1,y1,x2,y2)
call write_xy_data(x1,y1)
end program myplot
Apparently, from their gnufor.f90 file, I only need to do:
write_xy_data(X,Y) and it should work. In their example code, they did not use gnufor.f90 as a module so I didnt put: use gnufor at the beginning. Although, I tried that and it didn't work as well. So in my current directory, I have:
gnufor.90
myplot.f90
And to compile it, I am on Linux: gfortran myplot.f90 -o test
The error was:
Program received signal SIGSEGV: Segmentation fault - invalid memory reference.
Backtrace for this error:
#0 0x7FB0B051DE08
#1 0x7FB0B051CF90
#2 0x7FB0AFF574AF
#3 0x40302F in write_xy_data_
#4 0x400D7E in MAIN__ at myplot.f90:?
Segmentation fault (core dumped)
Any help is greatly appreciated. I expected the problem to be in my own code, although all array sizes are declared.
The subroutine write_xy_data in gnufor.f90 from the website you linked has a signature
subroutine write_xy_data(data_filename, n, x, y, ierror)
implicit none
character ( len = * ) data_filename
integer ( kind = 4 ) n
real ( kind = 8 ) x(n)
real ( kind = 8 ) y(n)
integer(kind = 4) ierror
Which means instead of passing only x,y, you have to call it as
call write_xy_data('myfile',N1,x1,y1,ierr)
passing as arguments the name of the file to write to ('myfile' in the example), the number of points to write (N1), the data (x1,y1) and an integer that carries information on the success of the write (ierror, this one has to be declared, too)
Also, there is no module gnufor, so no use clause is required, all functions in gnufor.F90 are global, which is also the reason why you need to pass the number of points as an extra argument, as assumed shape would require an explicit interface.
This question already has answers here:
Using scientific notation and underscore kind specifier at the same time for real literals in Fortran
(1 answer)
Numerical Precision in Fortran 95:
(2 answers)
Closed 4 years ago.
Relevant code:
Parts(3, indexcount) = push(pi/2, 0.0d0,0.0d0,0.0d0, Parts(1, indexcount),Parts(2, indexcount), 0.01d0, 20000.0d0)
real(16) function push(phaseinit, yinit, pxinit, pyinit, a0, R, dt, Time_total)
implicit none
real(16) :: phaseinit, yinit, pxinit, pyinit, gammamax, I0, R_star, k_ratio, Vph
etc...
When compiling:
Error: Type mismatch in argument 'yinit' at (1); passed REAL(8) to REAL(16)
I tried to find online resources detailing how exactly to properly initialize a numerical value for a real(16), but I couldn't find anything. This seemingly allows me to initialize real(8), but I don't know what the corresponding procedure is for real(16).
This question already has an answer here:
Asterisks in Fortran: Syntax error in argument list at (1)
(1 answer)
Closed 5 years ago.
What does the * mean as the argument in the subroutine? Does it mean a label and it returns to main program? is it related to return, return 1, ERR or ERROR? I don't know why we have these, but I think that asterisk is related to them.
!>Start the creation of boundary conditions for the equation set.
SUBROUTINE BOUNDARY_CONDITIONS_CREATE_START(SOLVER_EQUATIONS,BOUNDARY_CONDITIONS,ERR,ERROR,*)
!Argument variables
INTEGER(INTG), INTENT(OUT) :: ERR !<The error code
TYPE(VARYING_STRING), INTENT(OUT) :: ERROR !<The error string
!Local Variables
TYPE(VARYING_STRING) :: LOCAL_ERROR
ENTERS("BOUNDARY_CONDITIONS_CREATE_START",ERR,ERROR,*999)
IF(ASSOCIATED(BOUNDARY_CONDITIONS)) THEN
CALL FlagError("Boundary conditions is already associated.",ERR,ERROR,*999)
ELSE
IF(ASSOCIATED(SOLVER_EQUATIONS%SOLVER_MAPPING)) THEN
!Initialise the boundary conditions
CALL BOUNDARY_CONDITIONS_INITIALISE(SOLVER_EQUATIONS,ERR,ERROR,*999)
ELSE
LOCAL_ERROR="Solver equations solver mapping is not associated."
CALL FlagError(LOCAL_ERROR,ERR,ERROR,*999)
ENDIF
ENDIF
ENDIF
EXITS("BOUNDARY_CONDITIONS_CREATE_START")
RETURN
999 ERRORSEXITS("BOUNDARY_CONDITIONS_CREATE_START",ERR,ERROR)
RETURN 1
END SUBROUTINE BOUNDARY_CONDITIONS_CREATE_START
This is a label for the alternate return from the function. It is the label where the function can eventually return to when using
return 1
instead of
return
which returns to the location from which the subroutine was called.
This feature is strongly discouraged for new code although I have seen a proposal how to use this code for a kind of exceptions.
Very related question, almost a duplicate (asking for the return statement instead): Fortran return statement