| 1 |
/****************************************************************************** |
| 2 |
This source file is part of the Avogadro project. |
| 3 |
This source code is released under the 3-Clause BSD License, (see "LICENSE"). |
| 4 |
******************************************************************************/ |
| 5 |
|
| 6 |
#include "elements.h" |
| 7 |
|
| 8 |
#include "avogadrocore.h" |
| 9 |
#include "elementdata.h" |
| 10 |
#include "utilities.h" |
| 11 |
|
| 12 |
#include <algorithm> |
| 13 |
#include <cctype> |
| 14 |
#include <vector> |
| 15 |
|
| 16 |
using Avogadro::Core::isCustomElement; |
| 17 |
|
| 18 |
namespace Avogadro::Core { |
| 19 |
|
| 20 |
// Handle custom element identifiers: |
| 21 |
namespace { |
| 22 |
const static std::string CustomElementSymbolPrefix = "X"; |
| 23 |
const static std::string CustomElementNamePrefix = "CustomElement_"; |
| 24 |
|
| 25 |
static std::vector<std::string> CustomElementSymbols; |
| 26 |
static std::vector<std::string> CustomElementNames; |
| 27 |
|
| 28 |
// Match carbon's radii |
| 29 |
static double CustomElementCovalentRadius = element_covalent[6]; |
| 30 |
static double CustomElementVDWRadius = element_VDW[6]; |
| 31 |
|
| 32 |
inline std::string encodeCustomElement(unsigned char atomicNumber) |
| 33 |
{ |
| 34 |
std::string result; |
| 35 |
if (isCustomElement(atomicNumber)) { |
| 36 |
result.resize(2); |
| 37 |
unsigned char index = atomicNumber - Avogadro::CustomElementMin; |
| 38 |
result[0] = 'a' + static_cast<char>(index / 26); |
| 39 |
result[1] = 'a' + static_cast<char>(index % 26); |
| 40 |
} |
| 41 |
return result; |
| 42 |
} |
| 43 |
|
| 44 |
inline unsigned char decodeCustomElement(const std::string& str) |
| 45 |
{ |
| 46 |
if (str.size() == 2) { |
| 47 |
if (str[0] >= 'a' && str[0] <= 'z' && str[1] >= 'a' && str[1] <= 'z') { |
| 48 |
return CustomElementMin + static_cast<unsigned char>(str[0] - 'a') * 26 + |
| 49 |
static_cast<unsigned char>(str[1] - 'a'); |
| 50 |
} |
| 51 |
} |
| 52 |
return Avogadro::InvalidElement; |
| 53 |
} |
| 54 |
|
| 55 |
inline unsigned char interpretCustomElementName(const std::string& name) |
| 56 |
{ |
| 57 |
if (startsWith(name, CustomElementNamePrefix)) { |
| 58 |
const std::string number(name.substr(CustomElementNamePrefix.size())); |
| 59 |
return decodeCustomElement(number); |
| 60 |
} |
| 61 |
return InvalidElement; |
| 62 |
} |
| 63 |
|
| 64 |
inline std::string createCustomElementName(unsigned char atomicNumber) |
| 65 |
{ |
| 66 |
return CustomElementNamePrefix + encodeCustomElement(atomicNumber); |
| 67 |
} |
| 68 |
|
| 69 |
inline const char* customElementName(unsigned char atomicNumber) |
| 70 |
{ |
| 71 |
return CustomElementNames[atomicNumber - CustomElementMin].c_str(); |
| 72 |
} |
| 73 |
|
| 74 |
inline unsigned char interpretCustomElementSymbol(const std::string& symbol) |
| 75 |
{ |
| 76 |
if (symbol.size() == 3) |
| 77 |
return decodeCustomElement(symbol.substr(1)); |
| 78 |
return InvalidElement; |
| 79 |
} |
| 80 |
|
| 81 |
inline std::string createCustomElementSymbol(unsigned char atomicNumber) |
| 82 |
{ |
| 83 |
return CustomElementSymbolPrefix + encodeCustomElement(atomicNumber); |
| 84 |
} |
| 85 |
|
| 86 |
inline const char* customElementSymbol(unsigned char atomicNumber) |
| 87 |
{ |
| 88 |
return CustomElementSymbols[atomicNumber - CustomElementMin].c_str(); |
| 89 |
} |
| 90 |
|
| 91 |
inline const unsigned char* customElementColor(unsigned char atomicNumber) |
| 92 |
{ |
| 93 |
return Core::element_color[atomicNumber % element_count]; |
| 94 |
} |
| 95 |
|
| 96 |
// Initialize the static lookup tables. |
| 97 |
class InitializeCustomElementTables |
| 98 |
{ |
| 99 |
public: |
| 100 |
InitializeCustomElementTables() |
| 101 |
{ |
| 102 |
CustomElementSymbols.resize(CustomElementCount); |
| 103 |
CustomElementNames.resize(CustomElementCount); |
| 104 |
std::string suffix; |
| 105 |
for (unsigned char i = CustomElementMin; i <= CustomElementMax; ++i) { |
| 106 |
suffix = encodeCustomElement(i); |
| 107 |
CustomElementSymbols[i - CustomElementMin] = |
| 108 |
CustomElementSymbolPrefix + suffix; |
| 109 |
CustomElementNames[i - CustomElementMin] = |
| 110 |
CustomElementNamePrefix + suffix; |
| 111 |
} |
| 112 |
} |
| 113 |
} CustomElementTableInitializer; |
| 114 |
|
| 115 |
} // namespace |
| 116 |
|
| 117 |
unsigned char Elements::elementCount() |
| 118 |
{ |
| 119 |
return element_count; |
| 120 |
} |
| 121 |
|
| 122 |
unsigned char Elements::atomicNumberFromName(const std::string& name) |
| 123 |
{ |
| 124 |
for (unsigned char i = 0; i < element_count; ++i) |
| 125 |
if (name == element_names[i]) |
| 126 |
return i; |
| 127 |
|
| 128 |
return interpretCustomElementName(name); |
| 129 |
} |
| 130 |
|
| 131 |
unsigned char Elements::atomicNumberFromSymbol(const std::string& symbol) |
| 132 |
{ |
| 133 |
if (symbol.length() == 1) { |
| 134 |
switch (symbol[0]) { |
| 135 |
case 'H': |
| 136 |
return 1; |
| 137 |
case 'B': |
| 138 |
return 5; |
| 139 |
case 'C': |
| 140 |
return 6; |
| 141 |
case 'N': |
| 142 |
return 7; |
| 143 |
case 'O': |
| 144 |
return 8; |
| 145 |
case 'F': |
| 146 |
return 9; |
| 147 |
case 'P': |
| 148 |
return 15; |
| 149 |
case 'S': |
| 150 |
return 16; |
| 151 |
case 'K': |
| 152 |
return 19; |
| 153 |
case 'V': |
| 154 |
return 23; |
| 155 |
case 'Y': |
| 156 |
return 39; |
| 157 |
case 'I': |
| 158 |
return 53; |
| 159 |
case 'W': |
| 160 |
return 74; |
| 161 |
case 'U': |
| 162 |
return 92; |
| 163 |
default: |
| 164 |
return InvalidElement; |
| 165 |
} |
| 166 |
} else { |
| 167 |
for (unsigned char i = 0; i < element_count; ++i) |
| 168 |
if (symbol == element_symbols[i]) |
| 169 |
return i; |
| 170 |
return interpretCustomElementSymbol(symbol); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
unsigned char Elements::guessAtomicNumber(const std::string& inputStr) |
| 175 |
{ |
| 176 |
std::string str(trimmed(inputStr)); |
| 177 |
if (str.empty()) |
| 178 |
return InvalidElement; |
| 179 |
|
| 180 |
// atomic number? |
| 181 |
bool ok; |
| 182 |
int atomicNumberInt = lexicalCast<int>(str, ok); |
| 183 |
if (ok) |
| 184 |
return static_cast<unsigned char>(atomicNumberInt); |
| 185 |
|
| 186 |
// Format string as text |
| 187 |
std::transform(str.begin(), str.end(), str.begin(), tolower); |
| 188 |
str[0] = static_cast<char>(toupper(static_cast<int>(str[0]))); |
| 189 |
|
| 190 |
int length = str.size(); |
| 191 |
unsigned char atomicNumber = InvalidElement; |
| 192 |
while (length > 0) { |
| 193 |
if (length > 3) |
| 194 |
atomicNumber = atomicNumberFromName(str.substr(0, length)); |
| 195 |
else |
| 196 |
atomicNumber = atomicNumberFromSymbol(str.substr(0, length)); |
| 197 |
if (atomicNumber != InvalidElement) |
| 198 |
break; |
| 199 |
length--; |
| 200 |
} |
| 201 |
|
| 202 |
return atomicNumber; |
| 203 |
} |
| 204 |
|
| 205 |
const char* Elements::name(unsigned char atomicNumber) |
| 206 |
{ |
| 207 |
if (atomicNumber < element_count) |
| 208 |
return element_names[atomicNumber]; |
| 209 |
else if (isCustomElement(atomicNumber)) |
| 210 |
return customElementName(atomicNumber); |
| 211 |
else |
| 212 |
return element_names[0]; |
| 213 |
} |
| 214 |
|
| 215 |
const char* Elements::symbol(unsigned char atomicNumber) |
| 216 |
{ |
| 217 |
if (atomicNumber < element_count) |
| 218 |
return element_symbols[atomicNumber]; |
| 219 |
else if (isCustomElement(atomicNumber)) |
| 220 |
return customElementSymbol(atomicNumber); |
| 221 |
else |
| 222 |
return element_symbols[0]; |
| 223 |
} |
| 224 |
|
| 225 |
double Elements::mass(unsigned char atomicNumber) |
| 226 |
{ |
| 227 |
if (atomicNumber < element_count) |
| 228 |
return element_masses[atomicNumber]; |
| 229 |
else |
| 230 |
return element_masses[0]; |
| 231 |
} |
| 232 |
|
| 233 |
double Elements::radiusVDW(unsigned char atomicNumber) |
| 234 |
{ |
| 235 |
if (atomicNumber < element_count) |
| 236 |
return element_VDW[atomicNumber]; |
| 237 |
else if (isCustomElement(atomicNumber)) |
| 238 |
return CustomElementVDWRadius; |
| 239 |
else |
| 240 |
return element_VDW[0]; |
| 241 |
} |
| 242 |
|
| 243 |
double Elements::radiusCovalent(unsigned char atomicNumber) |
| 244 |
{ |
| 245 |
if (atomicNumber < element_count) |
| 246 |
return element_covalent[atomicNumber]; |
| 247 |
else if (isCustomElement(atomicNumber)) |
| 248 |
return CustomElementCovalentRadius; |
| 249 |
else |
| 250 |
return element_covalent[0]; |
| 251 |
} |
| 252 |
|
| 253 |
const unsigned char* Elements::color(unsigned char atomicNumber) |
| 254 |
{ |
| 255 |
if (atomicNumber < element_count) |
| 256 |
return element_color[atomicNumber]; |
| 257 |
else if (isCustomElement(atomicNumber)) |
| 258 |
return customElementColor(atomicNumber); |
| 259 |
else |
| 260 |
return element_color[0]; |
| 261 |
} |
| 262 |
|
| 263 |
unsigned char Elements::valenceElectrons(unsigned char atomicNumber) |
| 264 |
{ |
| 265 |
if (atomicNumber < element_count) |
| 266 |
return valence_electrons[atomicNumber]; |
| 267 |
else |
| 268 |
return valence_electrons[0]; |
| 269 |
} |
| 270 |
|
| 271 |
double Elements::isotopeMass(unsigned char atomicNumber, unsigned short isotope) |
| 272 |
{ |
| 273 |
if (atomicNumber >= element_count) |
| 274 |
return 0.0; |
| 275 |
|
| 276 |
// also handles element zero |
| 277 |
const auto& list = isotopes[atomicNumber]; |
| 278 |
// if there's only one isotope, return its mass |
| 279 |
if (list.size() == 1) |
| 280 |
return list.begin()->second; |
| 281 |
|
| 282 |
// find the isotope |
| 283 |
for (auto&& [i, mass] : list) { |
| 284 |
if (i == isotope) |
| 285 |
return mass; |
| 286 |
} |
| 287 |
|
| 288 |
return 0.0; |
| 289 |
} |
| 290 |
|
| 291 |
} // namespace Avogadro::Core |