Octopus
anygrid.hpp
Go to the documentation of this file.
1#ifndef PSEUDO_ANYGRID_HPP
2#define PSEUDO_ANYGRID_HPP
3
4/*
5 Copyright (C) 2018 Xavier Andrade
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20*/
21
22#include "base.hpp"
23#include "spline.h"
24#include <string>
25#include <vector>
26
27namespace pseudopotential {
28
29class anygrid : public pseudopotential::base {
30
31public:
32 anygrid(bool uniform_grid) : uniform_grid_(uniform_grid) {}
33
34 double mesh_spacing() const { return 0.01; }
35
36 int mesh_size() const {
37 if (uniform_grid_) {
38 return mesh_size_;
39 } else {
40 return grid_.size();
41 }
42 }
43
44 virtual void grid(std::vector<double> &val) const {
45 if (uniform_grid_) {
46 pseudopotential::base::grid(val);
47 } else {
48 val = grid_;
49 }
50 }
51
52 virtual void grid_weights(std::vector<double> &val) const {
53 if (uniform_grid_) {
54 pseudopotential::base::grid(val);
55 } else {
56 val = grid_weights_;
57 }
58 }
59
60protected:
61 void interpolate(std::vector<double> &function) const {
62 if (!uniform_grid_)
63 return;
64
65 std::vector<double> function_in_grid = function;
66
67 assert(function.size() == grid_.size());
68
69 Spline function_spline;
70 function_spline.fit(grid_.data(), function_in_grid.data(),
71 function_in_grid.size(), SPLINE_FLAT_BC,
72 SPLINE_NATURAL_BC);
73
74 function.clear();
75 for (double rr = 0.0; rr <= grid_[grid_.size() - 1]; rr += mesh_spacing()) {
76 function.push_back(function_spline.value(rr));
77 }
78 }
79
80 bool uniform_grid_;
81 std::vector<double> grid_;
82 std::vector<double> grid_weights_;
83 int mesh_size_;
84};
85
86} // namespace pseudopotential
87
88#endif