votca 2024-dev
Loading...
Searching...
No Matches
statetracker.cc
Go to the documentation of this file.
1/*
2 * Copyright 2009-2023 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// Local VOTCA includes
23
24namespace votca {
25namespace xtp {
26using std::flush;
27
29
30 FilterFactory factory;
31 for (const tools::Property& filter : options) {
32 filters_.push_back(factory.Create(filter.name()));
33 }
34
35 for (auto& filter : filters_) {
36 const tools::Property& filterop = options.get(filter->Identify());
37 filter->Initialize(filterop);
38 }
39}
40
43 << "Initial state: " << statehist_[0].ToString() << flush;
44 if (statehist_.size() > 1) {
46 << "Last state: " << statehist_.back().ToString() << flush;
47 }
48
49 if (filters_.empty()) {
50 XTP_LOG(Log::error, *log_) << "WARNING: No tracker is used " << flush;
51 } else {
52 for (const auto& filter : filters_) {
53 filter->Info(*log_);
54 }
55 }
56}
57
59 std::vector<Index>& vec1, std::vector<Index>& vec2) const {
60 std::vector<Index> result(std::min(vec1, vec2));
61 std::sort(vec1.begin(), vec1.end());
62 std::sort(vec2.begin(), vec2.end());
63 std::vector<Index>::iterator it = std::set_intersection(
64 vec1.begin(), vec1.end(), vec2.begin(), vec2.end(), result.begin());
65 result.resize(it - result.begin());
66 return result;
67}
68
70 std::vector<std::vector<Index>>& results) const {
71 if (results.empty()) {
72 return std::vector<Index>(0);
73 } else {
74 std::vector<Index> result = results[0];
75 for (Index i = 1; i < Index(results.size()); i++) {
76 result = ComparePairofVectors(result, results[i]);
77 }
78 return result;
79 }
80}
81
83
84 if (filters_.empty()) {
85 return statehist_[0];
86 }
87
88 std::vector<std::vector<Index>> results;
89 for (const auto& filter : filters_) {
90 if (statehist_.size() < 2 && filter->NeedsInitialState()) {
92 << "Filter " << filter->Identify()
93 << " not used in first iteration as it needs a reference state"
94 << flush;
95 continue;
96 }
97 results.push_back(filter->CalcIndeces(orbitals, statehist_[0].Type()));
98 }
99
100 std::vector<Index> result = CollapseResults(results);
101 QMState state;
102 if (result.size() < 1) {
103 state = statehist_.back();
105 << "No State found by tracker using last state: " << state.ToString()
106 << flush;
107 } else {
108 state = QMState(statehist_.back().Type(), result[0], false);
110 << "Next State is: " << state.ToString() << flush;
111 }
112 return state;
113}
114
116 QMState result = CalcState(orbitals);
117 statehist_.push_back(result);
118 for (auto& filter : filters_) {
119 filter->UpdateHist(orbitals, result);
120 }
121 return result;
122}
123
125 std::vector<std::string> statehiststring;
126 statehiststring.reserve(statehist_.size());
127 for (const QMState& s : statehist_) {
128 statehiststring.push_back(s.ToString());
129 }
130 w(statehiststring, "statehist");
131
132 for (const auto& filter : filters_) {
133 CheckpointWriter ww = w.openChild(filter->Identify());
134 filter->WriteToCpt(ww);
135 }
136}
137
139 std::vector<std::string> statehiststring;
140 r(statehiststring, "statehist");
141 statehist_.clear();
142 statehist_.reserve(statehiststring.size());
143 for (const std::string& s : statehiststring) {
144 statehist_.push_back(QMState(s));
145 }
146 filters_.clear();
147 FilterFactory filter;
148 for (const std::string& filtername : r.getChildGroupNames()) {
149 CheckpointReader rr = r.openChild(filtername);
150 filters_.push_back(filter.Create(filtername));
151 filters_.back()->ReadFromCpt(rr);
152 }
153}
154
155} // namespace xtp
156} // namespace votca
virtual std::unique_ptr< T > Create(const key_t &key, args_t &&...arguments)
class to manage program options with xml serialization functionality
Definition property.h:55
Property & get(const std::string &key)
get existing property
Definition property.cc:79
std::vector< std::string > getChildGroupNames() const
CheckpointReader openChild(const std::string &childName) const
CheckpointWriter openChild(const std::string &childName) const
container for molecular orbitals
Definition orbitals.h:46
Identifier for QMstates. Strings like S1 are converted into enum +zero indexed int.
Definition qmstate.h:132
std::string ToString() const
Definition qmstate.cc:146
void Initialize(const tools::Property &options)
std::vector< std::unique_ptr< StateFilter_base > > filters_
QMState CalcStateAndUpdate(const Orbitals &orbitals)
void ReadFromCpt(CheckpointReader &r)
void WriteToCpt(CheckpointWriter &w) const
QMState CalcState(const Orbitals &orbitals) const
std::vector< Index > ComparePairofVectors(std::vector< Index > &vec1, std::vector< Index > &vec2) const
std::vector< QMState > statehist_
std::vector< Index > CollapseResults(std::vector< std::vector< Index > > &results) const
#define XTP_LOG(level, log)
Definition logger.h:40
base class for all analysis tools
Definition basebead.h:33
Eigen::Index Index
Definition types.h:26