10 #include "McSimulation.h" 11 #include <mcMd/chemistry/Molecule.h> 12 #include <mcMd/chemistry/Atom.h> 13 #include <mcMd/analyzers/Analyzer.h> 14 #include <mcMd/trajectory/TrajectoryReader.h> 15 #include <mcMd/generators/Generator.h> 16 #include <mcMd/generators/generatorFactory.h> 18 #include <mcMd/potentials/pair/McPairPotential.h> 21 #include <mcMd/potentials/bond/BondPotential.h> 24 #include <mcMd/potentials/angle/AnglePotential.h> 27 #include <mcMd/potentials/dihedral/DihedralPotential.h> 31 #include <mcMd/perturb/ReplicaMove.h> 35 #include <simp/species/Species.h> 37 #include <util/param/Factory.h> 38 #include <util/archives/Serializable_includes.h> 39 #include <util/format/Dbl.h> 40 #include <util/format/Int.h> 41 #include <util/format/Str.h> 42 #include <util/misc/Log.h> 43 #include <util/misc/Timer.h> 44 #include <util/misc/ioUtil.h> 65 mcMoveManager_(*this),
66 mcAnalyzerManager_(*this),
67 mcCommandManager_(*this),
71 isInitialized_(false),
93 mcMoveManager_(*this),
94 mcAnalyzerManager_(*this),
95 mcCommandManager_(*this),
99 isInitialized_(false),
144 while ((c = getopt(argc, argv,
"qer:p:c:i:o:f")) != -1) {
178 char optChar = optopt;
179 Log::file() <<
"Unknown option -" << optChar << std::endl;
203 std::string msg(
"Error: Options -r and -f are incompatible. ");
204 msg +=
"Existence of a perturbation is specified in restart file.";
223 UTIL_THROW(
"Error: Options -r and -p are incompatible.");
232 isRestarting_ =
true;
233 load(std::string(rarg));
261 if (isInitialized_) {
262 UTIL_THROW(
"Error: Called readParam when already initialized");
284 readOptional<int>(in,
"saveInterval", saveInterval_);
285 if (saveInterval_ > 0) {
286 read<std::string>(in,
"saveFileName", saveFileName_);
290 isInitialized_ =
true;
299 if (isInitialized_) {
319 if (isInitialized_) {
320 UTIL_THROW(
"Error: Called readParam when already initialized");
324 UTIL_THROW(
"Error: Has a param communicator in loadParameters");
333 loadParameter<int>(ar,
"saveInterval", saveInterval_);
334 if (saveInterval_ > 0) {
335 loadParameter<std::string>(ar,
"saveFileName", saveFileName_);
341 isInitialized_ =
true;
355 if (saveInterval_ > 0) {
365 if (isInitialized_) {
366 UTIL_THROW(
"Error: Called load when already initialized");
368 if (!isRestarting_) {
369 UTIL_THROW(
"Error: Called load without restart option");
374 std::ios_base::openmode mode = std::ios_base::in | std::ios_base::binary;
381 if (
system().hasPerturbation()) {
390 isInitialized_ =
true;
396 std::ios_base::openmode mode = std::ios_base::out | std::ios_base::binary;
407 if (!isInitialized_) {
408 UTIL_THROW(
"McSimulation is not initialized");
412 std::string filename;
413 std::ifstream inputFile;
414 std::ofstream outputFile;
417 std::istream& inBuffer = in;
419 std::stringstream inBuffer;
423 bool readNext =
true;
440 for (
unsigned i=0; i < line.size(); ++i) {
441 inBuffer.put(line[i]);
450 if (command ==
"RESTART") {
454 << endStep << std::endl;
456 isRestarting_ =
false;
463 if (command ==
"FINISH") {
470 Log::file() <<
"Error: Unknown command " << std::endl;
501 if (!isInitialized_) {
506 if (isContinuation) {
507 Log::file() <<
"Continuing from iStep = " 514 int nStep = endStep - beginStep;
535 if (saveInterval_ != 0) {
536 if (
iStep_ % saveInterval_ == 0) {
547 if (
system().hasPerturbation()) {
548 if (
system().hasReplicaMove()) {
565 double time = timer.
time();
568 assert(
iStep_ == endStep);
580 if (saveInterval_ != 0) {
581 if (
iStep_ % saveInterval_ == 0) {
596 Log::file() <<
"endStep " << endStep << std::endl;
597 Log::file() <<
"nStep " << nStep << std::endl;
599 <<
" sec" << std::endl;
600 double rStep = double(nStep);
601 Log::file() <<
"time / nStep " << time / rStep
602 <<
" sec" << std::endl;
609 Log::file() <<
"Move Statistics:" << endl << endl;
610 Log::file() << setw(32) << left <<
"Move Name" 611 << setw(12) << right <<
"Attempted" 612 << setw(12) << right <<
"Accepted" 613 << setw(15) << right <<
"AcceptRate" 616 for (
int iMove = 0; iMove < nMove; ++iMove) {
621 << setw(12) << right << attempt
622 << setw(12) << accept
623 << setw(15) << fixed << setprecision(6)
624 << ( attempt == 0 ? 0.0 : double(accept)/double(attempt) )
632 if (
system().hasPerturbation()) {
633 if (
system().hasReplicaMove()) {
636 int nAttempt, nAccept;
640 ratio = nAttempt == 0 ? 0.0 : double(nAccept)/double(nAttempt);
642 <<
Int(nAttempt) <<
Int(nAccept) <<
Dbl(ratio)
664 std::string filename;
665 std::stringstream indexString;
666 std::ifstream configFile;
668 nConfig = max - min + 1;
671 Log::file() <<
"begin main loop" << std::endl;
677 filename += indexString.str();
706 Log::file() <<
"end main loop" << std::endl;
713 Log::file() <<
"nConfig " << nConfig << std::endl;
715 <<
" sec" << std::endl;
716 Log::file() <<
"time / config " << timer.
time()/double(nConfig)
717 <<
" sec" << std::endl;
727 std::string classname,
728 std::string filename)
738 if (!trajectoryReaderPtr) {
740 message =
"Invalid TrajectoryReader class name " + classname;
745 Log::file() <<
"Reading " << filename << std::endl;
746 trajectoryReaderPtr->
open(filename);
750 Log::file() <<
"Begin main loop" << std::endl;
751 bool hasFrame =
true;
754 hasFrame = trajectoryReaderPtr->
readFrame();
770 Log::file() <<
"end main loop" << std::endl;
771 int nFrames =
iStep_ - min;
773 trajectoryReaderPtr->
close();
774 delete trajectoryReaderPtr;
781 Log::file() <<
"# of frames " << nFrames << std::endl;
783 <<
" sec" << std::endl;
784 Log::file() <<
"time / frame " << timer.
time()/double(nFrames)
785 <<
" sec" << std::endl;
803 if (&
system().simulation() !=
this) {
804 UTIL_THROW(
"Invalid pointer to Simulation in System");
virtual void loadConfig(Serializable::IArchive &ar)
Load the system configuration from an archive.
virtual ~McSimulation()
Destructor.
McSystem & system()
Get the McSystem by reference.
virtual bool move()
Generate, attempt, and accept or reject a Monte Carlo move.
virtual void save(Serializable::OArchive &ar)
Save a set of objects to an output archive.
void readCommands()
Read and execute commands from the default parameter file.
bool readCommand(std::string command, std::istream &in)
Read and execute a single command from an input stream.
int iStep_
Step index for main MC or MD loop.
virtual void readParameters(std::istream &in)
Read parameter file block and initialize simulation.
void setCommandFileName(const std::string &commandFileName)
Set the command file name.
void buildCellList()
Build the CellList with current configuration.
static long baseInterval
The interval for an Analyzer must be a multiple of baseInterval.
Signal & positionSignal()
Signal to indicate change in atomic positions.
void outputOptions(std::ostream &out) const
Output a list of options enabled and disabled during compilation.
long nAccept()
Number of accepted swaps.
void setExpectPerturbation()
Set to expect a Perturbation in the parameter file.
virtual void save(Serializable::OArchive &ar)
Save internal state to an archive.
void analyzeConfigs(int min, int max, std::string basename)
Read and analyze a sequence of configuration files.
void saveConfig(Serializable::OArchive &ar)
Save configuration.
Factory< Data > & factory()
Return a reference to the factory.
AnalyzerManager & analyzerManager()
Get the associated AnalyzerManager by reference.
void openRestartIFile(const std::string &name, std::ifstream &in, std::ios_base::openmode mode=std::ios_base::in) const
Open an input restart dump file for reading.
void loadParamComposite(Serializable::IArchive &ar, ParamComposite &child, bool next=true)
Add and load a required child ParamComposite.
static void setEcho(bool echo=true)
Enable or disable echoing for all subclasses of ParamComponent.
std::string className(int i) const
Get the subclass name for object number i.
End & readEnd(std::istream &in)
Add and read the closing bracket.
void openInputFile(const std::string &filename, std::ifstream &in, std::ios_base::openmode mode=std::ios_base::in) const
Open an input file.
Wrapper for a double precision number, for formatted ostream output.
virtual bool isValid() const
Return true if Simulation is valid, or throw an Exception.
File containing preprocessor macros for error handling.
Classes used by all simpatico molecular simulations.
void setFileMaster(FileMaster &filemaster)
Set the FileMaster.
virtual void readConfig(std::istream &in)
Read system configuration from file.
virtual void save(Serializable::OArchive &ar)
Save state to an archive.
virtual bool isValid() const
Return true if McSystem is valid, or throw Exception.
virtual bool move()
Attempt, and accept or reject a replica exchange move.
The main object in a simulation, which coordinates others.
long nAttempt()
Number of swap attempts.
Saving / output archive for binary ostream.
bool getNextLine(std::istream &in, std::string &line)
Read the next non-empty line into a string, strip trailing whitespace.
void removeAllMolecules()
Remove all molecules from this System.
void output()
Output statistics for all moves.
double time()
Get the accumulated time, in seconds.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
std::string className() const
Get class name string.
CommandManager & commandManager()
Get the associated CommandManager by reference.
void output()
Call output method of each analyzer.
Factory< McMove > & mcMoveFactory()
Get the McMove factory by reference.
void stop()
Stop the clock, increment time.
void setId(int Id)
Set the integer Id for this System.
virtual void readParameters(std::istream &in)
Read body of parameter block from a specific file.
virtual void saveParameters(Serializable::OArchive &ar)
Save parameters to an archive, without configuration.
void notify(const T &t)
Notify all observers.
Utility classes for scientific computation.
void setAnalyzerManager(AnalyzerManager *ptr)
Set the associated AnalyzerManager.
void setCommandManager(CommandManager *ptr)
Set the associated CommandManager.
virtual void open(std::string filename)=0
Open trajectory file and read header, if any.
virtual void save(Serializable::OArchive &ar)
Save internal state to an archive.
Wrapper for an int, for formatted ostream output.
virtual void close()=0
Close the trajectory file.
bool isIoProcessor() const
Can this processor do file I/O ?
MPI::Intracomm & communicator()
Get the MPI communicator by reference.
Trajectory file reader (base class).
McPairPotential & pairPotential() const
Return the McPairPotential by reference.
void readParamCompositeOptional(std::istream &in, ParamComposite &child, bool next=true)
Add and attempt to read an optional child ParamComposite.
static std::ostream & file()
Get log ostream by reference.
bool hasIoCommunicator() const
Does this object have an associated MPI communicator?
void setOutputPrefix(const std::string &outputPrefix)
Set the output file prefix string.
Saving archive for binary istream.
std::ifstream & file()
Get the underlying ifstream by reference.
Single-processor Monte Carlo (MC) and molecular dynamics (MD).
void setInputPrefix(const std::string &inputPrefix)
Set the input file prefix string.
virtual bool readFrame()=0
Read a single frame.
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
void setClassName(const char *className)
Set class name string.
void setParamFileName(const std::string ¶mFileName)
Set the parameter file name.
virtual bool isValid() const
Return true if valid, or throw an Exception.
void readParamComposite(std::istream &in, ParamComposite &child, bool next=true)
Add and read a required child ParamComposite.
Begin & readBegin(std::istream &in, const char *label, bool isRequired=true)
Add and read a class label and opening bracket.
virtual void loadParameters(Serializable::IArchive &ar)
Load parameters from an archive.
void setIoCommunicator()
Set MPI job to read one parameter file and one command file.
void setup()
Call initialize method of each Analyzer.
virtual Data * factory(const std::string &className) const =0
Returns a pointer to a new instance of specified subclass.
McMoveManager & mcMoveManager()
Get the McMoveManager by reference.
int size() const
Get logical size.
bool readCommand(std::string name, std::istream &in)
Attempt to read a command line, execute if recognized.
void analyzeTrajectory(int min, int max, std::string classname, std::string filename)
Read and analyze a trajectory file.
std::ofstream & file()
Get the underlying ifstream by reference.
void openRestartOFile(const std::string &name, std::ofstream &out, std::ios_base::openmode mode=std::ios_base::out) const
Open an output restart file for writing.
void start()
Start the clock.
void sample(long iStep)
Call sample method of each Analyzer.
void simulate(int endStep, bool isContinuation=false)
Run an MC simulation of specified length.
void setSimulation(Simulation &simulation)
Set the parent Simulation.
McSimulation()
Constructor.
void readParam()
Read parameters from the default parameter istream.
void load(const std::string &filename)
Read restart file to continue simulation.
Factory< TrajectoryReader > & trajectoryReaderFactory()
Get the trajectory reader/writer factory by reference.
virtual void save(Serializable::OArchive &ar)
Save internal state to an archive.
McMove & chooseMove()
Choose an McMove at random, using specified probabilities.
FileMaster & fileMaster()
Get the FileMaster object.
virtual void loadParameters(Serializable::IArchive &ar)
Load internal state from an archive.
ReplicaMove & replicaMove() const
Get the associated ReplicaMove by reference.
void setOptions(int argc, char **argv)
Process command line options.