DistanceCmd.h 2.77 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
// DistanceCmd.h:
//
////////////////////////////////////////////////////////////////////////////////

#ifndef DISTANCECMD_H
#define DISTANCECMD_H

#include <iostream>
#include "UserInterface.h"
#include "Sample.h"
#include <cstdlib>

class DistanceCmd : public Cmd
{
  public:

  Sample *s;

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

35 36
  const char *name(void) const { return "distance"; }
  const char *help_msg(void) const
37
  {
38
    return
39
    "\n distance\n\n"
40 41 42 43
    " syntax: distance [-pbc] name1 name2\n\n"
    "   The distance command prints the distance between two atoms.\n"
    "   If the -pbc option is used, the smallest distance is\n"
    "   computed taking into account periodic boundary conditions.\n\n";
44 45 46 47
  }

  int action(int argc, char **argv)
  {
48
    if ( ! ( argc == 3 || argc == 4 ) )
49 50 51
    {
      if ( ui->onpe0() )
      {
52
        cout << " use: distance [-pbc] name1 name2" << endl;
53 54 55
      }
      return 1;
    }
56

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
    string name1, name2;
    bool use_pbc = false;

    if ( argc == 3 )
    {
      name1 = argv[1];
      name2 = argv[2];
    }
    if ( argc == 4 )
    {
      if ( strcmp(argv[1],"-pbc") )
      {
        if ( ui->onpe0() )
        {
          cout << " use: distance [-pbc] name1 name2" << endl;
        }
        return 1;
      }
      use_pbc = true;
      name1 = argv[2];
      name2 = argv[3];
    }

80 81 82 83
    Atom* a1 = s->atoms.findAtom(name1);
    Atom* a2 = s->atoms.findAtom(name2);
    if ( a1 == 0 || a2 == 0 )
    {
84 85 86 87
      // either a1 or a2 was not found
      if ( ui->onpe0() )
      {
        if ( a1 == 0 )
88
          cout << " DistanceCmd: atom " << name1 << " not defined"
89 90
               << endl;
        if ( a2 == 0 )
91
          cout << " DistanceCmd: atom " << name2 << " not defined"
92 93 94 95
               << endl;
      }
      return 1;
    }
96

97 98
    if ( ui->onpe0() )
    {
99 100 101 102 103 104 105
      D3vector r12 = a1->position()-a2->position();
      if ( use_pbc )
      {
        const UnitCell& cell = s->wf.cell();
        cell.fold_in_ws(r12);
      }
      const double d = length(r12);
106
      cout.setf(ios::fixed,ios::floatfield);
107
      cout << " distance " << name1 << "-" << name2 << ": "
108
           << setprecision(3)
109
           << d << " (a.u.) / " << 0.529177*d << " (Ang)" << endl;
110 111 112 113 114
    }
    return 0;
  }
};
#endif