1 //
2 //  setup.cpp
3 //  pb_solvers_code
4 //
5 //  Created by David Brookes on 3/9/16.
6 //  Copyright © 2016 David Brookes. All rights reserved.
7 //
8 
9 #include "setup.h"
10 
11 const double Setup::MAX_DIST = 1.4e18;
12 
Setup(string infile)13 Setup::Setup(string infile)
14 :
15 ompThreads_( 1 ),
16 saltConc_( 0.01 ),
17 nType_( 2 ),
18 PBCs_( 0 ),
19 blen_( MAX_DIST ),
20 maxtime_( 1000000 ),
21 ntraj_( 5 ),
22 gridPts_( 30 ),
23 gridCt_(0),
24 idiel_( 4.0 ),
25 sdiel_( 78.0 ),
26 temp_( 298.0 ),
27 npoles_( 5 ),
28 srand_( (unsigned)time(NULL) ),
29 nTypenCount_(2),
30 typeDef_(2),
31 typeDiff_(2),
32 pqr_names_(2),
33 surfNames_(2),
34 imatNames_(2),
35 expNames_(2),
36 xyz_names_(2),
37 isTransRot_(2),
38 runSpecs_(2),
39 mbdfile_loc_(2),
40 termvals_(2),
41 termtype_(2),
42 andCombine_(false),
43 sphBeta_(2.0),
44 tolSP_(1.0),
45 maxTrials_(40),
46 nTrials_(9)
47 {
48   nTypenCount_[0] = 1;
49   nTypenCount_[1] = 1;
50 
51   for (int i = 0; i<nType_; i++)
52   {
53     typeDiff_[i] = vector<double> (2);
54     typeDiff_[i][0] = 0.0;
55     typeDiff_[i][1] = 0.0;
56   }
57 
58   typeDef_[0]  = "stat";
59   typeDef_[1]  = "stat";
60   runSpecs_[0] = "electrostatics";
61   runSpecs_[1] = "test";
62 
63   potOutfnames_.resize(3);
64   potOutfnames_[0] = "";
65   potOutfnames_[1] = "";
66   potOutfnames_[2] = "";
67 
68   mbdfile_loc_[0] = "";
69   mbdfile_loc_[1] = "";
70 
71   imatNames_[0] = "";
72   imatNames_[1] = "";
73 
74   expNames_[0] = "";
75   expNames_[1] = "";
76 
77   units_ = "internal";
78 
79   // Initializing file locs to defaults
80   // pqr fname, imat path, spol path, spol name
81   vector<vector<string> > molfn = {{"../Config/test1.pqr",
82     "../Config/test1.xyz"}, {"../Config/test2.pqr", "../Config/test2.xyz"}};
83 
84   for (int i=0; i<nType_; i++)
85   {
86     pqr_names_[i] = molfn[i][0];
87     xyz_names_[i].resize(1);
88     isTransRot_[i].resize(1);
89     xyz_names_[i][0] = molfn[i][1];
90     isTransRot_[i][0] = false;
91   }
92 
93   confiles_.resize(0);
94 
95   read_infile(infile);
96 }
97 
98 // APBS
Setup(double temp,double salt_conc,double int_diel,double solv_diel,int nmol,string runtype,string runname,bool randorient,double boxl,int pbc_type,int gridpts,string map3d,int g2dct,vector<string> grid2Dfn,vector<string> grid2Dax,vector<double> grid2Dloc,string dxnam,int ntraj,bool termcomb,vector<string> difftype,vector<vector<double>> diffcon,vector<string> termcond,vector<double> termval,vector<vector<int>> termnu,vector<string> confil,vector<double> conpad,vector<vector<string>> xyzfil,string unt)99 Setup::Setup(double temp, double salt_conc, double int_diel, double solv_diel,
100              int nmol, string runtype, string runname, bool randorient,
101              double boxl, int pbc_type, int gridpts, string map3d, int g2dct,
102              vector<string> grid2Dfn, vector <string> grid2Dax,
103              vector <double> grid2Dloc, string dxnam, int ntraj, bool termcomb,
104              vector<string> difftype, vector<vector<double> > diffcon,
105              vector<string> termcond, vector<double> termval,
106              vector<vector <int > > termnu, vector<string> confil,
107              vector<double> conpad, vector<vector <string> > xyzfil, string unt)
108 :
109 ompThreads_( 1 ),
110 saltConc_( salt_conc ), //
111 nType_( nmol ),  //
112 PBCs_( pbc_type ),  //
113 blen_( boxl ),   //
114 maxtime_( 1000000 ),
115 ntraj_( ntraj ), //
116 gridPts_( gridpts ), //
117 gridCt_(g2dct), //
118 idiel_( int_diel ),  //
119 sdiel_( solv_diel ), //
120 temp_( temp ),       //
121 srand_( (unsigned)time(NULL) ),
122 nTypenCount_(nmol), //
123 typeDef_(nmol),
124 typeDiff_(nmol),
125 pqr_names_(nmol),
126 xyz_names_(nmol),
127 imatNames_(nmol),
128 surfNames_(nmol),
129 expNames_(nmol),
130 isTransRot_(nmol),
131 runSpecs_(2),  //
132 mbdfile_loc_(2),
133 numTerm_((int)termcond.size()), //
134 termvals_((int)termcond.size()), //
135 termmols_((int)termcond.size()), //
136 termtype_((int)termcond.size()), //
137 confiles_((int)confil.size()),  //
138 conpads_((int)confil.size()),  //
139 andCombine_(termcomb), //
140 orientRand_(randorient) //
141 {
142   runSpecs_[0] = runtype; //
143   runSpecs_[1] = runname; //
144   units_ = unt;
145 
146   for (int i = 0; i<nType_; i++) nTypenCount_[i] = 1; //
147 
148   // Dynamics part
149   confiles_.resize(0);
150   for (int i = 0; i<nType_; i++)
151   {
152     typeDef_[i] = difftype[i];
153     typeDiff_[i] = vector<double> (2);
154     typeDiff_[i][0] = diffcon[i][0];
155     typeDiff_[i][1] = diffcon[i][1];
156   }
157 
158   for (int i = 0; i < numTerm_; i++)
159   {
160     termtype_[i] = termcond[i];
161     termmols_[i] = vector<int> (1);
162     termmols_[i][0] = termnu[i][0];
163     termvals_[i] = termval[i];
164   }
165 
166   for (int i = 0; i < confiles_.size(); i++)
167   {
168     confiles_[i] = confil[i];
169     conpads_[i] = conpad[i];
170   }
171 
172   // Electrostatics part
173   potOutfnames_.resize(2+g2dct); //
174   axis_.resize(g2dct);  //
175   axLoc_.resize(g2dct); //
176   potOutfnames_[0] = dxnam;  //
177   potOutfnames_[1] = map3d;  //
178 
179   for (int i = 0; i<grid2Dax.size(); i++)
180   {
181     potOutfnames_[2+i] = grid2Dfn[i]; //
182     axis_[i] = grid2Dax[i];  //
183     axLoc_[i] = grid2Dloc[i];
184   }
185 
186   // Mutibody expansion part
187   mbdfile_loc_[0] = "";
188   mbdfile_loc_[1] = "";
189 
190   // Molecule part
191   for (int i=0; i<nType_; i++)
192   {
193     pqr_names_[i] = "mol" + to_string(i) + ".pqr";
194     surfNames_[i] = "";
195     imatNames_[i] = "";
196     expNames_[i]  = "";
197     xyz_names_[i].resize(ntraj);
198     isTransRot_[i].resize(1);
199     isTransRot_[i][0] = false;
200 
201     for (int j=0; j<ntraj; j++) xyz_names_[i][j] = xyzfil[i][j];
202   }
203 }
204 
apbs_pbsam_set(vector<string> surffil,vector<string> imatfil,vector<string> expfil)205 void Setup::apbs_pbsam_set(vector<string> surffil, vector<string> imatfil,
206                            vector<string> expfil)
207 {
208   int i;
209 //if ( nType_ > surffil.size() )
210 //  cout << "Missing surf file, assuming its the end ones" << endl;
211   for (i=0; i<surffil.size(); i++)
212     surfNames_[i] = surffil[i];
213 
214 //if ( nType_ > imatfil.size() )
215 //  cout << "Missing imat file, assuming its the end ones" << endl;
216   for (i=0; i<imatfil.size(); i++)
217     imatNames_[i] = imatfil[i];
218 
219 //if ( nType_ > expfil.size() )
220 //  cout << "Missing exp file, assuming its the end ones" << endl;
221   for (i=0; i<expfil.size(); i++)
222     expNames_[i] = expfil[i];
223 }
224 
read_infile(string fname)225 void Setup::read_infile(string fname)
226 {
227   cout << "Reading Input file " << fname << endl ;
228   ifstream fin(fname);
229   if (!fin.is_open()) throw CouldNotReadException(fname);
230 
231   string inputLine;
232   vector<vector <string> > keywordLines;
233   getline(fin,inputLine);
234 
235   while (!fin.eof())
236   {
237     findLines(inputLine);
238     getline(fin, inputLine);
239   }
240 }
241 
split(string str,char delim)242 vector<string> Setup::split(string str, char delim)
243 {
244   vector<string> internal;
245   stringstream ss(str); // Turn the string into a stream.
246   string tok;
247 
248   while(getline(ss, tok, delim))
249   {
250     internal.push_back(tok);
251   }
252   return internal;
253 }
254 
255 
findLines(string fline)256 void Setup::findLines(string fline)
257 {
258     if (!fline.empty())
259     {
260       findKeyword( split(fline, ' '));
261     }
262 }
263 
findKeyword(vector<string> fline)264 void Setup::findKeyword(vector<string> fline)
265 {
266   string keyword = fline[0];
267   if (keyword == "runname")
268   {
269     cout << "Runname command found" << endl;
270     setRunName( fline[1] );
271   } else if (keyword == "runtype")
272   {
273     cout << "Runtype command found" << endl;
274     setRunType( fline[1] );
275     if (fline[1] == "dynamics")
276     {
277       if (fline.size() > 2)
278       {
279         setNTraj( atoi( fline[2].c_str() ) );
280       }
281       if (fline.size() > 3)
282       {
283         setMaxTime( atoi( fline[3].c_str() ));
284       }
285     } else if (fline[1] == "electrostatics")
286     {
287       if (fline.size() > 2)
288       {
289         setGridPts( atoi( fline[2].c_str() ));
290       }
291     }
292   } else if (keyword == "dx")
293   {
294     cout << "DX command found" << endl;
295     setDXoutName( fline[1].c_str());
296   } else if (keyword == "3dmap")
297   {
298     cout << "3D map command found" << endl;
299     set_3dmap_name( fline[1].c_str());
300   } else if (keyword == "gridct")
301   {
302     cout << "Grid count command found" << endl;
303     setGridCt( atoi(fline[1].c_str()));
304     axis_.resize( gridCt_); axLoc_.resize(gridCt_);
305     potOutfnames_.resize(gridCt_+2);
306   } else if (keyword == "grid2D")
307   {
308     cout << "Grid command found" << endl;
309     setGridOutName( atoi(fline[1].c_str()), fline[2].c_str());
310     setGridAx( atoi(fline[1].c_str()), fline[3].c_str());
311     setGridAxLoc( atoi(fline[1].c_str()), atof(fline[4].c_str()));
312   } else if (keyword == "3bdloc")
313   {
314     cout << "3BD loc command found" << endl;
315     set3BDLoc(fline[1].c_str());
316   } else if (keyword == "2bdloc")
317   {
318     cout << "2BD loc command found" << endl;
319     set2BDLoc(fline[1].c_str());
320   } else if (keyword == "omp")
321   {
322     cout << "OMP command found" << endl;
323     setOMP(atoi(fline[1].c_str()));
324   } else if (keyword == "pbc")
325   {
326     cout << "PBC command found" << endl;
327     setPBCT( atoi(fline[1].c_str()) );
328     if ( getPBCs() > 0 )
329       setBoxl(atof(fline[2].c_str()));
330   } else if (keyword == "salt")
331   {
332     cout << "Salt command found" << endl;
333     setSaltCon( atof(fline[1].c_str()) );
334   } else if (keyword == "temp")
335   {
336     cout << "Temperature command found" << endl;
337     setTemp( atof(fline[1].c_str()) );
338   } else if (keyword == "idiel")
339   {
340     cout << "Interior dielectric command found" << endl;
341     setIDiel( atof(fline[1].c_str()) );
342   } else if (keyword == "sdiel")
343   {
344     cout << "Solvent dielectric command found" << endl;
345     setSDiel( atof(fline[1].c_str()) );
346   } else if (keyword == "poles")
347   {
348     cout << "Number of poles command found" << endl;
349     setNPoles( atoi(fline[1].c_str()) );
350   }else if (keyword == "termct")
351   {
352     cout << "Termination count command found" << endl;
353     set_numterms(atoi(fline[1].c_str()));
354     resize_termcond(atoi(fline[1].c_str()));
355   } else if (keyword == "termcombine")
356   {
357     cout << "Termination combine command found" << endl;
358     set_term_combine(fline[1]);
359   } else if (keyword == "term")
360   {
361     cout << "Termination condition command found" << endl;
362     int idx = atoi(fline[1].c_str()) - 1;
363     if (idx > get_numterms()-1)
364     {
365       cout << "WARNING: trying to add more term types than specified" << endl;
366       return;
367     }
368     string type = fline[2];
369     double val = atof(fline[3].c_str());
370     vector<int> mol_idx(2);
371     if (type == "contact")
372     {
373       string confile = fline[3];
374       double pad  = atof(fline[4].c_str());
375       // placeholders:
376       mol_idx = {-1};
377       val = -1;
378       confiles_.push_back(fline[3]);
379       cout << "Contact size " << confiles_.size() << endl;
380       conpads_.push_back(pad);
381       cout << "This is my first contact file " << confiles_[0] << endl;
382       cout << "This is my first contact file " << fline[3] << endl;
383     }
384     else
385     {
386       mol_idx[0] = atoi(fline[4].c_str()) - 1;
387     }
388     add_termcond(idx, type, mol_idx, val);
389 
390   } else if (keyword == "attypes")
391   {
392     cout << "Atom Types command found" << endl;
393     setNType( atoi(fline[1].c_str()) );
394     resizeVecs();
395     cout << "done with attypes" << endl;
396   } else if (keyword == "type")
397   {
398     cout << "Type def command found" << endl;
399     int typeNo = atoi(fline[1].c_str())-1;
400     if (typeNo > getNType()-1)
401     {
402       cout << "WARNING: trying to add more mol types than specified" << endl;
403       return;
404     }
405     if (fline.size() > 2)
406       setTypeNCount( typeNo, atoi(fline[2].c_str()) );
407     if (fline.size() > 3)
408     {
409       setTypeNDef( typeNo, fline[3].c_str() );
410       if (getTypeNDef(typeNo) == "move")
411       {
412         setTypeNDtr( typeNo, atof(fline[4].c_str()));
413         setTypeNDrot( typeNo, atof(fline[5].c_str()));
414       } else if (getTypeNDef(typeNo) == "rot")
415       {
416         setTypeNDtr( typeNo, 0.0);
417         setTypeNDrot( typeNo, atof(fline[4].c_str()));
418       } else
419       {
420         setTypeNDtr( typeNo, 0.0);
421         setTypeNDrot( typeNo, 0.0);
422       }
423     }
424   } else if (keyword == "pqr")
425   {
426     cout << "PQR command found" << endl;
427     int typeNo = atoi(fline[1].c_str())-1;
428     if (typeNo > getNType()-1)
429       return;
430     setTypeNPQR( typeNo, fline[2].c_str() );
431   } else if (keyword == "xyz")
432   {
433     string xyz;
434     int traj, typeNo = atoi(fline[1].c_str())-1;
435 
436     if ( fline.size() == 4 )
437     {
438       traj = atoi(fline[2].c_str())-1;
439       xyz = fline[3];
440     } else
441     {
442       traj = 0;
443       xyz = fline[2];
444     }
445     if (typeNo > getNType()-1)
446       return;
447     setTypeNXYZ( typeNo, traj, xyz );
448     cout << "XYZ command found " << xyz << endl;
449     setTypeNisTransRot(typeNo, traj, false);
450   } else if (keyword == "transrot")
451   {
452     string transrot;
453     int traj, typeNo = atoi(fline[1].c_str())-1;
454     cout << "transrot command found" << endl;
455 
456     if ( fline.size() == 4 )
457     {
458       traj = atoi(fline[2].c_str())-1;
459       transrot = fline[3];
460     } else
461     {
462       traj = 0;
463       transrot = fline[2];
464     }
465     if (typeNo > getNType()-1)
466       return;
467     setTypeNXYZ( typeNo, traj, transrot );
468     setTypeNisTransRot(typeNo, traj, true);
469   } else if (keyword == "surf")
470   {
471     cout << "surf command found" << endl;
472     int typeNo = atoi(fline[1].c_str())-1;
473     if (typeNo > getNType()-1)
474       return;
475     setTypeNSurf( typeNo, fline[2].c_str() );
476   } else if (keyword == "imat")
477   {
478     cout << "IMAT prefix command found" << endl;
479     int typeNo = atoi(fline[1].c_str())-1;
480     if (typeNo > getNType()-1)
481       return;
482     setTypeNImat( typeNo, fline[2].c_str() );
483   } else if (keyword == "exp")
484   {
485     cout << "Expansion prefix command found" << endl;
486     int typeNo = atoi(fline[1].c_str())-1;
487     if (typeNo > getNType()-1)
488       return;
489     setTypeNExp( typeNo, fline[2].c_str() );
490   } else if (keyword == "randorient")
491   {
492     cout << "Random orientation command found" << endl;
493     setRandOrient();
494   } else if (keyword == "random")
495   {
496     cout << "RNG Seed command found" << endl;
497     setRand( atoi(fline[1].c_str()) );
498   } else if (keyword == "units")
499   {
500     cout << "Units command found" << endl;
501     setUnits( fline[1].c_str() );
502   } else if (keyword == "tolsp")
503   {
504     cout << "tolsp command found" << endl;
505     set_tol_sp(atof(fline[1].c_str()));
506   } else if (keyword == "ntrials")
507   {
508     cout << "ntrials command found" << endl;
509     set_n_trials(atoi(fline[1].c_str()));
510   } else if (keyword == "maxtrials")
511   {
512     cout << "maxtrials command found" << endl;
513     set_max_trials(atoi(fline[1].c_str()));
514   } else if (keyword == "sphbeta")
515   {
516     cout << "sphbeta command found" << endl;
517     set_sph_beta(atof(fline[1].c_str()));
518   } else
519     cout << "Keyword not found, read in as " << fline[0] << endl;
520 }
521 
resizeVecs()522 void Setup::resizeVecs()
523 {
524   int ntraj= (getRunType() == "dynamics") ? getNTraj() : 1;
525   nTypenCount_.resize(nType_);
526   typeDef_.resize(nType_);
527 
528   typeDiff_.resize(nType_);
529   for (int i = 0; i<nType_; i++)
530   {
531     typeDiff_[i].resize(2);
532   }
533 
534   pqr_names_.resize(nType_);
535   xyz_names_.resize(nType_);
536   surfNames_.resize(nType_);
537   imatNames_.resize(nType_);
538   expNames_.resize(nType_);
539   isTransRot_.resize(nType_);
540   for(int i = 0; i < nType_; i++)
541   {
542     xyz_names_[i].resize(ntraj);
543     surfNames_[i] = "";
544     imatNames_[i] = "";
545     expNames_[i] = "";
546     isTransRot_[i].resize(nType_);
547   }
548 
549 } // end resizeVecs
550 
check_inputs()551 void Setup::check_inputs()
552 {
553   vector<string> problems;
554   if (typeDiff_.size() < nType_ )
555   {
556     problems.push_back("Number of molecular input type parameters is less \
557                        than the specified number of MoleculeSAMAM types");
558   }
559   if (typeDef_.size() < nType_)
560   {
561     problems.push_back("Number of movement types is less \
562                         than the specified number of MoleculeSAMAM types");
563   }
564   if (pqr_names_.size() < nType_)
565   {
566     problems.push_back("Number of provided PQR files is less than the\
567                        specified number of molecular types");
568   }
569   if (xyz_names_.size() < nType_)
570   {
571     problems.push_back("Number of provided XYZ files is less than the\
572                        specified number of molecular types");
573     for (int i = 0; i < nType_; i++)
574       if (xyz_names_[i].size() < ntraj_)
575       {
576         problems.push_back("Number of provided XYZ files is less than the\
577                            specified number of trajectories");
578       }
579   }
580   if (runSpecs_[0] == "electrostatics")
581   {
582     if (axis_.size() < gridCt_ || axLoc_.size() < gridCt_)
583     {
584       problems.push_back("Number grids provided is less than specified \
585                           grid count");
586     }
587   }
588   if (runSpecs_[0] == "dynamics")
589   {
590     if (termtype_.size() < numTerm_ || termtype_.size() < numTerm_)
591     {
592       problems.push_back("Number of termination conditions provided is less \
593                               than specified termination count");
594     }
595   }
596   if (problems.size() > 0) throw BadInputException(problems);
597 }
598 
599