PSCF v1.2
EdgeIterator.cpp
1/*
2* PSCF - Polymer Self-Consistent Field Theory
3*
4* Copyright 2016 - 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 } else
60 if (p10[0] == currentEdgeId_) {
61 UTIL_CHECK(p11[0] == currentEdgeId_);
62 UTIL_CHECK(p00[0] != currentEdgeId_);
63 UTIL_CHECK(p01[0] != currentEdgeId_);
64 currentVertexId_ = s0;
65 } else {
66 UTIL_THROW("Error in finding leading vertex for source");
67 }
68
69 // Paths from target vertices towards source vertices
70 p00 = polymerPtr_->path(t0, s0);
71 p01 = polymerPtr_->path(t0, s1);
72 p10 = polymerPtr_->path(t1, s0);
73 p11 = polymerPtr_->path(t1, s1);
74 // Find paths that go through the target edge
75 if (p00[0] == targetEdgeId_) {
76 UTIL_CHECK(p01[0] == targetEdgeId_);
77 UTIL_CHECK(p10[0] != targetEdgeId_);
78 UTIL_CHECK(p11[0] != targetEdgeId_);
79 targetVertexId_ = t0;
80 } else
81 if (p10[0] == targetEdgeId_) {
82 UTIL_CHECK(p11[0] == targetEdgeId_);
83 UTIL_CHECK(p00[0] != targetEdgeId_);
84 UTIL_CHECK(p01[0] != targetEdgeId_);
85 targetVertexId_ = t1;
86 } else {
87 UTIL_THROW("Error in finding target vertex");
88 }
89
90 }
91
92 /*
93 * Increment vertex - update current vertex to next one in path.
94 */
96 {
98 Pair<int> propId = polymerPtr_->path(currentVertexId_, targetVertexId_);
99 int edgeId = propId[0];
100 int dirId = propId[1];
101 UTIL_CHECK(edgeId >= 0);
102 UTIL_CHECK(edgeId < polymerPtr_->nBlock());
103 UTIL_CHECK(dirId >= 0);
104 UTIL_CHECK(dirId < 2);
105 Edge const & edge = polymerPtr_->edge(edgeId);
106 UTIL_CHECK(edge.vertexId(dirId) == currentVertexId_);
107 if (dirId == 0) {
108 currentEdgeId_ = edgeId;
109 currentVertexId_ = edge.vertexId(1);
110 } else {
111 currentEdgeId_ = edgeId;
112 currentVertexId_ = edge.vertexId(0);
113 }
114 return *this;
115 }
116
117 /*
118 * Get the current edge id.
119 */
121 { return currentEdgeId_; }
122
123 /*
124 * Get the current vertex id.
125 */
127 { return currentVertexId_; }
128
129 /*
130 * Is the current vertex equal to the target:
131 */
133 {
134 return (bool)( currentVertexId_ == targetVertexId_ );
135 }
136
137 /*
138 * Is the current vertex not equal to the target.
139 */
141 {
142 return (bool)( currentVertexId_ != targetVertexId_ );
143 }
144
145}
Edge iterator for graph associated with a polymer.
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.
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 an acyclic block polymer.
Definition Edge.h:50
int vertexId(int i) const
Get id of an associated vertex.
Definition Edge.h:247
Descriptor for a linear or acyclic branched block polymer.
Pair< int > const & path(int is, int it) const
Get an id for a propagator from one vertex towards a target.
virtual Edge & edge(int id)=0
Get a specified Edge (block descriptor) by non-const reference.
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:51
PSCF package top-level namespace.
Definition param_pc.dox:1