AngleCmd.h 2.76 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
// AngleCmd.h:
//
////////////////////////////////////////////////////////////////////////////////

#ifndef ANGLECMD_H
#define ANGLECMD_H

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

class AngleCmd : public Cmd
{
  public:

  Sample *s;

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

  char *name(void) const { return "angle"; }
  char *help_msg(void) const
  {
38
    return
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
    "\n angle\n\n"
    " syntax: angle name1 name2 name3\n\n"
    "   The angle command prints the angle defined by three atoms.\n\n";
  }

  int action(int argc, char **argv)
  {
    if ( argc != 4 )
    {
      if ( ui->onpe0() )
      {
        cout << " use: angle name1 name2 name3" << endl;
      }
      return 1;
    }
54 55 56 57 58 59 60 61 62

    string name1(argv[1]);
    string name2(argv[2]);
    string name3(argv[3]);
    Atom* a1 = s->atoms.findAtom(name1);
    Atom* a2 = s->atoms.findAtom(name2);
    Atom* a3 = s->atoms.findAtom(name3);
    if ( a1 == 0 || a2 == 0 || a3 == 0 )
    {
63 64 65
      if ( ui->onpe0() )
      {
        if ( a1 == 0 )
66
          cout << " AngleCmd: atom " << name1 << " not defined" << endl;
67
        if ( a2 == 0 )
68
          cout << " AngleCmd: atom " << name2 << " not defined" << endl;
69
        if ( a3 == 0 )
70
          cout << " AngleCmd: atom " << name3 << " not defined" << endl;
71 72 73
      }
      return 1;
    }
74 75 76

    if ( a1 == a2 || a2 == a3 || a3 == a1 )
    {
77 78
      if ( ui->onpe0() )
      {
79 80
        cout << " AngleCmd: replicated atoms in " << name1
             << " " << name2 << " " << name3 << endl;
81 82 83
      }
      return 1;
    }
84

85 86
    D3vector r12(a1->position()-a2->position());
    D3vector r32(a3->position()-a2->position());
87
    if ( norm2(r12) == 0.0 || norm2(r32) == 0.0 )
88
    {
89 90
      if ( ui->onpe0() )
      {
91
        cout << " AngleCmd: atoms are too close" << endl;
92 93 94
      }
      return 1;
    }
95

96 97 98 99 100 101
    const double sp = normalized(r12) * normalized(r32);
    const double c = max(-1.0,min(1.0,sp));
    const double a = (180.0/M_PI)*acos(c);
    if ( ui->onpe0() )
    {
      cout.setf(ios::fixed,ios::floatfield);
102
      cout << " angle " << name1 << "-" << name2  << "-" << name3
103 104
           << ": "
           << setprecision(3)
105
           << a << " (deg)" << endl;
106 107 108 109 110
    }
    return 0;
  }
};
#endif