Mechanics of Defect Evolution Library
Loading...
Searching...
No Matches
NetworkLink.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_NetworkLink_H_
9#define model_NetworkLink_H_
10
11#include <StaticID.h>
12#include <set>
13#include <CRTP.h>
14#include <NetworkBase.h>
15#include <LoopLink.h>
16
17//#include <NetworkLinkObserver.h>
18//#include <NetworkComponent.h>
19
20
21#ifndef NDEBUG
22#define VerboseNetworkLink(N,x) if(verboseLevel>=N){std::cout<<magentaColor<<x<<defaultColor;}
23#else
24#define VerboseNetworkLink(N,x)
25#endif
26
27namespace model
28{
29 template<typename Derived>
30 class NetworkLink : public StaticID<Derived>
31 /* */,public CRTP<Derived>
32 /* */,public NetworkBase<Derived,std::pair<size_t,size_t>>
33 /* */,private std::set<typename TypeTraits<Derived>::LoopLinkType*>
34 {
35
36 public:
37
44
45 typedef std::set<LoopLinkType*> LoopLinkContainerType;
46
47// typedef NetworkComponent<NodeType,LinkType> NetworkComponentType;
48
49
50// friend class LoopNode<NodeType>; // allow NetworkNode to call private NetworkLink::formNetworkComponent
51
52 private:
53
54
55// std::shared_ptr<NetworkComponentType> psn;
56
57
58 /* formNetworkComponent ***********************************************/
59// void formNetworkComponent(const std::shared_ptr<NetworkComponentType> & psnOther)
60// {
61// if (psn!=psnOther)
62// {
63// psn->remove(this->p_derived());
64// psn=psnOther; // redirect psn to the new NetworkComponent
65// psn->add(this->p_derived()); // add this in the new NetworkComponent
66// }
67// }
68//
69// /**********************************************************************/
70// void changeSN(const NetworkComponent<NodeType,LinkType>& SN)
71// {
72//
73// const std::map<size_t,NodeType* const> tempNodeMap(SN);
74// const std::map<std::pair<size_t,size_t>,LinkType* const> tempLinkMap(SN);
75//
76// for (typename std::map<size_t,NodeType* const>::const_iterator vIter=tempNodeMap.begin();vIter!=tempNodeMap.end();++vIter)
77// {
78// vIter->second->formNetworkComponent(psn);
79// }
80//
81// for (typename std::map<std::pair<size_t,size_t>,LinkType* const>::const_iterator lIter=tempLinkMap.begin();lIter!=tempLinkMap.end();++lIter)
82// {
83// lIter->second->formNetworkComponent(psn);
84// }
85//
86// }
87//
88// /**********************************************************************/
89// void makeTopologyChange()
90// {
91// // Add this to NetworkLinkObserver
92// loopNetwork->addLink(this->p_derived());
93//
94// // Add this to neighobors of source and sink
95//
96// //! 2 - Joins source and sink NetworkComponents
97// if (source->pSN()==sink->pSN()) // source and sink are already in the same NetworkComponent
98// {
99// psn=source->pSN(); // redirect psn to the source psn
100// psn->add(this->p_derived()); // add this to the existing NetworkComponent
101// }
102// else // source and sink are in different NetworkComponents
103// {
104// // find the size of the source and sink
105// size_t sourceSNsize(source->pSN()->nodeOrder());
106// size_t sinkSNsize( sink->pSN()->nodeOrder());
107// if (sourceSNsize>=sinkSNsize)
108// {
109// psn=source->pSN(); // redirect psn to the source psn
110// psn->add(this->p_derived()); // add this to the source NetworkComponent
111// changeSN(*(sink->psn.get()));
112// }
113// else
114// {
115// psn=sink->pSN(); // redirect psn to the sink psn
116// psn->add(this->p_derived()); // add this to the source NetworkComponent
117// changeSN(*(source->psn.get()));
118// }
119// }
120// }
121
122 public:
123
124 const std::shared_ptr<NetworkNodeType> source;
125 const std::shared_ptr<NetworkNodeType> sink;
126
127 static int verboseLevel;
128
129
130 /**********************************************************************/
131 NetworkLink(LoopNetworkType* const loopNetwork,
132 const std::shared_ptr<NetworkNodeType>& nI,
133 const std::shared_ptr<NetworkNodeType>& nJ) :
134 /* init */ NetworkBaseType(loopNetwork,&loopNetwork->networkLinks(),getKey(nI,nJ))
135 /* init */,source(nI->sID<nJ->sID? nI : nJ)
136 /* init */,sink(nI->sID<nJ->sID? nJ : nI)
137 {
138 VerboseNetworkLink(1,"Constructing NetworkLink "<<tag()<<std::endl);
139
140 if (source==sink)
141 {
142 throw std::runtime_error(" Source and Sinks are equal for the node "+std::to_string(source->sID));
143// std::cout<<" Source and Sinks are equal for the node "<<source->sID<<std::endl;
144 }
145 // assert(source!=sink && "Source and Sink cannot be same for a network link");
146 source->addToNeighborhood(this->p_derived());
147 sink->addToNeighborhood(this->p_derived());
148
149 // makeTopologyChange();
150 }
151
152 /**********************************************************************/
154 {
155 VerboseNetworkLink(1,"Destroying NetworkLink "<<tag()<<std::endl);
156
157 if (source!=sink)
158 {
159 source->removeFromNeighborhood(this->p_derived());
160 sink->removeFromNeighborhood(this->p_derived());
161 }
162
163// this->psn->remove(this->p_derived());
164
165// //! 3- If Now Source and Sink are disconnected then reset the NetworkComponent in the sink
166// if(!network().commonNetworkComponent)
167// {
168// const bool sourceCanReachSink(source->depthFirstSearch(sink->sID));
169//
170// if (!sourceCanReachSink)
171// {
172// sink -> resetPSN();
173// }
174// }
175
176
177 }
178
179 std::set<LoopType *> loops() const
180 {
181 std::set<LoopType *> temp;
182 for (const auto &ll : loopLinks())
183 {
184 temp.insert(ll->loop.get());
185 }
186 return temp;
187 }
188
189 std::set<size_t> loopIDs() const
190 {
191 std::set<size_t> temp;
192 for (const auto &loopIter : loops())
193 {
194 temp.insert(loopIter->sID);
195 }
196 return temp;
197 }
198
199 /**********************************************************************/
200 static typename NetworkBaseType::KeyType getKey(const LoopLinkType* const pL)
201 {
202 return getKey(pL->source->networkNode,pL->sink->networkNode);
203 }
204
205 /**********************************************************************/
206 static typename NetworkBaseType::KeyType getKey(const std::shared_ptr<NetworkNodeType>& Ni,
207 const std::shared_ptr<NetworkNodeType>& Nj)
208 {
209 return getKey(Ni->sID,Nj->sID);
210 }
211
212 /**********************************************************************/
213 static typename NetworkBaseType::KeyType getKey(const size_t& i,
214 const size_t& j)
215 {
216 return typename NetworkBaseType::KeyType(std::min(i,j),std::max(i,j));
217 }
218
219 /**********************************************************************/
221 {
222 return *this;
223 }
224
225 /**********************************************************************/
227 {
228 return *this;
229 }
230
231
232 /**********************************************************************/
233 void addLoopLink(LoopLinkType* const pL)
234 {
235 const bool success=loopLinks().insert(pL).second;
236 if(!success)
237 {
238 throw std::runtime_error("Could not insert LoopLink in NetworkLink");
239 }
240 }
241
242 /**********************************************************************/
244 {
245 const size_t erased=loopLinks().erase(pL);
246 if(erased!=1)
247 {
248 throw std::runtime_error("Could not erase LoopLink from NetworkLink");
249 }
250 }
251
252 /**********************************************************************/
253 std::string tag() const
254 {
255 return std::to_string(source->sID)+"->"+std::to_string(sink->sID);
256 }
257
258 };
259
260 template<typename Derived>
262
263}
264#endif
Derived * p_derived()
A pointer to the derived object.
Definition CRTP.h:27
CRTP()
The default constructor.
Definition CRTP.h:37
NetworkBase(const NetworkBase< LoopNetworkType, KeyType > &)=delete
const size_t sID
The static ID of this.
Definition StaticID.h:41
int j
enum insertvertexresult success
int i
vertex temp