Phyth v0.1.0
A dimensional safe C++ header-only mechanics and electromagnetics simulation library.
Loading...
Searching...
No Matches
Materials.hpp
Go to the documentation of this file.
1#ifndef PHYTH_MATERIALS_HPP
2#define PHYTH_MATERIALS_HPP
3
6
7#include <unordered_map>
8#include <string>
9#include <vector>
10#include <fstream>
11
13 namespace detail {
18 public:
29
40 static std::unordered_map<std::string, MaterialData> Parse(const std::string &filename) {
41 std::ifstream file(filename);
42 if (!file.is_open()) {
43 throw std::runtime_error("Cannot open material file: " + filename);
44 }
45
46 std::unordered_map<std::string, MaterialData> result;
47 std::string line, current_section;
48 MaterialData current_data{.youngs_modulus = 0_Pa, .poisson_ratio = 0_};
49
50 while (std::getline(file, line)) {
51 trim(line);
52
53 if (line.empty() || line[0] == '#' || line[0] == ';') {
54 continue;
55 }
56
57 if (line[0] == '[') {
58 if (!current_section.empty()) {
60 }
61
62 auto end = line.find(']');
63 if (end == std::string::npos) {
64 throw std::runtime_error("Invalid section header: " + line);
65 }
66 current_section = line.substr(1, end - 1);
68 current_data = MaterialData{.youngs_modulus = 0.0, .poisson_ratio = 0.0};
69 continue;
70 }
71
72 if (auto eq_pos = line.find('='); eq_pos != std::string::npos) {
73 std::string key = line.substr(0, eq_pos);
74 std::string value = line.substr(eq_pos + 1);
75 trim(key);
76 trim(value);
77
78 if (auto comment_pos = value.find_first_of("#;"); comment_pos != std::string::npos) {
79 value = value.substr(0, comment_pos);
80 trim(value);
81 }
82
83 if (key == "youngs_modulus" || key == "E") {
84 current_data.youngs_modulus = parseNumber(value) * 1_Pa;
85 } else if (key == "poisson_ratio" || key == "nu") {
86 current_data.poisson_ratio = parseNumber(value) * 1_;
87 }
88 }
89 }
90
91 if (!current_section.empty()) {
93 }
94
95 return result;
96 }
97
98 private:
105 static void trim(std::string &str) {
106 const auto start = str.find_first_not_of(" \t\r\n");
107 if (start == std::string::npos) {
108 str.clear();
109 return;
110 }
111 const auto end = str.find_last_not_of(" \t\r\n");
112 str = str.substr(start, end - start + 1);
113 }
114
125 static double parseNumber(const std::string &str) {
126 try {
127 // Scientific notation
128 return std::stod(str);
129 } catch (const std::exception &) {
130 throw std::runtime_error("Invalid number format: " + str);
131 }
132 }
133 };
134 }
135
146 public:
162 [[nodiscard]] IsotropicMaterial Get(const std::string &name) const {
163 const auto it = materials_.find(name);
164 if (it == materials_.end()) {
165 throw std::runtime_error("Material not found: " + name);
166 }
167 return it->second;
168 }
169
183 [[nodiscard]] std::vector<std::string> ListMaterials() const {
184 std::vector<std::string> names;
185 for (const auto &[name, _]: materials_) {
186 names.push_back(name);
187 }
188 return names;
189 }
190
204 void RegisterMaterial(const std::string &name, const IsotropicMaterial &material) {
205 materials_[name] = material;
206 }
207
239 [[nodiscard]] static MaterialDatabase FromINI(const std::string &path) {
241 db.materials_ = {};
243 for (const auto &[category, material_data] : materials) {
244 db.RegisterMaterial(category, IsotropicMaterial(material_data.youngs_modulus, material_data.poisson_ratio));
245 }
246 return db;
247 }
248
249 private:
250 std::unordered_map<std::string, IsotropicMaterial> materials_;
251 };
252}
253
254#endif //PHYTH_MATERIALS_HPP
Linear elastic isotropic material model.
Database for managing and retrieving material properties.
std::vector< std::string > ListMaterials() const
Get a list of all registered material names.
IsotropicMaterial Get(const std::string &name) const
Retrieve a material by its registered name.
void RegisterMaterial(const std::string &name, const IsotropicMaterial &material)
Register a new material in the database.
static MaterialDatabase FromINI(const std::string &path)
Create a MaterialDatabase from an INI configuration file.
std::unordered_map< std::string, IsotropicMaterial > materials_
Internal storage mapping names to materials.
Parser for material property files in INI format.
Definition Materials.hpp:17
static double parseNumber(const std::string &str)
Parse a string as a double-precision floating point number.
Quantity< Pascal > youngs_modulus
Young's modulus E (Pa)
Definition Materials.hpp:26
static std::unordered_map< std::string, MaterialData > Parse(const std::string &filename)
Parse an INI file and extract material definitions.
Definition Materials.hpp:40
static void trim(std::string &str)
Remove leading and trailing whitespace characters.
constexpr bool is_unit_v
Definition Unit.hpp:79
Physical quantity with a value and a compile-time unit.
Definition Quantity.hpp:27