Galerkin FEM for elliptic PDEs
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

FEM4EllipticPDE.cpp 40KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  1. // ===========================================================================
  2. //
  3. // Filename: FEM4EllipticPDE.cpp
  4. //
  5. // Created: 08/16/12 18:19:57
  6. // Compiler: Tested with g++, icpc, and MSVC 2010
  7. //
  8. // Author: Trevor Irons (ti)
  9. //
  10. // Organisation: Colorado School of Mines (CSM)
  11. // United States Geological Survey (USGS)
  12. //
  13. // Email: tirons@mines.edu, tirons@usgs.gov
  14. //
  15. // This program is free software: you can redistribute it and/or modify
  16. // it under the terms of the GNU General Public License as published by
  17. // the Free Software Foundation, either version 3 of the License, or
  18. // (at your option) any later version.
  19. //
  20. // This program is distributed in the hope that it will be useful,
  21. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. // GNU General Public License for more details.
  24. //
  25. // You should have received a copy of the GNU General Public License
  26. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. //
  28. // ===========================================================================
  29. /**
  30. @file
  31. @author Trevor Irons
  32. @date 08/16/12
  33. @version 0.0
  34. **/
  35. #include "FEM4EllipticPDE.h"
  36. namespace Lemma {
  37. std::ostream &operator<<(std::ostream &stream,
  38. const FEM4EllipticPDE &ob) {
  39. stream << *(LemmaObject*)(&ob);
  40. return stream;
  41. }
  42. // ==================== LIFECYCLE =======================
  43. FEM4EllipticPDE::FEM4EllipticPDE(const std::string&name) :
  44. LemmaObject(name), BndryH(10), BndrySigma(10),
  45. vtkSigma(NULL), vtkG(NULL), vtkGrid(NULL), gFcn3(NULL) {
  46. }
  47. FEM4EllipticPDE::~FEM4EllipticPDE() {
  48. }
  49. void FEM4EllipticPDE::Release() {
  50. delete this;
  51. }
  52. FEM4EllipticPDE* FEM4EllipticPDE::New( ) {
  53. FEM4EllipticPDE* Obj = new FEM4EllipticPDE("FEM4EllipticPDE");
  54. Obj->AttachTo(Obj);
  55. return Obj;
  56. }
  57. void FEM4EllipticPDE::Delete() {
  58. this->DetachFrom(this);
  59. }
  60. // ==================== OPERATIONS =======================
  61. void FEM4EllipticPDE::SetSigmaFunction(vtkImplicitFunction* sigma) {
  62. vtkSigma = sigma;
  63. }
  64. void FEM4EllipticPDE::SetBoundaryStep(const Real& h) {
  65. BndryH = h;
  66. }
  67. void FEM4EllipticPDE::SetGFunction(vtkImplicitFunction* g) {
  68. vtkG = g;
  69. }
  70. void FEM4EllipticPDE::SetGFunction( Real (*gFcn)(const Real&, const Real&, const Real&) ) {
  71. // vtkG = g;
  72. gFcn3 = gFcn;
  73. }
  74. void FEM4EllipticPDE::SetGrid(vtkDataSet* grid) {
  75. vtkGrid = grid;
  76. }
  77. vtkSmartPointer<vtkIdList> FEM4EllipticPDE::GetConnectedPoints(const int& id0) {
  78. vtkSmartPointer<vtkIdList> pointIds = vtkSmartPointer<vtkIdList>::New();
  79. vtkSmartPointer<vtkIdList> cellList = vtkSmartPointer<vtkIdList>::New();
  80. vtkGrid->GetPointCells(id0, cellList);
  81. for(int i=0;i<cellList->GetNumberOfIds(); ++i){
  82. vtkCell* cell = vtkGrid->GetCell(cellList->GetId(i));
  83. if(cell->GetNumberOfEdges() > 0){
  84. for(int j=0; j<cell->GetNumberOfEdges(); ++j){
  85. vtkCell* edge = cell->GetEdge(j);
  86. vtkIdList* edgePoints=edge->GetPointIds();
  87. if(edgePoints->GetId(0)==id0){
  88. pointIds->InsertUniqueId(edgePoints->GetId(1));
  89. } else if(edgePoints->GetId(1)==id0){
  90. pointIds->InsertUniqueId(edgePoints->GetId(0));
  91. }
  92. }
  93. }
  94. }
  95. return pointIds;
  96. }
  97. Real FEM4EllipticPDE::dist(Real r0[3], Real r1[3]) {
  98. Real rm0 = r1[0] - r0[0];
  99. Real rm1 = r1[1] - r0[1];
  100. Real rm2 = r1[2] - r0[2];
  101. return std::sqrt( rm0*rm0 + rm1*rm1 + rm2*rm2 );
  102. }
  103. Real FEM4EllipticPDE::dist(const Vector3r& r0, const Vector3r& r1) {
  104. Real rm0 = r1[0] - r0[0];
  105. Real rm1 = r1[1] - r0[1];
  106. Real rm2 = r1[2] - r0[2];
  107. return std::sqrt( rm0*rm0 + rm1*rm1 + rm2*rm2 );
  108. }
  109. //--------------------------------------------------------------------------------------
  110. // Class: FEM4EllipticPDE
  111. // Method: SetupDC
  112. //--------------------------------------------------------------------------------------
  113. void FEM4EllipticPDE::SetupDC ( DCSurvey* Survey, const int& ij ) {
  114. ////////////////////////////////////////////////////////////
  115. // Load vector g, solution vector u
  116. std::cout << "\nBuilding load vector (g)" << std::endl;
  117. g = VectorXr::Zero(vtkGrid->GetNumberOfPoints());
  118. std::cout << "made g with " << vtkGrid->GetNumberOfPoints() << " pnts" << std::endl;
  119. int iia(0);
  120. Real jja(0);
  121. Survey->GetA( ij, iia, jja );
  122. //g(ii) = jj;
  123. int iib(0);
  124. Real jjb(0);
  125. Survey->GetB( ij, iib, jjb );
  126. //g(ii) = jj;
  127. /* 3D Phi */
  128. for (int ic=0; ic < vtkGrid->GetNumberOfCells(); ++ic) {
  129. // Eigen::Matrix<Real, 4, 4> C = Eigen::Matrix<Real, 4, 4>::Zero() ;
  130. // for (int ii=0; ii<4; ++ii) {
  131. // double* pts = vtkGrid->GetCell(ic)->GetPoints()->GetPoint(ii); //[ipc] ;
  132. // C(ii, 0) = 1;
  133. // C(ii, 1) = pts[0];
  134. // C(ii, 2) = pts[1];
  135. // C(ii, 3) = pts[2];
  136. // }
  137. // Real V = (1./6.) * C.determinant(); // volume of tetrahedra
  138. //
  139. vtkIdList* Ids = vtkGrid->GetCell(ic)->GetPointIds();
  140. int ID[4];
  141. ID[0] = Ids->GetId(0);
  142. ID[1] = Ids->GetId(1);
  143. ID[2] = Ids->GetId(2);
  144. ID[3] = Ids->GetId(3);
  145. //Real V = C.determinant(); // volume of tetrahedra
  146. Real sum(0);
  147. if (ID[0] == iia || ID[1] == iia || ID[2] == iia || ID[3] == iia ) {
  148. std::cout << "Caught A electrode, injecting " << iia << std::endl;
  149. //sum = 10;
  150. //g(ID[iia]) += jja;
  151. g(iia) += jja;
  152. }
  153. if (ID[0] == iib || ID[1] == iib || ID[2] == iib || ID[3] == iib) {
  154. //sum = -10;
  155. std::cout << "Caught B electrode, injecting " << iib << std::endl;
  156. //g(ID[iib]) += jjb;
  157. g(iib) += jjb;
  158. }
  159. //g(ID[0]) = sum; //(V/4.) * sum; // Why 3, Why!!!?
  160. std::cout << "\r" << (int)(1e2*((float)(ic) / (float)(vtkGrid->GetNumberOfCells()))) << std::flush ;
  161. }
  162. return ;
  163. } // ----- end of method FEM4EllipticPDE::SetupDC -----
  164. void FEM4EllipticPDE::Solve( const std::string& resfile ) {
  165. ConstructAMatrix();
  166. SetupPotential();
  167. //ConstructLoadVector();
  168. std::cout << "\nSolving" << std::endl;
  169. std::cout << " rows\tcols\n";
  170. std::cout << "A: " << A.rows() << "\t" << A.cols() << std::endl;
  171. std::cout << "g: " << g.rows() << "\t" << g.cols() << std::endl;
  172. ////////////////////////////////////////////////////////////
  173. // Solving:
  174. //Eigen::SimplicialCholesky<Eigen::SparseMatrix<Real>, Eigen::Lower > chol(A); // performs a Cholesky factorization of A
  175. //VectorXr u = chol.solve(g);
  176. //Eigen::SparseLU<Eigen::SparseMatrix<Real, Eigen::ColMajor>, Eigen::COLAMDOrdering<int> > solver;
  177. //solver.analyzePattern(A);
  178. //solver.factorize(A);
  179. //VectorXr u = solver.solve(g);
  180. //#ifdef CGSOLVE
  181. //Eigen::ConjugateGradient<Eigen::SparseMatrix<Real, Eigen::Lower > Eigen::DiagonalPreconditioner > cg;
  182. Eigen::ConjugateGradient< Eigen::SparseMatrix<Real> > cg(A);
  183. //Eigen::BiCGSTAB<Eigen::SparseMatrix<Real> > cg(A);
  184. //cg.setMaxIterations(3000);
  185. //cg.setTolerance(1e-28);
  186. VectorXr u = cg.solve(g);
  187. std::cout << "#iterations: " << cg.iterations() << std::endl;
  188. std::cout << "estimated error: " << cg.error() << std::endl;
  189. //#endif
  190. vtkDoubleArray *gArray = vtkDoubleArray::New();
  191. vtkDoubleArray *uArray = vtkDoubleArray::New();
  192. uArray->SetNumberOfComponents(1);
  193. gArray->SetNumberOfComponents(1);
  194. for (int iu = 0; iu<u.size(); ++iu) {
  195. uArray->InsertTuple1(iu, u[iu]);
  196. gArray->InsertTuple1(iu, g[iu]);
  197. }
  198. uArray->SetName("u");
  199. gArray->SetName("g");
  200. vtkGrid->GetPointData()->AddArray(uArray);
  201. vtkGrid->GetPointData()->AddArray(gArray);
  202. vtkXMLDataSetWriter *Writer = vtkXMLDataSetWriter::New();
  203. Writer->SetInputData(vtkGrid);
  204. Writer->SetFileName(resfile.c_str());
  205. Writer->Write();
  206. Writer->Delete();
  207. gArray->Delete();
  208. uArray->Delete();
  209. }
  210. //--------------------------------------------------------------------------------------
  211. // Class: FEM4EllipticPDE
  212. // Method: ConstructAMatrix
  213. //--------------------------------------------------------------------------------------
  214. void FEM4EllipticPDE::ConstructAMatrix ( ) {
  215. /////////////////////////////////////////////////////////////////////////
  216. // Build stiffness matrix (A)
  217. std::cout << "Building Stiffness Matrix (A) " << std::endl;
  218. std::cout << "\tMesh has " << vtkGrid->GetNumberOfCells() << " cells " << std::endl;
  219. std::cout << "\tMesh has " << vtkGrid->GetNumberOfPoints() << " points " << std::endl;
  220. //Eigen::SparseMatrix<Real>
  221. A.resize(vtkGrid->GetNumberOfPoints(), vtkGrid->GetNumberOfPoints());
  222. std::vector< Eigen::Triplet<Real> > coeffs;
  223. if ( !vtkGrid->GetPointData()->GetScalars("HomogeneousDirichlet") ) {
  224. throw std::runtime_error("No HomogeneousDirichlet boundary conditions in input file.");
  225. }
  226. if ( !vtkGrid->GetCellData()->GetScalars("G") && !vtkGrid->GetPointData()->GetScalars("G") ) {
  227. throw std::runtime_error("No Cell or Point Data G");
  228. }
  229. bool GCell = false;
  230. if ( vtkGrid->GetCellData()->GetScalars("G") ) {
  231. GCell = true;
  232. }
  233. // Here we iterate over all of the cells
  234. for (int ic=0; ic < vtkGrid->GetNumberOfCells(); ++ic) {
  235. assert ( vtkGrid->GetCell(ic)->GetNumberOfPoints() == 4 );
  236. // TODO, in production code we might not want to do this check here
  237. if ( vtkGrid->GetCell(ic)->GetNumberOfPoints() != 4 ) {
  238. throw std::runtime_error("Non-tetrahedral mesh encountered!");
  239. }
  240. // construct coordinate matrix C
  241. Eigen::Matrix<Real, 4, 4> C = Eigen::Matrix<Real, 4, 4>::Zero() ;
  242. for (int ii=0; ii<4; ++ii) {
  243. double* pts = vtkGrid->GetCell(ic)->GetPoints()->GetPoint(ii); //[ipc] ;
  244. C(ii, 0) = 1;
  245. C(ii, 1) = pts[0] ;
  246. C(ii, 2) = pts[1] ;
  247. C(ii, 3) = pts[2] ;
  248. }
  249. Eigen::Matrix<Real, 4, 4> GradPhi = C.inverse(); // nabla \phi
  250. Real V = (1./6.) * C.determinant(); // volume of tetrahedra
  251. vtkIdList* Ids = vtkGrid->GetCell(ic)->GetPointIds();
  252. int ID[4];
  253. ID[0] = Ids->GetId(0);
  254. ID[1] = Ids->GetId(1);
  255. ID[2] = Ids->GetId(2);
  256. ID[3] = Ids->GetId(3);
  257. Real sigma_bar(0);
  258. /*
  259. if (GCell) {
  260. sigma_bar = vtkGrid->GetCellData()->GetScalars("G")->GetTuple1(ic);
  261. } else {
  262. sigma_bar = vtkGrid->GetPointData()->GetScalars("G")->GetTuple1(ID[0]);
  263. sigma_bar += vtkGrid->GetPointData()->GetScalars("G")->GetTuple1(ID[1]);
  264. sigma_bar += vtkGrid->GetPointData()->GetScalars("G")->GetTuple1(ID[2]);
  265. sigma_bar += vtkGrid->GetPointData()->GetScalars("G")->GetTuple1(ID[3]);
  266. sigma_bar /= 4.;
  267. }
  268. */
  269. // TEST VOID IN SIGMA!! TODO DON"T KEEP THIS
  270. Real xc = C.col(1).array().mean();
  271. Real yc = C.col(2).array().mean();
  272. Real zc = C.col(3).array().mean();
  273. if ( xc >= 2.5 && xc <= 5. && yc>=2.5 && yc <= 5.) {
  274. sigma_bar = 0.;
  275. } else {
  276. sigma_bar = 1.;
  277. }
  278. sigma_bar = 1.;
  279. for (int ii=0; ii<4; ++ii) {
  280. for (int jj=0; jj<4; ++jj) {
  281. /* homogeneous Dirichlet boundary */
  282. if (jj==ii && C(ii,3)==13.5) {
  283. //Real bb = vtkGrid->GetPointData()->GetScalars("HomogeneousDirichlet")->GetTuple(ID[ii])[0];
  284. //if (jj==ii && ((bb-1.)<1e-8) ) {
  285. // Apply Homogeneous Dirichlet Boundary conditions
  286. Real bb = vtkGrid->GetPointData()->GetScalars("HomogeneousDirichlet")->GetTuple(ID[ii])[0];
  287. Real bdry = (1./(BndryH*BndryH))*BndrySigma*bb; // + sum;
  288. //Real bdry = GradPhi.col(ii).tail<3>().dot(GradPhi.col(ii).tail<3>())*BndrySigma*bb; // + sum;
  289. coeffs.push_back( Eigen::Triplet<Real> ( ID[ii], ID[jj], bdry + GradPhi.col(ii).tail<3>().dot(GradPhi.col(jj).tail<3>() ) * V * sigma_bar ) );
  290. //coeffs.push_back( Eigen::Triplet<Real> ( ID[ii], ID[jj], 1));
  291. //break;
  292. } else {
  293. coeffs.push_back( Eigen::Triplet<Real> ( ID[ii], ID[jj], GradPhi.col(ii).tail<3>().dot(GradPhi.col(jj).tail<3>() ) * V * sigma_bar ) );
  294. }
  295. /* */
  296. /* Inhomogeneous Dirichlet */
  297. //coeffs.push_back( Eigen::Triplet<Real> ( ID[ii], ID[jj], GradPhi.col(ii).tail<3>().dot(GradPhi.col(jj).tail<3>())*V*sigma_bar ));
  298. // Stiffness matrix no longer contains boundary conditions...
  299. //coeffs.push_back( Eigen::Triplet<Real> ( ID[ii], ID[jj], GradPhi.col(ii).tail<3>().dot(GradPhi.col(jj).tail<3>() ) * V * sigma_bar ) );
  300. }
  301. }
  302. //std::cout << "\r" << (int)(1e2*((float)(ic) / (float)(vtkGrid->GetNumberOfCells()))) << std::flush ;
  303. }
  304. A.setFromTriplets(coeffs.begin(), coeffs.end());
  305. //std::cout << "A\n" << A << std::endl;
  306. }
  307. void FEM4EllipticPDE::SetupPotential() {
  308. ////////////////////////////////////////////////////////////
  309. // Load vector g
  310. std::cout << "\nBuilding load vector (g)" << std::endl;
  311. g = VectorXr::Zero(vtkGrid->GetNumberOfPoints());
  312. std::cout << "made g with " << vtkGrid->GetNumberOfPoints() << " points" << std::endl;
  313. VectorXr DB = VectorXr::Zero(vtkGrid->GetNumberOfPoints());
  314. for (int ic=0; ic < vtkGrid->GetNumberOfCells(); ++ic) {
  315. Eigen::Matrix<Real, 4, 4> C = Eigen::Matrix<Real, 4, 4>::Zero() ;
  316. for (int ii=0; ii<4; ++ii) {
  317. double* pts = vtkGrid->GetCell(ic)->GetPoints()->GetPoint(ii); //[ipc] ;
  318. C(ii, 0) = 1;
  319. C(ii, 1) = pts[0];
  320. C(ii, 2) = pts[1];
  321. C(ii, 3) = pts[2];
  322. }
  323. Real V = (1./6.) * C.determinant(); // volume of tetrahedra
  324. Eigen::Matrix<Real, 4, 4> GradPhi = C.inverse(); // nabla \phi
  325. vtkIdList* Ids = vtkGrid->GetCell(ic)->GetPointIds();
  326. int ID[4];
  327. ID[0] = Ids->GetId(0);
  328. ID[1] = Ids->GetId(1);
  329. ID[2] = Ids->GetId(2);
  330. ID[3] = Ids->GetId(3);
  331. /*
  332. Real avg(0);
  333. Real GG[4];
  334. for (int ii=0; ii<4; ++ii) {
  335. GG[ii] = vtkGrid->GetPointData()->GetScalars("G")->GetTuple(ID[ii])[0];
  336. //avg /= 4.;
  337. }
  338. if ( std::abs( (GG[0]+GG[1]+GG[2]+GG[3])/4. - GG[0]) < 1e-5) {
  339. avg = GG[0];
  340. }
  341. */
  342. VectorXr W = VectorXr::Zero(4);
  343. for (int ii=0; ii<4; ++ii) {
  344. W[ii] = vtkGrid->GetPointData()->GetScalars("HomogeneousDirichlet")->GetTuple(ID[ii])[0] *
  345. vtkGrid->GetPointData()->GetScalars("analytic_phi")->GetTuple(ID[ii])[0];
  346. DB[ID[ii]] = vtkGrid->GetPointData()->GetScalars("HomogeneousDirichlet")->GetTuple(ID[ii])[0] *
  347. vtkGrid->GetPointData()->GetScalars("analytic_phi")->GetTuple(ID[ii])[0];
  348. }
  349. //auto G = GradPhi.block<3,4>(1,0).transpose()*GradPhi.block<3,4>(1,0)*W;
  350. VectorXr G = GradPhi.block<3,4>(1,0).transpose()*GradPhi.block<3,4>(1,0)*W;
  351. Real sigma_bar(1.);
  352. for (int ii=0; ii<4; ++ii) {
  353. // avg += vtkGrid->GetPointData()->GetScalars()->GetTuple(ID[ii])[0];
  354. // //if ( std::abs(vtkGrid->GetPointData()->GetScalars()->GetTuple(ID[ii])[0]) > 1e-3 )
  355. //}
  356. //TODO this seems wrong!
  357. //avg /= 4.;
  358. // TODO test code remove
  359. g(ID[ii]) += V/4*(vtkGrid->GetPointData()->GetScalars("G")->GetTuple(ID[ii])[0]) ;
  360. if (std::abs(C(ii,3)-13.5) > 1e-5) {
  361. g(ID[ii]) -= G[ii]*(V/3.)*sigma_bar;
  362. }
  363. //g(ID[ii]) += V/4*avg;
  364. //g(ID[ii]) += 6.67 *(V/4.) * avg;
  365. }
  366. //g(ID[0]) += (V/4.) * avg;
  367. }
  368. //g -= A*DB;
  369. }
  370. void FEM4EllipticPDE::SolveOLD(const std::string& fname) {
  371. Real r0[3];
  372. Real r1[3];
  373. /////////////////////////////////////////////////////////////////////////
  374. // Surface filter, to determine if points are on boundary, and need
  375. // boundary conditions applied
  376. vtkDataSetSurfaceFilter* Surface = vtkDataSetSurfaceFilter::New();
  377. Surface->SetInputData(vtkGrid);
  378. Surface->PassThroughPointIdsOn( );
  379. Surface->Update();
  380. vtkIdTypeArray* BdryIds = static_cast<vtkIdTypeArray*>
  381. (Surface->GetOutput()->GetPointData()->GetScalars("vtkOriginalPointIds"));
  382. // Expensive search for whether or not point is on boundary. O(n) cost.
  383. VectorXi bndryCnt = VectorXi::Zero(vtkGrid->GetNumberOfPoints());
  384. for (int isp=0; isp < Surface->GetOutput()->GetNumberOfPoints(); ++isp) {
  385. //double* pts = vtkGrid->GetCell(ic)->GetPoints()->GetPoint(ii); //[ipc] ;
  386. // x \in -14.5 to 14.5
  387. // y \in 0 to 30
  388. bndryCnt(BdryIds->GetTuple1(isp)) += 1;
  389. }
  390. /////////////////////////////////////////////////////////////////////////
  391. // Build stiffness matrix (A)
  392. std::cout << "Building Stiffness Matrix (A) " << std::endl;
  393. std::cout << "\tMesh has " << vtkGrid->GetNumberOfCells() << " cells " << std::endl;
  394. std::cout << "\tMesh has " << vtkGrid->GetNumberOfPoints() << " points " << std::endl;
  395. Eigen::SparseMatrix<Real> A(vtkGrid->GetNumberOfPoints(), vtkGrid->GetNumberOfPoints());
  396. std::vector< Eigen::Triplet<Real> > coeffs;
  397. // Here we iterate over all of the cells
  398. for (int ic=0; ic < vtkGrid->GetNumberOfCells(); ++ic) {
  399. assert ( vtkGrid->GetCell(ic)->GetNumberOfPoints() == 4 );
  400. // TODO, in production code we might not want to do this check here
  401. if ( vtkGrid->GetCell(ic)->GetNumberOfPoints() != 4 ) {
  402. std::cout << "DOOM FEM4EllipticPDE encountered non-tetrahedral mesh\n";
  403. std::cout << "Number of points in cell " << vtkGrid->GetCell(ic)->GetNumberOfPoints() << std::endl ;
  404. exit(1);
  405. }
  406. // construct coordinate matrix C
  407. Eigen::Matrix<Real, 4, 4> C = Eigen::Matrix<Real, 4, 4>::Zero() ;
  408. for (int ii=0; ii<4; ++ii) {
  409. double* pts = vtkGrid->GetCell(ic)->GetPoints()->GetPoint(ii); //[ipc] ;
  410. C(ii, 0) = 1;
  411. C(ii, 1) = pts[0] ;
  412. C(ii, 2) = pts[1] ;
  413. C(ii, 3) = pts[2] ;
  414. }
  415. Eigen::Matrix<Real, 4, 4> GradPhi = C.inverse(); // nabla \phi
  416. Real V = (1./6.) * C.determinant(); // volume of tetrahedra
  417. vtkIdList* Ids = vtkGrid->GetCell(ic)->GetPointIds();
  418. int ID[4];
  419. ID[0] = Ids->GetId(0);
  420. ID[1] = Ids->GetId(1);
  421. ID[2] = Ids->GetId(2);
  422. ID[3] = Ids->GetId(3);
  423. Real sum(0);
  424. Real sigma_bar = vtkGrid->GetCellData()->GetScalars()->GetTuple1(ic);
  425. for (int ii=0; ii<4; ++ii) {
  426. for (int jj=0; jj<4; ++jj) {
  427. if (jj == ii) {
  428. // I apply boundary to Stiffness matrix, it's common to take the other approach and apply to the load vector and then
  429. // solve for the boundaries? Is one better? This seems to work, which is nice.
  430. //Real bdry = V*(1./(BndryH*BndryH))*BndrySigma*bndryCnt( ID[ii] ); // + sum;
  431. Real bb = vtkGrid->GetPointData()->GetScalars("vtkValidPointMask")->GetTuple(ID[ii])[0];
  432. //std::cout << "bb " << bb << std::endl;
  433. Real bdry = V*(1./(BndryH*BndryH))*BndrySigma*bb; // + sum;
  434. coeffs.push_back( Eigen::Triplet<Real> ( ID[ii], ID[jj], bdry + GradPhi.col(ii).tail<3>().dot(GradPhi.col(jj).tail<3>() ) * V * sigma_bar ) );
  435. } else {
  436. coeffs.push_back( Eigen::Triplet<Real> ( ID[ii], ID[jj], GradPhi.col(ii).tail<3>().dot(GradPhi.col(jj).tail<3>() ) * V * sigma_bar ) );
  437. }
  438. // Stiffness matrix no longer contains boundary conditions...
  439. //coeffs.push_back( Eigen::Triplet<Real> ( ID[ii], ID[jj], GradPhi.col(ii).tail<3>().dot(GradPhi.col(jj).tail<3>() ) * V * sigma_bar ) );
  440. }
  441. }
  442. std::cout << "\r" << (int)(1e2*((float)(ic) / (float)(vtkGrid->GetNumberOfCells()))) << std::flush ;
  443. }
  444. A.setFromTriplets(coeffs.begin(), coeffs.end());
  445. //A.makeCompressed();
  446. ////////////////////////////////////////////////////////////
  447. // Load vector g, solution vector u
  448. std::cout << "\nBuilding load vector (g)" << std::endl;
  449. VectorXr g = VectorXr::Zero(vtkGrid->GetNumberOfPoints());
  450. std::cout << "made g with " << vtkGrid->GetNumberOfPoints() << " pnts" << std::endl;
  451. // If the G function has been evaluated at each *node*
  452. // --> but still needs to be integrated at the surfaces
  453. // Aha, requires that there is in fact a pointdata memeber // BUG TODO BUG!!!
  454. std::cout << "Point Data ptr " << vtkGrid->GetPointData() << std::endl;
  455. //if ( vtkGrid->GetPointData() != NULL && std::string( vtkGrid->GetPointData()->GetScalars()->GetName() ).compare( std::string("G") ) == 0 ) {
  456. bool pe(false);
  457. bool ne(false);
  458. if ( true ) {
  459. std::cout << "\nUsing G from file" << std::endl;
  460. /* 3D Phi */
  461. for (int ic=0; ic < vtkGrid->GetNumberOfCells(); ++ic) {
  462. Eigen::Matrix<Real, 4, 4> C = Eigen::Matrix<Real, 4, 4>::Zero() ;
  463. for (int ii=0; ii<4; ++ii) {
  464. double* pts = vtkGrid->GetCell(ic)->GetPoints()->GetPoint(ii); //[ipc] ;
  465. C(ii, 0) = 1;
  466. C(ii, 1) = pts[0];
  467. C(ii, 2) = pts[1];
  468. C(ii, 3) = pts[2];
  469. }
  470. Real V = (1./6.) * C.determinant(); // volume of tetrahedra
  471. //Real V = C.determinant(); // volume of tetrahedra
  472. vtkIdList* Ids = vtkGrid->GetCell(ic)->GetPointIds();
  473. int ID[4];
  474. ID[0] = Ids->GetId(0);
  475. ID[1] = Ids->GetId(1);
  476. ID[2] = Ids->GetId(2);
  477. ID[3] = Ids->GetId(3);
  478. /* bad news bears for magnet */
  479. double* pt = vtkGrid->GetCell(ic)->GetPoints()->GetPoint(0);
  480. Real sum(0);
  481. /*
  482. if (!pe) {
  483. if (std::abs(pt[0]) < .2 && std::abs(pt[1]-15) < .2 && pt[2] < 3.25 ) {
  484. sum = 1; //vtkGrid->GetPointData()->GetScalars()->GetTuple(ID[ii])[0];
  485. pe = true;
  486. }
  487. }*/
  488. if (ID[0] == 26) {
  489. sum = 10;
  490. }
  491. if (ID[0] == 30) {
  492. sum = -10;
  493. }
  494. /*
  495. if (!ne) {
  496. if (std::abs(pt[0]+1.) < .2 && std::abs(pt[1]-15) < .2 && pt[2] < 3.25 ) {
  497. sum = -1; //vtkGrid->GetPointData()->GetScalars()->GetTuple(ID[ii])[0];
  498. std::cout << "Negative Electroce\n";
  499. ne = true;
  500. }
  501. }
  502. */
  503. //for (int ii=0; ii<4; ++ii) {
  504. //g(ID[ii]) += (V/4.) * ( vtkGrid->GetPointData()->GetScalars()->GetTuple(ID[ii])[0] ) ;
  505. //if ( std::abs(vtkGrid->GetPointData()->GetScalars()->GetTuple(ID[ii])[0]) > 1e-3 )
  506. //}
  507. // TODO check Load Vector...
  508. g(ID[0]) = sum; //(V/4.) * sum; // Why 3, Why!!!?
  509. std::cout << "\r" << (int)(1e2*((float)(ic) / (float)(vtkGrid->GetNumberOfCells()))) << std::flush ;
  510. }
  511. /*
  512. for (int ic=0; ic < vtkGrid->GetNumberOfPoints(); ++ic) {
  513. vtkGrid->GetPoint(ic, r0);
  514. vtkSmartPointer<vtkIdList> connectedVertices = GetConnectedPoints(ic);
  515. double g0 = vtkGrid->GetPointData()->GetScalars()->GetTuple(ic)[0] ;
  516. //std::cout << "num conn " << connectedVertices->GetNumberOfIds() << std::endl;
  517. if ( std::abs(g0) > 1e-3 ) {
  518. for(vtkIdType i = 0; i < connectedVertices->GetNumberOfIds(); ++i) {
  519. int ii = connectedVertices->GetId(i);
  520. vtkGrid->GetPoint(ii, r1);
  521. double g1 = vtkGrid->GetPointData()->GetScalars()->GetTuple(ii)[0] ;
  522. //g(ic) += g0*dist(r0,r1); //CompositeSimpsons2(g0, r0, r1, 8);
  523. if ( std::abs(g1) > 1e-3 ) {
  524. g(ic) += CompositeSimpsons2(g1, g0, r1, r0, 1000);
  525. }
  526. //g(ic) += CompositeSimpsons2(g0, r1, r0, 8);
  527. //if ( std::abs(g1) > 1e-3 ) {
  528. //g(ic) += CompositeSimpsons2(g0, g1, r0, r1, 8);
  529. //g(ic) += CompositeSimpsons2(g0, g1, r0, r1, 100); // / (2*dist(r0,r1)) ;
  530. // g(ic) += g0*dist(r0,r1); //CompositeSimpsons2(g0, r0, r1, 8);
  531. //g(ic) += CompositeSimpsons2(g0, r0, r1, 8);
  532. // g(ic) += g0; //CompositeSimpsons2(g0, r0, r1, 8);
  533. //} //else {
  534. // g(ic) += g0; //CompositeSimpsons2(g0, r0, r1, 8);
  535. //}
  536. }
  537. }
  538. //g(ic) = 2.* vtkGrid->GetPointData()->GetScalars()->GetTuple(ic)[0] ; // Why 2?
  539. //std::cout << "\r" << (int)(1e2*((float)(ic) / (float)(vtkGrid->GetNumberOfPoints()))) << std::flush ;
  540. }
  541. */
  542. } else if (vtkG) { // VTK implicit function, proceed with care
  543. std::cout << "\nUsing implicit file from file" << std::endl;
  544. // OpenMP right here
  545. for (int ic=0; ic < vtkGrid->GetNumberOfPoints(); ++ic) {
  546. vtkSmartPointer<vtkIdList> connectedVertices = GetConnectedPoints(ic);
  547. //vtkGrid->GetPoint(ic, r0);
  548. //g(ic) += vtkG->FunctionValue(r0[0], r0[1], r0[2]) ;
  549. // std::cout << vtkG->FunctionValue(r0[0], r0[1], r0[2]) << std::endl;
  550. //g(ic) += vtkGrid->GetPointData()->GetScalars()->GetTuple1(ic);// FunctionValue(r0[0], r0[1], r0[2]) ;
  551. /*
  552. for(vtkIdType i = 0; i < connectedVertices->GetNumberOfIds(); ++i) {
  553. int ii = connectedVertices->GetId(i);
  554. vtkGrid->GetPoint(ii, r1);
  555. g(ic) += CompositeSimpsons2(vtkG, r0, r1, 8); // vtkG->FunctionValue(r0[0], r0[1], r0[2]) ;
  556. }
  557. */
  558. std::cout << "\r" << (int)(1e2*((float)(ic) / (float)(vtkGrid->GetNumberOfPoints()))) << std::flush ;
  559. }
  560. } else if (gFcn3) {
  561. std::cout << "\nUsing gFcn3" << std::endl;
  562. for (int ic=0; ic < vtkGrid->GetNumberOfPoints(); ++ic) {
  563. vtkSmartPointer<vtkIdList> connectedVertices = GetConnectedPoints(ic);
  564. vtkGrid->GetPoint(ic, r0);
  565. // TODO, test OMP sum reduction here. Is vtkGrid->GetPoint thread safe?
  566. //Real sum(0.);
  567. //#ifdef LEMMAUSEOMP
  568. //#pragma omp parallel for reduction(+:sum)
  569. //#endif
  570. for(vtkIdType i = 0; i < connectedVertices->GetNumberOfIds(); ++i) {
  571. int ii = connectedVertices->GetId(i);
  572. vtkGrid->GetPoint(ii, r1);
  573. g(ic) += CompositeSimpsons2(gFcn3, r0, r1, 8); // vtkG->FunctionValue(r0[0], r0[1], r0[2]) ;
  574. //sum += CompositeSimpsons2(gFcn3, r0, r1, 8); // vtkG->FunctionValue(r0[0], r0[1], r0[2]) ;
  575. }
  576. std::cout << "\r" << (int)(1e2*((float)(ic) / (float)(vtkGrid->GetNumberOfPoints()))) << std::flush ;
  577. //g(ic) = sum;
  578. }
  579. } else {
  580. std::cout << "No source specified\n";
  581. exit(EXIT_FAILURE);
  582. }
  583. // std::cout << g << std::endl;
  584. //g(85) = 1;
  585. std::cout << "\nSolving" << std::endl;
  586. ////////////////////////////////////////////////////////////
  587. // Solving:
  588. //Eigen::SimplicialCholesky<Eigen::SparseMatrix<Real>, Eigen::Lower > chol(A); // performs a Cholesky factorization of A
  589. //VectorXr u = chol.solve(g);
  590. //Eigen::SparseLU<Eigen::SparseMatrix<Real, Eigen::ColMajor>, Eigen::COLAMDOrdering<int> > solver;
  591. //solver.analyzePattern(A);
  592. //solver.factorize(A);
  593. //VectorXr u = solver.solve(g);
  594. //Eigen::ConjugateGradient<Eigen::SparseMatrix<Real, Eigen::Lower > Eigen::DiagonalPreconditioner > cg;
  595. Eigen::ConjugateGradient< Eigen::SparseMatrix<Real> > cg(A);
  596. //Eigen::BiCGSTAB<Eigen::SparseMatrix<Real> > cg(A);
  597. cg.setMaxIterations(3000);
  598. //cg.compute(A);
  599. //std::cout << "Computed " << std::endl;
  600. VectorXr u = cg.solve(g);
  601. std::cout << "#iterations: " << cg.iterations() << std::endl;
  602. std::cout << "estimated error: " << cg.error() << std::endl;
  603. vtkDoubleArray *gArray = vtkDoubleArray::New();
  604. vtkDoubleArray *uArray = vtkDoubleArray::New();
  605. uArray->SetNumberOfComponents(1);
  606. gArray->SetNumberOfComponents(1);
  607. for (int iu = 0; iu<u.size(); ++iu) {
  608. uArray->InsertTuple1(iu, u[iu]);
  609. gArray->InsertTuple1(iu, g[iu]);
  610. }
  611. uArray->SetName("u");
  612. gArray->SetName("g");
  613. vtkGrid->GetPointData()->AddArray(uArray);
  614. vtkGrid->GetPointData()->AddArray(gArray);
  615. vtkXMLDataSetWriter *Writer = vtkXMLDataSetWriter::New();
  616. Writer->SetInputData(vtkGrid);
  617. Writer->SetFileName(fname.c_str());
  618. Writer->Write();
  619. Writer->Delete();
  620. Surface->Delete();
  621. gArray->Delete();
  622. uArray->Delete();
  623. }
  624. // Uses simpon's rule to perform a definite integral of a
  625. // function (passed as a pointer). The integration occurs from
  626. // (Shamelessly adapted from http://en.wikipedia.org/wiki/Simpson's_rule
  627. Real FEM4EllipticPDE::CompositeSimpsons(vtkImplicitFunction* f, Real r0[3], Real r1[3], int n) {
  628. Vector3r R0(r0[0], r0[1], r0[2]);
  629. Vector3r R1(r1[0], r1[1], r1[2]);
  630. // force n to be even
  631. assert(n > 0);
  632. //n += (n % 2);
  633. Real h = dist(r0, r1) / (Real)(n) ;
  634. Real S = f->FunctionValue(r0) + f->FunctionValue(r1);
  635. Vector3r dr = (R1 - R0).array() / Real(n);
  636. Vector3r rx;
  637. rx.array() = R0.array() + dr.array();
  638. for (int i=1; i<n; i+=2) {
  639. S += 4. * f->FunctionValue(rx[0], rx[1], rx[2]);
  640. rx += 2.*dr;
  641. }
  642. rx.array() = R0.array() + 2*dr.array();
  643. for (int i=2; i<n-1; i+=2) {
  644. S += 2.*f->FunctionValue(rx[0], rx[1], rx[2]);
  645. rx += 2.*dr;
  646. }
  647. return h * S / 3.;
  648. }
  649. // Uses simpon's rule to perform a definite integral of a
  650. // function (passed as a pointer). The integration occurs from
  651. // (Shamelessly adapted from http://en.wikipedia.org/wiki/Simpson's_rule
  652. // This is just here as a convenience
  653. Real FEM4EllipticPDE::CompositeSimpsons(const Real& f, Real r0[3], Real r1[3], int n) {
  654. return dist(r0,r1)*f;
  655. /*
  656. Vector3r R0(r0[0], r0[1], r0[2]);
  657. Vector3r R1(r1[0], r1[1], r1[2]);
  658. // force n to be even
  659. assert(n > 0);
  660. //n += (n % 2);
  661. Real h = dist(r0, r1) / (Real)(n) ;
  662. Real S = f + f;
  663. Vector3r dr = (R1 - R0).array() / Real(n);
  664. //Vector3r rx;
  665. //rx.array() = R0.array() + dr.array();
  666. for (int i=1; i<n; i+=2) {
  667. S += 4. * f;
  668. //rx += 2.*dr;
  669. }
  670. //rx.array() = R0.array() + 2*dr.array();
  671. for (int i=2; i<n-1; i+=2) {
  672. S += 2. * f;
  673. //rx += 2.*dr;
  674. }
  675. return h * S / 3.;
  676. */
  677. }
  678. /*
  679. * Performs numerical integration of two functions, one is passed as a vtkImplicitFunction, the other is the FEM
  680. * test function owned by the FEM implimentaion.
  681. */
  682. Real FEM4EllipticPDE::CompositeSimpsons2(vtkImplicitFunction* f,
  683. Real r0[3], Real r1[3], int n) {
  684. Vector3r R0(r0[0], r0[1], r0[2]);
  685. Vector3r R1(r1[0], r1[1], r1[2]);
  686. // force n to be even
  687. assert(n > 0);
  688. //n += (n % 2);
  689. Real h = dist(r0, r1) / (Real)(n) ;
  690. // For Gelerkin (most) FEM, we can omit this one as test functions are zero valued at element boundaries
  691. Real S = f->FunctionValue(r0) + f->FunctionValue(r1);
  692. //Real S = f->FunctionValue(r0) + f->FunctionValue(r1);
  693. Vector3r dr = (R1 - R0).array() / Real(n);
  694. Vector3r rx;
  695. rx.array() = R0.array() + dr.array();
  696. for (int i=1; i<n; i+=2) {
  697. S += 4. * f->FunctionValue(rx[0], rx[1], rx[2]) * Hat(rx, R0, R1) * Hat(rx, R1, R0);
  698. rx += 2.*dr;
  699. }
  700. rx.array() = R0.array() + 2*dr.array();
  701. for (int i=2; i<n-1; i+=2) {
  702. S += 2. * f->FunctionValue(rx[0], rx[1], rx[2]) * Hat(rx, R0, R1) * Hat(rx, R1, R0);
  703. rx += 2.*dr;
  704. }
  705. return h * S / 3.;
  706. }
  707. /*
  708. * Performs numerical integration of two functions, one is passed as a vtkImplicitFunction, the other is the FEM
  709. * test function owned by the FEM implimentaion.
  710. */
  711. Real FEM4EllipticPDE::CompositeSimpsons2( Real (*f)(const Real&, const Real&, const Real&),
  712. Real r0[3], Real r1[3], int n) {
  713. Vector3r R0(r0[0], r0[1], r0[2]);
  714. Vector3r R1(r1[0], r1[1], r1[2]);
  715. // force n to be even
  716. assert(n > 0);
  717. //n += (n % 2);
  718. Real h = dist(r0, r1) / (Real)(n) ;
  719. // For Gelerkin (most) FEM, we can omit this one as test functions are zero valued at element boundaries
  720. //Real S = f(r0[0], r0[1], r0[2])*Hat(R0, R0, R1) + f(r1[0], r1[1], r1[2])*Hat(R1, R0, R1);
  721. Real S = f(r0[0], r0[1], r0[2]) + f(r1[0], r1[1], r1[2]);
  722. Vector3r dr = (R1 - R0).array() / Real(n);
  723. Vector3r rx;
  724. rx.array() = R0.array() + dr.array();
  725. for (int i=1; i<n; i+=2) {
  726. S += 4. * f(rx[0], rx[1], rx[2]) * Hat(rx, R0, R1) * Hat(rx, R1, R0);
  727. rx += 2.*dr;
  728. }
  729. rx.array() = R0.array() + 2*dr.array();
  730. for (int i=2; i<n-1; i+=2) {
  731. S += 2. * f(rx[0], rx[1], rx[2]) * Hat(rx, R0, R1) * Hat(rx, R1, R0);
  732. rx += 2.*dr;
  733. }
  734. return h * S / 3.;
  735. }
  736. /*
  737. * Performs numerical integration of two functions, one is constant valued f, the other is the FEM
  738. * test function owned by the FEM implimentaion.
  739. */
  740. Real FEM4EllipticPDE::CompositeSimpsons2( const Real& f, Real r0[3], Real r1[3], int n) {
  741. Vector3r R0(r0[0], r0[1], r0[2]);
  742. Vector3r R1(r1[0], r1[1], r1[2]);
  743. // force n to be even
  744. assert(n > 0);
  745. //n += (n % 2);
  746. Real h = dist(r0, r1) / (Real)(n) ;
  747. // For Gelerkin (most) FEM, we can omit this one as test functions are zero valued at element boundaries
  748. Real S = 2*f; //*Hat(R0, R0, R1) + f*Hat(R1, R0, R1);
  749. Vector3r dr = (R1 - R0).array() / Real(n);
  750. Vector3r rx;
  751. rx.array() = R0.array() + dr.array();
  752. for (int i=1; i<n; i+=2) {
  753. S += 4. * f * Hat(rx, R0, R1) * Hat(rx, R1, R0);
  754. rx += 2.*dr;
  755. }
  756. rx.array() = R0.array() + 2*dr.array();
  757. for (int i=2; i<n-1; i+=2) {
  758. S += 2. * f * Hat(rx, R0, R1) * Hat(rx, R1, R0);
  759. rx += 2.*dr;
  760. }
  761. return h * S / 3.;
  762. }
  763. /*
  764. * Performs numerical integration of two functions, one is passed as a vtkImplicitFunction, the other is the FEM
  765. * test function owned by the FEM implimentaion.
  766. */
  767. Real FEM4EllipticPDE::CompositeSimpsons2( const Real& f0, const Real& f1, Real r0[3], Real r1[3], int n) {
  768. Vector3r R0(r0[0], r0[1], r0[2]);
  769. Vector3r R1(r1[0], r1[1], r1[2]);
  770. // force n to be even
  771. assert(n > 0);
  772. //n += (n % 2);
  773. Real h = dist(r0, r1) / (Real)(n) ;
  774. // For Gelerkin (most) FEM, we can omit this one as test functions are zero valued at element boundaries
  775. // NO! We are looking at 1/2 hat often!!! So S = f1!
  776. Real S = f1; //f0*Hat(R0, R0, R1) + f1*Hat(R1, R0, R1);
  777. Vector3r dr = (R1 - R0).array() / Real(n);
  778. // linear interpolate function
  779. //Vector3r rx;
  780. //rx.array() = R0.array() + dr.array();
  781. for (int i=1; i<n; i+=2) {
  782. double fx = f0 + (f1 - f0) * ((i*h)/(h*n));
  783. S += 4. * fx * Hat(R0.array() + i*dr.array(), R0, R1);// * Hat(R1.array() + i*dr.array(), R1, R0) ;
  784. }
  785. //rx.array() = R0.array() + 2*dr.array();
  786. for (int i=2; i<n-1; i+=2) {
  787. double fx = f0 + (f1 - f0) * ((i*h)/(h*n));
  788. S += 2. * fx * Hat(R0.array() + i*dr.array(), R0, R1);// * Hat(R1.array() + i*dr.array(), R1, R0);
  789. }
  790. return h * S / 3.;
  791. }
  792. /*
  793. * Performs numerical integration of two functions, one is passed as a vtkImplicitFunction, the other is the FEM
  794. * test function owned by the FEM implimentaion.
  795. */
  796. Real FEM4EllipticPDE::CompositeSimpsons3( const Real& f0, const Real& f1, Real r0[3], Real r1[3], int n) {
  797. Vector3r R0(r0[0], r0[1], r0[2]);
  798. Vector3r R1(r1[0], r1[1], r1[2]);
  799. // force n to be even
  800. assert(n > 0);
  801. //n += (n % 2);
  802. Real h = dist(r0, r1) / (Real)(n) ;
  803. // For Gelerkin (most) FEM, we can omit this one as test functions are zero valued at element boundaries
  804. // NO! We are looking at 1/2 hat often!!! So S = f1!
  805. Real S = f0+f1; //Hat(R0, R0, R1) + f1*Hat(R1, R0, R1);
  806. Vector3r dr = (R1 - R0).array() / Real(n);
  807. // linear interpolate function
  808. //Vector3r rx;
  809. //rx.array() = R0.array() + dr.array();
  810. for (int i=1; i<n; i+=2) {
  811. double fx = 1;// f0 + (f1 - f0) * ((i*h)/(h*n));
  812. S += 4. * fx * Hat(R0.array() + i*dr.array(), R0, R1) * Hat(R1.array() + i*dr.array(), R1, R0) ;
  813. }
  814. //rx.array() = R0.array() + 2*dr.array();
  815. for (int i=2; i<n-1; i+=2) {
  816. double fx = 1; // f0 + (f1 - f0) * ((i*h)/(h*n));
  817. S += 2. * fx * Hat(R0.array() + i*dr.array(), R0, R1)* Hat(R1.array() + i*dr.array(), R1, R0);
  818. }
  819. return h * S / 3.;
  820. }
  821. //--------------------------------------------------------------------------------------
  822. // Class: FEM4EllipticPDE
  823. // Method: Hat
  824. //--------------------------------------------------------------------------------------
  825. Real FEM4EllipticPDE::Hat ( const Vector3r& r, const Vector3r& r0, const Vector3r& r1 ) {
  826. //return (r-r0).norm() / (r1-r0).norm() ;
  827. return dist(r, r0) / dist(r1, r0) ;
  828. } // ----- end of method FEM4EllipticPDE::Hat -----
  829. } // ----- end of Lemma name -----