1#ifndef RP_MC_SIMULATOR_TPP
2#define RP_MC_SIMULATOR_TPP
11#include "McSimulator.h"
13#include <util/param/Factory.h>
14#include <util/param/ParamComposite.h>
15#include <util/random/Random.h>
16#include <util/misc/Timer.h>
18#include <gsl/gsl_eigen.h>
28 template <
int D,
class T>
31 mcMoveManagerPtr_(nullptr),
32 analyzerManagerPtr_(nullptr),
33 trajectoryReaderFactoryPtr_(nullptr)
37 =
new typename T::McMoveManager(mcSimulator, system),
39 =
new typename T::AnalyzerManager(mcSimulator, system),
40 trajectoryReaderFactoryPtr_
41 =
new typename T::TrajectoryReaderFactory(system);
42 AnalyzerT::initStatic();
48 template <
int D,
class T>
51 delete mcMoveManagerPtr_;
52 delete analyzerManagerPtr_;
53 if (trajectoryReaderFactoryPtr_) {
54 delete trajectoryReaderFactoryPtr_;
61 template <
int D,
class T>
65 SimulatorT::readRandomSeed(in);
72 SimulatorT::readCompressor(in, isEnd);
81 SimulatorT::readPerturbation(in, isEnd);
83 SimulatorT::readRamp(in, isEnd);
87 AnalyzerT::baseInterval = 0;
91 state().needsCc =
false;
92 state().needsDc =
false;
93 state().needsHamiltonian =
true;
95 state().needsCc =
true;
97 if (mcMoveManager().needsDc()){
98 state().needsDc =
true;
102 SimulatorT::allocate();
108 template <
int D,
class T>
109 void McSimulator<D,T>::setup(
int nStep)
111 UTIL_CHECK(SimulatorT::system().w().hasData());
114 SimulatorT::analyzeChi();
116 if (SimulatorT::hasPerturbation()) {
117 SimulatorT::perturbation().setup();
120 if (SimulatorT::hasRamp()) {
121 SimulatorT::ramp().setup(nStep);
125 SimulatorT::system().compute();
128 if (SimulatorT::hasCompressor()) {
129 SimulatorT::compressor().compress();
130 SimulatorT::compressor().clearTimers();
134 SimulatorT::computeWc();
135 if (state().needsCc || state().needsDc) {
136 SimulatorT::computeCc();
138 if (state().needsDc) {
139 SimulatorT::computeDc();
141 SimulatorT::computeHamiltonian();
145 mcMoveManager().setup();
149 if (analyzerManager().size() > 0){
150 analyzerManager().setup();
158 template <
int D,
class T>
167 if (SimulatorT::hasRamp()) {
168 SimulatorT::ramp().setParameters(iStep_);
170 int analyzerBaseInterval = AnalyzerT::baseInterval;
181 analyzerTimer.
stop();
184 for (iTotalStep_ = 0; iTotalStep_ < nStep; ++iTotalStep_) {
193 if (SimulatorT::hasRamp()) {
194 SimulatorT::ramp().setParameters(iStep_);
198 analyzerTimer.
start();
199 if (analyzerBaseInterval != 0) {
200 if (iStep_ % analyzerBaseInterval == 0) {
206 analyzerTimer.
stop();
210 <<
" failed to converge" <<
"\n";
214 double time = timer.
time();
215 double analyzerTime = analyzerTimer.
time();
218 mcMoveManager().output();
219 if (analyzerBaseInterval != 0){
220 analyzerManager().output();
224 if (SimulatorT::hasRamp()){
225 SimulatorT::ramp().output();
230 Log::file() <<
"nStep " << nStep << std::endl;
231 if (iStep_ != nStep){
233 << (nStep - iStep_) << std::endl;
236 <<
" sec" << std::endl;
237 double rStep = double(nStep);
238 Log::file() <<
"time / nStep " << time / rStep
239 <<
" sec" << std::endl;
240 Log::file() <<
"Analyzer run time " << analyzerTime
241 <<
" sec" << std::endl;
245 SimulatorT::outputMdeCounter(
Log::file());
252 Log::file() <<
"Move Statistics:" << endl << endl;
253 Log::file() << setw(20) << left <<
"Move Name"
254 << setw(10) << right <<
"Attempted"
255 << setw(10) << right <<
"Accepted"
256 << setw(13) << right <<
"AcceptRate"
257 << setw(10) << right <<
"Failed"
258 << setw(13) << right <<
"FailRate"
260 int nMove = mcMoveManager().size();
261 for (
int iMove = 0; iMove < nMove; ++iMove) {
262 attempt = mcMoveManager()[iMove].nAttempt();
263 accept = mcMoveManager()[iMove].nAccept();
264 fail = mcMoveManager()[iMove].nFail();
266 << mcMoveManager()[iMove].className()
267 << setw(10) << right << attempt
268 << setw(10) << accept
269 << setw(13) << fixed << setprecision(5)
270 << ( attempt == 0 ? 0.0 : double(accept)/double(attempt) )
272 << setw(13) << fixed << setprecision(5)
273 << ( attempt == 0 ? 0.0 : double(fail)/double(attempt) )
283 template <
int D,
class T>
285 std::string classname,
286 std::string filename)
295 typename T::TrajectoryReader* trajectoryReaderPtr;
297 if (!trajectoryReaderPtr) {
299 message =
"Invalid TrajectoryReader class name " + classname;
304 trajectoryReaderPtr->open(filename);
305 trajectoryReaderPtr->readHeader();
310 bool hasFrame = trajectoryReaderPtr->readFrame();
312 for (iStep_ = 0; iStep_ <= max && hasFrame; ++iStep_) {
314 SimulatorT::clearData();
327 hasFrame = trajectoryReaderPtr->readFrame();
330 Log::file() <<
"end main loop" << std::endl;
331 int nFrames = iStep_ - min;
332 trajectoryReaderPtr->close();
333 delete trajectoryReaderPtr;
340 Log::file() <<
"# of frames " << nFrames << std::endl;
342 <<
" sec" << std::endl;
343 Log::file() <<
"time / frame " << timer.
time()/double(nFrames)
344 <<
" sec" << std::endl;
352 template <
int D,
class T>
355 SimulatorT::compressor().outputTimers(out);
357 out <<
"MC move time contributions:\n";
364 template <
int D,
class T>
Types< D >::AnalyzerManager const & analyzerManager() const
McSimulator(SystemT &system, McSimulatorT &mcSimulator)
Constructor.
virtual void clearTimers()
Clear timers.
Factory< typename T::TrajectoryReader > & trajectoryReaderFactory()
Get the trajectory reader factory by reference.
T::McMoveManager const & mcMoveManager() const
Get the McMoveManager (const).
virtual void readParameters(std::istream &in)
Read parameter file block.
virtual void analyze(int min, int max, std::string classname, std::string filename)
Read and analyze a trajectory file.
typename T::System SystemT
Alias for System class in program-level namespace.
virtual void outputTimers(std::ostream &out) const
Output timing results.
~McSimulator()
Destructor.
typename T::Simulator SimulatorT
Alias for Simulator class in program-level namespace.
typename T::McSimulator McSimulatorT
Alias for McSimulator class in program-level namespace.
void simulate(int nStep)
Perform a field theoretic Monte-Carlo simulation.
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.
Class templates for real-valued periodic fields.
PSCF package top-level namespace.