PSCF v1.3
rpc/fts/simulator/Simulator.h
1#ifndef RPC_SIMULATOR_H
2#define RPC_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
13#include <rpc/fts/simulator/SimState.h> // member
14#include <prdc/cpu/RField.h> // member (template arg)
15#include <util/random/Random.h> // member
16#include <util/containers/DArray.h> // member (template)
17#include <util/containers/DMatrix.h> // member (template)
18
19namespace Pscf {
20namespace Rpc {
21
22 // Forward declarations
23 template <int D> class System;
24 template <int D> class Compressor;
25 template <int D> class CompressorFactory;
26 template <int D> class Perturbation;
27 template <int D> class PerturbationFactory;
28 template <int D> class Ramp;
29 template <int D> class RampFactory;
30
31 using namespace Util;
32 using namespace Prdc;
33 using namespace Prdc::Cpu;
34
67 template <int D>
68 class Simulator : public ParamComposite
69 {
70
71 public:
72
73 Simulator() = delete;
74
80 Simulator(System<D>& system);
81
85 ~Simulator();
86
95 void allocate();
96
109 virtual void readParameters(std::istream &in);
110
113
125 virtual void simulate(int nStep);
126
143 virtual void analyze(int min, int max,
144 std::string classname,
145 std::string filename);
146
153 void clearData();
154
158
166 virtual void outputTimers(std::ostream& out) const;
167
176 virtual void outputMdeCounter(std::ostream& out) const;
177
183 virtual void clearTimers();
184
188 long iStep();
189
193 long iTotalStep();
194
198
205 void analyzeChi();
206
217 DArray<double> const & chiEvals() const;
218
224 double chiEval(int a) const;
225
244 DMatrix<double> const & chiEvecs() const;
245
254 double chiEvecs(int a, int i) const;
255
270 DArray<double> const & sc() const;
271
280 double sc(int a) const;
281
285
289 void computeHamiltonian();
290
298 double hamiltonian() const;
299
303 double idealHamiltonian() const;
304
308 double fieldHamiltonian() const;
309
320 double perturbationHamiltonian() const;
321
325 bool hasHamiltonian() const;
326
330
347 void computeWc();
348
356 DArray< RField<D> > const & wc() const;
357
365 RField<D> const & wc(int a) const;
366
370 bool hasWc() const;
371
375
383 void computeCc();
384
406 DArray< RField<D> > const & cc() const;
407
416 RField<D> const & cc(int a) const;
417
421 bool hasCc() const;
422
426
434 void computeDc();
435
444 DArray< RField<D> > const & dc() const;
445
451 RField<D> const & dc(int i) const;
452
456 bool hasDc() const;
457
461
473 void saveState();
474
485 void restoreState();
486
495 void clearState();
496
500
504 System<D>& system();
505
509 Random& random();
510
514 bool hasCompressor() const;
515
520
524 Compressor<D> const & compressor() const;
525
529 bool hasPerturbation() const;
530
535
539 Perturbation<D> const & perturbation() const;
540
544 bool hasRamp() const;
545
549 Ramp<D> const & ramp() const;
550
554 Ramp<D>& ramp();
555
557
558 protected:
559
560 // Protected member functions
561
563
569 void readRandomSeed(std::istream& in);
570
575
584 void readCompressor(std::istream& in, bool& isEnd);
585
590
599 void readPerturbation(std::istream& in, bool& isEnd);
600
607
612
621 void readRamp(std::istream& in, bool& isEnd);
622
628 void setRamp(Ramp<D>* ptr);
629
630 // Protected data members
631
636
647
658
666
674
679
684
689
698
710 long iStep_;
711
716
720 long seed_;
721
722 // Boolean status indicators
723
728
732 bool hasWc_;
733
737 bool hasCc_;
738
742 bool hasDc_;
743
744 private:
745
753 DMatrix<double> chiP_;
754
764 DMatrix<double> chiEvecs_;
765
771 DArray<double> chiEvals_;
772
781 DArray<double> sc_;
782
786 System<D>* systemPtr_;
787
791 CompressorFactory<D>* compressorFactoryPtr_;
792
796 Compressor<D>* compressorPtr_;
797
801 PerturbationFactory<D>* perturbationFactoryPtr_;
802
806 Perturbation<D>* perturbationPtr_;
807
811 RampFactory<D>* rampFactoryPtr_;
812
816 Ramp<D>* rampPtr_;
817
821 bool isAllocated_;
822
823 };
824
825 // Inline functions
826
827 // Management of owned and associated objects
828
829 // Get the parent System by reference.
830 template <int D>
832 {
833 UTIL_ASSERT(systemPtr_);
834 return *systemPtr_;
835 }
836
837 // Get the random number generator by reference.
838 template <int D>
840 { return random_; }
841
842 // Get the compressor factory by reference.
843 template <int D>
845 {
846 UTIL_CHECK(compressorFactoryPtr_);
847 return *compressorFactoryPtr_;
848 }
849
850 // Does this Simulator have an associated Compressor?
851 template <int D>
852 inline bool Simulator<D>::hasCompressor() const
853 { return (bool)compressorPtr_; }
854
855 // Get the Compressor by non-const reference.
856 template <int D>
858 {
860 return *compressorPtr_;
861 }
862
863 // Get the Compressor by const reference.
864 template <int D>
866 {
868 return *compressorPtr_;
869 }
870
871 // Does this Simulator have an associated Perturbation?
872 template <int D>
874 { return (bool)perturbationPtr_; }
875
876 // Get the perturbation (if any) by const reference.
877 template <int D>
879 {
880 UTIL_CHECK(perturbationPtr_);
881 return *perturbationPtr_;
882 }
883
884 // Get the perturbation (if any) by non-const reference.
885 template <int D>
887 {
888 UTIL_CHECK(perturbationPtr_);
889 return *perturbationPtr_;
890 }
891
892 // Get the perturbation factory by reference.
893 template <int D>
895 {
896 UTIL_CHECK(perturbationFactoryPtr_);
897 return *perturbationFactoryPtr_;
898 }
899
900 // Does this Simulator have an associated Ramp?
901 template <int D>
902 inline bool Simulator<D>::hasRamp() const
903 { return (bool)rampPtr_; }
904
905 // Get the ramp (if any) by const reference.
906 template <int D>
907 inline Ramp<D> const & Simulator<D>::ramp() const
908 {
909 UTIL_CHECK(rampPtr_);
910 return *rampPtr_;
911 }
912
913 // Get the ramp (if any) by non-const reference.
914 template <int D>
916 {
917 UTIL_CHECK(rampPtr_);
918 return *rampPtr_;
919 }
920
921 // Get the ramp factory.
922 template <int D>
924 {
925 UTIL_CHECK(rampFactoryPtr_);
926 return *rampFactoryPtr_;
927 }
928
929 // Projected Chi Matrix
930
931 // Return an array of eigenvalues of the projected chi matrix.
932 template <int D>
934 { return chiEvals_; }
935
936 // Return a single eigenvalue of the projected chi matrix.
937 template <int D>
938 inline double Simulator<D>::chiEval(int a) const
939 { return chiEvals_[a]; }
940
941 // Return a matrix of eigenvectors of the projected chi matrix.
942 template <int D>
944 { return chiEvecs_; }
945
946 // Return an element of an eigenvector of the projected chi matrix.
947 template <int D>
948 inline double Simulator<D>::chiEvecs(int a, int i) const
949 { return chiEvecs_(a, i); }
950
951 // Return the vector S in eigenvector basis.
952 template <int D>
953 inline DArray<double> const & Simulator<D>::sc() const
954 { return sc_; }
955
956 // Return one component of vector S in an eigenvector basis.
957 template <int D>
958 inline double Simulator<D>::sc(int a) const
959 { return sc_[a]; }
960
961 // Hamiltonian and its derivatives
962
963 // Get the precomputed Hamiltonian.
964 template <int D>
965 inline double Simulator<D>::hamiltonian() const
966 {
968 return hamiltonian_;
969 }
970
971 // Get the ideal gas component of the precomputed Hamiltonian.
972 template <int D>
973 inline double Simulator<D>::idealHamiltonian() const
974 {
976 return idealHamiltonian_;
977 }
978
979 // Get the W field component of the precomputed Hamiltonian.
980 template <int D>
981 inline double Simulator<D>::fieldHamiltonian() const
982 {
984 return fieldHamiltonian_;
985 }
986
987 // Get the perturbation component of the precomputed Hamiltonian.
988 template <int D>
994
995 // Has the Hamiltonian been computed for the current w fields ?
996 template <int D>
998 { return hasHamiltonian_; }
999
1000 // Fields
1001
1002 // Return all eigencomponents of the w fields.
1003 template <int D>
1004 inline DArray< RField<D> > const & Simulator<D>::wc() const
1005 { return wc_; }
1006
1007 // Return a single eigenvector component of the w fields.
1008 template <int D>
1009 inline RField<D> const & Simulator<D>::wc(int a) const
1010 { return wc_[a]; }
1011
1012 // Have eigenvector components of current w fields been computed?
1013 template <int D>
1014 inline bool Simulator<D>::hasWc() const
1015 { return hasWc_; }
1016
1017 // Return all eigenvector components of the current c fields.
1018 template <int D>
1019 inline DArray< RField<D> > const & Simulator<D>::cc() const
1020 { return cc_; }
1021
1022 // Return a single eigenvector component of the current c fields.
1023 template <int D>
1024 inline RField<D> const & Simulator<D>::cc(int a) const
1025 { return cc_[a]; }
1026
1027 // Have eigenvector components of current c fields been computed?
1028 template <int D>
1029 inline bool Simulator<D>::hasCc() const
1030 { return hasCc_; }
1031
1032 // Return all eigenvector components of the current d fields.
1033 template <int D>
1034 inline DArray< RField<D> > const & Simulator<D>::dc() const
1035 { return dc_; }
1036
1037 // Return a single eigenvector component of the current d fields.
1038 template <int D>
1039 inline RField<D> const & Simulator<D>::dc(int a) const
1040 { return dc_[a]; }
1041
1042 // Have eigenvector components of current d fields been computed?
1043 template <int D>
1044 inline bool Simulator<D>::hasDc() const
1045 { return hasDc_; }
1046
1047 // Return the current converged simulation step index.
1048 template <int D>
1050 { return iStep_; }
1051
1052 // Return the current simulation step index.
1053 template <int D>
1055 { return iTotalStep_; }
1056
1057 #ifndef RPC_SIMULATOR_TPP
1058 // Suppress implicit instantiation
1059 extern template class Simulator<1>;
1060 extern template class Simulator<2>;
1061 extern template class Simulator<3>;
1062 #endif
1063
1064}
1065}
1066#endif
Field of real double precision values on an FFT mesh.
Definition cpu/RField.h:29
Factory for subclasses of Compressor.
Base class for iterators that impose incompressibility.
Factory for subclasses of Perturbation.
Base class for additive perturbations of standard FTS Hamiltonian.
Factory for subclasses of Ramp.
Class that varies parameters during a simulation (abstract).
Field theoretic simulator (base class).
bool hasCc_
Have eigen-components of the current c fields been computed ?
Ramp< D > const & ramp() const
Get the associated Ramp by const reference.
void readPerturbation(std::istream &in, bool &isEnd)
Optionally read an associated perturbation.
Perturbation< D > & perturbation()
Get the perturbation factory by non-const reference.
System< D > & system()
Get parent system by reference.
bool hasWc_
Have eigen-components of the current w fields been computed ?
void computeDc()
Compute functional derivatives of the Hamiltonian.
SimState< D > state_
Previous state saved during at the beginning of a step.
bool hasHamiltonian_
Has the Hamiltonian been computed for the current w and c fields?
virtual void analyze(int min, int max, std::string classname, std::string filename)
Read and analyze a trajectory file.
long iTotalStep()
Return the current simulation step index.
double perturbationHamiltonian() const
Get the perturbation to the standard Hamiltonian (if any).
void clearState()
Clear the saved copy of the fts state.
bool hasRamp() const
Does this Simulator have a Ramp?
PerturbationFactory< D > & perturbationFactory()
Get the perturbation factory by reference.
void computeWc()
Compute eigenvector components of the current w fields.
DArray< RField< D > > const & cc() const
Get all eigenvector components of the current c fields.
void setPerturbation(Perturbation< D > *ptr)
Set the associated perturbation.
double chiEval(int a) const
Get a single eigenvalue of the projected chi matrix.
void clearData()
Clear field eigen-components and hamiltonian components.
Compressor< D > & compressor()
Get the compressor by non-const reference.
bool hasDc() const
Are the current d fields valid ?
void analyzeChi()
Perform eigenvalue analysis of projected chi matrix.
double fieldHamiltonian_
Field contribution (H_W) to Hamiltonian.
DArray< double > const & sc() const
Get all components of the vector S.
long iStep()
Return the current converged simulation step index.
virtual void outputMdeCounter(std::ostream &out) const
Output MDE counter.
long seed_
Random number generator seed.
virtual void readParameters(std::istream &in)
Read parameters for a simulation.
void allocate()
Allocate required memory.
DMatrix< double > const & chiEvecs() const
Get the matrix of all eigenvectors of the projected chi matrix.
double idealHamiltonian_
Ideal gas contribution (-lnQ) to Hamiltonian H[W].
virtual void simulate(int nStep)
Perform a field theoretic Monte-Carlo simulation.
void setRamp(Ramp< D > *ptr)
Set the associated ramp.
DArray< RField< D > > wc_
Eigenvector components of w fields on a real space grid.
DArray< RField< D > > dc_
Components of d fields on a real space grid.
bool hasCc() const
Are eigen-components of current c fields valid ?
RampFactory< D > & rampFactory()
Get the ramp factory by reference.
double hamiltonian_
Total field theoretic Hamiltonian H[W] (extensive value).
double perturbationHamiltonian_
Perturbation to the standard Hamiltonian (if any).
virtual void clearTimers()
Clear timers.
bool hasWc() const
Are eigen-components of current w fields valid ?
CompressorFactory< D > & compressorFactory()
Get the compressor factory by reference.
bool hasDc_
Have functional derivatives of H[W] been computed ?
void restoreState()
Restore the saved copy of the fts move state.
void saveState()
Save a copy of the fts move state.
long iTotalStep_
Step counter - total number of attempted BD or MC steps.
double hamiltonian() const
Get the Hamiltonian used in PS-FTS.
bool hasCompressor() const
Does this Simulator have a Compressor?
void readRandomSeed(std::istream &in)
Optionally read a random number generator seed.
bool hasPerturbation() const
Does this Simulator have a Perturbation?
DArray< RField< D > > cc_
Eigenvector components of c fields on a real space grid.
virtual void outputTimers(std::ostream &out) const
Output timing results.
void readCompressor(std::istream &in, bool &isEnd)
Read the compressor block of the parameter file.
double fieldHamiltonian() const
Get the quadratic field contribution to the Hamiltonian.
long iStep_
Step counter - attempted steps for which compressor converges.
void readRamp(std::istream &in, bool &isEnd)
Optionally read an associated ramp.
DArray< double > const & chiEvals() const
Get an array of the eigenvalues of the projected chi matrix.
bool hasHamiltonian() const
Has the Hamiltonian been computed for current w and c fields?
DArray< RField< D > > const & dc() const
Get all of the current d fields.
Random random_
Random number generator.
DArray< RField< D > > const & wc() const
Get all eigenvector components of the current w fields.
void computeHamiltonian()
Compute the Hamiltonian used in PS-FTS.
double idealHamiltonian() const
Get ideal gas contribution to the Hamiltonian.
Random & random()
Get random number generator by reference.
void computeCc()
Compute eigenvector components of the current c fields.
Main class, representing one complete system.
Dynamically allocatable contiguous array template.
Definition DArray.h:32
Dynamically allocated Matrix.
Definition DMatrix.h:25
void setClassName(const char *className)
Set class name string.
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
Periodic fields and crystallography.
Definition CField.cpp:11
Real periodic fields, SCFT and PS-FTS (CPU).
Definition param_pc.dox:2
PSCF package top-level namespace.
Definition param_pc.dox:1
SimState stores the state used by an FTS simulation.