1#ifndef RP_MC_SIMULATOR_TPP
2#define RP_MC_SIMULATOR_TPP
11#include "McSimulator.h"
13#include <rp/fts/montecarlo/McMove.h>
14#include <rp/fts/montecarlo/McMoveFactory.h>
15#include <rp/fts/montecarlo/McMoveManager.h>
17#include <rp/fts/analyzer/Analyzer.h>
18#include <rp/fts/analyzer/AnalyzerFactory.h>
19#include <rp/fts/analyzer/AnalyzerManager.h>
20#include <rp/fts/trajectory/TrajectoryReaderFactory.h>
21#include <rp/fts/trajectory/TrajectoryReader.h>
22#include <rp/fts/perturbation/Perturbation.h>
23#include <rp/fts/ramp/Ramp.h>
25#include <rp/fts/simulator/Simulator.h>
26#include <rp/fts/simulator/SimState.h>
27#include <rp/fts/compressor/Compressor.h>
28#include <rp/system/System.h>
29#include <rp/field/WFields.h>
30#include <rp/field/CFields.h>
32#include <pscf/field/RField.h>
34#include <util/param/Factory.h>
35#include <util/param/ParamComposite.h>
36#include <util/random/Random.h>
37#include <util/misc/Timer.h>
39#include <gsl/gsl_eigen.h>
49 template <
int D,
class T>
52 mcMoveManagerPtr_(nullptr),
53 analyzerManagerPtr_(nullptr),
54 trajectoryReaderFactoryPtr_(nullptr)
61 trajectoryReaderFactoryPtr_
69 template <
int D,
class T>
72 delete mcMoveManagerPtr_;
73 delete analyzerManagerPtr_;
74 if (trajectoryReaderFactoryPtr_) {
75 delete trajectoryReaderFactoryPtr_;
82 template <
int D,
class T>
112 state().needsCc =
false;
113 state().needsDc =
false;
114 state().needsHamiltonian =
true;
116 state().needsCc =
true;
119 state().needsDc =
true;
129 template <
int D,
class T>
130 void McSimulator<D,T>::setup(
int nStep,
bool isSimulation)
159 if (state().needsCc || state().needsDc) {
162 if (state().needsDc) {
168 if (isSimulation && hasMcMoves()) {
169 mcMoveManager().setup();
173 if (analyzerManager().size() > 0){
174 analyzerManager().setup();
182 template <
int D,
class T>
203 analyzerTimer.
start();
204 if (analyzerBaseInterval != 0) {
209 analyzerTimer.
stop();
226 analyzerTimer.
start();
227 if (analyzerBaseInterval != 0) {
229 if (
iStep_ % analyzerBaseInterval == 0) {
234 analyzerTimer.
stop();
238 <<
" failed to converge" <<
"\n";
250 double time = timer.
time();
251 double analyzerTime = analyzerTimer.
time();
255 if (analyzerBaseInterval != 0){
266 Log::file() <<
"nStep " << nStep << std::endl;
269 << (nStep -
iStep_) << std::endl;
272 <<
" sec" << std::endl;
273 double rStep = double(nStep);
274 Log::file() <<
"time / nStep " << time / rStep
275 <<
" sec" << std::endl;
276 Log::file() <<
"Analyzer run time " << analyzerTime
277 <<
" sec" << std::endl;
288 Log::file() <<
"Move Statistics:" << endl << endl;
289 Log::file() << setw(20) << left <<
"Move Name"
290 << setw(10) << right <<
"Attempted"
291 << setw(10) << right <<
"Accepted"
292 << setw(13) << right <<
"AcceptRate"
293 << setw(10) << right <<
"Failed"
294 << setw(13) << right <<
"FailRate"
297 for (
int iMove = 0; iMove < nMove; ++iMove) {
303 << setw(10) << right << attempt
304 << setw(10) << accept
305 << setw(13) << fixed << setprecision(5)
306 << ( attempt == 0 ? 0.0 : double(accept)/double(attempt) )
308 << setw(13) << fixed << setprecision(5)
309 << ( attempt == 0 ? 0.0 : double(fail)/double(attempt) )
319 template <
int D,
class T>
321 std::string classname,
322 std::string filename)
333 if (!trajectoryReaderPtr) {
335 message =
"Invalid TrajectoryReader class name " + classname;
340 trajectoryReaderPtr->
open(filename);
349 while (iStep_ <= max && trajectoryReaderPtr->readFrame()) {
366 Log::file() <<
"end main loop" << std::endl;
367 int nFrame =
iStep_ - min;
368 trajectoryReaderPtr->
close();
369 delete trajectoryReaderPtr;
377 Log::file() <<
"# of frames " << nFrame << std::endl;
379 <<
" sec" << std::endl;
380 Log::file() <<
"time / frame " << timer.
time()/double(nFrame)
381 <<
" sec" << std::endl;
389 template <
int D,
class T>
396 out <<
"MC move time contributions:\n";
403 template <
int D,
class T>
AnalyzerManager(Simulator< D, T > &simulator, System< D, T > &system)
Constructor.
static void initStatic()
Define and initialize baseInterval (initialized to 1).
static long baseInterval
The interval for every Analyzer must be a multiple of baseInterval.
McMoveManager(McSimulator< D, T > &simulator, System< D, T > &system)
Constructor.
AnalyzerManager< D, T > const & analyzerManager() const
Get the AnalyzerManager (const).
virtual void clearTimers()
Clear timers.
virtual void readParameters(std::istream &in)
Read parameter file block.
McMoveManager< D, T > const & mcMoveManager() const
Get the McMoveManager (const).
virtual void analyze(int min, int max, std::string classname, std::string filename)
Read and analyze a trajectory file.
bool hasMcMoves() const
Have any MC moves been defined?
virtual void outputTimers(std::ostream &out) const
Output timing results.
~McSimulator()
Destructor.
void simulate(int nStep)
Perform a field theoretic Monte-Carlo simulation.
Factory< TrajectoryReader< D, T > > & trajectoryReaderFactory()
Get the trajectory reader factory by reference.
McSimulator(System< D, T > &system)
Constructor.
Simulator(System< D, T > &system)
Constructor.
void computeHamiltonian()
Compute the Hamiltonian used in PS-FTS.
void readRamp(std::istream &in, bool &isEnd)
Optionally read a Ramp parameter file block.
void clearData()
Clear field eigen-components and Hamiltonian components.
void readPerturbation(std::istream &in, bool &isEnd)
Optionally read a Perturbation parameter file block.
void computeWc()
Compute eigenvector components of the current w fields.
void readCompressor(std::istream &in, bool &isEnd)
Optionally read a Compressor parameter file block.
void computeCc()
Compute eigenvector components of the current c fields.
System< D, T > & system()
Get the parent system by reference.
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.
Perturbation< D, T > const & perturbation() const
Get a Perturbation by const reference.
void computeDc()
Compute functional derivatives of the Hamiltonian.
long iTotalStep_
Step counter - total number of attempted BD or MC steps.
bool hasPerturbation() const
Does this Simulator have a Perturbation?
bool hasRamp() const
Does this Simulator have a Ramp?
SimState< D, CPT > & state()
bool hasCompressor() const
Does this Simulator have a Compressor?
Compressor< D, T > const & compressor() const
Get the Compressor by const reference.
long iStep_
Step counter - number of steps for which the compressor converged.
void analyzeChi()
Perform eigenvalue analysis of projected chi matrix.
Ramp< D, T > const & ramp() const
Get a Ramp by const reference.
Base class template for classes that represent a complete system.
Factory for subclasses of TrajectoryReader.
Trajectory file reader (abstract base class).
virtual void open(std::string filename)=0
Open trajectory file and allocate memory if necessary.
virtual void readHeader()
Read header of trajectory file (if any).
virtual void close()=0
Close the trajectory file.
static std::ostream & file()
Get log ostream by reference.
void setClassName(const char *className)
Set class name string.
void readParamCompositeOptional(std::istream &in, ParamComposite &child, bool next=true)
Add and attempt to read an optional child ParamComposite.
void start(TimePoint begin)
Start timing from an externally supplied time.
double time() const
Return the accumulated time, in seconds.
void stop(TimePoint end)
Stop the clock at an externally supplied time.
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Code for programs that simulate real-valued periodic fields.
PSCF package top-level namespace.