Chaste Commit::29c3c4ab2bee00ddd311a035a1e522c03cbe928a
NodeBasedCellPopulationWithParticles.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 "NodeBasedCellPopulationWithParticles.hpp"
37#include "VtkMeshWriter.hpp"
38
39template<unsigned DIM>
41 std::vector<CellPtr>& rCells,
42 const std::vector<unsigned> locationIndices,
43 bool deleteMesh)
44 : NodeBasedCellPopulation<DIM>(rMesh, rCells, locationIndices, deleteMesh, false)
45{
47 if (!locationIndices.empty())
48 {
49 // Create a set of node indices corresponding to particles
50 std::set<unsigned> node_indices;
51 std::set<unsigned> location_indices;
52 std::set<unsigned> particle_indices;
53
54 for (typename AbstractMesh<DIM,DIM>::NodeIterator node_iter = rMesh.GetNodeIteratorBegin();
55 node_iter != rMesh.GetNodeIteratorEnd();
56 ++node_iter)
57 {
58 node_indices.insert(node_iter->GetIndex());
59 }
60 for (unsigned i=0; i<locationIndices.size(); i++)
61 {
62 location_indices.insert(locationIndices[i]);
63 }
64
65 std::set_difference(node_indices.begin(), node_indices.end(),
66 location_indices.begin(), location_indices.end(),
67 std::inserter(particle_indices, particle_indices.begin()));
68
69 // This method finishes and then calls Validate()
70 SetParticles(particle_indices);
71 }
72 else
73 {
74 for (typename NodesOnlyMesh<DIM>::NodeIterator node_iter = rMesh.GetNodeIteratorBegin();
75 node_iter != rMesh.GetNodeIteratorEnd();
76 ++node_iter)
77 {
78 (*node_iter).SetIsParticle(false);
79 }
81 }
82}
83
84template<unsigned DIM>
89
90template<unsigned DIM>
92{
93 return this->GetNode(index)->IsParticle();
94}
95
96template<unsigned DIM>
98{
99 std::set<unsigned> particle_indices;
100
101 for (typename AbstractMesh<DIM,DIM>::NodeIterator node_iter = this->mrMesh.GetNodeIteratorBegin();
102 node_iter != this->mrMesh.GetNodeIteratorEnd();
103 ++node_iter)
104 {
105 if (node_iter->IsParticle())
106 {
107 particle_indices.insert(node_iter->GetIndex());
108 }
109 }
110
111 return particle_indices;
112}
113
114template<unsigned DIM>
115void NodeBasedCellPopulationWithParticles<DIM>::SetParticles(const std::set<unsigned>& rParticleIndices)
116{
117 for (typename AbstractMesh<DIM,DIM>::NodeIterator node_iter = this->mrMesh.GetNodeIteratorBegin();
118 node_iter != this->mrMesh.GetNodeIteratorEnd();
119 ++node_iter)
120 {
121 if (rParticleIndices.find(node_iter->GetIndex()) != rParticleIndices.end())
122 {
123 node_iter->SetIsParticle(true);
124 }
125 }
127}
128
129template<unsigned DIM>
133
134template<unsigned DIM>
135CellPtr NodeBasedCellPopulationWithParticles<DIM>::AddCell(CellPtr pNewCell, CellPtr pParentCell)
136{
137 assert(pNewCell);
138
139 // Add new cell to population
140 CellPtr p_created_cell = AbstractCentreBasedCellPopulation<DIM>::AddCell(pNewCell, pParentCell);
141 assert(p_created_cell == pNewCell);
142
143 // Then set the new cell radius in the NodesOnlyMesh
144 unsigned node_index = this->GetLocationIndexUsingCell(p_created_cell);
145 this->GetNode(node_index)->SetRadius(0.5);
146 this->GetNode(node_index)->SetIsParticle(false);
147
148 // Return pointer to new cell
149 return p_created_cell;
150}
151
152template<unsigned DIM>
154{
155 std::map<unsigned, bool> validated_nodes;
156 for (typename AbstractMesh<DIM, DIM>::NodeIterator node_iter = this->mrMesh.GetNodeIteratorBegin();
157 node_iter != this->mrMesh.GetNodeIteratorEnd();
158 ++node_iter)
159 {
160 validated_nodes[node_iter->GetIndex()] = node_iter->IsParticle();
161 }
162
163 // Look through all of the cells and record what node they are associated with.
164 for (typename AbstractCellPopulation<DIM>::Iterator cell_iter=this->Begin(); cell_iter!=this->End(); ++cell_iter)
165 {
166 unsigned node_index = this->GetLocationIndexUsingCell((*cell_iter));
167
168 // If the node attached to this cell is labelled as a particle, then throw an error
169 if (this->GetNode(node_index)->IsParticle())
170 {
171 EXCEPTION("Node " << node_index << " is labelled as a particle and has a cell attached");
172 }
173 validated_nodes[node_index] = true;
174 }
175
176 for (std::map<unsigned, bool>::iterator map_iter = validated_nodes.begin();
177 map_iter != validated_nodes.end();
178 map_iter++)
179 {
180 if (!map_iter->second)
181 {
182 EXCEPTION("Node " << map_iter->first << " does not appear to be a particle or has a cell associated with it");
183 }
184 }
185}
186
187template<unsigned DIM>
189{
190 for (typename AbstractMesh<DIM, DIM>::NodeIterator node_iter = this->rGetMesh().GetNodeIteratorBegin();
191 node_iter != this->rGetMesh().GetNodeIteratorEnd();
192 ++node_iter)
193 {
194 // If it isn't a particle then there might be cell writers attached
195 if (! this->IsParticle(node_iter->GetIndex()))
196 {
197 for (typename std::vector<boost::shared_ptr<AbstractCellWriter<DIM, DIM> > >::iterator cell_writer_iter = this->mCellWriters.begin();
198 cell_writer_iter != this->mCellWriters.end();
199 ++cell_writer_iter)
200 {
201 CellPtr cell_from_node = this->GetCellUsingLocationIndex(node_iter->GetIndex());
202 this->AcceptCellWriter(*cell_writer_iter, cell_from_node);
203 }
204 }
205 }
206}
207
208template<unsigned DIM>
210{
211 // Store the present time as a string
212 std::stringstream time;
214
215 // Make sure the nodes are ordered contiguously in memory
216 NodeMap map(1 + this->mpNodesOnlyMesh->GetMaximumNodeIndex());
217 this->mpNodesOnlyMesh->ReMesh(map);
218
219 // Store the number of cells for which to output data to VTK
220 unsigned num_nodes = this->GetNumNodes();
221 std::vector<double> rank(num_nodes);
222 std::vector<double> particles(num_nodes);
223
224 unsigned num_cell_data_items = 0;
225 std::vector<std::string> cell_data_names;
226
227 // We assume that the first cell is representative of all cells
228 if (num_nodes > 0)
229 {
230 num_cell_data_items = this->Begin()->GetCellData()->GetNumItems();
231 cell_data_names = this->Begin()->GetCellData()->GetKeys();
232 }
233
234 std::vector<std::vector<double> > cell_data;
235 for (unsigned var=0; var<num_cell_data_items; var++)
236 {
237 std::vector<double> cell_data_var(num_nodes);
238 cell_data.push_back(cell_data_var);
239 }
240
241 // Create mesh writer for VTK output
242 VtkMeshWriter<DIM, DIM> mesh_writer(rDirectory, "results_"+time.str(), false);
243 mesh_writer.SetParallelFiles(*(this->mpNodesOnlyMesh));
244
245 // Iterate over any cell writers that are present
246 for (typename std::vector<boost::shared_ptr<AbstractCellWriter<DIM, DIM> > >::iterator cell_writer_iter = this->mCellWriters.begin();
247 cell_writer_iter != this->mCellWriters.end();
248 ++cell_writer_iter)
249 {
250 // Create vector to store VTK cell data
251 std::vector<double> vtk_cell_data(num_nodes);
252
253 // Loop over nodes
254 for (typename AbstractMesh<DIM,DIM>::NodeIterator node_iter = this->mrMesh.GetNodeIteratorBegin();
255 node_iter != this->mrMesh.GetNodeIteratorEnd();
256 ++node_iter)
257 {
258 unsigned node_index = node_iter->GetIndex();
259
260 // If this node is a particle (not a cell), then we set the 'dummy' VTK cell data for this to be -2.0...
261 if (this->IsParticle(node_index))
262 {
263 vtk_cell_data[node_index] = -2.0;
264 }
265 else
266 {
267 // ...otherwise we populate the vector of VTK cell data as usual
268 CellPtr p_cell = this->GetCellUsingLocationIndex(node_index);
269 vtk_cell_data[node_index] = (*cell_writer_iter)->GetCellDataForVtkOutput(p_cell, this);
270 }
271 }
272
273 mesh_writer.AddPointData((*cell_writer_iter)->GetVtkCellDataName(), vtk_cell_data);
274 }
275
276 // Loop over cells
277 for (typename AbstractCellPopulation<DIM>::Iterator cell_iter = this->Begin();
278 cell_iter != this->End();
279 ++cell_iter)
280 {
281 // Get the node index corresponding to this cell
282 unsigned global_index = this->GetLocationIndexUsingCell(*cell_iter);
283 unsigned node_index = this->rGetMesh().SolveNodeMapping(global_index);
284
285 for (unsigned var=0; var<num_cell_data_items; var++)
286 {
287 cell_data[var][node_index] = cell_iter->GetCellData()->GetItem(cell_data_names[var]);
288 }
289
290 rank[node_index] = (PetscTools::GetMyRank());
291 }
292
293 mesh_writer.AddPointData("Process rank", rank);
294
295 // Loop over nodes
296 for (typename AbstractMesh<DIM,DIM>::NodeIterator node_iter = this->mrMesh.GetNodeIteratorBegin();
297 node_iter != this->mrMesh.GetNodeIteratorEnd();
298 ++node_iter)
299 {
300 unsigned node_index = node_iter->GetIndex();
301 particles[node_index] = (double) (this->IsParticle(node_index));
302 }
303
304 mesh_writer.AddPointData("Non-particles", particles);
305
306 if (num_cell_data_items > 0)
307 {
308 for (unsigned var=0; var<cell_data.size(); var++)
309 {
310 mesh_writer.AddPointData(cell_data_names[var], cell_data[var]);
311 }
312 }
313
314 mesh_writer.WriteFilesUsingMesh(*(this->mpNodesOnlyMesh));
315
316 *(this->mpVtkMetaFile) << " <DataSet timestep=\"";
317 *(this->mpVtkMetaFile) << SimulationTime::Instance()->GetTimeStepsElapsed();
318 *(this->mpVtkMetaFile) << "\" group=\"\" part=\"0\" file=\"results_";
319 *(this->mpVtkMetaFile) << SimulationTime::Instance()->GetTimeStepsElapsed();
321 {
322 *(this->mpVtkMetaFile) << ".vtu\"/>\n";
323 }
324/* {
325 // Parallel vtu files .vtu -> .pvtu
326 *(this->mpVtkMetaFile) << ".pvtu\"/>\n";
327 }*/
328}
329
330template<unsigned DIM>
332{
333 // Call method on direct parent class
335}
336
337// Explicit instantiation
341
342// Serialization for Boost >= 1.36
#define EXCEPTION(message)
#define EXCEPT_IF_NOT(test)
#define EXPORT_TEMPLATE_CLASS_SAME_DIMS(CLASS)
CellPtr AddCell(CellPtr pNewCell, CellPtr pParentCell=CellPtr())
NodeIterator GetNodeIteratorEnd()
NodeIterator GetNodeIteratorBegin(bool skipDeletedNodes=true)
CellPtr AddCell(CellPtr pNewCell, CellPtr pParentCell)
virtual void WriteVtkResultsToFile(const std::string &rDirectory)
void SetParticles(const std::set< unsigned > &rParticleIndices)
NodeBasedCellPopulationWithParticles(NodesOnlyMesh< DIM > &rMesh, std::vector< CellPtr > &rCells, const std::vector< unsigned > locationIndices=std::vector< unsigned >(), bool deleteMesh=false)
void OutputCellPopulationParameters(out_stream &rParamsFile)
static bool IsSequential()
static unsigned GetMyRank()
static SimulationTime * Instance()
unsigned GetTimeStepsElapsed() const
void AddPointData(std::string name, std::vector< double > data)
void SetParallelFiles(AbstractTetrahedralMesh< ELEMENT_DIM, SPACE_DIM > &rMesh)
void WriteFilesUsingMesh(AbstractTetrahedralMesh< ELEMENT_DIM, SPACE_DIM > &rMesh, bool keepOriginalElementIndexing=true)