Galerkin FEM for elliptic PDEs
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

rectilinearstructuredgrid.cpp 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. // ===========================================================================
  2. //
  3. // Filename: rectilinearstructuredgrid.cpp
  4. //
  5. // Description:
  6. //
  7. // Version: 0.0
  8. // Created: 09/16/2013 01:55:17 PM
  9. // Revision: none
  10. // Compiler: Tested with g++
  11. //
  12. // Author: M. Andy Kass (MAK)
  13. //
  14. // Organisation: US Geological Survey
  15. //
  16. //
  17. // Email: mkass@usgs.gov
  18. //
  19. // This program is free software: you can redistribute it and/or modify
  20. // it under the terms of the GNU General Public License as published by
  21. // the Free Software Foundation, either version 3 of the License, or
  22. // (at your option) any later version.
  23. //
  24. // This program is distributed in the hope that it will be useful,
  25. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. // GNU General Public License for more details.
  28. //
  29. // You should have received a copy of the GNU General Public License
  30. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  31. //
  32. // ===========================================================================
  33. #include "rectilinearstructuredgrid.h"
  34. namespace formalhaut {
  35. std::ostream &operator<<(std::ostream &stream,
  36. const RectilinearStructuredGrid &ob) {
  37. return stream;
  38. }
  39. RectilinearStructuredGrid::RectilinearStructuredGrid() {
  40. this->TypeOfGrid = rectilinear;
  41. }
  42. RectilinearStructuredGrid::~RectilinearStructuredGrid() {
  43. }
  44. RectilinearStructuredGrid* RectilinearStructuredGrid::New() {
  45. return new RectilinearStructuredGrid;
  46. }
  47. void RectilinearStructuredGrid::Delete() {
  48. delete this;
  49. }
  50. void RectilinearStructuredGrid::ComputeCentroids() {
  51. int ntot;
  52. ntot = this->Nx3(0)*this->Nx3(1)*this->Nx3(2);
  53. this->VecCentroid.resize(Eigen::NoChange,ntot);
  54. this->CW.resize(Eigen::NoChange,ntot);
  55. int mm = 0;
  56. // First z changes, then x then y
  57. // first depth, then easting, then northing
  58. //Original
  59. ///*
  60. //std::cout << this->Dx3v[0].segment(0,1).sum() << std::endl;
  61. //std::cout << this->Dx3v[0](0) << " " << this->Dx3v[0](1) <<
  62. // std::endl;
  63. //std::cout << this->Dx3v[0].segment(0,1).sum() -
  64. // this->Dx3v[0](1)/2 + this->Dx3v[0](0) +
  65. // this->Origin(0) << std::endl;
  66. for (int jj=0;jj<this->Nx3(1);jj++) {
  67. for (int ii=this->Nx3(0)-1;ii>-1;ii--) {
  68. //for (int ii=0;ii<this->Nx3(0);ii++) {
  69. for (int kk=0;kk<this->Nx3(2);kk++) {
  70. //*/
  71. /*
  72. for (int ii=0;ii<this->Nx3(0);ii++) {
  73. for (int jj=0;jj<this->Nx3(1);jj++) {
  74. for (int kk=0;kk<this->Nx3(2);kk++) {
  75. */
  76. this->VecCentroid(0,mm) = this->Origin(0) +
  77. this->Dx3v[0].segment(0,ii).sum() -
  78. this->Dx3v[0](ii)/2 + this->Dx3v[0](ii);
  79. this->VecCentroid(1,mm) = this->Origin(1) +
  80. this->Dx3v[1].segment(0,jj).sum() -
  81. this->Dx3v[1](jj)/2 + this->Dx3v[1](jj);
  82. this->VecCentroid(2,mm) = this->Origin(2) +
  83. this->Dx3v[2].segment(0,kk).sum() -
  84. this->Dx3v[2](kk)/2 + this->Dx3v[2](kk);
  85. this->CW(0,mm) = this->Dx3v[0](ii);
  86. this->CW(1,mm) = this->Dx3v[1](jj);
  87. this->CW(2,mm) = this->Dx3v[2](kk);
  88. mm++;
  89. }
  90. }
  91. }
  92. }
  93. void RectilinearStructuredGrid::SetCellDims(const
  94. std::vector<VectorXr>& dx3v) {
  95. this->Dx3v[0] = dx3v[0];
  96. this->Dx3v[1] = dx3v[1];
  97. this->Dx3v[2] = dx3v[2];
  98. }
  99. std::vector<vtkActor*> RectilinearStructuredGrid::ReturnMeshActor() {
  100. // Coords are edges of cells, not centroids
  101. vtkFloatArray *xCoords = vtkFloatArray::New();
  102. vtkFloatArray *yCoords = vtkFloatArray::New();
  103. vtkFloatArray *zCoords = vtkFloatArray::New();
  104. Real val;
  105. val = this->Origin(0);
  106. xCoords->InsertNextValue(val);
  107. for (int ii=1;ii<=this->Nx3(0)+1;ii++) {
  108. val = this->Origin(0) + this->Dx3v[0].segment(0,ii-1).sum();
  109. xCoords->InsertNextValue(val);
  110. }
  111. val = this->Origin(1);
  112. yCoords->InsertNextValue(val);
  113. for (int ii=1;ii<=this->Nx3(1)+1;ii++) {
  114. val = this->Origin(1) + this->Dx3v[1].segment(0,ii-1).sum();
  115. yCoords->InsertNextValue(val);
  116. }
  117. val = -1 * this->Origin(2);
  118. zCoords->InsertNextValue(val);
  119. for (int ii=1;ii<=this->Nx3(2)+1;ii++) {
  120. val = this->Origin(2) + this->Dx3v[2].segment(0,ii-1).sum();
  121. val = -1*val;
  122. zCoords->InsertNextValue(val);
  123. }
  124. vtkRectilinearGrid *rgrid = vtkRectilinearGrid::New();
  125. rgrid->SetDimensions(this->Nx3(1)+2,this->Nx3(0)+2,this->Nx3(2)+2);
  126. rgrid->SetXCoordinates(yCoords);
  127. rgrid->SetYCoordinates(xCoords);
  128. rgrid->SetZCoordinates(zCoords);
  129. vtkRectilinearGridGeometryFilter *plane =
  130. vtkRectilinearGridGeometryFilter::New();
  131. plane->SetInputData(rgrid);
  132. //plane->SetExtent(0,this->Nx3(0),0,this->Nx3(1),0,this->Nx3(2));
  133. //Just for testing
  134. plane->SetExtent(0,0,0,this->Nx3(0)+1,0,this->Nx3(2)+1);
  135. vtkPolyDataMapper *rgridmapper = vtkPolyDataMapper::New();
  136. rgridmapper->SetInputConnection(plane->GetOutputPort());
  137. vtkActor *wireActor = vtkActor::New();
  138. wireActor->SetMapper(rgridmapper);
  139. wireActor->GetProperty()->SetRepresentationToWireframe();
  140. wireActor->GetProperty()->SetColor(0,1,0);
  141. //Make an actor for each face
  142. //Second face
  143. vtkRectilinearGridGeometryFilter *plane1 =
  144. vtkRectilinearGridGeometryFilter::New();
  145. plane1->SetInputData(rgrid);
  146. plane1->SetExtent(this->Nx3(1)+1,this->Nx3(1)+1,0,this->Nx3(0)+1,
  147. 0,this->Nx3(2)+1);
  148. vtkPolyDataMapper *rgridmapper1 = vtkPolyDataMapper::New();
  149. rgridmapper1->SetInputConnection(plane1->GetOutputPort());
  150. vtkActor *wireActor1 = vtkActor::New();
  151. wireActor1->SetMapper(rgridmapper1);
  152. wireActor1->GetProperty()->SetRepresentationToWireframe();
  153. wireActor1->GetProperty()->SetColor(0,1,0);
  154. //Third face
  155. vtkRectilinearGridGeometryFilter *plane2 =
  156. vtkRectilinearGridGeometryFilter::New();
  157. plane2->SetInputData(rgrid);
  158. plane2->SetExtent(0,this->Nx3(1)+1,0,0,
  159. 0,this->Nx3(2)+1);
  160. vtkPolyDataMapper *rgridmapper2 = vtkPolyDataMapper::New();
  161. rgridmapper2->SetInputConnection(plane2->GetOutputPort());
  162. vtkActor *wireActor2 = vtkActor::New();
  163. wireActor2->SetMapper(rgridmapper2);
  164. wireActor2->GetProperty()->SetRepresentationToWireframe();
  165. wireActor2->GetProperty()->SetColor(0,1,0);
  166. //Fourth face
  167. vtkRectilinearGridGeometryFilter *plane3 =
  168. vtkRectilinearGridGeometryFilter::New();
  169. plane3->SetInputData(rgrid);
  170. plane3->SetExtent(0,this->Nx3(1)+1,this->Nx3(0)+1,this->Nx3(0)+1,
  171. 0,this->Nx3(2)+1);
  172. vtkPolyDataMapper *rgridmapper3 = vtkPolyDataMapper::New();
  173. rgridmapper3->SetInputConnection(plane3->GetOutputPort());
  174. vtkActor *wireActor3 = vtkActor::New();
  175. wireActor3->SetMapper(rgridmapper3);
  176. wireActor3->GetProperty()->SetRepresentationToWireframe();
  177. wireActor3->GetProperty()->SetColor(0,1,0);
  178. //Fifth face
  179. vtkRectilinearGridGeometryFilter *plane4 =
  180. vtkRectilinearGridGeometryFilter::New();
  181. plane4->SetInputData(rgrid);
  182. plane4->SetExtent(0,this->Nx3(1)+1,0,this->Nx3(0)+1,
  183. 0,0);
  184. vtkPolyDataMapper *rgridmapper4 = vtkPolyDataMapper::New();
  185. rgridmapper4->SetInputConnection(plane4->GetOutputPort());
  186. vtkActor *wireActor4 = vtkActor::New();
  187. wireActor4->SetMapper(rgridmapper4);
  188. wireActor4->GetProperty()->SetRepresentationToWireframe();
  189. wireActor4->GetProperty()->SetColor(0,1,0);
  190. //Sixth face
  191. vtkRectilinearGridGeometryFilter *plane5 =
  192. vtkRectilinearGridGeometryFilter::New();
  193. plane5->SetInputData(rgrid);
  194. plane5->SetExtent(0,this->Nx3(1)+1,0,this->Nx3(0)+1,
  195. this->Nx3(2)+1,this->Nx3(2)+1);
  196. vtkPolyDataMapper *rgridmapper5 = vtkPolyDataMapper::New();
  197. rgridmapper5->SetInputConnection(plane5->GetOutputPort());
  198. vtkActor *wireActor5 = vtkActor::New();
  199. wireActor5->SetMapper(rgridmapper5);
  200. wireActor5->GetProperty()->SetRepresentationToWireframe();
  201. wireActor5->GetProperty()->SetColor(0,1,0);
  202. vtkSmartPointer<vtkOutlineFilter> outlineFilter =
  203. vtkSmartPointer<vtkOutlineFilter>::New();
  204. outlineFilter->SetInputData(rgrid);
  205. vtkSmartPointer<vtkDataSetMapper> outlinemapper =
  206. vtkSmartPointer<vtkDataSetMapper>::New();
  207. outlinemapper->SetInputConnection(outlineFilter->GetOutputPort());
  208. vtkActor *outlineActor = vtkActor::New();
  209. outlineActor->SetMapper(outlinemapper);
  210. std::vector<vtkActor*> actorreturn;
  211. // Order of actors:
  212. // 1. outlineActor
  213. // 2-7. plane 0-5
  214. actorreturn.push_back(outlineActor);
  215. actorreturn.push_back(wireActor);
  216. actorreturn.push_back(wireActor1);
  217. actorreturn.push_back(wireActor2);
  218. actorreturn.push_back(wireActor3);
  219. actorreturn.push_back(wireActor4);
  220. actorreturn.push_back(wireActor5);
  221. return actorreturn;
  222. }
  223. std::vector<VectorXr> RectilinearStructuredGrid::GetCellDims() {
  224. return this->Dx3v;
  225. }
  226. VectorXr RectilinearStructuredGrid::GetCorners(const int& vecpos) {
  227. VectorXr corners;
  228. corners.resize(6);
  229. Vector3r cntr;
  230. cntr = this->VecCentroid.col(vecpos);
  231. corners(0) = cntr(0) - this->CW(0,vecpos)/2;
  232. corners(1) = cntr(0) + this->CW(0,vecpos)/2;
  233. corners(2) = cntr(1) - this->CW(1,vecpos)/2;
  234. corners(3) = cntr(1) + this->CW(1,vecpos)/2;
  235. corners(4) = cntr(2) - this->CW(2,vecpos)/2;
  236. corners(5) = cntr(2) + this->CW(2,vecpos)/2;
  237. return corners;
  238. }
  239. void RectilinearStructuredGrid::DisplayMesh() {
  240. // Coords are edges of cells, not centroids
  241. vtkFloatArray *xCoords = vtkFloatArray::New();
  242. vtkFloatArray *yCoords = vtkFloatArray::New();
  243. vtkFloatArray *zCoords = vtkFloatArray::New();
  244. Real val;
  245. val = this->Origin(0);
  246. xCoords->InsertNextValue(val);
  247. for (int ii=1;ii<=this->Nx3(0)+1;ii++) {
  248. val = this->Origin(0) + this->Dx3v[0].segment(0,ii-1).sum();
  249. xCoords->InsertNextValue(val);
  250. }
  251. val = this->Origin(1);
  252. yCoords->InsertNextValue(val);
  253. for (int ii=1;ii<=this->Nx3(1)+1;ii++) {
  254. val = this->Origin(1) + this->Dx3v[1].segment(0,ii-1).sum();
  255. yCoords->InsertNextValue(val);
  256. }
  257. val = -1 * this->Origin(2);
  258. zCoords->InsertNextValue(val);
  259. for (int ii=1;ii<=this->Nx3(2)+1;ii++) {
  260. val = this->Origin(2) + this->Dx3v[2].segment(0,ii-1).sum();
  261. val = -1*val;
  262. zCoords->InsertNextValue(val);
  263. }
  264. vtkRectilinearGrid *rgrid = vtkRectilinearGrid::New();
  265. rgrid->SetDimensions(this->Nx3(1)+2,this->Nx3(0)+2,this->Nx3(2)+2);
  266. rgrid->SetXCoordinates(yCoords);
  267. rgrid->SetYCoordinates(xCoords);
  268. rgrid->SetZCoordinates(zCoords);
  269. vtkRectilinearGridGeometryFilter *plane =
  270. vtkRectilinearGridGeometryFilter::New();
  271. plane->SetInputData(rgrid);
  272. //plane->SetExtent(0,this->Nx3(0),0,this->Nx3(1),0,this->Nx3(2));
  273. //Just for testing
  274. plane->SetExtent(0,0,0,this->Nx3(0)+1,0,this->Nx3(2)+1);
  275. vtkPolyDataMapper *rgridmapper = vtkPolyDataMapper::New();
  276. rgridmapper->SetInputConnection(plane->GetOutputPort());
  277. vtkActor *wireActor = vtkActor::New();
  278. wireActor->SetMapper(rgridmapper);
  279. wireActor->GetProperty()->SetRepresentationToWireframe();
  280. wireActor->GetProperty()->SetColor(0,1,0);
  281. //Make an actor for each face
  282. //Second face
  283. vtkRectilinearGridGeometryFilter *plane1 =
  284. vtkRectilinearGridGeometryFilter::New();
  285. plane1->SetInputData(rgrid);
  286. plane1->SetExtent(this->Nx3(1)+1,this->Nx3(1)+1,0,this->Nx3(0)+1,
  287. 0,this->Nx3(2)+1);
  288. vtkPolyDataMapper *rgridmapper1 = vtkPolyDataMapper::New();
  289. rgridmapper1->SetInputConnection(plane1->GetOutputPort());
  290. vtkActor *wireActor1 = vtkActor::New();
  291. wireActor1->SetMapper(rgridmapper1);
  292. wireActor1->GetProperty()->SetRepresentationToWireframe();
  293. wireActor1->GetProperty()->SetColor(0,1,0);
  294. //Third face
  295. vtkRectilinearGridGeometryFilter *plane2 =
  296. vtkRectilinearGridGeometryFilter::New();
  297. plane2->SetInputData(rgrid);
  298. plane2->SetExtent(0,this->Nx3(1)+1,0,0,
  299. 0,this->Nx3(2)+1);
  300. vtkPolyDataMapper *rgridmapper2 = vtkPolyDataMapper::New();
  301. rgridmapper2->SetInputConnection(plane2->GetOutputPort());
  302. vtkActor *wireActor2 = vtkActor::New();
  303. wireActor2->SetMapper(rgridmapper2);
  304. wireActor2->GetProperty()->SetRepresentationToWireframe();
  305. wireActor2->GetProperty()->SetColor(0,1,0);
  306. //Fourth face
  307. vtkRectilinearGridGeometryFilter *plane3 =
  308. vtkRectilinearGridGeometryFilter::New();
  309. plane3->SetInputData(rgrid);
  310. plane3->SetExtent(0,this->Nx3(1)+1,this->Nx3(0)+1,this->Nx3(0)+1,
  311. 0,this->Nx3(2)+1);
  312. vtkPolyDataMapper *rgridmapper3 = vtkPolyDataMapper::New();
  313. rgridmapper3->SetInputConnection(plane3->GetOutputPort());
  314. vtkActor *wireActor3 = vtkActor::New();
  315. wireActor3->SetMapper(rgridmapper3);
  316. wireActor3->GetProperty()->SetRepresentationToWireframe();
  317. wireActor3->GetProperty()->SetColor(0,1,0);
  318. //Fifth face
  319. vtkRectilinearGridGeometryFilter *plane4 =
  320. vtkRectilinearGridGeometryFilter::New();
  321. plane4->SetInputData(rgrid);
  322. plane4->SetExtent(0,this->Nx3(1)+1,0,this->Nx3(0)+1,
  323. 0,0);
  324. vtkPolyDataMapper *rgridmapper4 = vtkPolyDataMapper::New();
  325. rgridmapper4->SetInputConnection(plane4->GetOutputPort());
  326. vtkActor *wireActor4 = vtkActor::New();
  327. wireActor4->SetMapper(rgridmapper4);
  328. wireActor4->GetProperty()->SetRepresentationToWireframe();
  329. wireActor4->GetProperty()->SetColor(0,1,0);
  330. //Sixth face
  331. vtkRectilinearGridGeometryFilter *plane5 =
  332. vtkRectilinearGridGeometryFilter::New();
  333. plane5->SetInputData(rgrid);
  334. plane5->SetExtent(0,this->Nx3(1)+1,0,this->Nx3(0)+1,
  335. this->Nx3(2)+1,this->Nx3(2)+1);
  336. vtkPolyDataMapper *rgridmapper5 = vtkPolyDataMapper::New();
  337. rgridmapper5->SetInputConnection(plane5->GetOutputPort());
  338. vtkActor *wireActor5 = vtkActor::New();
  339. wireActor5->SetMapper(rgridmapper5);
  340. wireActor5->GetProperty()->SetRepresentationToWireframe();
  341. wireActor5->GetProperty()->SetColor(0,1,0);
  342. vtkSmartPointer<vtkOutlineFilter> outlineFilter =
  343. vtkSmartPointer<vtkOutlineFilter>::New();
  344. outlineFilter->SetInputData(rgrid);
  345. vtkSmartPointer<vtkDataSetMapper> outlinemapper =
  346. vtkSmartPointer<vtkDataSetMapper>::New();
  347. outlinemapper->SetInputConnection(outlineFilter->GetOutputPort());
  348. vtkActor *outlineActor = vtkActor::New();
  349. outlineActor->SetMapper(outlinemapper);
  350. vtkRenderer *renderer = vtkRenderer::New();
  351. vtkRenderWindow *renWin = vtkRenderWindow::New();
  352. renWin->AddRenderer(renderer);
  353. vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
  354. iren->SetRenderWindow(renWin);
  355. renderer->AddActor(wireActor);
  356. //renderer->AddActor(wireActor1);
  357. renderer->AddActor(wireActor2);
  358. //renderer->AddActor(wireActor3);
  359. //renderer->AddActor(wireActor4);
  360. renderer->AddActor(wireActor5);
  361. renderer->AddActor(outlineActor);
  362. renderer->SetBackground(0,0,0);
  363. renderer->ResetCamera();
  364. renderer->GetActiveCamera()->Elevation(40.0);
  365. renderer->GetActiveCamera()->Azimuth(15);
  366. renderer->GetActiveCamera()->Roll(200);
  367. renderer->GetActiveCamera()->Zoom(1.0);
  368. renWin->SetSize(900,900);
  369. renWin->Render();
  370. iren->Start();
  371. }
  372. }