Hyperiso 1.0.3
Modular flavour-physics calculations, Wilson coefficients and statistical inference
Loading...
Searching...
No Matches
DependentParameter.h
Go to the documentation of this file.
1#ifndef DEPENDENT_PARAMETER_H
2#define DEPENDENT_PARAMETER_H
3
4#include <functional>
5#include <memory>
6#include <unordered_map>
7
8#include "Parameter.h"
9
10class ParamSrc;
12
47typedef std::function<void(const ParamSrc&, std::shared_ptr<DependentParameter>)> DepParamUpdateFunc;
48
81public:
105 explicit DependentParameter(ParamId pid, std::unordered_map<ParamId, std::shared_ptr<Parameter>> sources, DepParamUpdateFunc recalculateFunc);
106
113 bool dependsOn(const ParamId& pid);
114
125 void init();
126
141 void update() override;
142
150 void freeze() override;
151
158 void unfreeze() override;
159
168 void clear_above() override;
169
183 void clear_below() override;
184
190 std::unordered_map<ParamId, std::shared_ptr<Parameter>> get_source_parameters() const override {
191 return this->sources_raw;
192 }
193
206 scalar_t get_val() const override;
207
214 void mark_dirty();
215
231 void ensure_up_to_date();
232
249 void rebind(std::unordered_map<ParamId, std::shared_ptr<Parameter>> new_sources,
250 DepParamUpdateFunc new_lambda);
251
271 void detach();
272
290 void reattach();
291
301
302private:
303 std::weak_ptr<DependentParameter> self;
304 std::unordered_map<ParamId, std::shared_ptr<Parameter>> sources_raw;
305 std::unique_ptr<ParamSrc> sources;
306 DepParamUpdateFunc recalculateLambda;
307 bool frozen {false};
308 bool update_at_unfreeze {false};
309 bool dirty = true;
310
316 std::unordered_map<ParamId, std::shared_ptr<Parameter>> saved_sources_raw;
317
321 DepParamUpdateFunc saved_recalculateLambda;
322
326 bool dependency_detached = false;
327};
328
345inline std::shared_ptr<Parameter>& operator+=(std::shared_ptr<Parameter>& lhs, const std::shared_ptr<Parameter>& rhs) {
346 if (rhs) {
347 if (lhs && lhs->get_id() == rhs->get_id()) {
348 *lhs += *rhs;
349 }
350 else
351 lhs = std::make_shared<Parameter>(*rhs);
352 }
353 return lhs;
354}
355
371inline std::shared_ptr<Parameter>& operator*(std::shared_ptr<Parameter>& lhs, const scalar_t& rhs) {
372 if (rhs) {
373 *lhs *= rhs;
374 }
375 return lhs;
376}
377
378#endif
std::shared_ptr< Parameter > & operator*(std::shared_ptr< Parameter > &lhs, const scalar_t &rhs)
Scales a shared parameter in place.
std::shared_ptr< Parameter > & operator+=(std::shared_ptr< Parameter > &lhs, const std::shared_ptr< Parameter > &rhs)
Adds the payload of one shared parameter into another.
std::function< void(const ParamSrc &, std::shared_ptr< DependentParameter >)> DepParamUpdateFunc
Function signature used to recompute a DependentParameter.
Defines the Parameter class used to store individual physical/model parameters.
Parameter whose value is computed from other parameters and cached lazily.
std::unordered_map< ParamId, std::shared_ptr< Parameter > > get_source_parameters() const override
Returns the source parameters this dependent parameter currently uses.
void rebind(std::unordered_map< ParamId, std::shared_ptr< Parameter > > new_sources, DepParamUpdateFunc new_lambda)
Replaces the dependency set and recomputation rule at runtime.
void clear_above() override
Detaches this parameter from all upstream source parameters.
scalar_t get_val() const override
Returns the current cached value, recomputing it on demand if needed.
void unfreeze() override
Unfreezes the dependent parameter.
void update() override
Marks the parameter as dirty and propagates the update downstream.
void freeze() override
Freezes the dependent parameter.
void clear_below() override
Clears this parameter and all downstream dependent parameters.
void ensure_up_to_date()
Recomputes the parameter if its cached value is stale.
bool dependsOn(const ParamId &pid)
Checks whether this parameter depends on a given source parameter.
void reattach()
Restores previously detached dependencies and recomputation rule.
void mark_dirty()
Marks the cached value as dirty.
void init()
Initializes observer registration on all source parameters.
void detach()
Temporarily detaches all dependencies while keeping the current cached value.
Lightweight view over a set of source parameters keyed by ParamId.
Represents a single parameter with value, uncertainties, and dependency links.
Definition Parameter.h:78
Composite identifier for a single parameter.
Definition ParamID.h:57