2016-09-26 182 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

如果它已經在原代碼中,我就不那麼挑剔。我有時候把它留在那裏。 –