votca
2024.2-dev
Loading...
Searching...
No Matches
tools
src
libtools
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
25
#include "
votca/tools/histogram.h
"
26
27
namespace
votca
{
28
namespace
tools {
29
30
Histogram::Histogram
() =
default
;
31
32
Histogram::Histogram
(
const
options_t
& op) : options_(op) {}
33
34
Histogram::~Histogram
() =
default
;
35
36
void
Histogram::ProcessData
(
DataCollection<double>::selection
* data) {
37
38
pdf_
.assign(
options_
.
n_
, 0);
39
40
if
(
options_
.
auto_interval_
) {
41
min_
= std::numeric_limits<double>::max();
42
max_
= std::numeric_limits<double>::min();
43
options_
.
extend_interval_
=
true
;
44
}
else
{
45
min_
=
options_
.
min_
;
46
max_
=
options_
.
max_
;
47
}
48
for
(
auto
& array : *data) {
49
if
(
options_
.
extend_interval_
||
options_
.
auto_interval_
) {
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) < 1
e
-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) < 1
e
-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
116
void
Histogram::Normalize
() {
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
votca::tools::DataCollection::selection
class for array selection
Definition
datacollection.h:63
votca::tools::Histogram::max_
double max_
Definition
histogram.h:78
votca::tools::Histogram::interval_
double interval_
Definition
histogram.h:79
votca::tools::Histogram::ProcessData
void ProcessData(DataCollection< double >::selection *data)
Definition
histogram.cc:36
votca::tools::Histogram::pdf_
std::vector< double > pdf_
Definition
histogram.h:76
votca::tools::Histogram::Normalize
void Normalize(void)
Definition
histogram.cc:116
votca::tools::Histogram::options_
options_t options_
Definition
histogram.h:81
votca::tools::Histogram::min_
double min_
Definition
histogram.h:77
votca::tools::Histogram::~Histogram
~Histogram()
destructor
votca::tools::Histogram::Histogram
Histogram()
histogram.h
votca::tools::e
@ e
Definition
unitconverter.h:59
votca
base class for all analysis tools
Definition
basebead.h:33
votca::Index
Eigen::Index Index
Definition
types.h:26
votca::tools::Histogram::options_t
Definition
histogram.h:64
votca::tools::Histogram::options_t::normalize_
bool normalize_
Definition
histogram.h:71
votca::tools::Histogram::options_t::auto_interval_
bool auto_interval_
Definition
histogram.h:66
votca::tools::Histogram::options_t::periodic_
bool periodic_
Definition
histogram.h:70
votca::tools::Histogram::options_t::n_
Index n_
Definition
histogram.h:65
votca::tools::Histogram::options_t::min_
double min_
Definition
histogram.h:68
votca::tools::Histogram::options_t::max_
double max_
Definition
histogram.h:69
votca::tools::Histogram::options_t::scale_
std::string scale_
Definition
histogram.h:72
votca::tools::Histogram::options_t::extend_interval_
bool extend_interval_
Definition
histogram.h:67
Generated by
1.12.0