1. 阻尼振荡器
#include <ct/core/core.h>
int main(int argc, char** argv)
{
// a damped oscillator has two states, position and velocity
const size_t state_dim = ct::core::SecondOrderSystem::STATE_DIM; // = 2
// create a state
ct::core::StateVector<state_dim> x;
// we initialize it at a point with unit deflection and zero velocity
x(0) = 1.0;
x(1) = 0.0;
// create an oscillator, which is a predefined system in ct_core
double w_n = 10;
std::shared_ptr<ct::core::SecondOrderSystem> oscillator(new ct::core::SecondOrderSystem(w_n));
// create an integrator
ct::core::Integrator<state_dim> integrator(oscillator);
// simulate 1000 steps
double dt = 0.001;
ct::core::Time t0 = 0.0;
size_t nSteps = 1000;
integrator.integrate_n_steps(x, t0, nSteps, dt);
// print the new state
std::cout << "state after integration: " << x.transpose() << std::endl;
return 0;
}
2. 一个受摩擦力影响的简单质点
#include <ct/core/core.h> // as usual, include CT
// create a class that derives from ct::core::System
class Masspoint : public ct::core::System<2>
{
public:
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
static const size_t STATE_DIM = 2;
// constructor
Masspoint(double mass, double d) : mass_(mass), d_(d) {}
// copy constructor
Masspoint(const Masspoint& other) : mass_(other.mass_), d_(other.d_) {}
// destructor
~Masspoint() = default;
// clone method for deep copying
Masspoint* clone() const override
{
return new Masspoint(*this); // calls copy constructor
}
// The system dynamics. We override this method which gets called by e.g. the Integrator
void computeDynamics(const ct::core::StateVector<STATE_DIM>& x,
const ct::core::Time& t,
ct::core::StateVector<STATE_DIM>& derivative) override
{
// first part of state derivative is the velocity
derivative(0) = x(1);
// second part is the acceleration which is caused by damper forces
derivative(1) = -d_ / mass_ * x(1);
}
private:
double mass_;
double d_;
};
#include <ct/core/core.h>
#include <ct/core/examples/Masspoint.h>
int main(int argc, char** argv)
{
// a damped oscillator has two states, position and velocity
const size_t state_dim = Masspoint::STATE_DIM; // = 2
// create a state
ct::core::StateVector<state_dim> x;
// we initialize it at 0
x.setZero();
// create our mass point instance
double mass = 1.0;
double d = 0.01;
std::shared_ptr<Masspoint> masspoint(new Masspoint(mass, d));
// create an integrator
ct::core::Integrator<state_dim> integrator(masspoint);
// simulate 1000 steps
double dt = 0.001;
ct::core::Time t0 = 0.0;
size_t nSteps = 1000;
integrator.integrate_n_steps(x, t0, nSteps, dt);
// print the new state
std::cout << "state after integration: " << x.transpose() << std::endl;
return 0;
}
16万+

被折叠的 条评论
为什么被折叠?



