votca 2024.1-dev
Loading...
Searching...
No Matches
hist.h
Go to the documentation of this file.
1/*
2 * Copyright 2009-2020 The VOTCA Development Team
3 * (http://www.votca.org)
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License")
6 *
7 * You may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
19
20#pragma once
21#ifndef VOTCA_XTP_HIST_H
22#define VOTCA_XTP_HIST_H
23
24// Local VOTCA includes
25#include "checkpoint.h"
26#include "eigen.h"
27#include "energy_terms.h"
28
36namespace votca {
37namespace xtp {
38template <class T>
39class hist {
40 public:
41 T getDiff() const {
42 if (filled_ > 1) {
43 return metric_ - metric_old_;
44 } else if (filled_ == 1) {
45 return metric_;
46 } else {
47 throw std::runtime_error("hist is not filled yet");
48 }
49 }
50
51 const T& back() const { return metric_; }
52 void push_back(const T& metric) {
53 metric_old_ = std::move(metric_);
54 metric_ = metric;
55 filled_++;
56 }
57 void push_back(T&& metric) {
58 metric_old_ = std::move(metric_);
59 metric_ = std::move(metric);
60 filled_++;
61 }
62
63 bool filled() const { return filled_ > 0; }
64
66 w(filled_, "filled");
67 if (filled_ > 0) {
68 WriteMetric(metric_, "metric", w);
69 }
70 if (filled_ > 1) {
71 WriteMetric(metric_old_, "metric_old", w);
72 }
73 }
74
76 r(filled_, "filled");
77 if (filled_ > 0) {
78 ReadMetric(metric_, "metric", r);
79 }
80 if (filled_ > 1) {
81 ReadMetric(metric_old_, "metric_old", r);
82 }
83 }
84
85 private:
86 void ReadMetric(T&, std::string tag, CheckpointReader& r);
87 void WriteMetric(const T&, std::string tag, CheckpointWriter& w) const;
91};
92
93template <class T>
94inline void hist<T>::ReadMetric(T& metric, std::string tag,
96 r(metric, tag);
97}
98template <class T>
99inline void hist<T>::WriteMetric(const T& metric, std::string tag,
100 CheckpointWriter& w) const {
101 w(metric, tag);
102}
103
104template <>
106 std::string tag,
107 CheckpointReader& r) {
108 r(metric.data(), tag);
109}
110template <>
112 std::string tag,
113 CheckpointWriter& w) const {
114 w(metric.data(), tag);
115}
116
117} // namespace xtp
118} // namespace votca
119
120#endif // VOTCA_XTP_HIST_H
const Eigen::Matrix< double, 6, 1 > & data() const
void WriteToCpt(CheckpointWriter &w) const
Definition hist.h:65
const T & back() const
Definition hist.h:51
void WriteMetric(const T &, std::string tag, CheckpointWriter &w) const
Definition hist.h:99
T getDiff() const
Definition hist.h:41
bool filled() const
Definition hist.h:63
void ReadFromCpt(CheckpointReader &r)
Definition hist.h:75
void ReadMetric(T &, std::string tag, CheckpointReader &r)
Definition hist.h:94
void push_back(const T &metric)
Definition hist.h:52
Index filled_
Definition hist.h:88
void push_back(T &&metric)
Definition hist.h:57
base class for all analysis tools
Definition basebead.h:33
Eigen::Index Index
Definition types.h:26