1#ifndef RP_SIMULATOR_TPP
2#define RP_SIMULATOR_TPP
13#include <rp/fts/simulator/SimState.h>
14#include <rp/fts/compressor/Compressor.h>
15#include <rp/fts/compressor/CompressorFactory.h>
16#include <rp/fts/perturbation/Perturbation.h>
17#include <rp/fts/perturbation/PerturbationFactory.h>
18#include <rp/fts/ramp/Ramp.h>
19#include <rp/fts/ramp/RampFactory.h>
21#include <rp/system/System.h>
22#include <rp/solvers/Mixture.h>
23#include <rp/solvers/Polymer.h>
24#include <rp/solvers/Solvent.h>
25#include <rp/field/Domain.h>
26#include <rp/field/CFields.h>
27#include <rp/field/WFields.h>
29#include <pscf/field/RField.h>
31#include <pscf/backend/VecRandom.h>
32#include <pscf/backend/Reduce.h>
33#include <pscf/backend/VecOp.h>
34#include <pscf/interaction/Interaction.h>
35#include <pscf/chem/PolymerModel.h>
36#include <pscf/math/IntVec.h>
38#include <util/misc/Timer.h>
39#include <util/random/Random.h>
42#include <gsl/gsl_eigen.h>
52 template <
int D,
class T>
67 vecRandomPtr_(nullptr),
68 compressorFactoryPtr_(nullptr),
69 compressorPtr_(nullptr),
70 perturbationFactoryPtr_(nullptr),
71 perturbationPtr_(nullptr),
72 rampFactoryPtr_(nullptr),
80 perturbationFactoryPtr_
90 template <
int D,
class T>
100 if (compressorFactoryPtr_) {
101 delete compressorFactoryPtr_;
103 if (perturbationFactoryPtr_) {
104 delete perturbationFactoryPtr_;
106 if (rampFactoryPtr_) {
107 delete rampFactoryPtr_;
111 if (compressorPtr_) {
112 delete compressorPtr_;
114 if (perturbationPtr_) {
115 delete perturbationPtr_;
125 template <
int D,
class T>
130 const int nMonomer =
system().mixture().nMonomer();
133 chiP_.allocate(nMonomer, nMonomer);
134 chiEvals_.allocate(nMonomer);
135 chiEvecs_.allocate(nMonomer, nMonomer);
136 sc_.allocate(nMonomer);
139 wc_.allocate(nMonomer);
140 cc_.allocate(nMonomer);
142 for (
int i = 0; i < nMonomer; ++i) {
143 wc_[i].allocate(dimensions);
144 cc_[i].allocate(dimensions);
148 dc_.allocate(nMonomer-1);
149 for (
int i = 0; i < nMonomer - 1; ++i) {
150 dc_[i].allocate(dimensions);
154 tmpField_.allocate(dimensions);
157 if (!state().isAllocated) {
158 state().allocate(nMonomer, dimensions);
169 template <
int D,
class T>
190 template <
int D,
class T>
192 {
UTIL_THROW(
"Error: Unimplemented function Simulator<D,T>::simulate"); }
197 template <
int D,
class T>
199 std::string classname,
200 std::string filename)
201 {
UTIL_THROW(
"Error: Unimplemented function Simulator<D,T>::analyze"); }
206 template <
int D,
class T>
218 template <
int D,
class T>
232 const int nMonomer = mixture.
nMonomer();
233 const int meshSize = domain.
mesh().size();
247 for (
int i = 0; i < np; ++i) {
248 polymerPtr = &mixture.
polymer(i);
249 phi = polymerPtr->
phi();
250 mu = polymerPtr->
mu();
252 length = polymerPtr->
length();
254 length = (double)polymerPtr->
nBead();
258 if (polymerPtr->
ensemble() == Ensemble::Closed) {
271 for (
int i = 0; i < ns; ++i) {
272 solventPtr = &mixture.
solvent(i);
273 phi = solventPtr->
phi();
274 mu = solventPtr->
mu();
275 size = solventPtr->
size();
278 if (solventPtr->
ensemble() == Ensemble::Closed) {
297 double prefactor, wSquare;
298 for (
int j = 0; j < nMonomer - 1; ++j) {
303 prefactor = -0.5*double(nMonomer)/chiEvals_[j];
306 fieldHamiltonian_ /= double(meshSize);
309 fieldHamiltonian_ += 0.5*sc_[nMonomer - 1];
314 const double vSystem = domain.
unitCell().volume();
315 const double vMonomer = mixture.
vMonomer();
316 const double nMonomerSystem = vSystem / vMonomer;
319 fieldHamiltonian_ *= nMonomerSystem;
320 idealHamiltonian_ *= nMonomerSystem;
322 hamiltonian_ = idealHamiltonian_ + fieldHamiltonian_;
325 if (hasPerturbation()) {
326 perturbationHamiltonian_
327 = perturbation().hamiltonian(hamiltonian_);
328 hamiltonian_ += perturbationHamiltonian_;
330 perturbationHamiltonian_ = 0.0;
333 hasHamiltonian_ =
true;
339 template <
int D,
class T>
344 const int nMonomer =
system().mixture().nMonomer();
346 double d = 1.0/double(nMonomer);
352 for (i = 0; i < nMonomer; ++i) {
353 for (j = 0; j < nMonomer; ++j) {
362 for (i = 0; i < nMonomer; ++i) {
363 for (j = 0; j < nMonomer; ++j) {
365 for (k = 0; k < nMonomer; ++k) {
366 CP(i,j) += chi(i,k)*P(k,j);
374 for (i = 0; i < nMonomer; ++i) {
375 for (j = 0; j < nMonomer; ++j) {
377 for (k = 0; k < nMonomer; ++k) {
378 chiP(i,j) += P(i,k)*CP(k,j);
387 gsl_matrix* A = gsl_matrix_alloc(nMonomer, nMonomer);
390 for (i = 0; i < nMonomer; ++i) {
391 for (j = 0; j < nMonomer; ++j) {
392 gsl_matrix_set(A, i, j, chiP(i, j));
397 gsl_eigen_symmv_workspace* work = gsl_eigen_symmv_alloc(nMonomer);
398 gsl_vector* Avals = gsl_vector_alloc(nMonomer);
399 gsl_matrix* Avecs = gsl_matrix_alloc(nMonomer, nMonomer);
401 error = gsl_eigen_symmv(A, Avals, Avecs, work);
413 for (i = 0; i < nMonomer; ++i) {
414 val = gsl_vector_get(Avals, i);
415 if (std::abs(val) < 1.0E-8) {
422 for (j = 0; j < nMonomer; ++j) {
423 chiEvecs_(k, j) = gsl_matrix_get(Avecs, j, i);
425 if (chiEvecs_(k, 0) < 0.0) {
426 for (j = 0; j < nMonomer; ++j) {
427 chiEvecs_(k, j) = -chiEvecs_(k, j);
439 for (j = 0; j < nMonomer; ++j) {
440 chiEvecs_(i, j) = gsl_matrix_get(Avecs, j, iNull);
442 if (chiEvecs_(i, 0) < 0) {
443 for (j = 0; j < nMonomer; ++j) {
444 chiEvecs_(i, j) = -chiEvecs_(i, j);
450 gsl_vector_free(Avals);
451 gsl_matrix_free(Avecs);
452 gsl_eigen_symmv_free(work);
455 double vec, norm, prefactor;
456 for (i = 0; i < nMonomer; ++i) {
458 for (j = 0; j < nMonomer; ++j) {
459 vec = chiEvecs_(i, j);
462 prefactor = sqrt(
double(nMonomer)/norm );
463 for (j = 0; j < nMonomer; ++j) {
464 chiEvecs_(i, j) *= prefactor;
469 for (j = 0; j < nMonomer; ++j) {
470 UTIL_CHECK(std::abs(chiEvecs_(nMonomer-1, j) - 1.0) < 1.0E-8);
476 for (i = 0; i < nMonomer; ++i) {
478 for (j = 0; j < nMonomer; ++j) {
481 s[i] = s[i]/double(nMonomer);
485 for (i = 0; i < nMonomer; ++i) {
487 for (j = 0; j < nMonomer; ++j) {
488 sc_[i] += chiEvecs_(i,j)*s[j];
490 sc_[i] = sc_[i]/double(nMonomer);
499 template <
int D,
class T>
507 const int nMonomer =
system().mixture().nMonomer();
510 for (i = 0; i < nMonomer; ++i) {
517 for (j = 0; j < nMonomer; ++j) {
518 vec = chiEvecs_(i, j)/double(nMonomer);
530 template <
int D,
class T>
540 const int nMonomer =
system().mixture().nMonomer();
543 for (i = 0; i < nMonomer; ++i) {
550 for (j = 0; j < nMonomer; ++j) {
551 vec = chiEvecs_(i, j);
562 template <
int D,
class T>
571 const int nMonomer =
system().mixture().nMonomer();
572 const double vMonomer =
system().mixture().vMonomer();
573 const double a = 1.0/vMonomer;
577 for (
int i = 0; i < nMonomer - 1; ++i) {
581 b = -1.0*a*double(nMonomer)/chiEvals_[i];
587 if (hasPerturbation()) {
588 perturbation().incrementDc(dc_);
599 template <
int D,
class T>
607 const int nMonomer =
system().mixture().nMonomer();
610 for (
int i = 0; i < nMonomer; ++i) {
616 if (
state().needsCc) {
619 for (
int i = 0; i < nMonomer; ++i) {
625 if (
state().needsDc) {
628 for (
int i = 0; i < nMonomer - 1; ++i) {
634 if (
state().needsHamiltonian){
646 state().hasData =
true;
655 template <
int D,
class T>
660 const int nMonomer =
system().mixture().nMonomer();
666 for (
int i = 0; i < nMonomer; ++i) {
672 if (state().needsCc) {
673 for (
int i = 0; i < nMonomer; ++i) {
680 if (
state().needsDc) {
681 for (
int i = 0; i < nMonomer - 1; ++i) {
688 if (state().needsHamiltonian){
702 state().hasData =
false;
710 template <
int D,
class T>
717 template <
int D,
class T>
720 if (!compressorPtr_)
return;
722 compressorPtr_->outputTimers(out);
728 template <
int D,
class T>
732 out <<
"MDE counter "
733 << compressorPtr_->mdeCounter() << std::endl;
740 template <
int D,
class T>
753 template <
int D,
class T>
773 template <
int D,
class T>
792 template <
int D,
class T>
796 return *compressorFactoryPtr_;
804 template <
int D,
class T>
823 template <
int D,
class T>
827 return *perturbationFactoryPtr_;
833 template <
int D,
class T>
837 perturbationPtr_ = ptr;
845 template <
int D,
class T>
863 template <
int D,
class T>
867 return *rampFactoryPtr_;
873 template <
int D,
class T>
int nMonomer() const
Get number of monomer types.
double vMonomer() const
Get monomer reference volume (set to 1.0 by default).
int nPolymer() const
Get number of polymer species.
int nSolvent() const
Get number of solvent (point particle) species.
An IntVec<D, T> is a D-component vector of elements of integer type T.
int nBead() const
Total number of beads in the polymer (bead model).
double length() const
Sum of the lengths of all blocks in the polymer (thread model).
Factory for subclasses of Compressor.
CompressorFactory(System< D, T > &system)
Constructor.
Spatial domain for a periodic structure with real fields.
UnitCell< D > & unitCell()
Get the UnitCell by non-const reference.
Mesh< D > & mesh()
Get the Mesh by non-const reference.
Solver and descriptor for a mixture of polymers and solvents.
SolventT & solvent(int id)
Get a solvent solver object.
PolymerT & polymer(int id)
Get a polymer solver object by non-const reference.
Factory for subclasses of Perturbation.
PerturbationFactory(Simulator< D, T > &simulator)
Constructor.
Base class for additive perturbations of standard FTS Hamiltonian.
Descriptor and MDE solver for one polymer species.
Factory for subclasses of Ramp.
RampFactory(Simulator< D, T > &simulator)
Constructor.
Class that varies parameters during a simulation (abstract).
virtual void clearTimers()
Clear timers.
Random & random()
Get the scalar random number generator by reference.
Simulator(System< D, T > &system)
Constructor.
void saveState()
Save a copy of the current system state.
virtual void simulate(int nStep)
Perform a field theoretic Monte-Carlo simulation.
bool hasDc_
Have functional derivatives of H[W] been computed ?
void computeHamiltonian()
Compute the Hamiltonian used in PS-FTS.
void readRamp(std::istream &in, bool &isEnd)
bool hasWc_
Have eigen-components of the current w fields been computed ?
void clearData()
Clear field eigen-components and Hamiltonian components.
CompressorFactory< D, T > & compressorFactory()
Get the Compressor factory by reference.
double fieldHamiltonian_
Quadratic field contribution to Hamiltonian.
void readPerturbation(std::istream &in, bool &isEnd)
bool hasCc() const
Are eigen-components of the current c fields valid ?
void computeWc()
Compute eigenvector components of the current w fields.
bool hasHamiltonian_
Has the Hamiltonian been computed for the current w and c fields?
void readCompressor(std::istream &in, bool &isEnd)
VecRandom< T > & vecRandom()
Get the vector random number generator by reference.
bool hasDc() const
Are the current d fields valid ?
void computeCc()
Compute eigenvector components of the current c fields.
RampFactory< D, T > & rampFactory()
Get the Ramp factory by reference.
System< D, T > & system()
Get the parent system by reference.
bool hasWc() const
Are eigen-components of the current w fields valid ?
void setRamp(Ramp< D, T > *ptr)
Set the associated Ramp.
void allocate()
Allocate required memory during initialization.
PerturbationFactory< D, T > & perturbationFactory()
Get the Perturbation factory by reference.
void readRandomSeed(std::istream &in)
Optionally read a random seed and initialize RNGs.
virtual void outputMdeCounter(std::ostream &out) const
void setPerturbation(Perturbation< D, T > *ptr)
Set the associated Perturbation.
DArray< RField< D, CPT > > dc_
Perturbation< D, T > const & perturbation() const
Get a Perturbation by const reference.
double idealHamiltonian() const
Get an ideal contribution to the Hamiltonian.
void restoreState()
Restore the system to the saved state.
virtual void outputTimers(std::ostream &out) const
Output timing results.
void computeDc()
Compute functional derivatives of the Hamiltonian.
DArray< RField< D, CPT > > wc_
long iTotalStep_
Step counter - total number of attempted BD or MC steps.
bool hasPerturbation() const
Does this Simulator have a Perturbation?
bool hasRamp() const
Does this Simulator have a Ramp?
double perturbationHamiltonian() const
Get a perturbation to the standard Hamiltonian.
double hamiltonian() const
Get the Hamiltonian used in PS-FTS.
SimState< D, CPT > & state()
bool hasCompressor() const
Does this Simulator have a Compressor?
Compressor< D, T > const & compressor() const
Get the Compressor by const reference.
bool hasCc_
Have eigen-components of the current c fields been computed ?
virtual void analyze(int min, int max, std::string classname, std::string filename)
Read and analyze a trajectory file.
virtual void readParameters(std::istream &in)
Read parameters for a simulation.
long iStep_
Step counter - number of steps for which the compressor converged.
void clearState()
Clear the saved copy of the system state.
double idealHamiltonian_
Ideal gas contribution (-lnQ) to Hamiltonian.
DArray< RField< D, CPT > > cc_
double perturbationHamiltonian_
Perturbation contribution to the Hamiltonian.
long seed_
Random number generator seed (input value).
DArray< RField< D, T > > const & dc() const
Get all of the current d fields.
double hamiltonian_
Total field theoretic Hamiltonian H[W] (extensive value).
void analyzeChi()
Perform eigenvalue analysis of projected chi matrix.
bool hasHamiltonian() const
Has the Hamiltonian been computed for the current w and c fields?
DArray< RField< D, T > > const & cc() const
Get all eigenvector components of the current c fields.
double fieldHamiltonian() const
Get the quadratic field contribution to the Hamiltonian.
Solver and descriptor for a solvent species.
Base class template for classes that represent a complete system.
double size() const
Get the size (number of monomers) in this solvent.
Ensemble ensemble() const
Get the statistical ensemble for this species.
WT mu() const
Get the chemical potential for this species (units kT=1).
WT phi() const
Get the overall volume fraction for this species.
Dynamically allocatable contiguous array template.
void allocate(int capacity)
Allocate the underlying C array.
Dynamically allocated Matrix.
void allocate(int capacity1, int capacity2)
Allocate memory for a matrix.
static std::ostream & file()
Get log ostream by reference.
std::string indent() const
Return indent string for this object (string of spaces).
static bool echo()
Get echo parameter.
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.
std::string className() const
Get class name string.
File containing preprocessor macros for error handling.
#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.
double sumSq(Array< double > const &in)
Compute sum of of squares of array elements (real).
double sum(Array< double > const &in)
Compute sum of array elements (real).
void eqS(Array< double > &a, double b)
Vector assignment, a[i] = b (real).
void subVS(Array< double > &a, Array< double > const &b, double c)
Vector-scalar subtraction, a[i] = b[i] - c (real).
void addEqVc(Array< double > &a, Array< double > const &b, const double c)
Add scaled vector in-place, a[i] += b[i]*c (real).
bool isThread()
Is the thread model in use ?
Code for programs that simulate real-valued periodic fields.
void addVcVcS(Array< double > &a, Array< double > const &b1, const double c1, Array< double > const &b2, const double c2, const double s)
Add scaled vectors + scalar, a[i] = b1[i]*c1 + b2[2]*c2 + s (real).
PSCF package top-level namespace.