VPTissue Reference Manual
MeshTopology.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2016 Universiteit Antwerpen
3  *
4  * Licensed under the EUPL, Version 1.1 or as soon they will be approved by
5  * the European Commission - subsequent versions of the EUPL (the "Licence");
6  * You may not use this work except in compliance with the Licence.
7  * You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl5
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the Licence is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the Licence for the specific language governing
13  * permissions and limitations under the Licence.
14  */
20 #include "MeshTopology.h"
21 
22 #include "bio/Cell.h"
23 #include "bio/Node.h"
24 #include "bio/Mesh.h"
25 
26 #include <memory>
27 #include <string>
28 #include <vector>
29 
30 using namespace std;
31 using namespace SimPT_Sim::Util;
32 
33 namespace SimPT_Sim {
34 
35 CSRMatrix MeshTopology::NodeCellNodeIncidence(shared_ptr<Mesh> mesh)
36 {
37  const auto& nodes = mesh->GetNodes();
38  const size_t num_nodes = nodes.size();
39 
40  // Nodes - cells incidence matrix
41  CSRMatrix A;
42 
43  // The "running" A.row_ptr; I.e: start idx of a row (n_i) in A.col_ind
44  size_t n_offset = 0;
45 
46  // For each node n_i, gather the cell indices (c_i) the node belongs to
47  for (size_t n_i = 0; n_i < num_nodes; ++n_i) {
48  const auto node = nodes[n_i];
49  // Begin a new row by storing its start offset
50  A.row_ptr.push_back(n_offset);
51  for (const auto nbr : mesh->GetNodeOwningNeighbors(node)) {
52  const int c_i = nbr.GetCell()->GetIndex();
53  // I assume nodes sharing the boundary polygon are independent
54  if (c_i != -1) {
55  // Put a 1 at A[n_i, c_i] only if it wasn't there yet.
56  // (i.e: if c_i is NOT in [A.col_ind[A.row_ptr[n_i]], ... end])
57  if (find(A.col_ind.begin() + A.row_ptr.back(), A.col_ind.end(), c_i) == A.col_ind.end()) {
58  A.col_ind.push_back(c_i);
59  ++n_offset;
60  }
61  }
62  }
63  }
64 
65  // Add the last element to A.row_ptr, defined as length(col_ind)
66  // See documentation of CSRMatrix.
67  A.row_ptr.push_back(A.col_ind.size());
68  return A;
69 }
70 
71 CSRMatrix MeshTopology::NodeEdgeNodeIncidence(shared_ptr<Mesh> mesh)
72 {
73  const auto& nodes = mesh->GetNodes();
74  const size_t num_nodes = nodes.size();
75 
76  // Nodes - nodes incidence matrix
77  CSRMatrix A;
78 
79  // The "running" A.row_ptr; I.e: start idx of a row (n_i) in A.col_ind
80  size_t n_offset = 0;
81 
82  // For each node n_i, gather the indices of nodes it shares an edge with
83  for (size_t n_i = 0; n_i < num_nodes; ++n_i) {
84  const auto node = nodes[n_i];
85  // Begin a new row by storing its start offset
86  A.row_ptr.push_back(n_offset);
87  for (const auto nbr : mesh->GetNodeOwningNeighbors(node)) {
88  const int n1_i = nbr.GetNb1()->GetIndex();
89  const int n2_i = nbr.GetNb2()->GetIndex();
90  // Put a 1 at A[n_i, n1/2_i] only if it wasn't there yet.
91  // (i.e: if n1/2_i is NOT in [A.col_ind[A.row_ptr[n_i]], ... end])
92  if (find(A.col_ind.begin() + A.row_ptr.back(), A.col_ind.end(), n1_i) == A.col_ind.end()) {
93  A.col_ind.push_back(n1_i);
94  ++n_offset;
95  }
96  if (find(A.col_ind.begin() + A.row_ptr.back(),
97  A.col_ind.end(),
98  n2_i) == A.col_ind.end()) {
99  A.col_ind.push_back(n2_i);
100  ++n_offset;
101  }
102  }
103  }
104 
105  // Add the last element to A.row_ptr, defined as length(col_ind)
106  // cf. documentation of BinaryCSRMatrix.
107  A.row_ptr.push_back(A.col_ind.size());
108  return A;
109 }
110 
111 } // namespace
STL namespace.
Namespace for miscellaneous utilities.
Definition: PTreeFile.cpp:44
Represents a binary matrix with n rows and m columns in a Compressed Sparse Row representation.
Definition: CSRMatrix.h:52
Namespace for the core simulator.
Interface for Cell.
Interface for MeshTopology.
std::vector< std::size_t > row_ptr
Indices to col_ind elements where rows start.
Definition: CSRMatrix.h:55
Interface for Node.
std::vector< int > col_ind
Indices of columns that have nonzeros in a row.
Definition: CSRMatrix.h:54
Interface for Mesh.