Browse Source

Added some catches for bad VTK files.

master
T-bone 8 years ago
parent
commit
1e3a263483
4 changed files with 31 additions and 13 deletions
  1. 4
    2
      examples/ResampleWithDataset.cpp
  2. 3
    0
      examples/VTKEdgeGsphere.cpp
  3. 11
    3
      src/FEM4EllipticPDE.cpp
  4. 13
    8
      src/LinearMag.cpp

+ 4
- 2
examples/ResampleWithDataset.cpp View File

56
         }
56
         }
57
 
57
 
58
     } else {
58
     } else {
59
-
60
         vtkXMLUnstructuredGridReader* Reader = vtkXMLUnstructuredGridReader::New();
59
         vtkXMLUnstructuredGridReader* Reader = vtkXMLUnstructuredGridReader::New();
61
             std::cout << "Reading" << argv[1] << std::endl;
60
             std::cout << "Reading" << argv[1] << std::endl;
62
             Reader->SetFileName(argv[1]);
61
             Reader->SetFileName(argv[1]);
82
         Resample->SetValidPointMaskArrayName("HomogeneousDirichlet");
81
         Resample->SetValidPointMaskArrayName("HomogeneousDirichlet");
83
         Resample->Update();
82
         Resample->Update();
84
 
83
 
85
-    std::cout << *Resample << std::endl;
84
+    //std::cout << *Resample << std::endl;.
85
+
86
+   std::cout << "Resampled grid, ncells=" << Resample->GetOutput()->GetNumberOfCells()  
87
+             << " npoints=" << Resample->GetOutput()->GetNumberOfPoints() << std::endl; 
86
 
88
 
87
     vtkXMLUnstructuredGridWriter* Writer = vtkXMLUnstructuredGridWriter::New();
89
     vtkXMLUnstructuredGridWriter* Writer = vtkXMLUnstructuredGridWriter::New();
88
         Writer->SetInputData( Resample->GetOutput() );
90
         Writer->SetInputData( Resample->GetOutput() );

+ 3
- 0
examples/VTKEdgeGsphere.cpp View File

184
         uGrid->GetPointData()->SetScalars( G );
184
         uGrid->GetPointData()->SetScalars( G );
185
     }
185
     }
186
 
186
 
187
+    std::cout << "Writing file with ncells=" << uGrid->GetNumberOfCells() << "\tnpoints=" << uGrid->GetNumberOfPoints() << std::endl;
188
+
187
     // Write out new file
189
     // Write out new file
188
     vtkXMLUnstructuredGridWriter* Writer = vtkXMLUnstructuredGridWriter::New();
190
     vtkXMLUnstructuredGridWriter* Writer = vtkXMLUnstructuredGridWriter::New();
189
         //Writer->SetInputConnection(Reader->GetOutputPort());
191
         //Writer->SetInputConnection(Reader->GetOutputPort());
190
         Writer->SetInputData( uGrid );
192
         Writer->SetInputData( uGrid );
191
         Writer->SetFileName( argv[4] );
193
         Writer->SetFileName( argv[4] );
194
+        Writer->Update();
192
         Writer->Write();
195
         Writer->Write();
193
 
196
 
194
     //Reader->Delete();
197
     //Reader->Delete();

+ 11
- 3
src/FEM4EllipticPDE.cpp View File

129
     //      Method:  SetVTUGridFile
129
     //      Method:  SetVTUGridFile
130
     //--------------------------------------------------------------------------------------
130
     //--------------------------------------------------------------------------------------
131
     void FEM4EllipticPDE::SetVTUGridFile ( std::string& fname  ) {
131
     void FEM4EllipticPDE::SetVTUGridFile ( std::string& fname  ) {
132
-        std::cout  << "Loading VTK .vtu file " << fname << std::endl;
132
+        std::cout  << "Loading VTK .vtu file " << fname;
133
         vtkXMLUnstructuredGridReader* MeshReader = vtkXMLUnstructuredGridReader::New();
133
         vtkXMLUnstructuredGridReader* MeshReader = vtkXMLUnstructuredGridReader::New();
134
         MeshReader->SetFileName(fname.c_str());
134
         MeshReader->SetFileName(fname.c_str());
135
         MeshReader->Update();
135
         MeshReader->Update();
137
         //vtkGrid->ShallowCopy( MeshReader->GetOutput() );
137
         //vtkGrid->ShallowCopy( MeshReader->GetOutput() );
138
         vtkGrid = MeshReader->GetOutput();
138
         vtkGrid = MeshReader->GetOutput();
139
         MeshReader->Delete();
139
         MeshReader->Delete();
140
+        std::cout  << " Finished! " << std::endl;
140
         return ;
141
         return ;
141
     }		// -----  end of method FEM4EllipticPDE::SetVTKGridFile  -----
142
     }		// -----  end of method FEM4EllipticPDE::SetVTKGridFile  -----
142
 
143
 
145
     //      Method:  SetVTUGridFile
146
     //      Method:  SetVTUGridFile
146
     //--------------------------------------------------------------------------------------
147
     //--------------------------------------------------------------------------------------
147
     void FEM4EllipticPDE::SetVTUGridFile ( char* fname  ) {
148
     void FEM4EllipticPDE::SetVTUGridFile ( char* fname  ) {
148
-        std::cout  << "Loading VTK .vtu file " << fname << std::endl;
149
+        std::cout  << "Loading VTK .vtu file " << fname;
149
         vtkXMLUnstructuredGridReader* MeshReader = vtkXMLUnstructuredGridReader::New();
150
         vtkXMLUnstructuredGridReader* MeshReader = vtkXMLUnstructuredGridReader::New();
150
         MeshReader->SetFileName(fname);
151
         MeshReader->SetFileName(fname);
151
         MeshReader->Update();
152
         MeshReader->Update();
152
         //vtkGrid->DeepCopy( MeshReader->GetOutput() );
153
         //vtkGrid->DeepCopy( MeshReader->GetOutput() );
153
         //vtkGrid->ShallowCopy( MeshReader->GetOutput() );
154
         //vtkGrid->ShallowCopy( MeshReader->GetOutput() );
154
         vtkGrid = MeshReader->GetOutput();
155
         vtkGrid = MeshReader->GetOutput();
155
-        MeshReader->Delete();
156
+        if (!vtkGrid->GetNumberOfCells()) {
157
+            throw std::runtime_error("In FEM4EllipticPDE::SetVTUGridFile NUMBER OF CELLS = 0");
158
+	}
159
+        if (!vtkGrid->GetNumberOfPoints()) {
160
+            throw std::runtime_error("In FEM4EllipticPDE::SetVTUGridFile NUMBER OF POINTS = 0");
161
+	}
162
+	MeshReader->Delete();
163
+        std::cout  << " Finished! " << std::endl;
156
         return ;
164
         return ;
157
     }		// -----  end of method FEM4EllipticPDE::SetVTKGridFile  -----
165
     }		// -----  end of method FEM4EllipticPDE::SetVTKGridFile  -----
158
 
166
 

+ 13
- 8
src/LinearMag.cpp View File

174
 //--------------------------------------------------------------------------------------
174
 //--------------------------------------------------------------------------------------
175
 void LinearMag::CalculateRHS ( const std::string& susName ) {
175
 void LinearMag::CalculateRHS ( const std::string& susName ) {
176
 
176
 
177
+    std::cout << "Calculating RHS...";
178
+    std::cout.flush();
179
+
177
     if ( !vtkGrid->GetCellData()->GetScalars(susName.c_str()) ) {
180
     if ( !vtkGrid->GetCellData()->GetScalars(susName.c_str()) ) {
178
         std::string err("No cell data by name ");
181
         std::string err("No cell data by name ");
179
         err.append(susName);
182
         err.append(susName);
180
         throw std::runtime_error(err.c_str());
183
         throw std::runtime_error(err.c_str());
181
     }
184
     }
182
 
185
 
186
+    if (!vtkGrid->GetNumberOfPoints()) {
187
+        throw std::runtime_error("Number of points zero in input grid!");
188
+    }
189
+
183
     vtkDoubleArray* G = vtkDoubleArray::New();
190
     vtkDoubleArray* G = vtkDoubleArray::New();
184
     G->SetNumberOfComponents(1);
191
     G->SetNumberOfComponents(1);
185
     G->SetNumberOfTuples( vtkGrid->GetNumberOfPoints() );
192
     G->SetNumberOfTuples( vtkGrid->GetNumberOfPoints() );
186
     G->SetName("G");
193
     G->SetName("G");
187
     //g.resize(vtkGrid->GetNumberOfPoints());
194
     //g.resize(vtkGrid->GetNumberOfPoints());
188
     VectorXr GG = VectorXr::Zero( vtkGrid->GetNumberOfPoints() );
195
     VectorXr GG = VectorXr::Zero( vtkGrid->GetNumberOfPoints() );
189
-
196
+    
190
     // Iterate over all the points or all of the cells?
197
     // Iterate over all the points or all of the cells?
191
     for (int ic=0; ic < vtkGrid->GetNumberOfCells(); ++ic) {
198
     for (int ic=0; ic < vtkGrid->GetNumberOfCells(); ++ic) {
192
 
199
 
193
         if ( vtkGrid->GetCell(ic)->GetNumberOfPoints() != 4 ) {
200
         if ( vtkGrid->GetCell(ic)->GetNumberOfPoints() != 4 ) {
194
             throw std::runtime_error("Non-tetrahedral mesh encountered!");
201
             throw std::runtime_error("Non-tetrahedral mesh encountered!");
195
         }
202
         }
196
-
197
         Real cellSus = vtkGrid->GetCellData()->GetScalars(susName.c_str())->GetTuple(ic)[0];
203
         Real cellSus = vtkGrid->GetCellData()->GetScalars(susName.c_str())->GetTuple(ic)[0];
198
 
204
 
199
         Eigen::Matrix<Real, 4, 4> C = Eigen::Matrix<Real, 4, 4>::Zero() ;
205
         Eigen::Matrix<Real, 4, 4> C = Eigen::Matrix<Real, 4, 4>::Zero() ;
221
         */
227
         */
222
         // Face 0, ID 0,1,2
228
         // Face 0, ID 0,1,2
223
         Eigen::Matrix<Real, 3, 2> CC = Eigen::Matrix<Real, 3, 2>::Ones() ;
229
         Eigen::Matrix<Real, 3, 2> CC = Eigen::Matrix<Real, 3, 2>::Ones() ;
224
-        {
225
-
226
             CC.col(1) = C.row(0).tail<3>() - C.row(1).tail<3>();
230
             CC.col(1) = C.row(0).tail<3>() - C.row(1).tail<3>();
227
             CC.col(2) = C.row(0).tail<3>() - C.row(2).tail<3>();
231
             CC.col(2) = C.row(0).tail<3>() - C.row(2).tail<3>();
228
             Vector3r nhat = CC.col(1).cross(CC.col(2));
232
             Vector3r nhat = CC.col(1).cross(CC.col(2));
231
             GG(ID[0]) += flux;
235
             GG(ID[0]) += flux;
232
             GG(ID[1]) += flux;
236
             GG(ID[1]) += flux;
233
             GG(ID[2]) += flux;
237
             GG(ID[2]) += flux;
234
-        }
235
-        // Face 1, ID 0,1,3
238
+	// Face 1, ID 0,1,3
236
         {
239
         {
237
             CC.col(1) = C.row(0).tail<3>() - C.row(1).tail<3>();
240
             CC.col(1) = C.row(0).tail<3>() - C.row(1).tail<3>();
238
             CC.col(2) = C.row(0).tail<3>() - C.row(3).tail<3>();
241
             CC.col(2) = C.row(0).tail<3>() - C.row(3).tail<3>();
243
             GG(ID[1]) += flux;
246
             GG(ID[1]) += flux;
244
             GG(ID[3]) += flux;
247
             GG(ID[3]) += flux;
245
         }
248
         }
246
-        // Face 1, ID 0,2,3
249
+        // Face 2, ID 0,2,3
247
         {
250
         {
248
             CC.col(1) = C.row(0).tail<3>() - C.row(2).tail<3>();
251
             CC.col(1) = C.row(0).tail<3>() - C.row(2).tail<3>();
249
             CC.col(2) = C.row(0).tail<3>() - C.row(3).tail<3>();
252
             CC.col(2) = C.row(0).tail<3>() - C.row(3).tail<3>();
254
             GG(ID[2]) += flux;
257
             GG(ID[2]) += flux;
255
             GG(ID[3]) += flux;
258
             GG(ID[3]) += flux;
256
         }
259
         }
257
-        // Face 1, ID 1,2,3
260
+        // Face 3, ID 1,2,3
258
         {
261
         {
259
             CC.col(1) = C.row(1).tail<3>() - C.row(2).tail<3>();
262
             CC.col(1) = C.row(1).tail<3>() - C.row(2).tail<3>();
260
             CC.col(2) = C.row(1).tail<3>() - C.row(3).tail<3>();
263
             CC.col(2) = C.row(1).tail<3>() - C.row(3).tail<3>();
273
 
276
 
274
     vtkGrid->GetPointData()->AddArray( G );
277
     vtkGrid->GetPointData()->AddArray( G );
275
     vtkGrid->GetPointData()->SetScalars( G );
278
     vtkGrid->GetPointData()->SetScalars( G );
279
+    std::cout << "finished" << std::endl;
280
+
276
     return ;
281
     return ;
277
 }		// -----  end of method LinearMag::CalculateRHS  -----
282
 }		// -----  end of method LinearMag::CalculateRHS  -----
278
 
283
 

Loading…
Cancel
Save