我正在尝试使用4个模块将。矩阵写入.txt文件:
当我运行程序时,我收到程序接收信号SIGSEGV:分段错误 - 内存参考无效。
我做错了什么?
input.f90
module input
implicit none
contains
subroutine lesinput(n,omega)
!Output
integer :: n
real(8) :: omega
!Åpner fil og gir variable verdier
open(1, file='input.txt',status='old',action='read')
read(1,*), n
read(1,*), omega
close(1)
end subroutine lesinput
end module input
navierstokes_loser.f90
module navierstokes_loser
implicit none
contains
subroutine los_navier(n,omega,u)
!input
integer :: n
real(8) :: omega
!lokal
real(8) :: u(n+1,n+1)
integer :: i,j
u(n+1,n+1)=0.0d0
end subroutine los_navier
end module navierstokes_loser
resultatplot.f90
module resultatplot
implicit none
contains
subroutine vektorplot(n,u)
!input
integer :: n
real(8) :: u(n+1,n+1)
!lokale
integer :: i,j
real(8) :: vek_x
!Skriver vektor verdier til fil som gnuplot skal bruke
open(2,access='sequential',file='vekdata.txt',status='unknown')
write(2,*)'# x y vx vy'
do i=1,n+1
do j=1,n+1
vek_x=u(j,i)
write(2,*) i, j, vek_x
end do
write(2,*)''
end do
close(2,status='keep')
end subroutine vektorplot
end module resultatplot
main.f90时
program main
use input
use navierstokes_loser
use resultatplot
implicit none
integer :: n
real(8) :: omega
real(8), dimension (:,:), allocatable :: u
call lesinput(n,omega)
allocate(u(n+1,n+1))
call los_navier(n,omega,u)
call vektorplot(n,u)
end program main
答案 0 :(得分:1)
好的,我在这里看到了很多东西:
open
文件时,使用newunit=<somevar>
然后使用此<somevar>
作为阅读,写入和关闭的单位。但至少要使用大于10的数字,而不是1和2。u
:
main.f90
中,它是一个可分配的一维数组,但在los_navier
中它是一个二维数组。intent
有助于编译器及早发现错误。所以,不用多说,这是我的建议:
module input
implicit none
contains
subroutine lesinput(n,omega)
integer :: n
real(8) :: omega
integer :: read_unit
open(newunit=read_unit, file='input.txt',status='old',action='read')
read(read_unit,*), n
read(read_unit,*), omega
close(read_unit)
end subroutine lesinput
end module input
module navierstokes_loser
implicit none
contains
subroutine los_navier(n,omega,u)
integer, intent(in) :: n
real(8), intent(in) :: omega
real(8), allocatable, intent(out) :: u(:,:)
integer :: i,j
if (allocated(u)) deallocate(u)
allocate(u(n+1, n+1))
u=0.0d0
end subroutine los_navier
end module navierstokes_loser
module resultatplot
implicit none
contains
subroutine vektorplot(n,u)
integer, intent(in) :: n
real(8), intent(in) :: u(n+1,n+1)
integer :: i,j
integer :: write_unit
open(newunit=write_unit,access='sequential',file='vekdata.txt',status='unknown')
write(write_unit,*)'# x y vx vy'
do i=1,n+1
do j=1,n+1
write(write_unit,*) i, j, u(j, i)
end do
write(write_unit,*)''
end do
close(write_unit,status='keep')
end subroutine vektorplot
end module resultatplot
program main
use input
use navierstokes_loser
use resultatplot
implicit none
integer :: n
real(8) :: omega
real(8), dimension (:, :), allocatable :: u
call lesinput(n,omega)
call los_navier(n,omega,u)
call vektorplot(n,u)
end program main