NuriKit v0.1.0b2
Loading...
Searching...
No Matches
element.h
Go to the documentation of this file.
1//
2// Project NuriKit - Copyright 2023 SNU Compbio Lab.
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#ifndef NURI_CORE_ELEMENT_H_
7#define NURI_CORE_ELEMENT_H_
8
10#include <algorithm>
11#include <cstdint>
12#include <string>
13#include <string_view>
14#include <vector>
15
16#include <absl/container/flat_hash_map.h>
18
19#include "nuri/utils.h"
20
21namespace nuri {
22namespace internal {
23 // Value taken from https://physics.nist.gov/cgi-bin/cuu/Value?are
24 constexpr double kElectronMass = 5.48579909065e-4;
25} // namespace internal
26
33
34constexpr bool operator==(const Isotope &lhs, const Isotope &rhs) noexcept {
35 return &lhs == &rhs;
36}
37
38constexpr bool operator!=(const Isotope &lhs, const Isotope &rhs) noexcept {
39 return &lhs != &rhs;
40}
41
157class Element {
158public:
159 enum class Type : std::uint8_t {
164 };
165
166 enum class State : std::uint8_t {
171 };
172
173 Element() = delete;
174 ~Element() noexcept = default;
175
180 constexpr int atomic_number() const noexcept { return atomic_number_; }
181
186 constexpr std::int16_t valence_electrons() const noexcept {
187 return valence_electrons_;
188 }
189
194 constexpr std::int16_t period() const noexcept { return period_; }
195
201 constexpr std::int16_t group() const noexcept { return group_; }
202
208 constexpr bool radioactive() const noexcept {
209 return internal::check_flag(flags_, ElementFlags::kRadioactive);
210 }
211
216 constexpr bool main_group() const noexcept {
217 return internal::check_flag(flags_, ElementFlags::kMainGroup);
218 }
219
224 constexpr bool lanthanide() const noexcept {
225 return internal::check_flag(flags_, ElementFlags::kLanthanide);
226 }
227
232 constexpr bool actinide() const noexcept {
233 return internal::check_flag(flags_, ElementFlags::kActinide);
234 }
235
240 constexpr Type type() const noexcept { return type_; }
241
246 constexpr State state() const noexcept { return state_; }
247
252 constexpr std::string_view symbol() const noexcept { return symbol_; }
253
258 constexpr std::string_view name() const noexcept { return name_; }
259
265 constexpr double atomic_weight() const noexcept { return atomic_weight_; }
266
272 constexpr double covalent_radius() const noexcept { return cov_rad_; }
273
279 constexpr double vdw_radius() const noexcept { return vdw_rad_; }
280
287 constexpr double eneg() const noexcept { return eneg_; }
288
296 constexpr const Isotope &major_isotope() const noexcept {
297 return *major_isotope_;
298 }
299
305 const Isotope *find_isotope(int mass_number) const noexcept {
306 auto it = std::find_if(isotopes_.begin(), isotopes_.end(),
307 [mass_number](const Isotope &iso) {
308 return iso.mass_number == mass_number;
309 });
310 if (it == isotopes_.end()) {
311 return nullptr;
312 }
313 return &(*it);
314 }
315
321 constexpr const std::vector<Isotope> &isotopes() const noexcept {
322 return isotopes_;
323 }
324
325private:
326 enum class ElementFlags : std::uint16_t {
327 kRadioactive = 0x1,
328 kMainGroup = 0x2,
329 kLanthanide = 0x4,
330 kActinide = 0x8,
331 };
332
333 Element(int atomic_number, std::string_view symbol, std::string_view name,
334 double atomic_weight, double cov_rad, double vdw_rad, double eneg,
335 std::vector<Isotope> &&isotopes) noexcept;
336
337 Element(const Element &) = default;
338 Element(Element &&) noexcept = default;
339 Element &operator=(const Element &) = default;
340 Element &operator=(Element &&) = default;
341
342 friend class PeriodicTable;
343
344 int atomic_number_;
345 std::int16_t valence_electrons_;
346 std::int16_t period_;
347 std::int16_t group_;
348 ElementFlags flags_;
349 Type type_;
350 State state_;
351 std::string_view symbol_;
352 std::string_view name_;
353 double atomic_weight_;
354 double cov_rad_;
355 double vdw_rad_;
356 double eneg_;
357 const Isotope *major_isotope_;
358 std::vector<Isotope> isotopes_;
359};
360
361constexpr bool operator==(const Element &lhs, const Element &rhs) noexcept {
362 return lhs.atomic_number() == rhs.atomic_number();
363}
364
365constexpr bool operator!=(const Element &lhs, const Element &rhs) noexcept {
366 return lhs.atomic_number() != rhs.atomic_number();
367}
368
374class PeriodicTable final {
375public:
376 PeriodicTable(const PeriodicTable &) = delete;
377 PeriodicTable(PeriodicTable &&) noexcept = delete;
378 PeriodicTable &operator=(const PeriodicTable &) = delete;
379 PeriodicTable &operator=(PeriodicTable &&) noexcept = delete;
380
381 ~PeriodicTable() noexcept = default;
382
387 static const PeriodicTable &get() noexcept;
388
397 constexpr const Element &operator[](int atomic_number) const noexcept {
398 return elements_[atomic_number];
399 }
400
408 constexpr const Element *find_element(int atomic_number) const noexcept {
409 return has_element(atomic_number) ? &elements_[atomic_number] : nullptr;
410 }
411
421 const Element *find_element(std::string_view symbol) const noexcept;
422
432 const Element *find_element_of_name(std::string_view name) const noexcept;
433
441 constexpr static bool has_element(int atomic_number) noexcept {
442 return static_cast<unsigned int>(atomic_number)
443 < static_cast<unsigned int>(kElementCount_);
444 }
445
455 bool has_element(std::string_view symbol) const noexcept;
456
466 bool has_element_of_name(std::string_view name) const noexcept;
467
468 const Element *begin() const noexcept { return elements_; }
469 const Element *end() const noexcept { return elements_ + kElementCount_; }
470
471 // 118 elements + dummy
472 // NOLINTNEXTLINE(readability-identifier-naming)
473 constexpr static int kElementCount_ = 118 + 1;
474
475private:
476 PeriodicTable() noexcept;
477
478 Element elements_[kElementCount_];
479 std::string symb_name_buf_;
480 absl::flat_hash_map<std::string_view, const Element *> symbol_to_element_;
481 absl::flat_hash_map<std::string_view, const Element *> name_to_element_;
482};
483
484// NOLINTNEXTLINE(readability-identifier-naming)
485static const PeriodicTable &kPt = PeriodicTable::get();
486} // namespace nuri
487
488#endif /* NURI_CORE_ELEMENT_H_ */
The class for element data.
Definition element.h:157
constexpr std::string_view name() const noexcept
Get the IUPAC Name of the atom.
Definition element.h:258
constexpr double eneg() const noexcept
Get electronegativity of the element (Pauling scale).
Definition element.h:287
friend class PeriodicTable
Definition element.h:342
~Element() noexcept=default
constexpr const std::vector< Isotope > & isotopes() const noexcept
Get all known isotopes of the element.
Definition element.h:321
constexpr bool main_group() const noexcept
Test if the molecule is a main group element.
Definition element.h:216
constexpr double covalent_radius() const noexcept
Get the covalent radius of the atom.
Definition element.h:272
Type
Definition element.h:159
@ kUnknown
Definition element.h:160
@ kMetalloid
Definition element.h:162
@ kMetal
Definition element.h:161
@ kNonmetal
Definition element.h:163
constexpr std::string_view symbol() const noexcept
Get the IUPAC Symbol of the atom.
Definition element.h:252
constexpr bool radioactive() const noexcept
Test if the molecule is radioactive. That is, all of its isotopes has natural abundance of 0.
Definition element.h:208
constexpr Type type() const noexcept
Get the type of the element.
Definition element.h:240
Element()=delete
constexpr bool actinide() const noexcept
Test if the molecule is an actinide.
Definition element.h:232
const Isotope * find_isotope(int mass_number) const noexcept
Find an element with the given mass number.
Definition element.h:305
State
Definition element.h:166
@ kLiquid
Definition element.h:169
@ kSolid
Definition element.h:168
@ kGas
Definition element.h:170
constexpr bool lanthanide() const noexcept
Test if the molecule is a lanthanide.
Definition element.h:224
constexpr std::int16_t group() const noexcept
Get the group of the element.
Definition element.h:201
constexpr const Isotope & major_isotope() const noexcept
Get the representative isotope of the element.
Definition element.h:296
constexpr int atomic_number() const noexcept
Get the atomic number of the atom.
Definition element.h:180
constexpr double atomic_weight() const noexcept
Get the atomic weight of the atom.
Definition element.h:265
constexpr double vdw_radius() const noexcept
Get the Van der Waals radius of the atom.
Definition element.h:279
constexpr std::int16_t period() const noexcept
Get the period of the atom.
Definition element.h:194
constexpr State state() const noexcept
Get the standard state of the element.
Definition element.h:246
constexpr std::int16_t valence_electrons() const noexcept
Get the number of valence electrons of the atom.
Definition element.h:186
bool has_element(std::string_view symbol) const noexcept
Check if the periodic table has an element with the given atomic symbol.
const Element * find_element_of_name(std::string_view name) const noexcept
Find element with the given name.
PeriodicTable(const PeriodicTable &)=delete
static constexpr bool has_element(int atomic_number) noexcept
Check if the periodic table has an element with the given atomic symbol.
Definition element.h:441
static constexpr int kElementCount_
Definition element.h:473
static const PeriodicTable & get() noexcept
Get the singleton instance of the periodic table.
PeriodicTable(PeriodicTable &&) noexcept=delete
constexpr const Element * find_element(int atomic_number) const noexcept
Find element with the given atomic number.
Definition element.h:408
const Element * find_element(std::string_view symbol) const noexcept
Find element with the given atomic symbol.
const Element * end() const noexcept
Definition element.h:469
bool has_element_of_name(std::string_view name) const noexcept
Check if the periodic table has an element with the given atomic name.
const Element * begin() const noexcept
Definition element.h:468
Definition crdgen.h:16
constexpr bool operator==(const Isotope &lhs, const Isotope &rhs) noexcept
Definition element.h:34
constexpr bool operator!=(const Isotope &lhs, const Isotope &rhs) noexcept
Definition element.h:38
Definition element.h:27
int atomic_number
Definition element.h:28
double atomic_weight
Definition element.h:30
int mass_number
Definition element.h:29
double abundance
Definition element.h:31