1#ifndef PSCF_AM_ITERATOR_TMPL_H
2#define PSCF_AM_ITERATOR_TMPL_H
11#include <util/containers/DArray.h>
12#include <util/containers/DMatrix.h>
13#include <util/containers/RingBuffer.h>
14#include <util/misc/Timer.h>
15#include <util/accumulators/Average.h>
43 template <
typename Iterator,
typename T>
71 int solve(
bool isContinuation =
false);
94 using Iterator::setClassName;
155 virtual double norm(T
const & hist);
171 virtual void clear();
183 virtual void setup(
bool isContinuation);
194 virtual double computeError(T& residTrial, T& fieldTrial,
340 bool hasAmTest_{
false};
342 double projectionError_{0};
343 double correctionError_{0};
344 double projectionRatio_{0};
345 double correctionRatio_{0};
354 void computeResidCoeff();
398 virtual void setEqual(T& a, T
const & b) = 0;
406 virtual double dotProduct(T
const & a, T
const & b) = 0;
413 virtual double maxAbs(T
const & hist) = 0;
437 void addHistories(T& trial,
450 void addPredictedError(T& fieldTrial, T
const & resTrial,
458 virtual bool hasInitialGuess() = 0;
467 virtual int nElements() = 0;
474 virtual void getCurrent(T& curr) = 0;
483 virtual void evaluate() = 0;
490 virtual void getResidual(T& resid) = 0;
497 virtual void update(T& newGuess) = 0;
502 virtual void outputToLog() = 0;
509 template <
typename Iterator,
typename T>
511 {
return resHists_[0]; }
516 template <
typename Iterator,
typename T>
518 {
return fieldHists_[0]; }
523 template <
typename Iterator,
typename T>
530 template <
typename Iterator,
typename T>
532 {
return isAllocatedAM_; }
537 template <
typename Iterator,
typename T>
539 {
return errorType_; }
544 template <
typename Iterator,
typename T>
546 {
return totalItr_; }
551 template <
typename Iterator,
typename T>
553 {
return timerMDE_.time(); }
558 template <
typename Iterator,
typename T>
560 {
return timerAM_.time(); }
565 template <
typename Iterator,
typename T>
567 {
return timerResid_.time(); }
572 template <
typename Iterator,
typename T>
574 {
return timerError_.time(); }
579 template <
typename Iterator,
typename T>
581 {
return timerCoeff_.time(); }
586 template <
typename Iterator,
typename T>
588 {
return timerOmega_.time(); }
593 template <
typename Iterator,
typename T>
595 {
return timerTotal_.time(); }
598#include "AmIteratorTmpl.tpp"
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.
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.
PSCF package top-level namespace.
Utility classes for scientific computation.