NAMD
GlobalMasterFreeEnergy.C
Go to the documentation of this file.
1 
7 /*
8  Forwards atoms to master node for force evaluation.
9 */
10 
11 #include <string.h>
12 #include "InfoStream.h"
13 #include "NamdTypes.h"
14 #include "FreeEnergyEnums.h"
15 #include "FreeEnergyAssert.h"
16 #include "FreeEnergyGroup.h"
17 #include "Vector.h"
18 #include "FreeEnergyVector.h"
19 #include "FreeEnergyRestrain.h"
20 #include "FreeEnergyRMgr.h"
21 #include "FreeEnergyLambda.h"
22 #include "FreeEnergyLambdMgr.h"
23 #include "GlobalMaster.h"
24 #include "GlobalMasterFreeEnergy.h"
25 #include "FreeEnergyParse.h"
26 
27 #include "Node.h"
28 #include "Molecule.h"
29 #include "ReductionMgr.h"
30 #include "SimParameters.h"
31 #include "ConfigList.h"
32 
33 #include <stdio.h>
34 
35 //#define DEBUGM
36 #define MIN_DEBUG_LEVEL 1
37 #include "Debug.h"
38 
39 
40 void GlobalMasterFreeEnergy::update() {
41 //-----------------------------------------------------------------
42 // get lambdas from LambdaManager, inform RestraintManager.
43 // calculate gradients for each center-of-mass of each restraint,
44 // and apply the forces to the atoms involved
45 //-----------------------------------------------------------------
46  double LambdaKf, LambdaRef;
47  double Sum_dU_dLambdas;
48 
49  if (m_LambdaManager.GetLambdas(LambdaKf, LambdaRef)) {
50 
51  // stuff that's done every time step
52  m_RestraintManager.SetLambdas(LambdaKf, LambdaRef);
53  m_RestraintManager.UpdateCOMs(*this);
54  m_RestraintManager.AddForces(*this);
55  if (m_LambdaManager.IsTimeToClearAccumulator()) {
56  m_LambdaManager.ZeroAccumulator();
57  }
58  Sum_dU_dLambdas = m_RestraintManager.Sum_dU_dLambdas();
59  m_LambdaManager.Accumulate(Sum_dU_dLambdas);
60 
61  // for integrating all the MCTI averages
62  if (m_LambdaManager.IsEndOf_MCTI_Step()) {
63  m_LambdaManager.Integrate_MCTI();
64  }
65 
66  // stuff that's done when it's time to print
67  if (m_LambdaManager.IsFirstStep()) {
68  m_LambdaManager.PrintLambdaHeader(simParams->dt);
69  }
70  if (m_LambdaManager.IsTimeToPrint()) {
71  m_LambdaManager.PrintHeader(simParams->dt);
72  if (m_LambdaManager.IsTimeToPrint_dU_dLambda()) {
73  m_RestraintManager.Print_dU_dLambda_Info();
74  if (m_RestraintManager.ThereIsAForcingRestraint()) {
75  m_LambdaManager.Print_dU_dLambda_Summary(Sum_dU_dLambdas);
76  }
77  }
78  else {
79  m_LambdaManager.PrintSomeSpaces();
80  }
81  m_RestraintManager.PrintEnergyInfo();
82  m_RestraintManager.PrintRestraintInfo();
83  if (m_LambdaManager.IsEndOf_MCTI()) {
84  m_LambdaManager.Print_MCTI_Integration();
85  }
86  }
87  }
88 }
89 
90 
91 void GlobalMasterFreeEnergy::user_initialize() {
92 //-----------------------------------------------------------------
93 // read all the input from config
94 //-----------------------------------------------------------------
95 
96  iout << iINFO << " FREE ENERGY PERTURBATION CONFIG\n";
97  iout << iINFO << "***********************************\n";
98  int config_len = strlen(config);
99  if ( config_len < 10000 ) {
100  iout << config;
101  } else {
102  char *new_config = new char[10000 + 10];
103  strncpy(new_config,config,10000);
104  new_config[10000] = 0;
105  strcat(new_config,"\n...\n");
106  iout << new_config;
107  delete [] new_config;
108  }
109  iout << iINFO << "***********************************\n" << endi;
110 
111  ReadInput(config, m_RestraintManager, m_LambdaManager, *this, simParams->dt);
112 
113  // exit if there aren't enough steps to complete all pmf & mcti blocks
114  int Total = m_LambdaManager.GetTotalNumSteps();
115  if (Total > simParams->N) {
116  iout << "FreeEnergy: Not enough steps to complete pfm & mcti blocks" << std::endl;
117  iout << "FreeEnergy: Num Steps Needed = " << Total << std::endl;
118  iout << "FreeEnergy: Num Steps Requested = " << simParams->N << std::endl << endi;
119  NAMD_die("FreeEnergy: Fatal Run-Time Error");
120  }
121 }
122 
123 
124 void GlobalMasterFreeEnergy::user_calculate() {
125 //-----------------------------------------------------------------
126 // this is what's executed every time-step
127 //-----------------------------------------------------------------
128  m_LambdaManager.IncCurrStep();
129  update();
130 }
131 
132 
134  getAtomID(const char *segid, int resid, const char *aname)
135 {
136  return molecule->get_atom_from_name(segid,resid,aname);
137 }
138 
140  getNumAtoms(const char* segid, int resid) // 0 on error
141 {
142  return molecule->get_residue_size(segid,resid);
143 }
144 
146  getAtomID(const char *segid, int resid, int index)
147 {
148  return molecule->get_atom_from_index_in_residue(segid,resid,index);
149 }
150 
152 {
153  if ( atomid < 0 || atomid >= molecule->numAtoms ) return -1.; // failure
154  return molecule->atommass(atomid);
155 }
156 
157 
159 {
160  if ( atomid < 0 || atomid >= molecule->numAtoms ) return -1; // failure
161  modifyRequestedAtoms().add(atomid);
162  return 0; // success
163 }
164 
166 {
170  for ( ; a_i != a_e; ++a_i, ++p_i ) {
171  if ( *a_i == atomid ) {
172  position = *p_i;
173  return 0; // success
174  }
175  }
176  return -1; // failure
177 }
178 
180 {
181  DebugM(2,"Forcing "<<atomid<<" with "<<force<<"\n");
182  if ( atomid < 0 || atomid >= molecule->numAtoms ) return -1; // failure
183  modifyForcedAtoms().add(atomid);
184  modifyAppliedForces().add(force);
185  return 0; // success
186 }
187 
188 
190 : GlobalMaster() {
191  DebugM(3,"Constructing GlobalMasterFreeEnergy\n");
192  molecule = Node::Object()->molecule;
193  simParams = Node::Object()->simParameters;
194 
195  // now set up the free energy stuff
196  initialize();
197 }
198 
200  DebugM(3,"Destructing GlobalMasterFreeEnergy\n");
201  delete config;
202 }
203 
204 
205 void GlobalMasterFreeEnergy::initialize() {
206  DebugM(4,"Initializing master\n");
207 
208  // Get our script
209  StringList *script = Node::Object()->configList->find("freeEnergyConfig");
210 
211  config = new char[1];
212  config[0] = '\0';
213 
214  for ( ; script; script = script->next) {
215  if ( strstr(script->data,"\n") ) {
216  size_t add_len = strlen(script->data);
217  size_t config_len = 0;
218  config_len = strlen(config);
219  char *new_config = new char[config_len + add_len + 2];
220  strcpy(new_config,config);
221  strcat(new_config,script->data);
222  strcat(new_config,"\n"); // just to be safe
223  delete [] config;
224  config = new_config;
225  } else {
226  FILE *infile = fopen(script->data,"r");
227  if ( ! infile ) {
228  char errmsg[256];
229  sprintf(errmsg,"Error trying to read file %s!\n",script->data);
230  NAMD_die(errmsg);
231  }
232  fseek(infile,0,SEEK_END);
233  size_t add_len = ftell(infile);
234  size_t config_len = 0;
235  config_len = strlen(config);
236  char *new_config = new char[config_len + add_len + 3];
237  strcpy(new_config,config);
238  delete [] config;
239  config = new_config;
240  new_config += config_len;
241  rewind(infile);
242  fread(new_config,sizeof(char),add_len,infile);
243  new_config += add_len;
244  new_config[0] = '\n';
245  new_config[1] = '\0';
246  fclose(infile);
247  }
248  }
249 
250  // iout << iDEBUG << "Free energy perturbation - initialize()\n" << endi;
251  user_initialize();
252 }
253 
254 
255 void GlobalMasterFreeEnergy::calculate() {
256  DebugM(4,"Calculating forces on master\n");
257 
258  /* zero out the forces */
261 
262  /* XXX is this line needed at all? */
264  modifyGroupForces().setall(Vector(0,0,0));
265 
266 // iout << iDEBUG << "Free energy perturbation - calculate()\n" << endi;
267  user_calculate();
268 
269  // Send results to clients
270  DebugM(3,"Sending results (" << forcedAtoms().size() << " forces) on master\n");
271  if ( changedAtoms() || changedGroups() ) {
272  DebugM(4,"Sending new configuration (" <<
273  requestedAtoms().size() << " atoms) on master\n");
274  }
275 }
static Node * Object()
Definition: Node.h:86
Bool_t IsTimeToPrint_dU_dLambda()
int getPosition(int atomid, Position &position)
ForceList & modifyAppliedForces()
Definition: GlobalMaster.C:163
std::ostream & iINFO(std::ostream &s)
Definition: InfoStream.C:81
AtomIDList & modifyRequestedAtoms()
Definition: GlobalMaster.C:128
Bool_t IsTimeToClearAccumulator()
void SetLambdas(double LambdaKf, double LambdaRef)
int getAtomID(const char *segid, int resid, const char *aname)
void PrintLambdaHeader(double dT)
void Accumulate(double dU_dLambda)
int get_residue_size(const char *segid, int resid) const
Definition: Molecule.C:149
AtomIDList::const_iterator getAtomIdBegin()
Definition: GlobalMaster.C:191
Definition: Vector.h:72
SimParameters * simParameters
Definition: Node.h:181
double Sum_dU_dLambdas()
bool changedAtoms()
Definition: GlobalMaster.C:108
#define DebugM(x, y)
Definition: Debug.h:75
std::ostream & endi(std::ostream &s)
Definition: InfoStream.C:54
#define iout
Definition: InfoStream.h:51
void PrintHeader(double dT)
int add(const Elem &elem)
Definition: ResizeArray.h:101
PositionList::const_iterator getAtomPositionBegin()
Definition: GlobalMaster.C:199
int addForce(int atomid, Force force)
BigRealList::const_iterator getGroupMassEnd()
Definition: GlobalMaster.C:240
void Print_dU_dLambda_Info()
void resize(int i)
Definition: ResizeArray.h:84
void setall(const Elem &elem)
Definition: ResizeArray.h:94
void Print_dU_dLambda_Summary(double Sum_dU_dLambdas)
int get_atom_from_name(const char *segid, int resid, const char *aname) const
Definition: Molecule.C:126
const AtomID * const_iterator
Definition: ResizeArray.h:38
void ReadInput(char *Str, ARestraintManager &RMgr, ALambdaManager &LMgr, GlobalMasterFreeEnergy &CFE, double dT)
bool changedGroups()
Definition: GlobalMaster.C:116
AtomIDList & modifyForcedAtoms()
Definition: GlobalMaster.C:158
int numAtoms
Definition: Molecule.h:585
void NAMD_die(const char *err_msg)
Definition: common.C:147
Bool_t GetLambdas(double &LambdaKf, double &LambdaRef)
BigRealList::const_iterator getGroupMassBegin()
Definition: GlobalMaster.C:235
ForceList & modifyGroupForces()
Definition: GlobalMaster.C:168
Real atommass(int anum) const
Definition: Molecule.h:1107
ConfigList * configList
Definition: Node.h:182
void UpdateCOMs(GlobalMasterFreeEnergy &CFE)
StringList * next
Definition: ConfigList.h:49
AtomIDList::const_iterator getAtomIdEnd()
Definition: GlobalMaster.C:195
char * data
Definition: ConfigList.h:48
Bool_t IsEndOf_MCTI_Step()
void AddForces(GlobalMasterFreeEnergy &CFE)
int get_atom_from_index_in_residue(const char *segid, int resid, int index) const
Definition: Molecule.C:163
Bool_t ThereIsAForcingRestraint()
const AtomIDList & forcedAtoms()
Definition: GlobalMaster.C:134
StringList * find(const char *name) const
Definition: ConfigList.C:341
Molecule * molecule
Definition: Node.h:179
int getNumAtoms(const char *segid, int resid)
const AtomIDList & requestedAtoms()
Definition: GlobalMaster.C:124