SSAGES  0.8.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  FFSConfigID(const unsigned int l,
54  const unsigned int n,
55  const unsigned int a,
56  const unsigned int lprev,
57  const unsigned int nprev,
58  const unsigned int aprev):
59  l(l),n(n),a(a),lprev(lprev),nprev(nprev),aprev(aprev)
60  {}
61 
64  l(0),n(0),a(0),lprev(0),nprev(0),aprev(0)
65  {}
66  };
67 
70  double _ninterfaces;
71 
73  std::vector<double> _interfaces;
74 
77 
80 
82  double _cvvalue;
83 
85  double _rate;
86 
88  std::vector<FFSConfigID> Lambda0ConfigLibrary;
89 
92 
94  unsigned int _N0Target;
95 
97  double _fluxA0;
98 
101  std::vector<unsigned int> _M;
102 
104  std::vector<unsigned int> _A;
105 
107  //bool _computefluxA0;
108 
110  std::vector<double> _P;
111 
114  std::vector<unsigned int> _S;
115 
119  std::vector<unsigned int> _N ;
120 
125 
128 
131 
134 
137 
141  unsigned int _nfailure_total;
142 
145  std::vector<std::vector<double>> _pB;
146 
148  unsigned int _current_interface;
149 
155  std::deque<FFSConfigID> FFSConfigIDQueue;
156 
158  std::string _output_directory;
159 
161  std::ofstream _trajectory_file;
162 
164  std::default_random_engine _generator;
165 
167  unsigned int iteration_;
168 
169  //-----------------------------------------------------------------
170  // Private Functions
171  //-----------------------------------------------------------------
172 
174  void CheckInitialStructure(const CVList&);
175 
177  void WriteInitialFlux();
178 
181  void AddNewIDsToQueue();
182 
184  bool HasReturnedToA(double);
185 
187 
195  int HasCrossedInterface(double current, double prev, unsigned int i);
196 
198  void WriteFFSConfiguration(Snapshot *snapshot,FFSConfigID& ffsconfig, bool wassuccess);
199 
202 
204  void ComputeInitialFlux(Snapshot*, const CVList&);
205 
207  virtual void CheckForInterfaceCrossings(Snapshot*, const class CVManager&) = 0;
208 
210  virtual void InitializeQueue(Snapshot*, const CVList&) =0;
211 
213 
218 
220  void PrintQueue();
221 
223  void PopQueueMPI(Snapshot*, const CVList&, unsigned);
224 
226 
229  void FluxBruteForce(Snapshot*, const CVList&);
230 
233 
236 
239  void AppendTrajectoryFile(Snapshot*, std::ofstream&);
240 
242  void OpenTrajectoryFile(std::ofstream&);
243 
244  public:
246 
261  ForwardFlux(const MPI_Comm& world,
262  const MPI_Comm& comm,
263  double ninterfaces, std::vector<double> interfaces,
264  unsigned int N0Target, std::vector<unsigned int> M,
265  bool initialFluxFlag, bool saveTrajectories,
266  unsigned int currentInterface, std::string output_directory, unsigned int frequency) :
267  Method(frequency, world, comm), _ninterfaces(ninterfaces), _interfaces(interfaces), _N0Target(N0Target),
268  _M(M), _initialFluxFlag(initialFluxFlag), _saveTrajectories(saveTrajectories), _current_interface(currentInterface),
269  _output_directory(output_directory), _generator(1), iteration_(0)
270  {
271  //_output_directory = "FFSoutput";
272  mkdir(_output_directory.c_str(),S_IRWXU); //how to make directory?
273 
274  //_current_interface = 0;
275  _N0TotalSimTime = 0;
276 
277  if (!_initialFluxFlag)
278  initializeQueueFlag = true;
279 
280  _A.resize(_ninterfaces);
281  _P.resize(_ninterfaces);
282  _S.resize(_ninterfaces);
283  _N.resize(_ninterfaces);
284 
285  //_N[_current_interface] = _N0Target;
286  /*_M.resize(_ninterfaces);
287 
288  //code for setting up simple simulation and debugging
289  _ninterfaces = 5;
290  int i;
291  _interfaces.resize(_ninterfaces);
292  _interfaces[0]=-1.0;
293  _interfaces[1]=-0.95;
294  _interfaces[2]=-0.8;
295  _interfaces[3]= 0;
296  _interfaces[4]= 1.0;
297 
298  _saveTrajectories = true;
299 
300  _initialFluxFlag = true;
301 
302  for(i=0;i<_ninterfaces;i++) _M[i] = 50;
303 
304  _N0Target = 100;*/
305  //_N0Target = 100;
306  _nfailure_total = 0;
307 
308 
309  //check if interfaces monotonically increase or decrease
310  bool errflag = false;
311  if (_interfaces[0]< _interfaces[1]) _interfaces_increase = true;
312  else if (_interfaces[0] > _interfaces[1]) _interfaces_increase = false;
313  else errflag = true;
314  for (unsigned int i=0;i<_ninterfaces-1;i++)
315  {
316  if((_interfaces_increase) && (_interfaces[i] >= _interfaces[i+1]))
317  errflag = true;
318  else if ((!_interfaces_increase) && (_interfaces[i] <= _interfaces[i+1]))
319  errflag = true;
320  }
321  if (errflag)
322  {
323  std::cerr << "Error! The interfaces are poorly defined. They must be monotonically increasing or decreasing and cannot equal one another! Please fix this.\n";
324  for (auto interface : _interfaces)
325  std::cerr << interface << " ";
326  std::cerr << "\n";
327  MPI_Abort(world_, EXIT_FAILURE);
328  }
329 
330  // This is to generate an artificial Lambda0ConfigLibrary, Hadi's code does this for real
331  // THIS SHOULD BE SOMEWHERE ELSE!!!
332  Lambda0ConfigLibrary.resize(_N0Target);
333  std::normal_distribution<double> distribution(0,1);
334  for (unsigned int i = 0; i < _N0Target ; i++)
335  {
336  Lambda0ConfigLibrary[i].l = 0;
337  Lambda0ConfigLibrary[i].n = i;
338  Lambda0ConfigLibrary[i].a = 0;
339  Lambda0ConfigLibrary[i].lprev = 0;
340  Lambda0ConfigLibrary[i].nprev = i;
341  Lambda0ConfigLibrary[i].aprev = 0;
342  //FFSConfigID ffsconfig = Lambda0ConfigLibrary[i];
343  }
344  /*// Write the dump file out
345  std::ofstream file;
346  std::string filename = _output_directory + "/l" + std::to_string(ffsconfig.l) + "-n" + std::to_string(ffsconfig.n) + ".dat";
347  file.open(filename.c_str());
348 
349  //first line gives ID of where it came from
350  file << ffsconfig.lprev << " " << ffsconfig.nprev << " " << ffsconfig.aprev << "\n";
351  //write position and velocity
352  file << "1 -1 0 0 " << distribution(_generator) << " " << distribution(_generator) << " 0\n";
353 
354  }
355  */
356  _pop_tried_but_empty_queue = false;
357  }
358 
360 
364  void PreSimulation(Snapshot* snapshot, const class CVManager& cvmanager) override;
365 
367 
371  virtual void PostIntegration(Snapshot* snapshot, const class CVManager& cvmanager) override = 0;
372 
374 
378  void PostSimulation(Snapshot* snapshot, const class CVManager& cvmanager) override;
379 
381  static ForwardFlux* Build(const Json::Value& json,
382  const MPI_Comm& world,
383  const MPI_Comm& comm,
384  const std::string& path);
385  };
386 }
387 
388 /*
389 File Formats:
390 _indexfile
391 interface(some integer) dump_file_name(a string that contains interface and trial number)
392 example: 1 dump_1_10.xyz
393 
394 dumpfile
395 atomid posx posy posz vx vy vz
396 
397 
398 */
unsigned int iteration_
Method iteration counter/.
Definition: ForwardFlux.h:167
void ReconstructTrajectories(Snapshot *)
When simulation is finished, parse through the trajectories that reached B, and reconstruct the compl...
void PostSimulation(Snapshot *snapshot, const class CVManager &cvmanager) override
Post-simulation hook.
Definition: ForwardFlux.cpp:48
double _rate
rate constant
Definition: ForwardFlux.h:85
Collective variable manager.
Definition: CVManager.h:40
FFSConfigID()
Empty Constructor.
Definition: ForwardFlux.h:63
std::vector< unsigned int > _M
Definition: ForwardFlux.h:101
std::vector< CollectiveVariable * > CVList
List of Collective Variables.
Definition: types.h:51
std::vector< unsigned int > _S
Definition: ForwardFlux.h:114
std::vector< unsigned int > _A
Number of attempts from interface i.
Definition: ForwardFlux.h:104
unsigned int _current_interface
Current Interface.
Definition: ForwardFlux.h:148
bool _pop_tried_but_empty_queue
Definition: ForwardFlux.h:124
std::vector< unsigned int > _N
Definition: ForwardFlux.h:119
Class containing a snapshot of the current simulation in time.
Definition: Snapshot.h:43
void CheckInitialStructure(const CVList &)
Function that checks the initial structure that user provides.
Definition: ForwardFlux.cpp:61
Interface for Method implementations.
Definition: Method.h:43
void WriteFFSConfiguration(Snapshot *snapshot, FFSConfigID &ffsconfig, bool wassuccess)
Write a file corresponding to FFSConfigID from current snapshot.
ForwardFlux sampling method.
Definition: ForwardFlux.h:38
virtual void CheckForInterfaceCrossings(Snapshot *, const class CVManager &)=0
Function that checks if interfaces have been crossed (different for each FFS flavor) ...
double _cvvalue_previous
Previous cv position, used to determine if you&#39;ve crossed an interface since last time...
Definition: ForwardFlux.h:79
unsigned int aprev
Previous Attempt number.
Definition: ForwardFlux.h:50
mxx::comm world_
Global MPI communicator.
Definition: Method.h:46
virtual void PostIntegration(Snapshot *snapshot, const class CVManager &cvmanager) override=0
Post-integration hook.
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:53
void ComputeInitialFlux(Snapshot *, const CVList &)
Compute Initial Flux.
double _cvvalue
current cv position
Definition: ForwardFlux.h:82
bool HasReturnedToA(double)
Function checks if configuration has returned to A.
bool _initialFluxFlag
if 1, compute initial flux
Definition: ForwardFlux.h:127
virtual void InitializeQueue(Snapshot *, const CVList &)=0
Initialize the Queue.
unsigned int a
Attempt number.
Definition: ForwardFlux.h:47
void ComputeTransitionProbabilities()
Compute the probability of going from each lambda_i to lambda_{i+1}.
std::deque< FFSConfigID > FFSConfigIDQueue
Definition: ForwardFlux.h:155
unsigned int lprev
Previous Interface number (i.e. traj I came from)
Definition: ForwardFlux.h:48
bool initializeQueueFlag
if 1, initialize the Queue
Definition: ForwardFlux.h:130
Nested class to store different FFS Config IDs.
Definition: ForwardFlux.h:42
void ComputeCommittorProbability(Snapshot *)
When simulation is finished, recursively parse through the trajectories that reached B or failed back...
void WriteInitialFlux()
Function to compute and write the initial flux.
unsigned int nprev
Previous Configuration Number.
Definition: ForwardFlux.h:49
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
std::ofstream _trajectory_file
file to which the current trajectory is written to
Definition: ForwardFlux.h:161
void ReadFFSConfiguration(Snapshot *, FFSConfigID &, bool)
Read a file corresponding to a FFSConfigID into current snapshot.
void AppendTrajectoryFile(Snapshot *, std::ofstream &)
std::string _output_directory
Directory of FFS output.
Definition: ForwardFlux.h:158
std::vector< double > _P
Flag to determine wheter fluxA0 should be calculated, seems not using this.
Definition: ForwardFlux.h:110
void FluxBruteForce(Snapshot *, const CVList &)
Compute the flux via brute force.
unsigned int _nfailure_total
Definition: ForwardFlux.h:141
double _fluxA0
Flux of trajectories out of state A. Denoted PhiA0 over h_A in Allen2009.
Definition: ForwardFlux.h:97
void PrintQueue()
Print the queue, useful for debugging.
unsigned int n
Configuration Number.
Definition: ForwardFlux.h:46
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:261
unsigned int _N0Target
Number of configurations to store at lambda0, target.
Definition: ForwardFlux.h:94
bool _saveTrajectories
should the FFS trajectories be saved
Definition: ForwardFlux.h:136
void PreSimulation(Snapshot *snapshot, const class CVManager &cvmanager) override
Pre-simulation hook.
Definition: ForwardFlux.cpp:39
std::default_random_engine _generator
random number generator
Definition: ForwardFlux.h:164
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.
double _N0TotalSimTime
Total Simulation Time spent in accumulating \ _N0.
Definition: ForwardFlux.h:91
std::vector< double > _interfaces
FFS Interfaces.
Definition: ForwardFlux.h:73
void OpenTrajectoryFile(std::ofstream &)
Take the current config in snapshot and append it to the provided ofstream.
bool _interfaces_increase
Interfaces must monotonically increase (or decrease), this determines whether going to the &#39;next&#39; int...
Definition: ForwardFlux.h:76
FFSConfigID myFFSConfigID
The current FFSConfigID of this MPI process.
Definition: ForwardFlux.h:133
unsigned int l
Interface number.
Definition: ForwardFlux.h:45
std::vector< FFSConfigID > Lambda0ConfigLibrary
Data structure that holds a Library N0 configurations at lambda0.
Definition: ForwardFlux.h:88
void PopQueueMPI(Snapshot *, const CVList &, unsigned)
Pop the queue, do MPI so that all procs maintain the same queue.
std::vector< std::vector< double > > _pB
Definition: ForwardFlux.h:145