SERiF 0.0.1a
3+1D Stellar Structure and Evolution
Loading...
Searching...
No Matches
const.cpp
Go to the documentation of this file.
1/* ***********************************************************************
2//
3// Copyright (C) 2025 -- The 4D-STAR Collaboration
4// File Author: Emily Boudreaux
5// Last Modified: March 17, 2025
6//
7// 4DSSE is free software; you can use it and/or modify
8// it under the terms and restrictions the GNU General Library Public
9// License version 3 (GPLv3) as published by the Free Software Foundation.
10//
11// 4DSSE is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14// See the GNU Library General Public License for more details.
15//
16// You should have received a copy of the GNU Library General Public License
17// along with this software; if not, write to the Free Software
18// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19//
20// *********************************************************************** */
21#include <iostream>
22#include <map>
23#include <vector>
24#include <string>
25#include <fstream>
26#include <sstream>
27#include <set>
28
29
30#include "const.h"
31#include "embedded_constants.h" // Generated at build time by meson
32
33namespace serif::constant {
37
39 return load();
40 }
41
42 Constant Constants::get(const std::string& name) const {
43 auto it = constants_.find(name);
44 if (it != constants_.end()) {
45 return it->second;
46 } else {
47 throw std::out_of_range("Constant '" + name + "' not found.");
48 }
49 }
50
51 Constant Constants::operator[](const std::string& name) const {
52 return this->get(name);
53 }
54
55 bool Constants::has(const std::string& name) const {
56 return constants_.find(name) != constants_.end();
57 }
58
59 std::set<std::string> Constants::keys() const {
60 std::set<std::string> keys;
61 for (const auto& pair : constants_) {
62 keys.insert(pair.first);
63 }
64 return keys;
65 }
66
67 std::string Constants::trim(const std::string& str) {
68 size_t first = str.find_first_not_of(" \t");
69 if (first == std::string::npos) return "";
70 size_t last = str.find_last_not_of(" \t");
71 return str.substr(first, last - first + 1);
72 }
73
75 std::istringstream fileStream(embeddedConstants);
76
77 std::string line;
78 bool data_section = false;
79 int line_count = 0;
80
81 while (std::getline(fileStream, line)) {
82 line_count++;
83
84 // Detect start of data section (double divider line)
85 if (!data_section) {
86 if (line.find("Symbol") != std::string::npos) { // Find header row
87 std::getline(fileStream, line); // Skip dashed divider
88 std::getline(fileStream, line); // Skip second dashed divider
89 data_section = true;
90 }
91 continue;
92 }
93
94 // Ensure the line is long enough to contain all fields
95 if (line.length() < 125) continue;
96
97 // Define exact column widths from Python script
98 int start = 0;
99
100 const std::string symbol = trim(line.substr(start, col_widths_[0])); start += col_widths_[0];
101 const std::string name = trim(line.substr(start, col_widths_[1])); start += col_widths_[1];
102 const std::string valueStr = line.substr(start, col_widths_[2]); start += col_widths_[2];
103 const std::string unit = trim(line.substr(start, col_widths_[3])); start += col_widths_[3]; // Only trim the unit
104 const std::string uncertaintyStr = line.substr(start, col_widths_[4]); start += col_widths_[4];
105 const std::string reference = trim(line.substr(start, col_widths_[5])); // Only trim reference
106
107 // Convert numerical fields safely
108 double value = 0.0, uncertainty = 0.0;
109 try {
110 value = std::stod(valueStr);
111 } catch (...) {
112 std::cerr << "Warning: Invalid value in line " << line_count << ": " << valueStr << std::endl;
113 }
114 try {
115 uncertainty = std::stod(uncertaintyStr);
116 } catch (...) {
117 std::cerr << "Warning: Invalid uncertainty in line " << line_count << ": " << uncertaintyStr << std::endl;
118 }
119
120 // Store in map
121 constants_.emplace(symbol, Constant{name, value, uncertainty, unit, reference});
122 }
123 loaded_ = true;
124 return true;
125 }
126}
Constant operator[](const std::string &key) const
Overloaded subscript operator to access constants by key.
Definition const.cpp:51
std::map< std::string, Constant > constants_
Map to store constants by name.
Definition const.h:67
bool has(const std::string &key) const
Check if a constant exists by key.
Definition const.cpp:55
std::set< std::string > keys() const
Get a list of all constant keys.
Definition const.cpp:59
bool loaded_
Flag to indicate if constants are loaded.
Definition const.h:65
bool initialize()
Initialize constants.
Definition const.cpp:38
bool load()
Load constants from the embedded header file.
Definition const.cpp:74
const int col_widths_[6]
Definition const.h:66
Constants()
Default constructor. Private to avoid direct instantiation.
Definition const.cpp:34
Constant get(const std::string &key) const
Get a constant by key.
Definition const.cpp:42
std::string trim(const std::string &str)
Trim leading and trailing whitespace from a string.
Definition const.cpp:67
Structure to hold a constant's details.
Definition const.h:31