You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

sgedmd.f90 50 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206
  1. !> \brief \b SGEDMD computes the Dynamic Mode Decomposition (DMD) for a pair of data snapshot matrices.
  2. !
  3. ! =========== DOCUMENTATION ===========
  4. !
  5. ! Definition:
  6. ! ===========
  7. !
  8. ! SUBROUTINE SGEDMD( JOBS, JOBZ, JOBR, JOBF, WHTSVD, &
  9. ! M, N, X, LDX, Y, LDY, NRNK, TOL, &
  10. ! K, REIG, IMEIG, Z, LDZ, RES, &
  11. ! B, LDB, W, LDW, S, LDS, &
  12. ! WORK, LWORK, IWORK, LIWORK, INFO )
  13. !.....
  14. ! USE iso_fortran_env
  15. ! IMPLICIT NONE
  16. ! INTEGER, PARAMETER :: WP = real32
  17. !.....
  18. ! Scalar arguments
  19. ! CHARACTER, INTENT(IN) :: JOBS, JOBZ, JOBR, JOBF
  20. ! INTEGER, INTENT(IN) :: WHTSVD, M, N, LDX, LDY, &
  21. ! NRNK, LDZ, LDB, LDW, LDS, &
  22. ! LWORK, LIWORK
  23. ! INTEGER, INTENT(OUT) :: K, INFO
  24. ! REAL(KIND=WP), INTENT(IN) :: TOL
  25. ! Array arguments
  26. ! REAL(KIND=WP), INTENT(INOUT) :: X(LDX,*), Y(LDY,*)
  27. ! REAL(KIND=WP), INTENT(OUT) :: Z(LDZ,*), B(LDB,*), &
  28. ! W(LDW,*), S(LDS,*)
  29. ! REAL(KIND=WP), INTENT(OUT) :: REIG(*), IMEIG(*), &
  30. ! RES(*)
  31. ! REAL(KIND=WP), INTENT(OUT) :: WORK(*)
  32. ! INTEGER, INTENT(OUT) :: IWORK(*)
  33. !
  34. !............................................................
  35. !> \par Purpose:
  36. ! =============
  37. !> \verbatim
  38. !> SGEDMD computes the Dynamic Mode Decomposition (DMD) for
  39. !> a pair of data snapshot matrices. For the input matrices
  40. !> X and Y such that Y = A*X with an unaccessible matrix
  41. !> A, SGEDMD computes a certain number of Ritz pairs of A using
  42. !> the standard Rayleigh-Ritz extraction from a subspace of
  43. !> range(X) that is determined using the leading left singular
  44. !> vectors of X. Optionally, SGEDMD returns the residuals
  45. !> of the computed Ritz pairs, the information needed for
  46. !> a refinement of the Ritz vectors, or the eigenvectors of
  47. !> the Exact DMD.
  48. !> For further details see the references listed
  49. !> below. For more details of the implementation see [3].
  50. !> \endverbatim
  51. !............................................................
  52. !> \par References:
  53. ! ================
  54. !> \verbatim
  55. !> [1] P. Schmid: Dynamic mode decomposition of numerical
  56. !> and experimental data,
  57. !> Journal of Fluid Mechanics 656, 5-28, 2010.
  58. !> [2] Z. Drmac, I. Mezic, R. Mohr: Data driven modal
  59. !> decompositions: analysis and enhancements,
  60. !> SIAM J. on Sci. Comp. 40 (4), A2253-A2285, 2018.
  61. !> [3] Z. Drmac: A LAPACK implementation of the Dynamic
  62. !> Mode Decomposition I. Technical report. AIMDyn Inc.
  63. !> and LAPACK Working Note 298.
  64. !> [4] J. Tu, C. W. Rowley, D. M. Luchtenburg, S. L.
  65. !> Brunton, N. Kutz: On Dynamic Mode Decomposition:
  66. !> Theory and Applications, Journal of Computational
  67. !> Dynamics 1(2), 391 -421, 2014.
  68. !> \endverbatim
  69. !......................................................................
  70. !> \par Developed and supported by:
  71. ! ================================
  72. !> \verbatim
  73. !> Developed and coded by Zlatko Drmac, Faculty of Science,
  74. !> University of Zagreb; drmac@math.hr
  75. !> In cooperation with
  76. !> AIMdyn Inc., Santa Barbara, CA.
  77. !> and supported by
  78. !> - DARPA SBIR project "Koopman Operator-Based Forecasting
  79. !> for Nonstationary Processes from Near-Term, Limited
  80. !> Observational Data" Contract No: W31P4Q-21-C-0007
  81. !> - DARPA PAI project "Physics-Informed Machine Learning
  82. !> Methodologies" Contract No: HR0011-18-9-0033
  83. !> - DARPA MoDyL project "A Data-Driven, Operator-Theoretic
  84. !> Framework for Space-Time Analysis of Process Dynamics"
  85. !> Contract No: HR0011-16-C-0116
  86. !> Any opinions, findings and conclusions or recommendations
  87. !> expressed in this material are those of the author and
  88. !> do not necessarily reflect the views of the DARPA SBIR
  89. !> Program Office
  90. !> \endverbatim
  91. !......................................................................
  92. !> \par Distribution Statement A:
  93. ! ==============================
  94. !> \verbatim
  95. !> Distribution Statement A:
  96. !> Approved for Public Release, Distribution Unlimited.
  97. !> Cleared by DARPA on September 29, 2022
  98. !> \endverbatim
  99. !============================================================
  100. ! Arguments
  101. ! =========
  102. !
  103. !> \param[in] JOBS
  104. !> \verbatim
  105. !> JOBS (input) CHARACTER*1
  106. !> Determines whether the initial data snapshots are scaled
  107. !> by a diagonal matrix.
  108. !> 'S' :: The data snapshots matrices X and Y are multiplied
  109. !> with a diagonal matrix D so that X*D has unit
  110. !> nonzero columns (in the Euclidean 2-norm)
  111. !> 'C' :: The snapshots are scaled as with the 'S' option.
  112. !> If it is found that an i-th column of X is zero
  113. !> vector and the corresponding i-th column of Y is
  114. !> non-zero, then the i-th column of Y is set to
  115. !> zero and a warning flag is raised.
  116. !> 'Y' :: The data snapshots matrices X and Y are multiplied
  117. !> by a diagonal matrix D so that Y*D has unit
  118. !> nonzero columns (in the Euclidean 2-norm)
  119. !> 'N' :: No data scaling.
  120. !> \endverbatim
  121. !.....
  122. !> \param[in] JOBZ
  123. !> \verbatim
  124. !> JOBZ (input) CHARACTER*1
  125. !> Determines whether the eigenvectors (Koopman modes) will
  126. !> be computed.
  127. !> 'V' :: The eigenvectors (Koopman modes) will be computed
  128. !> and returned in the matrix Z.
  129. !> See the description of Z.
  130. !> 'F' :: The eigenvectors (Koopman modes) will be returned
  131. !> in factored form as the product X(:,1:K)*W, where X
  132. !> contains a POD basis (leading left singular vectors
  133. !> of the data matrix X) and W contains the eigenvectors
  134. !> of the corresponding Rayleigh quotient.
  135. !> See the descriptions of K, X, W, Z.
  136. !> 'N' :: The eigenvectors are not computed.
  137. !> \endverbatim
  138. !.....
  139. !> \param[in] JOBR
  140. !> \verbatim
  141. !> JOBR (input) CHARACTER*1
  142. !> Determines whether to compute the residuals.
  143. !> 'R' :: The residuals for the computed eigenpairs will be
  144. !> computed and stored in the array RES.
  145. !> See the description of RES.
  146. !> For this option to be legal, JOBZ must be 'V'.
  147. !> 'N' :: The residuals are not computed.
  148. !> \endverbatim
  149. !.....
  150. !> \param[in] JOBF
  151. !> \verbatim
  152. !> JOBF (input) CHARACTER*1
  153. !> Specifies whether to store information needed for post-
  154. !> processing (e.g. computing refined Ritz vectors)
  155. !> 'R' :: The matrix needed for the refinement of the Ritz
  156. !> vectors is computed and stored in the array B.
  157. !> See the description of B.
  158. !> 'E' :: The unscaled eigenvectors of the Exact DMD are
  159. !> computed and returned in the array B. See the
  160. !> description of B.
  161. !> 'N' :: No eigenvector refinement data is computed.
  162. !> \endverbatim
  163. !.....
  164. !> \param[in] WHTSVD
  165. !> \verbatim
  166. !> WHTSVD (input) INTEGER, WHSTVD in { 1, 2, 3, 4 }
  167. !> Allows for a selection of the SVD algorithm from the
  168. !> LAPACK library.
  169. !> 1 :: SGESVD (the QR SVD algorithm)
  170. !> 2 :: SGESDD (the Divide and Conquer algorithm; if enough
  171. !> workspace available, this is the fastest option)
  172. !> 3 :: SGESVDQ (the preconditioned QR SVD ; this and 4
  173. !> are the most accurate options)
  174. !> 4 :: SGEJSV (the preconditioned Jacobi SVD; this and 3
  175. !> are the most accurate options)
  176. !> For the four methods above, a significant difference in
  177. !> the accuracy of small singular values is possible if
  178. !> the snapshots vary in norm so that X is severely
  179. !> ill-conditioned. If small (smaller than EPS*||X||)
  180. !> singular values are of interest and JOBS=='N', then
  181. !> the options (3, 4) give the most accurate results, where
  182. !> the option 4 is slightly better and with stronger
  183. !> theoretical background.
  184. !> If JOBS=='S', i.e. the columns of X will be normalized,
  185. !> then all methods give nearly equally accurate results.
  186. !> \endverbatim
  187. !.....
  188. !> \param[in] M
  189. !> \verbatim
  190. !> M (input) INTEGER, M>= 0
  191. !> The state space dimension (the row dimension of X, Y).
  192. !> \endverbatim
  193. !.....
  194. !> \param[in] N
  195. !> \verbatim
  196. !> N (input) INTEGER, 0 <= N <= M
  197. !> The number of data snapshot pairs
  198. !> (the number of columns of X and Y).
  199. !> \endverbatim
  200. !.....
  201. !> \param[in,out] X
  202. !> \verbatim
  203. !> X (input/output) REAL(KIND=WP) M-by-N array
  204. !> > On entry, X contains the data snapshot matrix X. It is
  205. !> assumed that the column norms of X are in the range of
  206. !> the normalized floating point numbers.
  207. !> < On exit, the leading K columns of X contain a POD basis,
  208. !> i.e. the leading K left singular vectors of the input
  209. !> data matrix X, U(:,1:K). All N columns of X contain all
  210. !> left singular vectors of the input matrix X.
  211. !> See the descriptions of K, Z and W.
  212. !> \endverbatim
  213. !.....
  214. !> \param[in] LDX
  215. !> \verbatim
  216. !> LDX (input) INTEGER, LDX >= M
  217. !> The leading dimension of the array X.
  218. !> \endverbatim
  219. !.....
  220. !> \param[in,out] Y
  221. !> \verbatim
  222. !> Y (input/workspace/output) REAL(KIND=WP) M-by-N array
  223. !> > On entry, Y contains the data snapshot matrix Y
  224. !> < On exit,
  225. !> If JOBR == 'R', the leading K columns of Y contain
  226. !> the residual vectors for the computed Ritz pairs.
  227. !> See the description of RES.
  228. !> If JOBR == 'N', Y contains the original input data,
  229. !> scaled according to the value of JOBS.
  230. !> \endverbatim
  231. !.....
  232. !> \param[in] LDY
  233. !> \verbatim
  234. !> LDY (input) INTEGER , LDY >= M
  235. !> The leading dimension of the array Y.
  236. !> \endverbatim
  237. !.....
  238. !> \param[in] NRNK
  239. !> \verbatim
  240. !> NRNK (input) INTEGER
  241. !> Determines the mode how to compute the numerical rank,
  242. !> i.e. how to truncate small singular values of the input
  243. !> matrix X. On input, if
  244. !> NRNK = -1 :: i-th singular value sigma(i) is truncated
  245. !> if sigma(i) <= TOL*sigma(1)
  246. !> This option is recommended.
  247. !> NRNK = -2 :: i-th singular value sigma(i) is truncated
  248. !> if sigma(i) <= TOL*sigma(i-1)
  249. !> This option is included for R&D purposes.
  250. !> It requires highly accurate SVD, which
  251. !> may not be feasible.
  252. !> The numerical rank can be enforced by using positive
  253. !> value of NRNK as follows:
  254. !> 0 < NRNK <= N :: at most NRNK largest singular values
  255. !> will be used. If the number of the computed nonzero
  256. !> singular values is less than NRNK, then only those
  257. !> nonzero values will be used and the actually used
  258. !> dimension is less than NRNK. The actual number of
  259. !> the nonzero singular values is returned in the variable
  260. !> K. See the descriptions of TOL and K.
  261. !> \endverbatim
  262. !.....
  263. !> \param[in] TOL
  264. !> \verbatim
  265. !> TOL (input) REAL(KIND=WP), 0 <= TOL < 1
  266. !> The tolerance for truncating small singular values.
  267. !> See the description of NRNK.
  268. !> \endverbatim
  269. !.....
  270. !> \param[out] K
  271. !> \verbatim
  272. !> K (output) INTEGER, 0 <= K <= N
  273. !> The dimension of the POD basis for the data snapshot
  274. !> matrix X and the number of the computed Ritz pairs.
  275. !> The value of K is determined according to the rule set
  276. !> by the parameters NRNK and TOL.
  277. !> See the descriptions of NRNK and TOL.
  278. !> \endverbatim
  279. !.....
  280. !> \param[out] REIG
  281. !> \verbatim
  282. !> REIG (output) REAL(KIND=WP) N-by-1 array
  283. !> The leading K (K<=N) entries of REIG contain
  284. !> the real parts of the computed eigenvalues
  285. !> REIG(1:K) + sqrt(-1)*IMEIG(1:K).
  286. !> See the descriptions of K, IMEIG, and Z.
  287. !> \endverbatim
  288. !.....
  289. !> \param[out] IMEIG
  290. !> \verbatim
  291. !> IMEIG (output) REAL(KIND=WP) N-by-1 array
  292. !> The leading K (K<=N) entries of IMEIG contain
  293. !> the imaginary parts of the computed eigenvalues
  294. !> REIG(1:K) + sqrt(-1)*IMEIG(1:K).
  295. !> The eigenvalues are determined as follows:
  296. !> If IMEIG(i) == 0, then the corresponding eigenvalue is
  297. !> real, LAMBDA(i) = REIG(i).
  298. !> If IMEIG(i)>0, then the corresponding complex
  299. !> conjugate pair of eigenvalues reads
  300. !> LAMBDA(i) = REIG(i) + sqrt(-1)*IMAG(i)
  301. !> LAMBDA(i+1) = REIG(i) - sqrt(-1)*IMAG(i)
  302. !> That is, complex conjugate pairs have consecutive
  303. !> indices (i,i+1), with the positive imaginary part
  304. !> listed first.
  305. !> See the descriptions of K, REIG, and Z.
  306. !> \endverbatim
  307. !.....
  308. !> \param[out] Z
  309. !> \verbatim
  310. !> Z (workspace/output) REAL(KIND=WP) M-by-N array
  311. !> If JOBZ =='V' then
  312. !> Z contains real Ritz vectors as follows:
  313. !> If IMEIG(i)=0, then Z(:,i) is an eigenvector of
  314. !> the i-th Ritz value; ||Z(:,i)||_2=1.
  315. !> If IMEIG(i) > 0 (and IMEIG(i+1) < 0) then
  316. !> [Z(:,i) Z(:,i+1)] span an invariant subspace and
  317. !> the Ritz values extracted from this subspace are
  318. !> REIG(i) + sqrt(-1)*IMEIG(i) and
  319. !> REIG(i) - sqrt(-1)*IMEIG(i).
  320. !> The corresponding eigenvectors are
  321. !> Z(:,i) + sqrt(-1)*Z(:,i+1) and
  322. !> Z(:,i) - sqrt(-1)*Z(:,i+1), respectively.
  323. !> || Z(:,i:i+1)||_F = 1.
  324. !> If JOBZ == 'F', then the above descriptions hold for
  325. !> the columns of X(:,1:K)*W(1:K,1:K), where the columns
  326. !> of W(1:k,1:K) are the computed eigenvectors of the
  327. !> K-by-K Rayleigh quotient. The columns of W(1:K,1:K)
  328. !> are similarly structured: If IMEIG(i) == 0 then
  329. !> X(:,1:K)*W(:,i) is an eigenvector, and if IMEIG(i)>0
  330. !> then X(:,1:K)*W(:,i)+sqrt(-1)*X(:,1:K)*W(:,i+1) and
  331. !> X(:,1:K)*W(:,i)-sqrt(-1)*X(:,1:K)*W(:,i+1)
  332. !> are the eigenvectors of LAMBDA(i), LAMBDA(i+1).
  333. !> See the descriptions of REIG, IMEIG, X and W.
  334. !> \endverbatim
  335. !.....
  336. !> \param[in] LDZ
  337. !> \verbatim
  338. !> LDZ (input) INTEGER , LDZ >= M
  339. !> The leading dimension of the array Z.
  340. !> \endverbatim
  341. !.....
  342. !> \param[out] RES
  343. !> \verbatim
  344. !> RES (output) REAL(KIND=WP) N-by-1 array
  345. !> RES(1:K) contains the residuals for the K computed
  346. !> Ritz pairs.
  347. !> If LAMBDA(i) is real, then
  348. !> RES(i) = || A * Z(:,i) - LAMBDA(i)*Z(:,i))||_2.
  349. !> If [LAMBDA(i), LAMBDA(i+1)] is a complex conjugate pair
  350. !> then
  351. !> RES(i)=RES(i+1) = || A * Z(:,i:i+1) - Z(:,i:i+1) *B||_F
  352. !> where B = [ real(LAMBDA(i)) imag(LAMBDA(i)) ]
  353. !> [-imag(LAMBDA(i)) real(LAMBDA(i)) ].
  354. !> It holds that
  355. !> RES(i) = || A*ZC(:,i) - LAMBDA(i) *ZC(:,i) ||_2
  356. !> RES(i+1) = || A*ZC(:,i+1) - LAMBDA(i+1)*ZC(:,i+1) ||_2
  357. !> where ZC(:,i) = Z(:,i) + sqrt(-1)*Z(:,i+1)
  358. !> ZC(:,i+1) = Z(:,i) - sqrt(-1)*Z(:,i+1)
  359. !> See the description of REIG, IMEIG and Z.
  360. !> \endverbatim
  361. !.....
  362. !> \param[out] B
  363. !> \verbatim
  364. !> B (output) REAL(KIND=WP) M-by-N array.
  365. !> IF JOBF =='R', B(1:M,1:K) contains A*U(:,1:K), and can
  366. !> be used for computing the refined vectors; see further
  367. !> details in the provided references.
  368. !> If JOBF == 'E', B(1:M,1;K) contains
  369. !> A*U(:,1:K)*W(1:K,1:K), which are the vectors from the
  370. !> Exact DMD, up to scaling by the inverse eigenvalues.
  371. !> If JOBF =='N', then B is not referenced.
  372. !> See the descriptions of X, W, K.
  373. !> \endverbatim
  374. !.....
  375. !> \param[in] LDB
  376. !> \verbatim
  377. !> LDB (input) INTEGER, LDB >= M
  378. !> The leading dimension of the array B.
  379. !> \endverbatim
  380. !.....
  381. !> \param[out] W
  382. !> \verbatim
  383. !> W (workspace/output) REAL(KIND=WP) N-by-N array
  384. !> On exit, W(1:K,1:K) contains the K computed
  385. !> eigenvectors of the matrix Rayleigh quotient (real and
  386. !> imaginary parts for each complex conjugate pair of the
  387. !> eigenvalues). The Ritz vectors (returned in Z) are the
  388. !> product of X (containing a POD basis for the input
  389. !> matrix X) and W. See the descriptions of K, S, X and Z.
  390. !> W is also used as a workspace to temporarily store the
  391. !> left singular vectors of X.
  392. !> \endverbatim
  393. !.....
  394. !> \param[in] LDW
  395. !> \verbatim
  396. !> LDW (input) INTEGER, LDW >= N
  397. !> The leading dimension of the array W.
  398. !> \endverbatim
  399. !.....
  400. !> \param[out] S
  401. !> \verbatim
  402. !> S (workspace/output) REAL(KIND=WP) N-by-N array
  403. !> The array S(1:K,1:K) is used for the matrix Rayleigh
  404. !> quotient. This content is overwritten during
  405. !> the eigenvalue decomposition by SGEEV.
  406. !> See the description of K.
  407. !> \endverbatim
  408. !.....
  409. !> \param[in] LDS
  410. !> \verbatim
  411. !> LDS (input) INTEGER, LDS >= N
  412. !> The leading dimension of the array S.
  413. !> \endverbatim
  414. !.....
  415. !> \param[out] WORK
  416. !> \verbatim
  417. !> WORK (workspace/output) REAL(KIND=WP) LWORK-by-1 array
  418. !> On exit, WORK(1:N) contains the singular values of
  419. !> X (for JOBS=='N') or column scaled X (JOBS=='S', 'C').
  420. !> If WHTSVD==4, then WORK(N+1) and WORK(N+2) contain
  421. !> scaling factor WORK(N+2)/WORK(N+1) used to scale X
  422. !> and Y to avoid overflow in the SVD of X.
  423. !> This may be of interest if the scaling option is off
  424. !> and as many as possible smallest eigenvalues are
  425. !> desired to the highest feasible accuracy.
  426. !> If the call to SGEDMD is only workspace query, then
  427. !> WORK(1) contains the minimal workspace length and
  428. !> WORK(2) is the optimal workspace length. Hence, the
  429. !> length of work is at least 2.
  430. !> See the description of LWORK.
  431. !> \endverbatim
  432. !.....
  433. !> \param[in] LWORK
  434. !> \verbatim
  435. !> LWORK (input) INTEGER
  436. !> The minimal length of the workspace vector WORK.
  437. !> LWORK is calculated as follows:
  438. !> If WHTSVD == 1 ::
  439. !> If JOBZ == 'V', then
  440. !> LWORK >= MAX(2, N + LWORK_SVD, N+MAX(1,4*N)).
  441. !> If JOBZ == 'N' then
  442. !> LWORK >= MAX(2, N + LWORK_SVD, N+MAX(1,3*N)).
  443. !> Here LWORK_SVD = MAX(1,3*N+M,5*N) is the minimal
  444. !> workspace length of SGESVD.
  445. !> If WHTSVD == 2 ::
  446. !> If JOBZ == 'V', then
  447. !> LWORK >= MAX(2, N + LWORK_SVD, N+MAX(1,4*N))
  448. !> If JOBZ == 'N', then
  449. !> LWORK >= MAX(2, N + LWORK_SVD, N+MAX(1,3*N))
  450. !> Here LWORK_SVD = MAX(M, 5*N*N+4*N)+3*N*N is the
  451. !> minimal workspace length of SGESDD.
  452. !> If WHTSVD == 3 ::
  453. !> If JOBZ == 'V', then
  454. !> LWORK >= MAX(2, N+LWORK_SVD,N+MAX(1,4*N))
  455. !> If JOBZ == 'N', then
  456. !> LWORK >= MAX(2, N+LWORK_SVD,N+MAX(1,3*N))
  457. !> Here LWORK_SVD = N+M+MAX(3*N+1,
  458. !> MAX(1,3*N+M,5*N),MAX(1,N))
  459. !> is the minimal workspace length of SGESVDQ.
  460. !> If WHTSVD == 4 ::
  461. !> If JOBZ == 'V', then
  462. !> LWORK >= MAX(2, N+LWORK_SVD,N+MAX(1,4*N))
  463. !> If JOBZ == 'N', then
  464. !> LWORK >= MAX(2, N+LWORK_SVD,N+MAX(1,3*N))
  465. !> Here LWORK_SVD = MAX(7,2*M+N,6*N+2*N*N) is the
  466. !> minimal workspace length of SGEJSV.
  467. !> The above expressions are not simplified in order to
  468. !> make the usage of WORK more transparent, and for
  469. !> easier checking. In any case, LWORK >= 2.
  470. !> If on entry LWORK = -1, then a workspace query is
  471. !> assumed and the procedure only computes the minimal
  472. !> and the optimal workspace lengths for both WORK and
  473. !> IWORK. See the descriptions of WORK and IWORK.
  474. !> \endverbatim
  475. !.....
  476. !> \param[out] IWORK
  477. !> \verbatim
  478. !> IWORK (workspace/output) INTEGER LIWORK-by-1 array
  479. !> Workspace that is required only if WHTSVD equals
  480. !> 2 , 3 or 4. (See the description of WHTSVD).
  481. !> If on entry LWORK =-1 or LIWORK=-1, then the
  482. !> minimal length of IWORK is computed and returned in
  483. !> IWORK(1). See the description of LIWORK.
  484. !> \endverbatim
  485. !.....
  486. !> \param[in] LIWORK
  487. !> \verbatim
  488. !> LIWORK (input) INTEGER
  489. !> The minimal length of the workspace vector IWORK.
  490. !> If WHTSVD == 1, then only IWORK(1) is used; LIWORK >=1
  491. !> If WHTSVD == 2, then LIWORK >= MAX(1,8*MIN(M,N))
  492. !> If WHTSVD == 3, then LIWORK >= MAX(1,M+N-1)
  493. !> If WHTSVD == 4, then LIWORK >= MAX(3,M+3*N)
  494. !> If on entry LIWORK = -1, then a workspace query is
  495. !> assumed and the procedure only computes the minimal
  496. !> and the optimal workspace lengths for both WORK and
  497. !> IWORK. See the descriptions of WORK and IWORK.
  498. !> \endverbatim
  499. !.....
  500. !> \param[out] INFO
  501. !> \verbatim
  502. !> INFO (output) INTEGER
  503. !> -i < 0 :: On entry, the i-th argument had an
  504. !> illegal value
  505. !> = 0 :: Successful return.
  506. !> = 1 :: Void input. Quick exit (M=0 or N=0).
  507. !> = 2 :: The SVD computation of X did not converge.
  508. !> Suggestion: Check the input data and/or
  509. !> repeat with different WHTSVD.
  510. !> = 3 :: The computation of the eigenvalues did not
  511. !> converge.
  512. !> = 4 :: If data scaling was requested on input and
  513. !> the procedure found inconsistency in the data
  514. !> such that for some column index i,
  515. !> X(:,i) = 0 but Y(:,i) /= 0, then Y(:,i) is set
  516. !> to zero if JOBS=='C'. The computation proceeds
  517. !> with original or modified data and warning
  518. !> flag is set with INFO=4.
  519. !> \endverbatim
  520. !
  521. ! Authors:
  522. ! ========
  523. !
  524. !> \author Zlatko Drmac
  525. !
  526. !> \ingroup gedmd
  527. !
  528. !.............................................................
  529. !.............................................................
  530. SUBROUTINE SGEDMD( JOBS, JOBZ, JOBR, JOBF, WHTSVD, &
  531. M, N, X, LDX, Y, LDY, NRNK, TOL, &
  532. K, REIG, IMEIG, Z, LDZ, RES, &
  533. B, LDB, W, LDW, S, LDS, &
  534. WORK, LWORK, IWORK, LIWORK, INFO )
  535. !
  536. ! -- LAPACK driver routine --
  537. !
  538. ! -- LAPACK is a software package provided by University of --
  539. ! -- Tennessee, University of California Berkeley, University of --
  540. ! -- Colorado Denver and NAG Ltd.. --
  541. !
  542. !.....
  543. USE iso_fortran_env
  544. IMPLICIT NONE
  545. INTEGER, PARAMETER :: WP = real32
  546. !
  547. ! Scalar arguments
  548. ! ~~~~~~~~~~~~~~~~
  549. CHARACTER, INTENT(IN) :: JOBS, JOBZ, JOBR, JOBF
  550. INTEGER, INTENT(IN) :: WHTSVD, M, N, LDX, LDY, &
  551. NRNK, LDZ, LDB, LDW, LDS, &
  552. LWORK, LIWORK
  553. INTEGER, INTENT(OUT) :: K, INFO
  554. REAL(KIND=WP), INTENT(IN) :: TOL
  555. !
  556. ! Array arguments
  557. ! ~~~~~~~~~~~~~~~
  558. REAL(KIND=WP), INTENT(INOUT) :: X(LDX,*), Y(LDY,*)
  559. REAL(KIND=WP), INTENT(OUT) :: Z(LDZ,*), B(LDB,*), &
  560. W(LDW,*), S(LDS,*)
  561. REAL(KIND=WP), INTENT(OUT) :: REIG(*), IMEIG(*), &
  562. RES(*)
  563. REAL(KIND=WP), INTENT(OUT) :: WORK(*)
  564. INTEGER, INTENT(OUT) :: IWORK(*)
  565. !
  566. ! Parameters
  567. ! ~~~~~~~~~~
  568. REAL(KIND=WP), PARAMETER :: ONE = 1.0_WP
  569. REAL(KIND=WP), PARAMETER :: ZERO = 0.0_WP
  570. !
  571. ! Local scalars
  572. ! ~~~~~~~~~~~~~
  573. REAL(KIND=WP) :: OFL, ROOTSC, SCALE, SMALL, &
  574. SSUM, XSCL1, XSCL2
  575. INTEGER :: i, j, IMINWR, INFO1, INFO2, &
  576. LWRKEV, LWRSDD, LWRSVD, &
  577. LWRSVQ, MLWORK, MWRKEV, MWRSDD, &
  578. MWRSVD, MWRSVJ, MWRSVQ, NUMRNK, &
  579. OLWORK
  580. LOGICAL :: BADXY, LQUERY, SCCOLX, SCCOLY, &
  581. WNTEX, WNTREF, WNTRES, WNTVEC
  582. CHARACTER :: JOBZL, T_OR_N
  583. CHARACTER :: JSVOPT
  584. !
  585. ! Local arrays
  586. ! ~~~~~~~~~~~~
  587. REAL(KIND=WP) :: AB(2,2), RDUMMY(2), RDUMMY2(2)
  588. !
  589. ! External functions (BLAS and LAPACK)
  590. ! ~~~~~~~~~~~~~~~~~
  591. REAL(KIND=WP) SLANGE, SLAMCH, SNRM2
  592. EXTERNAL SLANGE, SLAMCH, SNRM2, ISAMAX
  593. INTEGER ISAMAX
  594. LOGICAL SISNAN, LSAME
  595. EXTERNAL SISNAN, LSAME
  596. !
  597. ! External subroutines (BLAS and LAPACK)
  598. ! ~~~~~~~~~~~~~~~~~~~~
  599. EXTERNAL SAXPY, SGEMM, SSCAL
  600. EXTERNAL SGEEV, SGEJSV, SGESDD, SGESVD, SGESVDQ, &
  601. SLACPY, SLASCL, SLASSQ, XERBLA
  602. !
  603. ! Intrinsic functions
  604. ! ~~~~~~~~~~~~~~~~~~~
  605. INTRINSIC INT, FLOAT, MAX, SQRT
  606. !............................................................
  607. !
  608. ! Test the input arguments
  609. !
  610. WNTRES = LSAME(JOBR,'R')
  611. SCCOLX = LSAME(JOBS,'S') .OR. LSAME(JOBS,'C')
  612. SCCOLY = LSAME(JOBS,'Y')
  613. WNTVEC = LSAME(JOBZ,'V')
  614. WNTREF = LSAME(JOBF,'R')
  615. WNTEX = LSAME(JOBF,'E')
  616. INFO = 0
  617. LQUERY = ( ( LWORK == -1 ) .OR. ( LIWORK == -1 ) )
  618. !
  619. IF ( .NOT. (SCCOLX .OR. SCCOLY .OR. &
  620. LSAME(JOBS,'N')) ) THEN
  621. INFO = -1
  622. ELSE IF ( .NOT. (WNTVEC .OR. LSAME(JOBZ,'N') &
  623. .OR. LSAME(JOBZ,'F')) ) THEN
  624. INFO = -2
  625. ELSE IF ( .NOT. (WNTRES .OR. LSAME(JOBR,'N')) .OR. &
  626. ( WNTRES .AND. (.NOT.WNTVEC) ) ) THEN
  627. INFO = -3
  628. ELSE IF ( .NOT. (WNTREF .OR. WNTEX .OR. &
  629. LSAME(JOBF,'N') ) ) THEN
  630. INFO = -4
  631. ELSE IF ( .NOT.((WHTSVD == 1) .OR. (WHTSVD == 2) .OR. &
  632. (WHTSVD == 3) .OR. (WHTSVD == 4) )) THEN
  633. INFO = -5
  634. ELSE IF ( M < 0 ) THEN
  635. INFO = -6
  636. ELSE IF ( ( N < 0 ) .OR. ( N > M ) ) THEN
  637. INFO = -7
  638. ELSE IF ( LDX < M ) THEN
  639. INFO = -9
  640. ELSE IF ( LDY < M ) THEN
  641. INFO = -11
  642. ELSE IF ( .NOT. (( NRNK == -2).OR.(NRNK == -1).OR. &
  643. ((NRNK >= 1).AND.(NRNK <=N ))) ) THEN
  644. INFO = -12
  645. ELSE IF ( ( TOL < ZERO ) .OR. ( TOL >= ONE ) ) THEN
  646. INFO = -13
  647. ELSE IF ( LDZ < M ) THEN
  648. INFO = -18
  649. ELSE IF ( (WNTREF .OR. WNTEX ) .AND. ( LDB < M ) ) THEN
  650. INFO = -21
  651. ELSE IF ( LDW < N ) THEN
  652. INFO = -23
  653. ELSE IF ( LDS < N ) THEN
  654. INFO = -25
  655. END IF
  656. !
  657. IF ( INFO == 0 ) THEN
  658. ! Compute the minimal and the optimal workspace
  659. ! requirements. Simulate running the code and
  660. ! determine minimal and optimal sizes of the
  661. ! workspace at any moment of the run.
  662. IF ( N == 0 ) THEN
  663. ! Quick return. All output except K is void.
  664. ! INFO=1 signals the void input.
  665. ! In case of a workspace query, the default
  666. ! minimal workspace lengths are returned.
  667. IF ( LQUERY ) THEN
  668. IWORK(1) = 1
  669. WORK(1) = 2
  670. WORK(2) = 2
  671. ELSE
  672. K = 0
  673. END IF
  674. INFO = 1
  675. RETURN
  676. END IF
  677. MLWORK = MAX(2,N)
  678. OLWORK = MAX(2,N)
  679. IMINWR = 1
  680. SELECT CASE ( WHTSVD )
  681. CASE (1)
  682. ! The following is specified as the minimal
  683. ! length of WORK in the definition of SGESVD:
  684. ! MWRSVD = MAX(1,3*MIN(M,N)+MAX(M,N),5*MIN(M,N))
  685. MWRSVD = MAX(1,3*MIN(M,N)+MAX(M,N),5*MIN(M,N))
  686. MLWORK = MAX(MLWORK,N + MWRSVD)
  687. IF ( LQUERY ) THEN
  688. CALL SGESVD( 'O', 'S', M, N, X, LDX, WORK, &
  689. B, LDB, W, LDW, RDUMMY, -1, INFO1 )
  690. LWRSVD = MAX( MWRSVD, INT( RDUMMY(1) ) )
  691. OLWORK = MAX(OLWORK,N + LWRSVD)
  692. END IF
  693. CASE (2)
  694. ! The following is specified as the minimal
  695. ! length of WORK in the definition of SGESDD:
  696. ! MWRSDD = 3*MIN(M,N)*MIN(M,N) +
  697. ! MAX( MAX(M,N),5*MIN(M,N)*MIN(M,N)+4*MIN(M,N) )
  698. ! IMINWR = 8*MIN(M,N)
  699. MWRSDD = 3*MIN(M,N)*MIN(M,N) + &
  700. MAX( MAX(M,N),5*MIN(M,N)*MIN(M,N)+4*MIN(M,N) )
  701. MLWORK = MAX(MLWORK,N + MWRSDD)
  702. IMINWR = 8*MIN(M,N)
  703. IF ( LQUERY ) THEN
  704. CALL SGESDD( 'O', M, N, X, LDX, WORK, B, &
  705. LDB, W, LDW, RDUMMY, -1, IWORK, INFO1 )
  706. LWRSDD = MAX( MWRSDD, INT( RDUMMY(1) ) )
  707. OLWORK = MAX(OLWORK,N + LWRSDD)
  708. END IF
  709. CASE (3)
  710. !LWQP3 = 3*N+1
  711. !LWORQ = MAX(N, 1)
  712. !MWRSVD = MAX(1,3*MIN(M,N)+MAX(M,N),5*MIN(M,N))
  713. !MWRSVQ = N + MAX( LWQP3, MWRSVD, LWORQ )+ MAX(M,2)
  714. !MLWORK = N + MWRSVQ
  715. !IMINWR = M+N-1
  716. CALL SGESVDQ( 'H', 'P', 'N', 'R', 'R', M, N, &
  717. X, LDX, WORK, Z, LDZ, W, LDW, &
  718. NUMRNK, IWORK, -1, RDUMMY, &
  719. -1, RDUMMY2, -1, INFO1 )
  720. IMINWR = IWORK(1)
  721. MWRSVQ = INT(RDUMMY(2))
  722. MLWORK = MAX(MLWORK,N+MWRSVQ+INT(RDUMMY2(1)))
  723. IF ( LQUERY ) THEN
  724. LWRSVQ = INT(RDUMMY(1))
  725. OLWORK = MAX(OLWORK,N+LWRSVQ+INT(RDUMMY2(1)))
  726. END IF
  727. CASE (4)
  728. JSVOPT = 'J'
  729. !MWRSVJ = MAX( 7, 2*M+N, 6*N+2*N*N )! for JSVOPT='V'
  730. MWRSVJ = MAX( 7, 2*M+N, 4*N+N*N, 2*N+N*N+6 )
  731. MLWORK = MAX(MLWORK,N+MWRSVJ)
  732. IMINWR = MAX( 3, M+3*N )
  733. IF ( LQUERY ) THEN
  734. OLWORK = MAX(OLWORK,N+MWRSVJ)
  735. END IF
  736. END SELECT
  737. IF ( WNTVEC .OR. WNTEX .OR. LSAME(JOBZ,'F') ) THEN
  738. JOBZL = 'V'
  739. ELSE
  740. JOBZL = 'N'
  741. END IF
  742. ! Workspace calculation to the SGEEV call
  743. IF ( LSAME(JOBZL,'V') ) THEN
  744. MWRKEV = MAX( 1, 4*N )
  745. ELSE
  746. MWRKEV = MAX( 1, 3*N )
  747. END IF
  748. MLWORK = MAX(MLWORK,N+MWRKEV)
  749. IF ( LQUERY ) THEN
  750. CALL SGEEV( 'N', JOBZL, N, S, LDS, REIG, &
  751. IMEIG, W, LDW, W, LDW, RDUMMY, -1, INFO1 )
  752. LWRKEV = MAX( MWRKEV, INT(RDUMMY(1)) )
  753. OLWORK = MAX( OLWORK, N+LWRKEV )
  754. END IF
  755. !
  756. IF ( LIWORK < IMINWR .AND. (.NOT.LQUERY) ) INFO = -29
  757. IF ( LWORK < MLWORK .AND. (.NOT.LQUERY) ) INFO = -27
  758. END IF
  759. !
  760. IF( INFO /= 0 ) THEN
  761. CALL XERBLA( 'SGEDMD', -INFO )
  762. RETURN
  763. ELSE IF ( LQUERY ) THEN
  764. ! Return minimal and optimal workspace sizes
  765. IWORK(1) = IMINWR
  766. WORK(1) = MLWORK
  767. WORK(2) = OLWORK
  768. RETURN
  769. END IF
  770. !............................................................
  771. !
  772. OFL = SLAMCH('O')
  773. SMALL = SLAMCH('S')
  774. BADXY = .FALSE.
  775. !
  776. ! <1> Optional scaling of the snapshots (columns of X, Y)
  777. ! ==========================================================
  778. IF ( SCCOLX ) THEN
  779. ! The columns of X will be normalized.
  780. ! To prevent overflows, the column norms of X are
  781. ! carefully computed using SLASSQ.
  782. K = 0
  783. DO i = 1, N
  784. !WORK(i) = DNRM2( M, X(1,i), 1 )
  785. SSUM = ONE
  786. SCALE = ZERO
  787. CALL SLASSQ( M, X(1,i), 1, SCALE, SSUM )
  788. IF ( SISNAN(SCALE) .OR. SISNAN(SSUM) ) THEN
  789. K = 0
  790. INFO = -8
  791. CALL XERBLA('SGEDMD',-INFO)
  792. END IF
  793. IF ( (SCALE /= ZERO) .AND. (SSUM /= ZERO) ) THEN
  794. ROOTSC = SQRT(SSUM)
  795. IF ( SCALE .GE. (OFL / ROOTSC) ) THEN
  796. ! Norm of X(:,i) overflows. First, X(:,i)
  797. ! is scaled by
  798. ! ( ONE / ROOTSC ) / SCALE = 1/||X(:,i)||_2.
  799. ! Next, the norm of X(:,i) is stored without
  800. ! overflow as WORK(i) = - SCALE * (ROOTSC/M),
  801. ! the minus sign indicating the 1/M factor.
  802. ! Scaling is performed without overflow, and
  803. ! underflow may occur in the smallest entries
  804. ! of X(:,i). The relative backward and forward
  805. ! errors are small in the ell_2 norm.
  806. CALL SLASCL( 'G', 0, 0, SCALE, ONE/ROOTSC, &
  807. M, 1, X(1,i), M, INFO2 )
  808. WORK(i) = - SCALE * ( ROOTSC / FLOAT(M) )
  809. ELSE
  810. ! X(:,i) will be scaled to unit 2-norm
  811. WORK(i) = SCALE * ROOTSC
  812. CALL SLASCL( 'G',0, 0, WORK(i), ONE, M, 1, &
  813. X(1,i), M, INFO2 ) ! LAPACK CALL
  814. ! X(1:M,i) = (ONE/WORK(i)) * X(1:M,i) ! INTRINSIC
  815. END IF
  816. ELSE
  817. WORK(i) = ZERO
  818. K = K + 1
  819. END IF
  820. END DO
  821. IF ( K == N ) THEN
  822. ! All columns of X are zero. Return error code -8.
  823. ! (the 8th input variable had an illegal value)
  824. K = 0
  825. INFO = -8
  826. CALL XERBLA('SGEDMD',-INFO)
  827. RETURN
  828. END IF
  829. DO i = 1, N
  830. ! Now, apply the same scaling to the columns of Y.
  831. IF ( WORK(i) > ZERO ) THEN
  832. CALL SSCAL( M, ONE/WORK(i), Y(1,i), 1 ) ! BLAS CALL
  833. ! Y(1:M,i) = (ONE/WORK(i)) * Y(1:M,i) ! INTRINSIC
  834. ELSE IF ( WORK(i) < ZERO ) THEN
  835. CALL SLASCL( 'G', 0, 0, -WORK(i), &
  836. ONE/FLOAT(M), M, 1, Y(1,i), M, INFO2 ) ! LAPACK CALL
  837. ELSE IF ( Y(ISAMAX(M, Y(1,i),1),i ) &
  838. /= ZERO ) THEN
  839. ! X(:,i) is zero vector. For consistency,
  840. ! Y(:,i) should also be zero. If Y(:,i) is not
  841. ! zero, then the data might be inconsistent or
  842. ! corrupted. If JOBS == 'C', Y(:,i) is set to
  843. ! zero and a warning flag is raised.
  844. ! The computation continues but the
  845. ! situation will be reported in the output.
  846. BADXY = .TRUE.
  847. IF ( LSAME(JOBS,'C')) &
  848. CALL SSCAL( M, ZERO, Y(1,i), 1 ) ! BLAS CALL
  849. END IF
  850. END DO
  851. END IF
  852. !
  853. IF ( SCCOLY ) THEN
  854. ! The columns of Y will be normalized.
  855. ! To prevent overflows, the column norms of Y are
  856. ! carefully computed using SLASSQ.
  857. DO i = 1, N
  858. !WORK(i) = DNRM2( M, Y(1,i), 1 )
  859. SSUM = ONE
  860. SCALE = ZERO
  861. CALL SLASSQ( M, Y(1,i), 1, SCALE, SSUM )
  862. IF ( SISNAN(SCALE) .OR. SISNAN(SSUM) ) THEN
  863. K = 0
  864. INFO = -10
  865. CALL XERBLA('SGEDMD',-INFO)
  866. END IF
  867. IF ( SCALE /= ZERO .AND. (SSUM /= ZERO) ) THEN
  868. ROOTSC = SQRT(SSUM)
  869. IF ( SCALE .GE. (OFL / ROOTSC) ) THEN
  870. ! Norm of Y(:,i) overflows. First, Y(:,i)
  871. ! is scaled by
  872. ! ( ONE / ROOTSC ) / SCALE = 1/||Y(:,i)||_2.
  873. ! Next, the norm of Y(:,i) is stored without
  874. ! overflow as WORK(i) = - SCALE * (ROOTSC/M),
  875. ! the minus sign indicating the 1/M factor.
  876. ! Scaling is performed without overflow, and
  877. ! underflow may occur in the smallest entries
  878. ! of Y(:,i). The relative backward and forward
  879. ! errors are small in the ell_2 norm.
  880. CALL SLASCL( 'G', 0, 0, SCALE, ONE/ROOTSC, &
  881. M, 1, Y(1,i), M, INFO2 )
  882. WORK(i) = - SCALE * ( ROOTSC / FLOAT(M) )
  883. ELSE
  884. ! X(:,i) will be scaled to unit 2-norm
  885. WORK(i) = SCALE * ROOTSC
  886. CALL SLASCL( 'G',0, 0, WORK(i), ONE, M, 1, &
  887. Y(1,i), M, INFO2 ) ! LAPACK CALL
  888. ! Y(1:M,i) = (ONE/WORK(i)) * Y(1:M,i) ! INTRINSIC
  889. END IF
  890. ELSE
  891. WORK(i) = ZERO
  892. END IF
  893. END DO
  894. DO i = 1, N
  895. ! Now, apply the same scaling to the columns of X.
  896. IF ( WORK(i) > ZERO ) THEN
  897. CALL SSCAL( M, ONE/WORK(i), X(1,i), 1 ) ! BLAS CALL
  898. ! X(1:M,i) = (ONE/WORK(i)) * X(1:M,i) ! INTRINSIC
  899. ELSE IF ( WORK(i) < ZERO ) THEN
  900. CALL SLASCL( 'G', 0, 0, -WORK(i), &
  901. ONE/FLOAT(M), M, 1, X(1,i), M, INFO2 ) ! LAPACK CALL
  902. ELSE IF ( X(ISAMAX(M, X(1,i),1),i ) &
  903. /= ZERO ) THEN
  904. ! Y(:,i) is zero vector. If X(:,i) is not
  905. ! zero, then a warning flag is raised.
  906. ! The computation continues but the
  907. ! situation will be reported in the output.
  908. BADXY = .TRUE.
  909. END IF
  910. END DO
  911. END IF
  912. !
  913. ! <2> SVD of the data snapshot matrix X.
  914. ! =====================================
  915. ! The left singular vectors are stored in the array X.
  916. ! The right singular vectors are in the array W.
  917. ! The array W will later on contain the eigenvectors
  918. ! of a Rayleigh quotient.
  919. NUMRNK = N
  920. SELECT CASE ( WHTSVD )
  921. CASE (1)
  922. CALL SGESVD( 'O', 'S', M, N, X, LDX, WORK, B, &
  923. LDB, W, LDW, WORK(N+1), LWORK-N, INFO1 ) ! LAPACK CALL
  924. T_OR_N = 'T'
  925. CASE (2)
  926. CALL SGESDD( 'O', M, N, X, LDX, WORK, B, LDB, W, &
  927. LDW, WORK(N+1), LWORK-N, IWORK, INFO1 ) ! LAPACK CALL
  928. T_OR_N = 'T'
  929. CASE (3)
  930. CALL SGESVDQ( 'H', 'P', 'N', 'R', 'R', M, N, &
  931. X, LDX, WORK, Z, LDZ, W, LDW, &
  932. NUMRNK, IWORK, LIWORK, WORK(N+MAX(2,M)+1),&
  933. LWORK-N-MAX(2,M), WORK(N+1), MAX(2,M), INFO1) ! LAPACK CALL
  934. CALL SLACPY( 'A', M, NUMRNK, Z, LDZ, X, LDX ) ! LAPACK CALL
  935. T_OR_N = 'T'
  936. CASE (4)
  937. CALL SGEJSV( 'F', 'U', JSVOPT, 'N', 'N', 'P', M, &
  938. N, X, LDX, WORK, Z, LDZ, W, LDW, &
  939. WORK(N+1), LWORK-N, IWORK, INFO1 ) ! LAPACK CALL
  940. CALL SLACPY( 'A', M, N, Z, LDZ, X, LDX ) ! LAPACK CALL
  941. T_OR_N = 'N'
  942. XSCL1 = WORK(N+1)
  943. XSCL2 = WORK(N+2)
  944. IF ( XSCL1 /= XSCL2 ) THEN
  945. ! This is an exceptional situation. If the
  946. ! data matrices are not scaled and the
  947. ! largest singular value of X overflows.
  948. ! In that case SGEJSV can return the SVD
  949. ! in scaled form. The scaling factor can be used
  950. ! to rescale the data (X and Y).
  951. CALL SLASCL( 'G', 0, 0, XSCL1, XSCL2, M, N, Y, LDY, INFO2 )
  952. END IF
  953. END SELECT
  954. !
  955. IF ( INFO1 > 0 ) THEN
  956. ! The SVD selected subroutine did not converge.
  957. ! Return with an error code.
  958. INFO = 2
  959. RETURN
  960. END IF
  961. !
  962. IF ( WORK(1) == ZERO ) THEN
  963. ! The largest computed singular value of (scaled)
  964. ! X is zero. Return error code -8
  965. ! (the 8th input variable had an illegal value).
  966. K = 0
  967. INFO = -8
  968. CALL XERBLA('SGEDMD',-INFO)
  969. RETURN
  970. END IF
  971. !
  972. !<3> Determine the numerical rank of the data
  973. ! snapshots matrix X. This depends on the
  974. ! parameters NRNK and TOL.
  975. SELECT CASE ( NRNK )
  976. CASE ( -1 )
  977. K = 1
  978. DO i = 2, NUMRNK
  979. IF ( ( WORK(i) <= WORK(1)*TOL ) .OR. &
  980. ( WORK(i) <= SMALL ) ) EXIT
  981. K = K + 1
  982. END DO
  983. CASE ( -2 )
  984. K = 1
  985. DO i = 1, NUMRNK-1
  986. IF ( ( WORK(i+1) <= WORK(i)*TOL ) .OR. &
  987. ( WORK(i) <= SMALL ) ) EXIT
  988. K = K + 1
  989. END DO
  990. CASE DEFAULT
  991. K = 1
  992. DO i = 2, NRNK
  993. IF ( WORK(i) <= SMALL ) EXIT
  994. K = K + 1
  995. END DO
  996. END SELECT
  997. ! Now, U = X(1:M,1:K) is the SVD/POD basis for the
  998. ! snapshot data in the input matrix X.
  999. !<4> Compute the Rayleigh quotient S = U^T * A * U.
  1000. ! Depending on the requested outputs, the computation
  1001. ! is organized to compute additional auxiliary
  1002. ! matrices (for the residuals and refinements).
  1003. !
  1004. ! In all formulas below, we need V_k*Sigma_k^(-1)
  1005. ! where either V_k is in W(1:N,1:K), or V_k^T is in
  1006. ! W(1:K,1:N). Here Sigma_k=diag(WORK(1:K)).
  1007. IF ( LSAME(T_OR_N, 'N') ) THEN
  1008. DO i = 1, K
  1009. CALL SSCAL( N, ONE/WORK(i), W(1,i), 1 ) ! BLAS CALL
  1010. ! W(1:N,i) = (ONE/WORK(i)) * W(1:N,i) ! INTRINSIC
  1011. END DO
  1012. ELSE
  1013. ! This non-unit stride access is due to the fact
  1014. ! that SGESVD, SGESVDQ and SGESDD return the
  1015. ! transposed matrix of the right singular vectors.
  1016. !DO i = 1, K
  1017. ! CALL SSCAL( N, ONE/WORK(i), W(i,1), LDW ) ! BLAS CALL
  1018. ! ! W(i,1:N) = (ONE/WORK(i)) * W(i,1:N) ! INTRINSIC
  1019. !END DO
  1020. DO i = 1, K
  1021. WORK(N+i) = ONE/WORK(i)
  1022. END DO
  1023. DO j = 1, N
  1024. DO i = 1, K
  1025. W(i,j) = (WORK(N+i))*W(i,j)
  1026. END DO
  1027. END DO
  1028. END IF
  1029. !
  1030. IF ( WNTREF ) THEN
  1031. !
  1032. ! Need A*U(:,1:K)=Y*V_k*inv(diag(WORK(1:K)))
  1033. ! for computing the refined Ritz vectors
  1034. ! (optionally, outside SGEDMD).
  1035. CALL SGEMM( 'N', T_OR_N, M, K, N, ONE, Y, LDY, W, &
  1036. LDW, ZERO, Z, LDZ ) ! BLAS CALL
  1037. ! Z(1:M,1:K)=MATMUL(Y(1:M,1:N),TRANSPOSE(W(1:K,1:N))) ! INTRINSIC, for T_OR_N=='T'
  1038. ! Z(1:M,1:K)=MATMUL(Y(1:M,1:N),W(1:N,1:K)) ! INTRINSIC, for T_OR_N=='N'
  1039. !
  1040. ! At this point Z contains
  1041. ! A * U(:,1:K) = Y * V_k * Sigma_k^(-1), and
  1042. ! this is needed for computing the residuals.
  1043. ! This matrix is returned in the array B and
  1044. ! it can be used to compute refined Ritz vectors.
  1045. CALL SLACPY( 'A', M, K, Z, LDZ, B, LDB ) ! BLAS CALL
  1046. ! B(1:M,1:K) = Z(1:M,1:K) ! INTRINSIC
  1047. CALL SGEMM( 'T', 'N', K, K, M, ONE, X, LDX, Z, &
  1048. LDZ, ZERO, S, LDS ) ! BLAS CALL
  1049. ! S(1:K,1:K) = MATMUL(TANSPOSE(X(1:M,1:K)),Z(1:M,1:K)) ! INTRINSIC
  1050. ! At this point S = U^T * A * U is the Rayleigh quotient.
  1051. ELSE
  1052. ! A * U(:,1:K) is not explicitly needed and the
  1053. ! computation is organized differently. The Rayleigh
  1054. ! quotient is computed more efficiently.
  1055. CALL SGEMM( 'T', 'N', K, N, M, ONE, X, LDX, Y, LDY, &
  1056. ZERO, Z, LDZ ) ! BLAS CALL
  1057. ! Z(1:K,1:N) = MATMUL( TRANSPOSE(X(1:M,1:K)), Y(1:M,1:N) ) ! INTRINSIC
  1058. ! In the two SGEMM calls here, can use K for LDZ
  1059. CALL SGEMM( 'N', T_OR_N, K, K, N, ONE, Z, LDZ, W, &
  1060. LDW, ZERO, S, LDS ) ! BLAS CALL
  1061. ! S(1:K,1:K) = MATMUL(Z(1:K,1:N),TRANSPOSE(W(1:K,1:N))) ! INTRINSIC, for T_OR_N=='T'
  1062. ! S(1:K,1:K) = MATMUL(Z(1:K,1:N),(W(1:N,1:K))) ! INTRINSIC, for T_OR_N=='N'
  1063. ! At this point S = U^T * A * U is the Rayleigh quotient.
  1064. ! If the residuals are requested, save scaled V_k into Z.
  1065. ! Recall that V_k or V_k^T is stored in W.
  1066. IF ( WNTRES .OR. WNTEX ) THEN
  1067. IF ( LSAME(T_OR_N, 'N') ) THEN
  1068. CALL SLACPY( 'A', N, K, W, LDW, Z, LDZ )
  1069. ELSE
  1070. CALL SLACPY( 'A', K, N, W, LDW, Z, LDZ )
  1071. END IF
  1072. END IF
  1073. END IF
  1074. !
  1075. !<5> Compute the Ritz values and (if requested) the
  1076. ! right eigenvectors of the Rayleigh quotient.
  1077. !
  1078. CALL SGEEV( 'N', JOBZL, K, S, LDS, REIG, IMEIG, W, &
  1079. LDW, W, LDW, WORK(N+1), LWORK-N, INFO1 ) ! LAPACK CALL
  1080. !
  1081. ! W(1:K,1:K) contains the eigenvectors of the Rayleigh
  1082. ! quotient. Even in the case of complex spectrum, all
  1083. ! computation is done in real arithmetic. REIG and
  1084. ! IMEIG are the real and the imaginary parts of the
  1085. ! eigenvalues, so that the spectrum is given as
  1086. ! REIG(:) + sqrt(-1)*IMEIG(:). Complex conjugate pairs
  1087. ! are listed at consecutive positions. For such a
  1088. ! complex conjugate pair of the eigenvalues, the
  1089. ! corresponding eigenvectors are also a complex
  1090. ! conjugate pair with the real and imaginary parts
  1091. ! stored column-wise in W at the corresponding
  1092. ! consecutive column indices. See the description of Z.
  1093. ! Also, see the description of SGEEV.
  1094. IF ( INFO1 > 0 ) THEN
  1095. ! SGEEV failed to compute the eigenvalues and
  1096. ! eigenvectors of the Rayleigh quotient.
  1097. INFO = 3
  1098. RETURN
  1099. END IF
  1100. !
  1101. ! <6> Compute the eigenvectors (if requested) and,
  1102. ! the residuals (if requested).
  1103. !
  1104. IF ( WNTVEC .OR. WNTEX ) THEN
  1105. IF ( WNTRES ) THEN
  1106. IF ( WNTREF ) THEN
  1107. ! Here, if the refinement is requested, we have
  1108. ! A*U(:,1:K) already computed and stored in Z.
  1109. ! For the residuals, need Y = A * U(:,1;K) * W.
  1110. CALL SGEMM( 'N', 'N', M, K, K, ONE, Z, LDZ, W, &
  1111. LDW, ZERO, Y, LDY ) ! BLAS CALL
  1112. ! Y(1:M,1:K) = Z(1:M,1:K) * W(1:K,1:K) ! INTRINSIC
  1113. ! This frees Z; Y contains A * U(:,1:K) * W.
  1114. ELSE
  1115. ! Compute S = V_k * Sigma_k^(-1) * W, where
  1116. ! V_k * Sigma_k^(-1) is stored in Z
  1117. CALL SGEMM( T_OR_N, 'N', N, K, K, ONE, Z, LDZ, &
  1118. W, LDW, ZERO, S, LDS )
  1119. ! Then, compute Z = Y * S =
  1120. ! = Y * V_k * Sigma_k^(-1) * W(1:K,1:K) =
  1121. ! = A * U(:,1:K) * W(1:K,1:K)
  1122. CALL SGEMM( 'N', 'N', M, K, N, ONE, Y, LDY, S, &
  1123. LDS, ZERO, Z, LDZ )
  1124. ! Save a copy of Z into Y and free Z for holding
  1125. ! the Ritz vectors.
  1126. CALL SLACPY( 'A', M, K, Z, LDZ, Y, LDY )
  1127. IF ( WNTEX ) CALL SLACPY( 'A', M, K, Z, LDZ, B, LDB )
  1128. END IF
  1129. ELSE IF ( WNTEX ) THEN
  1130. ! Compute S = V_k * Sigma_k^(-1) * W, where
  1131. ! V_k * Sigma_k^(-1) is stored in Z
  1132. CALL SGEMM( T_OR_N, 'N', N, K, K, ONE, Z, LDZ, &
  1133. W, LDW, ZERO, S, LDS )
  1134. ! Then, compute Z = Y * S =
  1135. ! = Y * V_k * Sigma_k^(-1) * W(1:K,1:K) =
  1136. ! = A * U(:,1:K) * W(1:K,1:K)
  1137. CALL SGEMM( 'N', 'N', M, K, N, ONE, Y, LDY, S, &
  1138. LDS, ZERO, B, LDB )
  1139. ! The above call replaces the following two calls
  1140. ! that were used in the developing-testing phase.
  1141. ! CALL SGEMM( 'N', 'N', M, K, N, ONE, Y, LDY, S, &
  1142. ! LDS, ZERO, Z, LDZ)
  1143. ! Save a copy of Z into B and free Z for holding
  1144. ! the Ritz vectors.
  1145. ! CALL SLACPY( 'A', M, K, Z, LDZ, B, LDB )
  1146. END IF
  1147. !
  1148. ! Compute the real form of the Ritz vectors
  1149. IF ( WNTVEC ) CALL SGEMM( 'N', 'N', M, K, K, ONE, X, LDX, W, LDW, &
  1150. ZERO, Z, LDZ ) ! BLAS CALL
  1151. ! Z(1:M,1:K) = MATMUL(X(1:M,1:K), W(1:K,1:K)) ! INTRINSIC
  1152. !
  1153. IF ( WNTRES ) THEN
  1154. i = 1
  1155. DO WHILE ( i <= K )
  1156. IF ( IMEIG(i) == ZERO ) THEN
  1157. ! have a real eigenvalue with real eigenvector
  1158. CALL SAXPY( M, -REIG(i), Z(1,i), 1, Y(1,i), 1 ) ! BLAS CALL
  1159. ! Y(1:M,i) = Y(1:M,i) - REIG(i) * Z(1:M,i) ! INTRINSIC
  1160. RES(i) = SNRM2( M, Y(1,i), 1 ) ! BLAS CALL
  1161. i = i + 1
  1162. ELSE
  1163. ! Have a complex conjugate pair
  1164. ! REIG(i) +- sqrt(-1)*IMEIG(i).
  1165. ! Since all computation is done in real
  1166. ! arithmetic, the formula for the residual
  1167. ! is recast for real representation of the
  1168. ! complex conjugate eigenpair. See the
  1169. ! description of RES.
  1170. AB(1,1) = REIG(i)
  1171. AB(2,1) = -IMEIG(i)
  1172. AB(1,2) = IMEIG(i)
  1173. AB(2,2) = REIG(i)
  1174. CALL SGEMM( 'N', 'N', M, 2, 2, -ONE, Z(1,i), &
  1175. LDZ, AB, 2, ONE, Y(1,i), LDY ) ! BLAS CALL
  1176. ! Y(1:M,i:i+1) = Y(1:M,i:i+1) - Z(1:M,i:i+1) * AB ! INTRINSIC
  1177. RES(i) = SLANGE( 'F', M, 2, Y(1,i), LDY, &
  1178. WORK(N+1) ) ! LAPACK CALL
  1179. RES(i+1) = RES(i)
  1180. i = i + 2
  1181. END IF
  1182. END DO
  1183. END IF
  1184. END IF
  1185. !
  1186. IF ( WHTSVD == 4 ) THEN
  1187. WORK(N+1) = XSCL1
  1188. WORK(N+2) = XSCL2
  1189. END IF
  1190. !
  1191. ! Successful exit.
  1192. IF ( .NOT. BADXY ) THEN
  1193. INFO = 0
  1194. ELSE
  1195. ! A warning on possible data inconsistency.
  1196. ! This should be a rare event.
  1197. INFO = 4
  1198. END IF
  1199. !............................................................
  1200. RETURN
  1201. ! ......
  1202. END SUBROUTINE SGEDMD