Path Integral Quantum Monte Carlo
communicator.cpp
Go to the documentation of this file.
1 
8 #include "communicator.h"
9 #include <filesystem>
10 
11 // ---------------------------------------------------------------------------
12 // ---------------------------------------------------------------------------
13 // FILE CLASS ----------------------------------------------------------------
14 // ---------------------------------------------------------------------------
15 // ---------------------------------------------------------------------------
16 
17 /**************************************************************************/
28 File::File(string _type, string _data, string ensemble, string outDir) {
29 
30  /* The file name */
31  name = str(format("%s/%s-%s-%s.dat") % outDir % ensemble % _type % _data);
32 
33  /* Create a backup name */
34  bakname = str(format("%s/%s-%s-%s.bak") % outDir % ensemble % _type % _data);
35 
36  /* Determine if the file already exists */
37  exists_ = filesystem::exists(name);
38 }
39 
40 /**************************************************************************/
47 File::File(string _name) : name(_name), bakname() {
48 
49 }
50 
51 /**************************************************************************/
54 void File::close() {
55  if (rwfile.is_open())
56  rwfile.close();
57 }
58 
59 /**************************************************************************/
64 void File::open(ios_base::openmode mode) {
65 
66  /* Convert the filename to a c string, and open the file */
67  rwfile.open(name.c_str(), mode);
68  if (!rwfile) {
69  cerr << "Unable to process file: " << name << endl;
70  exit(EXIT_FAILURE);
71  }
72 }
73 
74 /**************************************************************************/
80 void File::open(ios_base::openmode mode, string _name) {
81 
82  /* Convert the filename to a c string, and open the file */
83  rwfile.open(_name.c_str(), mode);
84  if (!rwfile) {
85  cerr << "Unable to process file: " << _name << endl;
86  exit(EXIT_FAILURE);
87  }
88 }
89 
90 /**************************************************************************/
96 void File::reset() {
97 
98  /* Close the current file */
99  close();
100 
101  /* Open a backup file and replace any content if it exists */
102  open(ios::out|ios::trunc,bakname);
103 
104  /* Write the generic header to the file */
105 }
106 
107 /**************************************************************************/
112 void File::rename() {
113 
114  close();
115 
116  /* /1* Create the filesystem paths *1/ */
117  /* boost::filesystem::path datpath(name); */
118  /* boost::filesystem::path bakpath(bakname); */
119 
120  /* Perform the rename */
121  filesystem::rename(bakname.c_str(), name.c_str());
122 }
123 
124 // ---------------------------------------------------------------------------
125 // ---------------------------------------------------------------------------
126 // COMMUNICATOR CLASS --------------------------------------------------------
127 // ---------------------------------------------------------------------------
128 // ---------------------------------------------------------------------------
129 
130 /**************************************************************************/
137 void Communicator::init(double _tau, bool outputWorldline, string _initName,
138  string _fixedName)
139 {
140 
141  /* Set local class variables */
142  baseDir = "OUTPUT";
143  initName = _initName;
144  fixedName = _fixedName;
145  tau = _tau;
146 
147 
148  /* Determine the ensemble and unique parameter file string or dataname */
149  if (!constants()->canonical()) {
150  ensemble = "gce";
151  dataName = str(format("%06.3f-%07.3f-%+08.3f-%7.5f-%s") % constants()->T()
152  % constants()->L() % constants()->mu() % tau % constants()->id());
153  }
154  else {
155  ensemble = "ce";
156  dataName = str(format("%06.3f-%04d-%06.3f-%7.5f-%s") % constants()->T()
157  % constants()->initialNumParticles()
158  % (1.0*constants()->initialNumParticles()/constants()->V())
159  % tau % constants()->id());
160  }
161 
162  /* Check to make sure the correct directory structure for OUTPUT files is
163  * in place. */
164  filesystem::path outputPath(baseDir);
165  filesystem::create_directory(outputPath);
166 
167  /* If we have cylinder output files, add the required directory. */
168  if (constants()->extPotentialType().find("tube") != string::npos) {
169  filesystem::path cylPath(baseDir + "/CYLINDER");
170  filesystem::create_directory(cylPath);
171  }
172 
173  /* A header line for the files */
174  header = str(format("# PIMCID: %s\n") % constants()->id());
175 
176  /* Depending on whether or not we are restarting the simulations, the open mode
177  * changes. */
178  if (constants()->restart()) {
179  mode = ios::out|ios::app;
180  }
181  else {
182  mode = ios::out;
183  }
184 }
185 
186 /**************************************************************************/
189 void Communicator::initFile(string type) {
190 
191  /* Check a possible initialization file */
192  if (type.find("init") != string::npos ) {
193 
194  /* We need to determine the name of the state file. i.e. does it need
195  * an integer appended after it? */
196  string stateName = "state";
197 
198  /* If we have a numerical label, append it to the name of state */
199  if (type != "init")
200  stateName += stateName.substr(4,string::npos);
201 
202  /* There are only two reasons we would need an init file, either we are
203  * restarting, or starting from a given initialization file */
204  if (constants()->restart())
205  file_.insert(type, new File(stateName,dataName,ensemble,baseDir));
206  else
207  file_.insert(type, new File(initName));
208 
209  file_.at(type).open(ios::in);
210  }
211  /* Initialize a possible fixed coordinate file */
212  else if (type == "fixed") {
213  file_.insert(type,new File(fixedName));
214  file_.at(type).open(ios::in);
215  }
216  /* All other file types act normally */
217  else {
218  string outDir = baseDir;
219  string ctype = type;
220 
221  /* Deal with possible cylinder output files */
222  if (type.find("cyl_") != string::npos) {
223  outDir = baseDir + "/CYLINDER";
224  ctype.erase(0,4);
225  }
226 
227  /* Construct the file and open it */
228  file_.insert(type, new File(ctype,dataName,ensemble,outDir));
229  file_.at(type).open(mode);
230 
231  /* Write the header line if the file doesn't exist */
232  if (!file_.at(type).exists())
233  file_.at(type).stream() << header;
234  }
235 }
236 
237 /**************************************************************************/
241 
242  /* We create a new dataName based on the posibility of updated paramters. */
243 
244  /* Determine the ensemble and unique parameter file string or dataname */
245  if (!constants()->canonical()) {
246  dataName = str(format("%06.3f-%07.3f-%+08.3f-%7.5f-%s") % constants()->T()
247  % constants()->L() % constants()->mu() % tau % constants()->id());
248  }
249  else {
250  dataName = str(format("%06.3f-%04d-%06.3f-%7.5f-%s") % constants()->T()
251  % constants()->initialNumParticles()
252  % (1.0*constants()->initialNumParticles()/constants()->V())
253  % tau % constants()->id());
254  }
255 
256  /* Perform the rename for each file in the map */
257  for (auto const& [key, filePtr] : file_)
258  {
259 
260  string oldName(filePtr->name);
261 
262  /* Replace with the new data name, we need to do this for both name and
263  * backup name. */
264  filePtr->name.replace(filePtr->name.end()-dataName.length()-4,filePtr->name.end()-4,dataName);
265  filePtr->bakname.replace(filePtr->bakname.end()-dataName.length()-4,filePtr->bakname.end()-4,dataName);
266 
267  /* Perform the rename */
268  filesystem::rename(oldName.c_str(), filePtr->name.c_str());
269  }
270 }
271 /**************************************************************************/
276 {
277  static Communicator inst;
278  return &inst;
279 }
Performs input/output.
Definition: communicator.h:77
void init(double, bool, string, string)
Initialize the output files.
static Communicator * getInstance()
This public method gets an instance of the Communicator object, only one can ever exist at a time.
void updateNames()
Update the data name and rename any existing files.
A basic input/output file class.
Definition: communicator.h:26
void reset()
Reset a file.
void rename()
Rename a file.
File(string, string, string, string)
Constructor.
void close()
Close the file.
void open(ios_base::openmode)
Open the file.
ensemble
Each move can operate on only the digaonal ensemble, only the off-diagonal ensemble,...
Definition: common.h:133
Class definitions for all file input/output.
ConstantParameters * constants()
Global public access to the constants.
Definition: constants.h:201