PSCF v1.5.0
ShiftMoveBase.tpp
1#ifndef RP_SHIFT_TPP
2#define RP_SHIFT_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 "ShiftMoveBase.h"
12
13#include <rp/fts/montecarlo/McSimulator.h>
14#include <rp/system/System.h>
15#include <rp/solvers/Mixture.h>
16#include <rp/field/Domain.h>
17#include <rp/field/WFields.h>
18
19#include <pscf/mesh/Mesh.h>
20
21#include <util/containers/Array.h>
22#include <util/random/Random.h>
23
24namespace Pscf {
25namespace Rp {
26
27 using namespace Util;
28
29 /*
30 * Constructor.
31 */
32 template <int D, class T>
34 : McMove<D,T>(simulator),
35 maxShift_(0),
36 isAllocated_(false)
37 { ParamComposite::setClassName("ShiftMove"); }
38
39 /*
40 * Read body of parameter file block.
41 */
42 template <int D, class T>
44 {
45
46 // Read the probability
48
49 // Read the maximum shift
50 ParamComposite::read(in, "maxShift", maxShift_);
51
52 // Validate maxShift_ value
53 UTIL_CHECK(maxShift_ > 0);
54 IntVec<D> const & dim = system().domain().mesh().dimensions();
55 for (int i = 0; i < D; i++) {
56 UTIL_CHECK(maxShift_ < dim[i]);
57 }
58 }
59
60 /*
61 * Setup just before beginning a simulation.
62 */
63 template <int D, class T>
65 {
66 // Setup base class
68
69 // Allocate memory if necessary
70 if (!isAllocated_) {
71 const int nMonomer = system().mixture().nMonomer();
72 IntVec<D> const & dim = system().domain().mesh().dimensions();
73 w_.allocate(nMonomer);
74 for (int i = 0; i < nMonomer; ++i) {
75 w_[i].allocate(dim);
76 }
77 isAllocated_ = true;
78 }
79 }
80
81 /*
82 * Attempt unconstrained move
83 */
84 template <int D, class T>
86 {
87 // Select random displacement by integer numbers of mesh points
88 IntVec<D> shift;
89 for (int i = 0; i < D; i++){
90 shift[i]
91 = McMove<D,T>::random().uniformInt(-maxShift_, maxShift_ + 1);
92 }
93
94 // Compute shifted fields stored in w_ array
95 shiftFields(shift);
96
97 // Update w-fields in parent system
98 system().w().setRGrid(w_);
99 }
100
101 /*
102 * Shift a single field.
103 */
104 template <int D, class T>
106 Array<double> const & in,
107 IntVec<D> shift,
108 IntVec<D> dimensions) const
109 {
110 Mesh<D> mesh(dimensions);
111 IntVec<D> inPosition, outPosition;
112 int meshSize = mesh.size();
113 UTIL_CHECK(out.capacity() == meshSize);
114 UTIL_CHECK(in.capacity() == meshSize);
115
116 for (int i = 0; i < meshSize; ++i) {
117 inPosition = mesh.position(i);
118 for (int d = 0; d < D; ++d){
119 outPosition[d] = inPosition[d] + shift[d];
120 if (outPosition[d] < 0){
121 outPosition[d] += dimensions[d];
122 }
123 UTIL_CHECK(outPosition[d] >= 0);
124 outPosition[d] = outPosition[d] % dimensions[d];
125 }
126 out[mesh.rank(outPosition)] = in[mesh.rank(inPosition)];
127 }
128 }
129
130}
131}
132#endif
An IntVec<D, T> is a D-component vector of elements of integer type T.
Definition IntVec.h:27
Description of a regular grid of points in a periodic domain.
Definition Mesh.h:61
int size() const
Get total number of grid points.
Definition Mesh.h:229
int rank(IntVec< D > const &position) const
Get the rank of a grid point with specified position.
Definition Mesh.tpp:72
IntVec< D > position(int rank) const
Get the position IntVec<D> of a grid point with a specified rank.
Definition Mesh.tpp:89
Random & random()
Get the scalar random number generator.
Definition McMove.h:356
McSimulator< D, T > & simulator()
Get parent McSimulator object (non-const ref).
Definition McMove.h:342
void readProbability(std::istream &in)
Read the probability from file.
Definition McMove.tpp:54
System< D, T > & system()
Get parent System object (non-const ref).
Definition McMove.h:328
virtual void setup()
Setup before the beginning of each simulation run.
Definition McMove.tpp:71
McMove(McSimulator< D, T > &simulator)
Constructor.
Definition McMove.tpp:32
Monte-Carlo simulation coordinator.
Definition McSimulator.h:55
DArray< RField< D, T > > w_
Shifted field configurations.
void attemptMove() override
Attempt move that translates all w fields.
virtual void shiftFields(IntVec< D > const &shift)=0
Compute and store shifted w fields.
void setup() override
Setup before the beginning of each simulation run.
void readParameters(std::istream &in) override
Read body of parameter file block.
ShiftMoveBase(McSimulator< D, T > &simulator)
Constructor.
void shiftField(Array< double > &out, Array< double > const &in, IntVec< D > shift, IntVec< D > dimensions) const
Compute a shifted version of a field.
Array container class template.
Definition Array.h:41
int capacity() const
Return allocated size.
Definition Array.h:250
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.
long uniformInt(long range1, long range2)
Return random long int x uniform in range1 <= x < range2.
Definition Random.h:235
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition global.h:68
Code for programs that simulate real-valued periodic fields.
Definition param_pc.dox:2
PSCF package top-level namespace.