PSCF v1.4.0
MaxOrderParameter.tpp
1#ifndef RP_MAX_ORDER_PARAMETER_TPP
2#define RP_MAX_ORDER_PARAMETER_TPP
3
4#include "MaxOrderParameter.h"
5
6#include <prdc/crystal/shiftToMinimum.h>
7#include <prdc/crystal/UnitCell.h>
8
9#include <pscf/mesh/Mesh.h>
10#include <pscf/math/IntVec.h>
11
12#include <util/param/ParamComposite.h>
13#include <util/misc/FileMaster.h>
14#include <util/misc/ioUtil.h>
15#include <util/format/Int.h>
16#include <util/format/Dbl.h>
17#include <util/global.h>
18
19namespace Pscf {
20namespace Rp {
21
22 using namespace Util;
23 using namespace Pscf::Prdc;
24
25 /*
26 * Constructor.
27 */
28 template <int D, class T>
30 typename T::Simulator& simulator,
31 typename T::System& system)
32 : AverageAnalyzerT(simulator, system),
33 kSize_(-1)
34 { ParamComposite::setClassName("MaxOrderParameter"); }
35
36 /*
37 * Setup before main loop.
38 */
39 template <int D, class T>
41 {
42 // Precondition: Require that the system has two monomer types
43 const int nMonomer = system().mixture().nMonomer();
44 UTIL_CHECK(nMonomer == 2);
45
46 AverageAnalyzerT::setup();
47
48 // Set mesh dimensions
49 meshDimensions_ = system().domain().mesh().dimensions();
50 FFTT::computeKMesh(meshDimensions_, kMeshDimensions_, kSize_);
51
52 // Allocate variables
53 if (!wK_.isAllocated()){
54 UTIL_CHECK(!psi_.isAllocated());
55 wK_.allocate(meshDimensions_);
56 psi_.allocate(kMeshDimensions_);
57 }
58 UTIL_CHECK(wK_.capacity() == kSize_);
59 UTIL_CHECK(psi_.capacity() == kSize_);
60 }
61
62 /*
63 * Compute array psi_ of squared Fourier amplitudes.
64 */
65 template <int D, class T>
67 {
68 UTIL_CHECK(system().w().hasData());
69 if (!simulator().hasWc()){
70 simulator().computeWc();
71 }
72 system().domain().fft().forwardTransform(simulator().wc(0), wK_);
73 VecOp::sqAbsV(psi_, wK_);
74 }
75
76 /*
77 * Search for and return maximum Fourier amplitude.
78 */
79 template <int D, class T>
80 void
82 {
83 // Identify index of maximum element of array psi
84 maxPsi_ = psi[1];
85 int maxIndex = 1;
86 for (int i = 2; i < kSize_; ++i){
87 if (psi[i] > maxPsi_){
88 maxPsi_ = psi[i];
89 maxIndex = i;
90 }
91 }
92
93 // Find minimal indices of wavevector Gmax_ of maximum element
94 Mesh<D> kMesh(kMeshDimensions_);
95 Gmax_ = kMesh.position(maxIndex);
96 UnitCell<D> const & unitCell = system().domain().unitCell();
97 Gmax_ = shiftToMinimum(Gmax_, meshDimensions_, unitCell);
98 }
99
100 /*
101 * Output instantaneous value during simulation.
102 */
103 template <int D, class T>
104 void MaxOrderParameter<D,T>::outputValue(int step, double value)
105 {
106 std::ofstream& file = AverageAnalyzerT::outputFile_;
107 UTIL_CHECK(file.is_open());
108 int nSamplePerOutput = AverageAnalyzerT::nSamplePerOutput();
109 if (nSamplePerOutput == 1) {
110 file << Int(step);
111 file << " ( ";
112 for (int i = 0; i < D; i++){
113 file << Int(Gmax_[i],3) << " ";
114 }
115 file << " ) ";
116 file << Dbl(value);
117 file << "\n";
118 } else {
119 file << Int(step);
120 file << Dbl(value);
121 file << "\n";
122 }
123 }
124
125}
126}
127#endif
Description of a regular grid of points in a periodic domain.
Definition Mesh.h:61
IntVec< D > position(int rank) const
Get the position IntVec<D> of a grid point with a specified rank.
Definition Mesh.tpp:89
Base template for UnitCell<D> classes, D=1, 2 or 3.
Definition UnitCell.h:56
void computePsi()
Compute the psi_ array of squared Fourier coefficients.
void outputValue(int step, double value) override
Output a sampled or block average value.
void findMaximum(Array< typename T::Real > const &psi)
Find the wavevector of maximum Fourier magnitude.
void setup() override
Setup before simulation loop.
IntVec< D > Gmax_
Indices of wavevector with maximum magnitude.
double maxPsi_
Maximum square magnitude (value of maximum element of psi_).
T::RField psi_
Square magnitude |W_|^2 in Fourier space.
MaxOrderParameter(typename T::Simulator &simulator, typename T::System &system)
Constructor.
int kSize_
Number of wavevectors in Fourier space (k-grid) mesh.
Array container class template.
Definition Array.h:40
Wrapper for a double precision number, for formatted ostream output.
Definition Dbl.h:40
Wrapper for an int, for formatted ostream output.
Definition Int.h:37
void setClassName(const char *className)
Set class name string.
File containing preprocessor macros for error handling.
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition global.h:68
void sqAbsV(Array< double > &a, Array< fftw_complex > const &b)
Square of absolute magnitude, a[i] = |b[i]|^2 (complex).
Definition VecOpCx.cpp:698
Periodic fields and crystallography.
Definition complex.cpp:11
Class templates for real-valued periodic fields.
PSCF package top-level namespace.