3D EM based on Schur decomposition
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EMSchur3D.h 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. /* This file is part of Lemma, a geophysical modelling and inversion API.
  2. * More information is available at http://lemmasoftware.org
  3. */
  4. /* This Source Code Form is subject to the terms of the Mozilla Public
  5. * License, v. 2.0. If a copy of the MPL was not distributed with this
  6. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  7. */
  8. /**
  9. * @file
  10. * @date 02/19/2015 01:10:39 PM
  11. * @version $Id$
  12. * @author Trevor Irons (ti)
  13. * @email Trevor.Irons@xri-geo.com
  14. * @copyright Copyright (c) 2015, XRI Geophysics, LLC
  15. * @copyright Copyright (c) 2015, Trevor Irons
  16. * @copyright Copyright (c) 2011, Trevor Irons
  17. * @copyright Copyright (c) 2011, Colorado School of Mines
  18. */
  19. #ifndef EMSCHUR3D_INC
  20. #define EMSCHUR3D_INC
  21. #include "EMSchur3DBase.h"
  22. #include "bicgstab.h"
  23. #ifdef HAVE_SUPERLU
  24. #include "Eigen/SuperLUSupport"
  25. #endif
  26. #ifdef HAVE_UMFPACK
  27. #include <Eigen/UmfPackSupport>
  28. #endif
  29. #ifdef HAVE_PASTIX
  30. #include <Eigen/PaStiXSupport>
  31. #endif
  32. //#include "CSymSimplicialCholesky.h"
  33. namespace Lemma {
  34. /**
  35. \brief Templated concrete classes of EMSChur3DBase.
  36. \details
  37. */
  38. template < class Solver >
  39. class EMSchur3D : public EMSchur3DBase {
  40. friend std::ostream &operator << (std::ostream &stream, const EMSchur3D &ob) {
  41. stream << ob.Serialize() << "\n"; // End of doc
  42. return stream;
  43. }
  44. //friend std::ostream &operator<<(std::ostream &stream,
  45. // const EMSchur3D &ob);
  46. public:
  47. // ==================== LIFECYCLE =======================
  48. /**
  49. * @copybrief LemmaObject::New()
  50. * @copydetails LemmaObject::New()
  51. */
  52. static std::shared_ptr< EMSchur3D > NewSP() {
  53. return std::make_shared< EMSchur3D<Solver> >( ctor_key() );
  54. }
  55. /** Default protected constructor, use New */
  56. explicit EMSchur3D ( const ctor_key& key ) : EMSchur3DBase( key ), CSolver( nullptr ) {
  57. }
  58. /** Locked DeDerializing constructor, use factory DeSerialize method*/
  59. EMSchur3D (const YAML::Node& node, const ctor_key& key): EMSchur3DBase(node, key), CSolver( nullptr ) {
  60. }
  61. /** Default protected destructor, use Delete */
  62. virtual ~EMSchur3D () {
  63. // TODO delete arrays
  64. }
  65. /**
  66. * Uses YAML to serialize this object.
  67. * @return a YAML::Node
  68. */
  69. YAML::Node Serialize() const {
  70. YAML::Node node = EMSchur3DBase::Serialize();
  71. //node["NumberOfLayers"] = NumberOfLayers;
  72. node.SetTag( this->GetName() );
  73. return node;
  74. }
  75. /**
  76. * Constructs an object from a YAML::Node.
  77. */
  78. static EMSchur3D* DeSerialize(const YAML::Node& node);
  79. // ==================== OPERATORS =======================
  80. // ==================== OPERATIONS =======================
  81. /** Solves a single source problem. This method is thread safe.
  82. * @param[in] Source is the source term for generating primary fields
  83. * @param[in] isource is the source index
  84. */
  85. void SolveSource( std::shared_ptr<DipoleSource> Source , const int& isource);
  86. /** Builds the solver for the C matrix */
  87. void BuildCDirectSolver( );
  88. // ==================== ACCESS =======================
  89. virtual std::string GetName() const {
  90. return this->CName;
  91. }
  92. // ==================== INQUIRY =======================
  93. protected:
  94. // ==================== LIFECYCLE =======================
  95. private:
  96. /** Copy constructor */
  97. EMSchur3D( const EMSchur3D& ) = delete;
  98. // ==================== DATA MEMBERS =========================
  99. /** The templated solver for C */
  100. Solver* CSolver;
  101. Eigen::SparseMatrix<Complex> Csym;
  102. static constexpr auto CName = "EMSchur3D";
  103. }; // ----- end of class EMSchur3D -----
  104. ////////////////////////////////////////////////////////////////////////////////////////
  105. // Implimentation and Specialisations //
  106. ////////////////////////////////////////////////////////////////////////////////////////
  107. //--------------------------------------------------------------------------------------
  108. // Class: EMSchur3D
  109. // Method: SolveSource
  110. //--------------------------------------------------------------------------------------
  111. template < class Solver >
  112. void EMSchur3D<Solver>::SolveSource ( std::shared_ptr<DipoleSource> Source, const int& isource ) {
  113. std::cout << "In vanilla SolveSource" << std::endl;
  114. // figure out which omega we are working with
  115. int iw = -1;
  116. for (int iiw=0; iiw<Omegas.size(); ++iiw) {
  117. if (Omegas[iiw] - Source->GetAngularFrequency(0) < 1e-3 ) {
  118. iw = iiw;
  119. }
  120. }
  121. if (iw == -1) {
  122. std::cerr << "FREQUENCY DOOM IN EMSchur3D::SolveSource \n";
  123. exit(EXIT_FAILURE);
  124. }
  125. ///////////////////////////////////
  126. // Set up primary fields
  127. // TODO, this is a little stupid as they all share the same points. We need to extend
  128. // EmEARTH to be able to input a grid so that points are not explicitly needed like
  129. // this. This requires some care as calcs are made on faces.
  130. // Alternatively, the bins function of ReceiverPoints could be extended quite easily.
  131. // This may be the way to do this.
  132. //Lemma::ReceiverPoints* dpoint = Lemma::ReceiverPoints::New();
  133. std::shared_ptr< FieldPoints > dpoint = FieldPoints::NewSP();
  134. FillPoints(dpoint);
  135. PrimaryField(Source, dpoint);
  136. std::cout << "Done with primary field" << std::endl;
  137. // Allocate a ton of memory
  138. VectorXcr Phi = VectorXcr::Zero(uns);
  139. VectorXcr ms(unx+uny+unz); // mu sigma
  140. // Vector potential (A) Vector and phi
  141. VectorXcr Se = VectorXcr::Zero(unx+uny+unz);
  142. //VectorXcr A = VectorXcr::Zero(unx+uny+unz);
  143. VectorXcr E = VectorXcr::Zero(unx+uny+unz);
  144. VectorXcr E0 = VectorXcr::Zero(unx+uny+unz);
  145. // Lets get cracking
  146. std::cout << "Filling source terms" << std::endl;
  147. FillSourceTerms(ms, Se, E0, dpoint, Omegas[iw]);
  148. std::cout << "Done source terms" << std::endl;
  149. /////////////////////////////////////////////////
  150. // LOG File
  151. std::string logfile (ResFile);
  152. logfile += to_string(isource) + std::string(".log");
  153. ofstream logio(logfile.c_str());
  154. std::cout << "just logging, TODO fix source" << std::endl;
  155. // logio << *Source << std::endl;
  156. logio << *Grid << std::endl;
  157. logio << *LayModel << std::endl;
  158. std::cout << "dun logging" << std::endl;
  159. // solve for RHS
  160. int max_it(nx*ny*nz), iter_done(0);
  161. Real tol(3e-16), errorn(0);
  162. logio << "solving RHS for source " << isource << std::endl;
  163. // TODO, this is stupid, try and get rid of this copy!
  164. //Eigen::SparseMatrix<Complex> Cc = Cvec[iw];
  165. jsw_timer timer;
  166. jsw_timer timer2;
  167. timer.begin();
  168. timer2.begin();
  169. /////////////////////////////////////////
  170. // Solve for RHS
  171. //CSolver[iw].setMaxIterations(750);
  172. VectorXcr A = CSolver[iw].solve(Se);
  173. // // Solve Real system instead
  174. // The Real system is quasi-definite, though an LDLT decomposition exists, CHOLMOD doesn't find it.
  175. // An LU can be done on this, but compute performance is very similiar to the complex system, and diagonal pivoting
  176. // cannot be assumed to be best, hurting solve time.
  177. // /* EXPERIMENTAL */
  178. // VectorXr b2 = VectorXr::Zero(2*(unx+uny+unz));
  179. // b2.head(unx+uny+unz) = Se.real();
  180. // b2.tail(unx+uny+unz) = Se.imag();
  181. // VectorXr A2 = CReSolver[iw].solve(b2);
  182. // A.real() = A2.head( unx+uny+unz );
  183. // A.imag() = -A2.tail( unx+uny+unz ); // Due to decomp. negative!
  184. // /* END EXPERIMENTAL */
  185. VectorXcr ADiv = D*A; // ADiv == RHS == D C^I Se
  186. //VectorXcr Error = ((Cc.selfadjointView<Eigen::Lower>()*A).array() - Se.array());
  187. VectorXcr Error = ((Cvec[iw].selfadjointView<Eigen::Lower>()*A).array() - Se.array());
  188. logio << "|| Div(A) || = " << ADiv.norm()
  189. << "\tInital solution error="<< Error.norm() // Iteritive info
  190. // << "\tSolver reported error="<< CSolver[iw].error() // Iteritive info
  191. << "\ttime " << timer.end() / 60. << " [m] "
  192. // << CSolver[iw].iterations() << " iterations"
  193. << std::endl;
  194. //VectorXcr ADivMAC = ADiv.array() * MAC.array().cast<Complex>();
  195. //logio << "|| Div(A) || on MAC grid " << ADivMAC.norm() << std::endl;
  196. /////////////////////
  197. // Solve for Phi
  198. logio << "Solving for Phi " << std::flush;
  199. timer.begin();
  200. tol = 1e-20;
  201. int success(2);
  202. success = implicitbicgstab(D, idx, ms, ADiv, Phi, CSolver[iw], max_it, tol, errorn, iter_done, logio);
  203. //Phi.array() *= MAC.array().cast<Complex>(); // remove phi from air regions
  204. /* Restart if necessary */
  205. int nrestart(1);
  206. // TODO send MAC to implicitbicgstab?
  207. while (success == 2 && nrestart < 18 && iter_done > 1) {
  208. success = implicitbicgstab(D, idx, ms, ADiv, Phi, CSolver[iw], max_it, tol, errorn, iter_done, logio);
  209. //Phi.array() *= MAC.array().cast<Complex>(); // remove phi from air regions
  210. nrestart += 1;
  211. }
  212. logio << "Implicit BiCGStab solution in " << iter_done << " iterations."
  213. << " with error " << std::setprecision(8) << std::scientific << errorn << std::endl;
  214. logio << "time "<< timer.end()/60. << " [m]" << std::endl;
  215. E = ms.array()*(D.transpose()*Phi).array(); // Temp, field due to charge
  216. /////////////////////////////////////
  217. // Compute A
  218. /////////////////////////////////////
  219. logio << "Solving for A using phi" << std::endl;
  220. std::cout << "Solving for A" << std::endl;
  221. max_it = nx*ny*nz;
  222. tol = 5e-16;
  223. errorn = 0;
  224. iter_done = 0;
  225. timer.begin();
  226. A = CSolver[iw].solve( (Se-E).eval() ); // UmfPack requires eval?
  227. VectorXcr ADiv2 = D*A;
  228. //logio << "|| Div(A) || = " << ADiv2.norm() ;
  229. //" in " << iter_done << " iterations"
  230. //<< " with error " << errorn << "\t";
  231. // Report error of solutions
  232. //Error = ((Cc.selfadjointView<Eigen::Lower>()*A).array() + E.array() - Se.array());
  233. Error = ((Cvec[iw].selfadjointView<Eigen::Lower>()*A).array() + E.array() - Se.array());
  234. // << "\tSolver reported error="<< CSolver[iw].error() // Iteritive info
  235. // << "\ttime " << timer.end() / 60. << " [m] "
  236. // << CSolver[iw].iterations() << " iterations"
  237. logio << "|| Div(A) || = " << ADiv2.norm()
  238. << "\tSolution error="<< Error.norm() // Iteritive info
  239. // << "\tSolver reported error="<< CSolver[iw].error() // Iteritive info
  240. << "\ttime " << timer.end() / 60. << " [m] "
  241. // << CSolver[iw].iterations() << " iterations"
  242. << std::endl << std::endl;
  243. logio << std::fixed << std::setprecision(2) << "\ttime " << timer.end()/60. << "\ttotal time " << timer2.end()/60. << std::endl;
  244. logio.close();
  245. //////////////////////////////////////
  246. // Update Fields and report
  247. E.array() = Complex(0,-Omegas[iw])*A.array() - (D.transpose()*Phi).array(); // Secondary Field Only
  248. VectorXcr B = StaggeredGridCurl(A);
  249. WriteVTKResults( ResFile+ to_string(isource), A, Se, E0, E , Phi, ADiv, ADiv2, B);
  250. //dpoint->Delete();
  251. return ;
  252. } // ----- end of method EMSchur3D::SolveSource -----
  253. //--------------------------------------------------------------------------------------
  254. // Class: EMSchur3DBase
  255. // Method: BuildCDirectSolver
  256. //--------------------------------------------------------------------------------------
  257. template < class Solver >
  258. void EMSchur3D<Solver>::BuildCDirectSolver ( ) {
  259. CSolver = new Solver[Omegas.size()];
  260. for (int iw=0; iw<Omegas.size(); ++iw) {
  261. jsw_timer timer;
  262. timer.begin();
  263. /* Complex system */
  264. /*
  265. std::cout << "Generic solver pattern analyzing C_" << iw << ",";
  266. std::cout.flush();
  267. CSolver[iw].analyzePattern( Cvec[iw].selfadjointView< Eigen::Lower>() );
  268. std::cout << " done in " << timer.end() / 60. << " [m]" << std::endl;
  269. // factorize
  270. timer.begin();
  271. std::cout << "Generic solver factorising C_" << iw << ", ";
  272. std::cout.flush();
  273. CSolver[iw].factorize( Cvec[iw].selfadjointView< Eigen::Lower>() );
  274. */
  275. std::cerr << "No solver Specified!" << iw << ",";
  276. exit(EXIT_FAILURE);
  277. //CSolver[iw].compute( Cvec[iw].selfadjointView< Eigen::Lower>() );
  278. std::cout << " done in " << timer.end() / 60. << " [m]" << std::endl;
  279. }
  280. }
  281. #ifdef HAVE_SUPERLU
  282. template<>
  283. void EMSchur3D< Eigen::SuperLU<Eigen::SparseMatrix<Complex, Eigen::ColMajor> > >::BuildCDirectSolver() {
  284. CSolver = new Eigen::SuperLU<Eigen::SparseMatrix<Complex, Eigen::ColMajor> > [Omegas.size()];
  285. for (int iw=0; iw<Omegas.size(); ++iw) {
  286. jsw_timer timer;
  287. timer.begin();
  288. /* SuperLU */
  289. // Recommended values for symmetric mode
  290. CSolver[iw].options().DiagPivotThresh = 0.01;
  291. CSolver[iw].options().SymmetricMode = YES;
  292. CSolver[iw].options().ColPerm = MMD_AT_PLUS_A;
  293. //CSolver[iw].options().Trans = NOTRANS;
  294. //CSolver[iw].options().ConditionNumber = NO;
  295. //std::cout << "SuperLU options:\n";
  296. //std::cout << "\tPivot Threshold: " << CSolver[iw].options().DiagPivotThresh << std::endl;
  297. //std::cout << "\tSymmetric mode: " << CSolver[iw].options().SymmetricMode << std::endl;
  298. //std::cout << "\tEquilibrate: " << CSolver[iw].options().Equil << std::endl;
  299. //std::cout << "\tCol Permutation: " << CSolver[iw].options().ColPerm << std::endl;
  300. //std::cout << "\tTrans: " << CSolver[iw].options().Trans << std::endl;
  301. //std::cout << "\tCondition Number: " << CSolver[iw].options().ConditionNumber << std::endl;
  302. /* Complex system */
  303. std::cout << "SuperLU pattern analyzing C_" << iw << ",";
  304. std::cout.flush();
  305. CSolver[iw].analyzePattern( Cvec[iw].selfadjointView< Eigen::Lower>() );
  306. std::cout << " done in " << timer.end() / 60. << " [m]" << std::endl;
  307. // factorize
  308. timer.begin();
  309. std::cout << "SuperLU factorising C_" << iw << ", ";
  310. std::cout.flush();
  311. CSolver[iw].factorize( Cvec[iw].selfadjointView< Eigen::Lower>() );
  312. std::cout << " done in " << timer.end() / 60. << " [m]" << std::endl;
  313. }
  314. }
  315. #endif
  316. template<>
  317. void EMSchur3D< Eigen::SparseLU<Eigen::SparseMatrix<Complex, Eigen::ColMajor>, Eigen::COLAMDOrdering<int> > >::BuildCDirectSolver() {
  318. CSolver = new Eigen::SparseLU<Eigen::SparseMatrix<Complex, Eigen::ColMajor>, Eigen::COLAMDOrdering<int> > [Omegas.size()];
  319. for (int iw=0; iw<Omegas.size(); ++iw) {
  320. jsw_timer timer;
  321. timer.begin();
  322. CSolver[iw].isSymmetric(true);
  323. //CSolver[iw].setPivotThreshold(0.0); // OK for symmetric complex systems with real and imaginary positive definite parts.
  324. // // but our imaginary part is negative definite
  325. //http://www.ams.org/journals/mcom/1998-67-224/S0025-5718-98-00978-8/S0025-5718-98-00978-8.pdf
  326. /* Complex system */
  327. std::cout << "SparseLU pattern analyzing C_" << iw << ",";
  328. std::cout.flush();
  329. CSolver[iw].analyzePattern( Cvec[iw].selfadjointView< Eigen::Lower>() );
  330. std::cout << " done in " << timer.end() / 60. << " [m]" << std::endl;
  331. // factorize
  332. timer.begin();
  333. std::cout << "SparseLU factorising C_" << iw << ", ";
  334. std::cout.flush();
  335. CSolver[iw].factorize( Cvec[iw].selfadjointView< Eigen::Lower>() );
  336. std::cout << " done in " << timer.end() / 60. << " [m]" << std::endl;
  337. }
  338. }
  339. #ifdef HAVE_UMFPACK
  340. // Umfpack only seems to work when LOWER and UPPER are set to 1. Workarounds this have not been found.
  341. template<>
  342. void EMSchur3D< Eigen::UmfPackLU<Eigen::SparseMatrix<Complex, Eigen::ColMajor> > >::BuildCDirectSolver() {
  343. CSolver = new Eigen::UmfPackLU<Eigen::SparseMatrix<Complex, Eigen::ColMajor> > [Omegas.size()];
  344. for (int iw=0; iw<Omegas.size(); ++iw) {
  345. jsw_timer timer;
  346. timer.begin();
  347. /* Complex system */
  348. std::cout << "UmfPackLU pattern analyzing C_" << iw << ",";
  349. std::cout.flush();
  350. // Doesn't work, seg faults in solve
  351. //Eigen::SparseMatrix<Complex> Csym = Cvec[iw].selfadjointView< Eigen::Lower >();
  352. // Compiler errors get thrown with the view setting, good performance if LOWER and UPPER are set
  353. // CSolver[iw].analyzePattern( Cvec[iw].selfadjointView< Eigen::Lower>() ); // Compiler error
  354. CSolver[iw].analyzePattern( Cvec[iw] ); // requires LOWER=1 UPPER=1, double memory
  355. // CSolver[iw].analyzePattern( Csym ); // seg faults
  356. std::cout << " done in " << timer.end() / 60. << " [m]" << std::endl;
  357. // factorize
  358. timer.begin();
  359. std::cout << "UmfPackLU factorising C_" << iw << ", ";
  360. std::cout.flush();
  361. // CSolver[iw].factorize( Cvec[iw].selfadjointView< Eigen::Lower>() );
  362. CSolver[iw].factorize( Cvec[iw] );
  363. // CSolver[iw].factorize( Csym );
  364. std::cout << " done in " << timer.end() / 60. << " [m]" << std::endl;
  365. }
  366. }
  367. #endif
  368. // template<>
  369. // void EMSchur3D< Eigen::CholmodSupernodalLLT< Eigen::SparseMatrix<Complex, Eigen::ColMajor>, Eigen::Lower > > ::BuildCDirectSolver() {
  370. // CSolver = new Eigen::CholmodSupernodalLLT< Eigen::SparseMatrix<Complex, Eigen::ColMajor>, Eigen::Lower > [Omegas.size()];
  371. // for (int iw=0; iw<Omegas.size(); ++iw) {
  372. // Csym = Cvec[iw].selfadjointView<Eigen::Lower>();
  373. // jsw_timer timer;
  374. // timer.begin();
  375. // /* Complex system */
  376. // std::cout << "CholmodSupernodalLLT pattern analyzing C_" << iw << ",";
  377. // std::cout.flush();
  378. // CSolver[iw].analyzePattern( Csym );
  379. // std::cout << " done in " << timer.end() / 60. << " [m]" << std::endl;
  380. // /* factorize */
  381. // timer.begin();
  382. // std::cout << "CholmodSupernodalLLT factorising C_" << iw << ", ";
  383. // std::cout.flush();
  384. // CSolver[iw].factorize( Csym );
  385. // std::cout << " done in " << timer.end() / 60. << " [m]" << std::endl;
  386. // }
  387. // }
  388. // template<>
  389. // void EMSchur3D< Eigen::CSymSimplicialLLT< Eigen::SparseMatrix<Complex, Eigen::ColMajor>, Eigen::Lower, Eigen::NaturalOrdering<int> > > ::BuildCDirectSolver() {
  390. // CSolver = new Eigen::CSymSimplicialLLT< Eigen::SparseMatrix<Complex, Eigen::ColMajor>, Eigen::Lower, Eigen::NaturalOrdering<int> > [Omegas.size()];
  391. // for (int iw=0; iw<Omegas.size(); ++iw) {
  392. // Csym = Cvec[iw].selfadjointView<Eigen::Lower>();
  393. // jsw_timer timer;
  394. // timer.begin();
  395. // /* Complex system */
  396. // std::cout << "CSymSimplicialLLT<NaturalOrdering> pattern analyzing C_" << iw << ",";
  397. // std::cout.flush();
  398. // CSolver[iw].analyzePattern( Csym );
  399. // std::cout << " done in " << timer.end() / 60. << " [m]" << std::endl;
  400. // /* factorize */
  401. // timer.begin();
  402. // std::cout << "CSymSimplicialLLT<NaturalOrdering> factorising C_" << iw << ", ";
  403. // std::cout.flush();
  404. // CSolver[iw].factorize( Csym );
  405. // std::cout << " done in " << timer.end() / 60. << " [m]" << std::endl;
  406. // }
  407. // }
  408. //
  409. // template<>
  410. // void EMSchur3D< Eigen::CSymSimplicialLLT< Eigen::SparseMatrix<Complex, Eigen::ColMajor>, Eigen::Lower, Eigen::AMDOrdering<int> > > ::BuildCDirectSolver() {
  411. // CSolver = new Eigen::CSymSimplicialLLT< Eigen::SparseMatrix<Complex, Eigen::ColMajor>, Eigen::Lower, Eigen::AMDOrdering<int> > [Omegas.size()];
  412. // for (int iw=0; iw<Omegas.size(); ++iw) {
  413. // //Csym = Cvec[iw].selfadjointView<Eigen::Lower>();
  414. // jsw_timer timer;
  415. // timer.begin();
  416. // /* Complex system */
  417. // std::cout << "CSymSimplicialLLT<AMDOrdering> pattern analyzing C_" << iw << ",";
  418. // std::cout.flush();
  419. // CSolver[iw].analyzePattern( Cvec[iw] );
  420. // std::cout << " done in " << timer.end() / 60. << " [m]" << std::endl;
  421. // /* factorize */
  422. // timer.begin();
  423. // std::cout << "CSymSimplicialLLT<AMDOrdering> factorising C_" << iw << ", ";
  424. // std::cout.flush();
  425. // CSolver[iw].factorize( Cvec[iw] );
  426. // std::cout << " done in " << timer.end() / 60. << " [m]" << std::endl;
  427. // }
  428. // }
  429. //
  430. // template<>
  431. // void EMSchur3D< Eigen::CSymSimplicialLDLT< Eigen::SparseMatrix<Complex, Eigen::ColMajor>, Eigen::Lower, Eigen::AMDOrdering<int> > > ::BuildCDirectSolver() {
  432. // CSolver = new Eigen::CSymSimplicialLDLT< Eigen::SparseMatrix<Complex, Eigen::ColMajor>, Eigen::Lower, Eigen::AMDOrdering<int> > [Omegas.size()];
  433. // for (int iw=0; iw<Omegas.size(); ++iw) {
  434. // Csym = Cvec[iw].selfadjointView<Eigen::Lower>();
  435. // jsw_timer timer;
  436. // timer.begin();
  437. // /* Complex system */
  438. // std::cout << "CSymSimplicialLDLT<AMDOrdering> pattern analyzing C_" << iw << ",";
  439. // std::cout.flush();
  440. // CSolver[iw].analyzePattern( Csym );
  441. // std::cout << " done in " << timer.end() / 60. << " [m]" << std::endl;
  442. // /* factorize */
  443. // timer.begin();
  444. // std::cout << "CSymSimplicialLDLT<AMDOrdering> factorising C_" << iw << ", ";
  445. // std::cout.flush();
  446. // CSolver[iw].factorize( Csym );
  447. // std::cout << " done in " << timer.end() / 60. << " [m]" << std::endl;
  448. // }
  449. // }
  450. template<>
  451. void EMSchur3D< Eigen::BiCGSTAB<Eigen::SparseMatrix<Complex, Eigen::ColMajor>, Eigen::IncompleteLUT<Complex> > > ::BuildCDirectSolver() {
  452. CSolver = new Eigen::BiCGSTAB<Eigen::SparseMatrix<Complex, Eigen::ColMajor>, Eigen::IncompleteLUT<Complex> > [Omegas.size()];
  453. for (int iw=0; iw<Omegas.size(); ++iw) {
  454. Csym = Cvec[iw].selfadjointView<Eigen::Lower>();
  455. CSolver[iw].preconditioner().setDroptol(1e-6); //1e-5); // 1e-12
  456. CSolver[iw].preconditioner().setFillfactor(5e1); // 1e2
  457. jsw_timer timer;
  458. timer.begin();
  459. /* Complex system */
  460. std::cout << "BiCGSTAB(ILU) pattern analyzing C_" << iw << ",";
  461. std::cout.flush();
  462. CSolver[iw].analyzePattern( Csym );
  463. //CSolver[iw].analyzePattern( Cvec[iw]);
  464. std::cout << " done in " << timer.end() / 60. << " [m]" << std::endl;
  465. /* factorize */
  466. timer.begin();
  467. std::cout << "BiCGSTAB(ILU) factorising C_" << iw << ", ";
  468. std::cout.flush();
  469. CSolver[iw].factorize( Csym );
  470. //CSolver[iw].factorize( Cvec[iw] );
  471. std::cout << " done in " << timer.end() / 60. << " [m]" << std::endl;
  472. }
  473. }
  474. template<>
  475. void EMSchur3D< Eigen::BiCGSTAB< Eigen::SparseMatrix<Complex, Eigen::ColMajor> > > ::BuildCDirectSolver() {
  476. CSolver = new Eigen::BiCGSTAB< Eigen::SparseMatrix<Complex, Eigen::ColMajor> > [Omegas.size()];
  477. for (int iw=0; iw<Omegas.size(); ++iw) {
  478. Csym = Cvec[iw].selfadjointView<Eigen::Lower>();
  479. jsw_timer timer;
  480. timer.begin();
  481. /* Complex system */
  482. std::cout << "BiCGSTAB pattern analyzing C_" << iw << ",";
  483. std::cout.flush();
  484. CSolver[iw].analyzePattern( Csym );
  485. std::cout << " done in " << timer.end() / 60. << " [m]" << std::endl;
  486. // factorize
  487. timer.begin();
  488. std::cout << "BiCGSTAB factorising C_" << iw << ", ";
  489. std::cout.flush();
  490. CSolver[iw].factorize( Csym );
  491. std::cout << " done in " << timer.end() / 60. << " [m]" << std::endl;
  492. }
  493. }
  494. template<>
  495. void EMSchur3D< Eigen::LeastSquaresConjugateGradient< Eigen::SparseMatrix<Complex, Eigen::ColMajor> > > ::BuildCDirectSolver() {
  496. CSolver = new Eigen::LeastSquaresConjugateGradient< Eigen::SparseMatrix<Complex, Eigen::ColMajor> > [Omegas.size()];
  497. for (int iw=0; iw<Omegas.size(); ++iw) {
  498. Csym = Cvec[iw].selfadjointView<Eigen::Lower>();
  499. jsw_timer timer;
  500. timer.begin();
  501. /* Complex system */
  502. std::cout << "LeastSquaresConjugateGradient pattern analyzing C_" << iw << ",";
  503. std::cout.flush();
  504. CSolver[iw].analyzePattern( Csym );
  505. std::cout << " done in " << timer.end() / 60. << " [m]" << std::endl;
  506. // factorize
  507. timer.begin();
  508. std::cout << "LeastSquaresConjugateGradient factorising C_" << iw << ", ";
  509. std::cout.flush();
  510. CSolver[iw].factorize( Csym );
  511. std::cout << " done in " << timer.end() / 60. << " [m]" << std::endl;
  512. }
  513. }
  514. template<>
  515. void EMSchur3D< Eigen::ConjugateGradient<Eigen::SparseMatrix<Complex, Eigen::ColMajor>, Eigen::Lower > > ::BuildCDirectSolver() {
  516. CSolver = new Eigen::ConjugateGradient<Eigen::SparseMatrix<Complex, Eigen::ColMajor>, Eigen::Lower > [Omegas.size()];
  517. for (int iw=0; iw<Omegas.size(); ++iw) {
  518. //Csym = Cvec[iw].selfadjointView<Eigen::Lower>();
  519. jsw_timer timer;
  520. timer.begin();
  521. /* Complex system */
  522. std::cout << "ConjugateGradient pattern analyzing C_" << iw << ",";
  523. std::cout.flush();
  524. CSolver[iw].analyzePattern( Cvec[iw] );
  525. std::cout << " done in " << timer.end() / 60. << " [m]" << std::endl;
  526. // factorize
  527. timer.begin();
  528. std::cout << "ConjugateGradient factorising C_" << iw << ", ";
  529. std::cout.flush();
  530. CSolver[iw].factorize( Cvec[iw] );
  531. std::cout << " done in " << timer.end() / 60. << " [m]" << std::endl;
  532. }
  533. }
  534. template<>
  535. void EMSchur3D< Eigen::ConjugateGradient<Eigen::SparseMatrix<Complex, Eigen::ColMajor>, Eigen::Lower, Eigen::IncompleteCholesky<Complex> > > ::BuildCDirectSolver() {
  536. CSolver = new Eigen::ConjugateGradient<Eigen::SparseMatrix<Complex, Eigen::ColMajor>, Eigen::Lower, Eigen::IncompleteCholesky<Complex> > [Omegas.size()];
  537. for (int iw=0; iw<Omegas.size(); ++iw) {
  538. //Csym = Cvec[iw].selfadjointView<Eigen::Lower>();
  539. jsw_timer timer;
  540. timer.begin();
  541. /* Complex system */
  542. std::cout << "ConjugateGradient<IncompleteCholesky> pattern analyzing C_" << iw << ",";
  543. std::cout.flush();
  544. CSolver[iw].analyzePattern( Cvec[iw] );
  545. std::cout << " done in " << timer.end() / 60. << " [m]" << std::endl;
  546. // factorize
  547. timer.begin();
  548. std::cout << "ConjugateGradient<IncompleteCholesky> factorising C_" << iw << ", ";
  549. std::cout.flush();
  550. CSolver[iw].factorize( Cvec[iw] );
  551. std::cout << " done in " << timer.end() / 60. << " [m]" << std::endl;
  552. }
  553. }
  554. // template<>
  555. // void EMSchur3D< Eigen::PastixLLT<Eigen::SparseMatrix<Complex, Eigen::ColMajor>, Eigen::Lower > > ::BuildCDirectSolver() {
  556. // CSolver = new Eigen::PastixLLT<Eigen::SparseMatrix<Complex, Eigen::ColMajor>, Eigen::Lower > [Omegas.size()];
  557. // //MPI_Init(NULL, NULL);
  558. // for (int iw=0; iw<Omegas.size(); ++iw) {
  559. // //Csym = Cvec[iw].selfadjointView<Eigen::Lower>();
  560. // jsw_timer timer;
  561. // timer.begin();
  562. // /* Complex system */
  563. // std::cout << "PaStiX LLT pattern analyzing C_" << iw << ",";
  564. // std::cout.flush();
  565. // CSolver[iw].analyzePattern( Cvec[iw] );
  566. // std::cout << " done in " << timer.end() / 60. << " [m]" << std::endl;
  567. // // factorize
  568. // timer.begin();
  569. // std::cout << "PaStiX LLT factorising C_" << iw << ", ";
  570. // std::cout.flush();
  571. // CSolver[iw].factorize( Cvec[iw] );
  572. // std::cout << " done in " << timer.end() / 60. << " [m]" << std::endl;
  573. // }
  574. // }
  575. //
  576. // template<>
  577. // void EMSchur3D< Eigen::PastixLDLT<Eigen::SparseMatrix<Complex, Eigen::ColMajor>, Eigen::Lower > > ::BuildCDirectSolver() {
  578. // CSolver = new Eigen::PastixLDLT<Eigen::SparseMatrix<Complex, Eigen::ColMajor>, Eigen::Lower > [Omegas.size()];
  579. // //MPI_Init(NULL, NULL);
  580. // for (int iw=0; iw<Omegas.size(); ++iw) {
  581. // //Csym = Cvec[iw].selfadjointView<Eigen::Lower>();
  582. // jsw_timer timer;
  583. // timer.begin();
  584. // /* Complex system */
  585. // std::cout << "PaStiX LDLT pattern analyzing C_" << iw << ",";
  586. // std::cout.flush();
  587. // CSolver[iw].analyzePattern( Cvec[iw] );
  588. // std::cout << " done in " << timer.end() / 60. << " [m]" << std::endl;
  589. // // factorize
  590. // timer.begin();
  591. // std::cout << "PaStiX LDLT factorising C_" << iw << ", ";
  592. // std::cout.flush();
  593. // CSolver[iw].factorize( Cvec[iw] );
  594. // std::cout << " done in " << timer.end() / 60. << " [m]" << std::endl;
  595. // std::cout << "INFO " << CSolver[iw].info( ) << std::endl;
  596. // }
  597. // }
  598. //
  599. #ifdef HAVE_PASTIX
  600. template<>
  601. void EMSchur3D< Eigen::PastixLU<Eigen::SparseMatrix<Complex, Eigen::ColMajor>, true > > ::BuildCDirectSolver() {
  602. CSolver = new Eigen::PastixLU<Eigen::SparseMatrix<Complex, Eigen::ColMajor>, true > [Omegas.size()];
  603. for (int iw=0; iw<Omegas.size(); ++iw) {
  604. jsw_timer timer;
  605. timer.begin();
  606. /* Complex system */
  607. std::cout << "PastixLU pattern analyzing C_" << iw << ",";
  608. std::cout.flush();
  609. CSolver[iw].analyzePattern( Cvec[iw].selfadjointView< Eigen::Lower>() );
  610. //CSolver[iw].analyzePattern( Cvec[iw] );
  611. std::cout << " done in " << timer.end() / 60. << " [m]" << std::endl;
  612. // factorize
  613. timer.begin();
  614. std::cout << "PastixLU factorising C_" << iw << ", ";
  615. std::cout.flush();
  616. CSolver[iw].factorize( Cvec[iw].selfadjointView< Eigen::Lower>() );
  617. //CSolver[iw].factorize( Cvec[iw] );
  618. std::cout << " done in " << timer.end() / 60. << " [m]" << std::endl;
  619. }
  620. }
  621. #endif
  622. } // ----- end of Lemma name -----
  623. #endif // ----- #ifndef EMSCHUR3D_INC -----