/src/quantlib/ql/methods/finitedifferences/schemes/methodoflinesscheme.cpp
Line | Count | Source |
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 | | <https://www.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 <utility> |
23 | | |
24 | | namespace QuantLib { |
25 | | |
26 | | MethodOfLinesScheme::MethodOfLinesScheme(const Real eps, |
27 | | const Real relInitStepSize, |
28 | | ext::shared_ptr<FdmLinearOpComposite> map, |
29 | | const bc_set& bcSet) |
30 | 0 | : dt_(Null<Real>()), eps_(eps), relInitStepSize_(relInitStepSize), map_(std::move(map)), |
31 | 0 | bcSet_(bcSet) {} |
32 | | |
33 | | |
34 | 0 | std::vector<Real> MethodOfLinesScheme::apply(Time t, const std::vector<Real>& u) const { |
35 | 0 | map_->setTime(t, t + 0.0001); |
36 | 0 | bcSet_.applyBeforeApplying(*map_); |
37 | |
|
38 | 0 | const Array dxdt = -map_->apply(Array(u.begin(), u.end())); |
39 | |
|
40 | 0 | return std::vector<Real>(dxdt.begin(), dxdt.end()); |
41 | 0 | } |
42 | | |
43 | 0 | void MethodOfLinesScheme::step(array_type& a, Time t) { |
44 | 0 | QL_REQUIRE(t-dt_ > -1e-8, "a step towards negative time given"); |
45 | | |
46 | 0 | const std::vector<Real> v = |
47 | 0 | AdaptiveRungeKutta<Real>(eps_, relInitStepSize_*dt_)( |
48 | 0 | [&](Time _t, const std::vector<Real>& _u){ return apply(_t, _u); }, |
49 | 0 | std::vector<Real>(a.begin(), a.end()), |
50 | 0 | t, std::max(0.0, t-dt_)); |
51 | |
|
52 | 0 | Array y(v.begin(), v.end()); |
53 | |
|
54 | 0 | bcSet_.applyAfterSolving(y); |
55 | |
|
56 | 0 | a = y; |
57 | 0 | } |
58 | | |
59 | 0 | void MethodOfLinesScheme::setStep(Time dt) { |
60 | 0 | dt_ = dt; |
61 | 0 | } |
62 | | } |