2016-09-26 189 views
-1

我想写出一个矩阵来使用4个模块.txt文件的模块有:读取输入,然后写入文件

  1. input.f90从.txt文件
  2. navierstokes_loser读取输入.F90创建矩阵(我将在这里补充一些做循环,而且,第一次我只需要它来打印)
  3. resultatplot.f90采取从navierstokes_loser矩阵然后打印到.txt文件
  4. main.f90时将所有模块并运行程序

当我运行程序时,我得到程序接收到的信号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

你可以试着一步一步来。首先阅读输入信息,以确保你做得对。添加矩阵的计算,确保它能够正常工作,然后将矩阵的打印添加到文件中。等待人们做所有事情都不是很明智。 – innoSPG

+0

它在main.f90和navierstokes_loser.f90中正确读取输入,但是当我添加矩阵时,它会出现错误。 – ursmooth

+0

所以我的问题是存储矩阵u,然后将它传递给vektorplot – ursmooth

回答

1

好吧,我在这里看到了不少东西:小于10

  1. 单位 - 这是非常危险的。最佳:当open ing文件,使用newunit=<somevar>,然后使用此<somevar>作为读取,写入和关闭的单位。但至少使用号码大于10,而不是1和2
  2. 讲起u
    1. main.f90,它是一个可分配,1- d阵列,但在los_navier它是一个2-d阵列。
    2. 它从来没有实际分配。
  3. 在子程序中清楚地指示有助于编译器及早发现错误。

因此,事不宜迟,这里是我的建议:

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 
+1

更好地快速提取出真正的实物价值,否则VladimirF会变得怪异。 – IanH

+0

也许,但是这个代码已经有太多问题了。 – chw21

+0

如果它已经在原代码中,我就不那么挑剔。我有时候把它留在那里。 –