Hyperiso 1.0.3
Modular flavour-physics calculations, Wilson coefficients and statistical inference
Loading...
Searching...
No Matches
main_lazy.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#include <unordered_set>
7
8#include "BlockAccessor.h"
9#include "Block.h"
10#include "Parameter.h"
11#include "DependentParameter.h"
12#include "Include.h"
13#include "SourcesView.h"
14
15static std::shared_ptr<Parameter> mkp(ParameterType type, const std::string& blk, long code, double v){
16 return std::make_shared<Parameter>(ParamId{type, blk, LhaID(code)}, v, 0.0, 0.0);
17}
18
19static void print_block(BlockAccessor& ba, const std::string& name) {
20 std::cout << "\n=== BLOCK " << name << " ===\n";
21 if (!ba.contains(name)) {
22 std::cout << "(missing)\n";
23 return;
24 }
25 auto blk = ba.at(name);
26 auto ids = blk->getAllIDs();
27 std::cout << "IDs: ";
28 for (auto& id : ids) std::cout << id.to_string() << " ";
29 std::cout << "\n";
30 for (auto& [id, p] : blk->getItems()) {
31 std::cout << " " << id.to_string() << " = " << p->get_val() << "\n";
32 }
33}
34
35int main() {
36 std::cout << "== Deep dependency graph smoke test ==\n";
37
38 LhaID ID_X(100);
39 LhaID ID_P1(200);
40 LhaID ID_P2(201);
41 LhaID ID_SUM(300);
42 LhaID ID_POST(400);
43
44 auto A = std::make_shared<Block>(); A->blockname = "SRC_A";
45 auto B = std::make_shared<Block>(); B->blockname = "SRC_B";
46 A->store(ID_X, mkp(ParameterType::SM, "SRC_A", 100, 1.0));
47 B->store(ID_X, mkp(ParameterType::SM, "SRC_B", 100, 2.0));
48
49 auto acc = std::make_shared<BlockAccessor>();
50 acc->emplace("SRC_A", A);
51 acc->emplace("SRC_B", B);
52
53 auto depSUM = std::make_shared<DependentBlock>(
54 std::unordered_map<std::string, std::shared_ptr<Block>>{
55 {"SRC_A", A},
56 {"SRC_B", B}
57 },
58 [=](const auto& blocks, std::shared_ptr<DependentBlock> self){
59 double v = blocks.get_val("SRC_A", ID_X) + blocks.get_val("SRC_B", ID_X);
60 if (!self->contains(ID_SUM)) {
61 self->store(ID_SUM, mkp(ParameterType::SM, "SUM", 300, 0.0));
62 }
63 self->assign(ID_SUM, v);
64 }
65 );
66 depSUM->blockname = "SUM";
67 depSUM->init();
68 acc->emplace("SUM", depSUM);
69
70 auto DER = std::make_shared<Block>(); DER->blockname = "DERIVED";
71 acc->emplace("DERIVED", DER);
72
73 {
74 ParamId pid{ParameterType::SM, "DERIVED", ID_P1};
75 std::unordered_map<ParamId, std::shared_ptr<Parameter>> srcs;
76 // source = param A[ID_X]
77 srcs.emplace(ParamId{ParameterType::SM, "SRC_A", ID_X}, A->retrieve(ID_X));
78
79 auto P1 = std::make_shared<DependentParameter>(
80 pid, std::move(srcs),
81 [=](const ParamSrc& s, std::shared_ptr<DependentParameter> self){
82 double a = s.get_val(ParamId{ParameterType::SM, "SRC_A", ID_X});
83 self->set_expected(10.0 * a);
84 }
85 );
86 P1->init();
87 DER->store_or_assign(ID_P1, P1);
88 }
89
90 {
91 ParamId pid{ParameterType::SM, "DERIVED", ID_P2};
92 std::unordered_map<ParamId, std::shared_ptr<Parameter>> srcs;
93
94 srcs.emplace(ParamId{ParameterType::SM, "SUM", ID_SUM}, depSUM->retrieve(ID_SUM));
95
96 srcs.emplace(ParamId{ParameterType::SM, "DERIVED", ID_P1}, DER->retrieve(ID_P1));
97
98 auto P2 = std::make_shared<DependentParameter>(
99 pid, std::move(srcs),
100 [=](const ParamSrc& s, std::shared_ptr<DependentParameter> self){
101 double sumv = s.get_val(ParamId{ParameterType::SM, "SUM", ID_SUM});
102 double p1v = s.get_val(ParamId{ParameterType::SM, "DERIVED", ID_P1});
103 self->set_expected(sumv + p1v);
104 }
105 );
106 P2->init();
107 DER->store_or_assign(ID_P2, P2);
108 }
109
110 auto depPOST = std::make_shared<DependentBlock>(
111 std::unordered_map<std::string, std::shared_ptr<Block>>{
112 {"SUM", depSUM},
113 {"DERIVED", DER}
114 },
115 [=](const auto& blocks, std::shared_ptr<DependentBlock> self){
116 double sumv = blocks.get_val("SUM", ID_SUM);
117 double p2v = blocks.get_val("DERIVED", ID_P2);
118 double v = 3.0 * sumv + p2v;
119 if (!self->contains(ID_POST)) {
120 self->store(ID_POST, mkp(ParameterType::SM, "POST", 400, 0.0));
121 }
122 self->assign(ID_POST, v);
123 }
124 );
125 depPOST->blockname = "POST";
126 depPOST->init();
127 acc->emplace("POST", depPOST);
128
129
130 std::cout << "\n[1] Test lazy via contains() sur blocks dependants\n";
131 bool sum_has = acc->at("SUM")->contains(ID_SUM);
132 std::cout << "SUM contains(300) = " << sum_has << "\n";
133 assert(sum_has && "SUM should materialize on contains()");
134
135 bool post_has = acc->at("POST")->contains(ID_POST);
136 std::cout << "POST contains(400) = " << post_has << "\n";
137 assert(post_has && "POST should materialize on contains()");
138
139 std::cout << "\n[2] Lecture valeurs (force ensure_up_to_date en cascade)\n";
140 double sum0 = acc->getValue("SUM", ID_SUM);
141 double p1_0 = acc->getValue("DERIVED", ID_P1);
142 double p2_0 = acc->getValue("DERIVED", ID_P2);
143 double post0 = acc->getValue("POST", ID_POST);
144
145 std::cout << "SUM = " << sum0 << " (expected 1+2=3)\n";
146 std::cout << "P1 = " << p1_0 << " (expected 10*A=10)\n";
147 std::cout << "P2 = " << p2_0 << " (expected SUM+P1=13)\n";
148 std::cout << "POST = " << post0 << " (expected 3*SUM+P2=22)\n";
149
150 assert(std::abs(sum0 - 3.0) < 1e-12);
151 assert(std::abs(p1_0 - 10.0) < 1e-12);
152 assert(std::abs(p2_0 - 13.0) < 1e-12);
153 assert(std::abs(post0 - 22.0) < 1e-12);
154
155 std::cout << "\n[3] Mutation source: setValue(A=7) => cascade\n";
156 acc->setValue("SRC_A", ID_X, 7.0);
157 double sum1 = acc->getValue("SUM", ID_SUM); // 7+2=9
158 double p1_1 = acc->getValue("DERIVED", ID_P1); // 70
159 double p2_1 = acc->getValue("DERIVED", ID_P2); // 79
160 double post1 = acc->getValue("POST", ID_POST); // 3*9+79=106
161
162 std::cout << "SUM = " << sum1 << " (expected 9)\n";
163 std::cout << "P1 = " << p1_1 << " (expected 70)\n";
164 std::cout << "P2 = " << p2_1 << " (expected 79)\n";
165 std::cout << "POST = " << post1 << " (expected 106)\n";
166
167 assert(std::abs(sum1 - 9.0) < 1e-12);
168 assert(std::abs(p1_1 - 70.0) < 1e-12);
169 assert(std::abs(p2_1 - 79.0) < 1e-12);
170 assert(std::abs(post1 - 106.0) < 1e-12);
171
172 std::cout << "\n[4] Freeze: freeze POST, mutate B, POST doit rester stable\n";
173 acc->at("POST")->freeze();
174 acc->setValue("SRC_B", ID_X, 10.0);
175
176 double sum2 = acc->getValue("SUM", ID_SUM);
177 double post2 = acc->getValue("POST", ID_POST);
178
179 std::cout << "SUM = " << sum2 << " (expected 17)\n";
180 std::cout << "POST = " << post2 << " (expected STILL 106 because frozen)\n";
181 assert(std::abs(sum2 - 17.0) < 1e-12);
182 assert(std::abs(post2 - 106.0) < 1e-12);
183
184 std::cout << "\n[5] Unfreeze: POST doit se mettre à jour au prochain get\n";
185 acc->at("POST")->unfreeze();
186 double post3 = acc->getValue("POST", ID_POST);
187
188 std::cout << "POST = " << post3 << " (expected 138)\n";
189 assert(std::abs(post3 - 138.0) < 1e-12);
190
191 std::cout << "\n[6] Sanity: getAllIDs/getItems prints\n";
192 print_block(*acc, "SRC_A");
193 print_block(*acc, "SRC_B");
194 print_block(*acc, "SUM");
195 print_block(*acc, "DERIVED");
196 print_block(*acc, "POST");
197
198 std::cout << "\n[7] Remove source parameter A[100] then check behavior\n";
199
200 acc->remove_item("SRC_A", ID_X);
201
202 bool threw = !acc->at("SUM")->contains(300);
203 assert(threw);
204
205
206 threw = !acc->at("DERIVED")->contains(ID_P1);
207
208 assert(threw);
209 std::cout << "\n Deep dependency graph smoke test passed.\n";
210 return 0;
211}
Alias-aware façade for accessing and manipulating multiple parameter blocks.
Defines classes used to store parameters and to build derived/dependent parameter blocks.
Defines parameters whose values are lazily computed from other parameters.
ParameterType
Defines the Parameter class used to store individual physical/model parameters.
std::string to_string(const LhaID &id)
Convenience stringification for LhaID.
Definition SourceView.cpp:9
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.
bool contains(const BlockName &block_name) const
Checks whether a block exists (alias-aware).
Lightweight view over a set of source parameters keyed by ParamId.
int main()
Definition main_lazy.cpp:35
csl::Expr v
Definition sm.h:110
Hash specialization for SymbolId<Tag>.
Definition BlockName.h:353
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