PSCF v1.1
AmIteratorTmpl.h
1#ifndef PSCF_AM_ITERATOR_TMPL_H
2#define PSCF_AM_ITERATOR_TMPL_H
3
4/*
5* PSCF - Polymer Self-Consistent Field Theory
6*
7* Copyright 2016 - 2022, The Regents of the University of Minnesota
8* Distributed under the terms of the GNU General Public License.
9*/
10
11#include <util/containers/DArray.h> // member template
12#include <util/containers/DMatrix.h> // member template
13#include <util/containers/RingBuffer.h> // member template
14
15namespace Pscf {
16
17 using namespace Util;
18
38 template <typename Iterator, typename T>
39 class AmIteratorTmpl : virtual public Iterator
40 {
41 public:
42
47
52
58 void readParameters(std::istream& in);
59
66 int solve(bool isContinuation = false);
67
68 protected:
69
71 std::string errorType_;
72
73 // Members of parent classes with non-dependent names
74 using Iterator::setClassName;
77
87 void setMaxItr(int maxItr);
88
98 void setMaxHist(int maxHist);
99
109 void setErrorType(std::string errorType);
110
116 void readErrorType(std::istream& in);
117
125 virtual bool isValidErrorType();
126
135 virtual double norm(T const & hist);
136
143 void allocateAM();
144
151 virtual void clear();
152
163 virtual void setup(bool isContinuation);
164
171 virtual double computeError(int verbose);
172
176 T const & residual() const;
177
181 T const & field() const;
182
186 int verbose() const;
187
191 bool isAllocatedAM() const;
192
193 private:
194
195 // Private member variables
196
198 double epsilon_;
199
201 double lambda_;
202
204 int maxItr_;
205
207 int maxHist_;
208
210 int nBasis_;
211
213 int itr_;
214
216 int nElem_;
217
219 int verbose_;
220
222 bool outputTime_;
223
225 bool isAllocatedAM_;
226
228 RingBuffer<T> fieldHists_;
229
231 RingBuffer<T> fieldBasis_;
232
234 RingBuffer<T> resHists_;
235
237 RingBuffer<T> resBasis_;
238
241
243 DArray<double> coeffs_;
244
247
249 T fieldTrial_;
250
252 T resTrial_;
253
255 T temp_;
256
257 // --- Non-virtual private functions (implemented here) ---- //
258
262 void computeResidCoeff();
263
273 void updateGuess();
274
275 // --- Private virtual functions with default implementations --- //
276
284 virtual void updateU(DMatrix<double> & U,
285 RingBuffer<T> const & resBasis, int nHist);
286
295 virtual void updateV(DArray<double> & v, T const & resCurrent,
296 RingBuffer<T> const & resBasis, int nHist);
297
298 // --- Pure virtual methods for doing AM iterator math --- //
299
306 virtual void setEqual(T& a, T const & b) = 0;
307
314 virtual double dotProduct(T const & a, T const & b) = 0;
315
321 virtual double maxAbs(T const & hist) = 0;
322
332 virtual
333 void updateBasis(RingBuffer<T> & basis,
334 RingBuffer<T> const & hists) = 0;
335
344 virtual
345 void addHistories(T& trial,
346 RingBuffer<T> const & basis,
347 DArray<double> coeffs,
348 int nHist) = 0;
349
357 virtual
358 void addPredictedError(T& fieldTrial, T const & resTrial,
359 double lambda) = 0;
360
361 // -- Pure virtual functions to exchange data with parent system -- //
362
366 virtual bool hasInitialGuess() = 0;
367
375 virtual int nElements() = 0;
376
382 virtual void getCurrent(T& curr) = 0;
383
391 virtual void evaluate() = 0;
392
398 virtual void getResidual(T& resid) = 0;
399
405 virtual void update(T& newGuess) = 0;
406
410 virtual void outputToLog() = 0;
411
412 };
413
414 /*
415 * Return the current residual vector by const reference.
416 */
417 template <typename Iterator, typename T>
419 { return resHists_[0]; }
420
421 /*
422 * Return the current field/state vector by const reference.
423 */
424 template <typename Iterator, typename T>
426 { return fieldHists_[0]; }
427
428 /*
429 * Integer level for verbosity of the log output (0-2).
430 */
431 template <typename Iterator, typename T>
433 { return verbose_; }
434
435 /*
436 * Has memory required by AM algorithm been allocated?
437 */
438 template <typename Iterator, typename T>
440 { return isAllocatedAM_; }
441
442}
443#include "AmIteratorTmpl.tpp"
444#endif
Template for Anderson mixing iterator algorithm.
virtual void setup(bool isContinuation)
Initialize just before entry to iterative loop.
void setMaxHist(int maxHist)
Set value of maxHist (number of retained previous states)
virtual double computeError(int verbose)
Compute and return error used to test for convergence.
T const & residual() const
Return the current residual vector by const reference.
void allocateAM()
Allocate memory required by AM algorithm, if necessary.
bool isAllocatedAM() const
Have data structures required by the AM algorithm been allocated?
virtual void clear()
Clear information about history.
void setErrorType(std::string errorType)
Set and validate value of errorType string.
int verbose() const
Verbosity level, allowed values 0, 1, or 2.
~AmIteratorTmpl()
Destructor.
void readErrorType(std::istream &in)
Read and validate the optional errorType string parameter.
void setMaxItr(int maxItr)
Set value of maxItr.
AmIteratorTmpl()
Constructor.
virtual bool isValidErrorType()
Checks if a string is a valid error type.
std::string errorType_
Type of error criterion used to test convergence.
void readParameters(std::istream &in)
Read all parameters and initialize.
int solve(bool isContinuation=false)
Iterate to a solution.
T const & field() const
Return the current field or state vector by const reference.
virtual double norm(T const &hist)
Find the L2 norm of a vector.
Dynamically allocatable contiguous array template.
Definition: DArray.h:32
Dynamically allocated Matrix.
Definition: DMatrix.h:25
ScalarParam< Type > & read(std::istream &in, const char *label, Type &value)
Add and read a new required ScalarParam < Type > object.
ScalarParam< Type > & readOptional(std::istream &in, const char *label, Type &value)
Add and read a new optional ScalarParam < Type > object.
Class for storing history of previous values in an array.
Definition: RingBuffer.h:27
C++ namespace for polymer self-consistent field theory (PSCF).
Utility classes for scientific computation.
Definition: accumulators.mod:1