Galerkin FEM for elliptic PDEs
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.

setupGravSphere.cpp 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 08/07/2014 04:16:25 PM
  11. * @version $Id$
  12. * @author Trevor Irons (ti)
  13. * @email Trevor.Irons@xri-geo.com
  14. * @copyright Copyright (c) 2014, XRI Geophysics, LLC
  15. * @copyright Copyright (c) 2014, Trevor Irons
  16. */
  17. #include <vtkUnstructuredGrid.h>
  18. #include <vtkUnstructuredGridReader.h>
  19. #include <vtkUnstructuredGridWriter.h>
  20. #include <vtkGenericDataObjectReader.h>
  21. #include <vtkXMLUnstructuredGridReader.h>
  22. #include <vtkXMLUnstructuredGridWriter.h>
  23. #include <vtkDoubleArray.h>
  24. #include <vtkPointData.h>
  25. #include <vtkCellData.h>
  26. #include <vtkCell.h>
  27. #include <vtkCellCenters.h>
  28. #include <cmath>
  29. #include <Eigen/Core>
  30. const double PI = 4.0*atan(1.0);
  31. int main(int argc, char**argv) {
  32. std::cout << "Mesh processing routine\n";
  33. if (argc<4) {
  34. std::cout << "usage:\n" << "./utVTKEdge inMesh.vtu <radius> <problemType> outG.vtu" << std::endl;
  35. exit(EXIT_SUCCESS);
  36. }
  37. vtkUnstructuredGrid* uGrid = vtkUnstructuredGrid::New();
  38. std::string fn = argv[1];
  39. if(fn.substr(fn.find_last_of(".") + 1) == "vtk") {
  40. vtkGenericDataObjectReader* Reader = vtkGenericDataObjectReader::New();
  41. Reader->SetFileName(argv[1]);
  42. Reader->Update();
  43. if(Reader->IsFileUnstructuredGrid()) {
  44. std::cout << "Found Unstructured grid legacy file" << std::endl;
  45. uGrid = Reader->GetUnstructuredGridOutput();
  46. } else {
  47. std::cerr << "Unknown legacy format";
  48. exit(EXIT_FAILURE);
  49. }
  50. } else {
  51. vtkXMLUnstructuredGridReader* Reader = vtkXMLUnstructuredGridReader::New();
  52. std::cout << "Reading" << argv[1] << std::endl;
  53. Reader->SetFileName(argv[1]);
  54. Reader->Update();
  55. uGrid = Reader->GetOutput();
  56. }
  57. int nc = uGrid->GetNumberOfCells() ;
  58. int nn = uGrid->GetNumberOfPoints() ;
  59. std::cout << "Processing grid with nodes=" << nn << "\t cells=" << nc << "\n";
  60. // Input magnet size TODO get from file or .geo file even?
  61. double R = atof(argv[2]); //.25; // radius of magnet
  62. double eps = 1e-4; // epsilon for on the point
  63. // Pol = double[3];
  64. // Declare new array for the data
  65. vtkDoubleArray* G = vtkDoubleArray::New();
  66. G->SetNumberOfComponents(1);
  67. G->SetNumberOfTuples(nc);
  68. G->SetName("G");
  69. vtkDoubleArray* phi = vtkDoubleArray::New();
  70. phi->SetNumberOfComponents(1);
  71. phi->SetNumberOfTuples(nn);
  72. phi->SetName("analytic_phi");
  73. // Set point data
  74. double point[3];
  75. Eigen::Vector3d M; M << 0,0,1;
  76. for (int in=0; in<nn; ++in) {
  77. uGrid->GetPoint(in, &point[0]);
  78. //std::cout << point[0] << "\t" <<point[1] << "\t" << point[2] << std::endl;
  79. double raddist = sqrt( point[0]*point[0] + point[1]*point[1] + point[2]*point[2] );
  80. //double rho = sqrt( point[1]*point[1] + point[2]*point[2] );
  81. // Calculate RHS
  82. // Insert analytic phi part
  83. /* magnetics problem p. 198 Jackson */
  84. if (std::string(argv[3]) == "magnet") {
  85. if (raddist < R) {
  86. phi->InsertTuple1( in, (1./3.)*point[2] );
  87. } else {
  88. phi->InsertTuple1( in, 0);
  89. double costheta = point[2]/raddist ;
  90. //phi->InsertTuple1( in, -(1./3.)*(R*R*R) * ( costheta / (rho*rho) ) );
  91. phi->InsertTuple1( in, (1./3.) * (R*R*R) * (costheta / (raddist*raddist)) );
  92. }
  93. } else if (std::string(argv[3]) == "gravity") {
  94. double mass = (4.*PI/3.) * (R*R*R); // volume * density (1)
  95. double G = 1/(4.*PI);
  96. if (raddist < R) {
  97. phi->InsertTuple1( in, mass * G * (( 3*(R*R) - raddist*raddist )/(2*(R*R*R))) ); // (1./3.)*point[2] );
  98. //phi->InsertTuple1( in, (2*PI/3)*(3*R*R-raddist*raddist) ); // (1./3.)*point[2] );
  99. } else {
  100. //phi->InsertTuple1( in, 0);
  101. //double costheta = point[2]/raddist ;
  102. //phi->InsertTuple1( in, -(1./3.)*(R*R*R) * ( costheta / (rho*rho) ) );
  103. phi->InsertTuple1( in, G * mass/raddist );
  104. }
  105. }
  106. }
  107. vtkCellCenters* cellCentersFilter = vtkCellCenters::New();
  108. cellCentersFilter->SetInputData( uGrid );
  109. cellCentersFilter->VertexCellsOn();
  110. cellCentersFilter->Update();
  111. // Set Cell Data
  112. for (int ic=0; ic<nc; ++ic) {
  113. //uGrid->GetCell(ic)->GetParametricCenter( &point[0] );
  114. cellCentersFilter->GetOutput()->GetPoint(ic, &point[0]);
  115. double raddist = sqrt( point[0]*point[0] + point[1]*point[1] + point[2]*point[2] );
  116. //std::cout << "point " << point[0] << "\t" << point[1] << "\t" << point[2] << std::endl;
  117. if ( std::string(argv[3]) == "gravity") {
  118. if ( raddist < R + eps) {
  119. G->InsertTuple1( ic, 1 );
  120. } else {
  121. G->InsertTuple1( ic, 0 );
  122. }
  123. }
  124. }
  125. // Add new data
  126. uGrid->GetPointData()->AddArray( phi );
  127. uGrid->GetCellData()->AddArray( G );
  128. // Write out new file
  129. vtkXMLUnstructuredGridWriter* Writer = vtkXMLUnstructuredGridWriter::New();
  130. //Writer->SetInputConnection(Reader->GetOutputPort());
  131. Writer->SetInputData( uGrid );
  132. Writer->SetFileName( argv[4] );
  133. Writer->Write();
  134. //Reader->Delete();
  135. }