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