PSCF v1.5.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 <rp/system/System.h>
7#include <rp/fts/simulator/Simulator.h>
8#include <rp/solvers/Mixture.h>
9#include <rp/field/Domain.h>
10#include <rp/field/WFields.h>
11
12#include <pscf/field/FFT.h>
13#include <pscf/crystal/shiftToMinimum.h>
14#include <pscf/crystal/UnitCell.h>
15
16#include <pscf/backend/VecOp.h>
17#include <pscf/mesh/Mesh.h>
18#include <pscf/math/IntVec.h>
19
20#include <util/param/ParamComposite.h>
21#include <util/misc/FileMaster.h>
22#include <util/misc/ioUtil.h>
23#include <util/format/Int.h>
24#include <util/format/Dbl.h>
25#include <util/global.h>
26
27namespace Pscf {
28namespace Rp {
29
30 using namespace Util;
31
32 /*
33 * Constructor.
34 */
35 template <int D, class T>
42
43 /*
44 * Setup before main loop.
45 */
46 template <int D, class T>
48 {
49 // Precondition: Require that the system has two monomer types
50 const int nMonomer = system().mixture().nMonomer();
51 UTIL_CHECK(nMonomer == 2);
52
54
55 // Set mesh dimensions
56 meshDimensions_ = system().domain().mesh().dimensions();
57 FFT<D,T>::computeKMesh(meshDimensions_, kMeshDimensions_, kSize_);
58
59 // Allocate variables
60 if (!wK_.isAllocated()){
61 UTIL_CHECK(!psi_.isAllocated());
62 wK_.allocate(meshDimensions_);
63 psi_.allocate(kMeshDimensions_);
64 }
65 UTIL_CHECK(wK_.capacity() == kSize_);
66 UTIL_CHECK(psi_.capacity() == kSize_);
67 }
68
69 /*
70 * Compute and return maximum of square magnitude Fourier amplitude.
71 */
72 template <int D, class T>
74 {
75 // Preconditions
76 int kSize = kSize_;
77 UTIL_CHECK(kSize > 0);
78 UTIL_CHECK(psi_.capacity() == kSize);
79
80 // Compute backend array psi_ of squared Fourier magnitudes
81 //computePsi();
82 UTIL_CHECK(system().w().hasData());
83 if (!simulator().hasWc()){
84 simulator().computeWc();
85 }
86 system().domain().fft().forwardTransform(simulator().wc(0), wK_);
87 VecOp::sqAbsV(psi_, wK_);
88
89 // Copy backend array psi_ to const host array psi_h_
90 psi_h_ = psi_;
91
92 // Compute maximum from host array
93 // findMaximum();
94 // Identify index of maximum element of array psi_h_
95 maxPsi_ = psi_h_[1];
96 int maxIndex = 1;
97 for (int i = 2; i < kSize_; ++i){
98 if (psi_h_[i] > maxPsi_){
99 maxPsi_ = psi_h_[i];
100 maxIndex = i;
101 }
102 }
103
104 // Find minimal indices of wavevector Gmax_ of maximum element
105 Mesh<D> kMesh(kMeshDimensions_);
106 Gmax_ = kMesh.position(maxIndex);
107 UnitCell<D> const & unitCell = system().domain().unitCell();
108 Gmax_ = shiftToMinimum(Gmax_, meshDimensions_, unitCell);
109
110 // Release psi_h_
111 psi_h_.dissociate();
112
113 return maxPsi_;
114 }
115
116 /*
117 * Output instantaneous value during simulation.
118 */
119 template <int D, class T>
120 void MaxOrderParameter<D,T>::outputValue(int step, double value)
121 {
122 std::ofstream& file = AverageAnalyzer<D,T>::outputFile_;
123 UTIL_CHECK(file.is_open());
125 if (nSamplePerOutput == 1) {
126 file << Int(step);
127 file << " ( ";
128 for (int i = 0; i < D; i++){
129 file << Int(Gmax_[i],3) << " ";
130 }
131 file << " ) ";
132 file << Dbl(value);
133 file << "\n";
134 } else {
135 file << Int(step);
136 file << Dbl(value);
137 file << "\n";
138 }
139 }
140
141}
142}
143#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
Simulator< D, T > & simulator()
Get the parent Simulator by reference.
Definition Analyzer.h:233
System< D, T > & system()
Get the parent System by reference.
Definition Analyzer.h:243
int nSamplePerOutput() const
Get value of nSamplePerOutput.
AverageAnalyzer(Simulator< D, T > &simulator, System< D, T > &system)
Constructor.
std::ofstream outputFile_
Output file stream.
virtual void setup()
Setup before loop.
double compute() override
Compute and return the max order parameter.
MaxOrderParameter(Simulator< D, T > &simulator, System< D, T > &system)
Constructor.
void outputValue(int step, double value) override
Output a sampled or block average value.
void setup() override
Setup before simulation loop.
Base class for field theoretic PS-FTS simulator.
Definition Simulator.h:93
Base class template for classes that represent a complete system.
Base template for UnitCell<D> classes, D=1, 2 or 3.
Definition UnitCell.h:55
Dbl()
Default constructor.
Definition Dbl.cpp:17
Int()
Default constructor.
Definition Int.cpp:19
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 VecOp.cpp:1182
Code for programs that simulate real-valued periodic fields.
Definition param_pc.dox:2
PSCF package top-level namespace.