NetCharge.h 2.07 KB
Newer Older
Francois Gygi committed
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/>.
//
////////////////////////////////////////////////////////////////////////////////
//
Francois Gygi committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
// NetCharge.h
//
////////////////////////////////////////////////////////////////////////////////

#ifndef NETCHARGE_H
#define NETCHARGE_H

#include<iostream>
#include<iomanip>
#include<sstream>
#include<stdlib.h>

#include "Sample.h"

class NetCharge : public Var
{
  Sample *s;

  public:

35
  const char *name ( void ) const { return "net_charge"; };
Francois Gygi committed
36 37 38 39 40 41 42 43 44

  int set ( int argc, char **argv )
  {
    if ( argc != 2 )
    {
      if ( ui->onpe0() )
      cout << " net_charge takes only one value" << endl;
      return 1;
    }
45

Francois Gygi committed
46 47 48 49 50 51 52 53
    const int v = atoi(argv[1]);

    // compute the current netcharge
    // Definition: wf_nel = atoms_nel - netcharge
    // Note: the sign of netcharge is negative if extra electrons are present
    const int netcharge_before = s->atoms.nel() - s->wf.nel();
    if ( v == netcharge_before )
      return 0;
54

Francois Gygi committed
55 56 57 58 59 60 61 62
    // set new netcharge to v
    if ( s->atoms.nel() - v < 0 )
    {
      if ( ui->onpe0() )
        cout << " net_charge: cannot remove more than "
             << s->atoms.nel() << " electrons" << endl;
      return 1;
    }
63

Francois Gygi committed
64 65 66 67 68 69
    s->wf.set_nel(s->atoms.nel() - v);
    s->wf.update_occ(0.0);
    if ( s->wfv != 0 )
    {
      s->wfv->set_nel(s->atoms.nel() - v);
    }
70

Francois Gygi committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
    return 0;
  }

  string print (void) const
  {
     ostringstream st;
     st.setf(ios::left,ios::adjustfield);
     st << setw(10) << name() << " = ";
     st.setf(ios::right,ios::adjustfield);
     st << setw(10) << s->atoms.nel() - s->wf.nel();
     return st.str();
  }

  NetCharge(Sample *sample) : s(sample) {};
};
#endif