Search code examples
fortranfortran90

Allocated table variation


 PROGRAM satellite
  IMPLICIT NONE
  INTEGER                                  :: i, j, ok, nc
  REAL                                     :: alph, bet, chi, ninf1, C1
  REAL, DIMENSION(:), ALLOCATABLE          :: uexact, x, Econs
  REAL                                     :: E, k, Lc, hc, eps, h

  Read*,nc
  E=25. ; k=125. ; hc=0.01 ; eps=0.01 ; Lc=1 ;

  h = Lc/nc ; chi=sqrt((E*hc)/k) ; alph= -(1/h**2) ; bet=(2/h**2)+(k/(E*hc))

  ALLOCATE(x(0:nc), uexact(0:nc), Econs(0:nc))

  OPEN(UNIT=888,FILE="uetuexact.out",ACTION="write",STATUS='old')
  OPEN(UNIT=889,FILE="consistance.out",ACTION="write",STATUS='old')


  DO i = 0, nc
   x(i) = h*i-Lc/2
   uexact(i) = eps*chi*((sinh(x(i)/chi))/(cosh(Lc/(2*chi))))
 END DO
 !-------------------------------------------------------------------------------
 DO i=1,nc-1
   Econs(i)=(alph*(uexact(i-1)))+(bet*(uexact(i)))+(alph*(uexact(i+1)))
 END DO

 ninf1=maxval(Econs)
 C1=ninf1*(nc**2)

DO i = 0, nc
  WRITE(888,fmt='(3E15.6)') x(i), uexact(i)
  WRITE(889,fmt='(3E15.6)') x(i), uexact(i), -Econs(i)
END DO

Print* , 'nc :', nc
Print* , 'h :', h
Print* , 'ninf1 :', ninf1
Print* , 'C1 :', C1

END PROGRAM satellite

I need my nc variable to change from 10,50,100,500,1000,5000,10000 in order to write out for each given nc, the ninf1 and C1 values. For now, i was doing nc manually but i will need a file .out : that gives me nc | ninf1 | C1. I want to know how can i vary my nc to this values precisely.


Solution

  • You could do the following:

    • define an array nsizes that would hold all the values you want nc to take.
    • declare a variable iter that would run along this array
    • iterate with iter over the length of nsizes
    • At the beginning of each iteration assign nc = nsizes(iter)
    • At the end of each iteration deallocate the arrays

    Here is a patch that does just that.

    --- satellite.f90   2020-02-16 18:13:35.662123215 +0700
    +++ satellite_loop.f90  2020-02-16 18:50:09.662029872 +0700
    @@ -1,11 +1,15 @@
     PROGRAM satellite
       IMPLICIT NONE
    -  INTEGER                                  :: i, j, ok, nc
    +  INTEGER                                  :: i, j, ok, nc, iter
       REAL                                     :: alph, bet, chi, ninf1, C1
       REAL, DIMENSION(:), ALLOCATABLE          :: uexact, x, Econs
       REAL                                     :: E, k, Lc, hc, eps, h
    +  INTEGER, DIMENSION(7)                    :: nsizes = (/ 10, 50, 100, 500, 1000, 5000, 10000/)
    +
    + !  Read*,nc
    + do  iter = 1, 7
    +  nc = nsizes(iter)
    
    -  Read*,nc
       E=25. ; k=125. ; hc=0.01 ; eps=0.01 ; Lc=1 ;
    
       h = Lc/nc ; chi=sqrt((E*hc)/k) ; alph= -(1/h**2) ; bet=(2/h**2)+(k/(E*hc))
    @@ -38,4 +42,8 @@
     Print* , 'ninf1 :', ninf1
     Print* , 'C1 :', C1
    
    +deallocate(x, uexact, econs)
    +
    +end do
    +
     END PROGRAM satellite
    

    The output would look like this:

     nc :          10
     h :  0.100000001    
     ninf1 :   1.17741162E-02
     C1 :   1.17741168    
     nc :          50
     h :   1.99999996E-02
     ninf1 :   2.39971280E-03
     C1 :   5.99928188    
     nc :         100
     h :   9.99999978E-03
     ninf1 :   7.46726990E-04
     C1 :   7.46726990    
     nc :         500
     h :   2.00000009E-03
     ninf1 :   1.44958496E-04
     C1 :   36.2396240    
     nc :        1000
     h :   1.00000005E-03
     ninf1 :   8.23974609E-04
     C1 :   823.974609    
     nc :        5000
     h :   1.99999995E-04
     ninf1 :   2.73437500E-02
     C1 :   683593.750    
     nc :       10000
     h :   9.99999975E-05
     ninf1 :  0.125000000    
     C1 :   12500000.0