Hyperiso 1.0.3
Modular flavour-physics calculations, Wilson coefficients and statistical inference
Loading...
Searching...
No Matches
main_detach.cpp
Go to the documentation of this file.
1#include <cassert>
2#include <cmath>
3#include <iostream>
4#include <memory>
5#include <unordered_map>
6
7#include "BlockAccessor.h"
8#include "Block.h"
10#include "SourcesView.h"
11
12namespace {
13
14using PT = ParameterType;
15
16bool almost_equal(double a, double b, double eps = 1e-12) {
17 return std::abs(a - b) < eps;
18}
19
20void check(BlockAccessor& ba, const std::string& block, const LhaID& id, double expected) {
21 const double got = ba.getValue(block, id);
22 std::cout << block << "[" << id << "] = " << got << " (expected " << expected << ")\n";
23 assert(almost_equal(got, expected));
24}
25
26void banner(const std::string& title) {
27 std::cout << "\n==============================\n";
28 std::cout << title << "\n";
29 std::cout << "==============================\n";
30}
31
32void add_leaf_block(BlockAccessor& ba, const BlockName& name) {
33 auto blk = std::make_shared<Block>();
34 blk->blockname = name;
35 ba.emplace(name, blk);
36}
37
38void set_typed_leaf(BlockAccessor& ba, const BlockName& block, const LhaID& id, double value, PT type) {
39 ba.setValue(block, id, value);
40 ba.getParameter(block, id)->set_owner(type);
41}
42
43void add_dependent_block_local(
44 BlockAccessor& ba,
45 const BlockName& name,
46 std::unordered_map<std::string, std::shared_ptr<Block>> sources,
47 DepUpdateFunc recalculateFunc)
48{
49 auto dep = std::make_shared<DependentBlock>(sources, std::move(recalculateFunc));
50 dep->blockname = name;
51 ba.emplace(name, dep); // important avant init()
52 dep->init();
53 dep->update();
54}
55
56void add_dependent_parameter_local(
57 BlockAccessor& ba,
58 ParamId pid,
59 std::unordered_map<ParamId, std::shared_ptr<Parameter>> sources,
60 DepParamUpdateFunc recalculateFunc)
61{
62 if (!ba.contains(pid.block)) {
63 add_leaf_block(ba, pid.block);
64 }
65
66 auto blk = ba.at(pid.block);
67
68 if (blk->contains(pid.code)) {
69 auto existing = blk->retrieve(pid.code);
70 if (auto dep = std::dynamic_pointer_cast<DependentParameter>(existing)) {
71 dep->rebind(std::move(sources), std::move(recalculateFunc));
72 dep->init();
73 return;
74 }
75 }
76
77 auto dep = std::make_shared<DependentParameter>(pid, std::move(sources), std::move(recalculateFunc));
78 blk->store(pid.code, dep);
79 dep->init();
80 dep->update();
81}
82
83} // namespace
84
85int main() {
87
88 const LhaID A{1};
89 const LhaID C{1};
90 const LhaID P{1};
91 const LhaID Z{1};
92
93 // -------------------------------------------------------------------------
94 // 1) BASE : block leaf
95 // -------------------------------------------------------------------------
96 add_leaf_block(ba, "BASE");
97 set_typed_leaf(ba, "BASE", A, 10.0, PT::SM);
98
99 // -------------------------------------------------------------------------
100 // 2) DB1 : DependentBlock <- BASE
101 //
102 // c = 2 * BASE[a]
103 // -------------------------------------------------------------------------
104 add_dependent_block_local(
105 ba,
106 "DB1",
107 {
108 {"BASE", ba.at("BASE")}
109 },
110 [](const BlockSrc& src, std::shared_ptr<DependentBlock> self) {
111 const double a = src.get_val("BASE", 1);
112 const double c = 2.0 * a;
113
114 auto out = std::make_shared<Parameter>(
115 ParamId(PT::SM, "DB1", LhaID{1}),
116 c, 0.0, 0.0
117 );
118 self->store_or_assign(LhaID{1}, out);
119 }
120 );
121
122 // force existence de DB1[1]
123 (void)ba.getValue("DB1", C);
124
125 // -------------------------------------------------------------------------
126 // 3) PARAMS : block leaf contenant un DependentParameter
127 //
128 // p = DB1[c] + 5
129 // -------------------------------------------------------------------------
130 {
131 ParamId src_id(PT::SM, "DB1", C);
132 ParamId dst_id(PT::SM, "PARAMS", P);
133
134 add_dependent_parameter_local(
135 ba,
136 dst_id,
137 {
138 {src_id, ba.getParameter("DB1", C)}
139 },
140 [src_id](const ParamSrc& src, std::shared_ptr<DependentParameter> self) {
141 const double c = src.get_val(src_id);
142 self->set_expected_silent(c + 5.0);
143 }
144 );
145 }
146
147 // -------------------------------------------------------------------------
148 // 4) DB2 : DependentBlock <- DB1 + PARAMS
149 //
150 // z = DB1[c] + PARAMS[p]
151 // -------------------------------------------------------------------------
152 add_dependent_block_local(
153 ba,
154 "DB2",
155 {
156 {"DB1", ba.at("DB1")},
157 {"PARAMS", ba.at("PARAMS")}
158 },
159 [](const BlockSrc& src, std::shared_ptr<DependentBlock> self) {
160 const double c = src.get_val("DB1", 1);
161 const double p = src.get_val("PARAMS", 1);
162 const double z = c + p;
163
164 auto out = std::make_shared<Parameter>(
165 ParamId(PT::SM, "DB2", LhaID{1}),
166 z, 0.0, 0.0
167 );
168 self->store_or_assign(LhaID{1}, out);
169 }
170 );
171
172 // -------------------------------------------------------------------------
173 // Etat initial
174 // BASE[a] = 10
175 // DB1[c] = 20
176 // PARAMS[p] = 25
177 // DB2[z] = 45
178 // -------------------------------------------------------------------------
179 banner("Etat initial");
180 check(ba, "BASE", A, 10.0);
181 check(ba, "DB1", C, 20.0);
182 check(ba, "PARAMS", P, 25.0);
183 check(ba, "DB2", Z, 45.0);
184
185 // -------------------------------------------------------------------------
186 // BASE[a] = 20
187 // DB1[c] = 40
188 // PARAMS[p] = 45
189 // DB2[z] = 85
190 // -------------------------------------------------------------------------
191 banner("Apres BASE[a] = 20");
192 ba.setValue("BASE", A, 20.0);
193 check(ba, "BASE", A, 20.0);
194 check(ba, "DB1", C, 40.0);
195 check(ba, "PARAMS", P, 45.0);
196 check(ba, "DB2", Z, 85.0);
197
198 // -------------------------------------------------------------------------
199 // detach PARAMS[p], puis BASE[a] = 7
200 // DB1[c] = 14
201 // PARAMS[p] reste 45
202 // DB2[z] = 59
203 // -------------------------------------------------------------------------
204 banner("Detach PARAMS[p], puis BASE[a] = 7");
205 ba.detach_parameter("PARAMS", P);
206 ba.setValue("BASE", A, 7.0);
207 check(ba, "BASE", A, 7.0);
208 check(ba, "DB1", C, 14.0);
209 check(ba, "PARAMS", P, 45.0);
210 check(ba, "DB2", Z, 59.0);
211
212 // -------------------------------------------------------------------------
213 // reattach PARAMS[p]
214 // DB1[c] = 14
215 // PARAMS[p] = 19
216 // DB2[z] = 33
217 // -------------------------------------------------------------------------
218 banner("Reattach PARAMS[p]");
219 ba.reattach_parameter("PARAMS", P);
220 check(ba, "BASE", A, 7.0);
221 check(ba, "DB1", C, 14.0);
222 check(ba, "PARAMS", P, 19.0);
223 check(ba, "DB2", Z, 33.0);
224
225 // -------------------------------------------------------------------------
226 // detach DB1, puis BASE[a] = 100
227 // DB1[c] reste 14
228 // PARAMS[p] reste 19
229 // DB2[z] reste 33
230 // -------------------------------------------------------------------------
231 banner("Detach DB1, puis BASE[a] = 100");
232 ba.detach_block("DB1");
233 ba.setValue("BASE", A, 100.0);
234 check(ba, "BASE", A, 100.0);
235 check(ba, "DB1", C, 14.0);
236 check(ba, "PARAMS", P, 19.0);
237 check(ba, "DB2", Z, 33.0);
238
239 // -------------------------------------------------------------------------
240 // reattach DB1
241 // DB1[c] = 200
242 // PARAMS[p] = 205
243 // DB2[z] = 405
244 // -------------------------------------------------------------------------
245 banner("Reattach DB1");
246 ba.reattach_block("DB1");
247 check(ba, "BASE", A, 100.0);
248 check(ba, "DB1", C, 200.0);
249 check(ba, "PARAMS", P, 205.0);
250 check(ba, "DB2", Z, 405.0);
251
252 std::cout << "\nTous les checks sont passes.\n";
253 return 0;
254}
Alias-aware façade for accessing and manipulating multiple parameter blocks.
Defines classes used to store parameters and to build derived/dependent parameter blocks.
std::function< void(const BlockSrc &, std::shared_ptr< DependentBlock >)> DepUpdateFunc
Function type used to recompute a DependentBlock from its sources.
Definition Block.h:56
Defines parameters whose values are lazily computed from other parameters.
std::function< void(const ParamSrc &, std::shared_ptr< DependentParameter >)> DepParamUpdateFunc
Function signature used to recompute a DependentParameter.
ParameterType
Alias-aware container / façade over several parameter blocks.
std::shared_ptr< Block > & at(const BlockName &block_name)
Alias-aware mutable access to a block.
void reattach_block(const BlockName &block_name)
Reattaches a previously detached dependent block to its saved sources.
bool contains(const BlockName &block_name) const
Checks whether a block exists (alias-aware).
scalar_t getValue(const BlockName &blockName, LhaID pdgCode) const
Retrieves the current value of a parameter from a block.
void detach_parameter(const BlockName &block_name, LhaID id)
Detaches a dependent parameter from its upstream dependencies.
void reattach_parameter(const BlockName &block_name, LhaID id)
Reattaches a previously detached dependent parameter.
void emplace(const BlockName &name, std::shared_ptr< Block > blk)
Inserts or replaces a block, updating alias metadata.
void detach_block(const BlockName &block_name)
Detaches a dependent block from its upstream dependencies.
void setValue(const BlockName &blockName, LhaID pdgCode, scalar_t value)
Sets the value of a parameter inside an existing block.
std::shared_ptr< Parameter > getParameter(const BlockName &blockName, LhaID id) const
Retrieves the full Parameter object stored in a block.
Block identifier with alias support.
Definition BlockName.h:59
Lightweight view over a set of source blocks.
Definition SourcesView.h:71
scalar_t get_val(std::string_view blk, std::initializer_list< long > code) const
Retrieves the value of a parameter given as an initializer_list.
Lightweight view over a set of source parameters keyed by ParamId.
scalar_t get_val(const ParamId &id) const
Retrieves the current value of a parameter.
int main()
Represents an identifier of a LHA element, possibly containing several sub-ids.
Definition LhaID.h:56
Composite identifier for a single parameter.
Definition ParamID.h:57
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