Hyperiso 1.0.3
Modular flavour-physics calculations, Wilson coefficients and statistical inference
Loading...
Searching...
No Matches
DependentBlockManager.cpp
Go to the documentation of this file.
2#include "Parameters.h"
3
5 std::string name,
6 std::unordered_map<ParameterType, std::vector<std::string>> source_names,
7 ParameterType dest,
8 DepUpdateFunc recalculateFunc
9) {
10 std::unordered_map<std::string, std::shared_ptr<Block>> sources;
11 std::vector<std::string> missing;
12
13 for (const auto& [k, v] : source_names) {
14 auto ba = Parameters::GetInstance(k)->blockAccessor;
15 for (const auto& src_name : v) {
16 if (!ba->contains(src_name)) {
17 missing.push_back(ParameterTypeMapper::str(k) + "::" + src_name);
18 continue;
19 }
20 sources.emplace(src_name, ba->at(src_name));
21 }
22 }
23
24 if (!missing.empty()) {
25 std::ostringstream oss;
26 oss << "addDependentBlock '" << name << "': missing source blocks: ";
27 for (size_t i = 0; i < missing.size(); ++i) { if (i) oss << ", "; oss << missing[i]; }
28 oss << ". Available per model:\n";
29 for (const auto& [k, _] : source_names) {
30 auto ba = Parameters::GetInstance(k)->blockAccessor;
31 oss << " - " << ParameterTypeMapper::str(k) << ": ";
32 bool first = true;
33 for (auto& bn : ba->get_block_names()) { if (!first) oss << ", "; first = false; oss << bn; }
34 oss << "\n";
35 }
36 throw std::invalid_argument(oss.str());
37 }
38
39 auto dependentBlock = std::make_shared<DependentBlock>(sources, recalculateFunc);
40 dependentBlock->blockname = name;
41 Parameters::GetInstance(dest)->blockAccessor->emplace(name, dependentBlock);
42 dependentBlock->init();
43 dependentBlock->update();
44}
45
47 ParamId pid,
48 std::unordered_set<ParamId> source_pids,
49 DepParamUpdateFunc recalculateFunc
50) {
51 std::unordered_map<ParamId, std::shared_ptr<Parameter>> sources;
52 std::vector<std::string> missing;
53
54 for (const auto& id : source_pids) {
55 auto ba = Parameters::GetInstance(id.type.value())->blockAccessor;
56
57 auto blk = ba->at(id.block);
58
59 try {
60 auto p = blk->retrieve(id.code);
61 sources.emplace(id, p);
62 } catch (...) {
63 missing.push_back("Param " + ParameterTypeMapper::str(id.type.value()) +
64 "::" + id.block + "::" + id.code.to_string());
65 continue;
66 }
67 }
68
69 if (!missing.empty()) {
70 std::ostringstream oss;
71 oss << "addDependentParameter '"
72 << ParameterTypeMapper::str(pid.type.value()) << "::" << pid.block << "::" << pid.code.to_string()
73 << "': missing sources: ";
74 for (size_t i = 0; i < missing.size(); ++i) { if (i) oss << ", "; oss << missing[i]; }
75 throw std::invalid_argument(oss.str());
76 }
77
78 auto ba = Parameters::GetInstance(pid.type.value())->blockAccessor;
79 if (!ba->contains(pid.block)) {
80 ba->emplace(pid.block, std::make_shared<Block>());
81 ba->at(pid.block)->blockname = pid.block;
82 }
83
84 auto blk = ba->at(pid.block);
85
86 if (blk->contains(pid.code)) {
87 auto existing = blk->retrieve(pid.code);
88 if (auto dep = std::dynamic_pointer_cast<DependentParameter>(existing)) {
89 dep->rebind(std::move(sources), recalculateFunc);
90 dep->init();
91 return;
92 }
93
94 }
95
96 auto dependentParam = std::make_shared<DependentParameter>(pid, std::move(sources), recalculateFunc);
97 dependentParam->init();
98 dependentParam->update();
99 blk->store(pid.code, dependentParam);
100}
101
103 ParameterType src)
104{
105 auto params = Parameters::GetInstance(src);
106 auto accessor = params->blockAccessor;
107
108 if (!accessor->contains(name)) {
109 return;
110 }
111
112 std::shared_ptr<Block> dep_block = accessor->at(name);
113 if (dep_block) {
114 dep_block->destroy();
115 }
116
117 accessor->erase_block(name);
118}
119
120void DependentBlockManager::update(const std::string &name, ParameterType src) {
121 Parameters::GetInstance(src)->blockAccessor->at(name)->update();
122}
std::function< void(const BlockSrc &, std::shared_ptr< DependentBlock >)> DepUpdateFunc
Function type used to recompute a DependentBlock from its sources.
Definition Block.h:56
Provides static utilities to create, manage, and update dependent blocks and dependent parameters.
std::function< void(const ParamSrc &, std::shared_ptr< DependentParameter >)> DepParamUpdateFunc
Function signature used to recompute a DependentParameter.
ParameterType
Model-dependent parameter repository and initialization strategies.
static void update(const std::string &name, ParameterType src)
Manually triggers an update on a DependentBlock.
static void removeDependentBlock(const std::string &name, ParameterType src)
Removes a DependentBlock from a specified Parameters instance.
static void addDependentBlock(std::string name, std::unordered_map< ParameterType, std::vector< std::string > > source_names, ParameterType dest, DepUpdateFunc recalculateFunc)
Adds a DependentBlock with multiple sources coming from various Parameters instances.
static void addDependentParameter(ParamId pid, std::unordered_set< ParamId > source_pids, DepParamUpdateFunc recalculateFunc)
Adds a DependentParameter based on multiple source parameters.
static std::string str(const IdOf< ParamTypeTag > &id)
Returns the string representation associated with an identifier.
static std::shared_ptr< Parameters > GetInstance(ParameterType id=ParameterType::SM)
Returns the singleton-like repository for a given parameter type.
std::string to_string() const
Returns the canonical string representation of this LhaID.
Definition LhaID.cpp:19
Composite identifier for a single parameter.
Definition ParamID.h:57
std::optional< ParameterType > type
Optional high-level parameter category.
Definition ParamID.h:65
BlockName block
Name of the block where the parameter is stored.
Definition ParamID.h:73
LhaID code
Index or multi-index of the parameter inside the block.
Definition ParamID.h:82