SSAGES  0.1
A MetaDynamics Package
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Groups Pages
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 
129  bool initializeQueueFlag;
130 
133 
136 
140  unsigned int _nfailure_total;
141 
144  std::vector<std::vector<double>> _pB;
145 
147  unsigned int _current_interface;
148 
150  std::default_random_engine _generator;
151 
157  std::deque<FFSConfigID> FFSConfigIDQueue;
158 
160  std::string _output_directory;
161 
163  std::ofstream _trajectory_file;
164 
167 
168  //-----------------------------------------------------------------
169  // Private Functions
170  //-----------------------------------------------------------------
171 
173  void CheckInitialStructure(const CVList&);
174 
176  void WriteInitialFlux();
177 
180  void AddNewIDsToQueue();
181 
183  bool HasReturnedToA(double);
184 
186 
188  int HasCrossedInterface(double, double, unsigned int interface);
189 
191  void WriteFFSConfiguration(Snapshot *snapshot,FFSConfigID& ffsconfig, bool wassuccess);
192 
195 
197  void ComputeInitialFlux(Snapshot*, const CVList&);
198 
200  virtual void CheckForInterfaceCrossings(Snapshot*, const class CVManager&) = 0;
201 
203  virtual void InitializeQueue(Snapshot*, const CVList&) =0;
204 
206 
211 
213  void PrintQueue();
214 
216  void PopQueueMPI(Snapshot*, const CVList&, unsigned);
217 
219 
222  void FluxBruteForce(Snapshot*, const CVList&);
223 
226 
229 
232  void AppendTrajectoryFile(Snapshot*, std::ofstream&);
233 
235  void OpenTrajectoryFile(std::ofstream&);
236 
237  public:
239 
246  ForwardFlux(const MPI_Comm& world,
247  const MPI_Comm& comm,
248  double ninterfaces, std::vector<double> interfaces,
249  unsigned int N0Target, std::vector<unsigned int> M,
250  bool initialFluxFlag, bool saveTrajectories,
251  unsigned int currentInterface, std::string output_directory, unsigned int frequency) :
252  Method(frequency, world, comm), _ninterfaces(ninterfaces), _interfaces(interfaces), _N0Target(N0Target),
253  _M(M), _initialFluxFlag(initialFluxFlag), _saveTrajectories(saveTrajectories), _current_interface(currentInterface),
254  _output_directory(output_directory), _generator(1), iteration_(0)
255  {
256  //_output_directory = "FFSoutput";
257  mkdir(_output_directory.c_str(),S_IRWXU); //how to make directory?
258 
259  //_current_interface = 0;
260  _N0TotalSimTime = 0;
261 
262  if (!_initialFluxFlag)
263  initializeQueueFlag = true;
264 
265  _A.resize(_ninterfaces);
266  _P.resize(_ninterfaces);
267  _S.resize(_ninterfaces);
268  _N.resize(_ninterfaces);
269 
270  //_N[_current_interface] = _N0Target;
271  /*_M.resize(_ninterfaces);
272 
273  //code for setting up simple simulation and debugging
274  _ninterfaces = 5;
275  int i;
276  _interfaces.resize(_ninterfaces);
277  _interfaces[0]=-1.0;
278  _interfaces[1]=-0.95;
279  _interfaces[2]=-0.8;
280  _interfaces[3]= 0;
281  _interfaces[4]= 1.0;
282 
283  _saveTrajectories = true;
284 
285  _initialFluxFlag = true;
286 
287  for(i=0;i<_ninterfaces;i++) _M[i] = 50;
288 
289  _N0Target = 100;*/
290  //_N0Target = 100;
291  _nfailure_total = 0;
292 
293 
294  //check if interfaces monotonically increase or decrease
295  bool errflag = false;
296  if (_interfaces[0]< _interfaces[1]) _interfaces_increase = true;
297  else if (_interfaces[0] > _interfaces[1]) _interfaces_increase = false;
298  else errflag = true;
299  for (unsigned int i=0;i<_ninterfaces-1;i++)
300  {
301  if((_interfaces_increase) && (_interfaces[i] >= _interfaces[i+1]))
302  errflag = true;
303  else if ((!_interfaces_increase) && (_interfaces[i] <= _interfaces[i+1]))
304  errflag = true;
305  }
306  if (errflag)
307  {
308  std::cerr << "Error! The interfaces are poorly defined. They must be monotonically increasing or decreasing and cannot equal one another! Please fix this.\n";
309  for (auto interface : _interfaces)
310  std::cerr << interface << " ";
311  std::cerr << "\n";
312  MPI_Abort(world_, EXIT_FAILURE);
313  }
314 
315  // This is to generate an artificial Lambda0ConfigLibrary, Hadi's code does this for real
316  // THIS SHOULD BE SOMEWHERE ELSE!!!
318  std::normal_distribution<double> distribution(0,1);
319  for (unsigned int i = 0; i < _N0Target ; i++)
320  {
321  Lambda0ConfigLibrary[i].l = 0;
322  Lambda0ConfigLibrary[i].n = i;
323  Lambda0ConfigLibrary[i].a = 0;
324  Lambda0ConfigLibrary[i].lprev = 0;
325  Lambda0ConfigLibrary[i].nprev = i;
326  Lambda0ConfigLibrary[i].aprev = 0;
327  //FFSConfigID ffsconfig = Lambda0ConfigLibrary[i];
328  }
329  /*// Write the dump file out
330  std::ofstream file;
331  std::string filename = _output_directory + "/l" + std::to_string(ffsconfig.l) + "-n" + std::to_string(ffsconfig.n) + ".dat";
332  file.open(filename.c_str());
333 
334  //first line gives ID of where it came from
335  file << ffsconfig.lprev << " " << ffsconfig.nprev << " " << ffsconfig.aprev << "\n";
336  //write position and velocity
337  file << "1 -1 0 0 " << distribution(_generator) << " " << distribution(_generator) << " 0\n";
338 
339  }
340  */
342  }
343 
345 
349  void PreSimulation(Snapshot* snapshot, const class CVManager& cvmanager) override;
350 
352 
356  virtual void PostIntegration(Snapshot* snapshot, const class CVManager& cvmanager) = 0;
357 
359 
363  void PostSimulation(Snapshot* snapshot, const class CVManager& cvmanager) override;
364 
366  static ForwardFlux* Build(const Json::Value& json,
367  const MPI_Comm& world,
368  const MPI_Comm& comm,
369  const std::string& path);
370  };
371 }
372 
373 /*
374 File Formats:
375 _indexfile
376 interface(some integer) dump_file_name(a string that contains interface and trial number)
377 example: 1 dump_1_10.xyz
378 
379 dumpfile
380 atomid posx posy posz vx vy vz
381 
382 
383 */
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:147
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'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
uint iteration_
Method iteration counter/.
Definition: ForwardFlux.h:166
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:157
virtual void PostIntegration(Snapshot *snapshot, const class CVManager &cvmanager)=0
Post-integration hook.
unsigned int lprev
Previous Interface number (i.e. traj I came from)
Definition: ForwardFlux.h:48
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
std::ofstream _trajectory_file
file to which the current trajectory is written to
Definition: ForwardFlux.h:163
int HasCrossedInterface(double, double, unsigned int interface)
Function checks if configuration has crossed interface specified since the last check.
Definition: ForwardFlux.cpp:79
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:160
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:140
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:246
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:135
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:150
static ForwardFlux * Build(const Json::Value &json, const MPI_Comm &world, const MPI_Comm &comm, const std::string &path)
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 'next' int...
Definition: ForwardFlux.h:76
FFSConfigID myFFSConfigID
The current FFSConfigID of this MPI process.
Definition: ForwardFlux.h:132
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:144