Chaste Commit::6e4f5fe395bca70eb7641cf6e0e87f450383ca5a
VertexMeshWriter.cpp
1/*
2
3Copyright (c) 2005-2026, University of Oxford.
4All rights reserved.
5
6University of Oxford means the Chancellor, Masters and Scholars of the
7University of Oxford, having an administrative office at Wellington
8Square, Oxford OX1 2JD, UK.
9
10This file is part of Chaste.
11
12Redistribution and use in source and binary forms, with or without
13modification, are permitted provided that the following conditions are met:
14 * Redistributions of source code must retain the above copyright notice,
15 this list of conditions and the following disclaimer.
16 * Redistributions in binary form must reproduce the above copyright notice,
17 this list of conditions and the following disclaimer in the documentation
18 and/or other materials provided with the distribution.
19 * Neither the name of the University of Oxford nor the names of its
20 contributors may be used to endorse or promote products derived from this
21 software without specific prior written permission.
22
23THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
34*/
35
36#include "VertexMeshWriter.hpp"
37#include "Version.hpp"
38
42template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
44{
49};
50
52// Implementation
54
55template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
57 const std::string& rBaseName,
58 const bool clearOutputDir)
59 : AbstractMeshWriter<ELEMENT_DIM, SPACE_DIM>(rDirectory, rBaseName, clearOutputDir),
60 mpMesh(nullptr),
61 mpIters(new MeshWriterIterators<ELEMENT_DIM, SPACE_DIM>),
62 mpNodeMap(nullptr),
63 mNodeMapCurrentIndex(0)
64{
65 mpIters->pNodeIter = nullptr;
66 mpIters->pElemIter = nullptr;
67
68#ifdef CHASTE_VTK
69 // Dubious, since we shouldn't yet know what any details of the mesh are.
70 mpVtkUnstructedMesh = vtkUnstructuredGrid::New();
71#endif //CHASTE_VTK
72}
73
74template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
76{
77 if (mpIters->pNodeIter)
78 {
79 delete mpIters->pNodeIter;
80 delete mpIters->pElemIter;
81 }
82
83 delete mpIters;
84
85 if (mpNodeMap)
86 {
87 delete mpNodeMap;
88 }
89
90#ifdef CHASTE_VTK
91 // Dubious, since we shouldn't yet know what any details of the mesh are.
92 mpVtkUnstructedMesh->Delete(); // Reference counted
93#endif //CHASTE_VTK
94}
95
96template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
98{
99 if (mpMesh)
100 {
101 // Sanity check
102 assert(this->mNumNodes == mpMesh->GetNumNodes());
103
104 std::vector<double> coordinates(SPACE_DIM+1);
105
106 // Get the node coordinates using the node iterator (thus skipping deleted nodes)
107 for (unsigned j=0; j<SPACE_DIM; j++)
108 {
109 coordinates[j] = (*(mpIters->pNodeIter))->GetPoint()[j];
110 }
111 coordinates[SPACE_DIM] = (*(mpIters->pNodeIter))->IsBoundaryNode();
112
113 ++(*(mpIters->pNodeIter));
114
115 return coordinates;
116 }
117 else
118 {
120 }
121}
122
123template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
125{
126 // This method should only be called in 3D
127 assert(SPACE_DIM == 3); // LCOV_EXCL_LINE
128 assert(mpMesh);
129 assert(this->mNumElements == mpMesh->GetNumElements());
130
131 // Create data structure for this element
132 VertexElementData elem_data;
133
134 // Store node indices owned by this element
135 elem_data.NodeIndices.resize((*(mpIters->pElemIter))->GetNumNodes());
136 for (unsigned j=0; j<elem_data.NodeIndices.size(); j++)
137 {
138 unsigned old_index = (*(mpIters->pElemIter))->GetNodeGlobalIndex(j);
139 elem_data.NodeIndices[j] = mpMesh->IsMeshChanging() ? mpNodeMap->GetNewIndex(old_index) : old_index;
140 }
141
142 // Store faces owned by this element
143 elem_data.Faces.resize((*(mpIters->pElemIter))->GetNumFaces());
144 for (unsigned i=0; i<elem_data.Faces.size(); i++)
145 {
146 // Get pointer to this face
147 VertexElement<ELEMENT_DIM-1, SPACE_DIM>* p_face = (*(mpIters->pElemIter))->GetFace(i);
148
149 // Create data structure for this face
150 ElementData face_data;
151
152 // Store this face's index
153 face_data.AttributeValue = p_face->GetIndex();
154
155 // Store node indices owned by this face
156 face_data.NodeIndices.resize(p_face->GetNumNodes());
157 for (unsigned j=0; j<face_data.NodeIndices.size(); j++)
158 {
159 unsigned old_index = p_face->GetNodeGlobalIndex(j);
160 face_data.NodeIndices[j] = mpMesh->IsMeshChanging() ? mpNodeMap->GetNewIndex(old_index) : old_index;
161 }
162
163 // Store this face
164 elem_data.Faces[i] = face_data;
165
167 }
168
169 // Set attribute
170 elem_data.AttributeValue = (unsigned)(*(mpIters->pElemIter))->GetAttribute();
171 ++(*(mpIters->pElemIter));
172
173 return elem_data;
174}
175
176template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
178{
180
181 if (mpMesh)
182 {
183 assert(this->mNumElements == mpMesh->GetNumElements());
184
185 ElementData elem_data;
186 elem_data.NodeIndices.resize((*(mpIters->pElemIter))->GetNumNodes());
187 for (unsigned j=0; j<elem_data.NodeIndices.size(); j++)
188 {
189 unsigned old_index = (*(mpIters->pElemIter))->GetNodeGlobalIndex(j);
190 elem_data.NodeIndices[j] = mpMesh->IsMeshChanging() ? mpNodeMap->GetNewIndex(old_index) : old_index;
191 }
192
193 // Set attribute
194 elem_data.AttributeValue = (*(mpIters->pElemIter))->GetAttribute();
195 ++(*(mpIters->pElemIter));
196
197 return elem_data;
198 }
199 else
200 {
202 }
203}
204
205template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
207{
208#ifdef CHASTE_VTK
209 assert(SPACE_DIM==3 || SPACE_DIM == 2); // LCOV_EXCL_LINE
210
211 // Create VTK mesh
212 MakeVtkMesh(rMesh);
213
214 // Now write VTK mesh to file
215 assert(mpVtkUnstructedMesh->CheckAttributes() == 0);
216 vtkXMLUnstructuredGridWriter* p_writer = vtkXMLUnstructuredGridWriter::New();
217#if VTK_MAJOR_VERSION >= 6
218 p_writer->SetInputData(mpVtkUnstructedMesh);
219#else
220 p_writer->SetInput(mpVtkUnstructedMesh);
221#endif
222 // Uninitialised stuff arises (see #1079), but you can remove valgrind problems by removing compression:
223 // **** REMOVE WITH CAUTION *****
224 p_writer->SetCompressor(nullptr);
225 // **** REMOVE WITH CAUTION *****
226
227 std::string vtk_file_name = this->mpOutputFileHandler->GetOutputDirectoryFullPath() + this->mBaseName;
228 if (stamp != "")
229 {
230 vtk_file_name += "_" + stamp;
231 }
232 vtk_file_name += ".vtu";
233
234 p_writer->SetFileName(vtk_file_name.c_str());
235 //p_writer->PrintSelf(std::cout, vtkIndent());
236 p_writer->Write();
237 p_writer->Delete(); // Reference counted
238#endif //CHASTE_VTK
239}
240
247template<>
249{
250#ifdef CHASTE_VTK
251 // Create VTK mesh
252 VertexMesh<2, 2>* p_mesh_for_vtk = rMesh.GetMeshForVtk();
253 MakeVtkMesh(*p_mesh_for_vtk);
254
255 // Now write VTK mesh to file
256 assert(mpVtkUnstructedMesh->CheckAttributes() == 0);
257 vtkXMLUnstructuredGridWriter* p_writer = vtkXMLUnstructuredGridWriter::New();
258#if VTK_MAJOR_VERSION >= 6
259 p_writer->SetInputData(mpVtkUnstructedMesh);
260#else
261 p_writer->SetInput(mpVtkUnstructedMesh);
262#endif
263 // Uninitialised stuff arises (see #1079), but you can remove valgrind problems by removing compression:
264 // **** REMOVE WITH CAUTION *****
265 p_writer->SetCompressor(nullptr);
266 // **** REMOVE WITH CAUTION *****
267
268 std::string vtk_file_name = this->mpOutputFileHandler->GetOutputDirectoryFullPath() + this->mBaseName;
269 if (stamp != "")
270 {
271 vtk_file_name += "_" + stamp;
272 }
273 vtk_file_name += ".vtu";
274
275 p_writer->SetFileName(vtk_file_name.c_str());
276 //p_writer->PrintSelf(std::cout, vtkIndent());
277 p_writer->Write();
278 p_writer->Delete(); // Reference counted
279#endif //CHASTE_VTK
280}
281
282template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
284{
285#ifdef CHASTE_VTK
286 // Make the Vtk mesh
287 vtkPoints* p_pts = vtkPoints::New(VTK_DOUBLE);
288 p_pts->GetData()->SetName("Vertex positions");
289 for (unsigned node_num = 0; node_num < rMesh.GetNumNodes(); node_num++)
290 {
291 c_vector<double, SPACE_DIM> position;
292 position = rMesh.GetNode(node_num)->rGetLocation();
293 if constexpr (SPACE_DIM == 2)
294 {
295 p_pts->InsertPoint(node_num, position[0], position[1], 0.0);
296 }
297 else if constexpr (SPACE_DIM == 3)
298 {
299 p_pts->InsertPoint(node_num, position[0], position[1], position[2]);
300 }
301 else
302 {
304 }
305 }
306
307 mpVtkUnstructedMesh->SetPoints(p_pts);
308 p_pts->Delete(); // Reference counted
310 iter != rMesh.GetElementIteratorEnd();
311 ++iter)
312 {
313 vtkCell* p_cell;
314 if (SPACE_DIM == 2)
315 {
316 p_cell = vtkPolygon::New();
317 }
318 else
319 {
320 p_cell = vtkConvexPointSet::New();
321 }
322 vtkIdList* p_cell_id_list = p_cell->GetPointIds();
323 p_cell_id_list->SetNumberOfIds(iter->GetNumNodes());
324 for (unsigned j=0; j<iter->GetNumNodes(); ++j)
325 {
326 p_cell_id_list->SetId(j, iter->GetNodeGlobalIndex(j));
327 }
328 mpVtkUnstructedMesh->InsertNextCell(p_cell->GetCellType(), p_cell_id_list);
329 p_cell->Delete(); // Reference counted
330 }
331#endif //CHASTE_VTK
332}
333
334template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
335void VertexMeshWriter<ELEMENT_DIM, SPACE_DIM>::AddCellData(std::string dataName, std::vector<double> dataPayload)
336{
337#ifdef CHASTE_VTK
338 vtkDoubleArray* p_scalars = vtkDoubleArray::New();
339 p_scalars->SetName(dataName.c_str());
340 for (unsigned i=0; i<dataPayload.size(); i++)
341 {
342 p_scalars->InsertNextValue(dataPayload[i]);
343 }
344
345 vtkCellData* p_cell_data = mpVtkUnstructedMesh->GetCellData();
346 p_cell_data->AddArray(p_scalars);
347 p_scalars->Delete(); // Reference counted
348#endif //CHASTE_VTK
349}
350
351template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
352void VertexMeshWriter<ELEMENT_DIM, SPACE_DIM>::AddPointData(std::string dataName, std::vector<double> dataPayload)
353{
354#ifdef CHASTE_VTK
355 vtkDoubleArray* p_scalars = vtkDoubleArray::New();
356 p_scalars->SetName(dataName.c_str());
357 for (unsigned i=0; i<dataPayload.size(); i++)
358 {
359 p_scalars->InsertNextValue(dataPayload[i]);
360 }
361
362 vtkPointData* p_point_data = mpVtkUnstructedMesh->GetPointData();
363 p_point_data->AddArray(p_scalars);
364 p_scalars->Delete(); // Reference counted
365#endif //CHASTE_VTK
366}
367
369template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
371{
372 this->mpMeshReader = nullptr;
373 mpMesh = &rMesh;
374
375 this->mNumNodes = mpMesh->GetNumNodes();
376 this->mNumElements = mpMesh->GetNumElements();
377
378 typedef typename AbstractMesh<ELEMENT_DIM,SPACE_DIM>::NodeIterator NodeIterType;
379 mpIters->pNodeIter = new NodeIterType(mpMesh->GetNodeIteratorBegin());
380
382 mpIters->pElemIter = new ElemIterType(mpMesh->GetElementIteratorBegin());
383
384 // Set up node map if we might have deleted nodes
385 mNodeMapCurrentIndex = 0;
386 if (mpMesh->IsMeshChanging())
387 {
388 mpNodeMap = new NodeMap(mpMesh->GetNumAllNodes());
389 for (NodeIterType it = mpMesh->GetNodeIteratorBegin(); it != mpMesh->GetNodeIteratorEnd(); ++it)
390 {
391 mpNodeMap->SetNewIndex(it->GetIndex(), mNodeMapCurrentIndex++);
392 }
393 }
394 WriteFiles();
395}
396
397template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
399{
400 std::string comment = "# " + ChasteBuildInfo::GetProvenanceString();
401
402 // Write node file
403 std::string node_file_name = this->mBaseName + ".node";
404 out_stream p_node_file = this->mpOutputFileHandler->OpenOutputFile(node_file_name);
405
406 // Write the node header
407 unsigned num_attr = 0;
408 unsigned max_bdy_marker = 1; // as we include boundary node information in the node file
409 unsigned num_nodes = this->GetNumNodes();
410
411 *p_node_file << num_nodes << "\t";
412 *p_node_file << SPACE_DIM << "\t";
413 *p_node_file << num_attr << "\t";
414 *p_node_file << max_bdy_marker << "\n";
415 *p_node_file << std::setprecision(6);
416
417 // Write each node's data
418 for (unsigned item_num=0; item_num<num_nodes; item_num++)
419 {
420 std::vector<double> current_item = this->GetNextNode();
421 *p_node_file << item_num;
422 for (unsigned i=0; i<SPACE_DIM+1; i++)
423 {
424 *p_node_file << "\t" << current_item[i];
425 }
426 *p_node_file << "\n";
427 }
428 *p_node_file << comment << "\n";
429 p_node_file->close();
430
431 // Write element file
432 std::string element_file_name = this->mBaseName + ".cell";
433 out_stream p_element_file = this->mpOutputFileHandler->OpenOutputFile(element_file_name);
434
435 // Write the element header
436 num_attr = 1; //Always write element attributes
437 unsigned num_elements = this->GetNumElements();
438 *p_element_file << num_elements << "\t" << num_attr << "\n";
439
440 // Write each element's data
441 for (unsigned item_num=0; item_num<num_elements; item_num++)
442 {
443 if (SPACE_DIM == 2) // In 2D, write the node indices owned by this element
444 {
445 // Get data for this element
446 ElementData elem_data = this->GetNextElement();
447
448 // Get the node indices owned by this element
449 std::vector<unsigned> node_indices = elem_data.NodeIndices;
450
451 // Write this element's index and the number of nodes owned by it to file
452 *p_element_file << item_num << "\t" << node_indices.size();
453
454 // Write the node indices owned by this element to file
455 for (unsigned i=0; i<node_indices.size(); i++)
456 {
457 *p_element_file << "\t" << node_indices[i];
458 }
459
460 *p_element_file << "\t" << elem_data.AttributeValue;
461
462 // New line
463 *p_element_file << "\n";
464 }
465 else // 3D
466 {
467 assert(SPACE_DIM == 3); // LCOV_EXCL_LINE
468
469 // Get data for this element
470 VertexElementData elem_data_with_faces = this->GetNextElementWithFaces();
471
472 // Get the node indices owned by this element
473 std::vector<unsigned> node_indices = elem_data_with_faces.NodeIndices;
474
475 // Write this element's index and the number of nodes owned by it to file
476 *p_element_file << item_num << "\t" << node_indices.size();
477
478 // Write the node indices owned by this element to file
479 for (unsigned i=0; i<node_indices.size(); i++)
480 {
481 *p_element_file << "\t" << node_indices[i];
482 }
483
484 // Get the faces owned by this element (if any)
485 std::vector<ElementData> faces = elem_data_with_faces.Faces;
486
487 // Write the number of faces owned by this element to file
488 *p_element_file << "\t" << faces.size();
489
490 for (unsigned j=0; j<faces.size(); j++)
491 {
492 // Get the node indices owned by this face
493 std::vector<unsigned> face_node_indices = faces[j].NodeIndices;
494
495 // Write this face's index and the number of nodes owned by it to file
496 *p_element_file << "\t" << faces[j].AttributeValue << "\t" << face_node_indices.size();
497
498 // Write the node indices owned by this element to file
499 for (unsigned i=0; i<face_node_indices.size(); i++)
500 {
501 *p_element_file << "\t" << face_node_indices[i];
502 }
503
505 }
506
507 *p_element_file << "\t" << elem_data_with_faces.AttributeValue;
508
509 // New line
510 *p_element_file << "\n";
511 }
512 }
513
514 *p_element_file << comment << "\n";
515 p_element_file->close();
516}
517
519
520template class VertexMeshWriter<1,1>;
521template class VertexMeshWriter<1,2>;
522template class VertexMeshWriter<1,3>;
523template class VertexMeshWriter<2,2>;
524template class VertexMeshWriter<2,3>;
525template class VertexMeshWriter<3,3>;
#define NEVER_REACHED
virtual ElementData GetNextElement()
virtual std::vector< double > GetNextNode()
Node< SPACE_DIM > * GetNode(unsigned index) const
static std::string GetProvenanceString()
VertexElement< ELEMENT_DIM-1, SPACE_DIM > * GetFace(unsigned index) const
vtkUnstructuredGrid * mpVtkUnstructedMesh
ElementData GetNextElement()
std::vector< double > GetNextNode()
void AddPointData(std::string dataName, std::vector< double > dataPayload)
void MakeVtkMesh(VertexMesh< ELEMENT_DIM, SPACE_DIM > &rMesh)
void WriteVtkUsingMesh(VertexMesh< ELEMENT_DIM, SPACE_DIM > &rMesh, std::string stamp="")
MeshWriterIterators< ELEMENT_DIM, SPACE_DIM > * mpIters
void AddCellData(std::string dataName, std::vector< double > dataPayload)
VertexElementData GetNextElementWithFaces()
void WriteFilesUsingMesh(VertexMesh< ELEMENT_DIM, SPACE_DIM > &rMesh)
VertexMeshWriter(const std::string &rDirectory, const std::string &rBaseName, const bool clearOutputDir=true)
VertexElementIterator GetElementIteratorEnd()
virtual unsigned GetNumNodes() const
VertexElementIterator GetElementIteratorBegin(bool skipDeletedElements=true)
virtual VertexMesh< ELEMENT_DIM, SPACE_DIM > * GetMeshForVtk()
std::vector< unsigned > NodeIndices
VertexMesh< ELEMENT_DIM, SPACE_DIM >::VertexElementIterator * pElemIter
AbstractMesh< ELEMENT_DIM, SPACE_DIM >::NodeIterator * pNodeIter
std::vector< ElementData > Faces
std::vector< unsigned > NodeIndices