MoveCmd.h 2.7 KB
Newer Older
1 2
////////////////////////////////////////////////////////////////////////////////
//
Francois Gygi committed
3 4 5 6
// Copyright (c) 2008 The Regents of the University of California
//
// This file is part of Qbox
//
Francois Gygi committed
7 8
// Qbox is distributed under the terms of the GNU General Public License
// as published by the Free Software Foundation, either version 2 of
Francois Gygi committed
9 10 11 12 13 14
// the License, or (at your option) any later version.
// See the file COPYING in the root directory of this distribution
// or <http://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////////////
//
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
// MoveCmd.h:
//
////////////////////////////////////////////////////////////////////////////////

#ifndef MOVECMD_H
#define MOVECMD_H

#include <string>
#include <cstdlib>
#include <iostream>
using namespace std;

#include "UserInterface.h"
#include "Sample.h"

class MoveCmd : public Cmd
{
  public:

  Sample *s;

  MoveCmd(Sample *sample) : s(sample) {};

38 39
  const char *name(void) const { return "move"; }
  const char *help_msg(void) const
40
  {
41
    return
42 43 44 45
    "\n move\n\n"
    " syntax: move atom_name {to|by} x y z \n\n"
    "   The move command displaces an atom to a new position.\n"
    "   The new position is defined by absolute coordinates (to) or\n"
46 47 48
    "   by a relative displacement (by).\n"
    "   When using 'to', if one or more of the arguments is '*',\n"
    "   the corresponding component of the velocity is unchanged.\n\n";
49 50 51 52 53 54 55 56 57 58 59
  }

  int action(int argc, char **argv)
  {
    // move must have 6 arguments including the command name
    if ( argc != 6 )
    {
      if ( ui->onpe0() )
        cout << " use: move atom_name {to|by} x y z " << endl;
      return 1;
    }
60

61 62 63 64 65 66 67 68 69 70
    const string atom_name = argv[1];
    const string mode = argv[2];
    const string xs = argv[3];
    const string ys = argv[4];
    const string zs = argv[5];

    Atom* pa = s->atoms.findAtom(atom_name);
    if ( !pa )
    {
      if ( ui->onpe0() )
71
        cout << " MoveCmd: could not find atom " << atom_name << endl;
72 73 74 75 76 77 78 79
      return 1;
    }

    D3vector pos = pa->position();

    double x = pos.x;
    double y = pos.y;
    double z = pos.z;
80

81 82 83 84 85 86 87 88
    if ( mode == "to" )
    {
      if ( xs != "*" )
        x = atof(argv[3]);
      if ( ys != "*" )
        y = atof(argv[4]);
      if ( zs != "*" )
        z = atof(argv[5]);
89
      pos = D3vector(x,y,z);
90 91 92 93 94 95
    }
    else if ( mode == "by" )
    {
      x += atof(argv[3]);
      y += atof(argv[4]);
      z += atof(argv[5]);
96
      pos = D3vector(x,y,z);
97 98 99 100
    }
    else
    {
      if ( ui->onpe0() )
101
        cout << " MoveCmd: unknown mode" << endl;
102 103 104 105 106
      return 1;
    }

    pa->set_position(pos);
    if ( ui->onpe0() )
107 108
      cout << " MoveCmd: atom " << atom_name << " moved to "
           << pos << endl;
109

110 111 112 113
    return 0;
  }
};
#endif