43
Universit` a di Roma ”La Sapienza” Programmi utilizzati durante il corso ed esercizi Dott. Francesco Battista a.a. 2016-2017 Laboratorio di Calcolo di Aerodinamica Laurea in Ingegneria Aerospaziale

Programmi utilizzati durante il corso ed eserciziChapter 1 Prefazione Questo libretto e stato creato per raccogliere alcuni dei programmi che vengono mostrati durante il Laboratorio

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Programmi utilizzati durante il corso ed eserciziChapter 1 Prefazione Questo libretto e stato creato per raccogliere alcuni dei programmi che vengono mostrati durante il Laboratorio

Universita di Roma ”La Sapienza”

Programmi utilizzati durante il corso

ed esercizi

Dott. Francesco Battista

a.a. 2016-2017

Laboratorio di Calcolo di Aerodinamica

Laurea in Ingegneria Aerospaziale

Page 2: Programmi utilizzati durante il corso ed eserciziChapter 1 Prefazione Questo libretto e stato creato per raccogliere alcuni dei programmi che vengono mostrati durante il Laboratorio

Chapter 1

Prefazione

Questo libretto e stato creato per raccogliere alcuni dei programmi che vengono mostrati durante

il Laboratorio di Calcolo di Aerodinamica. Esso e in continuo aggiornamento quindi si consiglia

di controllare di tanto in tanto sul sito se vi e una versione aggiornata. La maggior parte dei

codici mostrati sono senza errori, alcuni, non indicati, presentano errori il cui scopo e mostrare

esplicitamente la corretta sintassi. Si consiglia vivamente percio di utilizzare questo libretto come

linea guida per la scrittura dei programmi e non come una dispensa tradizionale: la semplice lettura

dei codici non e di alcuna utilia.

I passi da seguire sono i seguenti:

• leggete la traccia dell’esercizio

• provate a risolvere l’esercizio scrivendo il programma in autonomia ovvero senza vederne la

soluzione sul libretto

• compilate il programma: il successo di questo passo vi permette di passare al passo successivo

in quanto il vostro programma non presenta errori sintattici, altrimenti cercate di correggere

l’errore. Ricordate in caso di errore il compilatore vi indica posizione e tipo di errore quindi

leggete con attenzione i messaggi del compilatore.

• eseguite il programma. A questo punto l’unica domanda che dovete porvi e: “il programma

esegue le istruzioni richieste dall’esercizio?” Se sı, non vi sono errori, altrimenti bisogna

rivedere il programma e le istruzioni in esso contenute.

• confrontate il vostro programma con quello riportato in questo libretto, ricordate “Non esiste

un unico modo per eseguire ognuno degli esercizi in questo libretto!!” quindi non necessaria-

mente i vostri programmi devono essere uguali a quelli riportati nel libretto.

Alcuni codici per il corretto funzionamento necessitano di un file di input che in questo libretto

non e riportato. Infine si ricorda ancora una volta che questo libretto e solo un’aiuto allo studio

che raccoglie tutti i programmi che sono stati discussi in modo approfondito durante le lezioni.

Buon Lavoro

2

Page 3: Programmi utilizzati durante il corso ed eserciziChapter 1 Prefazione Questo libretto e stato creato per raccogliere alcuni dei programmi che vengono mostrati durante il Laboratorio

Chapter 2

I primi programmi

Questo primo capitolo e dedicato ai primi programmi visti a lezione. Sono da ritenersi utili per

avere degli esempi della sintassi di base necessaria per poter scrivere programmi piu complicati.

2.1

Scrivere a schermo il saluto “Ciao Mondo”: definizione delle sezioni (esecutiva e conclusiva) fonda-

mentali ed imprescindibili di un programma.

1 ! File: hello.f90

2 ! The first program in Fortran 90

3 ! This code print a message on the screen

4 PROGRAM hello

5

6 ! Executive section

7 WRITE (*,*) ’Hello world!’

8

9 ! End section

10 STOP

11 END PROGRAM hello

2.2

Scrivere un numero, scelto dall’utente, a schermo: definizione della sezione dichiarativa.

1 ! File: assegnazione.f90

2 ! This program reads and prints a number on the screen

3 PROGRAM assegnazione

4

5 ! declaration section

6 IMPLICIT NONE

7 INTEGER :: i

8

9 ! execution section

10 WRITE (*,*) ’write and integer on the screen ’

11 READ (*,*) i

12

13 WRITE (*,*) ’You write ’,i

14

15 ! end section

3

Page 4: Programmi utilizzati durante il corso ed eserciziChapter 1 Prefazione Questo libretto e stato creato per raccogliere alcuni dei programmi che vengono mostrati durante il Laboratorio

16 STOP

17 END PROGRAM assegnazione

2.3

Scrivere due numeri scelti dall’utente a schermo ed effettuare calcoli aritmetici con essi scrivendo

a schermo il risultato: assegnazione del valore ad una variabile.

2.3.1

Operazione eseguita n2 = 10n1

1 ! File: assegnazione2.f90

2 PROGRAM assegnazione2

3

4 ! Sezione dichiarativa

5 IMPLICIT NONE

6 INTEGER :: num1 , num2

7

8 ! Sezione esecutiva

9 WRITE (*,*) ’Inserisci due interi (separati da spazio) e premi INVIO’

10 READ (*,*) num1 , num2

11

12 WRITE (*,*) ’Hai scritto:’,num1 , num2

13

14 num2 = num1 * 10

15

16 WRITE (*,*) ’Le nuove variabili sono:’,num1 , num2

17

18 ! Sezione conclusiva

19 STOP

20 END PROGRAM assegnazione2

2.3.2

Operazione eseguita n2 = 10 + n1

1 ! File: assegnazione3.f90

2 PROGRAM assegnazione3

3

4 ! Sezione dichiarativa

5 IMPLICIT NONE

6 INTEGER :: num1 , num2

7

8 ! Sezione esecutiva

9 WRITE (*,*) ’Inserisci due interi (separati da spazio) e premi INVIO’

10 READ (*,*) num1 , num2

11

12 WRITE (*,*) ’Hai scritto:’,num1 , num2

13

14 num2 = num1 + 10

15

16 WRITE (*,*) ’Le nuove variabili sono:’,num1 , num2

17

18 ! Sezione conclusiva

4

Page 5: Programmi utilizzati durante il corso ed eserciziChapter 1 Prefazione Questo libretto e stato creato per raccogliere alcuni dei programmi che vengono mostrati durante il Laboratorio

19 STOP

20 END PROGRAM assegnazione3

2.3.3

Operazione eseguita n1 = 10 + n1

1 ! File: assegnazione4.f90

2 PROGRAM assegnazione4

3

4 ! Sezione dichiarativa

5 IMPLICIT NONE

6 INTEGER :: num1 , num2

7

8 ! Sezione esecutiva

9 WRITE (*,*) ’Inserisci due interi (separati da spazio) e premi INVIO’

10 READ (*,*) num1 , num2

11

12 WRITE (*,*) ’Hai scritto:’,num1 , num2

13

14 num1 = num1 + 10

15

16 WRITE (*,*) ’Le nuove variabili sono:’,num1 , num2

17

18 ! Sezione conclusiva

19 STOP

20 END PROGRAM assegnazione4

2.3.4

Operazione eseguita n2 + 10 = n1

1 ! File: assegnazione5.f90

2 PROGRAM assegnazione5

3

4 ! Sezione dichiarativa

5 IMPLICIT NONE

6 INTEGER :: num1 , num2

7

8 ! Sezione esecutiva

9 WRITE (*,*) ’Inserisci due interi (separati da spazio) e premi INVIO’

10 READ (*,*) num1 , num2

11

12 WRITE (*,*) ’Hai scritto:’,num1 , num2

13

14 num2 + 10 = num1

15

16 WRITE (*,*) ’Le nuove variabili sono:’,num1 , num2

17

18 ! Sezione conclusiva

19 STOP

20 END PROGRAM assegnazione5

2.3.5

Operazione eseguita n2 = n1 + n2

5

Page 6: Programmi utilizzati durante il corso ed eserciziChapter 1 Prefazione Questo libretto e stato creato per raccogliere alcuni dei programmi che vengono mostrati durante il Laboratorio

1 ! File: assegnazione6.f90

2 PROGRAM assegnazione6

3

4 ! Sezione dichiarativa

5 IMPLICIT NONE

6 INTEGER :: num1 , num2

7

8 ! Sezione esecutiva

9 WRITE (*,*) ’Inserisci due interi (separati da spazio) e premi INVIO’

10 READ (*,*) num1 , num2

11

12 WRITE (*,*) ’Hai scritto:’,num1 , num2

13

14 num2 = num1 + num2

15

16 WRITE (*,*) ’Le nuove variabili sono:’,num1 , num2

17

18 ! Sezione conclusiva

19 STOP

20 END PROGRAM assegnazione6

2.4

Dato il raggio (numero reale) scrivere l’area ed il perimetro del cerchio: definizione di varibili ed

utilizzo delle stesse.

1 ! File: cerchio.f90

2 !Questo programma legge un reale dallo schermo

3 !e calcola l’area e la circonferenza del cerchio

4 !di cui il reale e’ il raggio

5 PROGRAM cerchio

6

7 ! Sezione dichiarativa

8 IMPLICIT NONE

9 REAL :: radius

10 REAL , PARAMETER :: pi =3.141592

11

12 ! Sezione esecutiva

13 WRITE (*,*) ’Qual il raggio del cerchio?’

14 READ (*,*) radius

15

16 WRITE (*,*) ’Il perimetro del cerchio e’’:’ ,2. * pi * radius

17 WRITE (*,*) ’L’’area del cerchio e’’:’, pi * radius **2

18

19 ! Sezione conclusiva

20 STOP

21 END PROGRAM cerchio

2.5

Dati due interi calcolarne il massimo.

6

Page 7: Programmi utilizzati durante il corso ed eserciziChapter 1 Prefazione Questo libretto e stato creato per raccogliere alcuni dei programmi che vengono mostrati durante il Laboratorio

2.5.1

Utilizzo della funzione max(a,b).

1 ! File: massimo1.f90

2 ! Calcolo del massimo dati due numeri

3 PROGRAM massimo

4

5 ! Sezione dichiarativa

6 IMPLICIT NONE

7 INTEGER :: num1 , num2 , massimo

8

9 ! Sezione esecutiva

10 WRITE (*,*) ’Inserisci due interi (separati da spazio)’

11 READ (*,*) num1 , num2

12

13 massimo = max(num1 ,num2)

14

15 WRITE (*,*) ’Il massimo e’’:’,massimo

16

17 ! Sezione conclusiva

18 STOP

19 END PROGRAM massimo

2.5.2

Utilizzo della sintassi if-then-else.

1 ! File: massimo.f90

2 ! Calcolo del massimo dati due numeri

3 PROGRAM massimo

4

5 ! Sezione dichiarativa

6 IMPLICIT NONE

7 INTEGER :: num1 , num2 , massimo

8

9 ! Sezione esecutiva

10 WRITE (*,*) ’Inserisci due interi (separati da spazio)’

11 READ (*,*) num1 , num2

12

13 IF (num1.GT.num2) then

14 massimo = num1

15 ELSE

16 massimo = num2

17 ENDIF

18

19 WRITE (*,*) ’Il massimo e’’:’,massimo

20

21 ! Sezione conclusiva

22 STOP

23 END PROGRAM massimo

2.6

Dato un intero scelto dall’utente dire se e pari o dispari: utilizzo della struttura if-then-else e della

funzione mod(a,b).

7

Page 8: Programmi utilizzati durante il corso ed eserciziChapter 1 Prefazione Questo libretto e stato creato per raccogliere alcuni dei programmi che vengono mostrati durante il Laboratorio

1 ! file even_odd.f90

2 ! This code reads one integer and say if it is even or odd

3 PROGRAM even_odd

4 !sezione dichiarativa

5 IMPLICIT NONE

6 INTEGER i1

7 CHARACTER (4) num

8 !sezione esecutiva

9 WRITE (*,*) ’Insert one integer , then press ENTER’

10 READ (*,*) i1

11 if (mod(i1 ,2).eq.0) then

12 num=’even’

13 else

14 num=’odd’

15 endif

16 write (*,*) ’the inserted number is ’,num

17 !sezione conclusiva

18 STOP

19 END

2.7

Data una sequenza di 3 numeri (interi) calcolarne la somma e scriverla a schermo: utilizzo dell’istruzione

ciclica do-loop

1 !file: somma.f90

2 !Show the cicle statements use

3 PROGRAM somma

4 !Declaration section

5 IMPLICIT NONE

6 INTEGER :: amount , num1 , summ , i

7 !Execution section

8 amount =3

9 WRITE (*,*)’Insert ’, amount ,’integer and press each time ENTER:’

10 summ=0

11 DO i=1,amount

12 READ (*,*) num1

13 summ = summ + num1

14 ENDDO

15 WRITE (*,*) ’The sum reads:’, summ

16 !End section

17 STOP

18 END

2.8

Dato un intero, scelto dall’utente, calcolare il fattoriale del numero stesso: utilizzo dell’istruzione

ciclica do-loop

1 !file: fattoriale.f90

2 !This program reads a number and compute the factorial

3 PROGRAM fattoriale

4 !sezione dichiarativa

5 IMPLICIT NONE

8

Page 9: Programmi utilizzati durante il corso ed eserciziChapter 1 Prefazione Questo libretto e stato creato per raccogliere alcuni dei programmi che vengono mostrati durante il Laboratorio

6 INTEGER :: n, m

7 INTEGER :: fact

8 !sezione esecutiva

9 WRITE (*,*) ’Write the number on the screen and press ENTER’

10 READ (*,*) n

11 fact=n

12 DO m=1,n-1

13 fact=fact*(n-m)

14 ENDDO

15 if (n.eq.0) fact=1

16 WRITE (*,*) ’The factorial of’, n,’is:’,fact

17 !sezione esecutiva

18 STOP

19 END PROGRAM fattoriale

2.9

Data una sequenza di interi (di 0 e 1) scelta dall’utente dire da quanti zeri e formata la sequenza

di zeri, consecutivi, piu lunga: utilizzo dell’istruzione ciclica indefinita do-loop

1 ! file: sequenza.f90

2 ! This program is an example of the undefined DO -LOOP

3 PROGRAM sequenza

4 !declaration section

5 IMPLICIT NONE

6 INTEGER :: bit , cont , maxleng

7 cont = 0

8 maxleng = 0

9 !execution section

10 WRITE (*,*) ’Insert a 0,1,2 sequence (press ENTER each time)’

11 DO

12 WRITE (*,*) ’Insert a number (no 0 or 1 to end):’

13 READ (*,*) bit

14 IF (bit .ne. 1 .and. bit .ne. 0) EXIT

15 IF (bit .eq. 0) THEN

16 cont = cont + 1

17 IF (cont .gt. maxleng) maxleng = maxleng + 1

18 ELSE

19 cont = 0

20 ENDIF

21 ENDDO

22 WRITE (*,*) ’The most length sequence of only 0 counts ’&

23 ,maxleng ,’ zeros ’

24 !end section

25 STOP

26 END

2.10

Dati due numeri interi scelti dall’utente calcolare il massimo comune denominatore: utilizzo delle

istruzioni if-then-else, do-loop e della funzione mod(a,b)

1 !file: mcd.f90

2 !This program prints the greater common divisor of two integers

9

Page 10: Programmi utilizzati durante il corso ed eserciziChapter 1 Prefazione Questo libretto e stato creato per raccogliere alcuni dei programmi che vengono mostrati durante il Laboratorio

3 PROGRAM mcd

4 !sezione dichiatativa

5 IMPLICIT NONE

6 INTEGER :: a, b, m

7 !sezione esecutiva

8 WRITE (*,*) ’Insert two number separated by a space:’

9 READ (*,*) a, b

10

11 m=a

12 IF (b.lt.a) m=b

13 DO

14 IF (mod(b,m).eq.0. and.mod(a,m).eq.0) EXIT

15 m = m-1

16 ENDDO

17 WRITE (*,*) ’The greater common divisor is’,m

18 !sezione conclusiva

19 STOP

20 END

2.11

Dati due numeri interi scelti dall’utente calcolare il massimo:

2.11.1

Utilizzo della sotto-unita subroutine

1 ! File: massimo.f90

2 ! Calcolo del massimo dati due numeri

3 PROGRAM massimo

4 ! Sezione dichiarativa

5 IMPLICIT NONE

6 INTEGER :: num1 , num2 , mas

7 ! Sezione esecutiva

8 WRITE (*,*) ’Inserisci due interi (separati da spazio)’

9 READ (*,*) num1 , num2

10 call calc(num1 ,num2 ,mas)

11 WRITE (*,*) ’Il massimo e’’:’,mas

12 ! Sezione conclusiva

13 STOP

14 END PROGRAM massimo

15 !____________________________________________________________

16 !____________________________________________________________

17 SUBROUTINE calc(x,y,z)

18 IMPLICIT NONE

19 INTEGER :: x,y,z

20 IF (x.GT.y) then

21 z = x

22 ELSE

23 z = y

24 ENDIF

25 RETURN

26 END SUBROUTINE calc

10

Page 11: Programmi utilizzati durante il corso ed eserciziChapter 1 Prefazione Questo libretto e stato creato per raccogliere alcuni dei programmi che vengono mostrati durante il Laboratorio

2.11.2

Utilizzo della sotto-unita function

1 ! File: massimo.f90

2 ! Calcolo del massimo dati due numeri

3 PROGRAM massimo

4 ! Sezione dichiarativa

5 IMPLICIT NONE

6 INTEGER :: num1 , num2 , mas , calc

7 ! Sezione esecutiva

8 WRITE (*,*) ’Inserisci due interi (separati da spazio)’

9 READ (*,*) num1 , num2

10 mas = calc(num1 ,num2)

11 WRITE (*,*) ’Il massimo e’’:’,mas

12 ! Sezione conclusiva

13 STOP

14 END PROGRAM massimo

15 !____________________________________________________________

16 !____________________________________________________________

17 FUNCTION calc(x,y)

18 IMPLICIT NONE

19 INTEGER :: x,y,calc

20 IF (x.GT.y) then

21 calc = x

22 ELSE

23 calc = y

24 ENDIF

25 RETURN

26 END FUNCTION calc

2.12

Creare una stringa (nome di un file) concatenando tre stringhe:

• la versione ‘a’

• il tempo ‘10’

• l’estensione ‘.dat’.

La stringa finale sara ‘a00010.dat’

1 ! File: name_file.f90

2 ! Gestire e creare una stringa corrispondende

3 ! al nome di un file

4 PROGRAM name_file

5 IMPlICIT NONE

6 CHARACTER (30):: name

7 CHARACTER (1):: ver

8 CHARACTER (6):: time

9 CHARACTER (4):: ext

10 INTEGER :: it

11

12 it= 10

13

14 ext=’.dat’

11

Page 12: Programmi utilizzati durante il corso ed eserciziChapter 1 Prefazione Questo libretto e stato creato per raccogliere alcuni dei programmi che vengono mostrati durante il Laboratorio

15 ver=’a’

16 WRITE(time ,’(i6.6)’) it

17 name = ver//time//ext

18

19 WRITE (*,*) ’Apro il file ’,name

20

21 STOP

22 END PROGRAM name_file

2.13

Leggere un numero da un file chiamato ‘input.dat’ e scrivere il numero a schermo

1 ! File: name_file.f90

2 ! Gestire e creare una stringa corrispondende

3 ! al nome di un file

4 PROGRAM read_file

5 IMPlICIT NONE

6 CHARACTER (30):: name_file

7 CHARACTER (7):: ver

8 CHARACTER (4):: ext

9 REAL :: pi

10

11 ext=’.dat’

12 ver=’input’

13 name_file = trim(ver)//ext

14

15 WRITE (*,*) ’Apro il file ’,name_file

16 OPEN(UNIT=1,FILE=name_file ,STATUS=’old’,ACTION=’read’)

17 READ (1,*) pi

18 CLOSE (1)

19

20 WRITE (*,*) ’Il contenuto del file ’,name_file ,’ e’’ ’, pi

21 WRITE (*,10) name_file , pi

22

23 10 FORMAT(’Il contenuto del file ’,A9,’ e’’’, F7.4)

24 STOP

25 END PROGRAM read_file

12

Page 13: Programmi utilizzati durante il corso ed eserciziChapter 1 Prefazione Questo libretto e stato creato per raccogliere alcuni dei programmi che vengono mostrati durante il Laboratorio

Chapter 3

Soluzione di ODE

3.1

Calcolare e la soluzione esatta della seguente equazione differenziale ordinaria:

dy

dx= −3y (3.1)

y(0) = y0 = 1 (3.2)

nel punto x=10, tramite l’uso di una subroutine.

1 PROGRAM main

2 IMPLICIT NONE

3 REAL :: y,x,alpha ,y0

4

5 x=1.0

6 alpha =3.0

7 y0=1.0

8 CALL exact_sol(y,x,y0,alpha)

9

10 WRITE (*,*) ’La soluzione al tempo t=’,x,’ e’’ uguale a y=’,y

11

12 STOP

13 END PROGRAM main

14 !_____________________________________________________________

15

16 SUBROUTINE exact_sol(y,x,y0 ,a)

17 IMPLICIT NONE

18 REAL ,INTENT(IN):: y0,x,a

19 REAL ,INTENT(OUT):: y

20

21 y = y0*exp(-x*a)

22

23 RETURN

24 END SUBROUTINE exact_sol

13

Page 14: Programmi utilizzati durante il corso ed eserciziChapter 1 Prefazione Questo libretto e stato creato per raccogliere alcuni dei programmi che vengono mostrati durante il Laboratorio

3.2

Calcolare e la soluzione esatta della seguente equazione differenziale ordinaria:

dy

dx= −3y (3.3)

y(0) = y0 = 1 (3.4)

dal punto x=0 al punto x=10, tramite l’uso di una subroutine.

1 PROGRAM main

2 IMPLICIT NONE

3 INTEGER ,PARAMETER :: Nx=10

4 INTEGER :: i

5 REAL :: y,x,alpha ,y0

6 REAL :: Dx ,x_max ,x_min

7

8 x_max =2.

9 x_min =0.

10 Dx=(x_max -x_min)/(Nx)

11 alpha =3.0

12 y0=1.

13

14 do i=0,Nx

15 x=x_min+Dx*i

16 CALL exact_sol(y,x,y0,alpha)

17

18 WRITE (*,*) x,y

19 enddo

20

21 WRITE (*,*) ’Fine della simulazione ’

22

23 STOP

24 END PROGRAM main

25 !_____________________________________________________________

26

27 SUBROUTINE exact_sol(y,x,y0 ,a)

28 IMPLICIT NONE

29 REAL ,INTENT(IN):: y0,x,a

30 REAL ,INTENT(OUT):: y

31

32 y = y0*exp(-x*a)

33

34 RETURN

35 END SUBROUTINE exact_sol

3.3

Calcolare e la soluzione esatta e la soluzione approssimata con il metodo di eulero della seguente

equazione differenziale ordinaria:

dy

dx= −3y (3.5)

y(0) = y0 = 1 (3.6)

dal punto x=0 al punto x=10. Scrivere infine la soluzione su un file.

14

Page 15: Programmi utilizzati durante il corso ed eserciziChapter 1 Prefazione Questo libretto e stato creato per raccogliere alcuni dei programmi che vengono mostrati durante il Laboratorio

1 PROGRAM main

2 IMPLICIT NONE

3 INTEGER ,PARAMETER :: Nx=10

4 INTEGER :: i

5 REAL :: y,x,alpha ,y0

6 REAL :: Dx ,y_old ,y_new

7 REAL :: rhs ,x_max ,x_min

8 REAL :: error , err_max

9

10 error = 0.0

11 err_max = 0.0

12

13 call input(x_min ,x_max ,alpha ,y0)

14 Dx=(x_max -x_min)/float(Nx)

15 y_old=y0

16

17 call write_sol(x_min ,y0,y0)

18

19 do i=1,Nx

20 x=x_min+Dx*i

21 CALL exact_sol(y,x,y0,alpha)

22

23 y_new = y_old + Dx*rhs(y_old ,alpha)

24

25 call write_sol(x,y,y_new)

26

27 y_old = y_new

28

29 error = abs(y_new -y)

30 err_max = max(err_max ,error)

31 enddo

32

33 WRITE (*,*) ’Dx , errore ’,Dx ,error

34 WRITE (*,*) ’Fine della simulazione ’

35

36 STOP

37 END PROGRAM main

38 !_____________________________________________________________

39 !_____________________________________________________________

40 SUBROUTINE exact_sol(y,x,y0 ,a)

41 IMPLICIT NONE

42 REAL ,INTENT(IN):: y0,x,a

43 REAL ,INTENT(OUT):: y

44

45 y = y0*exp(-x*a)

46

47 RETURN

48 END SUBROUTINE exact_sol

49 !_____________________________________________________________

50 SUBROUTINE input(x_min ,x_max ,alpha ,y0)

51 IMPLICIT NONE

52 REAL ,INTENT(OUT):: y0,alpha ,x_min ,x_max

53

54 OPEN(UNIT=1,FILE=’input.dat’,STATUS=’old’,ACTION=’read’)

55 READ (1,*) x_min

56 READ (1,*) x_max

15

Page 16: Programmi utilizzati durante il corso ed eserciziChapter 1 Prefazione Questo libretto e stato creato per raccogliere alcuni dei programmi che vengono mostrati durante il Laboratorio

57 READ (1,*) y0

58 READ (1,*) alpha

59 CLOSE (1)

60

61 RETURN

62 END SUBROUTINE input

63 !_____________________________________________________________

64 FUNCTION rhs(y,a)

65 IMPLICIT NONE

66 REAL ,INTENT(IN) :: a,y

67 REAL :: rhs

68

69 rhs=-a*y

70

71 RETURN

72 END FUNCTION rhs

73 !_____________________________________________________________

74 SUBROUTINE write_sol(x,ye ,ya)

75 IMPLICIT NONE

76 REAL ,INTENT(IN)::x,ye,ya

77

78 OPEN(UNIT=1,FILE=’output.dat’,STATUS=’unknown ’,ACTION=’write ’,ACCESS=’append ’)

79 WRITE (1,*) x,ye ,ya

80 CLOSE (1)

81

82 RETURN

83 END SUBROUTINE write_sol

84 !_____________________________________________________________

3.4

Risolvere il problema della sezione 3.3 con il metodo di Heun

1 PROGRAM main

2 IMPLICIT NONE

3 INTEGER ,PARAMETER :: Nx=10

4 INTEGER :: i

5 REAL :: y,x,alpha ,y0

6 REAL :: Dx ,y_old ,y_new ,y_ast

7 REAL :: rhs ,x_max ,x_min

8 REAL :: error ,err_max

9

10 error = 0.

11 err_max = 0.

12

13 call input(x_min ,x_max ,alpha ,y0)

14 Dx=(x_max -x_min)/float(Nx)

15 y_old=y0

16

17 call write_sol(x_min ,y0,y0)

18

19 do i=1,Nx

20 x=x_min+Dx*i

21 CALL exact_sol(y,x,y0,alpha)

22

23 y_ast = y_old + Dx*rhs(y_old ,alpha)

16

Page 17: Programmi utilizzati durante il corso ed eserciziChapter 1 Prefazione Questo libretto e stato creato per raccogliere alcuni dei programmi che vengono mostrati durante il Laboratorio

24 y_new = y_old + Dx *.5*( rhs(y_old ,alpha)+rhs(y_ast ,alpha))

25

26

27 error = abs(y_new -y)

28 err_max=max(err_max ,error)

29

30 call write_sol(x,y,y_new)

31

32 y_old = y_new

33 enddo

34

35 WRITE (*,*) ’ERRORE MAX’,Dx ,err_max

36 WRITE (*,*) ’Fine della simulazione ’

37

38 STOP

39 END PROGRAM main

40 !_____________________________________________________________

41 !_____________________________________________________________

42 SUBROUTINE exact_sol(y,x,y0 ,a)

43 IMPLICIT NONE

44 REAL ,INTENT(IN):: y0,x,a

45 REAL ,INTENT(OUT):: y

46

47 y = y0*exp(-x*a)

48

49 RETURN

50 END SUBROUTINE exact_sol

51 !_____________________________________________________________

52 SUBROUTINE input(x_min ,x_max ,alpha ,y0)

53 IMPLICIT NONE

54 REAL ,INTENT(OUT):: y0,alpha ,x_min ,x_max

55

56 OPEN(UNIT=1,FILE=’input.dat’,STATUS=’old’,ACTION=’read’)

57 READ (1,*) x_min

58 READ (1,*) x_max

59 READ (1,*) y0

60 READ (1,*) alpha

61 CLOSE (1)

62

63 RETURN

64 END SUBROUTINE input

65 !_____________________________________________________________

66 FUNCTION rhs(y,a)

67 IMPLICIT NONE

68 REAL ,INTENT(IN) :: a,y

69 REAL :: rhs

70

71 rhs=-a*y

72

73 RETURN

74 END FUNCTION rhs

75 !_____________________________________________________________

76 SUBROUTINE write_sol(x,ye ,ya)

77 IMPLICIT NONE

78 REAL ,INTENT(IN)::x,ye,ya

79

17

Page 18: Programmi utilizzati durante il corso ed eserciziChapter 1 Prefazione Questo libretto e stato creato per raccogliere alcuni dei programmi che vengono mostrati durante il Laboratorio

80 OPEN(UNIT=1,FILE=’output.dat’,STATUS=’unknown ’,ACTION=’write ’,POSITION=’append ’)

81 WRITE (1,*) x,ye ,ya

82 CLOSE (1)

83

84 RETURN

85 END SUBROUTINE write_sol

86 !____________________________________________________________

3.5

Risolvere il problema della sezione 3.3 con il metodo di Runge-Kutta al IV ordine

1 PROGRAM main

2 IMPLICIT NONE

3 INTEGER ,PARAMETER :: Nx =5000

4 INTEGER ,PARAMETER :: nrk=4

5 INTEGER :: i,l

6 REAL :: y,x,alpha ,y0

7 REAL :: Dx ,y_old ,y_new ,y_ast

8 REAL :: rhs ,x_max ,x_min

9 REAL :: error ,err_max

10 REAL ,DIMENSION (1: nrk) :: crk1 ,crk2

11 REAL ,DIMENSION (0: nrk) :: k

12 REAL :: rhsrk

13

14 crk1= (/0., .5, .5, 1./)

15 crk2= (/1., 2., 2., 1./)

16

17 error = 0.

18 err_max = 0.

19

20 call input(x_min ,x_max ,alpha ,y0)

21 Dx=(x_max -x_min)/float(Nx)

22 y_old=y0

23

24 call write_sol(x_min ,y0,y0)

25

26 do i=1,Nx

27 x=x_min+Dx*i

28 CALL exact_sol(y,x,y0,alpha)

29

30 rhsrk =0.

31 y_new = y_old

32 k(0)=0.

33 do l=1,nrk

34 y_old = y_new + crk1(l) * Dx * k(l-1)

35 k(l) = rhs(y_old ,alpha)

36 rhsrk = rhsrk + k(l)*crk2(l)

37 enddo

38

39 y_new = y_new + Dx/6.* rhsrk

40

41

42 error = abs(y_new -y)

43 err_max=max(err_max ,error)

44

18

Page 19: Programmi utilizzati durante il corso ed eserciziChapter 1 Prefazione Questo libretto e stato creato per raccogliere alcuni dei programmi che vengono mostrati durante il Laboratorio

45 call write_sol(x,y,y_new)

46

47 y_old = y_new

48 enddo

49

50 WRITE (*,*) ’ERRORE MAX’,Dx ,err_max

51 WRITE (*,*) ’Fine della simulazione ’

52

53 STOP

54 END PROGRAM main

55 !_____________________________________________________________

56 !_____________________________________________________________

57 SUBROUTINE exact_sol(y,x,y0 ,a)

58 IMPLICIT NONE

59 REAL ,INTENT(IN):: y0,x,a

60 REAL ,INTENT(OUT):: y

61

62 y = y0*exp(-x*a)

63

64 RETURN

65 END SUBROUTINE exact_sol

66 !_____________________________________________________________

67 SUBROUTINE input(x_min ,x_max ,alpha ,y0)

68 IMPLICIT NONE

69 REAL ,INTENT(OUT):: y0,alpha ,x_min ,x_max

70

71 OPEN(UNIT=1,FILE=’input.dat’,STATUS=’old’,ACTION=’read’)

72 READ (1,*) x_min

73 READ (1,*) x_max

74 READ (1,*) y0

75 READ (1,*) alpha

76 CLOSE (1)

77

78 RETURN

79 END SUBROUTINE input

80 !_____________________________________________________________

81 FUNCTION rhs(y,a)

82 IMPLICIT NONE

83 REAL ,INTENT(IN) :: a,y

84 REAL :: rhs

85

86 rhs=-a*y

87

88 RETURN

89 END FUNCTION rhs

90 !_____________________________________________________________

91 SUBROUTINE write_sol(x,ye ,ya)

92 IMPLICIT NONE

93 REAL ,INTENT(IN)::x,ye,ya

94

95 OPEN(UNIT=1,FILE=’output.dat’,STATUS=’unknown ’,ACTION=’write ’,POSITION=’append ’)

96 WRITE (1,*) x,ye ,ya

97 CLOSE (1)

98

99 RETURN

100 END SUBROUTINE write_sol

19

Page 20: Programmi utilizzati durante il corso ed eserciziChapter 1 Prefazione Questo libretto e stato creato per raccogliere alcuni dei programmi che vengono mostrati durante il Laboratorio

101 !____________________________________________________________

3.6

Risolvere il problema di una particella inerziale sottoposta a gravita ed a resistenza di Stokes:

dvpdt

= −vpτp

+ g (3.7)

vp(0) = 0 (3.8)

Il sistema precedente ha la seguente soluzione esatta:

vp(t) =(

1− e−t/τp)g τp (3.9)

Si usi un metodo di integrazione accurato al primo ordine.

1 PROGRAM main

2 IMPLICIT NONE

3 INTEGER ,PARAMETER :: Nt =100000

4 INTEGER :: n

5 REAL :: v,t,tau ,v0,g

6 REAL :: Dt ,v_old ,v_new

7 REAL :: rhs ,t_max ,t_min

8 REAL :: error ,err_max

9

10 error = 0.

11 err_max = 0.

12

13 CALL input(t_min ,t_max ,tau ,g,v0)

14 Dt=(t_max -t_min)/float(Nt)

15 v_old=v0

16

17 CALL write_sol(t_min ,v0,v0,error)

18

19 DO n=1,Nt

20 t=t_min+Dt*n

21 CALL exact_sol(v,t,v0,tau ,g)

22

23 v_new = v_old + rhs(Dt ,v_old ,tau ,g)

24

25

26 error = abs(v_new -v)

27 err_max=max(err_max ,error)

28

29 CALL write_sol(t,v,v_new ,error)

30

31 v_old = v_new

32 ENDDO

33

34 WRITE (*,*) ’ERRORE MAX’,Dt ,err_max

35 WRITE (*,*) ’Fine della simulazione ’

36

37 STOP

38 END PROGRAM main

39 !_____________________________________________________________

20

Page 21: Programmi utilizzati durante il corso ed eserciziChapter 1 Prefazione Questo libretto e stato creato per raccogliere alcuni dei programmi che vengono mostrati durante il Laboratorio

40 !_____________________________________________________________

41 SUBROUTINE exact_sol(v,t,v0 ,tau ,g)

42 IMPLICIT NONE

43 REAL ,INTENT(IN):: v0,t,tau ,g

44 REAL ,INTENT(OUT):: v

45

46 v = (v0 -g*tau)*exp(-t/tau)+g*tau

47

48 RETURN

49 END SUBROUTINE exact_sol

50 !_____________________________________________________________

51 SUBROUTINE input(t_min ,t_max ,tau ,g,v0)

52 IMPLICIT NONE

53 REAL ,INTENT(OUT):: v0,tau ,g,t_min ,t_max

54

55 OPEN(UNIT=1,FILE=’input.dat’,STATUS=’old’,ACTION=’read’)

56 READ (1,*) t_min

57 READ (1,*) t_max

58 READ (1,*) v0

59 READ (1,*) tau

60 READ (1,*) g

61 CLOSE (1)

62 RETURN

63 END SUBROUTINE input

64 !_____________________________________________________________

65 FUNCTION rhs(dt,v,tau ,g)

66 IMPLICIT NONE

67 REAL ,INTENT(IN) :: dt,tau ,g,v

68 REAL :: rhs

69

70 rhs = (-v/tau + g) * dt

71

72 RETURN

73 END FUNCTION rhs

74 !_____________________________________________________________

75 SUBROUTINE write_sol(x,ye ,ya ,err)

76 IMPLICIT NONE

77 REAL ,INTENT(IN)::x,ye,ya,err

78

79 OPEN(UNIT=1,FILE=’output.dat’,STATUS=’unknown ’,ACTION=’write ’,POSITION=’append ’)

80 WRITE (1,*) x,ye ,ya ,err

81 CLOSE (1)

82

83 RETURN

84 END SUBROUTINE write_sol

85 !_____________________________________________________________

3.7

Al problema della sezione precedente, § 3.6 si aggiunga l’equazione per la posizione della particella:

dxpdt

= vp (3.10)

xp(0) = 0 (3.11)

21

Page 22: Programmi utilizzati durante il corso ed eserciziChapter 1 Prefazione Questo libretto e stato creato per raccogliere alcuni dei programmi che vengono mostrati durante il Laboratorio

La soluzione esatta per questa nuova equazione :

xp(t) = τp g[t+ τp

(e−t/τp − 1

)](3.12)

1 PROGRAM main

2 IMPLICIT NONE

3 INTEGER ,PARAMETER :: Nt =1000

4 INTEGER :: n

5 REAL :: x,v,t,tau ,x0,v0,g

6 REAL :: Dt ,v_old ,v_new

7 REAL :: x_old ,x_new

8 REAL :: rhs_x ,rhs_v ,t_max ,t_min

9 REAL :: error ,err_max

10

11 error = 0.

12 err_max = 0.

13

14 CALL input(t_min ,t_max ,tau ,g,v0,x0)

15 Dt=(t_max -t_min)/float(Nt)

16 v_old=v0

17

18 CALL write_sol(t_min ,v0,v0,x0,x0)

19

20 DO n=1,Nt

21 t=t_min+Dt*n

22 CALL exact_sol(v,x,t,v0,tau ,g)

23

24 v_new = v_old + rhs_v(Dt ,v_old ,tau ,g)

25 x_new = x_old + rhs_x(Dt ,v_old)

26

27 CALL write_sol(t,v,v_new ,x,x_new)

28

29 v_old = v_new

30 x_old = x_new

31 ENDDO

32

33 WRITE (*,*) ’ERRORE MAX’,Dt ,err_max

34 WRITE (*,*) ’Fine della simulazione ’

35

36 STOP

37 END PROGRAM main

38 !_____________________________________________________________

39 !_____________________________________________________________

40 SUBROUTINE exact_sol(v,x,t,v0 ,tau ,g)

41 IMPLICIT NONE

42 REAL ,INTENT(IN):: v0,t,tau ,g

43 REAL ,INTENT(OUT):: v,x

44

45 x = tau*g*(t+tau*(exp(-t/tau) -1.))

46 v = (1.-exp(-t/tau))*g*tau

47

48 RETURN

49 END SUBROUTINE exact_sol

50 !_____________________________________________________________

51 SUBROUTINE input(t_min ,t_max ,tau ,g,v0 ,x0)

52 IMPLICIT NONE

22

Page 23: Programmi utilizzati durante il corso ed eserciziChapter 1 Prefazione Questo libretto e stato creato per raccogliere alcuni dei programmi che vengono mostrati durante il Laboratorio

53 REAL ,INTENT(OUT):: x0,v0,tau ,g,t_min ,t_max

54

55 OPEN(UNIT=1,FILE=’input.dat’,STATUS=’old’,ACTION=’read’)

56 READ (1,*) t_min

57 READ (1,*) t_max

58 READ (1,*) x0

59 READ (1,*) v0

60 READ (1,*) tau

61 READ (1,*) g

62 CLOSE (1)

63 RETURN

64 END SUBROUTINE input

65 !_____________________________________________________________

66 FUNCTION rhs_v(dt,v,tau ,g)

67 IMPLICIT NONE

68 REAL ,INTENT(IN) :: dt,tau ,g,v

69 REAL :: rhs_v

70

71 rhs_v = (-v/tau + g) * dt

72

73 RETURN

74 END FUNCTION rhs_v

75 !_____________________________________________________________

76 FUNCTION rhs_x(dt,v)

77 IMPLICIT NONE

78 REAL ,INTENT(IN) :: dt,v

79 REAL :: rhs_x

80

81 rhs_x = v * Dt

82

83 RETURN

84 END FUNCTION rhs_x

85 !_____________________________________________________________

86 SUBROUTINE write_sol(x,ye ,ya ,xe ,xa)

87 IMPLICIT NONE

88 REAL ,INTENT(IN)::x,ye,ya,xe,xa

89

90 OPEN(UNIT=1,FILE=’output.dat’,STATUS=’unknown ’,ACTION=’write ’,POSITION=’append ’)

91 WRITE (1,*) x,ye ,ya ,xe ,xa

92 CLOSE (1)

93

94 RETURN

95 END SUBROUTINE write_sol

96 !_____________________________________________________________

23

Page 24: Programmi utilizzati durante il corso ed eserciziChapter 1 Prefazione Questo libretto e stato creato per raccogliere alcuni dei programmi che vengono mostrati durante il Laboratorio

Chapter 4

Soluzione di PDE

4.1

Scrivere un programma per il calcolo della soluzione dell’equazione di convezione lineare se ne

confronti il risultato approssimato con quello analitico. Il sistema differenziale da risolvere :

∂u

∂t+ a

∂u

∂x= 0 [0, 1]× [0, 10] (4.1)

u(0, t) = 0 (4.2)

u(x, 0) = u0(x) = max [0, (x− 0.25)(0.5− x)] (4.3)

Scrivere il risultato su diversi file per diversi istanti temporali ed integrare il sistema con un metodo

esplicito accurato al primo ordine nel tempo e nello spazio.

1 !____. file: eulero_upwind.f90

2 !____. This program applies the eulero integration to the

3 !____. linear convection partial differential equation

4 PROGRAM main

5 !____.__________________________________________________________________

6 IMPLICIT NONE

7 INTEGER :: it , j

8 INTEGER :: itmin ,itmax ,itout

9 INTEGER ,PARAMETER :: Nx=100,Ng=1

10 !____.__________________________________________________________________

11 REAL ,DIMENSION (1-Ng:Nx+Ng):: v_new ,v_old ,v_exact ,x

12 REAL:: delta_t ,delta_x ,a_conv ,rhs ,rhs_uw ,rhs_uw2

13 REAL:: error ,error_max ,time ,v0

14 !____.__________________________________________________________________

15 ! metric definition

16 CALL griglia(Nx,Ng,x,delta_x)

17 !____.__________________________________________________________________

18 ! some initializations

19 DO j=1-ng,nx+ng

20 v_old(j)=v0(x(j))

21 ENDDO

22 error =0.

23 error_max = -999999.9

24 !____.__________________________________________________________________

25 ! read on file input.dat

26 CALL input(itmin ,itmax ,itout ,a_conv ,delta_t)

27 !____.__________________________________________________________________

24

Page 25: Programmi utilizzati durante il corso ed eserciziChapter 1 Prefazione Questo libretto e stato creato per raccogliere alcuni dei programmi che vengono mostrati durante il Laboratorio

28 ! compute maximun number of iterations and open write unit

29 !____.__________________________________________________________________

30 write (*,*) ’cfl’,delta_t*a_conv/delta_x

31 CALL write_disk(x,v_old ,v_old ,0,Nx,Ng)

32 DO it= itmin , itmax

33 DO j=1,nx

34 rhs = rhs_uw(v_old(j),v_old(j-1),a_conv ,delta_x)

35 v_new(j) = v_old(j) + delta_t * rhs

36 ENDDO

37 time = it*delta_t

38 !____. boundary conditions

39 DO j=1-ng ,0

40 v_new(j)=0.

41 ENDDO

42 !____. update the solution

43 DO j=1-ng ,Nx+ng

44 v_old(j)= v_new(j)

45 ENDDO

46 !____. compute the error

47 CALL exact_sol(Nx,Ng,x,time ,a_conv ,v_exact)

48

49 error =0.

50 DO j=1,Nx

51 error = error + abs(v_new(j)-v_exact(j))

52 ENDDO

53 error_max=max(error_max ,error/float(Nx))

54 !____. write on disk

55 if (mod(it ,itout).eq.0) CALL write_disk(x,v_new ,v_exact ,it,Nx,Ng)

56 ENDDO

57

58 WRITE (*,*) ’dt , error=’,delta_t ,error_max

59 WRITE (*,*) ’solution stored in file velocity.dat’

60 WRITE (*,*) ’Simulation ended!’

61 STOP

62 END

63 !____.__________________________________________________________________

64 !____.__________________________________________________________________

65 !____.__________________________________________________________________

66 !____.__________________________________________________________________

67 FUNCTION rhs_uw(vj,vjm ,a,Dx)

68 IMPLICIT NONE

69 REAL:: rhs_uw ,vj,vjm ,a,Dx

70

71 rhs_uw=-a*(vj-vjm)/Dx

72

73 RETURN

74 END

75 !____.__________________________________________________________________

76 !____.__________________________________________________________________

77 FUNCTION v0(x)

78 IMPLICIT NONE

79 REAL:: v0 ,x,fx

80

81 fx=(.25-x)*(x-.5)

82

83 v0=max(0.,fx)

25

Page 26: Programmi utilizzati durante il corso ed eserciziChapter 1 Prefazione Questo libretto e stato creato per raccogliere alcuni dei programmi che vengono mostrati durante il Laboratorio

84

85 RETURN

86 END

87 !____.__________________________________________________________________

88 !____.__________________________________________________________________

89 SUBROUTINE exact_sol(Nx ,Ng ,x,time ,a_conv ,v_exact)

90

91 IMPLICIT NONE

92 INTEGER :: Nx ,Ng ,j

93 REAL ,DIMENSION (1-ng:nx+ng):: x, v_exact

94 REAL:: v0 , appo , time , a_conv

95

96 DO j=1-ng,Nx+ng

97 appo=x(j)-a_conv*time

98 v_exact(j)=v0(appo)

99 ENDDO

100

101 RETURN

102 END

103 !____.__________________________________________________________________

104 !____.__________________________________________________________________

105 SUBROUTINE write_disk(x,v,u,it ,nx ,ng)

106

107 IMPLICIT NONE

108 INTEGER :: it ,nx ,ng ,j

109 REAL ,DIMENSION (1-ng:Nx+ng):: x,v,u

110 CHARACTER (32):: opfile

111

112 opfile=’bcl12345678.dat’

113 WRITE(opfile (4:11) ,1000) it

114 !!!!! WRITE(opfile (4:11) ,’(i8.8) ’) it

115 opfile = ’./DATA/’// opfile

116 1000 FORMAT(i8.8)

117

118 OPEN(UNIT=13,FILE=opfile ,STATUS=’unknown ’,ACTION=’write ’)

119 DO j=1-Ng,Nx

120 WRITE (13,*) x(j),v(j),u(j)

121 ENDDO

122 CLOSE (13)

123

124 RETURN

125 END

126 !____.__________________________________________________________________

127 !____.__________________________________________________________________

128 SUBROUTINE griglia(Nx ,Ng ,x,delta_x)

129 IMPLICIT NONE

130 INTEGER Nx ,Ng ,j

131 REAL ,DIMENSION (1-ng:Nx+ng):: x

132 REAL L,delta_x

133 L=1.

134 delta_x=L/float(Nx+1)

135

136 DO j=1-ng,Nx+ng

137 x(j)=j*delta_x

138 ENDDO

139

26

Page 27: Programmi utilizzati durante il corso ed eserciziChapter 1 Prefazione Questo libretto e stato creato per raccogliere alcuni dei programmi che vengono mostrati durante il Laboratorio

140 RETURN

141 END

142 !____.__________________________________________________________________

143 !____.__________________________________________________________________

144 SUBROUTINE input(itmin ,itmax ,itout ,a_conv ,delta_t)

145 IMPLICIT NONE

146 INTEGER :: itmin ,itmax ,itout

147 REAL :: a_conv ,delta_t

148 OPEN(UNIT=1,FILE=’input.dat’,STATUS=’old’,ACTION=’read’)

149 READ (1,*) itmin

150 READ (1,*) itmax

151 READ (1,*) itout

152 READ (1,*) a_conv

153 READ (1,*) delta_t

154 CLOSE (1)

155 RETURN

156 END SUBROUTINE input

4.2

Integrare il sistema della sezione precedente, § 4.1, con un metodo esplicito accurato al primo ordine

nel tempo e al secondo ordine nello spazio.

1 !____. file: eulero_upwind2.f90

2 !____. This program applies the eulero integration to the

3 !____. linear convection partial differential equation

4 PROGRAM main

5 !____.__________________________________________________________________

6 IMPLICIT NONE

7 INTEGER :: it , j

8 INTEGER :: itmin ,itmax ,itout

9 INTEGER ,PARAMETER :: Nx=100,Ng=2

10 !____.__________________________________________________________________

11 REAL ,DIMENSION (1-Ng:Nx+Ng):: v_new ,v_old ,v_exact ,x

12 REAL:: delta_t ,delta_x ,a_conv ,rhs ,rhs_uw ,rhs_uw2

13 REAL:: error ,error_max ,time ,v0

14 !____.__________________________________________________________________

15 ! metric definition

16 CALL griglia(Nx,Ng,x,delta_x)

17 !____.__________________________________________________________________

18 ! some initializations

19 DO j=1-ng,nx+ng

20 v_old(j)=v0(x(j))

21 ENDDO

22 error =0.

23 error_max = -999999.9

24 !____.__________________________________________________________________

25 ! read on file eulero_stokes.dat

26 CALL input(itmin ,itmax ,itout ,a_conv ,delta_t)

27 !____.__________________________________________________________________

28 ! compute maximun number of iterations and open write unit

29 !____.__________________________________________________________________

30 write (*,*) ’cfl’,delta_t*a_conv/delta_x

31 CALL write_disk(x,v_old ,v_old ,0,Nx,Ng)

32 DO it= itmin , itmax

27

Page 28: Programmi utilizzati durante il corso ed eserciziChapter 1 Prefazione Questo libretto e stato creato per raccogliere alcuni dei programmi che vengono mostrati durante il Laboratorio

33 DO j=1,nx

34 rhs = rhs_uw2(v_old(j),v_old(j-1),v_old(j-2),a_conv ,delta_x)

35 v_new(j) = v_old(j) + delta_t * rhs

36 ENDDO

37 time = it*delta_t

38 !____. boundary conditions

39 DO j=1-ng ,0

40 v_new(j)=0.

41 ENDDO

42 !____. update the solution

43 DO j=1-ng ,Nx+ng

44 v_old(j)= v_new(j)

45 ENDDO

46 !____. compute the error

47 CALL exact_sol(Nx,Ng,x,time ,a_conv ,v_exact)

48

49 error =0.

50 DO j=1,Nx

51 error = error + abs(v_new(j)-v_exact(j))

52 ENDDO

53 error_max=max(error_max ,error/float(Nx))

54 !____. write on disk

55 if (mod(it ,itout).eq.0) CALL write_disk(x,v_new ,v_exact ,it,Nx,Ng)

56 ENDDO

57

58 WRITE (*,*) ’dt , error=’,delta_t ,error_max

59 WRITE (*,*) ’solution stored in file velocity.dat’

60 WRITE (*,*) ’Simulation ended!’

61 STOP

62 END

63 !____.__________________________________________________________________

64 !____.__________________________________________________________________

65 !____.__________________________________________________________________

66 !____.__________________________________________________________________

67 FUNCTION rhs_uw2(vj,vjm ,vjmm ,a,Dx)

68 IMPLICIT NONE

69 REAL:: rhs_uw2 ,vj,vjm ,vjmm ,a,Dx

70

71 rhs_uw2=-a*(3.*vj -4.* vjm+vjmm)/(2.*Dx)

72

73 RETURN

74 END

75 !____.__________________________________________________________________

76 !____.__________________________________________________________________

77 FUNCTION v0(x)

78 IMPLICIT NONE

79 REAL:: v0 ,x,fx

80

81 fx=(.25-x)*(x-.5)

82

83 v0=max(0.,fx)

84

85 RETURN

86 END

87 !____.__________________________________________________________________

88 !____.__________________________________________________________________

28

Page 29: Programmi utilizzati durante il corso ed eserciziChapter 1 Prefazione Questo libretto e stato creato per raccogliere alcuni dei programmi che vengono mostrati durante il Laboratorio

89 SUBROUTINE exact_sol(Nx ,Ng ,x,time ,a_conv ,v_exact)

90

91 IMPLICIT NONE

92 INTEGER :: Nx ,Ng ,j

93 REAL ,DIMENSION (1-ng:nx+ng):: x,v_exact

94 REAL:: v0 , appo , a_conv , time

95

96 DO j=1-ng,Nx+ng

97 appo=x(j)-a_conv*time

98 v_exact(j)=v0(appo)

99 ENDDO

100

101 RETURN

102 END

103 !____.__________________________________________________________________

104 !____.__________________________________________________________________

105 SUBROUTINE write_disk(x,v,u,it ,nx ,ng)

106

107 IMPLICIT NONE

108 INTEGER it ,nx ,ng ,j

109 REAL ,DIMENSION (1-ng:nx+ng):: x,v,u

110 CHARACTER (32) opfile

111

112 opfile=’acl12345678.dat’

113 WRITE(opfile (4:11) ,1000) it

114 opfile = ’./DATA/’// opfile

115 1000 FORMAT(i8.8)

116

117 OPEN(UNIT=13,FILE=opfile ,STATUS=’unknown ’,ACTION=’write ’)

118 DO j=1-Ng,Nx

119 WRITE (13,*) x(j),v(j),u(j)

120 ENDDO

121 CLOSE (13)

122

123 RETURN

124 END

125 !____.__________________________________________________________________

126 !____.__________________________________________________________________

127 SUBROUTINE griglia(Nx ,Ng ,x,delta_x)

128 IMPLICIT NONE

129 INTEGER :: Nx ,Ng ,j

130 REAL ,DIMENSION (1-ng:nx+ng):: x

131 REAL:: L,delta_x

132 L=1.

133 delta_x=L/float(Nx)

134

135 DO j=1-ng,Nx+ng

136 x(j)=j*delta_x

137 ENDDO

138

139 RETURN

140 END

141 !____.__________________________________________________________________

142 !____.__________________________________________________________________

143 SUBROUTINE input(itmin ,itmax ,itout ,a_conv ,delta_t)

144 IMPLICIT NONE

29

Page 30: Programmi utilizzati durante il corso ed eserciziChapter 1 Prefazione Questo libretto e stato creato per raccogliere alcuni dei programmi che vengono mostrati durante il Laboratorio

145 INTEGER :: itmin ,itmax ,itout

146 REAL :: a_conv ,delta_t

147 OPEN(UNIT=1,FILE=’input.dat’,STATUS=’old’,ACTION=’read’)

148 READ (1,*) itmin

149 READ (1,*) itmax

150 READ (1,*) itout

151 READ (1,*) a_conv

152 READ (1,*) delta_t

153 CLOSE (1)

154 RETURN

155 END SUBROUTINE input

4.3

Scrivere un programma per il calcolo della soluzione dell’equazione del calore se ne confronti il

risultato approssimato con quello analitico. Il sistema differenziale da risolvere :

∂u

∂t= ν

∂2u

∂x2[0, 1]× [0, 10] (4.4)

u(0, t) = 100 (4.5)

u(1, t) = 100 (4.6)

u(x, 0) = u0(x) = u(0, t) + [v(1, t)− v(0, t)] x−∞∑m=1

400

(2m− 1)πsin((2m− 1)πx) (4.7)

La cui soluzione esatta :

u(x, t) = u(0, t) + [v(1, t)− v(0, t)] x−∞∑m=1

400

(2m− 1)πsin((2m− 1)πx) exp(−ν (2m− 1)2π2 t)

Scrivere il risultato su diversi file per diversi istanti temporali ed integrare il sistema con un

metodo esplicito accurato al primo ordine nel tempo e al secondo ordine nello spazio.

1 !____. file: FTCS.f90

2 !____. This program applies the eulero explicit time integration

3 !____. and a second order spatial discretization to the

4 !____. Fourier partial differential equation

5 PROGRAM main

6 !____.__________________________________________________________________

7 IMPLICIT NONE

8 INTEGER :: it , j

9 INTEGER :: itmin ,itmax ,itout

10 INTEGER ,PARAMETER :: Nx=20,Ng=1

11 !____.__________________________________________________________________

12 REAL ,DIMENSION (1-Ng:Nx+Ng):: v_new ,v_old ,v_exact ,x

13 REAL:: delta_t ,delta_x ,nu,rhs_cent ,rhs ,va,vb

14 REAL:: error ,error_max ,time ,v0

15 !____.__________________________________________________________________

16 ! metric definition

17 CALL griglia(Nx,Ng,x,delta_x)

18 !____.__________________________________________________________________

19 ! read on file eulero_stokes.dat

20 CALL input(itmin ,itmax ,itout ,nu,delta_t ,va,vb)

21 !____.__________________________________________________________________

30

Page 31: Programmi utilizzati durante il corso ed eserciziChapter 1 Prefazione Questo libretto e stato creato per raccogliere alcuni dei programmi che vengono mostrati durante il Laboratorio

22 ! some initializations

23 call exact_sol(Nx,Ng,x,0.,nu,va,vb,v_exact)

24

25 DO j=1,nx

26 v_old(j)=0.!v_exact(j)

27 ENDDO

28 v_old (0)=1.

29 v_old(Nx+1) =0.5

30

31 error =0.

32 error_max = -999999.9

33 !____.__________________________________________________________________

34 ! compute maximun number of iterations and open write unit

35 !____.__________________________________________________________________

36 write (*,*) ’cfl’,delta_t*nu/delta_x **2

37 CALL write_disk(x,v_old ,v_old ,0,Nx,Ng)

38 DO it= itmin , itmax

39 DO j=1,nx

40 rhs = rhs_cent(v_old(j+1),v_old(j),v_old(j-1),nu ,delta_x)

41 v_new(j) = v_old(j) + delta_t * rhs

42 ENDDO

43 time = it*delta_t

44 !____. boundary conditions

45 DO j=1-ng ,0

46 v_new( j) = 1.

47 v_new(Nx+1+j) = 0.5

48 ENDDO

49 !____. update the solution

50 DO j=1-ng ,Nx+ng

51 v_old(j)= v_new(j)

52 ENDDO

53 !____. compute the error

54 CALL exact_sol(Nx,Ng,x,time ,nu,va,vb,v_exact)

55

56 error =0.

57 DO j=1,Nx

58 error = error + abs(v_new(j)-v_exact(j))

59 ENDDO

60 error_max=max(error_max ,error/float(Nx))

61 !____. write on disk

62 if (mod(it ,itout).eq.0) CALL write_disk(x,v_new ,v_exact ,it,Nx,Ng)

63 ENDDO

64

65 WRITE (*,*) ’dt , error=’,delta_t ,error_max

66 WRITE (*,*) ’Simulation ended!’

67 STOP

68 END

69 !____.__________________________________________________________________

70 !____.__________________________________________________________________

71 !____.__________________________________________________________________

72 !____.__________________________________________________________________

73 FUNCTION rhs_cent(vjp ,vj,vjm ,a,Dx)

74 IMPLICIT NONE

75 REAL:: rhs_cent ,vjp ,vj,vjm ,a,Dx

76

77 rhs_cent=a*(vjp -2.*vj+vjm)/Dx**2

31

Page 32: Programmi utilizzati durante il corso ed eserciziChapter 1 Prefazione Questo libretto e stato creato per raccogliere alcuni dei programmi che vengono mostrati durante il Laboratorio

78

79 RETURN

80 END

81 !____.__________________________________________________________________

82 !____.__________________________________________________________________

83 SUBROUTINE exact_sol(Nx ,Ng ,x,time ,nu ,va ,vb ,v_exact)

84

85 IMPLICIT NONE

86 INTEGER :: Nx ,Ng ,j, MW , m

87 REAL ,DIMENSION (1-ng:nx+ng):: x,v_exact

88 REAL:: appo , va, vb, vs, appo2 ,time ,nu

89 REAL ,PARAMETER :: pi=acos (-1.)

90

91 MW = 1000

92

93 DO j=1-ng,Nx+ng

94 vs = va + (vb - va) * x(j)

95 appo2 = 0.

96 DO m = 1, MW

97 appo = (2 * m -1) * pi

98 appo2 = appo2 + 400./ appo * sin(appo*x(j)) &

99 * exp(-nu*appo **2* time)

100 ENDDO

101

102 v_exact(j) = - appo2 + vs

103 ENDDO

104

105 RETURN

106 END

107 !____.__________________________________________________________________

108 !____.__________________________________________________________________

109 SUBROUTINE write_disk(x,v,u,it ,nx ,ng)

110

111 IMPLICIT NONE

112 INTEGER :: it ,nx ,ng ,j

113 REAL ,DIMENSION (1-ng:nx+ng):: x,v,u

114 CHARACTER (32):: opfile

115

116 opfile=’acl12345678.dat’

117 WRITE(opfile (4:11) ,1000) it

118 opfile = ’./DATA/’// opfile

119 1000 FORMAT(i8.8)

120

121 OPEN(UNIT=13,FILE=opfile ,STATUS=’unknown ’,ACTION=’write ’)

122 DO j=1-Ng,Nx+ng

123 WRITE (13,*) x(j),v(j),u(j)

124 ENDDO

125 CLOSE (13)

126

127 RETURN

128 END

129 !____.__________________________________________________________________

130 !____.__________________________________________________________________

131 SUBROUTINE griglia(Nx ,Ng ,x,delta_x)

132 IMPLICIT NONE

133 INTEGER :: Nx ,Ng ,j

32

Page 33: Programmi utilizzati durante il corso ed eserciziChapter 1 Prefazione Questo libretto e stato creato per raccogliere alcuni dei programmi che vengono mostrati durante il Laboratorio

134 REAL ,DIMENSION (1-ng:nx+ng):: x

135 REAL:: L,delta_x

136 L=1.

137 delta_x=L/float(Nx+1)

138

139 DO j=1-ng,Nx+ng

140 x(j)=j*delta_x

141 ENDDO

142

143 RETURN

144 END

145 !____.__________________________________________________________________

146 !____.__________________________________________________________________

147 SUBROUTINE input(itmin ,itmax ,itout ,nu ,delta_t ,va ,vb)

148 IMPLICIT NONE

149 INTEGER :: itmin ,itmax ,itout

150 REAL :: nu,delta_t ,va,vb

151 OPEN(UNIT=1,FILE=’input.dat’,STATUS=’old’,ACTION=’read’)

152 READ (1,*) itmin

153 READ (1,*) itmax

154 READ (1,*) itout

155 READ (1,*)nu

156 READ (1,*) delta_t

157 READ (1,*)va

158 READ (1,*)vb

159 CLOSE (1)

160 RETURN

161 END SUBROUTINE input

4.4

Scrivere un programma per il calcolo della soluzione dell’equazione del calore, sistema differenziale

presentato nella $ 4.3, con un metodo implicito applicando il metodo di Jacobi per la soluzione del

sistema lineare,

1 !____. file: jacobi_BTCS.f90

2 !____. This program uses the Jacobi method to integrate

3 !____. the heat equation with an implicit method.

4 PROGRAM main

5 !____.__________________________________________________________________

6 IMPLICIT NONE

7 INTEGER :: it , j

8 INTEGER :: itmin ,itmax ,itout

9 INTEGER ,PARAMETER :: Nx=20,Ng=1

10 !____.__________________________________________________________________

11 REAL ,DIMENSION (1-Ng:Nx+Ng):: v_new ,v_old ,v_exact ,x

12 REAL:: delta_t ,delta_x ,nu,rhs_cent ,rhs ,va,vb

13 REAL:: error ,error_max ,time ,v0

14 !____.__________________________________________________________________

15 ! metric definition

16 CALL griglia(Nx,Ng,x,delta_x)

17 !____.__________________________________________________________________

18 ! read on file eulero_stokes.dat

19 CALL input(itmin ,itmax ,itout ,nu,delta_t ,va,vb)

20 !____.__________________________________________________________________

33

Page 34: Programmi utilizzati durante il corso ed eserciziChapter 1 Prefazione Questo libretto e stato creato per raccogliere alcuni dei programmi che vengono mostrati durante il Laboratorio

21 ! some initializations

22 call exact_sol(Nx,Ng,x,0.,nu,va,vb,v_exact)

23 DO j=1-ng,nx+ng

24 v_old(j)=v_exact(j)

25 ENDDO

26 error =0.

27 error_max = -999999.9

28 !____.__________________________________________________________________

29 write (*,*) ’cfl’,delta_t*nu/delta_x **2

30 CALL write_disk(x,v_old ,v_old ,0,Nx,Ng)

31 DO it= itmin , itmax

32

33 call jacobi(Nx,Ng,v_old ,v_new ,delta_x ,delta_t ,nu)

34 time = it*delta_t

35

36 !____. boundary conditions

37 DO j=1-ng ,0

38 v_new( j) = va

39 v_new(Nx+1+j) = vb

40 ENDDO

41

42 !____. update the solution

43 DO j=1-ng ,Nx+ng

44 v_old(j)= v_new(j)

45 ENDDO

46

47 !____. compute the error

48 CALL exact_sol(Nx,Ng,x,time ,nu,va,vb,v_exact)

49

50 error =0.

51 DO j=1,Nx

52 error = error + abs(v_new(j)-v_exact(j))

53 ENDDO

54 error_max=max(error_max ,error/float(Nx))

55 !____. write on disk

56 if (mod(it ,itout).eq.0) CALL write_disk(x,v_new ,v_exact ,it,Nx,Ng)

57 ENDDO

58

59 WRITE (*,*) ’dt , error=’,delta_t ,error_max

60 WRITE (*,*) ’Simulation ended!’

61 STOP

62 END

63 !____.__________________________________________________________________

64 !____.__________________________________________________________________

65 !____.__________________________________________________________________

66 !____.__________________________________________________________________

67 SUBROUTINE jacobi(nx ,ng ,v_old ,v_new ,delta_x ,Dt ,nu)

68 !____.

69 IMPLICIT NONE

70 INTEGER :: nx ,ng ,i,j,m,itmax

71 REAL ,DIMENSION (1-ng:nx+ng),INTENT(OUT):: v_new

72 REAL ,DIMENSION (1-ng:nx+ng),INTENT(IN):: v_old

73 REAL ,DIMENSION (1-ng:nx+ng):: v_it

74 REAL:: tol ,error ,appo ,delta_x , cfl , dt, nu

75

76 tol =1.E-10

34

Page 35: Programmi utilizzati durante il corso ed eserciziChapter 1 Prefazione Questo libretto e stato creato per raccogliere alcuni dei programmi che vengono mostrati durante il Laboratorio

77 m=0

78 cfl = Dt*nu/delta_x **2

79

80

81 DO j=1-ng,nx+ng

82 v_it(j)=v_old(j)

83 ENDDO

84

85 DO

86 error =0.

87 DO j=1,nx

88 v_new(j)=(v_old(j)+cfl*(v_it(j-1)+v_it(j+1)))/(1.+ cfl *2.)

89 error = error + abs(v_new(j)-v_it(j))

90 ENDDO

91 if (error/float(Nx).lt.tol) then

92 exit

93 else

94 DO j=1,nx

95 v_it(j)=v_new(j)

96 ENDDO

97 endif

98 m = m + 1

99 ENDDO

100 write (*,*) ’it=’,m,’error=’,error

101 !____.

102 RETURN

103 END SUBROUTINE jacobi

104 !____.__________________________________________________________________

105 !____.__________________________________________________________________

106 SUBROUTINE exact_sol(Nx ,Ng ,x,time ,nu ,va ,vb ,v_exact)

107

108 IMPLICIT NONE

109 INTEGER :: Nx ,Ng ,j, MW , m

110 REAL ,DIMENSION (1-ng:nx+ng):: x,v_exact

111 REAL:: appo , va, vb, vs, appo2 ,time ,nu

112 REAL ,PARAMETER :: pi=acos (-1.)

113

114 MW = 10

115

116 DO j=1-ng,Nx+ng

117 vs = va + (vb - va) * x(j)

118 appo2 = 0.

119 DO m = 1, MW

120 appo = (2 * m -1) * pi

121 appo2 = appo2 + 4./ appo * sin(appo*x(j)) &

122 * exp(-nu*appo **2* time)

123 ENDDO

124

125 v_exact(j) = - appo2 + vs

126 ENDDO

127

128 RETURN

129 END SUBROUTINE exact_sol

130 !____.__________________________________________________________________

131 !____.__________________________________________________________________

132 SUBROUTINE write_disk(x,v,u,it ,nx ,ng)

35

Page 36: Programmi utilizzati durante il corso ed eserciziChapter 1 Prefazione Questo libretto e stato creato per raccogliere alcuni dei programmi che vengono mostrati durante il Laboratorio

133

134 IMPLICIT NONE

135 INTEGER :: it ,nx ,ng ,j

136 REAL ,DIMENSION (1-ng:nx+ng):: x,v,u

137 CHARACTER (32):: opfile

138

139 opfile=’acl12345678.dat’

140 WRITE(opfile (4:11) ,1000) it

141 opfile = ’./DATA/’// opfile

142 1000 FORMAT(i8.8)

143

144 OPEN(UNIT=13,FILE=opfile ,STATUS=’unknown ’,ACTION=’write ’)

145 DO j=1-Ng,Nx+ng

146 WRITE (13,*) x(j),v(j),u(j)

147 ENDDO

148 CLOSE (13)

149

150 RETURN

151 END SUBROUTINE write_disk

152 !____.__________________________________________________________________

153 !____.__________________________________________________________________

154 SUBROUTINE griglia(Nx ,Ng ,x,delta_x)

155 IMPLICIT NONE

156 INTEGER :: Nx ,Ng ,j

157 REAL ,DIMENSION (1-ng:nx+ng):: x

158 REAL:: L, delta_x

159 L=1.

160 delta_x=L/float(Nx+1)

161

162 DO j=1-ng,Nx+ng

163 x(j)=j*delta_x

164 ENDDO

165

166 RETURN

167 END SUBROUTINE griglia

168 !____.__________________________________________________________________

169 !____.__________________________________________________________________

170 SUBROUTINE input(itmin ,itmax ,itout ,nu ,delta_t ,va ,vb)

171 IMPLICIT NONE

172 INTEGER :: itmin ,itmax ,itout

173 REAL :: nu,delta_t ,va,vb

174 OPEN(UNIT=1,FILE=’input.dat’,STATUS=’old’,ACTION=’read’)

175 READ (1,*) itmin

176 READ (1,*) itmax

177 READ (1,*) itout

178 READ (1,*)nu

179 READ (1,*) delta_t

180 READ (1,*)va

181 READ (1,*)vb

182 CLOSE (1)

183 RETURN

184 END SUBROUTINE input

36

Page 37: Programmi utilizzati durante il corso ed eserciziChapter 1 Prefazione Questo libretto e stato creato per raccogliere alcuni dei programmi che vengono mostrati durante il Laboratorio

4.5

Scrivere un programma per il calcolo della soluzione dell’equazione del calore, sistema differenziale

presentato nella $ 4.3, con un metodo implicito applicando il metodo di Gauss-Seidel per la soluzione

del sistema lineare,

1 !____. file: gauss_seidel_BTCS.f90

2 !____. This program uses the Gauss -Seidel method to integrate

3 !____. the heat equation with an implicit method.

4 PROGRAM main

5 !____.__________________________________________________________________

6 IMPLICIT NONE

7 INTEGER :: it , j

8 INTEGER :: itmin ,itmax ,itout

9 INTEGER ,PARAMETER :: Nx=20,Ng=1

10 !____.__________________________________________________________________

11 REAL ,DIMENSION (1-Ng:Nx+Ng):: v_new ,v_old ,v_exact ,x

12 REAL:: delta_t ,delta_x ,nu,rhs_cent ,rhs ,va,vb

13 REAL:: error ,error_max ,time ,v0

14 !____.__________________________________________________________________

15 ! metric definition

16 CALL griglia(Nx,Ng,x,delta_x)

17 !____.__________________________________________________________________

18 ! read on file eulero_stokes.dat

19 CALL input(itmin ,itmax ,itout ,nu,delta_t ,va,vb)

20 !____.__________________________________________________________________

21 ! some initializations

22 call exact_sol(Nx,Ng,x,0.,nu,va,vb,v_exact)

23 DO j=1-ng,nx+ng

24 v_old(j)=v_exact(j)

25 ENDDO

26 error =0.

27 error_max = -999999.9

28 !____.__________________________________________________________________

29 write (*,*) ’cfl’,delta_t*nu/delta_x **2

30 CALL write_disk(x,v_old ,v_old ,0,Nx,Ng)

31 DO it= itmin , itmax

32

33 call gauss_seidel(Nx,Ng,v_old ,v_new ,delta_x ,delta_t ,nu)

34 time = it*delta_t

35

36 !____. boundary conditions

37 DO j=1-ng ,0

38 v_new( j) = va

39 v_new(Nx+1+j) = vb

40 ENDDO

41

42 !____. update the solution

43 DO j=1-ng ,Nx+ng

44 v_old(j)= v_new(j)

45 ENDDO

46

47 !____. compute the error

48 CALL exact_sol(Nx,Ng,x,time ,nu,va,vb,v_exact)

49

50 error =0.

51 DO j=1,Nx

37

Page 38: Programmi utilizzati durante il corso ed eserciziChapter 1 Prefazione Questo libretto e stato creato per raccogliere alcuni dei programmi che vengono mostrati durante il Laboratorio

52 error = error + abs(v_new(j)-v_exact(j))

53 ENDDO

54 error_max=max(error_max ,error/float(Nx))

55 !____. write on disk

56 if (mod(it ,itout).eq.0) CALL write_disk(x,v_new ,v_exact ,it,Nx,Ng)

57 ENDDO

58

59 WRITE (*,*) ’dt , error=’,delta_t ,error_max

60 WRITE (*,*) ’Simulation ended!’

61 STOP

62 END

63 !____.__________________________________________________________________

64 !____.__________________________________________________________________

65 !____.__________________________________________________________________

66 !____.__________________________________________________________________

67 SUBROUTINE gauss_seidel(nx ,ng ,v_old ,v_new ,delta_x ,Dt ,nu)

68 !____.

69 IMPLICIT NONE

70 INTEGER nx ,ng ,i,j,m,itmax

71 REAL ,DIMENSION (1-ng:nx+ng),INTENT(OUT):: v_new

72 REAL ,DIMENSION (1-ng:nx+ng),INTENT(IN):: v_old

73 REAL ,DIMENSION (1-ng:nx+ng):: v_it

74 REAL tol ,error ,appo ,delta_x , cfl , dt, nu

75

76 tol =1.E-10

77 m=0

78 cfl = Dt*nu/delta_x **2

79

80

81 DO j=1-ng,nx+ng

82 v_it(j)=v_old(j)

83 ENDDO

84

85 DO

86 error =0.

87 DO j=1,nx

88 v_new(j)=(v_old(j)+cfl*( v_new(j-1)+v_it(j+1)))/(1.+ cfl *2.)

89 error = error + abs(v_new(j)-v_it(j))

90 ENDDO

91 if (error/float(Nx).lt.tol) then

92 exit

93 else

94 DO j=1,nx

95 v_it(j)=v_new(j)

96 ENDDO

97 endif

98 m = m + 1

99 ENDDO

100 write (*,*) ’it=’,m,’error=’,error

101 !____.

102 RETURN

103 END SUBROUTINE gauss_seidel

104 !____.__________________________________________________________________

105 !____.__________________________________________________________________

106 SUBROUTINE exact_sol(Nx ,Ng ,x,time ,nu ,va ,vb ,v_exact)

107

38

Page 39: Programmi utilizzati durante il corso ed eserciziChapter 1 Prefazione Questo libretto e stato creato per raccogliere alcuni dei programmi che vengono mostrati durante il Laboratorio

108 IMPLICIT NONE

109 INTEGER :: Nx ,Ng ,j, MW , m

110 REAL ,DIMENSION (1-ng:nx+ng):: x,v_exact

111 REAL:: appo , va, vb, vs, appo2 , time , nu

112 REAL ,PARAMETER :: pi=acos (-1.)

113

114 MW = 10

115

116 DO j=1-ng,Nx+ng

117 vs = va + (vb - va) * x(j)

118 appo2 = 0.

119 DO m = 1, MW

120 appo = (2 * m -1) * pi

121 appo2 = appo2 + 4./ appo * sin(appo*x(j)) &

122 * exp(-nu*appo **2* time)

123 ENDDO

124

125 v_exact(j) = - appo2 + vs

126 ENDDO

127

128 RETURN

129 END SUBROUTINE exact_sol

130 !____.__________________________________________________________________

131 !____.__________________________________________________________________

132 SUBROUTINE write_disk(x,v,u,it ,nx ,ng)

133

134 IMPLICIT NONE

135 INTEGER :: it ,nx ,ng ,j

136 REAL ,DIMENSION (1-ng:nx+ng):: x,v,u

137 CHARACTER (32) opfile

138

139 opfile=’acl12345678.dat’

140 WRITE(opfile (4:11) ,1000) it

141 opfile = ’./DATA/’// opfile

142 1000 FORMAT(i8.8)

143

144 OPEN(UNIT=13,FILE=opfile ,STATUS=’unknown ’,ACTION=’write ’)

145 DO j=1-Ng,Nx+ng

146 WRITE (13,*) x(j),v(j),u(j)

147 ENDDO

148 CLOSE (13)

149

150 RETURN

151 END SUBROUTINE write_disk

152 !____.__________________________________________________________________

153 !____.__________________________________________________________________

154 SUBROUTINE griglia(Nx ,Ng ,x,delta_x)

155 IMPLICIT NONE

156 INTEGER :: Nx ,Ng ,j

157 REAL ,DIMENSION (1-ng:nx+ng):: x

158 REAL:: L, delta_x

159 L=1.

160 delta_x=L/float(Nx+1)

161

162 DO j=1-ng,Nx+ng

163 x(j)=j*delta_x

39

Page 40: Programmi utilizzati durante il corso ed eserciziChapter 1 Prefazione Questo libretto e stato creato per raccogliere alcuni dei programmi che vengono mostrati durante il Laboratorio

164 ENDDO

165

166 RETURN

167 END SUBROUTINE griglia

168 !____.__________________________________________________________________

169 !____.__________________________________________________________________

170 SUBROUTINE input(itmin ,itmax ,itout ,nu ,delta_t ,va ,vb)

171 IMPLICIT NONE

172 INTEGER :: itmin ,itmax ,itout

173 REAL :: nu,delta_t ,va,vb

174 OPEN(UNIT=1,FILE=’input.dat’,STATUS=’old’,ACTION=’read’)

175 READ (1,*) itmin

176 READ (1,*) itmax

177 READ (1,*) itout

178 READ (1,*)nu

179 READ (1,*) delta_t

180 READ (1,*)va

181 READ (1,*)vb

182 CLOSE (1)

183 RETURN

184 END SUBROUTINE input

4.6

Risolvere il seguente problema di Poisson attraverso il metodo di Jacobi/Gauss-Seidel:

d2u

dx2= −1 x ∈ [−1.1]u(−1, t) = u(1, t) = 0 (4.8)

La soluzione esatta :

u(x, t) =1

2(1− x2)

Si considerino i metodi di risoluzione del sistema algebrico lineare convergenti quando due iterate

successive presentano una differenza pari al pi a 10−10. Scrivere il numero di iterate necessarie ad

ogni metodo per la convergenza.

1 !____. file: poisson.f90

2 !____. This program applies the Jacobi/Gauss -Seidel

3 !____. iterative method to integrate

4 !____. an ordinary differential equation of second order

5 PROGRAM poisson

6 !____.__________________________________________________________________

7 IMPLICIT NONE

8 INTEGER :: i,j

9 INTEGER :: ntime ,nout

10 INTEGER ,PARAMETER :: Nx=100,Ng=1

11 !____.__________________________________________________________________

12 REAL ,DIMENSION (1-ng:Nx+ng):: v_new , v_old , v_exact , x

13 REAL:: delta_t ,delta_x ,rhs ,rhs_cent

14 REAL:: time ,cfl

15 REAL:: error ,error_max

16 !____.__________________________________________________________________

17 ! metric definition

18 call griglia(Nx,Ng,x,delta_x)

19 !____.__________________________________________________________________

40

Page 41: Programmi utilizzati durante il corso ed eserciziChapter 1 Prefazione Questo libretto e stato creato per raccogliere alcuni dei programmi che vengono mostrati durante il Laboratorio

20 ! some initializations

21 call exact_sol(Nx,Ng,x,0., v_exact)

22

23 DO i=1-Ng,Nx+Ng

24 v_old(i)=0.

25 ENDDO

26 error =0.

27 error_max = -999999.9

28 !____.__________________________________________________________________

29

30 !call jacobi(nx ,ng ,v_old ,v_new ,delta_x)

31 call gauss_seidel(nx,ng,v_old ,v_new ,delta_x)

32

33 error =0.

34 DO j=1,Nx

35 error = error + abs(v_new(j)-v_exact(j))

36 ENDDO

37 error_max=max(error_max ,error/float(Nx))

38

39 !____. write on disk

40 call write_disk(x,v_new ,v_exact ,0,Nx,Ng)

41

42 WRITE (*,*) ’dt , error=’,delta_t ,error_max

43 WRITE (*,*) ’solution stored in file velocity.dat’

44 WRITE (*,*) ’the end falks!’

45 STOP

46 !____.__________________________________________________________________

47 END

48 !____.__________________________________________________________________

49 !____.__________________________________________________________________

50 !____.__________________________________________________________________

51 !____.__________________________________________________________________

52 SUBROUTINE jacobi(nx ,ng ,v_old ,v_new ,delta_x)

53 !____.

54 IMPLICIT NONE

55 INTEGER :: nx ,ng ,i,j,m,itmax

56 REAL ,DIMENSION (1-Ng:Nx+Ng):: v_new ,v_old

57 REAL:: tol ,error ,appo ,delta_x

58

59 tol =1.E-10

60 m=0

61

62 DO

63 error =0.

64 DO j=1,nx

65 v_new(j)=(-delta_x **2+ v_old(j-1)+v_old(j+1))/2.

66 error = error + abs(v_new(j)-v_old(j))

67 ENDDO

68 do j=1-ng ,0

69 v_new(j)=0.

70 v_new(-j+nx+1)=0.

71 enddo

72 if (error/float(Nx).lt.tol) then

73 exit

74 else

75 DO j=1,nx

41

Page 42: Programmi utilizzati durante il corso ed eserciziChapter 1 Prefazione Questo libretto e stato creato per raccogliere alcuni dei programmi che vengono mostrati durante il Laboratorio

76 v_old(j)=v_new(j)

77 ENDDO

78 endif

79 m = m + 1

80 ENDDO

81 write (*,*) ’it=’,m,’error=’,error

82 !____.

83 RETURN

84 END SUBROUTINE jacobi

85 !____.__________________________________________________________________

86 !____.__________________________________________________________________

87 SUBROUTINE gauss_seidel(nx ,ng ,v_old ,v_new ,delta_x)

88 !____.

89 IMPLICIT NONE

90 INTEGER :: nx ,ng ,i,j,m,itmax

91 REAL ,DIMENSION (1-ng:nx+ng):: v_new , v_old

92 REAL:: tol ,error ,appo ,delta_x

93

94 tol =1.E-10

95 itmax =40

96 m=0

97

98

99 write (*,*) ’sono qui’

100 DO

101 error =0.

102 DO j=1,nx

103 v_new(j)=(-delta_x **2+ v_new(j-1)+v_old(j+1))/2.

104 error = error + abs(v_new(j)-v_old(j))

105 ENDDO

106 if (error.lt.tol) then

107 exit

108 else

109 DO j=1,nx

110 v_old(j)=v_new(j)

111 ENDDO

112 endif

113 m = m + 1

114 ENDDO

115 write (*,*) ’it=’,m,’error=’,error

116 !____.

117 RETURN

118 END SUBROUTINE gauss_seidel

119 !____.__________________________________________________________________

120 !____.__________________________________________________________________

121 SUBROUTINE exact_sol(Nx ,Ng ,x,time ,v_exact)

122

123 IMPLICIT NONE

124 INTEGER :: Nx ,Ny ,Ng ,i,j,WN ,m,n

125 REAL ,DIMENSION (1-ng:nx+ng)::x,v_exact

126 REAL:: appo , appo1 , appo2 , pi, time

127

128 WN=30.

129 pi = acos (-1.)

130

131 DO i=1-Ng,Nx+Ng

42

Page 43: Programmi utilizzati durante il corso ed eserciziChapter 1 Prefazione Questo libretto e stato creato per raccogliere alcuni dei programmi che vengono mostrati durante il Laboratorio

132 v_exact(i)= 0.5*(x(i)**2 -1.)

133 ENDDO

134

135 RETURN

136 ENDSUBROUTINE exact_sol

137 !____.__________________________________________________________________

138 !____.__________________________________________________________________

139 SUBROUTINE write_disk(x,v,u,it ,nx ,ng)

140

141 IMPLICIT NONE

142 INTEGER :: it ,nx ,ny ,ng ,i,j

143 REAL ,DIMENSION (1-ng:Nx+ng):: x,v,u

144 CHARACTER (32):: opfile

145

146 opfile=’acl12345678.dat’

147 WRITE(opfile (4:11) ,1000) it

148 1000 FORMAT(i8.8)

149

150 OPEN(UNIT=13,FILE=opfile ,STATUS=’unknown ’,ACTION=’write ’)

151

152 DO i=1-Ng,Nx+ng

153 WRITE (13,*) x(i),v(i),u(i)

154 ENDDO

155

156 CLOSE (13)

157

158 RETURN

159 END SUBROUTINE write_disk

160 !____.__________________________________________________________________

161 !____.__________________________________________________________________

162 SUBROUTINE griglia(Nx ,Ng ,x,delta_x)

163 IMPLICIT NONE

164 INTEGER :: Nx ,j,Ng

165 REAL ,DIMENSION (1-ng:nx+ng):: x

166 REAL:: L, delta_x

167 L=2.

168 delta_x=L/float(Nx+1)

169

170

171 DO j=1-ng,Nx+ng

172 x(j)=-1.+j*delta_x

173 ENDDO

174

175 RETURN

176 ENDSUBROUTINE griglia

43