Simpatico  v1.10
OutputStressTensor.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 "OutputStressTensor.h"
9 #include <util/misc/FileMaster.h>
10 #include <util/mpi/MpiLoader.h>
11 #include <util/space/Tensor.h>
12 #include <util/misc/ioUtil.h>
13 #include <util/format/Int.h>
14 #include <util/format/Dbl.h>
15 
16 #include <sstream>
17 
18 namespace DdMd
19 {
20 
21  using namespace Util;
22 
23  /*
24  * Constructor.
25  */
27  : Analyzer(simulation),
28  nSample_(0),
29  isInitialized_(false)
30  { setClassName("OutputStressTensor"); }
31 
32  /*
33  * Read interval and outputFileName.
34  */
35  void OutputStressTensor::readParameters(std::istream& in)
36  {
37  readInterval(in);
39 
40  #if 0
41  std::string filename;
42  filename = outputFileName();
43  simulation().fileMaster().openOutputFile(filename, outputFile_);
44  #endif
45  isInitialized_ = true;
46  }
47 
48  /*
49  * Load internal state from an archive.
50  */
52  {
53  loadInterval(ar);
55 
56  MpiLoader<Serializable::IArchive> loader(*this, ar);
57  loader.load(nSample_);
58 
59  #if 0
60  std::string filename;
61  filename = outputFileName();
62  simulation().fileMaster().openOutputFile(filename, outputFile_);
63  #endif
64  isInitialized_ = true;
65  }
66 
67  /*
68  * Save internal state to an archive.
69  */
71  {
72  saveInterval(ar);
74  ar << nSample_;
75  }
76 
77 
78  /*
79  * Read interval and outputFileName.
80  */
82  { nSample_ = 0; }
83 
84  /*
85  * Open outputfile
86  */
88  {
89  if (simulation().domain().isMaster()) {
90  std::string filename;
91  filename = outputFileName();
92  simulation().fileMaster().openOutputFile(filename, outputFile_);
93  }
94  }
95 
96  /*
97  * Dump configuration to file
98  */
99  void OutputStressTensor::sample(long iStep)
100  {
101  if (isAtInterval(iStep)) {
102  Simulation& sys = simulation();
103  sys.computeVirialStress();
104  sys.computeKineticStress();
105  if (sys.domain().isMaster()) {
106  Tensor virial = sys.virialStress();
107  Tensor kinetic = sys.kineticStress();
108  Tensor total;
109  total.add(virial, kinetic);
110  outputFile_ << Int(iStep, 10)
111  << Dbl(virial(0,0), 20)
112  << Dbl(virial(0,1), 20)
113  << Dbl(virial(0,2), 20)
114  << Dbl(virial(1,0), 20)
115  << Dbl(virial(1,1), 20)
116  << Dbl(virial(1,2), 20)
117  << Dbl(virial(2,0), 20)
118  << Dbl(virial(2,1), 20)
119  << Dbl(virial(2,2), 20)
120  << Dbl(kinetic(0,0), 20)
121  << Dbl(kinetic(0,1), 20)
122  << Dbl(kinetic(0,2), 20)
123  << Dbl(kinetic(1,0), 20)
124  << Dbl(kinetic(1,1), 20)
125  << Dbl(kinetic(1,2), 20)
126  << Dbl(kinetic(2,0), 20)
127  << Dbl(kinetic(2,1), 20)
128  << Dbl(kinetic(2,2), 20)
129  << Dbl(total(0,0), 20)
130  << Dbl(total(0,1), 20)
131  << Dbl(total(0,2), 20)
132  << Dbl(total(1,0), 20)
133  << Dbl(total(1,1), 20)
134  << Dbl(total(1,2), 20)
135  << Dbl(total(2,0), 20)
136  << Dbl(total(2,1), 20)
137  << Dbl(total(2,2), 20)
138  << std::endl;
139  }
140 
141  ++nSample_;
142  }
143  }
144 
145 }
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.
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
void readInterval(std::istream &in)
Read parameter interval from file.
Wrapper for a double precision number, for formatted ostream output.
Definition: Dbl.h:39
Parallel domain decomposition (DD) MD simulation.
Main object for a domain-decomposition MD simulation.
A Tensor represents a Cartesian tensor.
Definition: Tensor.h:32
virtual void save(Serializable::OArchive &ar)
Save internal state to an archive.
Tensor & add(const Tensor &t1, const Tensor &t2)
Add tensors t1 and t2.
Definition: Tensor.h:567
Saving / output archive for binary ostream.
void loadOutputFileName(Serializable::IArchive &ar)
Load output file name to an archive.
FileMaster & fileMaster()
Get the associated FileMaster by reference.
bool isAtInterval(long counter) const
Return true iff counter is a multiple of the interval.
Utility classes for scientific computation.
Definition: accumulators.mod:1
void load(Data &value)
Load and broadcast a single Data value.
Definition: MpiLoader.h:137
Wrapper for an int, for formatted ostream output.
Definition: Int.h:36
Tensor kineticStress() const
Return total kinetic stress.
virtual void sample(long iStep)
Dump configuration to file.
const std::string & outputFileName() const
Return outputFileName string.
bool isMaster() const
Is this the master processor (gridRank == 0) ?
Definition: Domain.h:313
void computeVirialStress()
Calculate and store all virial stress contributions.
OutputStressTensor(Simulation &simulation)
Constructor.
Saving archive for binary istream.
Provides methods for MPI-aware loading of data from input archive.
Definition: MpiLoader.h:43
void setClassName(const char *className)
Set class name string.
void loadInterval(Serializable::IArchive &ar)
Load parameter interval from input archive.
virtual void setup()
Setup - open output file.
void saveOutputFileName(Serializable::OArchive &ar)
Save output file name to an archive.
Domain & domain()
Get the Domain by reference.
Tensor virialStress() const
Return total virial stress.
virtual void clear()
Clear nSample counter.
void computeKineticStress()
Calculate and store kinetic stress.
virtual void loadParameters(Serializable::IArchive &ar)
Load internal state from an archive.
virtual void readParameters(std::istream &in)
Read dumpPrefix and interval.