Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
INode.hpp
Go to the documentation of this file.
1// -------------------------------------------------------//
2//
3// SHAMROCK code for hydrodynamics
4// Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me>
5// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1
6// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information
7//
8// -------------------------------------------------------//
9
10#pragma once
11
19#include "shambase/WithUUID.hpp"
20#include "shambase/memory.hpp"
23#include <memory>
24#include <vector>
25
26namespace shamrock::solvergraph {
27
29 class INode : public std::enable_shared_from_this<INode>,
30 public shambase::WithUUID<INode, u64> {
31
33 std::vector<std::shared_ptr<IEdge>> ro_edges;
35 std::vector<std::shared_ptr<IEdge>> rw_edges;
36
37 public:
38 INode() = default;
39
40 INode(const INode &) = delete;
41 INode &operator=(const INode &) = delete;
42
44 INode(INode &&) noexcept = default;
45
47 INode &operator=(INode &&) noexcept = default;
48
50 inline std::shared_ptr<INode> getptr_shared() { return shared_from_this(); }
52 inline std::weak_ptr<INode> getptr_weak() { return weak_from_this(); }
53
55 inline std::vector<std::shared_ptr<IEdge>> &get_ro_edges() { return ro_edges; }
57 inline std::vector<std::shared_ptr<IEdge>> &get_rw_edges() { return rw_edges; }
58
60 inline void __internal_set_ro_edges(std::vector<std::shared_ptr<IEdge>> new_ro_edges);
62 inline void __internal_set_rw_edges(std::vector<std::shared_ptr<IEdge>> new_rw_edges);
63
65 template<class Func>
66 void on_edge_ro_edges(Func &&f);
67
69 template<class Func>
70 void on_edge_rw_edges(Func &&f);
71
73 virtual ~INode() {
76 }
77
79 template<class T>
80 inline const T &get_ro_edge(int slot) {
81 return shambase::get_check_ref(std::dynamic_pointer_cast<T>(ro_edges.at(slot)));
82 }
83
85 template<class T>
86 inline T &get_rw_edge(int slot) {
87 return shambase::get_check_ref(std::dynamic_pointer_cast<T>(rw_edges.at(slot)));
88 }
89
91 inline const IEdge &get_ro_edge_base(int slot) {
92 return shambase::get_check_ref(ro_edges.at(slot));
93 }
94
95 inline const IEdge &get_ro_edge_base(int slot) const {
96 return shambase::get_check_ref(ro_edges.at(slot));
97 }
98
100 inline IEdge &get_rw_edge_base(int slot) {
101 return shambase::get_check_ref(rw_edges.at(slot));
102 }
103
104 inline const IEdge &get_rw_edge_base(int slot) const {
105 return shambase::get_check_ref(rw_edges.at(slot));
106 }
107
110
112 inline std::string get_dot_graph() { return get_dot_graph_partial(); };
113
115 inline std::string get_dot_graph_partial() { return _impl_get_dot_graph_partial(); };
116
120 inline std::string get_dot_graph_node_end() { return _impl_get_dot_graph_node_end(); };
121
123 inline std::string get_tex() { return _impl_get_tex(); };
125 inline std::string get_tex_partial() { return _impl_get_tex(); };
126
128 inline virtual std::string print_node_info() const {
129 std::string node_info = shambase::format("Node info :\n");
130 node_info += shambase::format(" - Node type : {}\n", typeid(*this).name());
131 node_info += shambase::format(" - Node UUID : {}\n", get_uuid());
132 node_info += shambase::format(" - Node label : {}\n", _impl_get_label());
133
134 auto append_edges_info = [&](const char *title, const auto &edges) {
135 node_info += shambase::format(" - {}: {}\n", title, edges.size());
136 for (const auto &edge : edges) {
137 const auto &e = *edge; // necessary to avoid -Wpotentially-evaluated-expression
138 node_info += shambase::format(
139 " - Edge ptr = {}, uuid = {}, label = {},\n type = {} \n",
140 static_cast<void *>(edge.get()),
141 edge->get_uuid(),
142 edge->get_label(),
143 typeid(e).name());
144 }
145 };
146
147 append_edges_info("Node Read Only edges", ro_edges);
148 append_edges_info("Node Read Write edges", rw_edges);
149
150 return node_info;
151 };
152
153 protected:
155 virtual void _impl_evaluate_internal() = 0;
156
158 virtual std::string _impl_get_label() const = 0;
159
161 virtual std::string _impl_get_dot_graph_partial() const;
163 virtual std::string _impl_get_dot_graph_node_start() const;
165 virtual std::string _impl_get_dot_graph_node_end() const;
166
168 virtual std::string _impl_get_tex() const = 0;
169 };
170
171 inline void INode::__internal_set_ro_edges(std::vector<std::shared_ptr<IEdge>> new_ro_edges) {
172 for (auto e : ro_edges) {
173 // shambase::get_check_ref(e).parent = {};
174 }
175 this->ro_edges = new_ro_edges;
176 for (auto e : ro_edges) {
177 // shambase::get_check_ref(e).parent = getptr_weak();
178 }
179 }
180
181 inline void INode::__internal_set_rw_edges(std::vector<std::shared_ptr<IEdge>> new_rw_edges) {
182 for (auto e : rw_edges) {
183 // shambase::get_check_ref(e).child = {};
184 }
185 this->rw_edges = new_rw_edges;
186 for (auto e : rw_edges) {
187 // shambase::get_check_ref(e).child = getptr_weak();
188 }
189 }
190
191 template<class Func>
192 inline void INode::on_edge_ro_edges(Func &&f) {
193 for (auto &in : ro_edges) {
195 }
196 }
197
198 template<class Func>
199 inline void INode::on_edge_rw_edges(Func &&f) {
200 for (auto &out : rw_edges) {
202 }
203 }
204
205 inline std::string INode::_impl_get_dot_graph_partial() const {
206 std::string node_str
207 = shambase::format("n_{} [label=\"{}\"];\n", this->get_uuid(), _impl_get_label());
208
209 std::string edge_str = "";
210 for (auto &in : ro_edges) {
211 edge_str += shambase::format(
212 "e_{} -> n_{} [style=\"dashed\", color=green];\n",
213 in->get_uuid(),
214 this->get_uuid());
215 edge_str += shambase::format(
216 "e_{} [label=\"{}\",shape=rect, style=filled];\n", in->get_uuid(), in->get_label());
217 }
218 for (auto &out : rw_edges) {
219 edge_str += shambase::format(
220 "n_{} -> e_{} [style=\"dashed\", color=red];\n", this->get_uuid(), out->get_uuid());
221 edge_str += shambase::format(
222 "e_{} [label=\"{}\",shape=rect, style=filled];\n",
223 out->get_uuid(),
224 out->get_label());
225 }
226
227 return shambase::format("{}{}", node_str, edge_str);
228 };
229
230 inline std::string INode::_impl_get_dot_graph_node_start() const {
231 return shambase::format("n_{}", this->get_uuid());
232 }
233 inline std::string INode::_impl_get_dot_graph_node_end() const {
234 return shambase::format("n_{}", this->get_uuid());
235 }
236
237} // namespace shamrock::solvergraph
238
239#define INODE_DECL_RO(type, name) const type &name;
240#define INODE_DECL_RW(type, name) type & name;
241#define INODE_PARAM_RO(type, name) std::shared_ptr<type> name,
242#define INODE_PARAM_RW(type, name) std::shared_ptr<type> name,
243#define INODE_PUSH_RO1(type, name) name,
244#define INODE_PUSH_RW1(type, name)
245#define INODE_PUSH_RO2(type, name)
246#define INODE_PUSH_RW2(type, name) name,
247#define INODE_GET_RO(type, name) get_ro_edge<type>(ro++),
248#define INODE_GET_RW(type, name) get_rw_edge<type>(rw++),
249
250#define EXPAND_NODE_EDGES(EDGES) \
251 \
252 struct Edges { \
253 EDGES(INODE_DECL_RO, INODE_DECL_RW) \
254 }; \
255 \
256 inline void set_edges( \
257 EDGES(INODE_PARAM_RO, INODE_PARAM_RW) SourceLocation loc = SourceLocation{}) { \
258 __shamrock_log_callsite(loc); \
259 \
260 __internal_set_ro_edges({EDGES(INODE_PUSH_RO1, INODE_PUSH_RW1)}); \
261 __internal_set_rw_edges({EDGES(INODE_PUSH_RO2, INODE_PUSH_RW2)}); \
262 } \
263 \
264 inline Edges get_edges() { \
265 int ro = 0; \
266 int rw = 0; \
267 return Edges{EDGES(INODE_GET_RO, INODE_GET_RW)}; \
268 }
A class that provides unique identifiers (UUID) to instances.
Definition WithUUID.hpp:37
u64 get_uuid() const
Get the uuid of the class.
Definition WithUUID.hpp:51
Inode is node between data edges, takes multiple inputs, multiple outputs.
Definition INode.hpp:30
void evaluate()
Evaluate the node.
Definition INode.hpp:109
INode & operator=(const INode &)=delete
would violate shared_from_this() & unique UUID
std::vector< std::shared_ptr< IEdge > > & get_ro_edges()
Get the read only edges.
Definition INode.hpp:55
virtual std::string _impl_get_dot_graph_node_start() const
get the dot graph of the node start
Definition INode.hpp:230
void on_edge_ro_edges(Func &&f)
Apply a function to the read only edges.
Definition INode.hpp:192
T & get_rw_edge(int slot)
Get a read write edge and cast it to the type T.
Definition INode.hpp:86
INode(INode &&) noexcept=default
would violate shared_from_this() & unique UUID
void __internal_set_rw_edges(std::vector< std::shared_ptr< IEdge > > new_rw_edges)
Set the read write edges.
Definition INode.hpp:181
virtual ~INode()
Destructor (virtual) & reset the edges.
Definition INode.hpp:73
std::string get_dot_graph_node_start()
Get the id of the node start in the dot graph.
Definition INode.hpp:118
std::string get_tex_partial()
Get the TeX of the node partial.
Definition INode.hpp:125
std::weak_ptr< INode > getptr_weak()
Get a weak pointer to this node.
Definition INode.hpp:52
std::string get_tex()
Get the TeX of the node.
Definition INode.hpp:123
virtual void _impl_evaluate_internal()=0
evaluate the node
IEdge & get_rw_edge_base(int slot)
Get a reference to a read write edge and cast it to the type IEdge.
Definition INode.hpp:100
void on_edge_rw_edges(Func &&f)
Apply a function to the read write edges.
Definition INode.hpp:199
std::string get_dot_graph()
Get the dot graph of the node (Currently only an alias to get_dot_graph_partial)
Definition INode.hpp:112
std::shared_ptr< INode > getptr_shared()
Get a shared pointer to this node.
Definition INode.hpp:50
virtual std::string print_node_info() const
print the node info
Definition INode.hpp:128
void __internal_set_ro_edges(std::vector< std::shared_ptr< IEdge > > new_ro_edges)
Set the read only edges.
Definition INode.hpp:171
std::string get_dot_graph_partial()
Get the dot graph of the subgraph corresponding to the node.
Definition INode.hpp:115
std::string get_dot_graph_node_end()
Get the id of the node end in the dot graph.
Definition INode.hpp:120
virtual std::string _impl_get_label() const =0
get the label of the node
const T & get_ro_edge(int slot)
Get a read only edge and cast it to the type T.
Definition INode.hpp:80
virtual std::string _impl_get_dot_graph_partial() const
get the dot graph of the node partial
Definition INode.hpp:205
std::vector< std::shared_ptr< IEdge > > & get_rw_edges()
Get the read write edges.
Definition INode.hpp:57
virtual std::string _impl_get_tex() const =0
get the tex of the node
virtual std::string _impl_get_dot_graph_node_end() const
get the dot graph of the node end
Definition INode.hpp:233
const IEdge & get_ro_edge_base(int slot)
Get a reference to a read only edge.
Definition INode.hpp:91
T & get_check_ref(const std::unique_ptr< T > &ptr, SourceLocation loc=SourceLocation())
Takes a std::unique_ptr and returns a reference to the object it holds. It throws a std::runtime_erro...
Definition memory.hpp:110
STL namespace.
This file contains the definition for the stacktrace related functionality.