Mechanics of Defect Evolution Library
Loading...
Searching...
No Matches
NetworkNode.h
Go to the documentation of this file.
1/* This file is part of MoDELib, the Mechanics Of Defects Evolution Library.
2 *
3 *
4 * MoDELib is distributed without any warranty under the
5 * GNU General Public License (GPL) v2 <http://www.gnu.org/licenses/>.
6 */
7
8#ifndef model_NetworkNode_H_
9#define model_NetworkNode_H_
10
11#include <iostream>
12#include <set>
13#include <memory>
14#include <assert.h>
15#include <tuple>
16#include <limits.h>
17
18#include <StaticID.h>
19#include <CRTP.h>
20#include <NetworkBase.h>
21
22
23#ifndef NDEBUG
24#define VerboseNetworkNode(N,x) if(verboseLevel>=N){std::cout<<redColor<<x<<defaultColor;}
25#else
26#define VerboseNetworkNode(N,x)
27#endif
28
29namespace model
30{
31
32
33 template<typename Derived>
34 class NetworkNode : public StaticID<Derived>
35 /* */,public CRTP<Derived>
36 /* */,public NetworkBase<Derived,size_t>
37 /* */,public std::set<typename TypeTraits<Derived>::LoopNodeType*>
38 /* */,private std::map<size_t,std::tuple<Derived* const ,typename TypeTraits<Derived>::NetworkLinkType* const>>
39 {
40
41 public:
42
43 typedef Derived NetworkNodeType;
46 typedef typename std::set<LoopLinkType*> LoopLinkContainerType;
50 typedef std::set<LoopNodeType*> LoopNodeContainerType;
52 typedef std::tuple<Derived* const ,NetworkLinkType* const> NeighborType;
53 typedef std::map<size_t,NeighborType> NeighborContainerType;
54
55 public:
56
57 static int verboseLevel;
58
59 NetworkNode(LoopNetworkType* const loopNetwork_in) :
60 /* init */ NetworkBaseType(loopNetwork_in,&loopNetwork_in->networkNodes(),this->sID)
61 {
62 VerboseNetworkNode(1,"Constructing NetworkNode "<<tag()<<std::endl);
63 }
64
65 /**********************************************************************/
67 {
68 VerboseNetworkNode(1,"Destroying NetworkNode "<<tag()<<std::endl);
69 assert(neighbors().empty());
70 }
71
73 {
75 for (const auto &ln : loopNodes())
76 {
77 if (ln->next.second)
78 {
79 temp.insert(ln->next.second);
80 }
81 }
82 return temp;
83 }
84
86 {
88 for (const auto &ln : loopNodes())
89 {
90 if (ln->prev.second)
91 {
92 temp.insert(ln->prev.second);
93 }
94 }
95 return temp;
96 }
97
98 std::set<size_t> loopIDs() const
99 {
100 std::set<size_t> temp;
101 for (const auto &loopIter : loops())
102 {
103 temp.insert(loopIter->sID);
104 }
105 return temp;
106 }
107
108 std::set<LoopType*> loops() const
109 {
110 std::set<LoopType*> temp;
111 for (const auto& ln : loopNodes())
112 {
113 temp.insert(ln->loop().get());
114 }
115 return temp;
116 }
117
119 {
120 return *this;
121 }
122
124 {
125 return *this;
126 }
127
128 void addLoopNode(LoopNodeType* const pN)
129 {
130 const bool success(loopNodes().insert(pN).second);
131 if(!success)
132 {
133 throw std::runtime_error("Duplicate LoopNode could not be added");
134 }
135// assert(success && "Duplicate LoopNode could not be added");
136 }
137
139 {
140 const size_t erased(loopNodes().erase(pN));
141 if(erased!=1)
142 {
143 throw std::runtime_error("LoopNode could not be erased");
144 }
145 }
146
147 bool isContractableTo(const std::shared_ptr<Derived>&) const
148 {
149 return true;
150 }
151
152 size_t gID() const
153 {
156 const auto nodeIter(this->network().networkNodes().find(this->sID));
157 if(nodeIter!=this->network().networkNodes().end())
158 {
159 return std::distance(this->network().networkNodes().begin(),nodeIter);
160 }
161 else
162 {
163 throw std::runtime_error("NetworkNode "+std::to_string(this->sID)+" not found in calling NetworkNode::gID().");
164 return 0;
165 }
166// return ;
167// size_t globalNodeID(const size_t& sID) const
168// {
169//
170// }
171//
172// return this->network().globalNodeID(this->sID);
173 }
174
176 {
177 return *this;
178 }
179
181 {
182 return *this;
183 }
184
186 {
188 if (pL->source->sID==this->sID)
189 {// this vertex is the source of edge *pL, so sink of *pL is the neighbor
190 const NeighborType temp(pL->sink.get(),pL);
191 // std::cout<<" Addng to neighborhood "<<pL->tag()<<std::endl;
192 // std::cout<<" Current Neighborhood "<<std::endl;
193 // for (const auto& neigh : neighbors())
194 // {
195 // std::cout<<std::get<0>(neigh.second)->tag()<<"=>"<<std::get<1>(neigh.second)->tag()<<std::endl;
196 // }
197 const bool success=neighbors().emplace(pL->sink->sID,temp).second;
198 if(!success)
199 {
200 throw std::runtime_error("CANNOT INSERT IN NEIGHBORHOOD.");
201 }
202// assert(success && "CANNOT INSERT IN NEIGHBORHOOD.");
203 }
204 else if (pL->sink->sID==this->sID)
205 {// this vertex is the sink of edge *pL, so source of *pL is the neighbor
206 const NeighborType temp(pL->source.get(),pL);
207 const bool success=neighbors().emplace(pL->source->sID,temp).second;
208 if(!success)
209 {
210 throw std::runtime_error("CANNOT INSERT IN NEIGHBORHOOD.");
211 }
212// assert(success && "CANNOT INSERT IN NEIGHBORHOOD.");
213 }
214 else
215 {
216 throw std::runtime_error("CANNOT INSERT NON-INCIDENT EDGE");
217//
218// assert(0 && "CANNOT INSERT NON-INCIDENT EDGE");
219 }
220 }
221
222 /**********************************************************************/
224 {
225 if (pL->source->sID==this->sID)
226 {
227 const size_t key=pL->sink->sID;
228 const int success=neighbors().erase(key);
229 if(success!=1)
230 {
231 throw std::runtime_error("CANNOT REMOVE FROM NEIGHBORHOOD.");
232 }
233// assert(success==1);
234 }
235 else if (pL->sink->sID==this->sID)
236 {
237 const size_t key=pL->source->sID;
238 const int success=neighbors().erase(key);
239 if(success!=1)
240 {
241 throw std::runtime_error("CANNOT REMOVE FROM NEIGHBORHOOD.");
242 }
243 }
244 else
245 {
246 throw std::runtime_error("CANNOT REMOVE FROM NEIGHBORHOOD.");
247 }
248 }
249
250 std::string tag() const
251 {
253 return std::to_string(this->sID) ;
254 }
255 };
256
257 template<typename Derived>
259}
260#endif
CRTP()
The default constructor.
Definition CRTP.h:37
const LoopNetworkType & network() const
Definition NetworkBase.h:57
NetworkBase(const NetworkBase< LoopNetworkType, KeyType > &)=delete
std::map< size_t, NeighborType > NeighborContainerType
Definition NetworkNode.h:53
TypeTraits< Derived >::LoopNodeType LoopNodeType
Definition NetworkNode.h:49
static int verboseLevel
Definition NetworkNode.h:57
LoopLinkContainerType inLoopLinks() const
Definition NetworkNode.h:85
LoopLinkContainerType outLoopLinks() const
Definition NetworkNode.h:72
bool isContractableTo(const std::shared_ptr< Derived > &) const
void addLoopNode(LoopNodeType *const pN)
NeighborContainerType & neighbors()
void removeFromNeighborhood(NetworkLinkType *const pL)
std::set< LoopNodeType * > LoopNodeContainerType
Definition NetworkNode.h:50
NetworkBase< Derived, size_t > NetworkBaseType
Definition NetworkNode.h:51
Derived NetworkNodeType
Definition NetworkNode.h:43
std::set< size_t > loopIDs() const
Definition NetworkNode.h:98
size_t gID() const
void removeLoopNode(LoopNodeType *const pN)
TypeTraits< Derived >::LoopType LoopType
Definition NetworkNode.h:44
TypeTraits< Derived >::LoopLinkType LoopLinkType
Definition NetworkNode.h:45
LoopNodeContainerType & loopNodes()
NetworkNode(LoopNetworkType *const loopNetwork_in)
Definition NetworkNode.h:59
const NeighborContainerType & neighbors() const
const LoopNodeContainerType & loopNodes() const
TypeTraits< Derived >::LoopNetworkType LoopNetworkType
Definition NetworkNode.h:48
std::set< LoopType * > loops() const
std::string tag() const
std::tuple< Derived *const,NetworkLinkType *const > NeighborType
Definition NetworkNode.h:52
void addToNeighborhood(NetworkLinkType *const pL)
TypeTraits< Derived >::NetworkLinkType NetworkLinkType
Definition NetworkNode.h:47
std::set< LoopLinkType * > LoopLinkContainerType
Definition NetworkNode.h:46
const size_t sID
The static ID of this.
Definition StaticID.h:41
int end[2]
enum insertvertexresult success
vertex temp