55{
56
57 if (!this->FileName)
58 {
59 vtkErrorMacro(<<"FileName not set.");
60 return;
61 }
62 std::ofstream out (this->FileName);
63 if (!out.good())
64 {
65 vtkErrorMacro(<<"Could not open file for writing.");
66 return;
67 }
68
69
70 vtkUnstructuredGrid *input = vtkUnstructuredGrid::SafeDownCast(this->GetInput());
71 input->BuildLinks();
72 const int numberOfPoints = input->GetNumberOfPoints();
73 const int numberOfCells = input->GetNumberOfCells();
74
75
76 vtkIdTypeArray* cellEntityIds = NULL;
78 {
80 if (array)
81 {
82 cellEntityIds = vtkIdTypeArray::New();
83 cellEntityIds->DeepCopy(array);
84 }
85 else
86 {
87 vtkErrorMacro(<<"Array with specified BoundaryDataArrayName does not exist");
88 }
89 }
90
91
92 vtkIdTypeArray* tetraCellIdArray = vtkIdTypeArray::New();
93 input->GetIdsOfCellsOfType(VTK_TETRA, tetraCellIdArray);
94 const int numberOfTetras = tetraCellIdArray->GetNumberOfTuples();
95 const int numberOfTetraPoints = 4;
96
97
98 vtkIdList* volumeCellIdMap = vtkIdList::New();
99 volumeCellIdMap->SetNumberOfIds(numberOfCells);
100 for (int i=0; i<numberOfTetras; i++)
101 {
102 vtkIdType tetraCellId = tetraCellIdArray->GetValue(i);
103 volumeCellIdMap->SetId(tetraCellId,i);
104 }
105
106
107 out << "<?xml version=\"1.0\"?>" << endl;
108 out << "<dolfin xmlns:dolfin=\"http://www.fenicsproject.org\">" << endl;
109 out << " <mesh celltype=\"tetrahedron\" dim=\"3\">" << endl;
110
111
112 out << " <vertices size=\""<< numberOfPoints << "\">" << endl;
113 for (int i=0; i<numberOfPoints; i++)
114 {
115 double point[3];
116 input->GetPoint(i,point);
117 out << " <vertex index=\"" << i << "\" ";
118 out << "x=\"" << point[0] << "\" ";
119 out << "y=\"" << point[1] << "\" ";
120 out << "z=\"" << point[2] << "\" ";
121 out << "/>" <<endl;
122 }
123 out << " </vertices>" << endl;
124
125
126 out << " <cells size=\"" << numberOfTetras << "\">" << endl;
127 for (int i=0; i<numberOfTetras; i++)
128 {
129 vtkIdType tetraCellId = tetraCellIdArray->GetValue(i);
130 vtkIdList* cellPointIds = input->GetCell(tetraCellId)->GetPointIds();
131
132
133 vtkIdType dolfinCellPointIds[numberOfTetraPoints];
134 for (int k=0; k<numberOfTetraPoints; k++)
135 {
136 dolfinCellPointIds[k] = cellPointIds->GetId(k);
137 }
138 std::sort(dolfinCellPointIds, dolfinCellPointIds+numberOfTetraPoints);
139
140
141 out << " <tetrahedron index=\"" << i << "\" ";
142 for (int k=0; k<numberOfTetraPoints; k++)
143 {
144 out << "v" << k << "=\"" << dolfinCellPointIds[k] << "\" ";
145 }
146 out << "/>" << endl;
147 }
148 out << " </cells>" << endl;
149
150
151 if (cellEntityIds)
152 {
153 vtkIdTypeArray* triangleCellIdArray = vtkIdTypeArray::New();
154 input->GetIdsOfCellsOfType(VTK_TRIANGLE,triangleCellIdArray);
155 const int numberOfTriangles = triangleCellIdArray->GetNumberOfTuples();
156
157 vtkIdList* triangleToTetrahedron = vtkIdList::New();
158 triangleToTetrahedron->SetNumberOfIds(numberOfTriangles);
159
160 vtkIdList* triangleToLocalFacetId = vtkIdList::New();
161 triangleToLocalFacetId->SetNumberOfIds(numberOfTriangles);
162
163 int interiorFacetsFound = 0;
164 int exteriorFacetsFound = 0;
165 for (int i=0; i<numberOfTriangles; i++)
166 {
167 const vtkIdType triangleCellId = triangleCellIdArray->GetValue(i);
168
169 vtkIdList* faceCellPoints = vtkIdList::New();
170 input->GetCellPoints(triangleCellId,faceCellPoints);
171
172 vtkIdList* cellIds = vtkIdList::New();
173 input->GetCellNeighbors(triangleCellId,faceCellPoints,cellIds);
174
175 if (cellIds->GetNumberOfIds() != 1)
176 {
177 interiorFacetsFound++;
178 }
179 else
180 {
181 exteriorFacetsFound++;
182 }
183
184
185 vtkIdType cellId = cellIds->GetId(0);
186 if (cellIds->GetNumberOfIds() == 2 && cellIds->GetId(1) < cellId)
187 {
188 cellId = cellIds->GetId(1);
189 }
190 vtkCell* cell = input->GetCell(cellId);
191 int cellType = cell->GetCellType();
192 vtkIdList* cellPointIds = cell->GetPointIds();
193
194
195 if (cellType != VTK_TETRA)
196 {
197 vtkErrorMacro(<<"Volume cell adjacent to triangle is not tetrahedron (volume cell id: "<<cellId <<") and it is unsupported by Dolfin. Skipping face.");
198 faceCellPoints->Delete();
199 cellIds->Delete();
200 continue;
201 }
202
203
204 vtkIdType dolfinCellPointIds[numberOfTetraPoints];
205 for (int k=0; k<numberOfTetraPoints; k++)
206 {
207 dolfinCellPointIds[k] = cellPointIds->GetId(k);
208 }
209 std::sort(dolfinCellPointIds, dolfinCellPointIds+numberOfTetraPoints);
210
211
212 vtkIdType dolfinFaceId = -1;
213 for (int k=0; k<numberOfTetraPoints; k++)
214 {
215 bool found = false;
216 const int numberOfTrianglePoints = 3;
217 for (int j=0; j<numberOfTrianglePoints; j++)
218 {
219 if (dolfinCellPointIds[k] == faceCellPoints->GetId(j))
220 {
221 found = true;
222 break;
223 }
224 }
225 if (!found)
226 {
227 dolfinFaceId = k;
228 break;
229 }
230 }
231
232
233 triangleToTetrahedron->SetId(i, volumeCellIdMap->GetId(cellId));
234
235 triangleToLocalFacetId->SetId(i, dolfinFaceId);
236
237
238 faceCellPoints->Delete();
239 cellIds->Delete();
240 }
241
242
244 {
245 out << " <domains>" << endl;
246 }
247
248
249 if (numberOfTriangles)
250 {
251 out << " <mesh_value_collection type=\"uint\" dim=\"2\" size=\""<< numberOfTriangles<< "\">" << endl;
252 for (int i=0; i<numberOfTriangles; i++)
253 {
254 const vtkIdType triangleCellId = triangleCellIdArray->GetValue(i);
255 const vtkIdType tetrahedronCellId = triangleToTetrahedron->GetId(i);
257 const vtkIdType localEntity = triangleToLocalFacetId->GetId(i);
258
259 out << " <value cell_index=\"" << tetrahedronCellId <<"\""
260 << " local_entity=\"" << localEntity << "\""
261 << " value=\"" << value << "\""
262 << " />" << endl;
263 }
264 out << " </mesh_value_collection>" << endl;
265 }
266
267
269 {
270 out << " <mesh_value_collection type=\"uint\" dim=\"3\" size=\""<< numberOfTetras << "\">" << endl;
271 for (int i=0; i<numberOfTetras; i++)
272 {
273 const vtkIdType cellId = tetraCellIdArray->GetValue(i);
274 const vtkIdType value = cellEntityIds->GetValue(cellId);
275 out << " <value cell_index=\"" << i << "\""
276 << " local_entity=\"" << 0 << "\""
277 << " value=\"" << value << "\""
278 << " />" << endl;
279 }
280 out << " </mesh_value_collection>" << endl;
281 }
282
283
285 {
286 out << " </domains>" << endl;
287 }
288
289 if (interiorFacetsFound)
290 {
291 vtkWarningMacro("Found boundary cells not on boundary!");
292 }
293
294 triangleCellIdArray->Delete();
295 triangleToTetrahedron->Delete();
296 triangleToLocalFacetId->Delete();
297 }
298
299 out << " </mesh>" << endl;
300 out << "</dolfin>" << endl;
301
302 tetraCellIdArray->Delete();
303 volumeCellIdMap->Delete();
304
305 if (cellEntityIds)
306 {
307 cellEntityIds->Delete();
308 }
309}