1 /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 3 /* 4 Copyright (C) 2018 Klaus Spanderen 5 6 This file is part of QuantLib, a free-software/open-source library 7 for financial quantitative analysts and developers - http://quantlib.org/ 8 9 QuantLib is free software: you can redistribute it and/or modify it 10 under the terms of the QuantLib license. You should have received a 11 copy of the license along with this program; if not, please email 12 <quantlib-dev@lists.sf.net>. The license is also available online at 13 <http://quantlib.org/license.shtml>. 14 15 This program is distributed in the hope that it will be useful, but WITHOUT 16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 17 FOR A PARTICULAR PURPOSE. See the license for more details. 18 */ 19 20 #include <ql/math/ode/adaptiverungekutta.hpp> 21 #include <ql/methods/finitedifferences/schemes/methodoflinesscheme.hpp> 22 #include <ql/functional.hpp> 23 24 namespace QuantLib { 25 MethodOfLinesScheme(const Real eps,const Real relInitStepSize,const ext::shared_ptr<FdmLinearOpComposite> & map,const bc_set & bcSet)26 MethodOfLinesScheme::MethodOfLinesScheme( 27 const Real eps, 28 const Real relInitStepSize, 29 const ext::shared_ptr<FdmLinearOpComposite> & map, 30 const bc_set& bcSet) 31 : dt_(Null<Real>()), 32 eps_(eps), 33 relInitStepSize_(relInitStepSize), 34 map_(map), 35 bcSet_(bcSet) { 36 } 37 38 39 Disposable<std::vector<Real> > apply(Time t,const std::vector<Real> & u) const40 MethodOfLinesScheme::apply(Time t, const std::vector<Real>& u) const { 41 map_->setTime(t, t + 0.0001); 42 bcSet_.applyBeforeApplying(*map_); 43 44 const Array dxdt = -map_->apply(Array(u.begin(), u.end())); 45 46 std::vector<Real> retVal(dxdt.begin(), dxdt.end()); 47 return retVal; 48 } 49 step(array_type & a,Time t)50 void MethodOfLinesScheme::step(array_type& a, Time t) { 51 using namespace ext::placeholders; 52 QL_REQUIRE(t-dt_ > -1e-8, "a step towards negative time given"); 53 54 const std::vector<Real> v = 55 AdaptiveRungeKutta<Real>(eps_, relInitStepSize_*dt_)( 56 ext::bind(&MethodOfLinesScheme::apply, this, _1, _2), 57 std::vector<Real>(a.begin(), a.end()), 58 t, std::max(0.0, t-dt_)); 59 60 Array y(v.begin(), v.end()); 61 62 bcSet_.applyAfterSolving(y); 63 64 a = y; 65 } 66 setStep(Time dt)67 void MethodOfLinesScheme::setStep(Time dt) { 68 dt_ = dt; 69 } 70 } 71