Chaste Commit::675f9facbe008c5eacb9006feaeb6423206579ea
MeshBasedCellPopulationWithGhostNodes.cpp
1/*
2
3Copyright (c) 2005-2025, 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 "MeshBasedCellPopulationWithGhostNodes.hpp"
37#include "CellLocationIndexWriter.hpp"
38#include "VtkMeshWriter.hpp"
39
40template<unsigned DIM>
43 std::vector<CellPtr>& rCells,
44 const std::vector<unsigned> locationIndices,
45 bool deleteMesh,
46 double ghostCellSpringStiffness,
47 double ghostGhostSpringStiffness,
48 double ghostSpringRestLength)
49 : MeshBasedCellPopulation<DIM,DIM>(rMesh, rCells, locationIndices, deleteMesh, false), // do not call the base class Validate()
50 mGhostCellSpringStiffness(ghostCellSpringStiffness),
51 mGhostGhostSpringStiffness(ghostGhostSpringStiffness),
52 mGhostSpringRestLength(ghostSpringRestLength)
53{
54 if (!locationIndices.empty())
55 {
56 // Create a set of node indices corresponding to ghost nodes
57 std::set<unsigned> node_indices;
58 std::set<unsigned> location_indices;
59 std::set<unsigned> ghost_node_indices;
60
61 for (unsigned i=0; i<this->GetNumNodes(); i++)
62 {
63 node_indices.insert(this->GetNode(i)->GetIndex());
64 }
65 for (unsigned i=0; i<locationIndices.size(); i++)
66 {
67 location_indices.insert(locationIndices[i]);
68 }
69
70 std::set_difference(node_indices.begin(), node_indices.end(),
71 location_indices.begin(), location_indices.end(),
72 std::inserter(ghost_node_indices, ghost_node_indices.begin()));
73
74 // This method finishes and then calls Validate()
75 SetGhostNodes(ghost_node_indices);
76 }
77 else
78 {
79 this->mIsGhostNode = std::vector<bool>(this->GetNumNodes(), false);
80 Validate();
81 }
82 this->UpdateNodePairs();
83}
84
85template<unsigned DIM>
87 double ghostCellSpringStiffness,
88 double ghostGhostSpringStiffness,
89 double ghostSpringRestLength)
90 : MeshBasedCellPopulation<DIM,DIM>(rMesh),
91 mGhostCellSpringStiffness(ghostCellSpringStiffness),
92 mGhostGhostSpringStiffness(ghostGhostSpringStiffness),
93 mGhostSpringRestLength(ghostSpringRestLength)
94{
95}
96
97template<unsigned DIM>
101
102template<unsigned DIM>
104{
105 EXCEPTION("Currently can't solve PDEs on meshes with ghost nodes");
106 return static_cast<TetrahedralMesh<DIM, DIM>*>(&(this->mrMesh));
107}
108
109template<unsigned DIM>
111{
112 return this->mIsGhostNode;
113}
114
115template<unsigned DIM>
117{
118 return this->mIsGhostNode[index];
119}
120
121template<unsigned DIM>
123{
124 std::set<unsigned> ghost_node_indices;
125 for (unsigned i=0; i<this->mIsGhostNode.size(); i++)
126 {
127 if (this->mIsGhostNode[i])
128 {
129 ghost_node_indices.insert(i);
130 }
131 }
132 return ghost_node_indices;
133}
134
135template<unsigned DIM>
136void MeshBasedCellPopulationWithGhostNodes<DIM>::SetGhostNodes(const std::set<unsigned>& rGhostNodeIndices)
137{
138 // Reinitialise all entries of mIsGhostNode to false
139 this->mIsGhostNode = std::vector<bool>(this->mrMesh.GetNumNodes(), false);
140
141 // Update mIsGhostNode
142 for (std::set<unsigned>::iterator iter=rGhostNodeIndices.begin(); iter!=rGhostNodeIndices.end(); ++iter)
143 {
144 this->mIsGhostNode[*iter] = true;
145 }
146
147 Validate();
148}
149
150template<unsigned DIM>
151c_vector<double, DIM> MeshBasedCellPopulationWithGhostNodes<DIM>::CalculateForceBetweenGhostNodes(const unsigned& rNodeAGlobalIndex, const unsigned& rNodeBGlobalIndex)
152{
153 assert(rNodeAGlobalIndex != rNodeBGlobalIndex);
154 c_vector<double, DIM> unit_difference;
155 const c_vector<double, DIM>& r_node_a_location = this->GetNode(rNodeAGlobalIndex)->rGetLocation();
156 const c_vector<double, DIM>& r_node_b_location = this->GetNode(rNodeBGlobalIndex)->rGetLocation();
157
158 // There is reason not to subtract one position from the other (cylindrical meshes)
159 unit_difference = this->mrMesh.GetVectorFromAtoB(r_node_a_location, r_node_b_location);
160
161 double distance_between_nodes = norm_2(unit_difference);
162 unit_difference /= distance_between_nodes;
163
164 double rest_length = 1.0; // TODO this could also be a parameter.
165 double spring_stiffness = mGhostCellSpringStiffness;
166 if (this->mIsGhostNode[rNodeAGlobalIndex] && this->mIsGhostNode[rNodeBGlobalIndex])
167 {
168 rest_length = mGhostSpringRestLength;
169 spring_stiffness = mGhostGhostSpringStiffness;
170 }
171
172 return spring_stiffness * unit_difference * (distance_between_nodes - rest_length);
173}
174
175template<unsigned DIM>
176CellPtr MeshBasedCellPopulationWithGhostNodes<DIM>::AddCell(CellPtr pNewCell, CellPtr pParentCell)
177{
178 // Add new cell to population
179 CellPtr p_created_cell = MeshBasedCellPopulation<DIM,DIM>::AddCell(pNewCell, pParentCell);
180 assert(p_created_cell == pNewCell);
181
182 // Update size of mIsGhostNode if necessary
183 unsigned new_node_index = this->GetLocationIndexUsingCell(p_created_cell);
184
185 if (this->GetNumNodes() > this->mIsGhostNode.size())
186 {
187 this->mIsGhostNode.resize(this->GetNumNodes());
188 this->mIsGhostNode[new_node_index] = false;
189 }
190
191 // Return pointer to new cell
192 return p_created_cell;
193}
194
195template<unsigned DIM>
197{
198 // Get a list of all the nodes that are ghosts
199 std::vector<bool> validated_node = mIsGhostNode;
200 assert(mIsGhostNode.size()==this->GetNumNodes());
201
202 // Look through all of the cells and record what node they are associated with.
203 for (typename AbstractCellPopulation<DIM,DIM>::Iterator cell_iter=this->Begin(); cell_iter!=this->End(); ++cell_iter)
204 {
205 unsigned node_index = this->GetLocationIndexUsingCell((*cell_iter));
206
207 // If the node attached to this cell is labelled as a ghost node, then throw an error
208 if (mIsGhostNode[node_index])
209 {
210 EXCEPTION("Node " << node_index << " is labelled as a ghost node and has a cell attached");
211 }
212 validated_node[node_index] = true;
213 }
214
215 for (unsigned i=0; i<validated_node.size(); i++)
216 {
217 if (!validated_node[i])
218 {
219 EXCEPTION("Node " << i << " does not appear to be a ghost node or have a cell associated with it");
220 }
221 }
222}
223
224template<unsigned DIM>
226{
227 assert(mIsGhostNode[nodeIndex]);
228
229 static_cast<MutableMesh<DIM,DIM>&>((this->mrMesh)).DeleteNodePriorToReMesh(nodeIndex);
230}
231
232template<unsigned DIM>
234{
235 // Copy mIsGhostNode to a temporary vector
236 std::vector<bool> ghost_nodes_before_remesh = mIsGhostNode;
237
238 // Reinitialise mIsGhostNode
239 mIsGhostNode.clear();
240 mIsGhostNode.resize(this->GetNumNodes());
241
242 // Update mIsGhostNode using the node map
243 for (unsigned old_index=0; old_index<rMap.GetSize(); old_index++)
244 {
245 if (!rMap.IsDeleted(old_index))
246 {
247 unsigned new_index = rMap.GetNewIndex(old_index);
248 mIsGhostNode[new_index] = ghost_nodes_before_remesh[old_index];
249 }
250 }
251}
252
253template<unsigned DIM>
255{
256 unsigned node_index = this->GetLocationIndexUsingCell(pCell);
257 std::set<unsigned> neighbour_indices = this->GetNeighbouringNodeIndices(node_index);
258
259 // Remove ghost nodes from the neighbour indices
260 for (std::set<unsigned>::iterator iter = neighbour_indices.begin();
261 iter != neighbour_indices.end();)
262 {
263 if (this->IsGhostNode(*iter))
264 {
265 neighbour_indices.erase(iter++);
266 }
267 else
268 {
269 ++iter;
270 }
271 }
272
273 return neighbour_indices;
274}
275
276template<unsigned DIM>
278{
279 for (typename AbstractMesh<DIM, DIM>::NodeIterator node_iter = this->rGetMesh().GetNodeIteratorBegin();
280 node_iter != this->rGetMesh().GetNodeIteratorEnd();
281 ++node_iter)
282 {
283 // If it isn't a ghost node then there might be cell writers attached
284 if (! this->IsGhostNode(node_iter->GetIndex()))
285 {
286 for (typename std::vector<boost::shared_ptr<AbstractCellWriter<DIM, DIM> > >::iterator cell_writer_iter = this->mCellWriters.begin();
287 cell_writer_iter != this->mCellWriters.end();
288 ++cell_writer_iter)
289 {
290 CellPtr cell_from_node = this->GetCellUsingLocationIndex(node_iter->GetIndex());
291 this->AcceptCellWriter(*cell_writer_iter, cell_from_node);
292 }
293 }
294 }
295}
296
297template<unsigned DIM>
299{
300 // Initialise vector of forces on ghost nodes
301 std::vector<c_vector<double, DIM> > drdt(this->GetNumNodes());
302 for (unsigned i=0; i<drdt.size(); i++)
303 {
304 drdt[i] = zero_vector<double>(DIM);
305 }
306
307 // Calculate forces on ghost nodes
308 for (typename MutableMesh<DIM, DIM>::EdgeIterator edge_iterator = static_cast<MutableMesh<DIM, DIM>&>((this->mrMesh)).EdgesBegin();
309 edge_iterator != static_cast<MutableMesh<DIM, DIM>&>((this->mrMesh)).EdgesEnd();
310 ++edge_iterator)
311 {
312 unsigned nodeA_global_index = edge_iterator.GetNodeA()->GetIndex();
313 unsigned nodeB_global_index = edge_iterator.GetNodeB()->GetIndex();
314
315 c_vector<double, DIM> force = CalculateForceBetweenGhostNodes(nodeA_global_index, nodeB_global_index);
316
317 if (!this->mIsGhostNode[nodeA_global_index])
318 {
319 drdt[nodeB_global_index] -= force;
320 }
321 else
322 {
323 drdt[nodeA_global_index] += force;
324
325 if (this->mIsGhostNode[nodeB_global_index])
326 {
327 drdt[nodeB_global_index] -= force;
328 }
329 }
330 }
331
332 for (typename AbstractMesh<DIM,DIM>::NodeIterator node_iter = this->mrMesh.GetNodeIteratorBegin();
333 node_iter != this->mrMesh.GetNodeIteratorEnd();
334 ++node_iter)
335 {
336 unsigned node_index = node_iter->GetIndex();
337 if (this->mIsGhostNode[node_index])
338 {
339 node_iter->ClearAppliedForce();
340 node_iter->AddAppliedForceContribution(drdt[node_index]);
341 }
342 }
343}
344
345template<unsigned DIM>
347{
348 if (this->mOutputResultsForChasteVisualizer)
349 {
350 if (!this-> template HasWriter<CellLocationIndexWriter>())
351 {
352 this-> template AddCellWriter<CellLocationIndexWriter>();
353 }
354 }
355
357}
358
359template<unsigned DIM>
361{
362#ifdef CHASTE_VTK
363 // Store the present time as a string
364 unsigned num_timesteps = SimulationTime::Instance()->GetTimeStepsElapsed();
365 std::stringstream time;
366 time << num_timesteps;
367
368 if (this->mWriteVtkAsPoints)
369 {
370 // Create mesh writer for VTK output
371 VtkMeshWriter<DIM, DIM> mesh_writer(rDirectory, "mesh_results_"+time.str(), false);
372
373 // Iterate over any cell writers that are present
374 unsigned num_vtk_cells = this->rGetMesh().GetNumNodes();
375 for (typename std::vector<boost::shared_ptr<AbstractCellWriter<DIM, DIM> > >::iterator cell_writer_iter = this->mCellWriters.begin();
376 cell_writer_iter != this->mCellWriters.end();
377 ++cell_writer_iter)
378 {
379 // Create vector to store VTK cell data
380 std::vector<double> vtk_cell_data(num_vtk_cells);
381
382 // Loop over nodes of mesh
383 for (typename AbstractMesh<DIM, DIM>::NodeIterator node_iter = this->rGetMesh().GetNodeIteratorBegin();
384 node_iter != this->rGetMesh().GetNodeIteratorEnd();
385 ++node_iter)
386 {
387 // Get the indices of this node
388 unsigned node_index = node_iter->GetIndex();
389
390 // If this node corresponds to a ghost node, set any "cell" data to be -1.0
391 if (this->IsGhostNode(node_index))
392 {
393 // Populate the vector of VTK cell data
394 vtk_cell_data[node_index] = -1.0;
395 }
396 else
397 {
398 // Get the cell corresponding to this node
399 CellPtr p_cell = this->GetCellUsingLocationIndex(node_index);
400
401 // Populate the vector of VTK cell data
402 vtk_cell_data[node_index] = (*cell_writer_iter)->GetCellDataForVtkOutput(p_cell, this);
403 }
404 }
405
406 mesh_writer.AddPointData((*cell_writer_iter)->GetVtkCellDataName(), vtk_cell_data);
407 }
408
409 // Next, record which nodes are ghost nodes
410 // Note that the cell writer hierarchy can not be used to do this as ghost nodes don't have corresponding cells.
411 std::vector<double> ghosts(num_vtk_cells);
412 for (typename AbstractMesh<DIM, DIM>::NodeIterator node_iter = this->rGetMesh().GetNodeIteratorBegin();
413 node_iter != this->rGetMesh().GetNodeIteratorEnd();
414 ++node_iter)
415 {
416 unsigned node_index = node_iter->GetIndex();
417 ghosts[node_index] = (double) (this->IsGhostNode(node_index));
418 }
419 mesh_writer.AddPointData("Non-ghosts", ghosts);
420
422
423 mesh_writer.WriteFilesUsingMesh(this->rGetMesh());
424 *(this->mpVtkMetaFile) << " <DataSet timestep=\"";
425 *(this->mpVtkMetaFile) << num_timesteps;
426 *(this->mpVtkMetaFile) << "\" group=\"\" part=\"0\" file=\"mesh_results_";
427 *(this->mpVtkMetaFile) << num_timesteps;
428 *(this->mpVtkMetaFile) << ".vtu\"/>\n";
429 }
430 if (this->mpVoronoiTessellation != nullptr)
431 {
432 // Create mesh writer for VTK output
433 VertexMeshWriter<DIM, DIM> mesh_writer(rDirectory, "voronoi_results", false);
434
435 // Iterate over any cell writers that are present
436 unsigned num_vtk_cells = this->mpVoronoiTessellation->GetNumElements();
437 for (typename std::vector<boost::shared_ptr<AbstractCellWriter<DIM, DIM> > >::iterator cell_writer_iter = this->mCellWriters.begin();
438 cell_writer_iter != this->mCellWriters.end();
439 ++cell_writer_iter)
440 {
441 // Create vector to store VTK cell data
442 std::vector<double> vtk_cell_data(num_vtk_cells);
443
444 // Loop over elements of mpVoronoiTessellation
445 for (typename VertexMesh<DIM, DIM>::VertexElementIterator elem_iter = this->mpVoronoiTessellation->GetElementIteratorBegin();
446 elem_iter != this->mpVoronoiTessellation->GetElementIteratorEnd();
447 ++elem_iter)
448 {
449 // Get the indices of this element and the corresponding node in mrMesh
450 unsigned elem_index = elem_iter->GetIndex();
451 unsigned node_index = this->mpVoronoiTessellation->GetDelaunayNodeIndexCorrespondingToVoronoiElementIndex(elem_index);
452
453 // If this node corresponds to a ghost node, set any "cell" data to be -1.0
454 if (this->IsGhostNode(node_index))
455 {
456 // Populate the vector of VTK cell data
457 vtk_cell_data[elem_index] = -1.0;
458 }
459 else
460 {
461 // Get the cell corresponding to this node
462 CellPtr p_cell = this->GetCellUsingLocationIndex(node_index);
463
464 // Populate the vector of VTK cell data
465 vtk_cell_data[elem_index] = (*cell_writer_iter)->GetCellDataForVtkOutput(p_cell, this);
466 }
467 }
468
469 mesh_writer.AddCellData((*cell_writer_iter)->GetVtkCellDataName(), vtk_cell_data);
470 }
471
472 // Next, record which nodes are ghost nodes
473 // Note that the cell writer hierarchy can not be used to do this as ghost nodes don't have corresponding cells.
474 std::vector<double> ghosts(num_vtk_cells);
475 for (typename VertexMesh<DIM, DIM>::VertexElementIterator elem_iter = this->mpVoronoiTessellation->GetElementIteratorBegin();
476 elem_iter != this->mpVoronoiTessellation->GetElementIteratorEnd();
477 ++elem_iter)
478 {
479 unsigned elem_index = elem_iter->GetIndex();
480 unsigned node_index = this->mpVoronoiTessellation->GetDelaunayNodeIndexCorrespondingToVoronoiElementIndex(elem_index);
481 ghosts[elem_index] = (double) (this->IsGhostNode(node_index));
482 }
483 mesh_writer.AddCellData("Non-ghosts", ghosts);
484
486
487 mesh_writer.WriteVtkUsingMesh(*(this->mpVoronoiTessellation), time.str());
488 *(this->mpVtkMetaFile) << " <DataSet timestep=\"";
489 *(this->mpVtkMetaFile) << num_timesteps;
490 *(this->mpVtkMetaFile) << "\" group=\"\" part=\"0\" file=\"voronoi_results_";
491 *(this->mpVtkMetaFile) << num_timesteps;
492 *(this->mpVtkMetaFile) << ".vtu\"/>\n";
493 }
494#endif //CHASTE_VTK
495}
496
497template<unsigned DIM>
499{
500 *rParamsFile << "\t\t<GhostCellSpringStiffness>" << mGhostCellSpringStiffness << "</GhostCellSpringStiffness>\n";
501 *rParamsFile << "\t\t<GhostGhostSpringStiffness>" << mGhostGhostSpringStiffness << "</GhostGhostSpringStiffness>\n";
502 *rParamsFile << "\t\t<GhostSpringRestLength>" << mGhostSpringRestLength << "</GhostSpringRestLength>\n";
503
504 // Call method on direct parent class
506}
507
508// Explicit instantiation
512
513// Serialization for Boost >= 1.36
#define EXCEPTION(message)
#define EXPORT_TEMPLATE_CLASS_SAME_DIMS(CLASS)
std::set< unsigned > GetNeighbouringLocationIndices(CellPtr pCell)
virtual void OpenWritersFiles(OutputFileHandler &rOutputFileHandler)
MeshBasedCellPopulationWithGhostNodes(MutableMesh< DIM, DIM > &rMesh, std::vector< CellPtr > &rCells, const std::vector< unsigned > locationIndices=std::vector< unsigned >(), bool deleteMesh=false, double ghostCellSpringStiffness=15.0, double ghostGhostSpringStiffness=15.0, double ghostSpringRestLength=1.0)
c_vector< double, DIM > CalculateForceBetweenGhostNodes(const unsigned &rNodeAGlobalIndex, const unsigned &rNodeBGlobalIndex)
virtual void WriteVtkResultsToFile(const std::string &rDirectory)
CellPtr AddCell(CellPtr pNewCell, CellPtr pParentCell)
void SetGhostNodes(const std::set< unsigned > &rGhostNodeIndices)
virtual TetrahedralMesh< DIM, DIM > * GetTetrahedralMeshForPdeModifier()
virtual void OpenWritersFiles(OutputFileHandler &rOutputFileHandler)
void OutputCellPopulationParameters(out_stream &rParamsFile)
virtual CellPtr AddCell(CellPtr pNewCell, CellPtr pParentCell)
Node< SPACE_DIM > * GetNode(unsigned index)
void DeleteNodePriorToReMesh(unsigned index)
unsigned GetNewIndex(unsigned oldIndex) const
Definition NodeMap.cpp:87
unsigned GetSize()
Definition NodeMap.cpp:105
bool IsDeleted(unsigned index)
Definition NodeMap.cpp:82
static SimulationTime * Instance()
unsigned GetTimeStepsElapsed() const
EdgeIterator EdgesEnd()
void WriteVtkUsingMesh(VertexMesh< ELEMENT_DIM, SPACE_DIM > &rMesh, std::string stamp="")
void AddCellData(std::string dataName, std::vector< double > dataPayload)
void AddPointData(std::string name, std::vector< double > data)
void WriteFilesUsingMesh(AbstractTetrahedralMesh< ELEMENT_DIM, SPACE_DIM > &rMesh, bool keepOriginalElementIndexing=true)