SSAGES  0.9.3
Software Suite for Advanced General Ensemble Simulations
ForwardFlux.h
1 
22 #pragma once
23 
24 #include "Method.h"
25 #include <fstream>
26 #include <random>
27 #include <deque>
28 #include "sys/stat.h"
29 
30 namespace SSAGES
31 {
33 
38  class ForwardFlux : public Method
39  {
40  protected:
43  {
44  public:
45  unsigned int l;
46  unsigned int n;
47  unsigned int a;
48  unsigned int lprev;
49  unsigned int nprev;
50  unsigned int aprev;
51 
53 
61  FFSConfigID(const unsigned int l,
62  const unsigned int n,
63  const unsigned int a,
64  const unsigned int lprev,
65  const unsigned int nprev,
66  const unsigned int aprev):
68  {}
69 
72  l(0),n(0),a(0),lprev(0),nprev(0),aprev(0)
73  {}
74  };
75 
78  double _ninterfaces;
79 
81  std::vector<double> _interfaces;
82 
85 
88 
90  double _cvvalue;
91 
93  double _rate;
94 
96  std::vector<FFSConfigID> Lambda0ConfigLibrary;
97 
100 
102  unsigned int _N0Target;
103 
105  double _fluxA0;
106 
109  std::vector<unsigned int> _M;
110 
112  std::vector<unsigned int> _A;
113 
115  //bool _computefluxA0;
116 
118  std::vector<double> _P;
119 
122  std::vector<unsigned int> _S;
123 
127  std::vector<unsigned int> _N ;
128 
133 
136 
139 
142 
145 
149  unsigned int _nfailure_total;
150 
153  std::vector<std::vector<double>> _pB;
154 
156  unsigned int _current_interface;
157 
163  std::deque<FFSConfigID> FFSConfigIDQueue;
164 
166  std::string _output_directory;
167 
169  std::ofstream _trajectory_file;
170 
172  std::default_random_engine _generator;
173 
175  unsigned int iteration_;
176 
177  //-----------------------------------------------------------------
178  // Private Functions
179  //-----------------------------------------------------------------
180 
182  void CheckInitialStructure(const CVList&);
183 
185  void WriteInitialFlux();
186 
190 
192 
197  bool HasReturnedToA(double current);
198 
200 
208  int HasCrossedInterface(double current, double prev, unsigned int i);
209 
211 
216  void WriteFFSConfiguration(Snapshot *snapshot, FFSConfigID& ffsconfig, bool wassuccess);
217 
219 
224  void ReadFFSConfiguration(Snapshot *snapshot, FFSConfigID& ffsconfig, bool wassuccess);
225 
227  void ComputeInitialFlux(Snapshot*, const CVList&);
228 
230  virtual void CheckForInterfaceCrossings(Snapshot*, const class CVManager&) = 0;
231 
233  virtual void InitializeQueue(Snapshot*, const CVList&) =0;
234 
236 
241 
243  void PrintQueue();
244 
246  void PopQueueMPI(Snapshot*, const CVList&, unsigned);
247 
249 
253 
256 
259 
262  void AppendTrajectoryFile(Snapshot*, std::ofstream&);
263 
265  void OpenTrajectoryFile(std::ofstream&);
266 
267  public:
269 
284  ForwardFlux(const MPI_Comm& world,
285  const MPI_Comm& comm,
286  double ninterfaces, std::vector<double> interfaces,
287  unsigned int N0Target, std::vector<unsigned int> M,
288  bool initialFluxFlag, bool saveTrajectories,
289  unsigned int currentInterface, std::string output_directory, unsigned int frequency) :
290  Method(frequency, world, comm), _ninterfaces(ninterfaces), _interfaces(interfaces), _N0Target(N0Target),
291  _M(M), _initialFluxFlag(initialFluxFlag), _saveTrajectories(saveTrajectories), _current_interface(currentInterface),
292  _output_directory(output_directory), _generator(1), iteration_(0)
293  {
294  //_output_directory = "FFSoutput";
295  mkdir(_output_directory.c_str(),S_IRWXU); //how to make directory?
296 
297  //_current_interface = 0;
298  _N0TotalSimTime = 0;
299 
300  if (!_initialFluxFlag)
301  initializeQueueFlag = true;
302 
303  _A.resize(_ninterfaces);
304  _P.resize(_ninterfaces);
305  _S.resize(_ninterfaces);
306  _N.resize(_ninterfaces);
307 
308  //_N[_current_interface] = _N0Target;
309  /*_M.resize(_ninterfaces);
310 
311  //code for setting up simple simulation and debugging
312  _ninterfaces = 5;
313  int i;
314  _interfaces.resize(_ninterfaces);
315  _interfaces[0]=-1.0;
316  _interfaces[1]=-0.95;
317  _interfaces[2]=-0.8;
318  _interfaces[3]= 0;
319  _interfaces[4]= 1.0;
320 
321  _saveTrajectories = true;
322 
323  _initialFluxFlag = true;
324 
325  for(i=0;i<_ninterfaces;i++) _M[i] = 50;
326 
327  _N0Target = 100;*/
328  //_N0Target = 100;
329  _nfailure_total = 0;
330 
331 
332  //check if interfaces monotonically increase or decrease
333  bool errflag = false;
334  if (_interfaces[0]< _interfaces[1]) _interfaces_increase = true;
335  else if (_interfaces[0] > _interfaces[1]) _interfaces_increase = false;
336  else errflag = true;
337  for (unsigned int i=0;i<_ninterfaces-1;i++)
338  {
339  if((_interfaces_increase) && (_interfaces[i] >= _interfaces[i+1]))
340  errflag = true;
341  else if ((!_interfaces_increase) && (_interfaces[i] <= _interfaces[i+1]))
342  errflag = true;
343  }
344  if (errflag)
345  {
346  std::cerr << "Error! The interfaces are poorly defined. They must be monotonically increasing or decreasing and cannot equal one another! Please fix this.\n";
347  for (auto interface : _interfaces)
348  std::cerr << interface << " ";
349  std::cerr << "\n";
350  MPI_Abort(world_, EXIT_FAILURE);
351  }
352 
353  // This is to generate an artificial Lambda0ConfigLibrary, Hadi's code does this for real
354  // THIS SHOULD BE SOMEWHERE ELSE!!!
356  std::normal_distribution<double> distribution(0,1);
357  for (unsigned int i = 0; i < _N0Target ; i++)
358  {
359  Lambda0ConfigLibrary[i].l = 0;
360  Lambda0ConfigLibrary[i].n = i;
361  Lambda0ConfigLibrary[i].a = 0;
362  Lambda0ConfigLibrary[i].lprev = 0;
363  Lambda0ConfigLibrary[i].nprev = i;
364  Lambda0ConfigLibrary[i].aprev = 0;
365  //FFSConfigID ffsconfig = Lambda0ConfigLibrary[i];
366  }
367  /*// Write the dump file out
368  std::ofstream file;
369  std::string filename = _output_directory + "/l" + std::to_string(ffsconfig.l) + "-n" + std::to_string(ffsconfig.n) + ".dat";
370  file.open(filename.c_str());
371 
372  //first line gives ID of where it came from
373  file << ffsconfig.lprev << " " << ffsconfig.nprev << " " << ffsconfig.aprev << "\n";
374  //write position and velocity
375  file << "1 -1 0 0 " << distribution(_generator) << " " << distribution(_generator) << " 0\n";
376 
377  }
378  */
380  }
381 
383  void PreSimulation(Snapshot* snapshot, const class CVManager& cvmanager) override;
384 
386  virtual void PostIntegration(Snapshot* snapshot, const class CVManager& cvmanager) override = 0;
387 
389  void PostSimulation(Snapshot* snapshot, const class CVManager& cvmanager) override;
390 
392  static ForwardFlux* Build(const Json::Value& json,
393  const MPI_Comm& world,
394  const MPI_Comm& comm,
395  const std::string& path);
396  };
397 }
398 
399 /*
400 File Formats:
401 _indexfile
402 interface(some integer) dump_file_name(a string that contains interface and trial number)
403 example: 1 dump_1_10.xyz
404 
405 dumpfile
406 atomid posx posy posz vx vy vz
407 
408 
409 */
Collective variable manager.
Definition: CVManager.h:43
Nested class to store different FFS Config IDs.
Definition: ForwardFlux.h:43
unsigned int lprev
Previous Interface number (i.e. traj I came from)
Definition: ForwardFlux.h:48
unsigned int aprev
Previous Attempt number.
Definition: ForwardFlux.h:50
FFSConfigID(const unsigned int l, const unsigned int n, const unsigned int a, const unsigned int lprev, const unsigned int nprev, const unsigned int aprev)
Constructor.
Definition: ForwardFlux.h:61
unsigned int nprev
Previous Configuration Number.
Definition: ForwardFlux.h:49
unsigned int a
Attempt number.
Definition: ForwardFlux.h:47
unsigned int n
Configuration Number.
Definition: ForwardFlux.h:46
FFSConfigID()
Empty Constructor.
Definition: ForwardFlux.h:71
unsigned int l
Interface number.
Definition: ForwardFlux.h:45
ForwardFlux sampling method.
Definition: ForwardFlux.h:39
double _rate
rate constant
Definition: ForwardFlux.h:93
double _fluxA0
Flux of trajectories out of state A. Denoted PhiA0 over h_A in Allen2009.
Definition: ForwardFlux.h:105
std::vector< FFSConfigID > Lambda0ConfigLibrary
Data structure that holds a Library N0 configurations at lambda0.
Definition: ForwardFlux.h:96
virtual void InitializeQueue(Snapshot *, const CVList &)=0
Initialize the Queue.
bool _pop_tried_but_empty_queue
Definition: ForwardFlux.h:132
std::string _output_directory
Directory of FFS output.
Definition: ForwardFlux.h:166
void AppendTrajectoryFile(Snapshot *, std::ofstream &)
static ForwardFlux * Build(const Json::Value &json, const MPI_Comm &world, const MPI_Comm &comm, const std::string &path)
Build a derived method from JSON node.
void PostSimulation(Snapshot *snapshot, const class CVManager &cvmanager) override
Method call post simulation.
Definition: ForwardFlux.cpp:48
double _cvvalue_previous
Previous cv position, used to determine if you've crossed an interface since last time.
Definition: ForwardFlux.h:87
void PreSimulation(Snapshot *snapshot, const class CVManager &cvmanager) override
Method call prior to simulation initiation.
Definition: ForwardFlux.cpp:39
void ComputeTransitionProbabilities()
Compute the probability of going from each lambda_i to lambda_{i+1}.
unsigned int _nfailure_total
Definition: ForwardFlux.h:149
std::vector< double > _interfaces
FFS Interfaces.
Definition: ForwardFlux.h:81
unsigned int iteration_
Method iteration counter/.
Definition: ForwardFlux.h:175
std::vector< unsigned int > _M
Definition: ForwardFlux.h:109
void OpenTrajectoryFile(std::ofstream &)
Take the current config in snapshot and append it to the provided ofstream.
FFSConfigID myFFSConfigID
The current FFSConfigID of this MPI process.
Definition: ForwardFlux.h:141
bool _saveTrajectories
should the FFS trajectories be saved
Definition: ForwardFlux.h:144
void ComputeInitialFlux(Snapshot *, const CVList &)
Compute Initial Flux.
ForwardFlux(const MPI_Comm &world, const MPI_Comm &comm, double ninterfaces, std::vector< double > interfaces, unsigned int N0Target, std::vector< unsigned int > M, bool initialFluxFlag, bool saveTrajectories, unsigned int currentInterface, std::string output_directory, unsigned int frequency)
Constructor.
Definition: ForwardFlux.h:284
void FluxBruteForce(Snapshot *, const CVList &)
Compute the flux via brute force.
void PrintQueue()
Print the queue, useful for debugging.
bool _interfaces_increase
Interfaces must monotonically increase (or decrease), this determines whether going to the 'next' int...
Definition: ForwardFlux.h:84
bool _initialFluxFlag
if 1, compute initial flux
Definition: ForwardFlux.h:135
std::vector< unsigned int > _N
Definition: ForwardFlux.h:127
void WriteFFSConfiguration(Snapshot *snapshot, FFSConfigID &ffsconfig, bool wassuccess)
Write a file corresponding to FFSConfigID from current snapshot.
std::vector< std::vector< double > > _pB
Definition: ForwardFlux.h:153
std::ofstream _trajectory_file
file to which the current trajectory is written to
Definition: ForwardFlux.h:169
void CheckInitialStructure(const CVList &)
Function that checks the initial structure that user provides.
Definition: ForwardFlux.cpp:61
void ReadFFSConfiguration(Snapshot *snapshot, FFSConfigID &ffsconfig, bool wassuccess)
Read a file corresponding to a FFSConfigID into current snapshot.
virtual void CheckForInterfaceCrossings(Snapshot *, const class CVManager &)=0
Function that checks if interfaces have been crossed (different for each FFS flavor)
std::vector< unsigned int > _S
Definition: ForwardFlux.h:122
std::vector< double > _P
Flag to determine wheter fluxA0 should be calculated, seems not using this.
Definition: ForwardFlux.h:118
bool initializeQueueFlag
if 1, initialize the Queue
Definition: ForwardFlux.h:138
void WriteInitialFlux()
Function to compute and write the initial flux.
double _N0TotalSimTime
Total Simulation Time spent in accumulating \ _N0.
Definition: ForwardFlux.h:99
std::default_random_engine _generator
random number generator
Definition: ForwardFlux.h:172
unsigned int _current_interface
Current Interface.
Definition: ForwardFlux.h:156
double _cvvalue
current cv position
Definition: ForwardFlux.h:90
void PopQueueMPI(Snapshot *, const CVList &, unsigned)
Pop the queue, do MPI so that all procs maintain the same queue.
std::deque< FFSConfigID > FFSConfigIDQueue
Definition: ForwardFlux.h:163
virtual void PostIntegration(Snapshot *snapshot, const class CVManager &cvmanager) override=0
Method call post integration.
unsigned int _N0Target
Number of configurations to store at lambda0, target.
Definition: ForwardFlux.h:102
void ComputeCommittorProbability(Snapshot *)
When simulation is finished, recursively parse through the trajectories that reached B or failed back...
int HasCrossedInterface(double current, double prev, unsigned int i)
Function checks if configuration has crossed interface specified since the last check.
Definition: ForwardFlux.cpp:79
bool HasReturnedToA(double current)
Function checks if configuration has returned to A.
void ReconstructTrajectories(Snapshot *)
When simulation is finished, parse through the trajectories that reached B, and reconstruct the compl...
std::vector< unsigned int > _A
Number of attempts from interface i.
Definition: ForwardFlux.h:112
Interface for Method implementations.
Definition: Method.h:44
mxx::comm world_
Global MPI communicator.
Definition: Method.h:46
Class containing a snapshot of the current simulation in time.
Definition: Snapshot.h:48
std::vector< CollectiveVariable * > CVList
List of Collective Variables.
Definition: types.h:51