votca 2024.2-dev
Loading...
Searching...
No Matches
histogram.cc
Go to the documentation of this file.
1/*
2 * Copyright 2009-2020 The VOTCA Development Team (http://www.votca.org)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18// Standard includes
19#include <cmath>
20#include <functional>
21#include <limits>
22#include <numeric>
23
24// Local VOTCA includes
26
27namespace votca {
28namespace tools {
29
30Histogram::Histogram() = default;
31
32Histogram::Histogram(const options_t& op) : options_(op) {}
33
34Histogram::~Histogram() = default;
35
37
38 pdf_.assign(options_.n_, 0);
39
41 min_ = std::numeric_limits<double>::max();
42 max_ = std::numeric_limits<double>::min();
44 } else {
47 }
48 for (auto& array : *data) {
50 for (auto& value : *array) {
51 min_ = std::min(value, min_);
52 max_ = std::max(value, max_);
53 }
54 }
55 }
56
57 interval_ = (max_ - min_) / (double)(options_.n_ - 1);
58
59 double v = 1.;
60 for (auto& array : *data) {
61 for (auto& value : *array) {
62 Index ii = (Index)floor((value - min_) / interval_ +
63 0.5); // the interval should
64 // be centered around
65 // the sampling point
66 if (ii < 0 || ii >= options_.n_) {
67 if (options_.periodic_) {
68 while (ii < 0) {
69 ii += options_.n_;
70 }
71 ii = ii % options_.n_;
72 } else {
73 continue;
74 }
75 }
76 pdf_[ii] += v;
77 }
78 }
79
80 if (options_.scale_ == "bond") {
81 for (size_t i = 0; i < pdf_.size(); ++i) {
82 double r = min_ + interval_ * (double)i;
83 if (std::fabs(r) < 1e-10) {
84 r = min_ + interval_ * (double)(i + 1);
85 pdf_[i] = pdf_[i + 1];
86 }
87 pdf_[i] /= (r * r);
88 }
89 } else if (options_.scale_ == "angle") {
90 for (size_t i = 0; i < pdf_.size(); ++i) {
91 double alpha = min_ + interval_ * (double)i;
92 double sa = sin(alpha);
93 if (std::fabs(sa) < 1e-5) {
94 if (i < pdf_.size() - 1) {
95 alpha = min_ + interval_ * (double)(i + 1);
96 pdf_[i] = pdf_[i + 1] / sin(alpha);
97 } else {
98 pdf_[i] = pdf_[i - 1];
99 }
100
101 } else {
102 pdf_[i] /= sa;
103 }
104 }
105 }
106
107 if (options_.periodic_) {
108 pdf_[0] = (pdf_[0] + pdf_[options_.n_ - 1]);
109 pdf_[options_.n_ - 1] = pdf_[0];
110 }
111 if (options_.normalize_) {
112 Normalize();
113 }
114}
115
117 double norm = 1. / (interval_ * accumulate(pdf_.begin(), pdf_.end(), 0.0));
118 std::transform(pdf_.begin(), pdf_.end(), pdf_.begin(),
119 [norm](double a) { return a * norm; });
120}
121
122} // namespace tools
123} // namespace votca
void ProcessData(DataCollection< double >::selection *data)
Definition histogram.cc:36
std::vector< double > pdf_
Definition histogram.h:76
base class for all analysis tools
Definition basebead.h:33
Eigen::Index Index
Definition types.h:26