Loading [MathJax]/extensions/TeX/AMSsymbols.js
PSCF v1.2
rpc/fts/simulator/Simulator.h
1#ifndef RPC_SIMULATOR_H
2#define RPC_SIMULATOR_H
3
4/*
5* PSCF - Polymer Self-Consistent Field Theory
6*
7* Copyright 2016 - 2022, 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
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);
167
176 virtual void outputMdeCounter(std::ostream& out);
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 bool hasPerturbation() const;
525
529 Perturbation<D> const & perturbation() const;
530
535
539 bool hasRamp() const;
540
544 Ramp<D> const & ramp() const;
545
549 Ramp<D>& ramp();
550
552
553 protected:
554
555 // Protected member functions
556
558
564 void readRandomSeed(std::istream& in);
565
570
579 void readCompressor(std::istream& in, bool& isEnd);
580
585
594 void readPerturbation(std::istream& in, bool& isEnd);
595
602
607
616 void readRamp(std::istream& in, bool& isEnd);
617
623 void setRamp(Ramp<D>* ptr);
624
625 // Protected data members
626
631
642
653
661
669
674
679
684
693
705 long iStep_;
706
711
715 long seed_;
716
717 // Boolean status indicators
718
723
727 bool hasWc_;
728
732 bool hasCc_;
733
737 bool hasDc_;
738
739 private:
740
748 DMatrix<double> chiP_;
749
759 DMatrix<double> chiEvecs_;
760
766 DArray<double> chiEvals_;
767
776 DArray<double> sc_;
777
781 System<D>* systemPtr_;
782
786 CompressorFactory<D>* compressorFactoryPtr_;
787
791 Compressor<D>* compressorPtr_;
792
796 PerturbationFactory<D>* perturbationFactoryPtr_;
797
801 Perturbation<D>* perturbationPtr_;
802
806 RampFactory<D>* rampFactoryPtr_;
807
811 Ramp<D>* rampPtr_;
812
816 bool isAllocated_;
817
818 };
819
820 // Inline functions
821
822 // Management of owned and associated objects
823
824 // Get the parent System by reference.
825 template <int D>
827 {
828 assert(systemPtr_);
829 return *systemPtr_;
830 }
831
832 // Get the random number generator by reference.
833 template <int D>
835 { return random_; }
836
837 // Get the compressor factory by reference.
838 template <int D>
840 {
841 UTIL_CHECK(compressorFactoryPtr_);
842 return *compressorFactoryPtr_;
843 }
844
845 // Does this Simulator have an associated Compressor?
846 template <int D>
847 inline bool Simulator<D>::hasCompressor() const
848 { return (bool)compressorPtr_; }
849
850 // Get the Compressor by reference.
851 template <int D>
853 {
854 UTIL_CHECK(hasCompressor());
855 return *compressorPtr_;
856 }
857
858 // Does this Simulator have an associated Perturbation?
859 template <int D>
861 { return (bool)perturbationPtr_; }
862
863 // Get the perturbation (if any) by const reference.
864 template <int D>
866 {
867 UTIL_CHECK(perturbationPtr_);
868 return *perturbationPtr_;
869 }
870
871 // Get the perturbation (if any) by non-const reference.
872 template <int D>
874 {
875 UTIL_CHECK(perturbationPtr_);
876 return *perturbationPtr_;
877 }
878
879 // Get the perturbation factory by reference.
880 template <int D>
882 {
883 UTIL_CHECK(perturbationFactoryPtr_);
884 return *perturbationFactoryPtr_;
885 }
886
887 // Does this Simulator have an associated Ramp?
888 template <int D>
889 inline bool Simulator<D>::hasRamp() const
890 { return (bool)rampPtr_; }
891
892 // Get the ramp (if any) by const reference.
893 template <int D>
894 inline Ramp<D> const & Simulator<D>::ramp() const
895 {
896 UTIL_CHECK(rampPtr_);
897 return *rampPtr_;
898 }
899
900 // Get the ramp (if any) by non-const reference.
901 template <int D>
903 {
904 UTIL_CHECK(rampPtr_);
905 return *rampPtr_;
906 }
907
908 // Get the ramp factory.
909 template <int D>
911 {
912 UTIL_CHECK(rampFactoryPtr_);
913 return *rampFactoryPtr_;
914 }
915
916 // Projected Chi Matrix
917
918 // Return an array of eigenvalues of the projected chi matrix.
919 template <int D>
921 { return chiEvals_; }
922
923 // Return a single eigenvalue of the projected chi matrix.
924 template <int D>
925 inline double Simulator<D>::chiEval(int a) const
926 { return chiEvals_[a]; }
927
928 // Return a matrix of eigenvectors of the projected chi matrix.
929 template <int D>
931 { return chiEvecs_; }
932
933 // Return an element of an eigenvector of the projected chi matrix.
934 template <int D>
935 inline double Simulator<D>::chiEvecs(int a, int i) const
936 { return chiEvecs_(a, i); }
937
938 // Return the vector S in eigenvector basis.
939 template <int D>
940 inline DArray<double> const & Simulator<D>::sc() const
941 { return sc_; }
942
943 // Return one component of vector S in an eigenvector basis.
944 template <int D>
945 inline double Simulator<D>::sc(int a) const
946 { return sc_[a]; }
947
948 // Hamiltonian and its derivatives
949
950 // Get the precomputed Hamiltonian.
951 template <int D>
952 inline double Simulator<D>::hamiltonian() const
953 {
954 UTIL_CHECK(hasHamiltonian_);
955 return hamiltonian_;
956 }
957
958 // Get the ideal gas component of the precomputed Hamiltonian.
959 template <int D>
960 inline double Simulator<D>::idealHamiltonian() const
961 {
962 UTIL_CHECK(hasHamiltonian_);
963 return idealHamiltonian_;
964 }
965
966 // Get the W field component of the precomputed Hamiltonian.
967 template <int D>
968 inline double Simulator<D>::fieldHamiltonian() const
969 {
970 UTIL_CHECK(hasHamiltonian_);
971 return fieldHamiltonian_;
972 }
973
974 // Get the perturbation component of the precomputed Hamiltonian.
975 template <int D>
977 {
978 UTIL_CHECK(hasHamiltonian_);
979 return perturbationHamiltonian_;
980 }
981
982 // Has the Hamiltonian been computed for the current w fields ?
983 template <int D>
985 { return hasHamiltonian_; }
986
987 // Fields
988
989 // Return all eigencomponents of the w fields.
990 template <int D>
991 inline DArray< RField<D> > const & Simulator<D>::wc() const
992 { return wc_; }
993
994 // Return a single eigenvector component of the w fields.
995 template <int D>
996 inline RField<D> const & Simulator<D>::wc(int a) const
997 { return wc_[a]; }
998
999 // Have eigenvector components of current w fields been computed?
1000 template <int D>
1001 inline bool Simulator<D>::hasWc() const
1002 { return hasWc_; }
1003
1004 // Return all eigenvector components of the current c fields.
1005 template <int D>
1006 inline DArray< RField<D> > const & Simulator<D>::cc() const
1007 { return cc_; }
1008
1009 // Return a single eigenvector component of the current c fields.
1010 template <int D>
1011 inline RField<D> const & Simulator<D>::cc(int a) const
1012 { return cc_[a]; }
1013
1014 // Have eigenvector components of current c fields been computed?
1015 template <int D>
1016 inline bool Simulator<D>::hasCc() const
1017 { return hasCc_; }
1018
1019 // Return all eigenvector components of the current d fields.
1020 template <int D>
1021 inline DArray< RField<D> > const & Simulator<D>::dc() const
1022 { return dc_; }
1023
1024 // Return a single eigenvector component of the current d fields.
1025 template <int D>
1026 inline RField<D> const & Simulator<D>::dc(int a) const
1027 { return dc_[a]; }
1028
1029 // Have eigenvector components of current d fields been computed?
1030 template <int D>
1031 inline bool Simulator<D>::hasDc() const
1032 { return hasDc_; }
1033
1034 // Return the current converged simulation step index.
1035 template <int D>
1037 { return iStep_; }
1038
1039 // Return the current simulation step index.
1040 template <int D>
1042 { return iTotalStep_; }
1043
1044 #ifndef RPC_SIMULATOR_TPP
1045 // Suppress implicit instantiation
1046 extern template class Simulator<1>;
1047 extern template class Simulator<2>;
1048 extern template class Simulator<3>;
1049 #endif
1050
1051}
1052}
1053#endif
Field of real double precision values on an FFT mesh.
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).
Definition rpc/System.h:38
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.
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 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.
virtual void outputTimers(std::ostream &out)
Output timing results.
long iStep()
Return the current converged simulation step index.
virtual void outputMdeCounter(std::ostream &out)
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.
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.
Perturbation< D > const & perturbation() const
Get the associated Perturbation by const reference.
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 for SCFT or PS-FTS simulation of one system.
Definition rpc/System.h:100
Dynamically allocatable contiguous array template.
Dynamically allocated Matrix.
Definition DMatrix.h:25
An object that can read multiple parameters from file.
void setClassName(const char *className)
Set class name string.
Random number generator.
Definition Random.h:47
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition global.h:68
PSCF package top-level namespace.
Definition param_pc.dox:1
Utility classes for scientific computation.
SimState stores the state used by an FTS simulation.