Hyperiso 1.0.3
Modular flavour-physics calculations, Wilson coefficients and statistical inference
Loading...
Searching...
No Matches
TemplateManager.cpp
Go to the documentation of this file.
1#include "TemplateManager.h"
2#include <iostream>
3#include <vector>
4#include <string>
5#include <sstream>
6#include "config.hpp"
8#include "FileNameManager.h"
9
10std::string joinLines(const std::vector<std::string>& lines) {
11 std::stringstream ss;
12 for (const auto& line : lines) {
13 ss << line << "\n";
14 }
15 return ss.str();
16}
17
18void NumericTemplateManager::generateTemplateImpl(const std::string&,
19 const std::string& outputPath) {
20 namespace fs = std::filesystem;
21
22 auto mgr = FileNameManager::getInstance(this->wilson, this->model);
23
24 const fs::path helper_cpp_src = mgr->getBaseHelperFileName("cpp");
25 const fs::path helper_h_src = mgr->getBaseHelperFileName("h");
26 const fs::path helper_cpp_dst = mgr->getHelperFileName("cpp");
27 const fs::path helper_h_dst = mgr->getHelperFileName("h");
28
29 fs::create_directories(helper_cpp_dst.parent_path());
30 fs::create_directories(helper_h_dst.parent_path());
31
32 if (!this->already_generated(helper_cpp_dst.string())) {
33 fs::copy_file(helper_cpp_src, helper_cpp_dst, fs::copy_options::overwrite_existing);
34 fs::copy_file(helper_h_src, helper_h_dst, fs::copy_options::overwrite_existing);
35 }
36
37 const fs::path templatePath = mgr->getNumGeneratedFileName();
38 std::ifstream templateFile(templatePath);
39
40 const fs::path paramPath = mgr->getParamFileName();
41 fs::create_directories(paramPath.parent_path());
42 const fs::path paramTmpPath = paramPath.string() + ".tmp";
43 {
44 std::ofstream paramFile(paramTmpPath, std::ios::trunc);
45 if (!paramFile) {
46 throw std::runtime_error("Cannot write MARTY parameter file: " + paramTmpPath.string());
47 }
48 std::unordered_map<std::string, double> params = numModifier->get_params();
49 numModifier->createparamfile(paramFile, params);
50 paramFile.flush();
51 if (!paramFile) {
52 throw std::runtime_error("Failed while writing MARTY parameter file: " + paramTmpPath.string());
53 }
54 }
55 std::error_code paramEc;
56 fs::rename(paramTmpPath, paramPath, paramEc);
57 if (paramEc) {
58 std::error_code cleanupEc;
59 fs::remove(paramTmpPath, cleanupEc);
60 throw std::runtime_error("Cannot atomically publish MARTY parameter file: " + paramEc.message());
61 }
62
63 const fs::path outPathFs = outputPath;
64 fs::create_directories(outPathFs.parent_path());
65
66 if (this->already_generated(outputPath)) {
67 return;
68 }
69
70 const fs::path tmpPath = outPathFs.string() + ".tmp";
71 std::ofstream outputFile(tmpPath);
72 if (!outputFile) return;
73
74 outputFile << "//42\n";
75 numModifier->modify(templateFile, outputFile);
76 templateFile.close();
77 outputFile.close();
78
79 fs::copy_file(tmpPath, outPathFs, fs::copy_options::overwrite_existing);
80}
81
82void NonNumericTemplateManager::generateTemplateImpl(const std::string& templateName, const std::string& outputPath) {
83 std::string templatePath = templatesDir + "/" + templateName + ".cpp";
84 std::ifstream templateFile(templatePath);
85
86 if (!templateFile) {
87 return;
88 }
89
90 if (this->already_generated(outputPath)) {
91 return;
92 }
93 std::ofstream outputFile(outputPath);
94 if (!outputFile) {
95 return;
96 }
97
98 std::string line;
99 std::getline(templateFile, line);
100
101 modelModifier->addLine(outputFile, line);
102 outputFile << "//42" << "\n";
103
104 while (std::getline(templateFile, line)) {
105 if (modelModifier) {
106 modelModifier->modifyLine(line);
107 modelModifier->addLine(outputFile, line);
108 } else {
109 outputFile << line << "\n";
110 }
111 }
112}
113
114std::unordered_set<InterpretedParam> TemplateManagerBase::get_dependencies() {
115 std::unordered_set<InterpretedParam> deps;
116 for (auto& [k, v] : numModifier->get_interpreted_param_map()) {
117 deps.emplace(v);
118 }
119 return deps;
120}
121
122bool TemplateManagerBase::already_generated(const std::string &path)
123{
124 std::ifstream file(path);
125
126 if (!file.is_open()) {
127 return false;
128 }
129
130 // Non-numeric MARTY sources preserve the template's first include and put
131 // the generation marker on the following line. Checking only line one
132 // therefore caused every calculate() call to rewrite the analytical source
133 // even when its ABI/model/template cache metadata was still valid.
134 std::string line;
135 for (std::size_t line_number = 0;
136 line_number < 16 && std::getline(file, line);
137 ++line_number) {
138 if (line.find("//42") != std::string::npos) {
139 return true;
140 }
141 }
142
143 return false;
144}
Manages file and directory names for generated MARTY code and assets.
Declares the helper used to prepare numeric MARTY models and parameter files.
std::string joinLines(const std::vector< std::string > &lines)
Declares managers for generating (numeric and non-numeric) model templates.
static std::shared_ptr< FileNameManager > getInstance(const std::string &wilson="", const std::string &model="")
Retrieves a FileNameManager for a given (wilson, model) pair.
std::string wilson
Wilson basis name.
std::string templatesDir
Directory containing the template sources.
std::unique_ptr< GeneralNumModelModifier > numModifier
Optional modifier for numeric templates.
bool already_generated(const std::string &path)
Checks whether a file was already generated by this system.
std::unordered_set< InterpretedParam > get_dependencies()
Returns the set of interpreted parameters used by the numeric modifier.
std::unique_ptr< ModelModifier > modelModifier
Optional modifier for non-numeric templates.
std::string model
Model name.