1#ifndef RP_BD_SIMULATOR_TPP
2#define RP_BD_SIMULATOR_TPP
11#include "BdSimulator.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>
26 template <
int D,
class T>
29 analyzerManagerPtr_(nullptr),
31 bdStepFactoryPtr_(nullptr),
32 trajectoryReaderFactoryPtr_(nullptr)
36 =
new typename T::AnalyzerManager(bdSimulator, system),
37 bdStepFactoryPtr_ =
new typename T::BdStepFactory(bdSimulator);
38 trajectoryReaderFactoryPtr_
39 =
new typename T::TrajectoryReaderFactory(system);
40 AnalyzerT::initStatic();
46 template <
int D,
class T>
49 delete analyzerManagerPtr_;
50 if (bdStepFactoryPtr_) {
51 delete bdStepFactoryPtr_;
56 if (trajectoryReaderFactoryPtr_) {
57 delete trajectoryReaderFactoryPtr_;
64 template <
int D,
class T>
68 SimulatorT::readRandomSeed(in);
72 std::string className;
76 bdStepFactoryPtr_->readObjectOptional(in, *
this,
80 <<
" BdStep{ [absent] }\n";
87 SimulatorT::readCompressor(in, isEnd);
93 SimulatorT::readPerturbation(in, isEnd);
97 SimulatorT::readRamp(in, isEnd);
101 AnalyzerT::baseInterval = 0;
105 state().needsCc =
false;
106 state().needsDc =
false;
107 state().needsHamiltonian =
false;
109 if (bdStep().needsCc()){
110 state().needsCc =
true;
112 if (bdStep().needsDc()){
113 state().needsDc =
true;
118 SimulatorT::allocate();
124 template <
int D,
class T>
125 void BdSimulator<D,T>::setup(
int nStep)
127 UTIL_CHECK(SimulatorT::system().w().hasData());
130 SimulatorT::analyzeChi();
132 if (SimulatorT::hasPerturbation()) {
133 SimulatorT::perturbation().setup();
136 if (SimulatorT::hasRamp()) {
137 SimulatorT::ramp().setup(nStep);
141 SimulatorT::system().compute();
144 if (SimulatorT::hasCompressor()) {
145 SimulatorT::compressor().compress();
146 SimulatorT::compressor().clearTimers();
150 SimulatorT::computeWc();
151 SimulatorT::computeCc();
152 SimulatorT::computeDc();
153 SimulatorT::computeHamiltonian();
159 if (analyzerManager().size() > 0){
160 analyzerManager().setup();
168 template <
int D,
class T>
173 UTIL_CHECK(SimulatorT::system().w().hasData());
178 if (SimulatorT::hasRamp()) {
179 SimulatorT::ramp().setParameters(iStep_);
181 int analyzerBaseInterval = AnalyzerT::baseInterval;
189 analyzerTimer.
start();
193 analyzerTimer.
stop();
196 for (iTotalStep_ = 0; iTotalStep_ < nStep; ++iTotalStep_) {
200 converged =
bdStep().step();
206 if (SimulatorT::hasRamp()) {
207 SimulatorT::ramp().setParameters(iStep_);
211 analyzerTimer.
start();
212 if (analyzerBaseInterval != 0) {
214 if (iStep_ % analyzerBaseInterval == 0) {
219 analyzerTimer.
stop();
223 <<
" failed to converge" <<
"\n";
228 double time = timer.
time();
229 double analyzerTime = analyzerTimer.
time();
232 if (analyzerBaseInterval != 0){
237 if (SimulatorT::hasRamp()){
239 SimulatorT::ramp().output();
244 Log::file() <<
"nStep " << nStep << std::endl;
245 if (iStep_ != nStep){
246 Log::file() <<
"nFail Step " << (nStep - iStep_)
250 <<
" sec" << std::endl;
251 double rStep = double(nStep);
252 Log::file() <<
"time / nStep " << time / rStep
253 <<
" sec" << std::endl;
254 Log::file() <<
"Analyzer run time " << analyzerTime
255 <<
" sec" << std::endl;
260 << SimulatorT::compressor().mdeCounter() << std::endl;
268 template <
int D,
class T>
271 std::string classname,
272 std::string filename)
281 typename T::TrajectoryReader* trajectoryReaderPtr;
283 if (!trajectoryReaderPtr) {
285 message =
"Invalid TrajectoryReader class name " + classname;
290 trajectoryReaderPtr->open(filename);
291 trajectoryReaderPtr->readHeader();
296 bool hasFrame = trajectoryReaderPtr->readFrame();
298 for (iStep_ = 0; iStep_ <= max && hasFrame; ++iStep_) {
300 SimulatorT::clearData();
313 hasFrame = trajectoryReaderPtr->readFrame();
316 Log::file() <<
"end main loop" << std::endl;
317 int nFrames = iStep_ - min;
318 trajectoryReaderPtr->close();
319 delete trajectoryReaderPtr;
326 Log::file() <<
"# of frames " << nFrames << std::endl;
328 <<
" sec" << std::endl;
329 Log::file() <<
"time / frame " << timer.
time()/double(nFrames)
330 <<
" sec" << std::endl;
338 template <
int D,
class T>
343 return *trajectoryReaderFactoryPtr_;
virtual void readParameters(std::istream &in)
Read parameter file block.
T::BdStep & bdStep()
Get the BdStep by non-const reference.
T::AnalyzerManager & analyzerManager()
Get the AnalyzerManager (const).
typename T::BdSimulator BdSimulatorT
Alias for BdSimulator class in program-level namespace.
Factory< typename T::TrajectoryReader > & trajectoryReaderFactory()
Get the trajectory reader factory by reference.
~BdSimulator()
Destructor.
typename T::System SystemT
Alias for System class in program-level namespace.
virtual void analyze(int min, int max, std::string classname, std::string filename)
Read and analyze a trajectory file.
typename T::Simulator SimulatorT
Alias for Simulator class in program-level namespace.
bool hasBdStep() const
Does this BdSimulator have a BdStep object?
void simulate(int nStep)
Perform a field theoretic Brownian dynamics (BD) simulation.
static std::ostream & file()
Get log ostream by reference.
std::string indent() const
Return indent string for this object (string of spaces).
static bool echo()
Get echo parameter.
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.