PSCF v1.3
rpg/fts/analyzer/MaxOrderParameter.tpp
1#ifndef RPG_MAX_ORDER_PARAMETER_TPP
2#define RPG_MAX_ORDER_PARAMETER_TPP
3
4#include "MaxOrderParameter.h"
5
6#include <rpg/fts/simulator/Simulator.h>
7#include <rpg/system/System.h>
8
9#include <prdc/cuda/FFT.h>
10#include <prdc/cuda/RField.h>
11#include <prdc/cuda/resources.h>
12#include <prdc/crystal/shiftToMinimum.h>
13
14#include <pscf/inter/Interaction.h>
15#include <pscf/mesh/MeshIterator.h>
16#include <pscf/math/IntVec.h>
17
18#include <util/format/Int.h>
19#include <util/format/Dbl.h>
20#include <util/global.h>
21
22namespace Pscf {
23namespace Rpg {
24
25 using namespace Util;
26 using namespace Pscf::Prdc;
27
28 /*
29 * Constructor.
30 */
31 template <int D>
35 kSize_(1),
36 isInitialized_(false)
37 { setClassName("MaxOrderParameter"); }
38
39 /*
40 * Destructor.
41 */
42 template <int D>
45
46 /*
47 * MaxOrderParameter setup
48 */
49 template <int D>
51 {
52 // Local copies of data
53 const int nMonomer = system().mixture().nMonomer();
54 IntVec<D> const & dimensions = system().domain().mesh().dimensions();
55
56 // Preconditions
57 UTIL_CHECK(system().w().hasData());
58 UTIL_CHECK(nMonomer == 2); // Require system has 2 monomer types
59
61 if (!simulator().hasWc()){
62 simulator().computeWc();
63 }
64
65 // Compute Fourier space dimension kMeshDimensions_ and kSize_
66 FFT<D>::computeKMesh(dimensions, kMeshDimensions_, kSize_);
67
68 // Allocate variables
69 if (!isInitialized_){
70 wc0_.allocate(dimensions);
71 wK_.allocate(dimensions);
72 }
73
74 isInitialized_ = true;
75 }
76
77 template <int D>
79 {
80 UTIL_CHECK(system().w().hasData());
81
82 if (!simulator().hasWc()){
83 simulator().computeWc();
84 }
85
86 const int meshSize = system().domain().mesh().size();
87 RField<D> psi;
88 psi.allocate(kMeshDimensions_);
89
90 // Conver W_(r) to fourier mode W_(k)
91 VecOp::eqV(wc0_, simulator().wc(0));
92 system().domain().fft().forwardTransform(wc0_, wK_);
93
94 // Comput W_(k)^2
95 VecOp::sqNormV(psi, wK_);
96
97 HostDArray<cudaReal> psiHost(kSize_);
98
99 psiHost = psi;
100
101 // Obtain max[W_(k)^2]
102 maxOrderParameter_ = psiHost[1];
103 int maxIndex = 1;
104 for (int i = 2; i < kSize_; ++i){
105 if (psiHost[i] > maxOrderParameter_){
106 maxOrderParameter_ = psiHost[i];
107 maxIndex = i;
108 }
109 }
110
111 MeshIterator<D> itr;
112 itr.setDimensions(kMeshDimensions_);
113
114 DArray<IntVec<D>> GminList;
115 GminList.allocate(kSize_);
116 IntVec<D> G;
117 IntVec<D> Gmin;
118 UnitCell<D> const & unitCell = system().domain().unitCell();
119 IntVec<D> const & dimensions = system().domain().mesh().dimensions();
120 for (itr.begin(); !itr.atEnd(); ++itr) {
121 G = itr.position();
122 Gmin = shiftToMinimum(G, dimensions, unitCell);
123 GminList[itr.rank()] = Gmin;
124 }
125
126 GminStar_ = GminList[maxIndex];
127
128 return maxOrderParameter_;
129 }
130
131 template <int D>
132 void MaxOrderParameter<D>::outputValue(int step, double value)
133 {
134 if (simulator().hasRamp() && nSamplePerOutput() == 1) {
135 double chi= system().interaction().chi(0,1);
136
137 UTIL_CHECK(outputFile_.is_open());
138 outputFile_ << Int(step);
139 outputFile_ << Dbl(chi);
140 outputFile_ << " ( ";
141 for (int i = 0; i < D; i++){
142 outputFile_ << Int(GminStar_[i],3) << " ";
143 }
144 outputFile_ << " ) ";
145 outputFile_ << Dbl(value);
146 outputFile_ << "\n";
147 } else {
148 // AverageAnalyzer<D>::outputValue(step, value);
149 outputFile_ << Int(step);
150 outputFile_ << " ( ";
151 for (int i = 0; i < D; i++){
152 outputFile_ << Int(GminStar_[i],3) << " ";
153 }
154 outputFile_ << " ) ";
155 outputFile_ << Dbl(value);
156 outputFile_ << "\n";
157 }
158 }
159
160}
161}
162#endif
Template for dynamic array stored in host CPU memory.
Definition HostDArray.h:43
An IntVec<D, T> is a D-component vector of elements of integer type T.
Definition IntVec.h:27
Iterator over points in a Mesh<D>.
int rank() const
Get the rank of current element.
void begin()
Set iterator to the first point in the mesh.
bool atEnd() const
Is this the end (i.e., one past the last point)?
void setDimensions(const IntVec< D > &dimensions)
Set the grid dimensions in all directions.
IntVec< D > position() const
Get current position in the grid, as integer vector.
static void computeKMesh(IntVec< D > const &rMeshDimensions, IntVec< D > &kMeshDimensions, int &kSize)
Compute dimensions and size of k-space mesh for DFT of real data.
Definition cpu/FFT.tpp:262
Field of real double precision values on an FFT mesh.
Definition cpu/RField.h:29
void allocate(IntVec< D > const &meshDimensions)
Allocate the underlying C array for an FFT grid.
Base template for UnitCell<D> classes, D=1, 2 or 3.
Definition UnitCell.h:56
AverageAnalyzer(Simulator< D > &simulator, System< D > &system)
Constructor.
virtual void setup()
Setup before loop.
int nSamplePerOutput() const
Get value of nSamplePerOutput.
Simulator< D > & simulator()
Return reference to parent simulator.
System< D > & system()
Return reference to parent system.
virtual void outputValue(int step, double value)
Output a sampled or block average value.
void setClassName(const char *className)
Set class name string.
virtual double compute()
Compute and return the max order parameter.
void setup()
Setup before simulation loop.
MaxOrderParameter(Simulator< D > &simulator, System< D > &system)
Constructor.
Field theoretic simulator (base class).
Main class, representing one complete system.
Dynamically allocatable contiguous array template.
Definition DArray.h:32
void allocate(int capacity)
Allocate the underlying C array.
Definition DArray.h:199
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
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 eqV(DeviceArray< cudaReal > &a, DeviceArray< cudaReal > const &b, const int beginIdA, const int beginIdB, const int n)
Vector assignment, a[i] = b[i], kernel wrapper (cudaReal).
Definition VecOp.cu:1039
void sqNormV(DeviceArray< cudaReal > &a, DeviceArray< cudaComplex > const &b)
Squared norm of complex number, a[i] = norm(b[i])^2, kernel wrapper.
Definition VecOpMisc.cu:585
Periodic fields and crystallography.
Definition CField.cpp:11
SCFT and PS-FTS with real periodic fields (GPU)
PSCF package top-level namespace.
Definition param_pc.dox:1