Advertisement
EgorYankovsky

LR3

Apr 5th, 2022
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.70 KB | None | 0 0
  1. PROGRAM main
  2. COMMON /size/ N1, ial
  3. PARAMETER(mem=429496730) !Num of values (1,6GB)
  4. DIMENSION A(mem) !factual array
  5.  
  6. c ia ( 1 : N1+1 )
  7. c di ( N1+2 : 2*N1+1 )
  8. c al ( 2*N1+2 : 2*N1+1+ial )
  9. c vec( 2*N1+2+ial : 3*N1+1+ial )
  10. c res( 3*N1+2+ial : 4*N1+1+ial )
  11. c Where ial = ia(N1+1)-1 (number of al() elements)
  12.  
  13. BYTE i_read_flag=0 !=1 when the matrix has been read
  14.  
  15. CALL menu
  16. 1 READ *, icom
  17. SELECT CASE (icom)
  18. CASE (1)
  19. CALL read_sizes
  20. CALL dat_to_txt
  21. CALL input (A(1), A(N1+2), mem)
  22. i_read_flag=1
  23. PRINT *, 'Done.'
  24.  
  25. CASE (2)
  26. CALL read_sizes
  27. c CALL txt_to_dat
  28. CALL direct_input (A(1), A(N1+2), mem)
  29. i_read_flag=1
  30. PRINT *, 'Done.'
  31.  
  32. CASE (3)
  33. IF(i_read_flag .NE. 1) GOTO 3
  34. CALL multiply (A(1), A(N1+2), A(2*N1+2),
  35. + A(2*N1+2+ial), A(3*N1+2+ial))
  36. CALL output(A(3*N1+ial+2))
  37. PRINT *, 'Done.'
  38.  
  39. CASE (4)
  40. IF(i_read_flag .NE. 1) GOTO 3
  41. CALL full_matrix_out(A(1), A(N1+2), A(2*N1+2))
  42. PRINT *, 'Done.'
  43.  
  44. CASE (5)
  45. CALL generate_matrix
  46. PRINT *, 'Done.'
  47.  
  48. CASE (6)
  49. GOTO 2
  50.  
  51. CASE DEFAULT
  52. PRINT *, 'Unexpected command.'
  53. PRINT *, 'Done.'
  54. END SELECT
  55.  
  56. GOTO 1
  57. 3 PRINT *, 'Input the matrix first.'
  58. GOTO 1
  59. 2 END
  60.  
  61. SUBROUTINE menu
  62. PRINT *, '1. Read matrix from .txt.'
  63. PRINT *, '2. Read matrix from direct access files.'
  64. PRINT *, '3. Multyply matrix by vector.'
  65. PRINT *, '4. Output of the entered matrix in dense format.'
  66. PRINT *, '5. Generate new matrix (into direct access files).'
  67. PRINT *, '6. Exit.'
  68. END
  69.  
  70. c------------------ Reading matrix and vector dimensions --------------------
  71. SUBROUTINE read_sizes
  72. COMMON /size/ N1, ial
  73. OPEN (1, FILE='N1N2.txt', ERR=1)
  74.  
  75. c N1 - matrix size, N2 - vector size
  76. READ (1, *) N1, N2
  77. IF (N1 .ne. N2 .OR. N1 .EQ. 0) STOP 'Matrix N1 != Vector N2.'
  78. IF (N1 .EQ. 0) STOP 'Matrix size = 0.'
  79.  
  80. CLOSE(1)
  81. GOTO 2
  82. 1 STOP 'Input error: check the input files.'
  83. 2 END
  84.  
  85. c--------------------- Input data from .txt files ---------------------------
  86. SUBROUTINE input(ia, A, mem)
  87. COMMON /size/ N1, ial
  88. c formal arrays:
  89. DIMENSION ia(*)
  90. DIMENSION A(*) ! (di, ia, al, vec)
  91. OPEN (1, FILE='ia.txt', ERR=1)
  92. OPEN (2, FILE='di.txt', ERR=1)
  93. OPEN (3, FILE='al.txt', ERR=1)
  94. OPEN (4, FILE='vec.txt', ERR=1)
  95.  
  96. READ (1, *) (ia(i), i=1, N1+1) !ia(N1+1)
  97. ial=ia(N1+1)-1 !num of al() elements
  98. IF(5*N1+ial+1 .GT. mem) STOP 'Not enough memory.'
  99. READ (2, *) (A(i), i=1, N1) !di(N1)
  100. READ (3, *) (A(i), i=N1+1, N1+ial) !al(ia(N1+1)-1)
  101. READ (4, *) (A(i), i=N1+ial+1, 2*N1+ial) !vec(N1)
  102.  
  103. CLOSE(1)
  104. CLOSE(2)
  105. CLOSE(3)
  106. CLOSE(4)
  107. GOTO 2
  108. 1 STOP 'Input error: check the input files.'
  109. 2 END
  110.  
  111. c------------------ Input data from direct access files ---------------------
  112. SUBROUTINE direct_input(ia, A, mem)
  113. COMMON /size/ N1, ial
  114.  
  115. c A( di, ia, al, vec )
  116. DIMENSION ia(*)
  117. DIMENSION A(*) !formal arrays
  118.  
  119. OPEN (1, FILE='ia.dat', ACCESS='DIRECT', RECL=4, ERR=1)
  120. OPEN (2, FILE='di.dat', ACCESS='DIRECT', RECL=4, ERR=1)
  121. OPEN (3, FILE='al.dat', ACCESS='DIRECT', RECL=4, ERR=1)
  122. OPEN (4, FILE='vec.dat', ACCESS='DIRECT', RECL=4, ERR=1)
  123.  
  124. DO i=1, N1+1
  125. READ(1, REC=i) ia(i)
  126. ENDDO
  127. ial=ia(N1+1)-1 !num of al elements
  128. IF(5*N1+ial+1 .GT. mem) STOP 'Not enough memory.'
  129.  
  130. DO i=1, N1
  131. READ(2, REC=i) A(i) !di(N1)
  132. ENDDO
  133.  
  134. DO i=1, ial
  135. READ(3, REC=i) A(N1+i) !al(ia(N1+1)-1)
  136. ENDDO
  137.  
  138. DO i=1, N1
  139. READ(4, REC=i) A(N1+ial+i) !vec(N1)
  140. ENDDO
  141.  
  142. CLOSE(1)
  143. CLOSE(2)
  144. CLOSE(3)
  145. CLOSE(4)
  146. GOTO 2
  147. 1 STOP 'Input error: check the input files.'
  148. 2 END
  149.  
  150. c--------------- Multiply matrix (profile format) on vector -----------------
  151. SUBROUTINE multiply (ia, di, al, vec, res)
  152. COMMON /size/ N1, ial
  153. DIMENSION ia(*), di(*), al(*), vec(*), res(*)
  154.  
  155. DO i=1, N1
  156. DO j=ia(i), ia(i+1)-1
  157. ind=i+j-ia(i+1)
  158. res(i) = res(i) + al(j) * vec(ind)
  159. res(ind) = res(ind) + al(j) * vec(i)
  160. ENDDO
  161. res(i) = res(i) + di(i) * vec(i)
  162. ENDDO
  163. END
  164.  
  165. c-------------------------- Output vector - result --------------------------
  166. SUBROUTINE output(res)
  167. COMMON /size/ N1, ial
  168. DIMENSION res(*)
  169. OPEN(1, FILE='Result.txt', ERR=1)
  170.  
  171. WRITE(1, *)'result = ( '
  172. DO i=1, N1
  173. WRITE(1, 10) res(i)
  174. ENDDO
  175. WRITE(1, *)')'
  176.  
  177. CLOSE(1)
  178. 10 FORMAT(F10.2)
  179. GOTO 2
  180. 1 STOP 'Output error: check the output file.'
  181. 2 END
  182.  
  183. c---------------- Full matrix output (99 * 99 max size) -------------------
  184. SUBROUTINE full_matrix_out(ia, di, al)
  185. COMMON /size/ N1, ial
  186.  
  187. PARAMETER(Nmax=99)
  188. DIMENSION Fm(Nmax, Nmax)
  189. DIMENSION ia(*), di(*), al(*)
  190.  
  191. DO i=1, N1
  192. DO j = ia(i), ia(i+1)-1
  193. ind = i+j-ia(i+1)
  194. Fm(i, ind) = al(j)
  195. Fm(ind, i) = al(j)
  196. ENDDO
  197. Fm(i, i) = di(i)
  198. ENDDO
  199.  
  200. OPEN (1, FILE='full_matrix.txt', ERR=1)
  201. DO i=1, N1
  202. DO j=1, N1
  203. WRITE(1, 10) Fm(i, j)
  204. ENDDO
  205. WRITE (1, 11)
  206. ENDDO
  207.  
  208. CLOSE(1)
  209. GOTO 2
  210. 1 STOP 'Error: failed to create the output file.'
  211. c 10 FORMAT (F5.2\)
  212. 10 FORMAT(F5.2)
  213. 11 FORMAT (1x)
  214. 2 END
  215.  
  216. c------------- Generate new matrix (into direct access files) ---------------
  217. SUBROUTINE generate_matrix
  218.  
  219. PRINT *, 'Enter size:'
  220. PRINT *, '(Or enter 0 to read size of matrix from file.)'
  221. READ *, m_size
  222. OPEN(1, FILE = 'N1N2.txt', ERR=1)
  223. OPEN(2, FILE = 'vec.dat', ACCESS='DIRECT', RECL=4, ERR=2)
  224. OPEN(3, FILE = 'di.dat', ACCESS='DIRECT', RECL=4, ERR=2)
  225. OPEN(4, FILE = 'ia.dat', ACCESS='DIRECT', RECL=4, ERR=2)
  226. OPEN(5, FILE = 'al.dat', ACCESS='DIRECT', RECL=4, ERR=2)
  227.  
  228. IF(m_size .GT. 0) GOTO 5
  229. READ(1, *) m_size
  230. 5 WRITE(1, 10) m_size, m_size
  231.  
  232. DO i = 1, m_size
  233. WRITE(2, REC=i) i/2.0
  234. WRITE(3, REC=i) i/10
  235. ENDDO
  236.  
  237. WRITE(4, REC=1) 1
  238. it = 1
  239. ik = 1
  240.  
  241. DO WHILE (ik .LT. m_size)
  242. IF (MOD(it, 3) .EQ. 0) GOTO 20
  243. WRITE(4, REC=ik) it
  244. ik = ik + 1
  245. 20 it = it + 1
  246. ENDDO
  247. WRITE(4, REC=ik+1) it
  248.  
  249. DO i=1, it
  250. WRITE(5, REC=i) i/10
  251. ENDDO
  252.  
  253. CLOSE(1)
  254. CLOSE(2)
  255. CLOSE(3)
  256. CLOSE(4)
  257. CLOSE(5)
  258. GOTO 3
  259. 10 FORMAT(I2)
  260. 1 STOP 'Error: something went wrong with N1N2.txt'
  261. 2 STOP 'Error: something went wrong with input files.'
  262. 3 END
  263.  
  264. c-------------- Converting .txt files to direct access files ----------------
  265. SUBROUTINE txt_to_dat
  266. COMMON /size/ N1, ial
  267. PARAMETER(Nmax=999)
  268. DIMENSION ia(Nmax+1), di(Nmax), al(Nmax*Nmax), vec(Nmax)
  269.  
  270. OPEN (1, FILE='ia.txt', ERR=1)
  271. OPEN (2, FILE='di.txt', ERR=1)
  272. OPEN (3, FILE='al.txt', ERR=1)
  273. OPEN (4, FILE='vec.txt', ERR=1)
  274.  
  275. OPEN (5, FILE='ia.dat', ACCESS='DIRECT', RECL=4, ERR=2)
  276. OPEN (6, FILE='di.dat', ACCESS='DIRECT', RECL=4, ERR=2)
  277. OPEN (7, FILE='al.dat', ACCESS='DIRECT', RECL=4, ERR=2)
  278. OPEN (8, FILE='vec.dat', ACCESS='DIRECT', RECL=4, ERR=2)
  279.  
  280. READ (1, *) (ia(i), i=1, N1+1)
  281. DO i=1, N1+1
  282. WRITE (5, REC=i) ia(i)
  283. ENDDO
  284. ial = ia(N1+1)-1
  285.  
  286. READ (2, *) (di(i), i=1, N1)
  287. DO i=1, N1
  288. WRITE (6, REC=i) di(i)
  289. ENDDO
  290.  
  291. READ (3, *) (al(i), i=1, ial)
  292. DO i=1, ial
  293. WRITE (7, REC=i) al(i)
  294. ENDDO
  295.  
  296. READ (4, *) (vec(i), i=1, N1)
  297. DO i=1, N1
  298. WRITE (8, REC=i) vec(i)
  299. ENDDO
  300.  
  301. CLOSE(1)
  302. CLOSE(2)
  303. CLOSE(3)
  304. CLOSE(4)
  305. CLOSE(5)
  306. CLOSE(6)
  307. CLOSE(7)
  308. CLOSE(8)
  309. GOTO 3
  310. 1 STOP 'Input error: check the input files.'
  311. 2 STOP 'Output error: failed to create direct access file.'
  312. 3 END
  313.  
  314. c-------------- Converting direct access files to .txt files ----------------
  315. SUBROUTINE dat_to_txt
  316. COMMON /size/ N1, ial
  317. PARAMETER(Nmax=999)
  318. DIMENSION ia(Nmax+1), di(Nmax), al(Nmax*Nmax), vec(Nmax)
  319.  
  320. OPEN (1, FILE='ia.txt', ERR=1)
  321. OPEN (2, FILE='di.txt', ERR=1)
  322. OPEN (3, FILE='al.txt', ERR=1)
  323. OPEN (4, FILE='vec.txt', ERR=1)
  324.  
  325. OPEN (5, FILE='ia.dat', ACCESS='DIRECT', RECL=4, ERR=2)
  326. OPEN (6, FILE='di.dat', ACCESS='DIRECT', RECL=4, ERR=2)
  327. OPEN (7, FILE='al.dat', ACCESS='DIRECT', RECL=4, ERR=2)
  328. OPEN (8, FILE='vec.dat', ACCESS='DIRECT', RECL=4, ERR=2)
  329.  
  330. DO i=1, N1+1
  331. READ (5, REC = i) ia(i)
  332. WRITE (1, *) ia(i)
  333. ENDDO
  334. ial = ia(N1+1)-1
  335.  
  336. DO i=1, N1
  337. READ (6, REC = i) di(i)
  338. WRITE (2, *) di(i)
  339. ENDDO
  340.  
  341. DO i=1, ial
  342. READ (7, REC = i) al(i)
  343. WRITE (3, *) al(i)
  344. ENDDO
  345.  
  346. DO i=1, N1
  347. READ (8, REC = i) vec(i)
  348. WRITE (4, *) vec(i)
  349. ENDDO
  350.  
  351. CLOSE(1)
  352. CLOSE(2)
  353. CLOSE(3)
  354. CLOSE(4)
  355. CLOSE(5)
  356. CLOSE(6)
  357. CLOSE(7)
  358. CLOSE(8)
  359. GOTO 3
  360. 1 STOP 'Input error: check the input files.'
  361. 2 STOP 'Output error: failed to create direct access file.'
  362. 3 END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement