GlobalExtForce.C 2.24 KB
Newer Older
Francois Gygi committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
////////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2009 The Regents of the University of California
//
// This file is part of Qbox
//
// Qbox is distributed under the terms of the GNU General Public License
// as published by the Free Software Foundation, either version 2 of
// 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/>.
//
////////////////////////////////////////////////////////////////////////////////
//
//  GlobalExtForce.C
//
////////////////////////////////////////////////////////////////////////////////
// $Id: GlobalExtForce.C,v 1.1 2010-02-20 23:13:02 fgygi Exp $

#include "GlobalExtForce.h"
#include "AtomSet.h"
#include "Atom.h"
#include "Species.h"
#include <cassert>
#include <cmath>
#include <iostream>
#include <iomanip>
using namespace std;

////////////////////////////////////////////////////////////////////////////////
void GlobalExtForce::setup(const AtomSet& atoms)
{
  // All atoms are affected, no setup needed
  na_ = atoms.size();
}

////////////////////////////////////////////////////////////////////////////////
double GlobalExtForce::energy(const vector<vector<double> > &r,
                              vector<vector<double> > &f)
{
  double sum = 0.0;
  for ( int is = 0; is < f.size(); is++ )
  {
    const int nais = f[is].size() / 3;
    for ( int ia = 0; ia < nais; ia++ )
    {
      f[is][3*ia+0] += force_.x / na_;
      f[is][3*ia+1] += force_.y / na_;
      f[is][3*ia+2] += force_.z / na_;
      sum -= force_.x * r[is][3*ia+0] / na_ +
             force_.y * r[is][3*ia+1] / na_ +
             force_.z * r[is][3*ia+2] / na_;
    }
  }
  return sum;
}

////////////////////////////////////////////////////////////////////////////////
ostream& GlobalExtForce::print( ostream &os )
{
  os.setf(ios::left,ios::adjustfield);
  os << "  <extforce name=\"" << name();
  os << "\" type=\"" << type();
  os << "\" atoms=\"_all_\"\n";
  os.setf(ios::fixed,ios::floatfield);
  os.setf(ios::right,ios::adjustfield);
  os << "            value=\"" << setprecision(6) << force_.x << " "
     << setprecision(6) << force_.y << " "
     << setprecision(6) << force_.z << "\"/>";
  return os;
}