1#ifndef PSCF_SWEEP_TMPL_TPP
2#define PSCF_SWEEP_TMPL_TPP
12#include <util/misc/Log.h>
21 template <
class State>
25 historyCapacity_(historyCapacity),
34 template <
class State>
41 template <
class State>
48 read<int>(in,
"ns", ns_);
49 readOptional<std::string>(in,
"baseFileName", baseFileName_);
50 readOptional<int>(in,
"historyCapacity", historyCapacity_);
51 readOptional<bool>(in,
"reuseState", reuseState_);
55 states_.allocate(historyCapacity_);
56 stateHistory_.allocate(historyCapacity_);
57 sHistory_.allocate(historyCapacity_);
58 c_.allocate(historyCapacity_);
61 template <
class State>
66 double ds = 1.0/double(ns_);
69 Log::file() <<
"ns = " << ns_ << std::endl;
70 Log::file() <<
"ds = " << ds << std::endl;
78 Log::file() <<
"===========================================\n";
79 Log::file() <<
"Attempt s = " << sNew << std::endl;
82 bool isContinuation =
false;
83 error = solve(isContinuation);
86 UTIL_THROW(
"Failure to converge initial state of sweep");
92 bool finished =
false;
102 Log::file() <<
"===========================================\n";
103 Log::file() <<
"Attempt s = " << sNew << std::endl;
114 isContinuation = reuseState_;
115 error = solve(isContinuation);
119 Log::file() <<
"Backtrack and halve sweep step size:"
128 UTIL_THROW(
"Sweep decreased ds too many times.");
138 if (sNew + ds > 1.0000001) {
142 Log::file() <<
"===========================================\n";
152 template <
class State>
156 UTIL_CHECK(states_.capacity() == historyCapacity_);
157 UTIL_CHECK(sHistory_.capacity() == historyCapacity_);
158 UTIL_CHECK(stateHistory_.capacity() == historyCapacity_);
161 for (
int i = 0; i < historyCapacity_; ++i) {
162 checkAllocation(states_[i]);
168 for (
int i = 0; i < historyCapacity_; ++i) {
170 stateHistory_[i] = &states_[i];
174 template <
class State>
179 for (
int i = historyCapacity_ - 1; i > 0; --i) {
180 sHistory_[i] = sHistory_[i-1];
186 temp = stateHistory_[historyCapacity_-1];
187 for (
int i = historyCapacity_ - 1; i > 0; --i) {
188 stateHistory_[i] = stateHistory_[i-1];
190 stateHistory_[0] = temp;
194 if (historySize_ < historyCapacity_) {
205 template <
class State>
209 if (historySize_ == 1) {
214 for (i = 0; i < historySize_; ++i) {
217 for (j = 0; j < historySize_; ++j) {
219 num *= (sNew - s(j));
220 den *= (s(i) - s(j));
240 template <
class State>
Solve a sequence of problems along a path through parameter space.
virtual void cleanup()
Clean up operation at the end of a sweep.
void initialize()
Initialize variables that track history of solutions.
void setCoefficients(double sNew)
Compute coefficients of previous states for continuation.
virtual void sweep()
Iterate to solution.
virtual void readParameters(std::istream &in)
Read ns and baseFileName parameters.
static std::ostream & file()
Get log ostream by reference.
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.
C++ namespace for polymer self-consistent field theory (PSCF).
Utility classes for scientific computation.