PSCF v1.4.0
rp/fts/simulator/Simulator.h
1#ifndef RP_SIMULATOR_H
2#define RP_SIMULATOR_H
3
4/*
5* PSCF - Polymer Self-Consistent Field
6*
7* Copyright 2015 - 2025, The Regents of the University of Minnesota
8* Distributed under the terms of the GNU General Public License.
9*/
10
11#include <util/param/ParamComposite.h> // base class
12#include <util/containers/DArray.h> // member (template)
13#include <util/containers/DMatrix.h> // member (template)
14#include <iostream>
15
16// Forward declaration
17namespace Util { class Random; }
18
19namespace Pscf {
20namespace Rp {
21
22 using namespace Util;
23
72 template <int D, class T>
74 {
75
76 public:
77
79 using RFieldT = typename T::RField;
80
81 // Protected constructor and destructor (see below).
82
85
96 virtual void readParameters(std::istream &in);
97
106 void allocate();
107
111
123 virtual void simulate(int nStep);
124
141 virtual void analyze(int min, int max,
142 std::string classname,
143 std::string filename);
144
151 void clearData();
152
156
164 virtual void outputTimers(std::ostream& out) const;
165
174 virtual void outputMdeCounter(std::ostream& out) const;
175
181 virtual void clearTimers();
182
186 long iStep();
187
192
196
204
215 DArray<double> const & chiEvals() const;
216
222 double chiEval(int a) const;
223
242 DMatrix<double> const & chiEvecs() const;
243
252 double chiEvecs(int a, int i) const;
253
268 DArray<double> const & sc() const;
269
278 double sc(int a) const;
279
283
288
298 double hamiltonian() const;
299
321 double idealHamiltonian() const;
322
343 double fieldHamiltonian() const;
344
354
358 bool hasHamiltonian() const;
359
363
380 void computeWc();
381
389 DArray<RFieldT> const & wc() const;
390
398 RFieldT const & wc(int a) const;
399
403 bool hasWc() const;
404
408
416 void computeCc();
417
439 DArray<RFieldT> const & cc() const;
440
449 RFieldT const & cc(int a) const;
450
454 bool hasCc() const;
455
459
467 void computeDc();
468
477 DArray<RFieldT> const & dc() const;
478
484 RFieldT const & dc(int i) const;
485
489 bool hasDc() const;
490
494
506 void saveState();
507
519
529
533
537 typename T::System& system();
538
543
547 typename T::VecRandom& vecRandom();
548
552 bool hasCompressor() const;
553
557 typename T::Compressor const & compressor() const;
558
562 typename T::Compressor& compressor();
563
567 bool hasPerturbation() const;
568
572 typename T::Perturbation const & perturbation() const;
573
577 typename T::Perturbation& perturbation();
578
582 bool hasRamp() const;
583
587 typename T::Ramp const & ramp() const;
588
592 typename T::Ramp& ramp();
593
595
596 protected:
597
598 // Protected member functions
599
606 Simulator(typename T::System& system,
607 typename T::Simulator& simulator);
608
613
614 // Prohibit copying and assignment.
615 Simulator(Simulator<D,T> const &) = delete;
616 Simulator<D,T>& operator = (Simulator<D,T> const &) = delete;
617
623 void readRandomSeed(std::istream& in);
624
630 virtual void initializeVecRandom()
631 {};
632
636 typename T::CompressorFactory& compressorFactory();
637
647 void readCompressor(std::istream& in, bool& isEnd);
648
652 typename T::PerturbationFactory& perturbationFactory();
653
663 void readPerturbation(std::istream& in, bool& isEnd);
664
670 void setPerturbation(typename T::Perturbation* ptr);
671
675 typename T::RampFactory& rampFactory();
676
686 void readRamp(std::istream& in, bool& isEnd);
687
693 void setRamp(typename T::Ramp* ptr);
694
703 typename T::SimState& state();
704
705 // Protected data members
706
717
728
736
743 mutable typename T::SimState state_;
744
749
754
759
768
780 long iStep_;
781
786
790 long seed_;
791
792 // Boolean status flags
793
798
802 bool hasWc_;
803
807 bool hasCc_;
808
812 bool hasDc_;
813
814 private:
815
823 DMatrix<double> chiP_;
824
834 DMatrix<double> chiEvecs_;
835
841 DArray<double> chiEvals_;
842
851 DArray<double> sc_;
852
856 mutable RFieldT tmpField_;
857
858 // Pointers to associated objects
859
863 typename T::System* systemPtr_;
864
868 typename T::Simulator* simulatorPtr_;
869
873 Random* randomPtr_;
874
878 typename T::VecRandom* vecRandomPtr_;
879
883 typename T::CompressorFactory* compressorFactoryPtr_;
884
888 typename T::Compressor* compressorPtr_;
889
893 typename T::PerturbationFactory* perturbationFactoryPtr_;
894
898 typename T::Perturbation* perturbationPtr_;
899
903 typename T::RampFactory* rampFactoryPtr_;
904
908 typename T::Ramp* rampPtr_;
909
913 bool isAllocated_;
914
915 };
916
917 // Inline functions
918
919 // Access to associated objects via pointers
920
921 // Get the parent System by reference.
922 template <int D, class T> inline
923 typename T::System& Simulator<D,T>::system()
924 {
925 UTIL_ASSERT(systemPtr_);
926 return *systemPtr_;
927 }
928
929 // Get the scalar random number generator by reference.
930 template <int D, class T> inline
932 {
933 UTIL_ASSERT(randomPtr_);
934 return *randomPtr_;
935 }
936
937 // Get the vector random number generator by reference.
938 template <int D, class T> inline
939 typename T::VecRandom& Simulator<D,T>::vecRandom()
940 {
941 UTIL_ASSERT(vecRandomPtr_);
942 return *vecRandomPtr_;
943 }
944
945 // Does this Simulator have a Compressor?
946 template <int D, class T> inline
948 { return (bool)compressorPtr_; }
949
950 // Get the Compressor by non-const reference.
951 template <int D, class T> inline
952 typename T::Compressor& Simulator<D,T>::compressor()
953 {
954 UTIL_CHECK(compressorPtr_);
955 return *compressorPtr_;
956 }
957
958 // Get the Compressor by const reference.
959 template <int D, class T> inline
960 typename T::Compressor const & Simulator<D,T>::compressor() const
961 {
962 UTIL_CHECK(compressorPtr_);
963 return *compressorPtr_;
964 }
965
966 // Does this Simulator have an associated Perturbation?
967 template <int D, class T> inline
969 { return (bool)perturbationPtr_; }
970
971 // Get a Perturbation by const reference.
972 template <int D, class T> inline
973 typename T::Perturbation const & Simulator<D,T>::perturbation() const
974 {
975 UTIL_CHECK(perturbationPtr_);
976 return *perturbationPtr_;
977 }
978
979 // Get a Perturbation by non-const reference.
980 template <int D, class T> inline
981 typename T::Perturbation& Simulator<D,T>::perturbation()
982 {
983 UTIL_CHECK(perturbationPtr_);
984 return *perturbationPtr_;
985 }
986
987 // Does this Simulator have an associated Ramp?
988 template <int D, class T> inline
990 { return (bool)rampPtr_; }
991
992 // Get a Ramp by const reference.
993 template <int D, class T> inline
994 typename T::Ramp const & Simulator<D,T>::ramp() const
995 {
996 UTIL_CHECK(rampPtr_);
997 return *rampPtr_;
998 }
999
1000 // Get a Ramp by non-const reference.
1001 template <int D, class T> inline
1002 typename T::Ramp& Simulator<D,T>::ramp()
1003 {
1004 UTIL_CHECK(rampPtr_);
1005 return *rampPtr_;
1006 }
1007
1008 // Get the stored internal state by reference.
1009 template <int D, class T> inline
1010 typename T::SimState& Simulator<D,T>::state()
1011 { return state_; }
1012
1013 // Projected Chi Matrix
1014
1015 // Return an array of eigenvalues of the projected chi matrix.
1016 template <int D, class T> inline
1018 { return chiEvals_; }
1019
1020 // Return a single eigenvalue of the projected chi matrix.
1021 template <int D, class T> inline
1022 double Simulator<D,T>::chiEval(int a) const
1023 { return chiEvals_[a]; }
1024
1025 // Return a matrix of eigenvectors of the projected chi matrix.
1026 template <int D, class T> inline
1028 { return chiEvecs_; }
1029
1030 // Return an element of an eigenvector of the projected chi matrix.
1031 template <int D, class T> inline
1032 double Simulator<D,T>::chiEvecs(int a, int i) const
1033 { return chiEvecs_(a, i); }
1034
1035 // Return an array of values of vector S.
1036 template <int D, class T> inline
1038 { return sc_; }
1039
1040 // Return one component of vector S.
1041 template <int D, class T> inline
1042 double Simulator<D,T>::sc(int a) const
1043 { return sc_[a]; }
1044
1045 // Hamiltonian and its components
1046
1047 // Has the Hamiltonian been computed for the current w fields ?
1048 template <int D, class T> inline
1051
1052 // Get the precomputed total Hamiltonian.
1053 template <int D, class T> inline
1055 {
1057 return hamiltonian_;
1058 }
1059
1060 // Get the ideal gas component of the precomputed Hamiltonian.
1061 template <int D, class T> inline
1063 {
1065 return idealHamiltonian_;
1066 }
1067
1068 // Get the harmonic field component of the precomputed Hamiltonian.
1069 template <int D, class T> inline
1071 {
1073 return fieldHamiltonian_;
1074 }
1075
1076 // Get the perturbation component of the precomputed Hamiltonian.
1077 template <int D, class T> inline
1083
1084 // Fields
1085
1086 // Have eigenvector components of the current w fields been computed?
1087 template <int D, class T> inline
1089 { return hasWc_; }
1090
1091 // Return all eigencomponents of the w fields.
1092 template <int D, class T> inline
1094 { return wc_; }
1095
1096 // Return a single eigenvector component of the w fields.
1097 template <int D, class T> inline
1098 typename T::RField const & Simulator<D,T>::wc(int a) const
1099 { return wc_[a]; }
1100
1101 // Have eigenvector components of the current c fields been computed?
1102 template <int D, class T> inline
1104 { return hasCc_; }
1105
1106 // Return all eigenvector components of the current c fields.
1107 template <int D, class T> inline
1109 { return cc_; }
1110
1111 // Return a single eigenvector component of the current c fields.
1112 template <int D, class T> inline
1113 typename T::RField const & Simulator<D,T>::cc(int a) const
1114 { return cc_[a]; }
1115
1116 // Have eigenvector components of the current d fields been computed?
1117 template <int D, class T>
1118 inline bool Simulator<D,T>::hasDc() const
1119 { return hasDc_; }
1120
1121 // Return all eigenvector components of the current d fields.
1122 template <int D, class T> inline
1124 { return dc_; }
1125
1126 // Return a single eigenvector component of the current d fields.
1127 template <int D, class T> inline
1128 typename T::RField const & Simulator<D,T>::dc(int a) const
1129 { return dc_[a]; }
1130
1131 // Return the current converged simulation step index.
1132 template <int D, class T>
1134 { return iStep_; }
1135
1136 // Return the current total simulation step index.
1137 template <int D, class T>
1139 { return iTotalStep_; }
1140
1141}
1142}
1143#endif
long iTotalStep()
Return the current simulation step index.
virtual void clearTimers()
Clear timers.
Random & random()
Get the scalar random number generator by reference.
double chiEval(int a) const
Get a single eigenvalue of the projected chi matrix.
void saveState()
Save a copy of the current system state.
RFieldT const & wc(int a) const
Get one eigenvector component of the current w fields.
T::CompressorFactory & compressorFactory()
Get the Compressor factory by reference.
virtual void simulate(int nStep)
Perform a field theoretic Monte-Carlo simulation.
T::PerturbationFactory & perturbationFactory()
Get the Perturbation factory by reference.
T::Perturbation & perturbation()
Get a Perturbation by non-const reference.
void computeHamiltonian()
Compute the Hamiltonian used in PS-FTS.
DArray< RFieldT > const & wc() const
Get all eigenvector components of the current w fields.
T::System & system()
Get the parent system by reference.
void readRamp(std::istream &in, bool &isEnd)
Optionally read a Ramp parameter file block.
double sc(int a) const
Get a single component of the S vector.
double chiEvecs(int a, int i) const
Get one element of an eigenvector of the projected chi matrix.
void clearData()
Clear field eigen-components and Hamiltonian components.
DArray< RFieldT > const & dc() const
Get all of the current d fields.
T::SimState & state()
Get the SimState stored internal state by reference.
void readPerturbation(std::istream &in, bool &isEnd)
Optionally read a Perturbation parameter file block.
T::Compressor & compressor()
Get the Compressor by non-const reference.
DArray< RFieldT > const & cc() const
Get all eigenvector components of the current c fields.
bool hasCc() const
Are eigen-components of the current c fields valid ?
DMatrix< double > const & chiEvecs() const
Get the matrix of all eigenvectors of the projected chi matrix.
void computeWc()
Compute eigenvector components of the current w fields.
void readCompressor(std::istream &in, bool &isEnd)
Optionally read a Compressor parameter file block.
bool hasDc() const
Are the current d fields valid ?
void computeCc()
Compute eigenvector components of the current c fields.
RFieldT const & cc(int a) const
Get one eigenvector component of the current c fields.
RFieldT const & dc(int i) const
Get one eigenvector component of the current d fields.
bool hasWc() const
Are eigen-components of the current w fields valid ?
void allocate()
Allocate required memory during initialization.
void readRandomSeed(std::istream &in)
Optionally read a random seed and initialize RNGs.
virtual void outputMdeCounter(std::ostream &out) const
Output MDE counter.
Simulator(typename T::System &system, typename T::Simulator &simulator)
Constructor.
double idealHamiltonian() const
Get an ideal contribution to the Hamiltonian.
void restoreState()
Restore the system to the saved state.
void setPerturbation(typename T::Perturbation *ptr)
Set the associated Perturbation.
T::VecRandom & vecRandom()
Get the vector random number generator by reference.
virtual void outputTimers(std::ostream &out) const
Output timing results.
void computeDc()
Compute functional derivatives of the Hamiltonian.
T::Ramp const & ramp() const
Get a Ramp by const reference.
bool hasPerturbation() const
Does this Simulator have a Perturbation?
T::RampFactory & rampFactory()
Get the Ramp factory by reference.
bool hasRamp() const
Does this Simulator have a Ramp?
double perturbationHamiltonian() const
Get a perturbation to the standard Hamiltonian.
typename T::RField RFieldT
Container for a real-valued periodic field.
T::Compressor const & compressor() const
Get the Compressor by const reference.
double hamiltonian() const
Get the Hamiltonian used in PS-FTS.
bool hasCompressor() const
Does this Simulator have a Compressor?
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.
T::Perturbation const & perturbation() const
Get a Perturbation by const reference.
virtual void initializeVecRandom()
Initialize the vector RNG.
DArray< double > const & sc() const
Get all components of the vector S.
T::Ramp & ramp()
Get a Ramp by non-const reference.
void clearState()
Clear the saved copy of the system state.
DArray< double > const & chiEvals() const
Get an array of the eigenvalues of the projected chi matrix.
long iStep()
Return the current converged simulation step index.
void analyzeChi()
Perform eigenvalue analysis of projected chi matrix.
bool hasHamiltonian() const
Has the Hamiltonian been computed for the current w and c fields?
double fieldHamiltonian() const
Get the quadratic field contribution to the Hamiltonian.
void setRamp(typename T::Ramp *ptr)
Set the associated Ramp.
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
Class templates for real-valued periodic fields.
PSCF package top-level namespace.
Utility classes for scientific computation.