Dynamic Neural Field Composer 0.0.0
A C++20 library and interactive application for building and simulating Dynamic Neural Field (DNF) architectures.
Loading...
Searching...
No Matches
element.h
Go to the documentation of this file.
1#pragma once
2
3#include <vector>
4#include <string>
5#include <unordered_map>
6#include <memory>
7#include <ranges>
8#include <algorithm>
9#include <numeric>
10
12#include "tools/logger.h"
14
17
19{
27 class Element : public std::enable_shared_from_this<Element>
28 {
29 protected:
31 std::unordered_map<std::string, std::vector<double>> components;
32 std::unordered_map<std::shared_ptr<Element>, std::string> inputs;
33 std::unordered_map<std::shared_ptr<Element>, std::string> outputs;
34 private:
35 struct CachedInput { const double* src; std::size_t size; };
36 std::vector<CachedInput> cachedInputs;
37 double* inputPtr = nullptr;
38 std::size_t inputSize = 0;
39 public:
42 explicit Element(const ElementCommonParameters& parameters);
43
45 virtual void init() = 0;
46
51 virtual void changeDimensions(const ElementDimensions& newDimensions);
52
56 virtual void step(double t, double deltaT) = 0;
57
58 virtual std::shared_ptr<Element> clone() const = 0;
59
60 virtual ~Element() = default;
61
62 virtual std::string toString() const = 0;
63
64 void close();
65 void print() const;
66
70 virtual void addInput(const std::shared_ptr<Element>& inputElement,
71 const std::string& inputComponent = "output");
72
73 void removeInput(const std::string& inputElementId);
74 void removeInput(int uniqueId);
75 void removeInputs();
76 bool hasInput(const std::string& inputElementName, const std::string& inputComponent);
77 bool hasInput(int inputElementId, const std::string& inputComponent);
78
80 void updateInput();
81
83 void buildInputCache();
84
86 void removeOutput(const std::string& outputElementId);
87
89 void removeOutput(int uniqueId);
90
91 void removeOutputs();
92 bool hasOutput(const std::string& outputElementName, const std::string& outputComponent);
93 bool hasOutput(int outputElementId, const std::string& outputComponent);
94
95 int getMaxSpatialDimension() const;
96
98 int getSize() const;
99
101 double getStepSize() const;
102
104 int getUniqueIdentifier() const;
105 std::string getUniqueName() const;
106 void setUniqueName(const std::string& name);
107 ElementLabel getLabel() const;
108 bool hasOutput() const;
109 bool hasInput() const;
110
113 std::vector<double> getComponent(const std::string& componentName);
114
115 std::vector<double>* getComponentPtr(const std::string& componentName);
116 std::vector<std::string> getComponentList() const;
117
119 const std::unordered_map<std::string, std::vector<double>>* getComponents() const;
120
121 std::vector<std::shared_ptr<Element>> getInputs();
122
124 std::unordered_map<std::shared_ptr<Element>, std::string> getInputsAndComponents();
125
126 std::vector<std::shared_ptr<Element>> getOutputs();
127 };
128}
Abstract base class for all simulation elements.
Definition element.h:28
void print() const
Definition element.cpp:38
std::string getUniqueName() const
Definition element.cpp:245
std::vector< double > getComponent(const std::string &componentName)
Return a copy of the named component vector.
Definition element.cpp:265
void setUniqueName(const std::string &name)
Definition element.cpp:250
void close()
Definition element.cpp:29
double getStepSize() const
Return the spatial resolution (d_x).
Definition element.cpp:171
int getMaxSpatialDimension() const
Definition element.cpp:166
virtual void step(double t, double deltaT)=0
Advance the element by one time step.
std::vector< std::shared_ptr< Element > > getInputs()
Definition element.cpp:299
void buildInputCache()
Cache raw pointers to input component data. Call after all element init()s complete.
Definition element.cpp:141
std::unordered_map< std::string, std::vector< double > > components
Named data arrays (e.g. "output").
Definition element.h:31
bool hasOutput() const
Definition element.cpp:315
void removeOutput(const std::string &outputElementId)
Deregister this element as an input of outputElementId.
Definition element.cpp:232
bool hasInput() const
Definition element.cpp:320
const std::unordered_map< std::string, std::vector< double > > * getComponents() const
Return a read-only pointer to the full components map.
Definition element.cpp:294
void removeInput(const std::string &inputElementId)
Definition element.cpp:79
void removeOutputs()
Definition element.cpp:203
void updateInput()
Pull data from all registered input elements into this element's components.
Definition element.cpp:156
std::vector< std::string > getComponentList() const
Definition element.cpp:279
std::unordered_map< std::shared_ptr< Element >, std::string > getInputsAndComponents()
Return all inputs mapped to the component name they expose.
Definition element.cpp:310
std::vector< std::shared_ptr< Element > > getOutputs()
Definition element.cpp:325
virtual void changeDimensions(const ElementDimensions &newDimensions)
Resize all components to newDimensions and re-initialize.
Definition element.cpp:20
ElementCommonParameters commonParameters
Name, label, and spatial dimensions.
Definition element.h:30
ElementLabel getLabel() const
Definition element.cpp:260
std::unordered_map< std::shared_ptr< Element >, std::string > inputs
Upstream elements and the component they expose.
Definition element.h:32
int getUniqueIdentifier() const
Definition element.cpp:255
virtual std::shared_ptr< Element > clone() const =0
void removeInputs()
Definition element.cpp:129
int getSize() const
Return the number of spatial samples (size = round(x_max / d_x)).
Definition element.cpp:214
virtual void addInput(const std::shared_ptr< Element > &inputElement, const std::string &inputComponent="output")
Register inputElement as an upstream source for this element.
Definition element.cpp:43
std::vector< double > * getComponentPtr(const std::string &componentName)
Definition element.cpp:272
virtual void init()=0
Initialize the element (called once before the simulation loop).
ElementCommonParameters getElementCommonParameters() const
Definition element.cpp:187
virtual std::string toString() const =0
std::unordered_map< std::shared_ptr< Element >, std::string > outputs
Downstream elements that read this element's output.
Definition element.h:33
Definition element_parameters.h:10
ElementLabel
Definition element_parameters.h:12
Definition element_parameters.h:188
Definition element_parameters.h:159