~ubuntu-branches/debian/sid/lammps/sid

« back to all changes in this revision

Viewing changes to lib/colvars/colvarscript.cpp

  • Committer: Package Import Robot
  • Author(s): Anton Gladky
  • Date: 2015-04-29 23:44:49 UTC
  • mfrom: (5.1.3 experimental)
  • Revision ID: package-import@ubuntu.com-20150429234449-mbhy9utku6hp6oq8
Tags: 0~20150313.gitfa668e1-1
Upload into unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// -*- c++ -*-
 
2
 
 
3
#include <cstdlib>
 
4
#include <stdlib.h>
 
5
#include <string.h>
 
6
 
 
7
#include "colvarscript.h"
 
8
 
 
9
 
 
10
colvarscript::colvarscript(colvarproxy *p)
 
11
 : proxy(p),
 
12
   colvars(p->colvars),
 
13
   proxy_error(0)
 
14
{
 
15
}
 
16
 
 
17
/// Run method based on given arguments
 
18
int colvarscript::run(int argc, char const *argv[]) {
 
19
 
 
20
  result = "";
 
21
 
 
22
  if (cvm::debug()) {
 
23
    cvm::log("Called script run with " + cvm::to_str(argc) + " args");
 
24
    for (int i = 0; i < argc; i++) { cvm::log(argv[i]); }
 
25
  }
 
26
 
 
27
  if (argc < 2) {
 
28
    result = "usage: "+std::string(argv[0])+" <subcommand> [args...]\n\
 
29
\n\
 
30
Managing the colvars module:\n\
 
31
  configfile <file name>      -- read configuration from a file\n\
 
32
  config <string>             -- read configuration from the given string\n\
 
33
  reset                       -- delete all internal configuration\n\
 
34
  delete                      -- delete this colvars module instance\n\
 
35
  version                     -- return version of colvars code\n\
 
36
  \n\
 
37
Input and output:\n\
 
38
  list                        -- return a list of all variables\n\
 
39
  list biases                 -- return a list of all biases\n\
 
40
  load <file name>            -- load a state file (requires configuration)\n\
 
41
  update                      -- recalculate colvars and biases based\n\
 
42
  printframe                  -- return a summary of the current frame\n\
 
43
  printframelabels            -- return labels to annotate printframe's output\n";
 
44
 
 
45
  if (proxy->frame() != COLVARS_NOT_IMPLEMENTED) {
 
46
      result += "\
 
47
  frame                       -- return current frame number\n\
 
48
  frame <new_frame>           -- set frame number\n";
 
49
  }
 
50
 
 
51
  result += "\n\
 
52
Accessing collective variables:\n\
 
53
  colvar <name> value         -- return the current value of colvar <name>\n\
 
54
  colvar <name> update        -- recalculate colvar <name>\n\
 
55
  colvar <name> type          -- return the type of colvar <name>\n\
 
56
  colvar <name> delete        -- delete colvar <name>\n\
 
57
  colvar <name> addforce <F>  -- apply given force on colvar <name>\n\
 
58
  colvar <name> getconfig     -- return config string of colvar <name>\n\
 
59
\n\
 
60
Accessing biases:\n\
 
61
  bias <name> energy          -- return the current energy of bias <name>\n\
 
62
  bias <name> update          -- recalculate bias <name>\n\
 
63
  bias <name> delete          -- delete bias <name>\n\
 
64
  bias <name> getconfig       -- return config string of bias <name>\n\
 
65
\n\
 
66
";
 
67
    return COLVARSCRIPT_OK;
 
68
  }
 
69
 
 
70
  std::string cmd = argv[1];
 
71
 
 
72
  if (cmd == "colvar") {
 
73
    return proc_colvar(argc-1, &(argv[1]));
 
74
  }
 
75
 
 
76
  if (cmd == "bias") {
 
77
    return proc_bias(argc-1, &(argv[1]));
 
78
  }
 
79
 
 
80
  if (cmd == "version") {
 
81
    result = COLVARS_VERSION;
 
82
    return COLVARSCRIPT_OK;
 
83
  }
 
84
 
 
85
  if (cmd == "reset") {
 
86
    /// Delete every child object
 
87
    colvars->reset();
 
88
    return COLVARSCRIPT_OK;
 
89
  }
 
90
 
 
91
  if (cmd == "delete") {
 
92
    colvars->reset();
 
93
    // Note: the delete bit may be ignored by some backends
 
94
    // it is mostly useful in VMD
 
95
    colvars->set_error_bits(DELETE_COLVARS);
 
96
    return COLVARSCRIPT_OK;
 
97
  }
 
98
 
 
99
  if (cmd == "update") {
 
100
    colvars->calc();
 
101
    return COLVARSCRIPT_OK;
 
102
  }
 
103
 
 
104
  if (cmd == "list") {
 
105
    if (argc == 2) {
 
106
      for (std::vector<colvar *>::iterator cvi = colvars->colvars.begin();
 
107
           cvi != colvars->colvars.end();
 
108
           ++cvi) {
 
109
        result += (cvi == colvars->colvars.begin() ? "" : " ") + (*cvi)->name;
 
110
      }
 
111
      return COLVARSCRIPT_OK;
 
112
    } else if (argc == 3 && !strcmp(argv[2], "biases")) {
 
113
      for (std::vector<colvarbias *>::iterator bi = colvars->biases.begin();
 
114
           bi != colvars->biases.end();
 
115
           ++bi) {
 
116
        result += (bi == colvars->biases.begin() ? "" : " ") + (*bi)->name;
 
117
      }
 
118
      return COLVARSCRIPT_OK;
 
119
    } else {
 
120
      result = "Wrong arguments to command \"list\"";
 
121
      return COLVARSCRIPT_ERROR;
 
122
    }
 
123
  }
 
124
 
 
125
  /// Parse config from file
 
126
  if (cmd == "configfile") {
 
127
    if (argc < 3) {
 
128
      result = "Missing arguments";
 
129
      return COLVARSCRIPT_ERROR;
 
130
    }
 
131
    if (colvars->read_config_file(argv[2]) == COLVARS_OK) {
 
132
      return COLVARSCRIPT_OK;
 
133
    } else {
 
134
      return COLVARSCRIPT_ERROR;
 
135
    }
 
136
  }
 
137
 
 
138
  /// Parse config from string
 
139
  if (cmd == "config") {
 
140
    if (argc < 3) {
 
141
      result = "Missing arguments";
 
142
      return COLVARSCRIPT_ERROR;
 
143
    }
 
144
    std::string conf = argv[2];
 
145
    if (colvars->read_config_string(conf) == COLVARS_OK) {
 
146
      return COLVARSCRIPT_OK;
 
147
    } else {
 
148
      return COLVARSCRIPT_ERROR;
 
149
    }
 
150
  }
 
151
 
 
152
  /// Load an input state file
 
153
  if (cmd == "load") {
 
154
    if (argc < 3) {
 
155
      result = "Missing arguments";
 
156
      return COLVARSCRIPT_ERROR;
 
157
    }
 
158
    proxy->input_prefix_str = argv[2];
 
159
    if (colvars->setup_input() == COLVARS_OK) {
 
160
      return COLVARSCRIPT_OK;
 
161
    } else {
 
162
      return COLVARSCRIPT_ERROR;
 
163
    }
 
164
  }
 
165
 
 
166
  /// TODO Write an output state file? (Useful for testing)
 
167
 
 
168
  /// Print the values that would go on colvars.traj
 
169
  if (cmd == "printframelabels") {
 
170
    std::ostringstream os;
 
171
    colvars->write_traj_label(os);
 
172
    result = os.str();
 
173
    return COLVARSCRIPT_OK;
 
174
  }
 
175
  if (cmd == "printframe") {
 
176
    std::ostringstream os;
 
177
    colvars->write_traj(os);
 
178
    result = os.str();
 
179
    return COLVARSCRIPT_OK;
 
180
  }
 
181
 
 
182
  if (cmd == "frame") {
 
183
    if (argc == 2) {
 
184
      int f = proxy->frame();
 
185
      if (f >= 0) {
 
186
        result = cvm::to_str(f);
 
187
        return COLVARSCRIPT_OK;
 
188
      } else {
 
189
        result = "Frame number is not available";
 
190
        return COLVARSCRIPT_ERROR;
 
191
      }
 
192
    } else if (argc == 3) {
 
193
      // Failure of this function does not trigger an error, but
 
194
      // returns the plain result to let scripts detect available frames
 
195
      long int f = proxy->frame(strtol(argv[2], NULL, 10));
 
196
      colvars->it = proxy->frame();
 
197
      result = cvm::to_str(f);
 
198
      return COLVARSCRIPT_OK;
 
199
    } else {
 
200
      result = "Wrong arguments to command \"frame\"";
 
201
      return COLVARSCRIPT_ERROR;
 
202
    }
 
203
  }
 
204
 
 
205
  result = "Syntax error";
 
206
  return COLVARSCRIPT_ERROR;
 
207
}
 
208
 
 
209
 
 
210
int colvarscript::proc_colvar(int argc, char const *argv[]) {
 
211
  std::string name = argv[1];
 
212
  colvar *cv = cvm::colvar_by_name(name);
 
213
  if (cv == NULL) {
 
214
    result = "Colvar not found: " + name;
 
215
    return COLVARSCRIPT_ERROR;
 
216
  }
 
217
  if (argc < 3) {
 
218
    result = "Missing parameters";
 
219
    return COLVARSCRIPT_ERROR;
 
220
  }
 
221
  std::string subcmd = argv[2];
 
222
 
 
223
  if (subcmd == "value") {
 
224
    result = (cv->value()).to_simple_string();
 
225
    return COLVARSCRIPT_OK;
 
226
  }
 
227
 
 
228
  if (subcmd == "width") {
 
229
    result = cvm::to_str(cv->width, 0, cvm::cv_prec);
 
230
    return COLVARSCRIPT_OK;
 
231
  }
 
232
 
 
233
  if (subcmd == "type") {
 
234
    result = cv->value().type_desc(cv->value().value_type);
 
235
    return COLVARSCRIPT_OK;
 
236
  }
 
237
 
 
238
  if (subcmd == "update") {
 
239
    cv->calc();
 
240
    cv->update();
 
241
    result = (cv->value()).to_simple_string();
 
242
    return COLVARSCRIPT_OK;
 
243
  }
 
244
 
 
245
  if (subcmd == "delete") {
 
246
    if (cv->biases.size() > 0) {
 
247
      result = "Cannot delete a colvar currently used by biases, delete those biases first";
 
248
      return COLVARSCRIPT_ERROR;
 
249
    }
 
250
    // colvar destructor is tasked with the cleanup
 
251
    delete cv;
 
252
    // TODO this could be done by the destructors
 
253
    colvars->write_traj_label(colvars->cv_traj_os);
 
254
    return COLVARSCRIPT_OK;
 
255
  }
 
256
 
 
257
  if (subcmd == "getconfig") {
 
258
    result = cv->get_config();
 
259
    return COLVARSCRIPT_OK;
 
260
  }
 
261
 
 
262
  if (subcmd == "addforce") {
 
263
    if (argc < 4) {
 
264
      result = "addforce: missing parameter: force value";
 
265
      return COLVARSCRIPT_ERROR;
 
266
    }
 
267
    std::string f_str = argv[3];
 
268
    std::istringstream is(f_str);
 
269
    is.width(cvm::cv_width);
 
270
    is.precision(cvm::cv_prec);
 
271
    colvarvalue force(cv->value());
 
272
    force.is_derivative();
 
273
    if (force.from_simple_string(is.str()) != COLVARS_OK) {
 
274
      result = "addforce : error parsing force value";
 
275
      return COLVARSCRIPT_ERROR;
 
276
    }
 
277
    cv->add_bias_force(force);
 
278
    result = force.to_simple_string();
 
279
    return COLVARSCRIPT_OK;
 
280
  }
 
281
 
 
282
  result = "Syntax error";
 
283
  return COLVARSCRIPT_ERROR;
 
284
}
 
285
 
 
286
 
 
287
int colvarscript::proc_bias(int argc, char const *argv[]) {
 
288
  std::string name = argv[1];
 
289
  colvarbias *b = cvm::bias_by_name(name);
 
290
  if (b == NULL) {
 
291
    result = "Bias not found: " + name;
 
292
    return COLVARSCRIPT_ERROR;
 
293
  }
 
294
 
 
295
  if (argc < 3) {
 
296
    result = "Missing parameters";
 
297
    return COLVARSCRIPT_ERROR;
 
298
  }
 
299
  std::string subcmd = argv[2];
 
300
 
 
301
  if (subcmd == "energy") {
 
302
    result = cvm::to_str(b->get_energy());
 
303
    return COLVARSCRIPT_OK;
 
304
  }
 
305
 
 
306
  if (subcmd == "update") {
 
307
    b->update();
 
308
    result = cvm::to_str(b->get_energy());
 
309
    return COLVARSCRIPT_OK;
 
310
  }
 
311
 
 
312
  if (subcmd == "getconfig") {
 
313
    result = b->get_config();
 
314
    return COLVARSCRIPT_OK;
 
315
  }
 
316
 
 
317
  // Subcommands for MW ABF
 
318
  if (subcmd == "bin") {
 
319
    int r = b->current_bin();
 
320
    result = cvm::to_str(r);
 
321
    return COLVARSCRIPT_OK;
 
322
  }
 
323
 
 
324
  if (subcmd == "binnum") {
 
325
    int r = b->bin_num();
 
326
    if (r < 0) {
 
327
      result = "Error: calling bin_num() for bias " + b->name;
 
328
      return COLVARSCRIPT_ERROR;
 
329
    }
 
330
    result = cvm::to_str(r);
 
331
    return COLVARSCRIPT_OK;
 
332
  }
 
333
 
 
334
  if (subcmd == "share") {
 
335
    int r = b->replica_share();
 
336
    if (r < 0) {
 
337
      result = "Error: calling replica_share() for bias " + b->name;
 
338
      return COLVARSCRIPT_ERROR;
 
339
    }
 
340
    result = cvm::to_str(r);
 
341
    return COLVARSCRIPT_OK;
 
342
  }
 
343
  // End commands for MW ABF
 
344
 
 
345
  if (subcmd == "delete") {
 
346
    // the bias destructor takes care of the cleanup at cvm level
 
347
    delete b;
 
348
    // TODO this could be done by the destructors
 
349
    colvars->write_traj_label(colvars->cv_traj_os);
 
350
    return COLVARSCRIPT_OK;
 
351
  }
 
352
 
 
353
  if (argc >= 4) {
 
354
    std::string param = argv[3];
 
355
    if (subcmd == "count") {
 
356
      int index;
 
357
      if (!(std::istringstream(param) >> index)) {
 
358
        result = "bin_count: error parsing bin index";
 
359
        return COLVARSCRIPT_ERROR;
 
360
      }
 
361
      result = cvm::to_str(b->bin_count(index));
 
362
      return COLVARSCRIPT_OK;
 
363
    }
 
364
 
 
365
    result = "Syntax error";
 
366
    return COLVARSCRIPT_ERROR;
 
367
  }
 
368
  result = "Syntax error";
 
369
  return COLVARSCRIPT_ERROR;
 
370
}