1#ifndef RPC_MC_SIMULATOR_TPP
2#define RPC_MC_SIMULATOR_TPP
11#include "McSimulator.h"
13#include <rpc/system/System.h>
14#include <rpc/fts/montecarlo/McMoveFactory.h>
15#include <rpc/fts/analyzer/AnalyzerFactory.h>
16#include <rpc/fts/trajectory/TrajectoryReader.h>
17#include <rpc/fts/trajectory/TrajectoryReaderFactory.h>
18#include <rpc/fts/compressor/Compressor.h>
19#include <rpc/fts/perturbation/PerturbationFactory.h>
20#include <rpc/fts/perturbation/Perturbation.h>
21#include <rpc/fts/ramp/RampFactory.h>
22#include <rpc/fts/ramp/Ramp.h>
24#include <util/random/Random.h>
25#include <util/misc/Timer.h>
28#include <gsl/gsl_eigen.h>
41 mcMoveManager_(*this,
system),
42 analyzerManager_(*this,
system),
43 trajectoryReaderFactoryPtr_(nullptr)
46 trajectoryReaderFactoryPtr_
56 if (trajectoryReaderFactoryPtr_) {
57 delete trajectoryReaderFactoryPtr_;
94 state_.needsHamiltonian =
true;
95 if (mcMoveManager_.needsCc()){
98 if (mcMoveManager_.needsDc()){
111 void McSimulator<D>::setup(
int nStep)
118 if (hasPerturbation()) {
119 perturbation().setup();
130 if (hasCompressor()) {
131 compressor().compress();
132 compressor().clearTimers();
137 if (state_.needsCc || state_.needsDc) {
140 if (state_.needsDc) {
143 computeHamiltonian();
145 mcMoveManager_.setup();
146 if (analyzerManager_.size() > 0){
147 analyzerManager_.setup();
176 analyzerTimer.
start();
177 analyzerManager_.sample(
iStep_);
178 analyzerTimer.
stop();
185 converged = mcMoveManager_.chooseMove().move();
195 analyzerTimer.
start();
198 if (analyzerManager_.size() > 0) {
199 analyzerManager_.sample(
iStep_);
203 analyzerTimer.
stop();
207 <<
" failed to converge" <<
"\n";
212 double time = timer.
time();
213 double analyzerTime = analyzerTimer.
time();
216 mcMoveManager_.output();
218 analyzerManager_.output();
228 Log::file() <<
"nStep " << nStep << std::endl;
233 <<
" sec" << std::endl;
234 double rStep = double(nStep);
235 Log::file() <<
"time / nStep " << time / rStep
236 <<
" sec" << std::endl;
237 Log::file() <<
"Analyzer run time " << analyzerTime
238 <<
" sec" << std::endl;
249 Log::file() <<
"Move Statistics:" << endl << endl;
250 Log::file() << setw(20) << left <<
"Move Name"
251 << setw(10) << right <<
"Attempted"
252 << setw(10) << right <<
"Accepted"
253 << setw(13) << right <<
"AcceptRate"
254 << setw(10) << right <<
"Failed"
255 << setw(13) << right <<
"FailRate"
257 int nMove = mcMoveManager_.size();
258 for (
int iMove = 0; iMove < nMove; ++iMove) {
259 attempt = mcMoveManager_[iMove].nAttempt();
260 accept = mcMoveManager_[iMove].nAccept();
261 fail = mcMoveManager_[iMove].nFail();
263 << mcMoveManager_[iMove].className()
264 << setw(10) << right << attempt
265 << setw(10) << accept
266 << setw(13) << fixed << setprecision(5)
267 << ( attempt == 0 ? 0.0 : double(accept)/double(attempt) )
269 << setw(13) << fixed << setprecision(5)
270 << ( attempt == 0 ? 0.0 : double(fail)/double(attempt) )
282 std::string classname,
283 std::string filename)
294 if (!trajectoryReaderPtr) {
296 message =
"Invalid TrajectoryReader class name " + classname;
301 trajectoryReaderPtr->
open(filename);
302 trajectoryReaderPtr->readHeader();
308 hasFrame = trajectoryReaderPtr->
readFrame();
321 analyzerManager_.sample(
iStep_);
325 hasFrame = trajectoryReaderPtr->
readFrame();
328 Log::file() <<
"end main loop" << std::endl;
329 int nFrames =
iStep_ - min;
330 trajectoryReaderPtr->
close();
331 delete trajectoryReaderPtr;
334 analyzerManager_.output();
338 Log::file() <<
"# of frames " << nFrames << std::endl;
340 <<
" sec" << std::endl;
341 Log::file() <<
"time / frame " << timer.
time()/double(nFrames)
342 <<
" sec" << std::endl;
355 out <<
"MC move time contributions:\n";
356 mcMoveManager_.outputTimers(out);
365 { mcMoveManager_.clearTimers(); }
static long baseInterval
The interval for an Analyzer must be a multiple of baseInterval.
Factory< TrajectoryReader< D > > & trajectoryReaderFactory()
Get the trajectory reader factory by reference.
virtual void analyze(int min, int max, std::string classname, std::string filename)
Read and analyze a trajectory file.
void setClassName(const char *className)
Set class name string.
bool hasMcMoves() const
Have any McMove algorithms been defined?
virtual void readParameters(std::istream &in)
Read parameters file block for an MC simulation.
virtual void outputTimers(std::ostream &out) const
Output timing results.
virtual void clearTimers()
Clear timers.
void simulate(int nStep)
Perform a field theoretic Monte-Carlo simulation.
~McSimulator()
Destructor.
McSimulator(System< D > &system)
Constructor.
void readParamCompositeOptional(std::istream &in, ParamComposite &child, bool next=true)
Add and attempt to read an optional child ParamComposite.
Ramp< D > const & ramp() const
Get the associated Ramp by const reference.
void readPerturbation(std::istream &in, bool &isEnd)
Optionally read a Perturbation parameter file block.
System< D > & system()
Get parent system by reference.
SimState< D > state_
Previous state saved during at the beginning of a step.
bool hasRamp() const
Does this Simulator have a Ramp?
void clearData()
Clear field eigen-components and hamiltonian components.
Compressor< D > & compressor()
Get the compressor by non-const reference.
virtual void outputMdeCounter(std::ostream &out) const
Output MDE counter.
void allocate()
Allocate required memory.
long iTotalStep_
Step counter - total number of attempted BD or MC steps.
bool hasCompressor() const
Does this Simulator have a Compressor?
void readRandomSeed(std::istream &in)
Optionally read a random number generator seed.
void readCompressor(std::istream &in, bool &isEnd)
Optionally read a Compressor parameter file block.
long iStep_
Step counter - attempted steps for which compressor converges.
void readRamp(std::istream &in, bool &isEnd)
Optionally read a Ramp parameter file block.
Main class, representing a complete physical system.
Factory for subclasses of TrajectoryReader.
Trajectory file reader (base class).
virtual bool readFrame()=0
Read a single frame.
virtual void close()=0
Close the trajectory file.
virtual void open(std::string filename)=0
Open trajectory file and read header, if any.
static std::ostream & file()
Get log ostream by reference.
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.
File containing preprocessor macros for error handling.
#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.
Real periodic fields, SCFT and PS-FTS (CPU).
PSCF package top-level namespace.