Commit 5e09e770 authored by Grégoire Uhlrich's avatar Grégoire Uhlrich
Browse files

C9_SM working, param_t struct updated

parent 3c9f9f27
......@@ -23,7 +23,7 @@ class InitSanitizer {
public:
constexpr InitSanitizer(char const t_name[])
:name(t_name)
:m_name(t_name)
{
}
......@@ -33,23 +33,39 @@ public:
}
InitSanitizer &operator=(T const &t) {
safe = true;
value = t;
m_safe = true;
m_value = t;
return *this;
}
char const *name() {
return m_name;
}
operator T () const {
return get();
}
T get() const {
if (!safe) {
std::cerr << "Error: param \"" << name << "\" is used ";
std::cerr << "uninitialized, please assign it a value using ";
std::cerr << "standard value assignement.\n";
if (!m_safe) {
std::cerr << "Error: param \"" << m_name << "\" is used ";
std::cerr << "uninitialized, please assign it a m_value using ";
std::cerr << "standard m_value assignement.\n";
assert(false);
}
return value;
return m_value;
}
void reset() {
m_safe = false;
}
void print(std::ostream &out = std::cout) const
{
if (m_safe)
out << m_name << " = " << m_value << '\n';
else
out << m_name << " uninitialized.\n";
}
friend
......@@ -58,15 +74,15 @@ public:
InitSanitizer const &san
)
{
out << san.value;
out << san.m_value;
return out;
}
private:
char const *name;
T value;
bool safe { false };
char const *m_name;
T m_value;
bool m_safe { false };
};
} // namespace csl
......@@ -113,7 +113,7 @@ namespace csl {
LibDependency getDependencies() const;
std::shared_ptr<LibraryGroup> getGroup(std::string_view name) const;
bool hasGlobalFile() const {
return !diagData.empty();
return !diagData.empty() || !massExpressions.empty();
}
bool hasUniqueParamStruct() const {
return uniqueParamStruct;
......@@ -155,6 +155,7 @@ namespace csl {
Expr expression,
std::string const &nameGroup = "G"
);
void addMassExpression(std::string const &mass);
void addDiagonalization(
std::vector<std::string> const &mixing,
std::vector<std::string> const &masses,
......@@ -171,6 +172,15 @@ namespace csl {
void printCallable() const;
void printTest() const;
void printDiagonalizationFacility(
std::ostream &header,
std::ostream &source
) const;
void printMassExpressionsFacility(
std::ostream &header,
std::ostream &source
) const;
void printMakefile() const;
void printPythonDir() const;
......@@ -230,15 +240,20 @@ namespace csl {
std::vector<DiagonalizationData> diagData;
mutable
std::vector<LibParameter> uniqueParams;
std::vector<std::string> massExpressions;
bool uniqueParamStruct;
};
std::vector<LibParameter> diagonalizationParameters(
std::vector<LibParameter> inputParams(
std::vector<LibraryGenerator::DiagonalizationData> const &diagData
);
std::vector<LibParameter> outputParams(
std::vector<LibraryGenerator::DiagonalizationData> const &diagData,
std::vector<std::string> const &massExpressions
);
} // End of namespace csl
#endif
......@@ -73,6 +73,12 @@ namespace csl {
return functions.back();
}
std::vector<LibParameter> const &getForcedParameters() const {
return forcedParameters;
}
void setForcedParameters(std::vector<LibParameter> const &t_params);
static
std::vector<LibParameter> gatherParameters(
std::vector<std::shared_ptr<LibraryGroup>> &groups,
......@@ -81,6 +87,32 @@ namespace csl {
void gatherParameters();
static
void printResetParameterList(
std::string const &nameContainer,
std::vector<LibParameter> const &params,
std::ostream &out,
int nIndent
);
void printResetDefinition(
std::ostream &out,
int nIndent
) const;
static
void printPrintParameterList(
std::string const &nameContainer,
std::vector<LibParameter> const &params,
std::ostream &out,
int nIndent
);
void printPrintDefinition(
std::ostream &out,
int nIndent
) const;
void printStructDefinition(
std::ostream &out,
int nIndent
......@@ -91,7 +123,10 @@ namespace csl {
int nIndent
) const;
void printParameterDefinition(std::ostream &out) const;
void printParameterDefinition(
std::ostream &out,
bool unusedParam
) const;
void printParameterInitialization(
std::ostream &out,
......@@ -115,6 +150,7 @@ namespace csl {
bool complexReturn;
mutable int posTensorParam;
mutable std::vector<LibParameter> parameters;
mutable std::vector<LibParameter> forcedParameters;
mutable std::vector<LibFunction> functions;
};
......
......@@ -191,7 +191,7 @@ namespace csl {
}
printName(out);
out << LibraryGenerator::indent(2);
group->printParameterDefinition(out);
group->printParameterDefinition(out, csl::IsNumerical(expression));
out << '\n' << LibraryGenerator::indent(2) << ")";
if (header)
out << ";\n";
......@@ -284,12 +284,16 @@ namespace csl {
int tensorParameter
)
{
auto comp = [&](LibParameter const &A, LibParameter const &B) {
return A.type < B.type
|| (A.type == B.type && A.name < B.name);
};
if (static_cast<size_t>(tensorParameter) < parameters.size()) {
std::sort(parameters.begin(), parameters.begin() + tensorParameter);
std::sort(parameters.begin() + tensorParameter, parameters.end());
std::sort(parameters.begin(), parameters.begin() + tensorParameter, comp);
std::sort(parameters.begin() + tensorParameter, parameters.end(), comp);
}
else
std::sort(parameters.begin(), parameters.end());
std::sort(parameters.begin(), parameters.end(), comp);
}
void LibFunction::parse()
......
......@@ -241,6 +241,14 @@ namespace csl {
return group->addFunction(std::move(f));
}
void LibraryGenerator::addMassExpression(std::string const &mass)
{
auto pos = std::find(massExpressions.begin(), massExpressions.end(), mass);
if (pos == massExpressions.end()) {
massExpressions.push_back(mass);
}
}
void LibraryGenerator::addDiagonalization(
std::vector<std::string> const &mixings,
std::vector<std::string> const &masses,
......@@ -273,17 +281,22 @@ namespace csl {
= std::make_shared<LibraryGroup>("", false);
dummy->addFunction(LibFunction("dummy", CSL_0, dummy));
dummy->getFunctions()[0].setParameters(
diagonalizationParameters(diagData));
inputParams(diagData));
groups.push_back(dummy);
LibraryGroup::gatherParameters(groups, "param_t");
groups.erase(groups.end() - 1);
file paramFile(path + "/" + incDir + "/params.h");
paramFile << "#ifndef CSL_LIB_PARAM_H_INCLUDED\n",
paramFile << "#define CSL_LIB_PARAM_H_INCLUDED\n\n";
paramFile << "#include <array>\n";
paramFile << "#include \"common.h\"\n";
paramFile << "#include \"libcomplexop.h\"\n";
paramFile << "#include \"csl/initSanitizer.h\"\n";
paramFile << '\n';
paramFile << "namespace " << regLibName() << " {\n\n";
dummy->setForcedParameters(
outputParams(diagData, massExpressions)
);
dummy->print(paramFile, true);
paramFile << "\n\n";
paramFile << "}\n\n";
......@@ -318,6 +331,7 @@ namespace csl {
void LibraryGenerator::printGlobal() const
{
LibraryGenerator::file header(path + "/" + incDir + "/global.h");
LibraryGenerator::file source(path + "/" + srcDir + "/global.cpp");
CSL_ASSERT_SPEC(
header,
CSLError::IOError,
......@@ -328,13 +342,38 @@ namespace csl {
if (uniqueParamStruct)
header << "#include \"params.h\"\n";
header << "#include \"common.h\"\n\n";
header << "namespace " << regLibName() << " {\n\n";
source << "#include \"global.h\"\n";
source << "#include \"libdiagonalization.h\"\n";
source << "#include \"" << regLibName() << ".h\"\n";
source << "#include \"libcomplexop.h\"\n\n";
source << "namespace " << regLibName() << " {\n\n";
source << '\n';
if (!diagData.empty()) {
printDiagonalizationFacility(header, source);
}
if (!massExpressions.empty() && uniqueParamStruct) {
printMassExpressionsFacility(header, source);
}
header << '\n';
header << "} // End of namespace " << regLibName() << "\n\n";
header << "#endif\n";
source << "} // End of namespace " << regLibName() << "\n\n";
}
void LibraryGenerator::printDiagonalizationFacility(
std::ostream &header,
std::ostream &source
) const
{
header << "struct SpectrumInput;\n";
header << "struct SpectrumOutput;\n\n";
header << "SpectrumOutput updateDiagonalization(SpectrumInput const&);\n\n";
if (uniqueParamStruct)
if (uniqueParamStruct) {
header << "void updateDiagonalization(param_t &params);\n\n";
}
header << "////////////////////////////////////////////////////\n";
header << "// Here are the parameters to set before calling \n"
<< "// updateDiagonalization() \n";
......@@ -393,17 +432,7 @@ namespace csl {
<< "diagData." << regularName(mix) << ";\n";
}
header << "}\n";
header << '\n';
header << "} // End of namespace " << regLibName() << "\n\n";
header << "#endif\n";
LibraryGenerator::file source(path + "/" + srcDir + "/global.cpp");
source << "#include \"global.h\"\n";
source << "#include \"libdiagonalization.h\"\n";
source << "#include \"" << regLibName() << ".h\"\n";
source << "#include \"libcomplexop.h\"\n\n";
source << "namespace " << regLibName() << " {\n\n";
source << '\n';
if (uniqueParamStruct) {
source << "void updateDiagonalization(param_t &params)\n";
source << "{\n";
......@@ -462,18 +491,44 @@ namespace csl {
source << "&outputs." << mass << ", ";
source << "}\n";
source << indent(2) << ");\n";
source << '\n';
if (diag.squaredMass) {
for (const auto &mass : diag.masses) {
source << "outputs." << mass << " = ";
source << indent(1) << "outputs." << mass << " = ";
source << ((quadruple) ? "sqrtq(" : "std::sqrt(");
source << "outputs." << mass << ");\n";
}
}
source << '\n';
}
source << indent(1) << "return outputs;\n";
source << "}\n\n";
source << "} // End of namespace " << regLibName() << "\n\n";
}
void LibraryGenerator::printMassExpressionsFacility(
std::ostream &header,
std::ostream &source
) const
{
header << "void updateMassExpressions(param_t &params);\n\n";
header << "void updateSpectrum(param_t &params);\n\n";
source << "void updateMassExpressions(param_t &params)\n";
source << "{\n";
for (const auto &mass : massExpressions) {
if (quadruple) {
source << indent(1) << "params." << mass << " = crealq("
<< mass << "(params));\n";
}
else {
source << indent(1) << "params." << mass << " = "
<< mass << "(params).real();\n";
}
}
source << "}\n\n";
source << "void updateSpectrum(param_t &params)\n";
source << "{\n";
source << indent(1) << "updateDiagonalization(params);\n";
source << indent(1) << "updateMassExpressions(params);\n";
source << "}\n\n";
}
void LibraryGenerator::printHeader() const
......@@ -1191,20 +1246,34 @@ namespace csl {
return std::string(nIndent * nSpaceIndent, ' ');
}
std::vector<LibParameter> diagonalizationParameters(
std::vector<LibParameter> inputParams(
std::vector<LibraryGenerator::DiagonalizationData> const &diagData
)
{
std::vector<LibParameter> res;
res.reserve(4 * diagData.size());
for (const auto &data : diagData) {
for (const auto &dep : data.dependencies)
res.push_back({dep, "complex_t"});
}
return res;
}
std::vector<LibParameter> outputParams(
std::vector<LibraryGenerator::DiagonalizationData> const &diagData,
std::vector<std::string> const &massExpressions
)
{
std::vector<LibParameter> res;
res.reserve(4 * diagData.size() + massExpressions.size());
for (const auto &data : diagData) {
for (const auto &mass : data.masses)
res.push_back({mass, "real_t"});
for (const auto &mix : data.mixings)
res.push_back({mix, "complex_t"});
for (const auto &dep : data.dependencies)
res.push_back({dep, "complex_t"});
}
for (const auto &mass : massExpressions)
res.push_back({mass, "real_t"});
return res;
}
......
......@@ -70,6 +70,163 @@ namespace csl {
*this = *v[0];
}
void LibraryGroup::setForcedParameters(
std::vector<LibParameter> const &t_params)
{
forcedParameters = t_params;
int tp = -1;
LibFunction::cutParameters(forcedParameters, tp);
tp = -1;
LibFunction::sortParameters(forcedParameters, tp);
for (const auto &fp : forcedParameters) {
for (size_t i = 0; i != parameters.size(); ++i) {
if (parameters[i].name == fp.name) {
parameters.erase(parameters.begin() + i);
if (posTensorParam >= static_cast<int>(i))
--posTensorParam;
break;
}
}
}
}
static
std::pair<std::vector<LibParameter>, std::vector<LibParameter>>
separateComplexParameters(
std::vector<LibParameter> const &params1,
std::vector<LibParameter> const &params2
)
{
std::pair<std::vector<LibParameter>, std::vector<LibParameter>> res;
res.first.reserve(params1.size()+params2.size()); // real params
res.second.reserve(params1.size()+params2.size()); // complex params
for (const auto &par : params1) {
if (par.type == LibraryGenerator::realUsing)
res.first.push_back(par);
else
res.second.push_back(par);
}
for (const auto &par : params2) {
if (par.type == LibraryGenerator::realUsing)
res.first.push_back(par);
else
res.second.push_back(par);
}
return res;
}
void LibraryGroup::printResetParameterList(
std::string const &nameContainer,
std::vector<LibParameter> const &params,
std::ostream &out,
int nIndent
)
{
constexpr size_t limitParams = 5;
const auto newParamLine = '\n' + LibraryGenerator::indent(nIndent + 2);
out << LibraryGenerator::indent(nIndent) << "for (auto &par : "
<< nameContainer << "{";
size_t count = 0;
if (params.size() > limitParams) {
out << newParamLine;
}
for (const auto &par : params) {
if (++count > limitParams) {
count = 0;
out << newParamLine;
}
out << '&' << par.name << ", ";
}
out << "})\n";
out << LibraryGenerator::indent(nIndent) << "{\n";
out << LibraryGenerator::indent(nIndent + 1) << "par->reset();\n";
out << LibraryGenerator::indent(nIndent) << "}\n";
}
void LibraryGroup::printResetDefinition(
std::ostream &out,
int nIndent
) const
{
auto const &[realParams, complexParams] = separateComplexParameters(
parameters, forcedParameters
);
out << LibraryGenerator::indent(nIndent) << "void reset()\n";
out << LibraryGenerator::indent(nIndent) << "{\n";
out << LibraryGenerator::indent(nIndent+1)
<< "using real_params = std::array<csl::InitSanitizer<"
<< LibraryGenerator::realUsing << ">*, "
<< realParams.size() << ">;\n";
out << LibraryGenerator::indent(nIndent+1)
<< "using complex_params = std::array<csl::InitSanitizer<"
<< LibraryGenerator::complexUsing << ">*, "
<< complexParams.size() << ">;\n";
out << '\n';
printResetParameterList("real_params", realParams, out, nIndent+1);
out << '\n';
printResetParameterList("complex_params", complexParams, out, nIndent+1);
out << LibraryGenerator::indent(nIndent) << "}\n";
}
void LibraryGroup::printPrintParameterList(
std::string const &nameContainer,
std::vector<LibParameter> const &params,
std::ostream &out,
int nIndent
)
{
constexpr size_t limitParams = 5;
const auto newParamLine = '\n' + LibraryGenerator::indent(nIndent + 2);
out << LibraryGenerator::indent(nIndent) << "for (auto const &par : "
<< nameContainer << "{";
size_t count = 0;
if (params.size() > limitParams) {
out << newParamLine;
}
for (const auto &par : params) {
if (++count > limitParams) {
count = 0;
out << newParamLine;
}
out << '&' << par.name << ", ";
}
out << "})\n";
out << LibraryGenerator::indent(nIndent) << "{\n";
out << LibraryGenerator::indent(nIndent + 1) << "out << \" -> \";\n";
out << LibraryGenerator::indent(nIndent + 1) << "par->print(out);\n";
out << LibraryGenerator::indent(nIndent) << "}\n";
}
void LibraryGroup::printPrintDefinition(
std::ostream &out,
int nIndent
) const
{
auto const &[realParams, complexParams] = separateComplexParameters(
parameters, forcedParameters
);
out << LibraryGenerator::indent(nIndent)
<< "void print(std::ostream &out = std::cout)\n";
out << LibraryGenerator::indent(nIndent) << "{\n";
out << LibraryGenerator::indent(nIndent+1)
<< "using real_params = std::array<csl::InitSanitizer<"
<< LibraryGenerator::realUsing << "> const*, "
<< realParams.size() << ">;\n";
out << LibraryGenerator::indent(nIndent+1)
<< "using complex_params = std::array<csl::InitSanitizer<"
<< LibraryGenerator::complexUsing << "> const*, "
<< complexParams.size() << ">;\n";
out << '\n';
out << LibraryGenerator::indent(1) << "out << \"param_t struct:\\n\";";
out << LibraryGenerator::indent(1) << "out << \"Real parameters\\n\";";
printPrintParameterList("real_params", realParams, out, nIndent+1);
out << '\n';
out << LibraryGenerator::indent(1) << "out << \"Complex parameters\\n\";";
printPrintParameterList("complex_params", complexParams, out, nIndent+1);
out << LibraryGenerator::indent(1) << "out << \"\\n\";";
out << LibraryGenerator::indent(nIndent) << "}\n";
}
void LibraryGroup::printStructDefinition(
std::ostream &out,
int nIndent
......@@ -77,13 +234,42 @@ namespace csl {
{
out << LibraryGenerator::indent(nIndent);
out << "struct " << getParamName();
out << " {\n";
for (size_t i = 0; i != parameters.size(); ++i) {
out << " {\n\n";
out << LibraryGenerator::indent(nIndent + 1)
<< "///////////////////////////////////////\n";
out << LibraryGenerator::indent(nIndent + 1)
<< "// Elementary parameters to be defined \n";
out << LibraryGenerator::indent(nIndent + 1)
<< "///////////////////////////////////////\n\n";
for (const auto &par : parameters) {
out << LibraryGenerator::indent(nIndent + 1);
out << "csl::InitSanitizer<" << parameters[i].type << "> "
<< parameters[i].name << " { \"" << parameters[i].name
out << "csl::InitSanitizer<" << par.type << "> "
<< par.name << " { \"" << par.name
<< "\" };\n";
}
out << "\n\n";
out << LibraryGenerator::indent(nIndent + 1)
<< "///////////////////////////////////////\n";