PSCF v1.5.0
PolymerSpecies.tpp
1/*
2* PSCF - Polymer Self-Consistent Field
3*
4* Copyright 2015 - 2026, David Morse (morse012@um.edu) and co-authors
5* Distributed under the terms of the GNU General Public License.
6*/
7
8#include "PolymerSpecies.h"
9
10#include <pscf/chem/Edge.h>
11#include <util/containers/GArray.h>
12#include <util/containers/FArray.h>
13#include <util/containers/DMatrix.h>
14#include <util/format/Int.h>
15
16#include <cmath>
17
18namespace Pscf
19{
20
21 /*
22 * Constructor.
23 */
24 template <typename WT>
26 : Species<WT>(),
27 vertices_(),
28 propagatorIds_(),
29 paths_(),
30 nBlock_(0),
31 nVertex_(0),
32 nPropagator_(0),
33 type_(PolymerType::Linear)
34 { ParamComposite::setClassName("PolymerSpecies"); }
35
36 /*
37 * Read parameter file block.
38 */
39 template <typename WT>
41 {
42 // Read polymer type (linear by default)
43 type_ = PolymerType::Linear;
45
46 ParamComposite::read<int>(in, "nBlock", nBlock_);
47
48 // Note: For any acyclic graph, nVertex = nBlock + 1
49 nVertex_ = nBlock_ + 1;
50
51 // Allocate all arrays
53 vertices_.allocate(nVertex_);
54 propagatorIds_.allocate(2*nBlock_);
55
56 // Set block id and polymerType for all blocks
57 for (int edgeId = 0; edgeId < nBlock_; ++edgeId) {
58 edge(edgeId).setId(edgeId);
59 edge(edgeId).setPolymerType(type_);
60 }
61
62 // Set sequential ids for all vertices
63 for (int vertexId = 0; vertexId < nVertex_; ++vertexId) {
64 vertices_[vertexId].setId(vertexId);
65 }
66
67 // If this is a linear polymer, set all block vertex Ids:
68 if (type_ == PolymerType::Linear) {
69 // In a linear chain, block i connects vertices i and i+1.
70 for (int blockId = 0; blockId < nBlock_; ++blockId) {
71 edge(blockId).setVertexIds(blockId, blockId + 1);
72 }
73 }
74
75 // Read array of block data from parameter file
76 readBlocks(in);
78 // Read phi or mu (but not both) and set ensemble accordingly
80
81 // Reading of parameter file is now complete
82
83 // Add edges to attached vertices
84 int vertexId0, vertexId1;
85 Edge* edgePtr;
86 for (int edgeId = 0; edgeId < nBlock_; ++edgeId) {
87 edgePtr = &(edge(edgeId));
88 vertexId0 = edgePtr->vertexId(0);
89 vertexId1 = edgePtr->vertexId(1);
90 if (vertexId0 < 0 || vertexId0 >= nVertex_ ||
91 vertexId1 < 0 || vertexId1 >= nVertex_) {
92 UTIL_THROW("Vertex id out of range in polymer block");
93 }
94 vertices_[vertexId0].addEdge(*edgePtr);
95 vertices_[vertexId1].addEdge(*edgePtr);
96 }
97
98 // Check that every vertex has one or more attached edges
99 for (int vertexId = 0; vertexId <= nBlock_; ++vertexId) {
100 UTIL_CHECK(vertices_[vertexId].size() > 0);
101 }
102
103 // Polymer graph topology is now fully specified.
104
105 // Construct a plan for the order in which block propagators
106 // should be computed when solving the MDE.
107 makePlan();
108
109 // Construct vertex-to-vertex path signposts
110 makePaths();
111
112 // Check internal consistency of edge and vertex data
113 isValid();
114
115 }
116
117 /*
118 * Make a plan for the order of computation of block propagators.
119 */
120 template <typename WT>
122 {
123 if (nPropagator_ != 0) {
124 UTIL_THROW("nPropagator !=0 on entry");
125 }
126
127 // Allocate and initialize isFinished matrix (all elements false)
128 DMatrix<bool> isFinished;
129 isFinished.allocate(nBlock_, 2);
130 for (int iBlock = 0; iBlock < nBlock_; ++iBlock) {
131 for (int iDirection = 0; iDirection < 2; ++iDirection) {
132 isFinished(iBlock, iDirection) = false;
133 }
134 }
135
136 Pair<int> propId;
137 Vertex* inVertexPtr = 0;
138 int inVertexId = -1;
139 bool isReady;
140 int nPrevious;
141 while (nPropagator_ < nBlock_*2) {
142 nPrevious = nPropagator_;
143
144 // Loop over blocks and directions, add new propagators
145 for (int iBlock = 0; iBlock < nBlock_; ++iBlock) {
146 for (int iDirection = 0; iDirection < 2; ++iDirection) {
147 if (!isFinished(iBlock, iDirection)) {
148 inVertexId = edge(iBlock).vertexId(iDirection);
149 inVertexPtr = &vertices_[inVertexId];
150
151 // Check if unfinished propagator is ready
152 isReady = true;
153 for (int j = 0; j < inVertexPtr->size(); ++j) {
154 propId = inVertexPtr->inPropagatorId(j);
155 if (propId[0] != iBlock) {
156 if (!isFinished(propId[0], propId[1])){
157 isReady = false;
158 break;
160 }
161 }
162
163 // If its ready, add it to propagatorsIds_ array
164 if (isReady) {
165 propagatorIds_[nPropagator_][0] = iBlock;
166 propagatorIds_[nPropagator_][1] = iDirection;
167 isFinished(iBlock, iDirection) = true;
168 ++nPropagator_;
169 }
170
171 } // if (!isFinished)
172 } // loop over directions
173 } // loop over blocks
174
175 // No propagator became ready in a pass - not a tree
176 if (nPropagator_ == nPrevious) {
177 UTIL_THROW("Polymer blocks do not form a tree");
178 }
179
180 }
181
182 }
183
184 /*
185 * Identify paths between vertices.
186 */
187 template <typename WT>
189 {
190 UTIL_CHECK(nVertex_ > 0);
191
192 // Local variables
193 DArray< GArray< FArray<int, 3> > > sendPaths;
194 DArray< GArray< FArray<int, 3> > > recvPaths;
197 Pair<int> pId;
198 int is; // sender vertex id
199 int ir; // receiver vertex id
200 int ib; // bond id
201 int io; // outgoing (source) direction id
202 int ii; // incoming (receiver) direction id
203 int j, k, n;
204
205 // Allocate and clear path containers
206 sendPaths.allocate(nVertex_);
207 recvPaths.allocate(nVertex_);
208 oldPaths.allocate(nVertex_);
209 for (is = 0; is < nVertex_; ++is) {
210 sendPaths[is].clear();
211 recvPaths[is].clear();
212 oldPaths[is].clear();
213 }
214
215 // Initialize sendPaths with path-to-self entries
216 for (is = 0; is < nVertex_; ++is) {
217 path[0] = is;
218 path[1] = -1;
219 path[2] = -1;
220 sendPaths[is].append(path);
221 }
222
223 // While loop to completion
224 bool done = false;
225 int iter = 0;
226 while (!done) {
227 UTIL_CHECK(iter < nBlock_ + 2);
228
229 // Check that recvPaths container is empty
230 for (ir = 0; is < nVertex_; ++is) {
231 UTIL_CHECK(recvPaths[ir].size() == 0);
232 }
233
234 // Loop over source vertices for sending
235 done = true;
236 for (is = 0; is < nVertex_; ++is) {
237
238 // Send any sendPaths data to neighbors
239 int n = sendPaths[is].size();
240 if (n > 0) {
241 done = false;
242 Vertex const & sender = vertex(is);
243
244 // Send sendPaths data to all new neighbors
245 for (j = 0; j < sender.size(); ++j) {
246 pId = sender.outPropagatorId(j);
247 ib = pId[0]; // block identifier
248 io = pId[1]; // outgoing direction identifier
249 if (io == 0) {
250 UTIL_CHECK(edge(ib).vertexId(0) == is);
251 ii = 1;
252 } else {
253 UTIL_CHECK(edge(ib).vertexId(1) == is);
254 ii = 0;
255 }
256 ir = edge(ib).vertexId(ii);
257 for (k = 0; k < n; ++k) {
258 path = sendPaths[is][k];
259 // Send unless just received along same bond
260 if (ib != path[1]) {
261 path[1] = ib;
262 path[2] = ii;
263 recvPaths[ir].append(path);
264 }
265 }
266 }
267
268 } // if (n > 0)
269 } // Loop over source vertices
270
271 // Loop over vertices to transfer data structures
272 for (is = 0; is < nVertex_; ++is) {
273
274 // Move sendPaths to oldPaths, clear sendPaths
275 n = sendPaths[is].size();
276 if (n > 0) {
277 for (k = 0; k < n; ++k) {
278 path = sendPaths[is][k];
279 oldPaths[is].append(path);
280 }
281 }
282 sendPaths[is].clear();
283
284 // Move recvPaths to sendPaths, clear recvPaths
285 n = recvPaths[is].size();
286 if (n > 0) {
287 for (k = 0; k < n; ++k) {
288 sendPaths[is].append(recvPaths[is][k]);
289 }
290 }
291 recvPaths[is].clear();
292
293 }
294
295 ++iter;
296 } // while not done
297
298 // Allocate and initialize member variable paths_
299 paths_.allocate(nVertex_);
300 for (is = 0; is < nVertex_; ++is) {
301 paths_[is].allocate(nVertex_);
302 for (ir = 0; ir < nVertex_; ++ir) {
303 paths_[is][ir][0] = -1;
304 paths_[is][ir][1] = -1;
305 }
306 }
307
308 // Assign values to all elements of paths_ container
309 for (is = 0; is < nVertex_; ++is) {
310 n = oldPaths[is].size();
311 UTIL_CHECK(n == nVertex_);
312 for (k = 0; k < n; ++k) {
313 path = oldPaths[is][k];
314 ir = path[0]; // id of target vertex
315 UTIL_CHECK(ir >= 0);
316 UTIL_CHECK(ir < nVertex_);
317 // Check that element was not set previously
318 UTIL_CHECK(paths_[is][ir][0] == -1);
319 UTIL_CHECK(paths_[is][ir][1] == -1);
320 //std::cout << std::endl << is << " " << ir << " "
321 // << path[1] << " " << path[2];
322 if (ir == is) {
323 UTIL_CHECK(path[1] == -1);
324 UTIL_CHECK(path[2] == -1);
325 } else {
326 UTIL_CHECK(path[1] >= 0);
327 UTIL_CHECK(path[1] < nBlock_);
328 UTIL_CHECK(path[2] >= 0);
329 UTIL_CHECK(path[2] < 2);
330 }
331 paths_[is][ir][0] = path[1];
332 paths_[is][ir][1] = path[2];
333 }
334 }
335
336 }
337
338 /*
339 * Check consistency of internal data structures.
340 */
341 template <typename WT>
342 void PolymerSpecies<WT>::isValid()
343 {
344 Pair<int> pair;
345 int ib, iv, ip, id, iv0, iv1, n;
346
347 // Check validity of ids owned by edges
348 for (ib = 0; ib < nBlock_; ++ib) {
349 UTIL_CHECK(edge(ib).id() == ib);
350 iv0 = edge(ib).vertexId(0);
351 iv1 = edge(ib).vertexId(1);
352 UTIL_CHECK(iv0 != iv1);
353 UTIL_CHECK(iv0 >= 0);
354 UTIL_CHECK(iv0 < nVertex_);
355 UTIL_CHECK(iv1 >= 0);
356 UTIL_CHECK(iv1 < nVertex_);
357 }
358
359 // Check consistency of vertex::outPropagatorId
360 for (iv = 0; iv < nVertex_; ++iv) {
361 UTIL_CHECK(vertex(iv).id() == iv);
362 n = vertex(iv).size();
363 for (ip = 0; ip < n; ++ip) {
364 pair = vertex(iv).outPropagatorId(ip);
365 ib = pair[0];
366 id = pair[1];
367 UTIL_CHECK(ib >= 0);
368 UTIL_CHECK(ib < nBlock_);
369 UTIL_CHECK(id >= 0);
370 UTIL_CHECK(id < 2);
371 UTIL_CHECK(edge(ib).vertexId(id) == iv);
372 }
373 }
374
375 // Check consistency of vertex::inPropagatorId
376 for (iv = 0; iv < nVertex_; ++iv) {
377 UTIL_CHECK(vertex(iv).id() == iv);
378 n = vertex(iv).size();
379 for (ip = 0; ip < n; ++ip) {
380 pair = vertex(iv).inPropagatorId(ip);
381 ib = pair[0];
382 id = pair[1];
383 UTIL_CHECK(ib >= 0);
384 UTIL_CHECK(ib < nBlock_);
385 UTIL_CHECK(id >= 0);
386 UTIL_CHECK(id < 2);
387 if (id == 0) {
388 UTIL_CHECK(edge(ib).vertexId(1) == iv);
389 } else {
390 UTIL_CHECK(edge(ib).vertexId(0) == iv);
391 }
392 }
393 }
394
395 // Check consistency of vertex ids in paths
396 for (iv0 = 0; iv0 < nVertex_; ++iv0) {
397 for (iv1 = 0; iv1 < nVertex_; ++iv1) {
398 pair = path(iv0, iv1);
399 ib = pair[0];
400 id = pair[1];
401 if (iv0 == iv1) {
402 UTIL_CHECK(ib == -1);
403 UTIL_CHECK(id == -1);
404 } else {
405 UTIL_CHECK(edge(ib).vertexId(id) == iv0);
406 }
407 }
408 }
409
410 }
411
412 /*
413 * Total length of all blocks = volume / reference volume
414 */
415 template <typename WT>
417 {
419 double value = 0.0;
420 for (int blockId = 0; blockId < nBlock_; ++blockId) {
421 value += edge(blockId).length();
422 }
423 return value;
424 }
425
426 /*
427 * Total number of beads in all blocks.
428 */
429 template <typename WT>
431 {
433 int value = 0.0;
434 for (int blockId = 0; blockId < nBlock_; ++blockId) {
435 value += edge(blockId).nBead();
436 }
437 return value;
438 }
439
440}
Descriptor for one block within a block polymer.
Definition Edge.h:62
int vertexId(int i) const
Get the id of one associated vertex.
Definition Edge.h:299
virtual Edge & edge(int id)=0
Get a specified Edge (block descriptor) by non-const reference.
int nBead() const
Total number of beads in the polymer (bead model).
double length() const
Sum of the lengths of all blocks in the polymer (thread model).
void makePaths()
Create a matrix of vertex-to-vertex path signposts.
Pair< int > const & path(int is, int it) const
Get an id for a propagator from one vertex towards a target.
PolymerSpecies()
Constructor.
virtual void readParameters(std::istream &in)
Read parameters and initialize.
virtual void readBlocks(std::istream &in)=0
Read array of blocks from parameter file.
const Vertex & vertex(int id) const
Get a specified Vertex by const reference.
virtual void allocateBlocks()=0
Allocate array of blocks.
virtual void makePlan()
Make a plan for order in which propagators should be computed.
virtual void readParameters(std::istream &in)
Read phi or mu (but not both) and set ensemble accordingly.
Definition Species.tpp:36
A junction or chain end in a block polymer.
Definition Vertex.h:31
int size() const
Get the number of attached blocks.
Definition Vertex.h:110
Pair< int > const & inPropagatorId(int i) const
Get the block and direction of an incoming propagator.
Definition Vertex.h:114
Pair< int > const & outPropagatorId(int i) const
Get the block and direction of an outgoing propagator.
Definition Vertex.h:118
int size() const
Return logical size of array.
Definition Array.h:243
Dynamically allocatable contiguous array template.
Definition DArray.h:32
void allocate(int capacity)
Allocate the underlying C array.
Definition DArray.h:262
Dynamically allocated Matrix.
Definition DMatrix.h:25
void allocate(int capacity1, int capacity2)
Allocate memory for a matrix.
Definition DMatrix.h:170
A fixed size, stack-allocated array.
Definition FArray.h:44
An array of exactly 2 objects.
Definition Pair.h:24
ScalarParam< Type > & read(std::istream &in, const char *label, Type &value)
Add and read a new required ScalarParam < Type > object.
void setClassName(const char *className)
Set class name string.
ScalarParam< Type > & readOptional(std::istream &in, const char *label, Type &value)
Add and read a new optional ScalarParam < Type > object.
#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
bool isThread()
Is the thread model in use ?
bool isBead()
Is the bead model in use ?
PSCF package top-level namespace.
Struct containing an enumeration of polymer structure types.
Definition PolymerType.h:27