PSCF v1.3
rpg/fts/simulator/Simulator.h
1#ifndef RPG_SIMULATOR_H
2#define RPG_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 <rpg/fts/simulator/SimState.h> // member
14#include <prdc/cuda/RField.h> // memmber (template arg)
15#include <util/random/Random.h> // member
16#include <pscf/cuda/CudaRandom.h> // member
17#include <util/containers/DArray.h> // member (template)
18#include <util/containers/DMatrix.h> // member (template)
19
20namespace Pscf {
21namespace Rpg {
22
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 Pscf::Prdc::Cuda;
33
60 template <int D>
62 {
63
64 public:
65
72
76 ~Simulator();
77
86 void allocate();
87
96 virtual void readParameters(std::istream &in);
97
100
112 virtual void simulate(int nStep);
113
130 virtual void analyze(int min, int max,
131 std::string classname,
132 std::string filename);
133
140 void clearData();
141
149 virtual void outputTimers(std::ostream& out) const;
150
159 virtual void outputMdeCounter(std::ostream& out) const;
160
166 virtual void clearTimers();
167
171 long iStep();
172
176 long iTotalStep();
177
181
188 void analyzeChi();
189
200 DArray<double> const & chiEvals() const;
201
207 double chiEval(int a) const;
208
227 DMatrix<double> const & chiEvecs() const;
228
237 double chiEvecs(int a, int i) const;
238
253 DArray<double> const & sc() const;
254
263 double sc(int a) const;
264
268
272 void computeHamiltonian();
273
281 double hamiltonian() const;
282
286 double idealHamiltonian() const;
287
291 double fieldHamiltonian() const;
292
303 double perturbationHamiltonian() const;
304
308 bool hasHamiltonian() const;
309
313
330 void computeWc();
331
339 DArray< RField<D> > const & wc() const;
340
348 RField<D> const & wc(int a) const;
349
353 bool hasWc() const;
354
358
366 void computeCc();
367
389 DArray< RField<D> > const & cc() const;
390
399 RField<D> const & cc(int a) const;
400
404 bool hasCc() const;
405
409
417 void computeDc();
418
427 DArray< RField<D> > const & dc() const;
428
434 RField<D> const & dc(int i) const;
435
439 bool hasDc() const;
440
444
456 void saveState();
457
468 void restoreState();
469
478 void clearState();
479
483
487 System<D>& system();
488
493
497 Compressor<D> const & compressor() const;
498
502 bool hasCompressor() const;
503
507 Random& random();
508
513
517 bool hasPerturbation() const;
518
522 Perturbation<D> const & perturbation() const;
523
528
532 bool hasRamp() const;
533
537 Ramp<D> const & ramp() const;
538
542 Ramp<D>& ramp();
543
545
546 protected:
547
548 // Protected member functions
549
551
557 void readRandomSeed(std::istream& in);
558
563
573 void readCompressor(std::istream& in, bool& isEnd);
574
579
589 void readPerturbation(std::istream& in, bool& isEnd);
590
597
602
612 void readRamp(std::istream& in, bool& isEnd);
613
619 void setRamp(Ramp<D>* ptr);
620
621 // Protected data members
622
627
632
640
648
656
661
666
671
676
685
689 long iStep_;
690
695
699 long seed_;
700
705
709 bool hasWc_;
710
714 bool hasCc_;
715
719 bool hasDc_;
720
721 private:
722
730 DMatrix<double> chiP_;
731
739 DMatrix<double> chiEvecs_;
740
746 DArray<double> chiEvals_;
747
756 DArray<double> sc_;
757
761 RField<D> wcs_;
762
766 System<D>* systemPtr_;
767
771 CompressorFactory<D>* compressorFactoryPtr_;
772
776 Compressor<D>* compressorPtr_;
777
781 PerturbationFactory<D>* perturbationFactoryPtr_;
782
786 Perturbation<D>* perturbationPtr_;
787
791 RampFactory<D>* rampFactoryPtr_;
792
796 Ramp<D>* rampPtr_;
797
801 bool isAllocated_;
802
803 };
804
805 // Inline functions
806
807 // Get the parent System.
808 template <int D>
810 {
811 UTIL_CHECK(systemPtr_);
812 return *systemPtr_;
813 }
814
815 // Get the CPU random number generator.
816 template <int D>
818 { return random_; }
819
820 // Get the GPU random number generator.
821 template <int D>
824
825 // Does this Simulator have a Compressor?
826 template <int D>
827 inline bool Simulator<D>::hasCompressor() const
828 { return (bool)compressorPtr_; }
829
830 // Get the Compressor by non-const reference.
831 template <int D>
833 {
834 UTIL_CHECK(compressorPtr_);
835 return *compressorPtr_;
836 }
837
838 // Get the Compressor by const reference.
839 template <int D>
841 {
842 UTIL_CHECK(compressorPtr_);
843 return *compressorPtr_;
844 }
845
846 // Get the Compressor factory.
847 template <int D>
849 {
850 UTIL_CHECK(compressorFactoryPtr_);
851 return *compressorFactoryPtr_;
852 }
853
854 // Does this Simulator have an associated Perturbation?
855 template <int D>
857 { return (bool)perturbationPtr_; }
858
859 // Get the perturbation (if any) by const reference.
860 template <int D>
862 {
863 UTIL_CHECK(perturbationPtr_);
864 return *perturbationPtr_;
865 }
866
867 // Get the perturbation (if any) by non-const reference.
868 template <int D>
870 {
871 UTIL_CHECK(perturbationPtr_);
872 return *perturbationPtr_;
873 }
874
875 // Get the perturbation factory.
876 template <int D>
878 {
879 UTIL_CHECK(perturbationFactoryPtr_);
880 return *perturbationFactoryPtr_;
881 }
882
883 // Does this Simulator have an associated Ramp?
884 template <int D>
885 inline bool Simulator<D>::hasRamp() const
886 { return (bool)rampPtr_; }
887
888 // Get the ramp by const reference.
889 template <int D>
890 inline Ramp<D> const & Simulator<D>::ramp() const
891 {
892 UTIL_CHECK(rampPtr_);
893 return *rampPtr_;
894 }
895
896 // Get the ramp by non-const reference.
897 template <int D>
899 {
900 UTIL_CHECK(rampPtr_);
901 return *rampPtr_;
902 }
903
904 // Get the ramp factory.
905 template <int D>
907 {
908 UTIL_CHECK(rampFactoryPtr_);
909 return *rampFactoryPtr_;
910 }
911
912 // Return an array of eigenvalues of projected chi matrix.
913 template <int D>
914 inline double Simulator<D>::chiEval(int a) const
915 { return chiEvals_[a]; }
916
917 // Return an array of eigenvalues of projected chi matrix.
918 template <int D>
920 { return chiEvals_; }
921
922 // Return a matrix of eigenvectors of the projected chi matrix.
923 template <int D>
925 { return chiEvecs_; }
926
927 // Return a matrix of eigenvectors of the projected chi matrix.
928 template <int D>
929 inline double Simulator<D>::chiEvecs(int a, int i) const
930 { return chiEvecs_(a, i); }
931
932 // Return array of values of vector S.
933 template <int D>
934 inline DArray<double> const & Simulator<D>::sc() const
935 { return sc_; }
936
937 // Return one component of vector S.
938 template <int D>
939 inline double Simulator<D>::sc(int a) const
940 { return sc_[a]; }
941
942 // Has the Hamiltonian been computed for the current w fields ?
943 template <int D>
945 { return hasHamiltonian_; }
946
947 // Get the precomputed Hamiltonian
948 template <int D>
949 inline double Simulator<D>::hamiltonian() const
950 {
952 return hamiltonian_;
953 }
954
955 // Get the ideal gas component of the precomputed Hamiltonian
956 template <int D>
957 inline double Simulator<D>::idealHamiltonian() const
958 {
960 return idealHamiltonian_;
961 }
962
963 // Get the W field component of the precomputed Hamiltonian.
964 template <int D>
965 inline double Simulator<D>::fieldHamiltonian() const
966 {
968 return fieldHamiltonian_;
969 }
970
971 // Get the perturbation component of the precomputed Hamiltonian.
972 template <int D>
978
979 // Return all eigencomponents of the w fields.
980 template <int D>
981 inline DArray< RField<D> > const & Simulator<D>::wc() const
982 { return wc_; }
983
984 // Return a single eigenvector component of the w fields.
985 template <int D>
986 inline RField<D> const & Simulator<D>::wc(int a) const
987 { return wc_[a]; }
988
989 // Have eigenvector components of current w fields been computed?
990 template <int D>
991 inline bool Simulator<D>::hasWc() const
992 { return hasWc_; }
993
994 // Return all eigenvector components of the current c fields.
995 template <int D>
996 inline DArray< RField<D> > const & Simulator<D>::cc() const
997 { return cc_; }
998
999 // Return a single eigenvector component of the current c fields.
1000 template <int D>
1001 inline RField<D> const & Simulator<D>::cc(int a) const
1002 { return cc_[a]; }
1003
1004 // Have eigenvector components of current c fields been computed?
1005 template <int D>
1006 inline bool Simulator<D>::hasCc() const
1007 { return hasCc_; }
1008
1009 // Return all eigenvector components of the current d fields.
1010 template <int D>
1011 inline DArray< RField<D> > const & Simulator<D>::dc() const
1012 { return dc_; }
1013
1014 // Return a single eigenvector component of the current d fields.
1015 template <int D>
1016 inline RField<D> const & Simulator<D>::dc(int a) const
1017 { return dc_[a]; }
1018
1019 // Have eigenvector components of current d fields been computed?
1020 template <int D>
1021 inline bool Simulator<D>::hasDc() const
1022 { return hasDc_; }
1023
1024 // Clear all data (eigen-components of w field and Hamiltonian)
1025 template <int D>
1027 {
1028 hasHamiltonian_ = false;
1029 hasWc_ = false;
1030 hasCc_ = false;
1031 hasDc_ = false;
1032 }
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 RPG_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
Random number generator on GPU.
Definition CudaRandom.h:30
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).
void readRandomSeed(std::istream &in)
Read random seed and initialize random number generators.
long iStep()
Return the current converged simulation step index.
PerturbationFactory< D > & perturbationFactory()
Get the perturbation factory by reference.
bool hasPerturbation() const
Does this Simulator have a Perturbation?
long iTotalStep()
Return the current simulation step index.
Random random_
Random number generator.
System< D > & system()
Get parent system by reference.
void computeWc()
Compute eigenvector components of the current w fields.
CudaRandom & cudaRandom()
Get cuda random number generator by reference.
void readRamp(std::istream &in, bool &isEnd)
Optionally read an associated ramp.
void computeCc()
Compute eigenvector components of the current c fields.
void clearData()
Clear field eigen-components and hamiltonian components.
DArray< RField< D > > dc_
Components of d fields on a real space grid.
DMatrix< double > const & chiEvecs() const
Get the matrix of all eigenvectors of the projected chi matrix.
bool hasHamiltonian_
Has the Hamiltonian been computed for the current w and c fields?
bool hasWc_
Have eigen-components of the current w fields been computed ?
bool hasCc() const
Are eigen-components of current c fields valid ?
void setPerturbation(Perturbation< D > *ptr)
Set the associated perturbation.
void computeDc()
Compute functional derivatives of the Hamiltonian.
SimState< D > state_
State saved during fts simulation.
bool hasHamiltonian() const
Has the MC Hamiltonian been computed for current w and c fields?
double perturbationHamiltonian_
Perturbation to the standard Hamiltonian (if any).
long iTotalStep_
Simulation step counter.
virtual void analyze(int min, int max, std::string classname, std::string filename)
Read and analyze a trajectory file.
Random & random()
Get random number generator by reference.
bool hasDc_
Have functional derivatives of H[W] been computed ?
void allocate()
Allocate required memory.
DArray< RField< D > > cc_
Eigenvector components of c fields on a real space grid.
void saveState()
Save a copy of the fts move state.
void restoreState()
Restore the saved copy of the fts move state.
void readPerturbation(std::istream &in, bool &isEnd)
Optionally read an associated perturbation.
CompressorFactory< D > & compressorFactory()
Get the compressor factory by reference.
void readCompressor(std::istream &in, bool &isEnd)
Read the compressor block of the parameter file.
virtual void simulate(int nStep)
Perform a field theoretic Monte-Carlo simulation.
DArray< double > const & sc() const
Get all components of the vector S.
Ramp< D > const & ramp() const
Get the associated Ramp by const reference.
DArray< RField< D > > const & wc() const
Get all eigenvector components of the current w fields.
virtual void outputMdeCounter(std::ostream &out) const
Output MDE counter.
double hamiltonian() const
Get the Hamiltonian used in field theoretic simulations.
bool hasCc_
Have eigen-components of the current c fields been computed ?
CudaRandom cudaRandom_
Random number generator.
bool hasRamp() const
Does this Simulator have a Ramp?
virtual void readParameters(std::istream &in)
Read parameters for a simulation.
Simulator(System< D > &system)
Constructor.
double fieldHamiltonian_
Field contribution (H_W) to Hamiltonian.
void analyzeChi()
Perform eigenvalue analysis of projected chi matrix.
DArray< double > const & chiEvals() const
Get an array of the eigenvalues of the projected chi matrix.
Compressor< D > & compressor()
Get the compressor by non-const reference.
bool hasCompressor() const
Does this Simulator have a Compressor object?
bool hasWc() const
Are eigen-components of current w fields valid ?
double chiEval(int a) const
Get a single eigenvalue of the projected chi matrix.
Perturbation< D > const & perturbation() const
Get the associated Perturbation by const reference.
bool hasDc() const
Are the current d fields valid ?
double idealHamiltonian_
Ideal gas contribution (lnQ) to Hamiltonian H[W].
void setRamp(Ramp< D > *ptr)
Set the associated ramp.
double fieldHamiltonian() const
Get the quadratic field contribution (HW) to MC Hamiltonian.
void clearState()
Clear the saved copy of the fts state.
long seed_
Random number generator seed.
long iStep_
Simulation step counter.
virtual void clearTimers()
Clear timers.
DArray< RField< D > > const & dc() const
Get all of the current d fields.
double hamiltonian_
Field theoretic Hamiltonian H[W] (extensive value).
RampFactory< D > & rampFactory()
Get the ramp factory by reference.
DArray< RField< D > > const & cc() const
Get all eigenvector components of the current c fields.
double idealHamiltonian() const
Get ideal gas contribution (-lnQ) to MC Hamiltonian.
DArray< RField< D > > wc_
Eigenvector components of w fields on a real space grid.
virtual void outputTimers(std::ostream &out) const
Output timing results.
double perturbationHamiltonian() const
Get the perturbation to the standard Hamiltonian (if any).
void computeHamiltonian()
Compute the Hamiltonian used in field theoretic simulations.
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
Fields, FFTs, and utilities for periodic boundary conditions (CUDA)
Definition Reduce.cpp:14
SCFT and PS-FTS with real periodic fields (GPU)
PSCF package top-level namespace.
Definition param_pc.dox:1
SimState stores the state used by an fts simulation.