1 |
|
---|
2 | #ifdef __GNUG__
|
---|
3 | #pragma implementation
|
---|
4 | #endif
|
---|
5 |
|
---|
6 | #include <string>
|
---|
7 | #include <string.h>
|
---|
8 | #include <stdlib.h>
|
---|
9 | #include <scconfig.h>
|
---|
10 | #include <util/ref/ref.h>
|
---|
11 | #include <util/keyval/keyval.h>
|
---|
12 | #include <util/misc/formio.h>
|
---|
13 | #include <chemistry/qc/psi/psiexenv.h>
|
---|
14 |
|
---|
15 | using namespace std;
|
---|
16 |
|
---|
17 | namespace sc {
|
---|
18 |
|
---|
19 | static ClassDesc PsiExEnv_cd(
|
---|
20 | typeid(PsiExEnv),"PsiExEnv",1,"public DescribedClass",
|
---|
21 | 0, create<PsiExEnv>, 0);
|
---|
22 |
|
---|
23 | string PsiExEnv::inputname_("input.dat");
|
---|
24 | string PsiExEnv::file11name_("file11.dat");
|
---|
25 | int PsiExEnv::ckptfile_(32);
|
---|
26 | string PsiExEnv::defaultcwd_("/tmp");
|
---|
27 | string PsiExEnv::defaultfileprefix_("psi");
|
---|
28 | string PsiExEnv::defaultpsiprefix_("/usr/local/psi/bin");
|
---|
29 | string PsiExEnv::defaultstdout_("psi.stdout");
|
---|
30 | string PsiExEnv::defaultstderr_("psi.stderr");
|
---|
31 |
|
---|
32 | PsiExEnv::PsiExEnv(const Ref<KeyVal>& keyval)
|
---|
33 | {
|
---|
34 | // Find Psi
|
---|
35 | char *psibin = getenv("PSIBIN");
|
---|
36 | if (psibin)
|
---|
37 | psiprefix_ = string(psibin);
|
---|
38 | else
|
---|
39 | psiprefix_ = string(defaultpsiprefix_);
|
---|
40 | add_to_path(psiprefix_);
|
---|
41 |
|
---|
42 | char *cwdchar = keyval->pcharvalue("cwd");
|
---|
43 | if (cwdchar)
|
---|
44 | cwd_ = string(cwdchar);
|
---|
45 | else
|
---|
46 | cwd_ = string(defaultcwd_);
|
---|
47 | char *fileprefixchar = keyval->pcharvalue("fileprefix");
|
---|
48 | if (fileprefixchar)
|
---|
49 | fileprefix_ = string(fileprefixchar);
|
---|
50 | else
|
---|
51 | fileprefix_ = string(defaultfileprefix_);
|
---|
52 |
|
---|
53 | char *stdout_char = keyval->pcharvalue("stdout");
|
---|
54 | if (stdout_char)
|
---|
55 | stdout_ = string(stdout_char);
|
---|
56 | else
|
---|
57 | stdout_ = string(defaultstdout_);
|
---|
58 | delete[] stdout_char;
|
---|
59 | char *stderr_char = keyval->pcharvalue("stderr");
|
---|
60 | if (stderr_char)
|
---|
61 | stderr_ = string(stderr_char);
|
---|
62 | else
|
---|
63 | stderr_ = string(defaultstderr_);
|
---|
64 | delete[] stderr_char;
|
---|
65 |
|
---|
66 | nscratch_ = keyval->intvalue("nscratch");
|
---|
67 | if (nscratch_ != keyval->count("scratch")) {
|
---|
68 | ExEnv::err0() << indent
|
---|
69 | << "PsiExEnv: number of scratch directories != nscratch\n";
|
---|
70 | abort();
|
---|
71 | }
|
---|
72 | scratch_ = new string[nscratch_];
|
---|
73 | for (int i=0; i<nscratch_; i++)
|
---|
74 | scratch_[i] = string(keyval->pcharvalue("scratch",i));
|
---|
75 |
|
---|
76 | char *s = new char[cwd_.size() + inputname_.size() + 2];
|
---|
77 | sprintf(s,"%s/%s",cwd_.c_str(),inputname_.c_str());
|
---|
78 | psiinput_ = new PsiInput(s);
|
---|
79 | delete[] s;
|
---|
80 |
|
---|
81 | s = new char[cwd_.size() + fileprefix_.size() + file11name_.size() + 3];
|
---|
82 | sprintf(s,"%s/%s.%s",cwd_.c_str(),fileprefix_.c_str(),file11name_.c_str());
|
---|
83 | psifile11_ = new PsiFile11(s);
|
---|
84 | delete[] s;
|
---|
85 | }
|
---|
86 |
|
---|
87 | PsiExEnv::PsiExEnv(char *cwd, char *fileprefix, int nscratch, char **scratch):
|
---|
88 | cwd_(cwd), fileprefix_(fileprefix), nscratch_(nscratch)
|
---|
89 | {
|
---|
90 | // Find Psi
|
---|
91 | char *psibin = 0;
|
---|
92 | psibin = getenv("PSIBIN");
|
---|
93 | if (psibin)
|
---|
94 | psiprefix_ = string(psibin);
|
---|
95 | else
|
---|
96 | psiprefix_ = string(defaultpsiprefix_);
|
---|
97 | add_to_path(psiprefix_);
|
---|
98 |
|
---|
99 | scratch_ = new string[nscratch_];
|
---|
100 | for(int i=0; i<nscratch_; i++)
|
---|
101 | scratch_[i] = string(scratch[i]);
|
---|
102 |
|
---|
103 | char *s = new char[cwd_.size() + inputname_.size() + 2];
|
---|
104 | sprintf(s,"%s/%s",cwd_.c_str(),inputname_.c_str());
|
---|
105 | psiinput_ = new PsiInput(s);
|
---|
106 | delete[] s;
|
---|
107 |
|
---|
108 | s = new char[cwd_.size() + fileprefix_.size() + file11name_.size() + 3];
|
---|
109 | sprintf(s,"%s/%s.%s",cwd_.c_str(),fileprefix_.c_str(),file11name_.c_str());
|
---|
110 | psifile11_ = new PsiFile11(s);
|
---|
111 | delete[] s;
|
---|
112 | }
|
---|
113 |
|
---|
114 | PsiExEnv::~PsiExEnv()
|
---|
115 | {
|
---|
116 | delete[] scratch_;
|
---|
117 | }
|
---|
118 |
|
---|
119 | void PsiExEnv::add_to_path(const string& dir)
|
---|
120 | {
|
---|
121 | if (dir.size()) {
|
---|
122 | char *path = getenv("PATH");
|
---|
123 | int newpath_len = strlen(path) + dir.size() + 1;
|
---|
124 | char *newpath = new char[newpath_len];
|
---|
125 | sprintf(newpath,"%s:%s",dir.c_str(),path);
|
---|
126 | #ifdef HAVE_SETENV
|
---|
127 | setenv("PATH",newpath,1);
|
---|
128 | #else
|
---|
129 | string putenvstr("PATH=");
|
---|
130 | putenvstr += newpath;
|
---|
131 | char *putenvcstr = strcpy(new char[putenvstr.size()+1], putenvstr.c_str());
|
---|
132 | putenv(putenvcstr);
|
---|
133 | #endif
|
---|
134 | delete[] newpath;
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | int PsiExEnv::run_psi()
|
---|
139 | {
|
---|
140 | int errcod;
|
---|
141 | if (errcod = run_psi_module("psi3")) {
|
---|
142 | return errcod;
|
---|
143 | }
|
---|
144 | if (errcod = run_psi_module("psiclean")) {
|
---|
145 | return errcod;
|
---|
146 | }
|
---|
147 | return 0;
|
---|
148 | }
|
---|
149 |
|
---|
150 | int PsiExEnv::run_psi_module(char *module)
|
---|
151 | {
|
---|
152 | int errcod;
|
---|
153 | char *module_cmd = new char[2*cwd_.size()+strlen(module)+psiprefix_.size()+fileprefix_.size()+stdout_.size()+stderr_.size()+40];
|
---|
154 | sprintf(module_cmd,"cd %s; %s/%s -p %s/%s 1>> %s 2>> %s",cwd_.c_str(),psiprefix_.c_str(),module,cwd_.c_str(),
|
---|
155 | fileprefix_.c_str(),stdout_.c_str(),stderr_.c_str());
|
---|
156 | if (errcod = system(module_cmd)) {
|
---|
157 | ExEnv::outn() << "PsiExEnv::run_psi_module -- module " << module << " failed" << endl;
|
---|
158 | abort();
|
---|
159 | }
|
---|
160 | return errcod;
|
---|
161 | }
|
---|
162 |
|
---|
163 | void PsiExEnv::print(std::ostream&o) const
|
---|
164 | {
|
---|
165 | o << endl;
|
---|
166 | o << indent << "PsiExEnv:" << endl << incindent;
|
---|
167 | o << indent << "Location of Psi: " << psiprefix_ << endl;
|
---|
168 | o << indent << "Current Psi Working Directory: " << cwd_ << endl;
|
---|
169 | o << indent << "Current Psi File Prefix: " << fileprefix_ << endl;
|
---|
170 | o << indent << "Number of Scratch Groups: " << nscratch_ << endl;
|
---|
171 | for(int i=0; i<nscratch_; i++)
|
---|
172 | o << indent << "Scratch Group " << i << ": " << scratch_[i] << endl;
|
---|
173 | o << endl << decindent;
|
---|
174 | }
|
---|
175 |
|
---|
176 | }
|
---|