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.

dgedmd.f90 50 kB

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