PSCF v1.3
EdgeIterator.cpp
1/*
2* PSCF - Polymer Self-Consistent Field
3*
4* Copyright 2015 - 2025, The Regents of the University of Minnesota
5* Distributed under the terms of the GNU General Public License.
6*/
7
8#include "EdgeIterator.h"
9#include <pscf/chem/PolymerSpecies.h>
10#include <pscf/chem/Edge.h>
11#include <util/containers/Pair.h>
12
13namespace Pscf {
14
15 /*
16 * Constructor
17 */
19 : currentVertexId_(-1),
20 targetVertexId_(-1),
21 polymerPtr_(&polymer)
22 {}
23
24 /*
25 * Destructor
26 */
29
30 /*
31 * Initialize iterator.
32 */
33 void EdgeIterator::begin(int sourceId, int targetId)
34 {
35 UTIL_CHECK(sourceId != targetId);
36 currentEdgeId_ = sourceId;
37 targetEdgeId_ = targetId;
38
39 // Identify ids for vertices of source edge
40 int s0 = polymerPtr_->edge(sourceId).vertexId(0);
41 int s1 = polymerPtr_->edge(sourceId).vertexId(1);
42
43 // Identify ids for vertices of target edge
44 int t0 = polymerPtr_->edge(targetId).vertexId(0);
45 int t1 = polymerPtr_->edge(targetId).vertexId(1);
46
47 // Paths from source vertices towards target vertices
48 Pair<int> p00, p01, p10, p11;
49 p00 = polymerPtr_->path(s0, t0);
50 p01 = polymerPtr_->path(s0, t1);
51 p10 = polymerPtr_->path(s1, t0);
52 p11 = polymerPtr_->path(s1, t1);
53 // Find paths that go through the source edge
54 if (p00[0] == currentEdgeId_) {
55 UTIL_CHECK(p01[0] == currentEdgeId_);
56 UTIL_CHECK(p10[0] != currentEdgeId_);
57 UTIL_CHECK(p11[0] != currentEdgeId_);
58 currentVertexId_ = s1;
59 currentDirectionId_ = 0;
60 } else
61 if (p10[0] == currentEdgeId_) {
62 UTIL_CHECK(p11[0] == currentEdgeId_);
63 UTIL_CHECK(p00[0] != currentEdgeId_);
64 UTIL_CHECK(p01[0] != currentEdgeId_);
65 currentVertexId_ = s0;
66 currentDirectionId_ = 1;
67 } else {
68 UTIL_THROW("Error in finding leading vertex for source");
69 }
70
71 // Paths from target vertices towards source vertices
72 p00 = polymerPtr_->path(t0, s0);
73 p01 = polymerPtr_->path(t0, s1);
74 p10 = polymerPtr_->path(t1, s0);
75 p11 = polymerPtr_->path(t1, s1);
76 // Find paths that go through the target edge
77 if (p00[0] == targetEdgeId_) {
78 UTIL_CHECK(p01[0] == targetEdgeId_);
79 UTIL_CHECK(p10[0] != targetEdgeId_);
80 UTIL_CHECK(p11[0] != targetEdgeId_);
81 targetVertexId_ = t0;
82 } else
83 if (p10[0] == targetEdgeId_) {
84 UTIL_CHECK(p11[0] == targetEdgeId_);
85 UTIL_CHECK(p00[0] != targetEdgeId_);
86 UTIL_CHECK(p01[0] != targetEdgeId_);
87 targetVertexId_ = t1;
88 } else {
89 UTIL_THROW("Error in finding target vertex");
90 }
91
92 }
93
94 /*
95 * Increment vertex - update current vertex to next one in path.
96 */
98 {
100 Pair<int> propId = polymerPtr_->path(currentVertexId_, targetVertexId_);
101 int edgeId = propId[0];
102 int dirId = propId[1];
103 UTIL_CHECK(edgeId >= 0);
104 UTIL_CHECK(edgeId < polymerPtr_->nBlock());
105 UTIL_CHECK(dirId >= 0);
106 UTIL_CHECK(dirId < 2);
107 Edge const & edge = polymerPtr_->edge(edgeId);
108 UTIL_CHECK(edge.vertexId(dirId) == currentVertexId_);
109 currentEdgeId_ = edgeId;
110 currentDirectionId_ = dirId;
111 if (dirId == 0) {
112 currentVertexId_ = edge.vertexId(1);
113 } else {
114 currentVertexId_ = edge.vertexId(0);
115 }
116 return *this;
117 }
118
119 /*
120 * Get the current edge id.
121 */
123 { return currentEdgeId_; }
124
125 /*
126 * Get the current direction id.
127 */
129 { return currentDirectionId_; }
130
131 /*
132 * Get the current vertex id.
133 */
135 { return currentVertexId_; }
136
137 /*
138 * Is the current vertex equal to the target.
139 */
141 {
142 return (bool)( currentVertexId_ == targetVertexId_ );
143 }
144
145 /*
146 * Is the current vertex not equal to the target.
147 */
149 {
150 return (bool)( currentVertexId_ != targetVertexId_ );
151 }
152
153}
int currentVertexId() const
Get index of the current vertex.
int currentEdgeId() const
Get index of the current edge.
~EdgeIterator()
Destructor.
EdgeIterator(PolymerSpecies const &polymer)
Constructor.
int currentDirectionId() const
Get direction index for the path within the current edge.
bool notEnd() const
Return true iff currentId != targetId.
void begin(int sourceId, int targetId)
Initialize iterator.
EdgeIterator & operator++()
Increment operator - move to next vertex.
bool isEnd() const
Return true iff currentId == targetId.
Descriptor for a block within a block polymer.
Definition Edge.h:59
int vertexId(int i) const
Get the id of one associated vertex.
Definition Edge.h:296
Descriptor for a linear or acyclic branched block polymer.
An array of exactly 2 objects.
Definition Pair.h:24
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition global.h:68
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition global.h:49
PSCF package top-level namespace.
Definition param_pc.dox:1