PSCF v1.5.0
PropagatorBase.tpp
1#ifndef RP_PROPAGATOR_BASE_TPP
2#define RP_PROPAGATOR_BASE_TPP
3
4/*
5* PSCF - Polymer Self-Consistent Field
6*
7* Copyright 2015 - 2026, David Morse (morse012@um.edu) and co-authors
8* Distributed under the terms of the GNU General Public License.
9*/
10
11#include <rp/solvers/PropagatorBase.h>
12#include <rp/solvers/Block.h>
13#include <pscf/field/RField.h>
14
15#include <pscf/backend/Reduce.h>
16#include <pscf/backend/VecOp.h>
17#include <pscf/mesh/Mesh.h>
18
19namespace Pscf {
20namespace Rp {
21
22 using namespace Util;
23
24 /*
25 * Constructor.
26 */
27 template <int D, class T>
29 : ns_(0),
30 isAllocated_(false),
31 blockPtr_(nullptr),
32 meshPtr_(nullptr),
33 hasComputedHead_(false)
34 {}
35
36 /*
37 * Destructor.
38 */
39 template <int D, class T>
42
43 /*
44 * Allocate memory used by this propagator.
45 */
46 template <int D, class T>
48 {
50 ns_ = ns;
51 meshPtr_ = &mesh;
52 }
53
54 /*
55 * Reallocate memory used by this propagator using new ns value.
56 */
57 template <int D, class T>
59 {
60 UTIL_CHECK(meshPtr_);
62 UTIL_CHECK(ns_ != ns);
63 ns_ = ns;
64 }
65
66 /*
67 * Compute initial head q-field as a product of tail slices of sources.
68 */
69 template <int D, class T>
71 {
72 UTIL_CHECK(meshPtr_);
74
75 // Reference to head slice of this propagator
76 RField<D,T>& qh = qFields_[0];
77
78 // Initialize head slice qh to 1.0 at all grid points
79 VecOp::eqS(qh, 1.0);
80
81 // Pointwise multiply tail q-fields of all source propagators
83 const int ns = PropagatorTmplT::nSource();
84 UTIL_CHECK(ns > 0);
85 for (int is = 0; is < ns; ++is) {
86 if (!source(is).isSolved()) {
87 UTIL_THROW("Source not solved in computeHead");
88 }
89 VecOp::mulEqV(qh, source(is).tail());
90 }
91 }
92
93 hasComputedHead_ = true;
94 }
95
96 /*
97 * Solve the modified diffusion equation for this block.
98 *
99 * This function calls computeHead to compute the head slice as
100 * a product of tail slices from source propagators.
101 */
102 template <int D, class T>
104 {
105 // Set flags to default unsolved state
107 hasComputedHead_ = false;
108
109 computeHead();
110 solveMde();
111
112 // Postconditions
114 UTIL_CHECK(hasComputedHead_);
115 }
116
117 /*
118 * Solve the MDE with a specified initial condition at the head.
119 */
120 template <int D, class T>
122 {
123 UTIL_CHECK(meshPtr_);
125 UTIL_CHECK(head.capacity() == mesh().size());
126
127 // Set flags to default unsolved state
129 hasComputedHead_ = false;
130
131 // Initialize initial (head) slice
133
134 solveMde();
135
136 // Postconditions
138 UTIL_CHECK(!hasComputedHead_);
139 }
140
141 /*
142 * Solve the MDE, using stored precomputed head slice (private).
143 */
144 template <int D, class T>
146 {
147 UTIL_CHECK(blockPtr_);
149
151
152 // MDE step loop for thread model
153 for (int iStep = 0; iStep < ns_ - 1; ++iStep) {
154 block().stepThread(qFields_[iStep], qFields_[iStep + 1]);
155 }
156
157 } else
158 if (PolymerModel::isBead()) {
159
160 // Half-bond and bead weight for first bead
163 } else {
164 block().stepHalfBondBead(qFields_[0], qFields_[1]);
165 }
166 block().stepFieldBead(qFields_[1]);
167
168 // MDE step loop for bead model (stop before tail vertex)
169 int iStep;
170 for (iStep = 1; iStep < ns_ - 2; ++iStep) {
171 block().stepBead(qFields_[iStep], qFields_[iStep + 1]);
172 }
173
174 // Half-bond for tail slice, applied to slice for the last bead
176 // Don't compute tail for chain end, since it won't be needed
178 } else {
179 // If not a chain end, apply a half-bond after the last bead
180 block().stepHalfBondBead(qFields_[ns_-2], qFields_[ns_-1]);
181 }
182
183 } else {
184 // This should be impossible
185 UTIL_THROW("Unexpected PolymerModel type");
186 }
187
189 }
190
191 /*
192 * Compute spatial average of product of head and tail of partner.
193 */
194 template <int D, class T>
195 void PropagatorBase<D,T>::computeQ(double & Q) const
196 {
197 // Preconditions
198 UTIL_CHECK(meshPtr_);
201 UTIL_THROW("Propagator is not solved.");
202 }
204 UTIL_THROW("Propagator has no partner set.");
205 }
206 if (!partner().isSolved()) {
207 UTIL_THROW("Partner propagator is not solved");
208 }
210
211 Q = 0.0;
213 // Tail of partner (ns_ - 1) is not defined
214 if (hasComputedHead_) {
215 // Computed head for an end is q(r,s) = 1 for all r.
216 // Compute average of q for last bead of partner
217 Q = Reduce::sum(partner().q(ns_-2));
218 } else {
219 // Compute average product for the first / last bead
221 }
222 } else {
223 // Compute average product of head slice and partner tail slice
225 }
226 Q /= double(mesh().size());
227 }
228
229} // namespace Rp
230} // namespace Pscf
231#endif
Description of a regular grid of points in a periodic domain.
Definition Mesh.h:61
int ns_
Number of slices, including head and tail slices.
virtual void reallocate(int ns)
Reallocate memory used by this propagator.
const Propagator< D, T > & partner() const
Get partner propagator.
int ns() const
Get the number of values of s (or slices), including head and tail.
void solve()
Solve the modified diffusion equation (MDE) for this block.
void solveMde()
Compute solution of modified diffusion equation (MDE).
RField< D, T > const & q(int i) const
Return q-field at a specified step.
DArray< RField< D, T > > qFields_
Array of propagator slices at different contour variable values.
Mesh< D > const & mesh() const
Return the associated Mesh object by const reference.
RField< D, T > const & head() const
Return q-field at the initial (head) vertex.
virtual void allocate(int ns, const Mesh< D > &mesh)
Allocate memory used by this propagator.
RField< D, T > const & tail() const
Return q-field at the terminal (tail) vertex.
const Propagator< D, T > & source(int id) const
Get a source propagator.
bool isAllocated_
Is this propagator allocated?
Block< D, T > const & block() const
Get the associated Block object by const reference.
void computeQ(double &Q) const
Compute and return partition function for the polymer molecule.
void computeHead()
Compute initial q-field at the head vertex.
#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
double innerProduct(Array< double > const &a, Array< double > const &b)
Compute Euclidean inner product of two real arrays .
Definition Reduce.cpp:176
double sum(Array< double > const &in)
Compute sum of array elements (real).
Definition Reduce.cpp:19
void mulEqV(Array< double > &a, Array< double > const &b)
Vector-vector in-place multiplication, a[i] *= b[i] (real).
Definition VecOp.cpp:257
void eqV(Array< double > &a, Array< double > const &b, const int beginIdA, const int beginIdB, const int n)
Vector assignment, a[i] = b[i] (real, slice).
Definition VecOp.cpp:26
void eqS(Array< double > &a, double b)
Vector assignment, a[i] = b (real).
Definition VecOp.cpp:55
bool isThread()
Is the thread model in use ?
bool isBead()
Is the bead model in use ?
Code for programs that simulate real-valued periodic fields.
Definition param_pc.dox:2
PSCF package top-level namespace.