potentialfunction.cc 2.75 KB
Newer Older
1 2
/*
 * Copyright 2009-2019 The VOTCA Development Team (http://www.votca.org)
Christoph Junghans's avatar
Christoph Junghans committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
17 18

#include <votca/csg/potentialfunctions/potentialfunction.h>
19 20 21 22 23 24
#include <votca/tools/table.h>

#include <boost/lexical_cast.hpp>

using namespace std;
using namespace votca::tools;
25

26
namespace votca {
Jens Wehner's avatar
Jens Wehner committed
27
namespace csg {
28

Jens Wehner's avatar
Jens Wehner committed
29
PotentialFunction::PotentialFunction(const string &name_, const int nlam_,
30
                                     const double min_, const double max_) {
31 32

  _name = name_;
33
  _lam = Eigen::VectorXd::Zero(nlam_);
34 35 36 37 38 39 40 41 42
  _min = min_;
  _cut_off = max_;
}

void PotentialFunction::setParam(string filename) {

  Table param;
  param.Load(filename);

43
  if (param.size() != _lam.size()) {
44

Jens Wehner's avatar
Jens Wehner committed
45 46 47 48 49
    throw std::runtime_error(
        "In potential " + _name + ": parameters size mismatch!\n"
                                  "Check input parameter file \"" +
        filename + "\" \nThere should be " +
        boost::lexical_cast<string>(_lam.size()) + " parameters");
50
  } else {
Jens Wehner's avatar
Jens Wehner committed
51
    for (unsigned int i = 0; i < _lam.size(); i++)
52 53 54 55
      _lam(i) = param.y(i);
  }
}

Jens Wehner's avatar
Jens Wehner committed
56
void PotentialFunction::SaveParam(const string &filename) {
57 58 59

  Table param;
  param.SetHasYErr(false);
Jens Wehner's avatar
Jens Wehner committed
60
  param.resize(_lam.size());
61

Christoph Junghans's avatar
Christoph Junghans committed
62
  for (unsigned int i = 0; i < _lam.size(); i++)
63 64 65 66 67
    param.set(i, i, _lam(i), 'i');

  param.Save(filename);
}

Jens Wehner's avatar
Jens Wehner committed
68
void PotentialFunction::SavePotTab(const string &filename, const double step) {
69
  int ngrid = (int)((_cut_off - _min) / step + 1.00000001);
70 71
  Table pot_tab;
  pot_tab.SetHasYErr(false);
Jens Wehner's avatar
Jens Wehner committed
72
  pot_tab.resize(ngrid);
73 74 75 76 77 78 79 80 81 82
  double r_init;
  int i;

  for (r_init = _min, i = 0; i < ngrid - 1; r_init += step)
    pot_tab.set(i++, r_init, CalculateF(r_init), 'i');

  pot_tab.set(i, _cut_off, CalculateF(_cut_off), 'i');
  pot_tab.Save(filename);
}

Jens Wehner's avatar
Jens Wehner committed
83
void PotentialFunction::SavePotTab(const string &filename, const double step,
84 85
                                   const double rmin, const double rcut) {
  int ngrid = (int)((rcut - rmin) / step + 1.00000001);
86 87
  Table pot_tab;
  pot_tab.SetHasYErr(false);
Jens Wehner's avatar
Jens Wehner committed
88
  pot_tab.resize(ngrid);
89 90
  double r_init;
  int i;
91
  char flag = 'i';
92

93
  for (r_init = rmin, i = 0; i < ngrid - 1; r_init += step)
94 95 96 97 98
    pot_tab.set(i++, r_init, CalculateF(r_init), flag);

  pot_tab.set(i, rcut, CalculateF(rcut), flag);
  pot_tab.Save(filename);
}
99 100
}
}