28 std::shared_ptr<Simulation>
createSimulation(
const std::string& identifier =
"",
double deltaT = 1,
double tZero = 0,
double t = 0);
38 class Simulation :
public std::enable_shared_from_this<Simulation>
43 std::vector<std::shared_ptr<element::Element>>
elements;
56 explicit Simulation(
const std::string& identifier =
"",
double deltaT = 1,
double tZero = 0,
double t = 0);
70 void run(
double runTime);
89 void save(const std::
string& savePath = {});
93 void read(
const std::string& readPath = {});
95 void addElement(
const std::shared_ptr<element::Element>& element);
104 void resetElement(
const std::string& idOfElementToReset,
const std::shared_ptr<element::Element>& newElement);
109 void changeDimensions(
const std::string& elementId,
const element::ElementDimensions& newDimensions);
112 void renameElement(
const std::string& oldName,
const std::string& newName);
118 void createInteraction(
const std::string& stimulusElementId,
const std::string& stimulusComponent,
119 const std::string& receivingElementId)
const;
123 std::vector<std::shared_ptr<element::Element>>
getElements()
const;
128 std::shared_ptr<element::Element>
getElement(
const std::string&
id)
const;
132 std::shared_ptr<element::Element>
getElement(
int index)
const;
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;
142 const std::string& inputComponent =
"output")
const;
159 bool componentExists(
const std::string&
id,
const std::string& componentName)
const;
183 bool measureStepDuration =
true;
185 void generateUniqueIdentifier();
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