1#ifndef RP_BINARY_STRUCTURE_FACTOR_TPP
2#define RP_BINARY_STRUCTURE_FACTOR_TPP
4#include "BinaryStructureFactor.h"
6#include <rp/fts/simulator/Simulator.h>
7#include <rp/system/System.h>
8#include <rp/solvers/Mixture.h>
9#include <rp/field/Domain.h>
10#include <rp/field/WFields.h>
12#include <pscf/field/FFT.h>
13#include <pscf/field/WaveList.h>
15#include <pscf/backend/HostRecvArray.h>
16#include <pscf/backend/VecOp.h>
17#include <pscf/backend/complex.h>
18#include <pscf/interaction/Interaction.h>
19#include <pscf/mesh/MeshIterator.h>
20#include <pscf/mesh/Mesh.h>
21#include <pscf/math/IntVec.h>
23#include <util/param/ParamComposite.h>
24#include <util/containers/Array.h>
25#include <util/containers/ConstArray.h>
26#include <util/misc/FileMaster.h>
27#include <util/format/Dbl.h>
28#include <util/format/Int.h>
44 template <
int D,
class T>
52 writeWaveData_(false),
62 template <
int D,
class T>
70 writeWaveData_ =
false;
72 isInitialized_ =
true;
78 template <
int D,
class T>
88 template <
int D,
class T>
102 template <
int D,
class T>
103 void BinaryStructureFactor<D,T>::allocate()
108 Mesh<D> const & mesh = system().domain().mesh();
113 if (!wm_.isAllocated()){
117 wm_.allocate(rMeshDimensions);
118 wk_d_.allocate(rMeshDimensions);
119 waveBunchIds_.allocate(nWave_);
120 waveWeights_.allocate(nWave_);
121 if (writeWaveData_) {
123 waveAccumulators_.allocate(nWave_);
128 UTIL_CHECK(waveBunchIds_.capacity() == nWave_);
129 UTIL_CHECK(waveWeights_.capacity() == nWave_);
130 if (writeWaveData_) {
131 UTIL_CHECK(waveAccumulators_.capacity() == nWave_);
138 template <
int D,
class T>
139 void BinaryStructureFactor<D,T>::setupWaveBunches()
141 WaveList<D,T>& waveList = system().waveList();
145 if (!waveList.hasKSq()) {
146 waveList.computeKSq();
151 HostRecvArray<double,T> kSq(waveList.kSq());
152 DArray<bool> const & implicit = waveList.implicitInverse();
157 waveList.sortWaves();
158 nBunch_ = waveList.nBunch();
162 if (bunchAccumulators_.isAllocated()) {
163 int const m = bunchAccumulators_.capacity();
164 UTIL_CHECK(bunchWavenumbers_.capacity() == m);
167 bunchAccumulators_.deallocate();
168 bunchWavenumbers_.deallocate();
169 bunchValues_.deallocate();
172 if (!bunchAccumulators_.isAllocated()) {
175 bunchAccumulators_.allocate(nBunch_);
176 bunchWavenumbers_.allocate(nBunch_);
177 bunchValues_.allocate(nBunch_);
179 int m = bunchAccumulators_.capacity();
181 UTIL_CHECK(bunchWavenumbers_.capacity() == m);
185 for (
int ib = 0; ib < m; ++ib) {
186 bunchAccumulators_[ib].clear();
187 bunchWavenumbers_[ib] = 0.0;
188 bunchValues_[ib] = 0.0;
190 for (
int iw = 0; iw < nWave_; ++iw) {
191 waveBunchIds_[iw] = -1;
192 waveWeights_[iw] = 0.0;
194 if (writeWaveData_) {
195 for (
int iw = 0; iw < nWave_; ++iw) {
196 waveAccumulators_[iw].clear();
207 double wavenumberSq, wavenumber, oldWavenumberSq;
208 double count,
sum, tot;
209 int begin, end, k, iw;
211 for (
int ib = 0; ib < nBunch_; ++ib) {
212 begin = bunches[ib][0];
213 end = bunches[ib][1];
216 iw = sortedIds[begin];
217 wavenumberSq = kSq[iw];
220 UTIL_CHECK(wavenumberSq - oldWavenumberSq > 5.0E-9);
222 oldWavenumberSq = wavenumberSq;
225 wavenumber = std::sqrt(wavenumberSq);
226 bunchWavenumbers_[ib] = wavenumber;
230 for (k = begin; k < end; ++k) {
234 UTIL_CHECK(std::abs(waveWeights_[iw]) < 1.0E-8);
235 waveBunchIds_[iw] = ib;
242 waveWeights_[iw] = count;
244 UTIL_CHECK(std::abs(kSq[iw] - wavenumberSq) < 2.0E-8);
249 for (k = begin; k < end; ++k) {
251 waveWeights_[iw] /=
sum;
252 tot += waveWeights_[iw];
265 template <
int D,
class T>
266 void BinaryStructureFactor<D,T>::computeW()
274 RField<D,T>
const & wa = system().w().rgrid(0);
275 RField<D,T>
const & wb = system().w().rgrid(1);
280 system().domain().fft().forwardTransform(wm_, wk_d_);
286 template <
int D,
class T>
287 void BinaryStructureFactor<D,T>::computeS()
293 int m = bunchAccumulators_.capacity();
295 UTIL_CHECK(bunchWavenumbers_.capacity() >= m);
297 UTIL_CHECK(waveBunchIds_.capacity() == nWave_);
298 UTIL_CHECK(waveWeights_.capacity() == nWave_);
301 for (
int ib = 0; ib < nBunch_; ++ib) {
302 bunchValues_[ib] = 0.0;
306 double const vSystem = system().domain().unitCell().volume();
307 double const vMonomer = system().mixture().vMonomer();
308 double const chi = -simulator().chiEval(0);
309 double a_ = vSystem / (chi * chi * vMonomer * vMonomer);
310 double b_ = 0.5 / (chi * vMonomer);
319 for (
int iw = 0; iw < nWave_; ++iw) {
320 value = a_ *
absSq( wk_h_[iw] );
322 if (writeWaveData_) {
323 waveAccumulators_[iw].sample(value);
325 ib = waveBunchIds_[iw];
326 bunchValues_[ib] += waveWeights_[iw] * value;
333 for (ib = 0; ib < nBunch_; ++ib) {
334 bunchAccumulators_[ib].sample(bunchValues_[ib]);
342 template <
int D,
class T>
345 std::string filename;
350 if (writeWaveData_) {
354 for (
int i = 0; i < nBunch_; ++i) {
355 file <<
Dbl(bunchWavenumbers_[i], 18, 8);
356 file <<
Dbl(bunchAccumulators_[i].average(), 18, 8);
362 if (writeWaveData_) {
372 for (j = 0; j < D; ++j) {
373 file <<
Int(p[j], 5);
375 file <<
Dbl(waveAccumulators_[iw].average(), 18, 8);
An IntVec<D, T> is a D-component vector of elements of integer type T.
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)?
IntVec< D > position() const
Get current position in the grid, as integer vector.
Description of a regular grid of points in a periodic domain.
IntVec< D > dimensions() const
Get an IntVec<D> of the grid dimensions.
int size() const
Get total number of grid points.
Analyzer(Simulator< D, T > &simulator, System< D, T > &system)
Constructor.
void readInterval(std::istream &in)
Optionally read interval from file, with error checking.
bool isAtInterval(long counter) const
Return true iff counter is a multiple of the interval.
void setFileMaster(FileMaster &fileMaster)
Set the FileMaster to use to open files.
Simulator< D, T > & simulator()
Get the parent Simulator by reference.
std::string const & outputFileName() const
Get the outputFileName string.
FileMaster & fileMaster()
Get the FileMaster by reference.
System< D, T > & system()
Get the parent System by reference.
void readOutputFileName(std::istream &in)
Read outputFileName from file.
void readParameters(std::istream &in) override
Read parameters from file.
void output() override
Output results to predefined output file.
BinaryStructureFactor(Simulator< D, T > &simulator, System< D, T > &system)
Constructor.
void setup() override
Setup before the main loop.
void sample(long iStep) override
Compute structure factors and add to accumulators.
Base class for field theoretic PS-FTS simulator.
Base class template for classes that represent a complete system.
FileMaster const & fileMaster() const
Get the FileMaster (const).
int capacity() const
Return allocated size.
Dynamically allocatable contiguous array template.
Dbl()
Default constructor.
void openOutputFile(const std::string &filename, std::ofstream &out, std::ios_base::openmode mode=std::ios_base::out) const
Open an output file.
An automatically growable array, analogous to a std::vector.
int size() const
Return the logical size of this array (number of elements).
Int()
Default constructor.
void setClassName(const char *className)
Set class name string.
ScalarParam< Type > & readOptional(std::istream &in, const char *label, Type &value)
Add and read a new optional ScalarParam < Type > object.
File containing preprocessor macros for error handling.
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
double absSq(fftw_complex const &a)
Return square of absolute magnitude of an fftw_complex number.
double sum(Array< double > const &in)
Compute sum of array elements (real).
void mulEqS(Array< double > &a, double b)
Vector-scalar in-place multiplication, a[i] *= b (real).
void subVV(Array< double > &a, Array< double > const &b, Array< double > const &c)
Vector-vector subtraction, a[i] = b[i] - c[i] (real).
Code for programs that simulate real-valued periodic fields.
PSCF package top-level namespace.