|
| 1 | +#include <fluxgraph/core/namespace.hpp> |
| 2 | +#include <fluxgraph/core/signal_store.hpp> |
| 3 | +#include <fluxgraph/engine.hpp> |
| 4 | +#include <fluxgraph/graph/compiler.hpp> |
| 5 | +#include <iomanip> |
| 6 | +#include <iostream> |
| 7 | + |
| 8 | +int main() { |
| 9 | + fluxgraph::SignalNamespace sig_ns; |
| 10 | + fluxgraph::FunctionNamespace func_ns; |
| 11 | + fluxgraph::SignalStore store; |
| 12 | + |
| 13 | + fluxgraph::GraphSpec spec; |
| 14 | + |
| 15 | + fluxgraph::ModelSpec model; |
| 16 | + model.id = "msd"; |
| 17 | + model.type = "mass_spring_damper"; |
| 18 | + model.params["position_signal"] = std::string("msd.position_m"); |
| 19 | + model.params["velocity_signal"] = std::string("msd.velocity_m_s"); |
| 20 | + model.params["force_signal"] = std::string("msd.force_N"); |
| 21 | + model.params["mass"] = 1.0; // kg |
| 22 | + model.params["damping_coeff"] = 2.0; // N*s/m |
| 23 | + model.params["spring_constant"] = 20.0; // N/m |
| 24 | + model.params["initial_position"] = 0.0; // m |
| 25 | + model.params["initial_velocity"] = 0.0; // m/s |
| 26 | + model.params["integration_method"] = std::string("rk4"); |
| 27 | + spec.models.push_back(model); |
| 28 | + |
| 29 | + fluxgraph::GraphCompiler compiler; |
| 30 | + auto program = compiler.compile(spec, sig_ns, func_ns); |
| 31 | + |
| 32 | + fluxgraph::Engine engine; |
| 33 | + engine.load(std::move(program)); |
| 34 | + |
| 35 | + const auto force_id = sig_ns.resolve("msd.force_N"); |
| 36 | + const auto position_id = sig_ns.resolve("msd.position_m"); |
| 37 | + const auto velocity_id = sig_ns.resolve("msd.velocity_m_s"); |
| 38 | + |
| 39 | + std::cout << "Mass-Spring-Damper Step Response\n"; |
| 40 | + std::cout << "================================\n"; |
| 41 | + std::cout << std::fixed << std::setprecision(4); |
| 42 | + |
| 43 | + constexpr double dt = 0.01; |
| 44 | + for (double t = 0.0; t <= 5.0; t += dt) { |
| 45 | + const double force = (t < 2.5) ? 1.0 : 0.0; |
| 46 | + store.write(force_id, force, "N"); |
| 47 | + |
| 48 | + engine.tick(dt, store); |
| 49 | + |
| 50 | + if (static_cast<int>(t / dt) % 50 == 0) { |
| 51 | + const double x = store.read_value(position_id); |
| 52 | + const double v = store.read_value(velocity_id); |
| 53 | + std::cout << "t=" << std::setw(5) << t << " s " |
| 54 | + << "F=" << std::setw(6) << force << " N " |
| 55 | + << "x=" << std::setw(8) << x << " m " |
| 56 | + << "v=" << std::setw(8) << v << " m/s\n"; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + return 0; |
| 61 | +} |
| 62 | + |
0 commit comments