Hex  2.2
Hydrogen-electron collision solver
utils.h
Go to the documentation of this file.
1 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
2 // //
3 // / / / / __ \ \ / / //
4 // / /__ / / / _ \ \ \/ / //
5 // / ___ / | |/_/ / /\ \ //
6 // / / / / \_\ / / \ \ //
7 // //
8 // //
9 // Copyright (c) 2016, Jakub Benda, Charles University in Prague //
10 // //
11 // MIT License: //
12 // //
13 // Permission is hereby granted, free of charge, to any person obtaining a //
14 // copy of this software and associated documentation files (the "Software"), //
15 // to deal in the Software without restriction, including without limitation //
16 // the rights to use, copy, modify, merge, publish, distribute, sublicense, //
17 // and/or sell copies of the Software, and to permit persons to whom the //
18 // Software is furnished to do so, subject to the following conditions: //
19 // //
20 // The above copyright notice and this permission notice shall be included //
21 // in all copies or substantial portions of the Software. //
22 // //
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS //
24 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, //
25 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE //
26 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, //
27 // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF //
28 // OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. //
29 // //
30 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
31 
32 #ifndef HEX_DB_UTIL_H
33 #define HEX_DB_UTIL_H
34 
35 #include <iostream>
36 #include <string>
37 #include <map>
38 #include <vector>
39 
40 #include "hex-arrays.h"
41 #include "hex-vec3d.h"
42 
43 #include "db.h"
44 
51 double change_units (eUnit A, eUnit B);
52 
59 double change_units (lUnit A, lUnit B);
60 
67 double change_units (aUnit A, aUnit B);
68 
74 std::string unit_name (eUnit u);
75 
81 std::string unit_name (lUnit u);
82 
88 std::string unit_name (aUnit u);
89 
93 inline std::ostream & operator << (std::ostream & os, std::pair<geom::vec3d,geom::vec3d> const & p)
94 {
95  os << p.first << " " << p.second;
96  return os;
97 }
98 
102 inline std::istream & operator >> (std::istream & is, std::pair<geom::vec3d,geom::vec3d> & p)
103 {
104  is >> p.first;
105  is >> p.second;
106  return is;
107 }
108 
113 inline std::istream & operator >> (std::istream & is, iArray & p)
114 {
115  std::string token;
116  p.resize(0);
117  while (std::getline(is, token, ','))
118  {
119  std::size_t idx, idx1, idx2;
120  int value = std::stoi(token, &idx);
121 
122  // convert token to integer
123  if (idx == token.size())
124  {
125  p.push_back(value);
126  }
127 
128  // convert token to range
129  else
130  {
131  std::size_t pos = token.find('-', 1);
132  if (pos == std::string::npos)
133  HexException("Failed to parse \"%s\"", token.c_str());
134 
135  std::string str_a = token.substr(0, pos);
136  std::string str_b = token.substr(pos + 1);
137  int a = std::stoi(str_a, &idx1);
138  int b = std::stoi(str_b, &idx2);
139  if (idx1 != str_a.size() or idx2 != str_b.size())
140  HexException("Failed to parse \"%s\"", token.c_str());
141 
142  iArray values = linspace(a, b, b - a + 1);
143  p.append(values.begin(), values.end());
144  }
145  }
146  return is;
147 }
148 
152 template<typename T> std::vector<T> readStandardInput ()
153 {
154  std::vector<T> data;
155 
156  T x;
157  while (not std::cin.eof())
158  {
159  std::cin >> std::ws;
160  std::cin >> x;
161  std::cin >> std::ws;
162  data.push_back(x);
163  }
164 
165  return data;
166 }
167 
179 template <typename T> T Conv
180 (
181  std::map<std::string,std::string> const & dict,
182  std::string const & keyword,
183  std::string const & name
184 )
185 {
186  // check existence of the keyword
187  std::map<std::string,std::string>::const_iterator it = dict.find(keyword);
188  if (it == dict.end())
189  throw exception ("ERROR: \"%s\" requires specifying the parameter \"--%s\"!\n", name.c_str(), keyword.c_str());
190 
191  // convert to int
192  T x;
193  std::istringstream ss(it->second);
194  ss >> x;
195  return x;
196 }
197 
198 #endif
std::istream & operator>>(std::istream &is, std::pair< geom::vec3d, geom::vec3d > &p)
Definition: utils.h:102
std::vector< T > readStandardInput()
Definition: utils.h:152
aUnit
Definition: db.h:59
std::string unit_name(eUnit u)
Energy unit name.
Definition: utils.cpp:101
eUnit
Energy units.
Definition: db.h:44
T Conv(std::map< std::string, std::string > const &dict, std::string const &keyword, std::string const &name)
Convert dictionary entry to a numeric type.
Definition: utils.h:180
lUnit
Output (length) units.
Definition: db.h:52
double change_units(eUnit A, eUnit B)
Energy units change.
Definition: utils.cpp:40