9#include <r1/system/System.h>
10#include <r1/solvers/Mixture.h>
11#include <r1/solvers/Polymer.h>
12#include <r1/solvers/Solvent.h>
13#include <pscf/interaction/Interaction.h>
14#include <pscf/chem/Ensemble.h>
15#include <util/misc/Log.h>
53 read(in,
"epsilon", epsilon_);
58 void NrIterator::setup()
75 wFieldsNew_.allocate(nm);
76 cFieldsNew_.allocate(nm);
77 for (
int i = 0; i < nm; ++i) {
78 wFieldsNew_[i].allocate(nx);
79 cFieldsNew_[i].allocate(nx);
86 void NrIterator::computeResidual(
Array<FieldT> const & wFields,
98 for (i = 0; i < nx; ++i) {
101 for (j = 0; j < nm; ++j) {
107 for (j = 0; j < nm; ++j) {
109 for (k = 0; k < nm; ++k) {
110 wArray_[j] += chi(j, k)*cArray_[k];
115 for (j = 0; j < nm; ++j) {
117 residual[ir] = wArray_[j] -
wFields[j][i];
121 for (j = 1; j < nm; ++j) {
123 residual[ir] = residual[ir] - residual[i];
128 for (j = 0; j < nm; ++j) {
129 residual[i] += cArray_[j];
144 residual[nx-1] =
wFields[nm-1][nx-1];
152 void NrIterator::computeJacobian()
160 for (i = 0; i < nm; ++i) {
161 for (j = 0; j < nx; ++j) {
169 double delta = 0.001;
173 for (i = 0; i < nm; ++i) {
174 for (j = 0; j < nx; ++j) {
175 wFieldsNew_[i][j] += delta;
177 computeResidual(wFieldsNew_, cFieldsNew_, residualNew_);
178 for (jr = 0; jr < nr; ++jr) {
180 (residualNew_[jr] - residual_[jr])/delta;
188 solver_.computeLU(jacobian_);
192 void NrIterator::incrementWFields(
Array<FieldT> const & wOld,
203 for (i = 0; i < nm; ++i) {
204 for (j = 0; j < nx; ++j) {
205 wNew[i][j] = wOld[i][j] - dW[k];
212 double shift = wNew[nm-1][nx-1];
213 for (i = 0; i < nm; ++i) {
214 for (j = 0; j < nx; ++j) {
222 double NrIterator::residualNorm(
Array<double> const & residual)
const
229 for (
int ir = 0; ir < nr; ++ir) {
230 value = fabs(residual[ir]);
234 if (!std::isfinite(value)) {
235 return std::numeric_limits<double>::infinity();
255 for (
int i = 0; i < np; ++i) {
257 if (ensemble == Ensemble::Unknown) {
260 if (ensemble == Ensemble::Open) {
261 isCanonical_ =
false;
265 for (
int i = 0; i < ns; ++i) {
267 if (ensemble == Ensemble::Unknown) {
270 if (ensemble == Ensemble::Open) {
271 isCanonical_ =
false;
279 double shift =
wFields()[nm-1][nx-1];
281 for (i = 0; i < nm; ++i) {
282 for (j = 0; j < nx; ++j) {
291 double norm = residualNorm(residual_);
292 if (!std::isfinite(norm)) {
293 Log::file() <<
"Initial residual is not finite" << std::endl;
298 newJacobian_ =
false;
299 if (!isContinuation) {
300 needsJacobian_ =
true;
306 for (i = 0; i < maxItr_; ++i) {
308 <<
" , error = " << norm
311 if (norm < epsilon_) {
318 if (needsJacobian_) {
319 Log::file() <<
"Computing jacobian" << std::endl;;
322 needsJacobian_ =
false;
326 solver_.solve(residual_, dOmega_);
331 computeResidual(wFieldsNew_, cFieldsNew_, residualNew_);
332 normNew = residualNorm(residualNew_);
336 while (normNew > norm && j < 3) {
337 Log::file() <<
" decreasing increment, error = "
338 << normNew << std::endl;
339 needsJacobian_ =
true;
340 for (k = 0; k < nr; ++k) {
341 dOmega_[k] *= 0.66666666;
345 computeResidual(wFieldsNew_, cFieldsNew_, residualNew_);
346 normNew = residualNorm(residualNew_);
351 if (normNew > norm) {
352 Log::file() <<
" reversing increment, norm = "
353 << normNew << std::endl;
354 needsJacobian_ =
true;
355 for (k = 0; k < nr; ++k) {
356 dOmega_[k] *= -1.000;
360 computeResidual(wFieldsNew_, cFieldsNew_, residualNew_);
361 normNew = residualNorm(residualNew_);
365 if (normNew < norm) {
368 for (j = 0; j < nm; ++j) {
369 for (k = 0; k < nx; ++k) {
374 for (j = 0; j < nr; ++j) {
375 residual_[j] = residualNew_[j];
377 newJacobian_ =
false;
378 if (!needsJacobian_) {
379 if (normNew/norm > 0.5) {
380 needsJacobian_ =
true;
385 Log::file() <<
"Iteration failed, norm = "
386 << normNew << std::endl;
389 Log::file() <<
"Unrecoverable failure " << std::endl;
391 Log::file() <<
"Try rebuilding Jacobian" << std::endl;
392 needsJacobian_ =
true;
int nMonomer() const
Get number of monomer types.
int nPolymer() const
Get number of polymer species.
int nSolvent() const
Get number of solvent (point particle) species.
DMatrix< double > const & chi() const
Return the chi matrix by const reference.
void allocate(int n)
Allocate memory.
PolymerT & polymer(int id)
Get a polymer solver object by non-const reference.
SolventT & solvent(int id)
Get a solvent solver object.
int nx() const
Get number of spatial grid points, including both endpoints.
Iterator()
Default constructor.
void compute(DArray< FieldT > const &wFields, DArray< FieldT > &cFields)
Compute concentrations.
virtual ~NrIterator()
Destructor.
void readParameters(std::istream &in)
Read all parameters and initialize.
int solve(bool isContinuation=false)
Iterate self-consistent field equations to solution.
NrIterator()
Default constructor.
DArray< System::WField > & wFields()
Get array of all chemical potential fields.
System::WField & wField(int monomerId)
Get chemical potential field for a specific monomer type.
DArray< System::CField > & cFields()
Get array of all chemical potential fields.
const System & system() const
Get parent System by reference.
const Domain & domain() const
Get spatial domain (including grid info) by reference.
Main class in SCFT simulation of one system.
Field & cField(int monomerId)
Get chemical potential field for a specific monomer type.
Mixture & mixture()
Get Mixture by reference.
void computeFreeEnergy()
Compute free energy density and pressure for current fields.
WField & wField(int monomerId)
Get chemical potential field for a specific monomer type.
Interaction & interaction()
Get interaction (i.e., excess free energy) by reference.
Array container class template.
int capacity() const
Return allocated size.
void allocate(int capacity)
Allocate the underlying C array.
void allocate(int capacity1, int capacity2)
Allocate memory for a matrix.
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.
ScalarParam< Type > & readOptional(std::istream &in, const char *label, Type &value)
Add and read a new optional ScalarParam < Type > object.
#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.
Ensemble
Statistical ensemble for the number of molecules of one species.
SCFT with real 1D fields.
PSCF package top-level namespace.