Mechanics of Defect Evolution Library
Loading...
Searching...
No Matches
Loop.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_Loop_H_
9#define model_Loop_H_
10
11#include <iostream>
12#include <list>
13#include <deque>
14#include <set>
15#include <memory>
16#include <iterator>
17#include <algorithm>
18#include <StaticID.h>
19#include <CRTP.h>
20#include <NetworkBase.h>
21
22#ifndef NDEBUG
23#define VerboseLoop(N,x) if(verboseLevel>=N){std::cout<<cyanColor<<x<<defaultColor;}
24#else
25#define VerboseLoop(N,x)
26#endif
27
28namespace model
29{
30
31
32 template<typename Derived>
33 class Loop : public CRTP<Derived>
34 /* */,public StaticID<Derived>
35 /* */,public NetworkBase<Derived,size_t>
36 /* */,private std::set<typename TypeTraits<Derived>::LoopLinkType*>
37 {
38
39 public:
40
41
46 typedef std::set<LoopLinkType*> LoopLinkContainerType;
47 typedef std::deque<const LoopLinkType*> LoopLinkSequenceType;
48 typedef std::deque<std::pair<std::shared_ptr<LoopNodeType>,std::shared_ptr<LoopNodeType>>> LoopNodeSequenceType;
50
51 private:
52
53 FlowType _flow;
54
55 public:
56
57 static int verboseLevel;
58
59
60
61 Loop(LoopNetworkType* const loopNetwork_in,
62 const FlowType& f) :
63 /* init */ NetworkBaseType(loopNetwork_in,&loopNetwork_in->loops(),this->sID)
64 /* init */,_flow(f)
65 {
66 VerboseLoop(1,"Creating Loop "<<tag()<<std::endl);
67 }
68
70 {
71 VerboseLoop(1,"Destroying Loop "<<tag()<<std::endl);
72 }
73
74 void update()
75 {
76
77 }
78
79 const FlowType& flow() const
80 {
81 return _flow;
82 }
83
85 {
86 return *this;
87 }
88
90 {
91 return *this;
92 }
93
94 void addLoopLink(LoopLinkType* const pL)
95 {
96 VerboseLoop(2,"Loop "<<tag()<<" addLoopLink "<<pL->tag()<<std::endl);
97 const bool success=loopLinks().insert(pL).second;
98 if(!success)
99 {
100 std::cout<<"DislocationLoop "<<this->sID<<" cannot add LoopLink "<<pL->tag()<<std::endl;
101 exit(EXIT_FAILURE);
102 }
103 }
104
106 {
107 VerboseLoop(2,"Loop "<<tag()<<" removeLoopLink "<<pL->tag()<<std::endl);
108 const size_t erased=loopLinks().erase(pL);
109 if(erased!=1)
110 {
111 throw std::runtime_error("Cannot remove link "+pL->tag()+" from loop "+tag());
112 }
113 }
114
116 {
117 //RECODE THIS USING prev/next
118 //typename LoopLinkContainerType::const_iterator iter;
119 VerboseLoop(3,"Loop "<<tag()<<" loopLinks().size()= "<<loopLinks().size()<<std::endl);
121 if(loopLinks().size())
122 {
123 //Start at the link starting at the minimum Node
124 // Select the same minimum-ID start without sorting all links.
125 // Strict comparison preserves the first occurrence on ties.
126 const LoopLinkType* pL=nullptr;
127 for(const auto* link : loopLinks())
128 {
129 if(!pL || link->source->sID<pL->source->sID)
130 {
131 pL=link;
132 }
133 }
134 for(size_t k=0;k<loopLinks().size();++k)
135 {
136 if(pL)
137 {
138 if(pL->next)
139 {
140 temp.push_back(pL);
141 pL=pL->next;
142 }
143 }
144 }
145 }
146 VerboseLoop(3,"Loop "<<tag()<<" linkSequence.size()= "<<temp.size()<<std::endl);
147 return temp;
148 }
149
151 {
153 for(const auto& link : linkSequence())
154 {
155 temp.emplace_back(link->source,link->sink);
156 }
157 return temp;
158 }
159
160 std::deque<size_t> nodeIDSequence() const
161 {
162 std::deque<size_t> temp;
163 for(const auto& link : linkSequence())
164 {
165 temp.emplace_back(link->source->sID);
166 }
167 return temp;
168 }
169
170 std::pair<bool,LoopLinkType*> linkStartingAt(const std::shared_ptr<LoopNodeType>& Ni) const
171 {
172
173 std::pair<bool,LoopLinkType*> temp=std::make_pair(false,static_cast<LoopLinkType*>(nullptr));
174 for(const auto& link : loopLinks())
175 {
176 if(link->source==Ni)
177 {
178 temp=std::make_pair(true,link);
179 break;
180 }
181 }
182 return temp;
183 }
184
185 bool isLoop() const
186 {
187 const LoopLinkSequenceType linkSeq(linkSequence());
188 bool temp(linkSeq.size()>=3);
189 for(typename LoopLinkSequenceType::const_iterator iter=linkSeq.begin();iter!=linkSeq.end();++iter)
190 {
191 auto next=std::next(iter,1);
192 if(next==linkSeq.end())
193 {
194 next=linkSeq.begin();
195 }
196
197 temp=(temp && ((*iter)->sink->sID==(*next)->source->sID));
198 if(!temp)
199 {
200 break;
201 }
202 }
203 return temp;
204 }
205
206 bool isIsolated() const
207 {
208 for(const auto& link : loopLinks())
209 {
210 if(link->networkLink())
211 {
212 if(link->networkLink()->loopLinks().size()!=1)
213 {
214 return false;
215 }
216 }
217 }
218 return true;
219 }
220
221 std::string tag() const
222 {
224 return std::to_string(this->sID);
225 }
226
227 void printLoop() const
228 {
229 std::cout<<"Loop "<<this->sID<<std::endl;
230 const LoopLinkSequenceType linkSeq(linkSequence());
231 for(const auto& link : linkSeq)
232 {
233 std::cout<<" "<<link->tag()
234 <<" (prev "<<link->prev->tag()<<")"
235 <<" (next "<<link->next->tag()<<")"<<std::endl;
236 }
237 }
238
239 };
240
241 template<typename Derived>
243
244}
245#endif
CRTP()
The default constructor.
Definition CRTP.h:37
bool isLoop() const
Definition Loop.h:185
void printLoop() const
Definition Loop.h:227
TypeTraits< Derived >::LoopNetworkType LoopNetworkType
Definition Loop.h:42
LoopLinkContainerType & loopLinks()
Definition Loop.h:84
static int verboseLevel
Definition Loop.h:57
TypeTraits< Derived >::LoopLinkType LoopLinkType
Definition Loop.h:45
void removeLoopLink(LoopLinkType *const pL)
Definition Loop.h:105
std::string tag() const
Definition Loop.h:221
const LoopLinkContainerType & loopLinks() const
Definition Loop.h:89
LoopLinkSequenceType linkSequence() const
Definition Loop.h:115
bool isIsolated() const
Definition Loop.h:206
LoopNodeSequenceType nodeSequence() const
Definition Loop.h:150
Loop(LoopNetworkType *const loopNetwork_in, const FlowType &f)
Definition Loop.h:61
~Loop()
Definition Loop.h:69
void update()
Definition Loop.h:74
std::deque< std::pair< std::shared_ptr< LoopNodeType >, std::shared_ptr< LoopNodeType > > > LoopNodeSequenceType
Definition Loop.h:48
std::set< LoopLinkType * > LoopLinkContainerType
Definition Loop.h:46
std::deque< size_t > nodeIDSequence() const
Definition Loop.h:160
NetworkBase< Derived, size_t > NetworkBaseType
Definition Loop.h:43
std::deque< const LoopLinkType * > LoopLinkSequenceType
Definition Loop.h:47
std::pair< bool, LoopLinkType * > linkStartingAt(const std::shared_ptr< LoopNodeType > &Ni) const
Definition Loop.h:170
void addLoopLink(LoopLinkType *const pL)
Definition Loop.h:94
TypeTraits< LoopLinkType >::FlowType FlowType
Definition Loop.h:49
const FlowType & flow() const
Definition Loop.h:79
TypeTraits< Derived >::LoopNodeType LoopNodeType
Definition Loop.h:44
NetworkBase(const NetworkBase< LoopNetworkType, KeyType > &)=delete
const size_t sID
The static ID of this.
Definition StaticID.h:41
function int_R syms a b c u real f
Definition int_R.m:4
int k
enum insertvertexresult success
vertex temp