Octopus
element.hpp
Go to the documentation of this file.
1#ifndef PSEUDO_ELEMENT_HPP
2#define PSEUDO_ELEMENT_HPP
3
4/*
5 Copyright (C) 2018 Xavier Andrade
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20*/
21
22#include <cassert>
23#include <cctype>
24#include <cstdlib>
25#include <fstream>
26#include <iostream>
27#include <map>
28#include <string>
29
30#include "share_directory.hpp"
31
32namespace pseudopotential {
33
34class element {
35
36private:
37 struct properties;
38 typedef std::map<std::string, properties> map_type;
39
40public:
41 element(const std::string &symbol = "none") : symbol_(trim(symbol)) {
42 symbol_[0] = std::toupper(symbol_[0]);
43 for (unsigned ii = 1; ii < symbol_.size(); ii++)
44 symbol_[ii] = std::tolower(symbol_[ii]);
45
46 map(); // make sure the map is initialized
47 }
48
49 element(int atomic_number) {
50
51 // special case: avoid ambiguity between isotopes
52 if (atomic_number == 1) {
53 symbol_ = 'H';
54 return;
55 }
56
57 for (map_type::iterator it = map().begin(); it != map().end(); ++it) {
58 if (it->second.z_ == atomic_number) {
59 symbol_ = trim(it->first);
60 break;
61 }
62 }
63 }
64
65 bool valid() const { return map().find(symbol_) != map().end(); }
66
67 double charge() const { return -1.0 * atomic_number(); }
68
69 const std::string &symbol() const { return symbol_; }
70
71 int atomic_number() const { return map().at(symbol_).z_; }
72
73 double mass() const { return map().at(symbol_).mass_; }
74
75 double vdw_radius() const { return map().at(symbol_).vdw_radius_; }
76
77private:
78 struct properties {
79 int z_;
80 double mass_;
81 double vdw_radius_;
82 };
83
84 static map_type &map() {
85
86 static map_type map;
87 if (map.empty()) {
88
89 std::string filename = pseudopotential::share_directory::get() +
90 "/pseudopotentials/elements.dat";
91
92 std::ifstream file(filename.c_str());
93
94 if (!file) {
95 std::cerr << "Internal error: cannot open file '" << filename << "'."
96 << std::endl;
97 exit(EXIT_FAILURE);
98 }
99
100 while (true) {
101 std::string symbol;
102
103 file >> symbol;
104
105 if (file.eof())
106 break;
107
108 if (symbol[0] == '#') {
109 getline(file, symbol);
110 continue;
111 }
112
113 properties prop;
114
115 file >> prop.z_ >> prop.mass_ >> prop.vdw_radius_;
116
117 if (file.eof())
118 break;
119
120 map[symbol] = prop;
121 }
122 }
123
124 return map;
125 }
126
127 static std::string &ltrim(std::string &str,
128 const std::string &chars = "\t\n\v\f\r ") {
129 str.erase(0, str.find_first_not_of(chars));
130 return str;
131 }
132
133 static std::string &rtrim(std::string &str,
134 const std::string &chars = "\t\n\v\f\r ") {
135 str.erase(str.find_last_not_of(chars) + 1);
136 return str;
137 }
138
139public:
140 static std::string trim(std::string str,
141 const std::string &chars = "\t\n\v\f\r ") {
142 return ltrim(rtrim(str, chars), chars);
143 }
144
145private:
146 std::string symbol_;
147};
148
149} // namespace pseudopotential
150
151#endif
152
153// Local Variables:
154// mode: c++
155// coding: utf-8
156// End:
if write to the Free Software Franklin Fifth USA !If the compiler accepts long Fortran it is better to use that and build all the preprocessor definitions in one line In !this the debuggers will provide the right line numbers !If the compiler accepts line number then CARDINAL and ACARDINAL !will put them just a new line or a ampersand plus a new line !These macros should be used in macros that span several lines They should by !put immedialty before a line where a compilation error might occur and at the !end of the macro !Note that the cardinal and newline words are substituted by the program !preprocess pl by the ampersand and by a real new line just before compilation !The assertions are ignored if the code is compiled in not debug when !prints out the assertion the file
Definition: global.h:46
if write to the Free Software Franklin Fifth USA !If the compiler accepts long Fortran it is better to use that and build all the preprocessor definitions in one line In !this the debuggers will provide the right line numbers !If the compiler accepts line number then CARDINAL and ACARDINAL !will put them just a new line or a ampersand plus a new line !These macros should be used in macros that span several lines They should by !put immedialty before a line where a compilation error might occur and at the !end of the macro !Note that the cardinal and newline words are substituted by the program !preprocess pl by the ampersand and by a real new line just before compilation !The assertions are ignored if the code is compiled in not debug when !prints out the assertion string
Definition: global.h:46
type(oct_iterator_t), save iterator
integer, parameter, public mass
Definition: quantity.F90:146
integer, parameter, public charge
Definition: quantity.F90:146