PSCF v1.5.0
Simulator.h
1#ifndef RP_SIMULATOR_H
2#define RP_SIMULATOR_H
3
4/*
5* PSCF - Polymer Self-Consistent Field
6*
7* Copyright 2015 - 2026, David Morse (morse012@um.edu) and co-authors
8* Distributed under the terms of the GNU General Public License.
9*/
10
11#include <util/param/ParamComposite.h> // base class
12#include <rp/fts/simulator/SimState.h> // member
13#include <pscf/field/RField.h> // member
14#include <util/containers/DArray.h> // member (template)
15#include <util/containers/DMatrix.h> // member (template)
16#include <pscf/backend/TmplDeclare.h> // declaration macros
17
18#include <iostream>
19
20// Forward declarations
21namespace Util {
22 class Random;
23}
24namespace Pscf {
25 namespace Rp {
26 template <int D, class T> class System;
27 template <int D, class T> class Compressor;
28 template <int D, class T> class CompressorFactory;
29 template <int D, class T> class Perturbation;
30 template <int D, class T> class PerturbationFactory;
31 template <int D, class T> class Ramp;
32 template <int D, class T> class RampFactory;
33 }
34}
35
36namespace Pscf {
37namespace Rp {
38
39 using namespace Util;
40
91 template <int D, class T>
93 {
94
95 public:
96
99
102
109
113 ~Simulator();
114
115 // Prohibit copying and assignment.
116 Simulator(Simulator<D,T> const &) = delete;
117 Simulator<D,T>& operator = (Simulator<D,T> const &) = delete;
118
129 virtual void readParameters(std::istream &in);
130
141 void allocate();
142
146
158 virtual void simulate(int nStep);
159
176 virtual void analyze(int min, int max,
177 std::string classname,
178 std::string filename);
179
189 void clearData();
190
194
201 void analyzeChi();
202
213 DArray<double> const & chiEvals() const;
214
220 double chiEval(int a) const;
221
240 DMatrix<double> const & chiEvecs() const;
241
250 double chiEvecs(int a, int i) const;
251
266 DArray<double> const & sc() const;
267
276 double sc(int a) const;
277
281
288 void computeHamiltonian();
289
299 double hamiltonian() const;
300
322 double idealHamiltonian() const;
323
344 double fieldHamiltonian() const;
345
355
359 bool hasHamiltonian() const;
360
364
383 void computeWc();
384
392 DArray< RField<D,T> > const & wc() const;
393
401 RField<D,T> const & wc(int a) const;
402
406 bool hasWc() const;
407
411
421 void computeCc();
422
444 DArray< RField<D,T> > const & cc() const;
445
454 RField<D,T> const & cc(int a) const;
455
459 bool hasCc() const;
460
464
479 void computeDc();
480
489 DArray< RField<D,T> > const & dc() const;
490
496 RField<D,T> const & dc(int i) const;
497
501 bool hasDc() const;
502
506
518 void saveState();
519
530 void restoreState();
531
540 void clearState();
541
545
553 virtual void outputTimers(std::ostream& out) const;
554
563 virtual void outputMdeCounter(std::ostream& out) const;
564
570 virtual void clearTimers();
571
575 long iStep();
576
581
585
590
595
600
604 bool hasCompressor() const;
605
610
615
619 bool hasPerturbation() const;
620
625
630
634 bool hasRamp() const;
635
639 Ramp<D,T> const & ramp() const;
640
644 Ramp<D,T>& ramp();
645
647
648 protected:
649
650 // Protected member functions
651
657 void readRandomSeed(std::istream& in);
658
663
673 void readCompressor(std::istream& in, bool& isEnd);
674
679
689 void readPerturbation(std::istream& in, bool& isEnd);
690
697
702
712 void readRamp(std::istream& in, bool& isEnd);
713
719 void setRamp(Ramp<D,T>* ptr);
720
730
731 // Protected data members
732
743
754
762
770
775
780
785
794
806 long iStep_;
807
812
816 long seed_;
817
818 // Boolean status flags
819
824
828 bool hasWc_;
829
833 bool hasCc_;
834
838 bool hasDc_;
839
840 private:
841
849 DMatrix<double> chiP_;
850
860 DMatrix<double> chiEvecs_;
861
867 DArray<double> chiEvals_;
868
877 DArray<double> sc_;
878
882 mutable RField<D,T> tmpField_;
883
884 // Pointers to associated objects
885
889 System<D,T>* systemPtr_;
890
894 Random* randomPtr_;
895
899 VecRandom<T>* vecRandomPtr_;
900
904 CompressorFactory<D,T>* compressorFactoryPtr_;
905
909 Compressor<D,T>* compressorPtr_;
910
914 PerturbationFactory<D,T>* perturbationFactoryPtr_;
915
919 Perturbation<D,T>* perturbationPtr_;
920
924 RampFactory<D,T>* rampFactoryPtr_;
925
929 Ramp<D,T>* rampPtr_;
930
934 bool isAllocated_;
935
936 };
937
938 // Inline functions
939
940 // Access to associated objects via pointers
941
942 // Get the parent System by reference.
943 template <int D, class T> inline
945 {
946 UTIL_ASSERT(systemPtr_);
947 return *systemPtr_;
948 }
949
950 // Get the scalar random number generator by reference.
951 template <int D, class T> inline
953 {
954 UTIL_ASSERT(randomPtr_);
955 return *randomPtr_;
956 }
957
958 // Get the vector random number generator by reference.
959 template <int D, class T> inline
961 {
962 UTIL_ASSERT(vecRandomPtr_);
963 return *vecRandomPtr_;
964 }
965
966 // Does this Simulator have a Compressor?
967 template <int D, class T> inline
969 { return (bool)compressorPtr_; }
970
971 // Get the Compressor by non-const reference.
972 template <int D, class T> inline
974 {
975 UTIL_CHECK(compressorPtr_);
976 return *compressorPtr_;
977 }
978
979 // Get the Compressor by const reference.
980 template <int D, class T> inline
982 {
983 UTIL_CHECK(compressorPtr_);
984 return *compressorPtr_;
985 }
986
987 // Does this Simulator have an associated Perturbation?
988 template <int D, class T> inline
990 { return (bool)perturbationPtr_; }
991
992 // Get a Perturbation by const reference.
993 template <int D, class T> inline
995 {
996 UTIL_CHECK(perturbationPtr_);
997 return *perturbationPtr_;
998 }
999
1000 // Get a Perturbation by non-const reference.
1001 template <int D, class T> inline
1003 {
1004 UTIL_CHECK(perturbationPtr_);
1005 return *perturbationPtr_;
1006 }
1007
1008 // Does this Simulator have an associated Ramp?
1009 template <int D, class T> inline
1011 { return (bool)rampPtr_; }
1012
1013 // Get a Ramp by const reference.
1014 template <int D, class T> inline
1016 {
1017 UTIL_CHECK(rampPtr_);
1018 return *rampPtr_;
1019 }
1020
1021 // Get a Ramp by non-const reference.
1022 template <int D, class T> inline
1024 {
1025 UTIL_CHECK(rampPtr_);
1026 return *rampPtr_;
1027 }
1028
1029 // Get the stored internal state by reference.
1030 template <int D, class T> inline
1033
1034 // Projected Chi Matrix
1035
1036 // Return an array of eigenvalues of the projected chi matrix.
1037 template <int D, class T> inline
1039 { return chiEvals_; }
1040
1041 // Return a single eigenvalue of the projected chi matrix.
1042 template <int D, class T> inline
1043 double Simulator<D,T>::chiEval(int a) const
1044 { return chiEvals_[a]; }
1045
1046 // Return a matrix of eigenvectors of the projected chi matrix.
1047 template <int D, class T> inline
1049 { return chiEvecs_; }
1050
1051 // Return an element of an eigenvector of the projected chi matrix.
1052 template <int D, class T> inline
1053 double Simulator<D,T>::chiEvecs(int a, int i) const
1054 { return chiEvecs_(a, i); }
1055
1056 // Return an array of values of vector S.
1057 template <int D, class T> inline
1059 { return sc_; }
1060
1061 // Return one component of vector S.
1062 template <int D, class T> inline
1063 double Simulator<D,T>::sc(int a) const
1064 { return sc_[a]; }
1065
1066 // Hamiltonian and its components
1067
1068 // Has the Hamiltonian been computed for the current w fields ?
1069 template <int D, class T> inline
1072
1073 // Get the precomputed total Hamiltonian.
1074 template <int D, class T> inline
1076 {
1078 return hamiltonian_;
1079 }
1080
1081 // Get the ideal gas component of the precomputed Hamiltonian.
1082 template <int D, class T> inline
1084 {
1086 return idealHamiltonian_;
1087 }
1088
1089 // Get the harmonic field component of the precomputed Hamiltonian.
1090 template <int D, class T> inline
1092 {
1094 return fieldHamiltonian_;
1095 }
1096
1097 // Get the perturbation component of the precomputed Hamiltonian.
1098 template <int D, class T> inline
1104
1105 // Fields
1106
1107 // Have eigenvector components of the current w fields been computed?
1108 template <int D, class T> inline
1110 { return hasWc_; }
1111
1112 // Return all eigencomponents of the w fields.
1113 template <int D, class T> inline
1115 { return wc_; }
1116
1117 // Return a single eigenvector component of the w fields.
1118 template <int D, class T> inline
1119 RField<D,T> const & Simulator<D,T>::wc(int a) const
1120 { return wc_[a]; }
1121
1122 // Have eigenvector components of the current c fields been computed?
1123 template <int D, class T> inline
1125 { return hasCc_; }
1126
1127 // Return all eigenvector components of the current c fields.
1128 template <int D, class T> inline
1130 { return cc_; }
1131
1132 // Return a single eigenvector component of the current c fields.
1133 template <int D, class T> inline
1134 RField<D,T> const & Simulator<D,T>::cc(int a) const
1135 { return cc_[a]; }
1136
1137 // Have eigenvector components of the current d fields been computed?
1138 template <int D, class T>
1139 inline bool Simulator<D,T>::hasDc() const
1140 { return hasDc_; }
1141
1142 // Return all eigenvector components of the current d fields.
1143 template <int D, class T> inline
1145 { return dc_; }
1146
1147 // Return a single eigenvector component of the current d fields.
1148 template <int D, class T> inline
1149 RField<D,T> const & Simulator<D,T>::dc(int a) const
1150 { return dc_[a]; }
1151
1152 // Return the current converged simulation step index.
1153 template <int D, class T>
1155 { return iStep_; }
1156
1157 // Return the current total simulation step index.
1158 template <int D, class T>
1160 { return iTotalStep_; }
1161
1162 // Explicit instantation declarations
1163 PSCF_TMPL_DECLARE(Simulator)
1164
1165}
1166}
1167#endif
Factory for subclasses of Compressor.
Base class for iterators that impose incompressibility.
Definition Compressor.h:35
Factory for subclasses of Perturbation.
Base class for additive perturbations of standard FTS Hamiltonian.
Factory for subclasses of Ramp.
Definition RampFactory.h:36
Class that varies parameters during a simulation (abstract).
Definition Ramp.h:35
SimState stores the state used by a FTS simulation.
Definition SimState.h:44
Base class for field theoretic PS-FTS simulator.
Definition Simulator.h:93
long iTotalStep()
Return the current simulation step index.
Definition Simulator.h:1159
Random & random()
Get the scalar random number generator by reference.
Definition Simulator.h:952
double chiEval(int a) const
Get a single eigenvalue of the projected chi matrix.
Definition Simulator.h:1043
Simulator(System< D, T > &system)
Constructor.
Definition Simulator.tpp:53
virtual void simulate(int nStep)
Perform a field theoretic Monte-Carlo simulation.
bool hasDc_
Have functional derivatives of H[W] been computed ?
Definition Simulator.h:838
void readRamp(std::istream &in, bool &isEnd)
bool hasWc_
Have eigen-components of the current w fields been computed ?
Definition Simulator.h:828
void clearData()
Clear field eigen-components and Hamiltonian components.
CompressorFactory< D, CPT > & compressorFactory()
double fieldHamiltonian_
Quadratic field contribution to Hamiltonian.
Definition Simulator.h:784
void readPerturbation(std::istream &in, bool &isEnd)
bool hasCc() const
Are eigen-components of the current c fields valid ?
Definition Simulator.h:1124
DMatrix< double > const & chiEvecs() const
Get the matrix of all eigenvectors of the projected chi matrix.
Definition Simulator.h:1048
bool hasHamiltonian_
Has the Hamiltonian been computed for the current w and c fields?
Definition Simulator.h:823
void readCompressor(std::istream &in, bool &isEnd)
SimState< D, T > state_
Previous state saved at the beginning of a step.
Definition Simulator.h:769
VecRandom< T > & vecRandom()
Get the vector random number generator by reference.
Definition Simulator.h:960
bool hasDc() const
Are the current d fields valid ?
Definition Simulator.h:1139
RampFactory< D, CPT > & rampFactory()
System< D, T > & system()
Get the parent system by reference.
Definition Simulator.h:944
bool hasWc() const
Are eigen-components of the current w fields valid ?
Definition Simulator.h:1109
void setRamp(Ramp< D, CPT > *ptr)
void allocate()
Allocate required memory during initialization.
PerturbationFactory< D, CPT > & perturbationFactory()
void readRandomSeed(std::istream &in)
virtual void outputMdeCounter(std::ostream &out) const
void setPerturbation(Perturbation< D, CPT > *ptr)
DArray< RField< D, T > > dc_
Components of d fields on a real space grid.
Definition Simulator.h:761
Perturbation< D, T > const & perturbation() const
Get a Perturbation by const reference.
Definition Simulator.h:994
DArray< RField< D, T > > const & wc() const
Get all eigenvector components of the current w fields.
Definition Simulator.h:1114
double idealHamiltonian() const
Get an ideal contribution to the Hamiltonian.
Definition Simulator.h:1083
virtual void outputTimers(std::ostream &out) const
DArray< RField< D, T > > wc_
Eigenvector components of w fields on a real space grid.
Definition Simulator.h:742
long iTotalStep_
Step counter - total number of attempted BD or MC steps.
Definition Simulator.h:811
bool hasPerturbation() const
Does this Simulator have a Perturbation?
Definition Simulator.h:989
bool hasRamp() const
Does this Simulator have a Ramp?
Definition Simulator.h:1010
double perturbationHamiltonian() const
Get a perturbation to the standard Hamiltonian.
Definition Simulator.h:1099
double hamiltonian() const
Get the Hamiltonian used in PS-FTS.
Definition Simulator.h:1075
SimState< D, T > & state()
Get the SimState stored internal state by reference.
Definition Simulator.h:1031
bool hasCompressor() const
Does this Simulator have a Compressor?
Definition Simulator.h:968
Compressor< D, T > const & compressor() const
Get the Compressor by const reference.
Definition Simulator.h:981
bool hasCc_
Have eigen-components of the current c fields been computed ?
Definition Simulator.h:833
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.
Definition Simulator.h:806
DArray< double > const & sc() const
Get all components of the vector S.
Definition Simulator.h:1058
double idealHamiltonian_
Ideal gas contribution (-lnQ) to Hamiltonian.
Definition Simulator.h:779
RField< D, T > RFieldT
Container for a real-valued periodic field.
Definition Simulator.h:98
DArray< double > const & chiEvals() const
Get an array of the eigenvalues of the projected chi matrix.
Definition Simulator.h:1038
DArray< RField< D, T > > cc_
Eigenvector components of c fields on a real space grid.
Definition Simulator.h:753
long iStep()
Return the current converged simulation step index.
Definition Simulator.h:1154
double perturbationHamiltonian_
Perturbation contribution to the Hamiltonian.
Definition Simulator.h:793
long seed_
Random number generator seed (input value).
Definition Simulator.h:816
DArray< RField< D, T > > const & dc() const
Get all of the current d fields.
Definition Simulator.h:1144
double hamiltonian_
Total field theoretic Hamiltonian H[W] (extensive value).
Definition Simulator.h:774
void analyzeChi()
Perform eigenvalue analysis of projected chi matrix.
bool hasHamiltonian() const
Has the Hamiltonian been computed for the current w and c fields?
Definition Simulator.h:1070
DArray< RField< D, T > > const & cc() const
Get all eigenvector components of the current c fields.
Definition Simulator.h:1129
double fieldHamiltonian() const
Get the quadratic field contribution to the Hamiltonian.
Definition Simulator.h:1091
Ramp< D, T > const & ramp() const
Get a Ramp by const reference.
Definition Simulator.h:1015
Base class template for classes that represent a complete system.
Dynamically allocatable contiguous array template.
Definition DArray.h:32
Dynamically allocated Matrix.
Definition DMatrix.h:25
ParamComposite()
Constructor.
Random number generator.
Definition Random.h:47
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition global.h:68
#define UTIL_ASSERT(condition)
Assertion macro suitable for debugging serial or parallel code.
Definition global.h:75
Code for programs that simulate real-valued periodic fields.
Definition param_pc.dox:2
PSCF package top-level namespace.
Utility classes for scientific computation.