1#ifndef RP_LR_COMPRESSOR_TPP
2#define RP_LR_COMPRESSOR_TPP
11#include "LrCompressor.h"
13#include <rp/system/System.h>
14#include <rp/solvers/Mixture.h>
15#include <rp/field/Domain.h>
16#include <rp/field/WFields.h>
17#include <rp/field/CFields.h>
19#include <pscf/field/FFT.h>
20#include <pscf/crystal/shiftToMinimum.h>
22#include <pscf/backend/Reduce.h>
23#include <pscf/backend/VecOp.h>
24#include <pscf/mesh/MeshIterator.h>
25#include <pscf/iterator/NanException.h>
27#include <util/format/Dbl.h>
38 template <
int D,
class T>
49 hasIntraCorrelationK_(false)
55 template <
int D,
class T>
73 template <
int D,
class T>
74 void LrCompressor<D,T>::setup()
76 IntVec<D> const & dimensions = system().domain().mesh().dimensions();
82 resid_.allocate(dimensions);
83 residK_.allocate(dimensions);
84 const int nMonomer = system().mixture().nMonomer();
85 wFieldTmp_.allocate(nMonomer);
86 intraCorrelationK_.allocate(kMeshDimensions_);
87 for (
int i = 0; i < nMonomer; ++i) {
88 wFieldTmp_[i].allocate(dimensions);
98 template <
int D,
class T>
115 for (itr_ = 0; itr_ < maxItr_; ++itr_) {
118 Log::file() <<
"------------------------------- \n";
131 error = computeError(verbose_);
133 Log::file() <<
", error = NaN" << std::endl;
137 Log::file() <<
", error = " <<
Dbl(error, 15) << std::endl;
141 if (error < epsilon_) {
147 Log::file() <<
"-------------------------------\n";
178 Log::file() <<
"Iterator failed to converge.\n";
185 template <
int D,
class T>
186 void LrCompressor<D,T>::computeResidual()
192 const int nMonomer = system().mixture().nMonomer();
193 for (
int i = 0; i < nMonomer; ++i) {
201 template <
int D,
class T>
202 void LrCompressor<D,T>::updateWFields()
209 if (!hasIntraCorrelationK_){
210 intra_.computeOmegaTotal(intraCorrelationK_);
211 hasIntraCorrelationK_ =
true;
214 const int nMonomer = system().mixture().nMonomer();
215 const double vMonomer = system().mixture().vMonomer();
218 system().domain().fft().forwardTransform(resid_, residK_);
224 system().domain().fft().inverseTransformUnsafe(residK_, resid_);
227 for (
int i = 0; i < nMonomer; i++) {
228 VecOp::addVV(wFieldTmp_[i], system().w().rgrid(i), resid_);
232 system().w().setRGrid(wFieldTmp_);
238 template <
int D,
class T>
239 void LrCompressor<D,T>::outputToLog()
245 template <
int D,
class T>
247 { hasIntraCorrelationK_ =
false; }
252 template <
int D,
class T>
256 double total = timerTotal_.time();
258 out <<
"LrCompressor time contributions:\n";
260 out <<
"Total" << std::setw(22)<<
"Per Iteration"
261 << std::setw(9) <<
"Fraction" <<
"\n";
262 out <<
"MDE solution: "
263 <<
Dbl(timerMDE_.time(), 9, 3) <<
" s, "
264 <<
Dbl(timerMDE_.time()/totalItr_, 9, 3) <<
" s, "
265 <<
Dbl(timerMDE_.time()/total, 9, 3) <<
"\n";
272 template <
int D,
class T>
284 template <
int D,
class T>
285 double LrCompressor<D,T>::computeError(
int verbose)
291 if (std::isnan(normSq)) {
299 if (errorType_ ==
"max") {
301 }
else if (errorType_ ==
"norm") {
302 error = sqrt(normSq);
303 }
else if (errorType_ ==
"rms") {
304 int meshSize = system().domain().mesh().size();
305 error = sqrt(normSq/
double(meshSize));
307 UTIL_THROW(
"Invalid iterator error type in parameter file.");
312 int meshSize = system().domain().mesh().size();
315 double normRes = sqrt(normSq);
316 double rmsRes = sqrt(normSq/
double(meshSize));
318 if (errorType_ ==
"max") {
320 }
else if (errorType_ ==
"norm") {
322 }
else if (errorType_ ==
"rms") {
325 UTIL_THROW(
"Invalid iterator error type in parameter file.");
329 Log::file() <<
"Max Residual = " <<
Dbl(maxRes,15) <<
"\n";
330 Log::file() <<
"Residual Norm = " <<
Dbl(normRes,15) <<
"\n";
331 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.
NanException(const char *function, const char *file, int line, int echo=1)
Constructor.
Compressor()
Default constructor.
System< D, T > const & system() const
Return parent system by const reference.
int mdeCounter_
Count how many times MDE has been solved.
int compress() override
Iterate pressure field to obtain partial saddle point.
void outputTimers(std::ostream &out) const override
Return compressor times contributions.
void readParameters(std::istream &in) override
Read all parameters and initialize.
void clearCache() override
Mark the stored intramolecular correlation function as outdated.
void clearTimers() override
Clear all timers.
LrCompressor(System< D, T > &system)
Constructor.
Base class template for classes that represent a complete system.
Dbl()
Default constructor.
Int()
Default constructor.
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).
Code for programs that simulate real-valued periodic fields.
PSCF package top-level namespace.