votca 2024.2-dev
Loading...
Searching...
No Matches
vaverage.cc
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// Local VOTCA includes
21#include "votca/xtp/topology.h"
22#include <votca/tools/getline.h>
23
24// Local private VOTCA includes
25#include "vaverage.h"
26
27namespace votca {
28namespace xtp {
29
31
32 ratefile_ = options.get(".ratefile").as<std::string>();
33 occfile_ = options.get(".occfile").as<std::string>();
34 outputfile_ = options.get(".output").as<std::string>();
35}
36
37std::vector<double> VAverage::ReadOccfile(std::string filename) const {
38 std::vector<double> result;
39 std::ifstream intt;
40 intt.open(filename);
41 if (!intt.is_open()) {
42 throw std::runtime_error("File:" + filename + " could not be opened");
43 }
44 std::string line;
45 Index id = 0;
46 while (intt.good()) {
47
48 tools::getline(intt, line);
49 std::vector<std::string> split = tools::Tokenizer(line, " \t").ToVector();
50 if (!split.size() || split[0] == "#" || split[0].substr(0, 1) == "#") {
51 continue;
52 }
53 if (split.size() != 2) {
54 throw std::runtime_error("Row should only contain id and occupation");
55 }
56
57 Index id_readin = std::stoi(split[0]);
58 if (id_readin != id) {
59 throw std::runtime_error("Ids should be sorted and start at zero.");
60 }
61 id++;
62 result.push_back(std::stod(split[1]));
63 }
64 return result;
65}
66
67std::vector<Rate_Engine::PairRates> VAverage::ReadRatefile(
68 std::string filename) const {
69 std::vector<Rate_Engine::PairRates> result;
70 std::ifstream intt;
71 intt.open(filename);
72 if (!intt.is_open()) {
73 throw std::runtime_error("File:" + filename + " could not be opened");
74 }
75 Index id = 0;
76 std::string line;
77 while (intt.good()) {
78
79 tools::getline(intt, line);
80 std::vector<std::string> split = tools::Tokenizer(line, " \t").ToVector();
81 if (!split.size() || split[0] == "#" || split[0].substr(0, 1) == "#") {
82 continue;
83 }
84 if (split.size() != 5) {
85 throw std::runtime_error(
86 "Row should only contain pairid,segid1,segid2 ,rate12,rate21");
87 }
88
89 Index id_readin = std::stoi(split[0]);
90 if (id_readin != id) {
91 throw std::runtime_error("Ids should be sorted and start at zero.");
92 }
93 id++;
95 pair.rate12 = std::stod(split[3]);
96 pair.rate21 = std::stod(split[4]);
97 result.push_back(pair);
98 }
99 return result;
100}
101
103 std::cout << std::endl
104 << "... ... Computing velocity average for all sites\n";
105 std::cout << "Reading in site occupations from " << occfile_ << std::endl;
106 std::vector<double> occ = ReadOccfile(occfile_);
107 if (top.Segments().size() != occ.size()) {
108 throw std::runtime_error(
109 "Number of occupations is" + std::to_string(occ.size()) +
110 " Topology has size:" + std::to_string(top.Segments().size()));
111 }
112 std::cout << "Reading in rates from " << ratefile_ << std::endl;
113 std::vector<Rate_Engine::PairRates> rates = ReadRatefile(ratefile_);
114 if (top.NBList().size() != Index(rates.size())) {
115 throw std::runtime_error(
116 "Number of pairs in file is" + std::to_string(rates.size()) +
117 " Topology has size:" + std::to_string(top.NBList().size()));
118 }
119 std::vector<Eigen::Vector3d> velocities =
120 std::vector<Eigen::Vector3d>(occ.size(), Eigen::Vector3d::Zero());
121 for (const QMPair* pair : top.NBList()) {
122 Index id1 = pair->Seg1()->getId();
123 Index id2 = pair->Seg1()->getId();
124 Index pairid = pair->getId();
125 double p1 = occ[id1];
126 double p2 = occ[id2];
127 double w12 = rates[pairid].rate12;
128 double w21 = rates[pairid].rate21;
129 const Eigen::Vector3d& dR12 =
130 pair->R(); // points from seg. (1) to seg. (2)
131 // Split current symmetrically among sites (1) and (2)
132 Eigen::Vector3d v_12_21 = 0.5 * (p1 * w12 - p2 * w21) * dR12;
133 velocities[id1] += v_12_21;
134 velocities[id2] += v_12_21;
135 }
136
137 std::ofstream ofs;
138 ofs.open(outputfile_, std::ofstream::out);
139 if (!ofs.is_open()) {
140 throw std::runtime_error("Bad file handle: " + outputfile_);
141 }
142 ofs << "# 1 | 2 | 3 4 5 | 6 7 8 |" << std::endl;
143 ofs << "# ----|---------|--------------|-------------------|" << std::endl;
144 ofs << "# ID | NAME | X Y Z [nm] | VX VY VZ [nm/s] |" << std::endl;
145 for (const Segment& seg : top.Segments()) {
146 const Eigen::Vector3d r = seg.getPos() * tools::conv::bohr2nm;
147 const Eigen::Vector3d v = velocities[seg.getId()] * tools::conv::bohr2nm;
148 ofs << (boost::format("%1$4d %2$-10s %3$+1.7e %4$+1.7e "
149 "%5$+1.7e %6$+1.7e %7$+1.7e %8$+1.7e") %
150 seg.getId() % seg.getType() % r.x() % r.y() % r.z() % v.x() %
151 v.y() % v.z())
152 << std::endl;
153 }
154 std::cout << "Writing velocities to " << outputfile_ << std::endl;
155 return true;
156}
157
158} // namespace xtp
159} // namespace votca
Index size() const
Definition pairlist.h:53
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
T as() const
return value as type
Definition property.h:283
break string into words
Definition tokenizer.h:72
std::vector< T > ToVector()
store all words in a vector of type T, does type conversion.
Definition tokenizer.h:109
Container for segments and box and atoms.
Definition topology.h:41
std::vector< Segment > & Segments()
Definition topology.h:58
QMNBList & NBList()
Definition topology.h:70
std::string occfile_
Definition vaverage.h:54
bool Evaluate(Topology &top)
Definition vaverage.cc:102
std::string outputfile_
Definition vaverage.h:55
std::string ratefile_
Definition vaverage.h:53
void ParseOptions(const tools::Property &user_options)
Definition vaverage.cc:30
std::vector< double > ReadOccfile(std::string filename) const
Definition vaverage.cc:37
std::vector< Rate_Engine::PairRates > ReadRatefile(std::string filename) const
Definition vaverage.cc:67
const double bohr2nm
Definition constants.h:46
std::istream & getline(std::istream &is, std::string &str)
Wrapper for a getline function.
Definition getline.h:35
base class for all analysis tools
Definition basebead.h:33
Eigen::Index Index
Definition types.h:26