NAMD
mainfunc.C
Go to the documentation of this file.
1 
7 #include "memusage.h"
8 #include "converse.h"
9 #include "common.h"
10 #include "BackEnd.h"
11 #include "InfoStream.h"
12 #include "Broadcasts.h"
13 
14 #include "NamdState.h"
15 #include "Node.h"
16 #if defined(WIN32) && !defined(__CYGWIN__)
17 #include <direct.h>
18 #define CHDIR _chdir
19 #define GETCWD _getcwd
20 #define PATHSEP '\\'
21 #define PATHSEPSTR "\\"
22 #else
23 #include <unistd.h>
24 #define CHDIR chdir
25 #define GETCWD getcwd
26 #define PATHSEP '/'
27 #define PATHSEPSTR "/"
28 #endif
29 #include <sys/stat.h>
30 #include "ConfigList.h"
31 #include "ScriptTcl.h"
32 #include "PatchData.h"
33 
34 
35 void after_backend_init(int argc, char **argv);
36 
37 #ifdef MEM_OPT_VERSION
38 //record the working directory when reading the configuration file
39 //for Parallel IO Input --Chao Mei
40 char *gWorkDir = NULL;
41 #endif
42 
43 int main(int argc, char **argv) {
44 #ifdef NAMD_TCL
45  if ( argc > 2 && ! strcmp(argv[1],"+tclsh") ) {
46  // pass all remaining arguments to script
47  return ScriptTcl::tclsh(argc-2,argv+2);
48  }
49 #endif
50  BackEnd::init(argc,argv);
51  after_backend_init(argc, argv);
52  return 0;
53 }
54 
55 void after_backend_init(int argc, char **argv){
56 #define CWDSIZE 1024
57  char origcwd_buf[CWDSIZE];
58  char currentdir_buf[CWDSIZE];
59 
60  ScriptTcl *script = new ScriptTcl;
61  Node::Object()->setScript(script);
62  // Node is a group that contains common pointers across many PEs,
63  // since script is not shared among nodes, into the nodegroup it goes
64 
65 #ifdef NODEGROUP_FORCE_REGISTER
66  CProxy_PatchData cpdata(CkpvAccess(BOCclass_group).patchData);
67  PatchData* patchData = cpdata.ckLocalBranch();
68  patchData->script =script;
69 #endif
70 
71  for(argc = 0; argv[argc]; ++argc);
72  if ( argc < 2 ) {
73 #if defined(WIN32) && !defined(__CYGWIN__)
74  CkPrintf("\nFATAL ERROR: No simulation config file specified on command line.\n");
75  CkPrintf("\nNOTE: NAMD has no graphical interface and must be run from a command line.\n");
76  int nsleep = 10;
77  CkPrintf("\nSleeping %d seconds before exiting...\n", nsleep);
78  fflush(stdout);
79  sleep(nsleep);
80  CkPrintf("\n");
81 #endif
82  NAMD_die("No simulation config file specified on command line.");
83  }
84  char *origcwd = GETCWD(origcwd_buf,CWDSIZE);
85  if ( ! origcwd ) NAMD_err("getcwd");
86 #ifdef NAMD_TCL
87  for(int i = 1; i < argc; ++i) {
88  if ( strstr(argv[i],"--") == argv[i] ) {
89  char buf[1024];
90  if ( i + 1 == argc ) {
91  sprintf(buf, "missing argument for command line option %s", argv[i]);
92  NAMD_die(buf);
93  }
94  if ( ! strcmp(argv[i],"--tclmain") ) {
95  // pass all remaining arguments to script
96  iout << iINFO << "Command-line argument is";
97  for ( int j=i; j<argc; ++j ) { iout << " " << argv[j]; }
98  iout << "\n" << endi;
99  script->tclmain(argc-i-1,argv+i+1);
100  BackEnd::exit();
101  return;
102  }
103  sprintf(buf, "%s %s", argv[i]+2, argv[i+1]);
104  iout << iINFO << "Command-line argument is --" << buf << "\n" << endi;
105  script->eval(buf);
106  ++i;
107  continue;
108  }
109  char *confFile = argv[i];
110 #else
111  char *confFile = argv[argc-1];
112 #endif
113  iout << iINFO << "Configuration file is " << confFile << "\n" << endi;
114 
115  char *currentdir=confFile;
116  char *tmp;
117  char tmpcurdir[3];
118  for(tmp=confFile;*tmp;++tmp); // find final null
119  for( ; tmp != confFile && *tmp != PATHSEP; --tmp); // find last '/'
120 #if defined(WIN32) && !defined(__CYGWIN__)
121  if (tmp == confFile) {
122  // in case this is under cygwin, search for '/' as well
123  for(tmp=confFile;*tmp;++tmp); // find final null
124  for( ; tmp != confFile && *tmp != '/'; --tmp); // find last '/'
125  }
126 #endif
127  if ( CHDIR(origcwd) ) NAMD_err(origcwd);
128  if ( tmp != confFile )
129  {
130  *tmp = 0; confFile = tmp + 1;
131  if ( CHDIR(currentdir) ) NAMD_err(currentdir);
132  struct stat statBuf;
133  if (stat(confFile, &statBuf)) {
134  char buf[1024];
135  sprintf(buf,"Unable to access config file %s%c%s",currentdir,PATHSEP,confFile);
136  NAMD_die(buf);
137  }
138  iout << iINFO << "Changed directory to " << currentdir << "\n" << endi;
139  currentdir = GETCWD(currentdir_buf,CWDSIZE);
140  if ( ! currentdir ) NAMD_err("getcwd after chdir");
141  }
142  else{
143  if ( *tmp == PATHSEP ){ // config file in / is odd, but it might happen
145  struct stat statBuf;
146  if (stat(confFile, &statBuf)) {
147  char buf[1024];
148  sprintf(buf,"Unable to access config file %s",confFile);
149  NAMD_die(buf);
150  }
151  }else{ // just a config file name, so the path is the current working path
152  struct stat statBuf;
153  if (stat(confFile, &statBuf)) {
154  char buf[1024];
155  if ( confFile[0] == '-' || confFile[0] == '+' ) {
156  sprintf(buf,"Unknown command-line option %s",confFile);
157  } else {
158  sprintf(buf,"Unable to access config file %s",confFile);
159  }
160  NAMD_die(buf);
161  }
162  tmpcurdir[0] = '.';
163  tmpcurdir[1] = PATHSEP;
164  tmpcurdir[2] = 0;
165  currentdir = tmpcurdir;
166  iout << iINFO << "Working in the current directory " << origcwd << "\n" << endi;
167  }
168  }
169 
170 #ifdef MEM_OPT_VERSION
171  int dirlen = strlen(currentdir);
172  gWorkDir = new char[dirlen+1];
173  gWorkDir[dirlen]=0;
174  memcpy(gWorkDir, currentdir, dirlen);
175 #endif
176 
177  currentdir = NULL;
178 
179 #ifdef NAMD_TCL
180  script->load(confFile);
181 #else
182  script->run(confFile);
183 #endif
184 
185 #ifdef NAMD_TCL
186 }
187  script->run();
188 #endif
189 
190  BackEnd::exit();
191 }
192 
static Node * Object()
Definition: Node.h:86
std::ostream & iINFO(std::ostream &s)
Definition: InfoStream.C:81
void NAMD_err(const char *err_msg)
Definition: common.C:170
void after_backend_init(int argc, char **argv)
Definition: mainfunc.C:55
void load(char *scriptFile)
Definition: ScriptTcl.C:2419
static void exit(int status=0)
Definition: BackEnd.C:277
std::ostream & endi(std::ostream &s)
Definition: InfoStream.C:54
#define iout
Definition: InfoStream.h:51
static void init(int argc, char **argv)
Definition: BackEnd.C:242
#define CHDIR
Definition: mainfunc.C:24
#define PATHSEP
Definition: mainfunc.C:26
void run()
Definition: ScriptTcl.C:2436
void setScript(ScriptTcl *s)
Definition: Node.h:194
#define CWDSIZE
ScriptTcl * script
Definition: PatchData.h:154
void NAMD_die(const char *err_msg)
Definition: common.C:147
void tclmain(int, char **)
Definition: ScriptTcl.C:2402
void eval(char *script)
Definition: ScriptTcl.C:2362
#define PATHSEPSTR
Definition: mainfunc.C:27
#define GETCWD
Definition: mainfunc.C:25
int main(int argc, char **argv)
Definition: mainfunc.C:43
static int tclsh(int, char **)
Definition: ScriptTcl.C:2380