ARTS 2.5.0 (git: 9ee3ac6c)
gen_auto_partfun.cc
Go to the documentation of this file.
1#include <filesystem>
2#include <fstream>
3#include <limits>
4#include <map>
5#include <vector>
6
7#include "debug.h"
8#include "gridded_fields.h"
9#include "species.h"
10
11#include "xml_io_partfun.h"
12
14struct PrintData {
17};
18
19
20std::pair<std::string_view, std::string_view> split_filename_species(const std::string_view isot) {
21 auto x = std::min(isot.find('-'), isot.size());
22 return {isot.substr(0, x),
23 isot.substr(std::min(x+1, isot.size()), isot.size())};
24}
25
26
27std::pair<Species::Species, PrintData> gen_fil(std::filesystem::path fil) {
28 if (not std::filesystem::exists(fil)) {
29 ARTS_USER_ERROR("file does not exist: ", fil)
30 }
31
32 PrintData out;
33
34 auto filname_no_ext = fil.filename().replace_extension().native();
35 const auto [specname, isotname] = split_filename_species(filname_no_ext);
36 const Species::Species spec = Species::fromShortName(specname);
37 out.name = isotname;
38 ARTS_USER_ERROR_IF(not good_enum(spec), "Got Species: ", specname, "\nFrom isotope: ", out.name, "\nIn filename of: ", fil)
39
41
42 return {spec, out};
43}
44
45
46std::map<Species::Species, std::vector<PrintData>> gen_dir(std::filesystem::path dir) {
47 if (not std::filesystem::is_directory(dir)) {
48 ARTS_USER_ERROR("not a directory: ", dir)
49 }
50
51 // Make sure all species exists in the data-map
52 std::map<Species::Species, std::vector<PrintData>> out;
53 for (auto spec: Species::enumtyps::SpeciesTypes) {
54 out[spec] = {};
55 }
56
57 // Fill out the data map per file in the directory
58 for(auto& p: std::filesystem::directory_iterator(dir)) {
59 if (p.path().extension() == ".xml") {
60 const auto [spec, data] = gen_fil(p.path());
61 out.at(spec).push_back(data);
62 }
63 }
64 for (auto spec: Species::enumtyps::SpeciesTypes) {
65 std::sort(out[spec].begin(), out[spec].end(), [](auto& a, auto& b){return a.name.compare(b.name) < 0;});
66 }
67
68 return out;
69}
70
71
73 data.data.print_data();
74}
75
76
78 data.data.print_method();
79}
80
81
82void print_auto_partfun_h(const std::map<Species::Species, std::vector<PrintData>>& data) {
83 std::puts(R"AUTO_PARTFUN(//! Auto-generated partition function data
84#ifndef auto_partfun_h
85#define auto_partfun_h
86
87#include <limits>
88
89#include "template_partfun.h"
90#include "debug.h"
91
92template <std::size_t N>
93std::string column_string(const std::array<std::string_view, N>& list) {
94 if constexpr (static_cast<bool>(N)) {
95 std::ostringstream os;
96 for (auto& x: list) os << x << '\n';
97 return os.str();
98 } else return "Nothing Was Compiled For This Species";
99}
100
101namespace PartitionFunctions {)AUTO_PARTFUN");
102
103 std::cout << std::setprecision(std::numeric_limits<Numeric>::digits10 + 1);
104 for (auto& [spec, vec_data]: data) {
105
106 std::cout << "constexpr std::array<std::string_view, " << vec_data.size() << "> has"<<spec<<"{\n";
107 for (auto& pfdata: vec_data) {
108 std::cout << "\"" << pfdata.name << "\",\n";
109 }
110 std::cout << "};\n";
111
112 std::cout << '\n';
113 std::cout << "template <Derivatives derivative>\nconstexpr Numeric compute"<<spec<<"(const Numeric T, const std::string_view name) {\n";
114
115 bool first = true;
116 for (auto& pfdata: vec_data) {
117 if (not first) {
118 std::cout << "else if";
119 } else {
120 std::cout << "if";
121 first = false;
122 }
123
124 std::cout << " (\"" << pfdata.name << "\" == name) {\n";
125 print_data(pfdata);
126 print_method(pfdata);
127 std::cout << "} ";
128 }
129
130 if (not first) {
131 std::cout << "else if";
132 } else {
133 std::cout << "if";
134 }
135
136 std::cout << " (\"*\" == name) {\n"
137 "return T * std::numeric_limits<Numeric>::signaling_NaN();\n"
138 "} else ARTS_USER_ERROR(\"Cannot understand species: \", name, \" of "
139 << spec << "\\n\"\n"
140 " \"Did you compile without supporting it?\\n\"\n"
141 " \"Supported species are:\\n\", column_string(has" << spec << "))"
142 "\n}\n\n";
143 }
144
145 std::puts(R"AUTO_PARTFUN(
146} // PartitionFunctions
147
148#endif // auto_partfun_h
149)AUTO_PARTFUN");
150}
151
152
153int main(int argc, char ** argv) try {
154 ARTS_USER_ERROR_IF(argc not_eq 2, "Bad call, must be used as ", argv[0], " DIR")
155
156
158
159 return EXIT_SUCCESS;
160} catch (std::exception& e) {
161 std::cerr << e.what() << '\n';
162 return EXIT_FAILURE;
163}
void * data
char * isot
Helper macros for debugging.
#define ARTS_USER_ERROR(...)
Definition: debug.h:150
#define ARTS_USER_ERROR_IF(condition,...)
Definition: debug.h:134
constexpr bool good_enum(EnumType x) noexcept
Checks if the enum number is good.
Definition: enums.h:21
std::pair< std::string_view, std::string_view > split_filename_species(const std::string_view isot)
std::pair< Species::Species, PrintData > gen_fil(std::filesystem::path fil)
int main(int argc, char **argv)
void print_method(const PrintData &data)
void print_data(const PrintData &data)
std::map< Species::Species, std::vector< PrintData > > gen_dir(std::filesystem::path dir)
void print_auto_partfun_h(const std::map< Species::Species, std::vector< PrintData > > &data)
Implementation of gridded fields.
#define min(a, b)
Data data_read_file(const std::filesystem::path &path)
constexpr Rational end(Rational Ju, Rational Jl, Polarization type) noexcept
Gives the largest M for a polarization type of this transition.
Definition: zeemandata.h:109
#define a
#define b
Contains the partition function data in a simple format.
PartitionFunctions::Data data