/* -------------------------------------------------------------------------- * * SimTK Core: SimTK Simmath(tm) * * -------------------------------------------------------------------------- * * This is part of the SimTK Core biosimulation toolkit originating from * * Simbios, the NIH National Center for Physics-Based Simulation of * * Biological Structures at Stanford, funded under the NIH Roadmap for * * Medical Research, grant U54 GM072970. See https://simtk.org. * * * * Portions copyright (c) 2006-10 Stanford University and the Authors. * * Authors: Michael Sherman * * Contributors: * * * * Permission is hereby granted, free of charge, to any person obtaining a * * copy of this software and associated documentation files (the "Software"), * * to deal in the Software without restriction, including without limitation * * the rights to use, copy, modify, merge, publish, distribute, sublicense, * * and/or sell copies of the Software, and to permit persons to whom the * * Software is furnished to do so, subject to the following conditions: * * * * The above copyright notice and this permission notice shall be included in * * all copies or substantial portions of the Software. * * * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE * * USE OR OTHER DEALINGS IN THE SOFTWARE. * * -------------------------------------------------------------------------- */ /** @file * This is the private (library side) implementation of the * RungeKuttaMersonIntegrator and RungeKuttaMersonIntegratorRep class which * is a concrete class implementing the abstract IntegratorRep. */ #include "SimTKcommon.h" #include "simmath/Integrator.h" #include "simmath/RungeKuttaMersonIntegrator.h" #include "IntegratorRep.h" #include "RungeKuttaMersonIntegratorRep.h" #include #include using namespace SimTK; //------------------------------------------------------------------------------ // RUNGE KUTTA MERSON INTEGRATOR //------------------------------------------------------------------------------ RungeKuttaMersonIntegrator::RungeKuttaMersonIntegrator(const System& sys) { rep = new RungeKuttaMersonIntegratorRep(this, sys); } RungeKuttaMersonIntegrator::~RungeKuttaMersonIntegrator() { delete rep; } //------------------------------------------------------------------------------ // RUNGE KUTTA MERSON INTEGRATOR REP //------------------------------------------------------------------------------ RungeKuttaMersonIntegratorRep::RungeKuttaMersonIntegratorRep (Integrator* handle, const System& sys) : AbstractIntegratorRep(handle, sys, 4, 4, "RungeKuttaMerson", true) { } // For a discussion of the Runge-Kutta-Merson method, see Hairer, // Norsett & Wanner, Solving ODEs I, 2nd rev. ed. pp. 166-8, and table 4.1 // on page 167. This is the Butcher diagram: // // 0| // 1/3| 1/3 // 1/3| 1/6 1/6 // 1/2| 1/8 0 3/8 // 1| 1/2 0 -3/2 2 // --|---------------------------- // 1| 1/6 0 0 2/3 1/6 propagated 4th order solution // --|---------------------------- // 1| 1/10 0 3/10 2/5 1/5 0 embedded 3rd order solution // // This is a 5-stage, first-same-as-last (FSAL) 4th order method which gives // us an embedded 3rd order method as well, so we can extract a 4th-order // error estimate for the 3rd-order result, which error estimate can then be // used for step size control, since it will behave as h^4. We then propagate // the 4th order result (whose error is unknown), which Hairer calls "local // extrapolation". We call the initial state (t0,y0) and want (t0+h,y1). We // are given the initial derivative f0=f(t0,y0), which most likely is left // over from an evaluation at the end of the last step. // // We will call the derivatives at stage f1,f2,f3,f4 but these are done with // only two temporaries fa and fb. (What we're calling "f" Hairer calls "k".) bool RungeKuttaMersonIntegratorRep::attemptODEStep (Real t0, Real t1, const Vector& q0, const Vector& qdot0, const Vector& qdotdot0, const Vector& u0, const Vector& udot0, const Vector& z0, const Vector& zdot0, Vector& y1err, int& errOrder, int& numIterations) { assert(t1 > t0); statsStepsAttempted++; errOrder = 4; const Vector& y0 = getPreviousY(); const Vector& f0 = getPreviousYDot(); if (ytmp[0].size() != y0.size()) for (int i=0; i