1#ifndef RP_LR_COMPRESSOR_TPP
2#define RP_LR_COMPRESSOR_TPP
11#include "LrCompressor.h"
12#include <prdc/crystal/shiftToMinimum.h>
13#include <pscf/mesh/MeshIterator.h>
14#include <pscf/iterator/NanException.h>
15#include <util/format/Dbl.h>
26 template <
int D,
class T>
28 : CompressorT(system),
37 isIntraCalculated_(false)
43 template <
int D,
class T>
61 template <
int D,
class T>
64 IntVec<D> const & dimensions = system().domain().mesh().dimensions();
66 FFTT::computeKMesh(dimensions, kMeshDimensions_, kSize);
70 resid_.allocate(dimensions);
71 residK_.allocate(dimensions);
72 const int nMonomer =
system().mixture().nMonomer();
73 wFieldTmp_.allocate(nMonomer);
74 intraCorrelationK_.allocate(kMeshDimensions_);
75 for (
int i = 0; i < nMonomer; ++i) {
76 wFieldTmp_[i].allocate(dimensions);
82 if (!isIntraCalculated_){
83 intra_.computeOmegaTotal(intraCorrelationK_);
84 isIntraCalculated_ =
true;
91 template <
int D,
class T>
104 ++CompressorT::mdeCounter_;
108 for (itr_ = 0; itr_ < maxItr_; ++itr_) {
111 Log::file() <<
"------------------------------- \n";
122 error = computeError(verbose_);
124 Log::file() <<
", error = NaN" << std::endl;
128 Log::file() <<
", error = " <<
Dbl(error, 15) << std::endl;
132 if (error < epsilon_) {
138 Log::file() <<
"-------------------------------\n";
158 ++CompressorT::mdeCounter_;
167 Log::file() <<
"Iterator failed to converge.\n";
175 template <
int D,
class T>
176 void LrCompressor<D,T>::computeResidual()
182 const int nMonomer = system().mixture().nMonomer();
183 for (
int i = 0; i < nMonomer; ++i) {
191 template <
int D,
class T>
192 void LrCompressor<D,T>::updateWFields()
194 const int nMonomer = system().mixture().nMonomer();
195 const double vMonomer = system().mixture().vMonomer();
198 system().domain().fft().forwardTransform(resid_, residK_);
204 system().domain().fft().inverseTransformUnsafe(residK_, resid_);
207 for (
int i = 0; i < nMonomer; i++) {
208 VecOp::addVV(wFieldTmp_[i], system().w().rgrid(i), resid_);
212 system().w().setRGrid(wFieldTmp_);
218 template <
int D,
class T>
219 void LrCompressor<D,T>::outputToLog()
225 template <
int D,
class T>
229 double total = timerTotal_.time();
231 out <<
"LrCompressor time contributions:\n";
233 out <<
"Total" << std::setw(22)<<
"Per Iteration"
234 << std::setw(9) <<
"Fraction" <<
"\n";
235 out <<
"MDE solution: "
236 <<
Dbl(timerMDE_.time(), 9, 3) <<
" s, "
237 <<
Dbl(timerMDE_.time()/totalItr_, 9, 3) <<
" s, "
238 <<
Dbl(timerMDE_.time()/total, 9, 3) <<
"\n";
245 template <
int D,
class T>
250 CompressorT::mdeCounter_ = 0;
257 template <
int D,
class T>
258 double LrCompressor<D,T>::computeError(
int verbose)
260 const int meshSize = system().domain().mesh().size();
268 double rmsRes = normRes/sqrt(meshSize);
269 if (errorType_ ==
"max") {
271 }
else if (errorType_ ==
"norm") {
273 }
else if (errorType_ ==
"rms") {
276 UTIL_THROW(
"Invalid iterator error type in parameter file.");
281 Log::file() <<
"Max Residual = " <<
Dbl(maxRes,15) <<
"\n";
282 Log::file() <<
"Residual Norm = " <<
Dbl(normRes,15) <<
"\n";
283 Log::file() <<
"RMS Residual = " <<
Dbl(rmsRes,15) <<
"\n";
An IntVec<D, T> is a D-component vector of elements of integer type T.
Exception thrown when not-a-number (NaN) is encountered.
int compress()
Iterate pressure field to obtain partial saddle point.
void outputTimers(std::ostream &out) const
Return compressor times contributions.
LrCompressor(typename T::System &system)
Constructor.
void setup()
Initialize just before entry to iterative loop.
void readParameters(std::istream &in)
Read all parameters and initialize.
void clearTimers()
Clear all timers.
System< D > const & system() const
Return parent system by const reference.
Wrapper for a double precision number, for formatted ostream output.
Wrapper for an int, for formatted ostream output.
static std::ostream & file()
Get log ostream by reference.
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.
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.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
double maxAbs(Array< double > const &in)
Get maximum absolute magnitude of array elements .
double sumSq(Array< double > const &in)
Compute sum of of squares of array elements (real).
void addEqV(Array< double > &a, Array< double > const &b)
Vector-vector in-place addition, a[i] += b[i] (real).
void eqS(Array< double > &a, double b)
Vector assignment, a[i] = b (real).
void addVV(Array< double > &a, Array< double > const &b, Array< double > const &c)
Vector-vector addition, a[i] = b[i] + c[i] (real)
void divEqVc(Array< fftw_complex > &a, Array< double > const &b, double c)
Vector division in-place w/ coeff., a[i] /= (b[i] * c).
Class templates for real-valued periodic fields.
PSCF package top-level namespace.