1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
/*
* Copyright (C) 2018 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <cassert>
#include <ignition/plugin/Register.hh>
#include "integrators.hh"
namespace ignition{
namespace plugin {
namespace examples {
namespace ForwardEuler {
/// \brief Add two vectors
std::vector<double> add(const std::vector<double> &v1,
const std::vector<double> &v2)
{
assert(v1.size() == v2.size());
std::vector<double> result = v1;
for(std::size_t i=0; i < v1.size(); ++i)
{
result[i] += v2[i];
}
return result;
}
/// \brief Multiply a vector by a scalar
std::vector<double> times(const double s,
const std::vector<double> &v)
{
std::vector<double> result = v;
for(double& c : result)
c *= s;
return result;
}
/// \brief Forward Euler implementation of a numerical integrator
class Integrator : public ignition::plugin::examples::NumericalIntegrator
{
// Documentation inherited
public: void SetFunction(
const std::function<Derivative(Time, const State&)> &_func) override
{
function = _func;
}
// Documentation inherited
public: bool SetTimeStep(TimeStep _step) override
{
if(_step <= 0.0)
return false;
timeStep = _step;
return true;
}
// Documentation inherited
public: TimeStep GetTimeStep() const override
{
return timeStep;
}
// Documentation inherited
public: State Integrate(Time _currentTime, const State &_state) const override
{
const Time tn = _currentTime;
const TimeStep h = timeStep;
const State &yn = _state;
return add(yn, times(h, function(tn, yn)));
}
/// \brief The time step that will be used when integrating
private: TimeStep timeStep;
/// \brief The function that represents the system of ordinary differential
/// equations.
private: SystemODE function;
};
IGNITION_ADD_PLUGIN(Integrator, NumericalIntegrator)
}
}
}
}
|