PSCF v1.4.0
cpc/fts/Simulator.h
1#ifndef CPC_SIMULATOR_H
2#define CPC_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 <prdc/cpu/CField.h> // member (template arg)
13#include <util/random/Random.h> // member
14#include <util/containers/DArray.h> // member (template)
15#include <util/containers/DMatrix.h> // member (template)
16
17
18namespace Pscf {
19namespace Cpc {
20
21 // Forward declarations
22 template <int D> class System;
23 template <int D> class Step;
24 // template <int D> class TrajectoryReader;
25
26 using namespace Util;
27 using namespace Prdc;
28 using namespace Prdc::Cpu;
29
49 template <int D>
50 class Simulator : public ParamComposite
51 {
52
53 public:
54
55 // Suppress automatically generated functions
56 Simulator() = delete;
57 Simulator(Simulator<D>& ) = delete;
58
64 Simulator(System<D>& system);
65
69 ~Simulator();
70
79 void allocate();
80
86 virtual void readParameters(std::istream &in);
87
90
101 virtual void simulate(int nStep);
102
103 #if 0
120 virtual void analyze(int min, int max,
121 std::string classname,
122 std::string filename);
123 #endif
124
131 void clearData();
132
136
144 virtual void outputTimers(std::ostream& out) const;
145
151 virtual void clearTimers();
152
156 long iStep();
157
161 long iTotalStep();
162
166
173 void analyzeInteraction();
174
178 DMatrix<double> const & u() const;
179
183 DArray<double> const & evals() const;
184
190 double eval(int i) const;
191
197 bool isPositiveEval(int i) const;
198
217 DMatrix<double> const & evecs() const;
218
227 double evecs(int a, int i) const;
228
229 #if 0
244 DArray<double> const & sc() const;
245
254 double sc(int a) const;
255 #endif
256
260
264 void computeHamiltonian();
265
273 std::complex<double> hamiltonian() const;
274
278 std::complex<double> idealHamiltonian() const;
279
283 std::complex<double> fieldHamiltonian() const;
284
288 bool hasHamiltonian() const;
289
293
310 void computeWc();
311
319 DArray< CField<D> > const & wc() const;
320
328 CField<D> const & wc(int a) const;
329
333 bool hasWc() const;
334
338
346 void computeCc();
347
369 DArray< CField<D> > const & cc() const;
370
379 CField<D> const & cc(int a) const;
380
384 bool hasCc() const;
385
389
397 void computeDc();
398
407 DArray< CField<D> > const & dc() const;
408
414 CField<D> const & dc(int i) const;
415
419 bool hasDc() const;
420
424
428 System<D>& system();
429
433 Random& random();
434
438 bool hasStep() const;
439
443 Step<D>& step();
444
445 #if 0
449 AnalyzerManager<D>& analyzerManager();
450
454 Factory<TrajectoryReader<D>>& trajectoryReaderFactory();
455 #endif
456
458
459 private:
460
461 #if 0
465 AnalyzerManager<D> analyzerManager_;
466 #endif
467
471 Random random_;
472
473 // Field components in eigenvector basis
474
484 DArray< CField<D> > wc_;
485
495 DArray< CField<D> > cc_;
496
503 DArray< CField<D> > dc_;
504
505 // Interaction matrix and eigen-properties
506
511
521 DMatrix<double> evecs_;
522
526 DArray<double> evals_;
527
531 DArray<bool> isPositiveEval_;
532
533 // Hamiltonian and components
534
538 std::complex<double> hamiltonian_;
539
543 std::complex<double> idealHamiltonian_;
544
548 std::complex<double> fieldHamiltonian_;
549
550 // Pointers to associated and child objects
551
555 System<D>* systemPtr_;
556
560 Step<D>* stepPtr_;
561
565 Factory< Step<D> >* stepFactoryPtr_;
566
567 #if 0
571 Factory< TrajectoryReader<D> >* trajectoryReaderFactoryPtr_;
572 #endif
573
574 // Counters and random seed
575
587 long iStep_;
588
592 long iTotalStep_;
593
597 long seed_;
598
599 // Boolean status indicators
600
604 bool hasHamiltonian_;
605
609 bool hasWc_;
610
614 bool hasCc_;
615
619 bool hasDc_;
620
624 bool isAllocated_;
625
626 // Private member functions
627
633 void readRandomSeed(std::istream& in);
634
640 void setup(int nStep);
641
642 };
643
644 // Inline functions
645
646 // Management of owned and associated objects
647
648 // Get the parent System by reference.
649 template <int D>
651 {
652 UTIL_ASSERT(systemPtr_);
653 return *systemPtr_;
654 }
655
656 // Get the random number generator by reference.
657 template <int D>
659 { return random_; }
660
661 // Get the Brownian dynamics stepper.
662 template <int D>
663 inline bool Simulator<D>::hasStep() const
664 { return (bool)stepPtr_; }
665
666 // Get the Brownian dynamics stepper.
667 template <int D>
669 {
671 return *stepPtr_;
672 }
673
674 #if 0
675 // Get the analyzer manager.
676 template <int D>
677 inline AnalyzerManager<D>& Simulator<D>::analyzerManager()
678 { return analyzerManager_; }
679
680 // Get the TrajectoryReaderfactory
681 template <int D>
682 inline
683 Factory<TrajectoryReader<D> >& Simulator<D>::trajectoryReaderFactory()
684 {
685 UTIL_ASSERT(trajectoryReaderFactoryPtr_);
686 return *trajectoryReaderFactoryPtr_;
687 }
688 #endif
689
690 // Interaction Matrix
691
692 // Return the interaction matrix U.
693 template <int D>
694 inline DMatrix<double> const & Simulator<D>::u() const
695 { return u_; }
696
697 // Return an array of eigenvalues of the interaction matrix U.
698 template <int D>
699 inline DArray<double> const & Simulator<D>::evals() const
700 { return evals_; }
701
702 // Return a single eigenvalue of the interaction matrix U.
703 template <int D>
704 inline double Simulator<D>::eval(int a) const
705 { return evals_[a]; }
706
707 // Return a matrix of eigenvectors of the interaction matrix U.
708 template <int D>
710 { return evecs_; }
711
712 // Return an element of an eigenvector of the interaction matrix U.
713 template <int D>
714 inline double Simulator<D>::evecs(int a, int i) const
715 { return evecs_(a, i); }
716
717 // Hamiltonian and its derivatives
718
719 // Get the precomputed Hamiltonian.
720 template <int D>
721 inline std::complex<double> Simulator<D>::hamiltonian() const
722 {
723 UTIL_CHECK(hasHamiltonian_);
724 return hamiltonian_;
725 }
726
727 // Get the ideal gas component of the precomputed Hamiltonian.
728 template <int D>
729 inline std::complex<double> Simulator<D>::idealHamiltonian() const
730 {
731 UTIL_CHECK(hasHamiltonian_);
732 return idealHamiltonian_;
733 }
734
735 // Get the W field component of the precomputed Hamiltonian.
736 template <int D>
737 inline std::complex<double> Simulator<D>::fieldHamiltonian() const
738 {
739 UTIL_CHECK(hasHamiltonian_);
740 return fieldHamiltonian_;
741 }
742
743 // Has the Hamiltonian been computed for the current w fields ?
744 template <int D>
746 { return hasHamiltonian_; }
747
748 // Fields
749
750 // Return all eigencomponents of the w fields.
751 template <int D>
752 inline DArray< CField<D> > const & Simulator<D>::wc() const
753 { return wc_; }
754
755 // Return a single eigenvector component of the w fields.
756 template <int D>
757 inline CField<D> const & Simulator<D>::wc(int a) const
758 { return wc_[a]; }
759
760 // Have eigenvector components of current w fields been computed?
761 template <int D>
762 inline bool Simulator<D>::hasWc() const
763 { return hasWc_; }
764
765 // Return all eigenvector components of the current c fields.
766 template <int D>
767 inline DArray< CField<D> > const & Simulator<D>::cc() const
768 { return cc_; }
769
770 // Return a single eigenvector component of the current c fields.
771 template <int D>
772 inline CField<D> const & Simulator<D>::cc(int a) const
773 { return cc_[a]; }
774
775 // Have eigenvector components of current c fields been computed?
776 template <int D>
777 inline bool Simulator<D>::hasCc() const
778 { return hasCc_; }
779
780 // Return all eigenvector components of the current d fields.
781 template <int D>
782 inline DArray< CField<D> > const & Simulator<D>::dc() const
783 { return dc_; }
784
785 // Return a single eigenvector component of the current d fields.
786 template <int D>
787 inline CField<D> const & Simulator<D>::dc(int a) const
788 { return dc_[a]; }
789
790 // Have eigenvector components of current d fields been computed?
791 template <int D>
792 inline bool Simulator<D>::hasDc() const
793 { return hasDc_; }
794
795 // Return the current converged simulation step index.
796 template <int D>
798 { return iStep_; }
799
800 // Return the current simulation step index.
801 template <int D>
803 { return iTotalStep_; }
804
805 // Explicit instantiation declarations
806 extern template class Simulator<1>;
807 extern template class Simulator<2>;
808 extern template class Simulator<3>;
809
810} // namespace Cpc
811} // namespace Pscf
812#endif
Simulator for complex Langevin field theoretic simulation.
void allocate()
Allocate required memory.
bool hasHamiltonian() const
Has the Hamiltonian been computed for current w and c fields?
long iStep()
Return the current converged simulation step index.
virtual void simulate(int nStep)
Perform a complex Langevin field theoretic simulation.
void computeHamiltonian()
Compute the Hamiltonian used in PS-FTS.
bool isPositiveEval(int i) const
Is the associated eigenvalue of U positive?
bool hasCc() const
Are eigen-components of current c fields valid ?
std::complex< double > fieldHamiltonian() const
Get the quadratic field contribution to the Hamiltonian.
virtual void clearTimers()
Clear timers.
System< D > & system()
Get parent system by reference.
virtual void outputTimers(std::ostream &out) const
Output timing results.
bool hasDc() const
Are the current d fields valid ?
void computeCc()
Compute eigenvector components of the current c fields.
void clearData()
Clear field eigen-components and hamiltonian components.
DMatrix< double > const & evecs() const
Get the matrix of all eigenvectors of the interaction matrix.
void computeWc()
Compute eigenvector components of the current w fields.
DArray< CField< D > > const & cc() const
Get all eigenvector components of the current c fields.
DArray< double > const & evals() const
Get an array of the eigenvalues of the interaction matrix U.
DMatrix< double > const & u() const
Get the interaction matrix U by const reference.
bool hasWc() const
Are eigen-components of current w fields valid ?
DArray< CField< D > > const & wc() const
Get all eigenvector components of the current w fields.
long iTotalStep()
Return the current simulation step index.
Random & random()
Get random number generator by reference.
Step< D > & step()
Get the Step by reference.
virtual void readParameters(std::istream &in)
Read parameters for a simulation.
double eval(int i) const
Get a single eigenvalue of the interaction matrix.
std::complex< double > hamiltonian() const
Get the Hamiltonian used in PS-FTS.
DArray< CField< D > > const & dc() const
Get all of the current d fields.
void analyzeInteraction()
Perform eigenvalue analysis of the interaction matrix U.
bool hasStep() const
Does this Simulator have a Step object?
void computeDc()
Compute functional derivatives of the Hamiltonian.
std::complex< double > idealHamiltonian() const
Get ideal gas contribution to the Hamiltonian.
Step is an abstract base class for Brownian dynamics steps.
Definition Step.h:33
Main class for CL-FTS, representing a complete physical system.
Field of complex double precision values on an FFT mesh.
Definition cpu/CField.h:29
Dynamically allocatable contiguous array template.
Definition DArray.h:32
Dynamically allocated Matrix.
Definition DMatrix.h:25
Factory template.
Definition Factory.h:34
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
Complex periodic fields, CL-FTS (CPU).
Definition cpc.mod:6
Periodic fields and crystallography.
Definition complex.cpp:11
PSCF package top-level namespace.