Simpatico  v1.10
ddMd/analyzers/trajectory/TrajectoryWriter.cpp
1 /*
2 * Simpatico - Simulation Package for Polymeric and Molecular Liquids
3 *
4 * Copyright 2010 - 2017, The Regents of the University of Minnesota
5 * Distributed under the terms of the GNU General Public License.
6 */
7 
8 #include "TrajectoryWriter.h"
9 #include <ddMd/simulation/Simulation.h>
10 #include <util/mpi/MpiLoader.h>
11 
12 #include <fstream>
13 #include <ios>
14 
15 namespace DdMd
16 {
17 
18  using namespace Util;
19 
20  /*
21  * Constructor.
22  */
23  TrajectoryWriter::TrajectoryWriter(Simulation& simulation, bool isBinary)
24  : Analyzer(simulation),
25  isInitialized_(false),
26  isBinary_(isBinary),
27  domainPtr_(0),
28  boundaryPtr_(0),
29  atomStoragePtr_(0)
30  #ifdef SIMP_BOND
31  , bondStoragePtr_(0)
32  #endif
33  #ifdef SIMP_ANGLE
34  , angleStoragePtr_(0)
35  #endif
36  #ifdef SIMP_DIHEDRAL
37  , dihedralStoragePtr_(0)
38  #endif
39  {
40  setClassName("TrajectoryWriter");
41  domainPtr_ = &simulation.domain();
42  boundaryPtr_ = &simulation.boundary();
43 
44  atomStoragePtr_ = &simulation.atomStorage();
45  #ifdef SIMP_BOND
46  bondStoragePtr_ = &simulation.bondStorage();
47  #endif
48  #ifdef SIMP_ANGLE
49  angleStoragePtr_ = &simulation.angleStorage();
50  #endif
51  #ifdef SIMP_DIHEDRAL
52  dihedralStoragePtr_ = &simulation.dihedralStorage();
53  #endif
54  }
55 
56  /*
57  * Read interval and outputFileName.
58  */
59  void TrajectoryWriter::readParameters(std::istream& in)
60  {
61  readInterval(in);
63  isInitialized_ = true;
64  }
65 
66  /*
67  * Load internal state from an archive.
68  */
70  {
71  loadInterval(ar);
73  isInitialized_ = true;
74  }
75 
76  /*
77  * Save internal state to output archive.
78  */
80  {
81  saveInterval(ar);
83  }
84 
85  /*
86  * Setup - open the trajectory file.
87  */
89  {
90  FileMaster& fileMaster = simulation().fileMaster();
91  if (isIoProcessor()) {
92  if (isBinary()) {
93  fileMaster.openOutputFile(outputFileName(), outputFile_,
94  std::ios::out | std::ios::binary);
95  } else {
96  fileMaster.openOutputFile(outputFileName(), outputFile_);
97  }
98  }
99  writeHeader(outputFile_);
100  }
101 
102  /*
103  * Write a frame to file.
104  */
105  void TrajectoryWriter::sample(long iStep)
106  {
107  if (isAtInterval(iStep)) {
108  writeFrame(outputFile_, iStep);
109  }
110  }
111 
112  /*
113  * Clear sample counter and close file.
114  */
116  {
117  if (outputFile_.is_open()) {
118  outputFile_.close();
119  }
120  }
121 
122  /*
123  * Clear sample counter and close output file.
124  */
126  { clear(); }
127 
128 }
Abstract base for periodic output and/or analysis actions.
Simulation & simulation()
Get the parent Simulation by reference.
void readOutputFileName(std::istream &in)
Read outputFileName from file.
void saveInterval(Serializable::OArchive &ar)
Save interval parameter to an archive.
virtual void clear()
Clear nSample counter.
AtomStorage & atomStorage()
Get the AtomStorage by reference.
DihedralStorage & dihedralStorage()
Get the DihedralStorage by reference.
virtual void save(Serializable::OArchive &ar)
Save internal state to an archive.
void openOutputFile(const std::string &filename, std::ofstream &out, std::ios_base::openmode mode=std::ios_base::out) const
Open an output file.
Definition: FileMaster.cpp:290
virtual void readParameters(std::istream &in)
Read parameters and initialize.
void readInterval(std::istream &in)
Read parameter interval from file.
virtual void setup()
Open the trajectory file.
BondStorage & bondStorage()
Get the BondStorage by reference.
Parallel domain decomposition (DD) MD simulation.
Main object for a domain-decomposition MD simulation.
virtual void writeFrame(std::ofstream &out, long iStep)=0
Write data that should appear in every frame.
Saving / output archive for binary ostream.
void loadOutputFileName(Serializable::IArchive &ar)
Load output file name to an archive.
bool isBinary() const
Is the file format binary (true) or text (false)?
FileMaster & fileMaster()
Get the associated FileMaster by reference.
virtual void sample(long iStep)
Dump configuration to file.
bool isAtInterval(long counter) const
Return true iff counter is a multiple of the interval.
Utility classes for scientific computation.
Definition: accumulators.mod:1
bool isIoProcessor() const
Can this processor do file I/O ?
Definition: MpiFileIo.h:92
virtual void loadParameters(Serializable::IArchive &ar)
Load internal state from an archive.
const std::string & outputFileName() const
Return outputFileName string.
A FileMaster manages input and output files for a simulation.
Definition: FileMaster.h:142
TrajectoryWriter(Simulation &simulation, bool isBinary=false)
Constructor.
Saving archive for binary istream.
void setClassName(const char *className)
Set class name string.
void loadInterval(Serializable::IArchive &ar)
Load parameter interval from input archive.
virtual void writeHeader(std::ofstream &out)
Write data that should appear once, at beginning of the file.
Boundary & boundary()
Get the Boundary by reference.
void saveOutputFileName(Serializable::OArchive &ar)
Save output file name to an archive.
Domain & domain()
Get the Domain by reference.
AngleStorage & angleStorage()
Get the AngleStorage by reference.