PSCF v1.2
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#include <util/misc/Timer.h> // member template
15#include <util/accumulators/Average.h> // member template
16
17// Uncomment to test details of Anderson-Mixing algorithm performance
18//#define PSCF_AM_TEST
19
20namespace Pscf {
21
22 using namespace Util;
23
43 template <typename Iterator, typename T>
44 class AmIteratorTmpl : virtual public Iterator
45 {
46 public:
47
52
57
63 void readParameters(std::istream& in);
64
71 int solve(bool isContinuation = false);
72
76 void outputTimers(std::ostream& out);
77
81 void clearTimers();
82
86 std::string errorType() const;
87
88 protected:
89
91 std::string errorType_;
92
93 // Members of parent classes with non-dependent names
94 using Iterator::setClassName;
97
107 void setMaxItr(int maxItr);
108
118 void setMaxHist(int maxHist);
119
129 void setErrorType(std::string errorType);
130
136 void readErrorType(std::istream& in);
137
145 virtual bool isValidErrorType();
146
155 virtual double norm(T const & hist);
156
163 void allocateAM();
164
171 virtual void clear();
172
183 virtual void setup(bool isContinuation);
184
194 virtual double computeError(T& residTrial, T& fieldTrial,
195 std::string errorType,
196 int verbose);
197
204 double computeError(int verbose);
205
212 virtual double computeLambda(double r);
213
217 T const & residual() const;
218
222 T const & field() const;
223
227 int verbose() const;
228
232 int totalItr();
233
235 double timerTotal();
236
238 double timerMDE();
239
241 double timerAM();
242
244 double timerResid();
245
247 double timerError();
248
250 double timerCoeff();
251
253 double timerOmega();
254
258 bool isAllocatedAM() const;
259
260 private:
261
262 // Private member variables
263
265 double error_;
266
268 double epsilon_;
269
271 double lambda_;
272
274 double r_;
275
277 int maxItr_;
278
280 int maxHist_;
281
283 int nBasis_;
284
286 int itr_;
287
289 int totalItr_;
290
292 int nElem_;
293
295 int verbose_;
296
298 bool isAllocatedAM_;
299
301 RingBuffer<T> fieldHists_;
302
304 RingBuffer<T> fieldBasis_;
305
307 RingBuffer<T> resHists_;
308
310 RingBuffer<T> resBasis_;
311
314
316 DArray<double> coeffs_;
317
320
322 T fieldTrial_;
323
325 T resTrial_;
326
328 T temp_;
329
330 // Timers for analyzing performance
331 Timer timerMDE_;
332 Timer timerAM_;
333 Timer timerResid_;
334 Timer timerError_;
335 Timer timerCoeff_;
336 Timer timerOmega_;
337 Timer timerTotal_;
338
339 #ifdef PSCF_AM_TEST
340 bool hasAmTest_{false};
341 double preError_{0};
342 double projectionError_{0};
343 double correctionError_{0};
344 double projectionRatio_{0};
345 double correctionRatio_{0};
346 int testCounter{0};
347 #endif
348
349 // --- Non-virtual private functions (implemented here) ---- //
350
354 void computeResidCoeff();
355
365 void updateGuess();
366
367 // --- Private virtual functions with default implementations --- //
368
376 virtual void updateU(DMatrix<double> & U,
377 RingBuffer<T> const & resBasis, int nHist);
378
387 virtual void updateV(DArray<double> & v, T const & resCurrent,
388 RingBuffer<T> const & resBasis, int nHist);
389
390 // --- Pure virtual methods for doing AM iterator math --- //
391
398 virtual void setEqual(T& a, T const & b) = 0;
399
406 virtual double dotProduct(T const & a, T const & b) = 0;
407
413 virtual double maxAbs(T const & hist) = 0;
414
424 virtual
425 void updateBasis(RingBuffer<T> & basis,
426 RingBuffer<T> const & hists) = 0;
427
436 virtual
437 void addHistories(T& trial,
438 RingBuffer<T> const & basis,
439 DArray<double> coeffs,
440 int nHist) = 0;
441
449 virtual
450 void addPredictedError(T& fieldTrial, T const & resTrial,
451 double lambda) = 0;
452
453 // -- Pure virtual functions to exchange data with parent system -- //
454
458 virtual bool hasInitialGuess() = 0;
459
467 virtual int nElements() = 0;
468
474 virtual void getCurrent(T& curr) = 0;
475
483 virtual void evaluate() = 0;
484
490 virtual void getResidual(T& resid) = 0;
491
497 virtual void update(T& newGuess) = 0;
498
502 virtual void outputToLog() = 0;
503
504 };
505
506 /*
507 * Return the current residual vector by const reference.
508 */
509 template <typename Iterator, typename T>
511 { return resHists_[0]; }
512
513 /*
514 * Return the current field/state vector by const reference.
515 */
516 template <typename Iterator, typename T>
518 { return fieldHists_[0]; }
519
520 /*
521 * Integer level for verbosity of the log output (0-2).
522 */
523 template <typename Iterator, typename T>
525 { return verbose_; }
526
527 /*
528 * Has memory required by AM algorithm been allocated?
529 */
530 template <typename Iterator, typename T>
532 { return isAllocatedAM_; }
533
534 /*
535 * Return error type
536 */
537 template <typename Iterator, typename T>
539 { return errorType_; }
540
541 /*
542 * Return total iteration counter
543 */
544 template <typename Iterator, typename T>
546 { return totalItr_; }
547
548 /*
549 * Return computing MDE time cost
550 */
551 template <typename Iterator, typename T>
553 { return timerMDE_.time(); }
554
555 /*
556 * Return computing AM time cost
557 */
558 template <typename Iterator, typename T>
560 { return timerAM_.time(); }
561
562 /*
563 * Return computing Resid time cost
564 */
565 template <typename Iterator, typename T>
567 { return timerResid_.time(); }
568
569 /*
570 * Return computing Error time cost
571 */
572 template <typename Iterator, typename T>
574 { return timerError_.time(); }
575
576 /*
577 * Return computing Coeff time cost
578 */
579 template <typename Iterator, typename T>
581 { return timerCoeff_.time(); }
582
583 /*
584 * Return computing Omega time cost
585 */
586 template <typename Iterator, typename T>
588 { return timerOmega_.time(); }
589
590 /*
591 * Return total time cost
592 */
593 template <typename Iterator, typename T>
595 { return timerTotal_.time(); }
596
597}
598#include "AmIteratorTmpl.tpp"
599#endif
Template for Anderson mixing iterator algorithm.
virtual void setup(bool isContinuation)
Initialize just before entry to iterative loop.
double timerError()
Get time evaluating scalar error.
void setMaxHist(int maxHist)
Set value of maxHist (number of retained previous states)
virtual double computeLambda(double r)
Compute mixing parameter for correction step of Anderson mixing.
virtual double computeError(T &residTrial, T &fieldTrial, std::string errorType, int verbose)
Compute and return error used to test for convergence.
T const & residual() const
Return the current residual vector by const reference.
double timerTotal()
Get total time.
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.
double timerOmega()
Get time updating w fields.
void setErrorType(std::string errorType)
Set and validate value of errorType string.
std::string errorType() const
Obtain error type.
void clearTimers()
Clear timers.
int totalItr()
Return the total number of iterations needed to converge.
double timerAM()
Get total time for AM algorithm, excluding MDE solution.
int verbose() const
Verbosity level, allowed values 0, 1, or 2.
~AmIteratorTmpl()
Destructor.
double timerCoeff()
Get time evaluating Anderson mixing coefficients.
void readErrorType(std::istream &in)
Read and validate the optional errorType string parameter.
double timerMDE()
Get time solving modified diffusion equation (MDE).
void setMaxItr(int maxItr)
Set value of maxItr.
double timerResid()
Get time computing residual.
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.
void outputTimers(std::ostream &out)
Log output timing results.
virtual double norm(T const &hist)
Find the L2 norm of a vector.
Dynamically allocatable contiguous array template.
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
Wall clock timer.
Definition Timer.h:35
PSCF package top-level namespace.
Definition param_pc.dox:1
Utility classes for scientific computation.