Hyperiso 1.0.3
Modular flavour-physics calculations, Wilson coefficients and statistical inference
Loading...
Searching...
No Matches
Block.h
Go to the documentation of this file.
1#ifndef BLOCK_H
2#define BLOCK_H
3
4#include <array>
5#include <functional>
6
7#include "Include.h"
8#include "Parameter.h"
9#include "IStorage.h"
10
38class BlockSrc;
39class Block;
40class DependentBlock;
41
56typedef std::function<void(const BlockSrc&, std::shared_ptr<DependentBlock>)> DepUpdateFunc;
57
58
73class Block : public IStorage<LhaID, Parameter>, public std::enable_shared_from_this<Block> {
74public:
77
83 Block() = default;
84
96 Block(std::shared_ptr<Block> other);
97
102 BlockName get_name() const { return this->blockname; }
103
116 void store(const LhaID& id, std::shared_ptr<Parameter> param) override;
117
133 void assign(const LhaID& key, std::shared_ptr<Parameter> param) override;
134
147 void assign(const LhaID& key, scalar_t value);
148
159 void store_or_assign(const LhaID& key, std::shared_ptr<Parameter> param) override;
160
171 bool contains(const LhaID& key) const override;
172
182 std::shared_ptr<Parameter> retrieve(const LhaID& id) override;
183
195 void remove(const LhaID& key) override;
196
204 std::unordered_set<LhaID> getAllIDs();
205
213 const std::map<LhaID, std::shared_ptr<Parameter>>& getItems();
214
222 void set_owner(ParameterType type);
223
231 void addObserver(std::shared_ptr<Block> observer);
232
240 void removeObserver(std::shared_ptr<Block> observer);
241
248 void notifyObservers();
249
254 std::vector<std::shared_ptr<Block>> getObservers() const;
255
262 virtual void init() {};
263
272 virtual void update();
273
286 virtual void destroy();
287
293 virtual void freeze();
294
300 virtual void unfreeze();
301
315 void copy(std::shared_ptr<Block> other);
316
325 std::shared_ptr<Block> deep_clone_plain() const;
326
332 virtual void clear_above();
333
340 virtual void clear_below();
341
346 bool has_scale();
347
355 void set_scale(double scale);
356
363 double get_scale();
364
373 void erase_local(const LhaID& id);
374
383 virtual std::unordered_map<std::string, std::shared_ptr<Block>> get_source_blocks() const;
384
391 virtual void ensure_up_to_date() {}
392
404 void bind_self(std::shared_ptr<Block> self) {
405 self_ = self;
406
407 std::weak_ptr<Block> w(self);
408 for (auto& [_, p] : items) {
409 if (p) p->set_owner_block(w);
410 }
411 }
412
420
435 friend std::ostream& operator<<(std::ostream&, std::shared_ptr<Block>);
436protected:
437 std::vector<std::shared_ptr<Block>> observers;
438 std::map<LhaID, std::shared_ptr<Parameter>> items;
439 std::optional<double> scale;
440
441 std::weak_ptr<Block> self_;
442
453 std::weak_ptr<Block> self_weak() {
454 if (auto s = self_.lock()) return self_;
455 try { return shared_from_this(); } catch (...) { return {}; }
456 }
457};
458
481class DependentBlock : public Block {
482public:
493 const std::unordered_map<std::string, std::shared_ptr<Block>>& sources,
494 DepUpdateFunc recalculateFunc)
495 : sourceBlocks(sources),
496 recalculateLambda(recalculateFunc),
497 saved_sourceBlocks(sources),
498 saved_recalculateLambda(recalculateFunc),
499 frozen(false) {}
500
507 bool dependsOn(const std::string& blockName);
508
515 void init() override;
516
526 void update() override;
527
533 void freeze() override;
534
541 void unfreeze() override;
542
558 void detach();
559
574 void reattach();
575
582
593 void assign(const LhaID& key, std::shared_ptr<Parameter> param);
594
603 void assign(const LhaID& key, double value);
604
609 std::unordered_map<std::string, std::shared_ptr<Block>> get_source_blocks() const override;
610
616 void clear_above() override;
617
624 void clear_below() override;
625
635 void destroy() override;
636
647 void mark_dirty();
648
656 void ensure_up_to_date() override { ensure_up_to_date_impl(); }
657
658private:
664 void ensure_up_to_date_impl();
665
666 std::weak_ptr<DependentBlock> self;
667 std::unordered_map<std::string, std::shared_ptr<Block>> sourceBlocks;
668 DepUpdateFunc recalculateLambda;
669 bool frozen = false;
670 bool update_at_unfreeze = false;
671 bool dirty = true;
672
679 std::unordered_map<std::string, std::shared_ptr<Block>> saved_sourceBlocks;
680
687 DepUpdateFunc saved_recalculateLambda;
688
697 bool dependency_detached = false;
698};
699
700#endif
std::function< void(const BlockSrc &, std::shared_ptr< DependentBlock >)> DepUpdateFunc
Function type used to recompute a DependentBlock from its sources.
Definition Block.h:56
ParameterType
Defines the Parameter class used to store individual physical/model parameters.
Block identifier with alias support.
Definition BlockName.h:59
Lightweight view over a set of source blocks.
Definition SourcesView.h:71
Definition Block.h:73
virtual void freeze()
Freezes all parameters contained in the block.
Definition Block.cpp:38
double get_scale()
Returns the block-wide scale.
Definition Block.cpp:198
void addObserver(std::shared_ptr< Block > observer)
Adds a block observer if not already present.
Definition Block.cpp:4
virtual void update()
Updates the block.
Definition Block.cpp:31
std::unordered_set< LhaID > getAllIDs()
Returns the set of all parameter ids stored in the block.
Definition Block.cpp:132
std::map< LhaID, std::shared_ptr< Parameter > > items
List of observing blocks notified through notifyObservers().
Definition Block.h:438
~Block()
Destructor.
Definition Block.h:419
virtual void unfreeze()
Unfreezes all parameters contained in the block.
Definition Block.cpp:44
void store_or_assign(const LhaID &key, std::shared_ptr< Parameter > param) override
Stores a parameter if absent, otherwise assigns into the existing one.
Definition Block.cpp:98
void removeObserver(std::shared_ptr< Block > observer)
Removes a previously registered observer block.
Definition Block.cpp:11
virtual void destroy()
Destroys the block and its downstream block dependencies.
Definition Block.cpp:205
void set_owner(ParameterType type)
Sets the owner ParameterType on all contained parameters.
Definition Block.cpp:126
void notifyObservers()
Notifies all observer blocks that this block has changed.
Definition Block.cpp:17
std::shared_ptr< Block > deep_clone_plain() const
Deep-copies this block as a plain independent block.
Definition Block.cpp:153
void erase_local(const LhaID &id)
Erases an entry from the local storage map only.
Definition Block.cpp:75
void assign(const LhaID &key, std::shared_ptr< Parameter > param) override
Replaces the payload of an existing parameter from another parameter object.
Definition Block.cpp:79
virtual void clear_below()
Clears parameter dependencies below this block.
Definition Block.cpp:176
void set_scale(double scale)
Sets the block-wide scale.
Definition Block.cpp:191
BlockName blockname
Name of the block (e.g. "SMINPUTS", "MASS", "FWCOEF"...)
Definition Block.h:76
std::vector< std::shared_ptr< Block > > observers
Definition Block.h:437
void bind_self(std::shared_ptr< Block > self)
Binds an explicit self shared_ptr to the block.
Definition Block.h:404
bool has_scale()
Returns whether the block has an associated scale.
Definition Block.cpp:187
Block()=default
Default constructor.
virtual void clear_above()
Clears parameter dependencies above this block.
Definition Block.cpp:170
void remove(const LhaID &key) override
Removes a parameter and clears its downstream dependency subtree.
Definition Block.cpp:113
std::optional< double > scale
Internal storage of parameters indexed by LHA id.
Definition Block.h:439
std::weak_ptr< Block > self_
Optional block-wide scale.
Definition Block.h:441
virtual std::unordered_map< std::string, std::shared_ptr< Block > > get_source_blocks() const
Returns the source blocks of this block.
Definition Block.cpp:216
virtual void ensure_up_to_date()
Ensures that the block content is up to date.
Definition Block.h:391
void copy(std::shared_ptr< Block > other)
Copies the content and metadata from another block.
Definition Block.cpp:142
bool contains(const LhaID &key) const override
Checks whether the block contains a parameter with the given id.
Definition Block.cpp:106
void store(const LhaID &id, std::shared_ptr< Parameter > param) override
Stores a new parameter under the given LHA id.
Definition Block.cpp:64
std::weak_ptr< Block > self_weak()
Optional explicit self-reference used to rebind owner_block on contained parameters.
Definition Block.h:453
virtual void init()
Initialization hook.
Definition Block.h:262
friend std::ostream & operator<<(std::ostream &, std::shared_ptr< Block >)
Stream output operator for a block.
Definition Block.cpp:413
const std::map< LhaID, std::shared_ptr< Parameter > > & getItems()
Returns the internal parameter map.
Definition Block.cpp:137
std::shared_ptr< Parameter > retrieve(const LhaID &id) override
Retrieves a stored parameter by id.
Definition Block.cpp:54
std::vector< std::shared_ptr< Block > > getObservers() const
Returns the current list of observer blocks.
Definition Block.cpp:27
BlockName get_name() const
Returns the name of the block.
Definition Block.h:102
Block whose content is derived from one or more source blocks.
Definition Block.h:481
void clear_below() override
Clears downstream block dependencies.
Definition Block.cpp:288
std::unordered_map< std::string, std::shared_ptr< Block > > get_source_blocks() const override
Returns the map of source blocks for this dependent block.
Definition Block.cpp:276
void destroy() override
Destroys this dependent block and its downstream block dependencies.
Definition Block.cpp:303
void mark_dirty()
Marks this block dirty and propagates the dirty state downstream.
Definition Block.cpp:369
void update() override
Marks the block as dirty.
Definition Block.cpp:384
void detach()
Temporarily detaches this block from its dependency graph.
Definition Block.cpp:320
bool dependsOn(const std::string &blockName)
Checks whether this dependent block uses a given source block name.
Definition Block.cpp:299
~DependentBlock()
Destructor.
Definition Block.cpp:245
void freeze() override
Freezes the block and all contained parameters.
Definition Block.cpp:228
void unfreeze() override
Unfreezes the block and contained parameters.
Definition Block.cpp:234
void init() override
Initializes the dependency graph for this block.
Definition Block.cpp:220
void assign(const LhaID &key, std::shared_ptr< Parameter > param)
Overwrites the payload of a local parameter without triggering block-level notification.
Definition Block.cpp:256
void ensure_up_to_date() override
Ensures this dependent block is up to date.
Definition Block.h:656
DependentBlock(const std::unordered_map< std::string, std::shared_ptr< Block > > &sources, DepUpdateFunc recalculateFunc)
Constructs a dependent block from source blocks and a recomputation callback.
Definition Block.h:492
void reattach()
Reattaches a previously detached dependent block.
Definition Block.cpp:343
void clear_above() override
Clears upstream block dependencies.
Definition Block.cpp:280
Generic storage interface for key–value containers.
Definition IStorage.h:24
Represents an identifier of a LHA element, possibly containing several sub-ids.
Definition LhaID.h:56