PSCF v1.3.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
6*
7* Copyright 2015 - 2025, 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/accumulators/Average.h> // member template
15#include <util/misc/Timer.h> // member
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
46 template <typename Iterator, typename T>
47 class AmIteratorTmpl : virtual public Iterator
48 {
49 public:
50
55
60
66 virtual void readParameters(std::istream& in);
67
74 int solve(bool isContinuation = false);
75
81 void outputTimers(std::ostream& out) const;
82
87
88 protected:
89
93 double epsilon_;
94
99
104
109
113 std::string errorType_;
114
115 // Parameter initialization
116
122 void readErrorType(std::istream& in);
123
131 virtual bool isValidErrorType();
132
143 void readMixingParameters(std::istream& in,
144 bool useLambdaRamp = true);
145
146 // Protected AM mixing operations
147
155
159 bool isAllocatedAM() const;
160
172 virtual void setup(bool isContinuation);
173
180 virtual void clear();
181
191 virtual
192 double computeError(T& residTrial, T& stateTrial,
193 std::string errorType, int verbose);
194
202
209
219 virtual double computeLambda();
220
221 // Protected accessors for member variables
222
226 int verbose() const;
227
231 std::string errorType() const;
232
236 T const & residual() const;
237
241 T const & state() const;
242
246 int totalItr();
247
248 // --- Timer value accessors --- //
249
253 double timerTotal();
254
258 double timerMDE();
259
263 double timerAM();
264
268 double timerResid();
269
273 double timerError();
274
278 double timerCoeff();
279
283 double timerOmega();
284
285 // Inherited members of parent classes with non-dependent names
288
289 private:
290
291 // Private member variables
292
294 double error_;
295
297 int nBasis_;
298
300 int itr_;
301
303 int totalItr_;
304
306 int nElem_;
307
309 double lambda_;
310
312 double r_;
313
315 bool useLambdaRamp_;
316
318 bool isAllocatedAM_;
319
321 RingBuffer<T> stateHistory_;
322
324 RingBuffer<T> stateBasis_;
325
327 RingBuffer<T> residualHistory_;
328
330 RingBuffer<T> residualBasis_;
331
334
337
339 DArray<double> coeffs_;
340
342 T stateTrial_;
343
345 T residualTrial_;
346
348 T temp_;
349
350 // Timers for analyzing performance
351 Timer timerMDE_;
352 Timer timerAM_;
353 Timer timerResid_;
354 Timer timerError_;
355 Timer timerCoeff_;
356 Timer timerOmega_;
357 Timer timerTotal_;
358
359 #ifdef PSCF_AM_TEST
360 bool hasAmTest_{false};
361 double preError_{0};
362 double projectionError_{0};
363 double correctionError_{0};
364 double projectionRatio_{0};
365 double correctionRatio_{0};
366 int testCounter{0};
367 #endif
368
369 // Private non-virtual functions used in AM algorithm
370
380 void updateBasis(RingBuffer<T> & basis,
381 RingBuffer<T> const & history);
382
389 void updateU(DMatrix<double> & U,
390 RingBuffer<T> const & resBasis);
391
399 void computeV(DArray<double> & v,
400 T const & resCurrent,
401 RingBuffer<T> const & resBasis);
402
410 void computeTrialCoeff();
411
422 void updateTrial();
423
435 void addEqVectors(T& v,
436 RingBuffer<T> const & basis,
437 DArray<double> coeffs);
438
439 // Private virtual functions with a default implementation
440
453 virtual
454 void addCorrection(T& stateTrial, T const & residualTrial);
455
456 // Private pure virtual functions that interact with parent system
457
465 virtual int nElements() = 0;
466
470 virtual bool hasInitialGuess() = 0;
471
477 virtual void getCurrent(T& curr) = 0;
478
486 virtual void evaluate() = 0;
487
493 virtual void getResidual(T& resid) = 0;
494
500 virtual void update(T& newGuess) = 0;
501
505 virtual void outputToLog() = 0;
506
507 // Private functions for vector math
508
517 virtual void setEqual(T& a, T const & b) = 0;
518
525 virtual double dotProduct(T const & a, T const & b) = 0;
526
535 double norm(T const & a);
536
542 virtual double maxAbs(T const & a) = 0;
543
551 virtual void subVV(T& a, T const & b, T const & c) = 0;
552
560 virtual void addEqVc(T& a, T const & b, double c) = 0;
561
562 };
563
564 /*
565 * Return integer level for verbosity of the log output (0-2).
566 */
567 template <typename Iterator, typename T>
570
571 /*
572 * Return error type string.
573 */
574 template <typename Iterator, typename T>
576 { return errorType_; }
577
578 /*
579 * Return the current state vector by const reference.
580 */
581 template <typename Iterator, typename T>
583 { return stateHistory_[0]; }
584
585 /*
586 * Return the current residual vector by const reference.
587 */
588 template <typename Iterator, typename T>
590 { return residualHistory_[0]; }
591
592 /*
593 * Has memory required by the AM algorithm been allocated?
594 */
595 template <typename Iterator, typename T>
597 { return isAllocatedAM_; }
598
599 /*
600 * Return total iteration counter.
601 */
602 template <typename Iterator, typename T>
604 { return totalItr_; }
605
606 /*
607 * Return computing MDE time cost.
608 */
609 template <typename Iterator, typename T>
611 { return timerMDE_.time(); }
612
613 /*
614 * Return computing AM time cost.
615 */
616 template <typename Iterator, typename T>
618 { return timerAM_.time(); }
619
620 /*
621 * Return computing Resid time cost.
622 */
623 template <typename Iterator, typename T>
625 { return timerResid_.time(); }
626
627 /*
628 * Return computing Error time cost.
629 */
630 template <typename Iterator, typename T>
632 { return timerError_.time(); }
633
634 /*
635 * Return computing Coeff time cost.
636 */
637 template <typename Iterator, typename T>
639 { return timerCoeff_.time(); }
640
641 /*
642 * Return computing Omega time cost.
643 */
644 template <typename Iterator, typename T>
646 { return timerOmega_.time(); }
647
648 /*
649 * Return total time cost.
650 */
651 template <typename Iterator, typename T>
653 { return timerTotal_.time(); }
654
655}
656#include "AmIteratorTmpl.tpp"
657#endif
double lambdaRampFactor()
Compute ramped prefactor of mixing parameter lambda.
virtual void setup(bool isContinuation)
Initialize just before entry to iterative loop.
double timerError()
Get time evaluating scalar error.
void readMixingParameters(std::istream &in, bool useLambdaRamp=true)
Read optional parameters used in default correction algorithm.
double computeError(int verbose)
Compute and return error used to test for convergence.
T const & residual() const
Get 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 spent updating w states.
void outputTimers(std::ostream &out) const
Log output timing results.
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.
~AmIteratorTmpl()
Destructor.
double timerCoeff()
Get time spent evaluating Anderson mixing coefficients.
void readErrorType(std::istream &in)
Read and validate the optional errorType string parameter.
double timerMDE()
Get time spent solving the modified diffusion equation (MDE).
double timerResid()
Get time spent computing residual.
AmIteratorTmpl()
Constructor.
virtual double computeLambda()
Compute mixing parameter for correction step of Anderson mixing.
virtual bool isValidErrorType()
Checks if a string is a valid error type.
virtual double computeError(T &residTrial, T &stateTrial, std::string errorType, int verbose)
Compute and return error used to test for convergence.
virtual void readParameters(std::istream &in)
Read all parameters and initialize.
T const & state() const
Return the current state vector by const reference.
int solve(bool isContinuation=false)
Iterate to a solution.
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
Wall clock timer.
Definition Timer.h:35
PSCF package top-level namespace.