Simpatico  v1.10
tools/analyzers/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 <tools/processor/Processor.h>
10 #include <tools/storage/Configuration.h>
11 #include <tools/storage/AtomStorage.h>
12 #include <tools/storage/GroupStorage.h>
13 #include <util/mpi/MpiLoader.h>
14 
15 #include <fstream>
16 #include <ios>
17 
18 namespace Tools
19 {
20 
21  using namespace Util;
22 
23  /*
24  * Constructor.
25  */
27  bool isBinary)
28  : Analyzer(processor),
29  nSample_(0),
30  isInitialized_(false),
31  isBinary_(isBinary),
32  boundaryPtr_(0),
33  atomStoragePtr_(0)
34  #ifdef SIMP_BOND
35  , bondStoragePtr_(0)
36  #endif
37  #ifdef SIMP_ANGLE
38  , angleStoragePtr_(0)
39  #endif
40  #ifdef SIMP_DIHEDRAL
41  , dihedralStoragePtr_(0)
42  #endif
43  {
44  setClassName("TrajectoryWriter");
45  boundaryPtr_ = &processor.boundary();
46 
47  atomStoragePtr_ = &processor.atoms();
48  #ifdef SIMP_BOND
49  bondStoragePtr_ = &processor.bonds();
50  #endif
51  #ifdef SIMP_ANGLE
52  angleStoragePtr_ = &processor.angles();
53  #endif
54  #ifdef SIMP_DIHEDRAL
55  dihedralStoragePtr_ = &processor.dihedrals();
56  #endif
57  }
58 
59 
60  /*
61  * Constructor.
62  */
65  bool isBinary)
66  : Analyzer(configuration, fileMaster),
67  nSample_(0),
68  isInitialized_(false),
69  isBinary_(isBinary),
70  boundaryPtr_(0),
71  atomStoragePtr_(0)
72  #ifdef SIMP_BOND
73  , bondStoragePtr_(0)
74  #endif
75  #ifdef SIMP_ANGLE
76  , angleStoragePtr_(0)
77  #endif
78  #ifdef SIMP_DIHEDRAL
79  , dihedralStoragePtr_(0)
80  #endif
81  {
82  setClassName("TrajectoryWriter");
83  boundaryPtr_ = &configuration.boundary();
84 
85  atomStoragePtr_ = &configuration.atoms();
86  #ifdef SIMP_BOND
87  bondStoragePtr_ = &configuration.bonds();
88  #endif
89  #ifdef SIMP_ANGLE
90  angleStoragePtr_ = &configuration.angles();
91  #endif
92  #ifdef SIMP_DIHEDRAL
93  dihedralStoragePtr_ = &configuration.dihedrals();
94  #endif
95  }
96 
97  /*
98  * Read interval and outputFileName.
99  */
100  void TrajectoryWriter::readParameters(std::istream& in)
101  {
102  readInterval(in);
103  readOutputFileName(in);
104  isInitialized_ = true;
105  }
106 
107  #if 0
108  /*
109  * Load internal state from an archive.
110  */
112  {
113  loadInterval(ar);
114  loadOutputFileName(ar);
115 
116  MpiLoader<Serializable::IArchive> loader(*this, ar);
117  loader.load(nSample_);
118 
119  isInitialized_ = true;
120  }
121 
122  /*
123  * Save internal state to output archive.
124  */
126  {
127  saveInterval(ar);
128  saveOutputFileName(ar);
129  ar << nSample_;
130  }
131  #endif
132 
133  /*
134  * Setup - open the trajectory file.
135  */
137  {
138  if (isBinary()) {
139  fileMaster().openOutputFile(outputFileName(), outputFile_,
140  std::ios::out | std::ios::binary);
141  } else {
142  fileMaster().openOutputFile(outputFileName(), outputFile_);
143  }
144  writeHeader(outputFile_);
145  }
146 
147  /*
148  * Write a frame to file.
149  */
150  void TrajectoryWriter::sample(long iStep)
151  {
152  if (isAtInterval(iStep)) {
153  writeFrame(outputFile_, iStep);
154  ++nSample_;
155  }
156  }
157 
158  /*
159  * Clear sample counter and close file.
160  */
162  {
163  nSample_ = 0;
164  if (outputFile_.is_open()) {
165  outputFile_.close();
166  }
167  }
168 
169  /*
170  * Clear sample counter and close output file.
171  */
173  { clear(); }
174 
175 }
virtual void readParameters(std::istream &in)
Read parameters and initialize.
void readOutputFileName(std::istream &in)
Read outputFileName from file.
A post-processor for analyzing outputs of MD simulations.
Definition: Processor.h:30
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
Abstract base for periodic output and/or analysis actions.
AtomStorage & atoms()
Get the AtomStorage.
virtual void sample(long iStep)
Write frame to file.
An instantaneous molecular dynamics configuration.
Definition: Configuration.h:40
Saving / output archive for binary ostream.
virtual void save(Serializable::OArchive &ar)
Saves all parameters to an archive.
virtual void writeHeader(std::ofstream &out)
Write data that should appear once, at beginning of the file.
Utility classes for scientific computation.
Definition: accumulators.mod:1
void load(Data &value)
Load and broadcast a single Data value.
Definition: MpiLoader.h:137
virtual void setup()
Open the trajectory file.
Configuration & configuration()
Get the parent Configuration by reference.
Single-processor classes for pre- and post-processing MD trajectories.
void readInterval(std::istream &in)
Read parameter interval from file.
virtual void writeFrame(std::ofstream &out, long iStep)=0
Write data that should appear in every frame.
virtual void loadParameters(Serializable::IArchive &ar)
Load state from archive, without adding Begin and End lines.
A FileMaster manages input and output files for a simulation.
Definition: FileMaster.h:142
bool isAtInterval(long counter) const
Return true iff counter is a multiple of the interval.
GroupStorage< 3 > & angles()
Get the Angle storage.
GroupStorage< 4 > & dihedrals()
Get the Dihedral storage.
Saving archive for binary istream.
TrajectoryWriter(Processor &processor, bool isBinary)
Constructor.
Provides methods for MPI-aware loading of data from input archive.
Definition: MpiLoader.h:43
Boundary & boundary()
Get the Boundary by non-const reference.
void setClassName(const char *className)
Set class name string.
bool isBinary() const
Is the file format binary (true) or text (false)?
FileMaster & fileMaster()
Get an associated FileMaster by reference.
virtual void output()
Close ouput file.
virtual void clear()
Clear nSample counter.
GroupStorage< 2 > & bonds()
Get the Bond storage.
const std::string & outputFileName() const
Return outputFileName string.