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
simulation.h
Go to the documentation of this file.
1#pragma once
2
3#include <vector>
4#include <memory>
5#include <string>
6#include <filesystem>
7#include <chrono>
8
9#include "elements/element.h"
11#include "tools/utils.h"
13
16
17namespace dnf_composer
18{
19 class Simulation;
20
28 std::shared_ptr<Simulation> createSimulation(const std::string& identifier = "", double deltaT = 1, double tZero = 0, double t = 0);
29
38 class Simulation : public std::enable_shared_from_this<Simulation>
39 {
40 protected:
42 bool paused;
43 std::vector<std::shared_ptr<element::Element>> elements;
44 std::string uniqueIdentifier;
45 public:
46 double deltaT;
47 double tZero;
48 double t;
49 public:
50
56 explicit Simulation(const std::string& identifier = "", double deltaT = 1, double tZero = 0, double t = 0);
57 Simulation(const Simulation& other);
58 Simulation& operator=(const Simulation& other);
59 Simulation(Simulation&& other) noexcept;
60 Simulation& operator=(Simulation&&) noexcept;
61
63 void init();
64
66 void step();
67
70 void run(double runTime);
71
74 void runForRealTime(double milliseconds);
75
77 void close();
78
80 void pause();
81
82 void resume();
83
85 void clean();
86
89 void save(const std::string& savePath = {});
90
93 void read(const std::string& readPath = {});
94
95 void addElement(const std::shared_ptr<element::Element>& element);
96
99 void removeElement(const std::string& elementId);
100
104 void resetElement(const std::string& idOfElementToReset, const std::shared_ptr<element::Element>& newElement);
105
109 void changeDimensions(const std::string& elementId, const element::ElementDimensions& newDimensions);
110
112 void renameElement(const std::string& oldName, const std::string& newName);
113
118 void createInteraction(const std::string& stimulusElementId, const std::string& stimulusComponent,
119 const std::string& receivingElementId) const;
120
121 void setUniqueIdentifier(const std::string& id);
122 void setDeltaT(double deltaT);
123 std::vector<std::shared_ptr<element::Element>> getElements() const;
124 std::string getUniqueIdentifier() const;
125
128 std::shared_ptr<element::Element> getElement(const std::string& id) const;
129
132 std::shared_ptr<element::Element> getElement(int index) const;
133
134 std::vector<double> getComponent(const std::string& id, const std::string& componentName) const;
135 std::vector<double>* getComponentPtr(const std::string& id, const std::string& componentName) const;
136 int getNumberOfElements() const;
137
141 std::vector<std::shared_ptr<element::Element>> getElementsThatHaveSpecifiedElementAsInput(const std::string& specifiedElement,
142 const std::string& inputComponent = "output") const;
143
144 int getHighestElementIndex() const;
145
147 std::string getIdentifier() const;
148
149 double getDeltaT() const;
150 double getTZero() const;
151 double getT() const;
152
154 std::chrono::nanoseconds getLastStepDuration() const;
155
157 std::chrono::nanoseconds getTotalRunDuration() const;
158
159 bool componentExists(const std::string& id, const std::string& componentName) const;
160
162 bool isInitialized() const;
163
165 bool isPaused() const;
166
167 ~Simulation() = default;
168 std::chrono::nanoseconds lastStepDuration{ 0 };
169 std::chrono::nanoseconds accumulatedRunDuration{ 0 };
170 std::chrono::steady_clock::time_point runSegmentStart{};
171
174 void setMeasureStepDuration(bool enable) { measureStepDuration = enable; }
175 bool getMeasureStepDuration() const { return measureStepDuration; }
176
180 SimulationRecorder& getRecorder() { return recorder; }
181
182 private:
183 bool measureStepDuration = true;
184 SimulationRecorder recorder;
185 void generateUniqueIdentifier();
186 };
187}
Manages ongoing time-series recordings and snapshot exports of element component data for a simulatio...
Definition simulation_recorder.h:32
Manages the element registry and drives the simulation loop.
Definition simulation.h:39
void runForRealTime(double milliseconds)
Run the simulation in real-time for milliseconds wall-clock ms.
Definition simulation.cpp:257
std::string uniqueIdentifier
Human-readable simulation name.
Definition simulation.h:44
std::string getUniqueIdentifier() const
Definition simulation.cpp:417
void setUniqueIdentifier(const std::string &id)
Definition simulation.cpp:402
void pause()
Pause the simulation (subsequent step() calls are no-ops).
Definition simulation.cpp:195
bool getMeasureStepDuration() const
Definition simulation.h:175
void changeDimensions(const std::string &elementId, const element::ElementDimensions &newDimensions)
Disconnect all connections from elementId and resize it to newDimensions.
Definition simulation.cpp:346
std::string getIdentifier() const
Return the simulation's unique identifier (alias for getUniqueIdentifier()).
Definition simulation.cpp:490
std::chrono::nanoseconds lastStepDuration
Definition simulation.h:168
std::vector< double > * getComponentPtr(const std::string &id, const std::string &componentName) const
Definition simulation.cpp:453
void setDeltaT(double deltaT)
Definition simulation.cpp:407
void resetElement(const std::string &idOfElementToReset, const std::shared_ptr< element::Element > &newElement)
Replace an existing element with a new one, preserving connections.
Definition simulation.cpp:322
void renameElement(const std::string &oldName, const std::string &newName)
Rename an element. No-op if oldName does not exist or newName is already in use.
Definition simulation.cpp:357
double deltaT
Integration step size.
Definition simulation.h:46
double getT() const
Definition simulation.cpp:505
bool paused
True while the simulation is paused.
Definition simulation.h:42
double t
Current simulation time.
Definition simulation.h:48
void read(const std::string &readPath={})
Deserialize the simulation state from a JSON file.
Definition simulation.cpp:227
std::vector< std::shared_ptr< element::Element > > elements
Ordered element registry.
Definition simulation.h:43
void close()
Release resources of all elements and reset timing state.
Definition simulation.cpp:182
std::vector< double > getComponent(const std::string &id, const std::string &componentName) const
Definition simulation.cpp:447
void clean()
Remove all elements and reset the simulation to its initial state.
Definition simulation.cpp:210
std::vector< std::shared_ptr< element::Element > > getElements() const
Definition simulation.cpp:558
std::shared_ptr< element::Element > getElement(const std::string &id) const
Retrieve an element by its unique name. Throws if not found.
Definition simulation.cpp:422
int getNumberOfElements() const
Definition simulation.cpp:459
std::chrono::nanoseconds getLastStepDuration() const
Return the wall-clock duration of the most recent step() call.
Definition simulation.cpp:510
void addElement(const std::shared_ptr< element::Element > &element)
Definition simulation.cpp:281
void removeElement(const std::string &elementId)
Remove and destroy the element with the given unique name.
Definition simulation.cpp:301
Simulation & operator=(const Simulation &other)
Definition simulation.cpp:52
void init()
Initialize all registered elements. Must be called before step().
Definition simulation.cpp:125
void setMeasureStepDuration(bool enable)
Enable or disable per-step wall-clock timing (default: enabled). Disable for headless/benchmark runs ...
Definition simulation.h:174
std::chrono::nanoseconds accumulatedRunDuration
Definition simulation.h:169
int getHighestElementIndex() const
Definition simulation.cpp:477
double tZero
Start time.
Definition simulation.h:47
void resume()
Definition simulation.cpp:203
SimulationRecorder & getRecorder()
Access the recorder to start/stop time-series recordings or take snapshots.
Definition simulation.h:180
bool isPaused() const
Return true if the simulation is currently paused.
Definition simulation.cpp:543
void save(const std::string &savePath={})
Serialize the simulation and its elements to a JSON file.
Definition simulation.cpp:221
void createInteraction(const std::string &stimulusElementId, const std::string &stimulusComponent, const std::string &receivingElementId) const
Wire stimulusElementId's stimulusComponent as input to receivingElementId.
Definition simulation.cpp:375
std::vector< std::shared_ptr< element::Element > > getElementsThatHaveSpecifiedElementAsInput(const std::string &specifiedElement, const std::string &inputComponent="output") const
Return all elements that list specifiedElement as an input.
Definition simulation.cpp:464
void run(double runTime)
Run the simulation for runTime milliseconds (blocking).
Definition simulation.cpp:235
std::chrono::steady_clock::time_point runSegmentStart
Definition simulation.h:170
void step()
Advance all elements by one deltaT.
Definition simulation.cpp:158
bool isInitialized() const
Return true if init() has been called and the simulation is ready.
Definition simulation.cpp:538
std::chrono::nanoseconds getTotalRunDuration() const
Return total wall-clock time since the last init() call.
Definition simulation.cpp:515
bool componentExists(const std::string &id, const std::string &componentName) const
Definition simulation.cpp:525
double getTZero() const
Definition simulation.cpp:500
bool initialized
True after init() has been called.
Definition simulation.h:41
double getDeltaT() const
Definition simulation.cpp:495
std::shared_ptr< Simulation > createSimulation(const std::string &identifier="", double deltaT=1, double tZero=0, double t=0)
Factory helper — create a Simulation with the given parameters.
Definition simulation.cpp:9
Definition application.h:20