GridFire 0.0.1a
General Purpose Nuclear Network
Loading...
Searching...
No Matches
construction.cpp
Go to the documentation of this file.
2
3#include <ranges>
4#include <stdexcept>
5
8
9#include "fourdst/composition/composition.h"
10
11#include "fourdst/logging/logging.h"
12
13#include "quill/Logger.h"
14#include "quill/LogMacros.h"
15
16namespace gridfire {
20 using fourdst::composition::Composition;
21 using fourdst::atomic::Species;
22
23
25 const Composition &composition,
26 BuildDepthType maxLayers,
27 bool reverse
28 ) {
29 int depth;
30 if (std::holds_alternative<NetworkBuildDepth>(maxLayers)) {
31 depth = static_cast<int>(std::get<NetworkBuildDepth>(maxLayers));
32 } else {
33 depth = std::get<int>(maxLayers);
34 }
35 auto logger = fourdst::logging::LogManager::getInstance().getLogger("log");
36 if (depth == 0) {
37 LOG_ERROR(logger, "Network build depth is set to 0. No reactions will be collected.");
38 throw std::logic_error("Network build depth is set to 0. No reactions will be collected.");
39 }
40
41 const auto allReactions = reaclib::get_all_reactions();
42 std::vector<Reaction> remainingReactions;
43 for (const auto& reaction : allReactions) {
44 if (reaction.is_reverse() == reverse) {
45 remainingReactions.push_back(reaction);
46 }
47 }
48
49 if (depth == static_cast<int>(NetworkBuildDepth::Full)) {
50 LOG_INFO(logger, "Building full nuclear network with a total of {} reactions.", allReactions.size());
51 const ReactionSet reactionSet(remainingReactions);
53 }
54
55 std::unordered_set<Species> availableSpecies;
56 for (const auto &entry: composition | std::views::values) {
57 if (entry.mass_fraction() > 0.0) {
58 availableSpecies.insert(entry.isotope());
59 }
60 }
61
62
63 std::vector<Reaction> collectedReactions;
64
65 LOG_INFO(logger, "Starting network construction with {} available species.", availableSpecies.size());
66 for (int layer = 0; layer < depth && !remainingReactions.empty(); ++layer) {
67 LOG_TRACE_L1(logger, "Collecting reactions for layer {} with {} remaining reactions. Currently there are {} available species", layer, remainingReactions.size(), availableSpecies.size());
68 std::vector<Reaction> reactionsForNextPass;
69 std::unordered_set<Species> newProductsThisLayer;
70 bool newReactionsAdded = false;
71
72 reactionsForNextPass.reserve(remainingReactions.size());
73
74 for (const auto &reaction : remainingReactions) {
75 bool allReactantsAvailable = true;
76 for (const auto& reactant : reaction.reactants()) {
77 if (!availableSpecies.contains(reactant)) {
78 allReactantsAvailable = false;
79 break;
80 }
81 }
82
83 if (allReactantsAvailable) {
84 collectedReactions.push_back(reaction);
85 newReactionsAdded = true;
86
87 for (const auto& product : reaction.products()) {
88 newProductsThisLayer.insert(product);
89 }
90 } else {
91 reactionsForNextPass.push_back(reaction);
92 }
93 }
94
95 if (!newReactionsAdded) {
96 LOG_INFO(logger, "No new reactions added in layer {}. Stopping network construction with {} reactions collected.", layer, collectedReactions.size());
97 break;
98 }
99
100 LOG_TRACE_L1(logger, "Layer {}: Collected {} reactions. New products this layer: {}", layer, collectedReactions.size(), newProductsThisLayer.size());
101 availableSpecies.insert(newProductsThisLayer.begin(), newProductsThisLayer.end());
102
103 remainingReactions = std::move(reactionsForNextPass);
104 }
105
106 LOG_INFO(logger, "Network construction completed with {} reactions collected.", collectedReactions.size());
107 const ReactionSet reactionSet(collectedReactions);
109 }
110
111
112
113}
Represents a single nuclear reaction from a specific data source.
Definition reaction.h:72
const reaction::LogicalReactionSet & get_all_reactions()
Provides global access to the fully initialized REACLIB reaction set.
Definition reaclib.cpp:138
TemplatedReactionSet< LogicalReaction > LogicalReactionSet
A set of logical reactions.
Definition reaction.h:563
LogicalReactionSet packReactionSetToLogicalReactionSet(const ReactionSet &reactionSet)
Definition reaction.cpp:273
TemplatedReactionSet< Reaction > ReactionSet
A set of reactions, typically from a single source like REACLIB.
Definition reaction.h:562
std::variant< NetworkBuildDepth, int > BuildDepthType
Variant specifying either a predefined NetworkBuildDepth or a custom integer depth.
Definition building.h:37
reaction::LogicalReactionSet build_reaclib_nuclear_network(const fourdst::composition::Composition &composition, BuildDepthType maxLayers=NetworkBuildDepth::Full, bool reverse=false)
Builds a nuclear reaction network from the Reaclib library based on an initial composition.
Defines classes for representing and managing nuclear reactions.