1#ifndef PSCF_SWEEP_TMPL_TPP
2#define PSCF_SWEEP_TMPL_TPP
12#include <util/misc/Log.h>
21 template <
class State>
34 template <
class State>
41 template <
class State>
50 UTIL_THROW(
"Sweep parameter ns (number of steps) must be "
53 readOptional<std::string>(in,
"baseFileName", baseFileName_);
54 readOptional<int>(in,
"historyCapacity", historyCapacity_);
55 if (historyCapacity_ <= 0) {
56 UTIL_THROW(
"Sweep parameter historyCapacity must be positive");
58 readOptional<bool>(in,
"reuseState", reuseState_);
61 states_.allocate(historyCapacity_);
62 stateHistory_.allocate(historyCapacity_);
63 sHistory_.allocate(historyCapacity_);
64 c_.allocate(historyCapacity_);
67 template <
class State>
72 double ds = 1.0/double(
ns_);
76 Log::file() <<
"ds = " << ds << std::endl;
92 Log::file() <<
"===========================================\n";
96 bool isContinuation =
false;
97 error =
solve(isContinuation);
100 UTIL_THROW(
"Failure to converge initial state of sweep");
106 bool finished =
false;
114 sNew = double(k + 1)/double(den);
116 Log::file() <<
"===========================================\n";
117 Log::file() <<
"Attempt s = " << sNew << std::endl;
128 isContinuation = reuseState_;
129 error = solve(isContinuation);
133 Log::file() <<
"Backtrack and halve sweep step size:"
144 UTIL_THROW(
"Sweep decreased ds too many times.");
159 Log::file() <<
"===========================================\n";
166 template <
class State>
178 paramType.name = name;
180 paramType.modifierPtr = &modifier;
184 template <
class State>
197 template <
class State>
200 for (
int i = 0; i < paramTypes.
size(); i++) {
215 template <
class State>
219 UTIL_CHECK(states_.capacity() == historyCapacity_);
220 UTIL_CHECK(sHistory_.capacity() == historyCapacity_);
221 UTIL_CHECK(stateHistory_.capacity() == historyCapacity_);
224 for (
int i = 0; i < historyCapacity_; ++i) {
231 for (
int i = 0; i < historyCapacity_; ++i) {
233 stateHistory_[i] = &states_[i];
237 template <
class State>
238 void SweepTmpl<State>::accept(
double sNew)
242 for (
int i = historyCapacity_ - 1; i > 0; --i) {
243 sHistory_[i] = sHistory_[i-1];
249 temp = stateHistory_[historyCapacity_-1];
250 for (
int i = historyCapacity_ - 1; i > 0; --i) {
251 stateHistory_[i] = stateHistory_[i-1];
253 stateHistory_[0] = temp;
257 if (historySize_ < historyCapacity_) {
268 template <
class State>
272 if (historySize_ == 1) {
277 for (i = 0; i < historySize_; ++i) {
280 for (j = 0; j < historySize_; ++j) {
282 num *= (sNew -
s(j));
283 den *= (
s(i) -
s(j));
303 template <
class State>
Base class allowing subclasses to define sweepable parameters.
void addParameterTypes(GArray< ParameterType > paramTypes)
Declare an array of specialized sweep parameter types.
virtual void checkAllocation(State &state)=0
Check allocation of one state, allocate if necessary.
std::string baseFileName_
Base name for output files.
virtual void cleanup()
Clean up operation at the end of a sweep.
void initialize()
Initialize variables that track history of solutions.
SweepTmpl(int historyCapacity)
Constructor (protected).
void setCoefficients(double sNew)
Compute coefficients of previous states for continuation.
void addParameterType(std::string name, int nId, ParameterModifier &modifier)
Declare a specialized parameter type.
virtual void sweep()
Iterate to solution.
virtual int solve(bool isContinuation)=0
virtual void readParameters(std::istream &in)
Read ns and baseFileName parameters.
GArray< ParameterType > parameterTypes_
Array of specialized parameter types.
double s(int i) const
Get the value of s for a stored solution, with i = 0 most recent.
int historyCapacity() const
Get the maximum number of stored previous states.
An automatically growable array, analogous to a std::vector.
int size() const
Return the logical size of this array (number of elements).
static std::ostream & file()
Get log ostream by reference.
ScalarParam< Type > & read(std::istream &in, const char *label, Type &value)
Add and read a new required ScalarParam < Type > object.
void setClassName(const char *className)
Set class name string.
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
PSCF package top-level namespace.
Declaration of a specialized sweep parameter type.