2016-09-26 3 views
-1

ファイルへの書き込みモジュールは、以下のとおりです。は、その後、私は4つのモジュールを使用して、.txtファイルへの行列を記述しようとしています

  1. input.f90は.txtファイル
  2. navierstokes_loserからの入力を読み取り.F90は行列を作成
  3. main.f90のすべての組み合わせ
  4. が、それは.txtにプリントnavierstokes_loserから行列にファイルを取るresultatplot.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ファイルを使用する場合は、newunit=<somevar>を使用し、この<somevar>を読み取り、書き込み、および終了の単位として使用してください。しかし、少なくとも、使用時に10より大きい数字ではなく、1と2
  2. がの約uお話しましょう:main.f90
    1. を、それが割り当て可能、1次元配列ですが、los_navierで、それは2-D配列です。
    2. 実際に割り当てられることはありません。
  3. 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 
+1

実際の種類の値を素早く除外するか、またはVladimirFが不安定になります。 – IanH

+0

おそらく、このコードではあまりにも多くの問題がありました。 – chw21

+0

すでに元のコードになっている場合は、私はそれほど嫌なことではありません。私は時々自分でそれを残します。 –

関連する問題