1#ifndef RPC_LR_POST_AM_COMPRESSOR_TPP
2#define RPC_LR_POST_AM_COMPRESSOR_TPP
11#include "LrAmCompressor.h"
12#include <rpc/system/System.h>
13#include <pscf/mesh/MeshIterator.h>
28 isIntraCalculated_(false),
30 { setClassName(
"LrAmCompressor"); }
61 const int nMonomer = system().mixture().nMonomer();
62 const int meshSize = system().domain().mesh().size();
63 IntVec<D> const & dimensions = system().domain().mesh().dimensions();
66 for (
int i = 0; i < D; ++i) {
68 kMeshDimensions_[i] = dimensions[i];
70 kMeshDimensions_[i] = dimensions[i]/2 + 1;
76 newBasis_.allocate(meshSize);
77 w0_.allocate(nMonomer);
78 wFieldTmp_.allocate(nMonomer);
79 resid_.allocate(dimensions);
80 residK_.allocate(dimensions);
81 intraCorrelationK_.allocate(kMeshDimensions_);
82 for (
int i = 0; i < nMonomer; ++i) {
83 w0_[i].allocate(dimensions);
84 wFieldTmp_[i].allocate(dimensions);
91 if (!isIntraCalculated_){
92 intra_.computeIntraCorrelations(intraCorrelationK_);
93 isIntraCalculated_ =
true;
97 for (
int i = 0; i < nMonomer; ++i) {
98 for (
int j = 0; j< meshSize; ++j){
99 w0_[i][j] = system().w().rgrid(i)[j];
134 for (
int i = 0; i < n; i++) {
136 if (std::isnan(a[i]) || std::isnan(b[i])) {
138 __FILE__,__LINE__,0);
154 for (
int i = 0; i < n; i++) {
156 if (std::isnan(value)) {
158 __FILE__, __LINE__, 0);
160 if (fabs(value) > max) {
172 LrAmCompressor<D>::updateBasis(
179 const int n = hists[0].capacity();
182 for (
int i = 0; i < n; i++) {
183 newBasis_[i] = hists[0][i] - hists[1][i];
186 basis.append(newBasis_);
197 for (
int i = 0; i < nHist; i++) {
198 for (
int j = 0; j < n; j++) {
200 trial[j] += coeffs[i] * -1 * basis[i][j];
209 double LrAmCompressor<D>::computeLambda(
double r)
225 const int n = fieldTrial.
capacity();
226 const double vMonomer = system().mixture().vMonomer();
230 for (
int i = 0 ; i < n; ++i) {
231 resid_[i] = resTrial[i];
235 system().domain().fft().forwardTransform(resid_, residK_);
241 for (iter.begin(); !iter.atEnd(); ++iter) {
242 factor = 1.0 / (vMonomer * intraCorrelationK_[iter.rank()]);
243 residK_[iter.rank()][0] *= factor;
244 residK_[iter.rank()][1] *= factor;
250 system().domain().fft().inverseTransformUnsafe(residK_, resid_);
253 for (
int i = 0; i < n; i++) {
254 fieldTrial[i] += lambda * resid_[i];
262 bool LrAmCompressor<D>::hasInitialGuess()
263 {
return system().w().hasData(); }
269 int LrAmCompressor<D>::nElements()
270 {
return system().domain().mesh().size(); }
282 const int meshSize = system().domain().mesh().size();
291 for (
int i = 0; i < meshSize; i++){
292 curr[i] = (*currSys)[0][i] - w0_[0][i];
301 void LrAmCompressor<D>::evaluate()
313 const int n = nElements();
314 const int nMonomer = system().mixture().nMonomer();
315 const int meshSize = system().domain().mesh().size();
318 for (
int i = 0 ; i < n; ++i) {
323 for (
int j = 0; j < nMonomer; ++j) {
324 for (
int k = 0; k < meshSize; ++k) {
325 resid[k] += system().c().rgrid(j)[k];
338 const int nMonomer = system().mixture().nMonomer();
339 const int meshSize = system().domain().mesh().size();
343 for (
int i = 0; i < nMonomer; i++){
344 for (
int k = 0; k < meshSize; k++){
345 wFieldTmp_[i][k] = w0_[i][k] + newGuess[k];
350 system().w().setRGrid(wFieldTmp_);
357 void LrAmCompressor<D>::outputToLog()
368 out <<
"LrAmCompressor time contributions:\n";
void readErrorType(std::istream &in)
int solve(bool isContinuation=false)
An IntVec<D, T> is a D-component vector of elements of integer type T.
Iterator over points in a Mesh<D>.
void setDimensions(const IntVec< D > &dimensions)
Set the grid dimensions in all directions.
Exception thrown when not-a-number (NaN) is encountered.
Base class for iterators that impose incompressibility.
void readParameters(std::istream &in)
Read all parameters and initialize.
~LrAmCompressor()
Destructor.
LrAmCompressor(System< D > &system)
Constructor.
void outputTimers(std::ostream &out) const
Return compressor times contributions.
void clearTimers()
Clear all timers (reset accumulated time to zero).
void setup(bool isContinuation)
Initialize just before entry to iterative loop.
int compress()
Compress to obtain partial saddle point w+.
Main class, representing one complete system.
int capacity() const
Return allocated size.
Dynamically allocatable contiguous array template.
Class for storing history of previous values in an array.
File containing preprocessor macros for error handling.
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Real periodic fields, SCFT and PS-FTS (CPU).
PSCF package top-level namespace.
float product(float a, float b)
Product for float Data.