Advertisement
Heart_Under_Blade

PR2

Mar 8th, 2022
1,677
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.       program main
  2.       implicit none
  3.       call input
  4.       call table
  5.       print *, 'The program has completed successfully'
  6.       stop
  7.       end
  8.  
  9.  
  10.       subroutine input
  11.       implicit none
  12.       integer IERR
  13.       real X_min,X_max,dx,Y_min,Y_max,dy
  14.       real temp_x, temp_y
  15.       common /arguments/ X_min,X_max,dx,Y_min,Y_max,dy
  16.       open(111,FILE='input.txt', status='old', IOSTAT=IERR, err=404)
  17.       read (111,*) X_min,X_max,dx,Y_min,Y_max,dy  
  18.       close(111)
  19. c Checking input values
  20.       if((dx .LE. 0) .OR. (dy .LE. 0)) then
  21.           write(6,*) 'Input values error:  Wrong x/y step'
  22.           stop
  23.       elseif(X_min .GT. X_max .OR. Y_min .GT. Y_max) then
  24.           write(6,*) 'Input values error: Wrong min/max arguments'
  25.           stop
  26.       endif
  27.       !Checking if step is too low
  28.       !Example: x: 0.01; step: 0.000000001; - error
  29.       temp_x = X_min + dx
  30.       temp_y = Y_min + dy
  31.       if((temp_x .EQ. X_min) .OR. (temp_y .EQ. Y_min)) then
  32.           write(6,*) 'Input values error: x/y step is too low'
  33.           stop
  34.       endif
  35.       return
  36. c Input error        
  37.   404 if(IERR .EQ. 29) then !FOR$IOS_FILNOTFOU
  38.           write(6,*) 'Error: File does not exist'
  39.       elseif(IERR .EQ. 43) then !FOR$IOS_FILNAMSPE
  40.           write(6,*) 'Error: File name specification error'
  41.       elseif(IERR .EQ. 9) then !FOR$IOS_PERACCFIL
  42.           write(6,*) 'Error: Permission to access file denied'
  43.       else
  44.           write(6,*) 'Error: Undefined error, code = ', IERR
  45.       endif
  46.       print *, 'Input file error', IERR
  47.       stop
  48.       end
  49.  
  50.  
  51.       subroutine cotg(value,eps_null,result,error_code,error_str)
  52.       implicit none
  53.       real result,value,temp_sin,temp_cos,eps_null
  54.       integer error_code
  55.       character*3 error_str
  56.       error_code = 0
  57.       error_str = ' '
  58.       temp_sin = sin(value)
  59.       if(temp_sin .EQ. 0) then
  60.           error_code = 1 !divided by zero
  61.           error_str = 'NaN'
  62.           return
  63.       elseif(abs(temp_sin) .LE. eps_null) then
  64.           error_code = -1 !infinity
  65.           error_str = 'INF'
  66.           return
  67.       endif
  68.       temp_cos = cos(value)
  69.       if(abs(temp_cos) .LE. eps_null) then
  70.         temp_cos = 0
  71.       endif
  72.       result = temp_cos/temp_sin
  73.       end
  74.  
  75.       subroutine find_steps(array,min,max,dx,n,eps,eps_null)
  76.       implicit none
  77.       character*15 str1,str2,str3
  78.       integer i,n
  79.       real array(*),eps,temp,min,max,dx,eps_null
  80.       real epsilon_power, power_temp
  81.       n = 5
  82.       temp = min
  83.       do 10 i=1,n
  84.           !Check max, eps = 10**(-3 + [lg|dx|])
  85.           if(temp .GE. max .OR. abs(temp-max) .LE. eps) then
  86.               n = i
  87.               array(n) = max
  88.               return
  89.           endif
  90.           !End check max
  91.           if(abs(temp) .LE. eps_null) temp = 0
  92.           array(i) = temp
  93.           !Skip invisible steps
  94.           write (str3,'(E13.7)') temp
  95.           write (str1,'(A6)') str3
  96.           write (str2,'(A6)') str3
  97.    20     if(str1 .EQ. str2) then
  98.               temp = temp + dx
  99.               write (str3,'(E13.7)') temp
  100.               write (str1,'(A6)') str3
  101.               goto 20
  102.           endif
  103.           !End skip invisible steps
  104.           power_temp = epsilon_power(temp,dx,eps)
  105.           temp = anint(temp*10**(-power_temp))*10**power_temp
  106.           if(abs(temp) .LE. eps_null) temp = 0
  107.    10 continue
  108.       min = temp
  109.       return
  110.       end
  111.  
  112.       real function epsilon_power(a,da,epsilon_da)
  113.       implicit none
  114.       real a, da, epsilon_da
  115.       if(abs(a) .LT. 1) then
  116.           if(abs(a) .GT. epsilon_da) then
  117.               epsilon_power = aint(log10(abs(a))) - 4
  118.           else
  119.               epsilon_power = aint(log10(abs(a-da))) - 4
  120.           endif
  121.       else
  122.           epsilon_power = aint(log10(abs(a))) - 3
  123.       endif
  124.       end
  125.  
  126.      
  127.       subroutine table
  128.       implicit none
  129.       integer IERR,error_code,n,m,i,j
  130.       real array_x(5), array_y(5)
  131.       character*3 error_string
  132.       real X_min,X_max,dx,Y_min,Y_max,dY,degreeToRadian,result
  133.       real eps_dx, eps_dy, temp, eps_null
  134.       common /arguments/ X_min,X_max,dx,Y_min,Y_max,dY
  135.       common /const/ degreeToRadian
  136.       degreeToRadian=3.1415926/180
  137.       open(112,FILE='output.txt',status='unknown',IOSTAT=IERR,err=404)
  138.       eps_null = 1e-10
  139.       eps_dx = 10**aint(log10(abs(dx)) - 3)
  140.       eps_dy = 10**aint(log10(abs(dy)) - 3)
  141.       if(eps_null .GE. eps_dx) then
  142.           eps_null = eps_dx*degreeToRadian
  143.       elseif(eps_null .GE. eps_dy) then
  144.           eps_null = eps_dy*degreeToRadian
  145.       endif
  146.    10 continue
  147.           call find_steps(array_x,X_min,X_max,dx,n,eps_dx,eps_null)
  148.           temp = Y_min
  149.    20     continue
  150.               m = 5
  151.               call find_steps(array_y,temp,Y_max,dy,m,eps_dy,eps_null)
  152.               do i = 1, n + 1
  153.                   write(112,1000) !print ---
  154.               enddo
  155.               write(112,1020)
  156.               write(112,1030) 'y/x'
  157.               write(112,1010) (array_x(i), i=1,n)
  158.               write(112,1020)
  159.               do i = 1, n + 1
  160.                   write(112,1000) !print ---
  161.               enddo
  162.               write(112,1020)
  163.               do 100 i = 1,m
  164.                   write(112,1010) array_y(i)
  165.                   do 110 j = 1,n
  166.                     call cotg((array_y(i)+array_x(j))*degreeToRadian,
  167.      *              eps_null,result, error_code, error_string)
  168.                     if(error_code .EQ. 0) then
  169.                         write(112,1010) result
  170.                     else
  171.                         write(112,1030) error_string
  172.                     endif
  173.   110             continue
  174.                   write(112,1020)
  175.   100         continue
  176.               do i = 1, n + 1
  177.                   write(112,1000) !print ---
  178.               enddo
  179.               write(112,1020)
  180.           if(array_y(m) .NE. Y_max) goto 20 !end do while
  181.       if(array_x(n) .NE. X_max) goto 10 !end do while
  182.       close(112)
  183.       return
  184. c Formats
  185.  1000 format(15('-')\)
  186.  1010 format('| 'E11.4\' |')
  187.  1020 format(/\)
  188.  1030 format('|     'A\'     |')
  189. c Output file errors
  190.   404 if(IERR .EQ. 43) then !FOR$IOS_FILNAMSPE
  191.           write(6,*) 'Output error: File specification error'
  192.       elseif(IERR .EQ. 10) then !FOR$IOS_CANOVEEXI
  193.           write(6,*) 'Output error: Cannot overwrite existing file'
  194.       elseif(IERR .EQ. 21) then !FOR$IOS_DUPFILSPE
  195.           write(6,*) 'Output error: Duplicate file specifications'
  196.       else
  197.           write(6,*) 'Output error: Undefined error, code = ', IERR
  198.       endif
  199.       print *, 'Output file error ', IERR
  200.       stop
  201.       end
  202.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement