Coverage Report

Created: 2025-11-04 06:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quantlib/ql/methods/finitedifferences/solvers/fdm2dimsolver.cpp
Line
Count
Source
1
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*
4
 Copyright (C) 2010 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/interpolations/bicubicsplineinterpolation.hpp>
21
#include <ql/methods/finitedifferences/finitedifferencemodel.hpp>
22
#include <ql/methods/finitedifferences/meshers/fdmmesher.hpp>
23
#include <ql/methods/finitedifferences/operators/fdmlinearoplayout.hpp>
24
#include <ql/methods/finitedifferences/solvers/fdm2dimsolver.hpp>
25
#include <ql/methods/finitedifferences/stepconditions/fdmsnapshotcondition.hpp>
26
#include <ql/methods/finitedifferences/stepconditions/fdmstepconditioncomposite.hpp>
27
#include <ql/methods/finitedifferences/utilities/fdminnervaluecalculator.hpp>
28
#include <utility>
29
30
namespace QuantLib {
31
32
    Fdm2DimSolver::Fdm2DimSolver(const FdmSolverDesc& solverDesc,
33
                                 const FdmSchemeDesc& schemeDesc,
34
                                 ext::shared_ptr<FdmLinearOpComposite> op)
35
0
    : solverDesc_(solverDesc), schemeDesc_(schemeDesc), op_(std::move(op)),
36
0
      thetaCondition_(ext::make_shared<FdmSnapshotCondition>(
37
0
          0.99 * std::min(1.0 / 365.0,
38
0
                          solverDesc.condition->stoppingTimes().empty() ?
39
0
                              solverDesc.maturity :
40
0
                              solverDesc.condition->stoppingTimes().front()))),
41
0
      conditions_(FdmStepConditionComposite::joinConditions(thetaCondition_, solverDesc.condition)),
42
0
      initialValues_(solverDesc.mesher->layout()->size()),
43
0
      resultValues_(solverDesc.mesher->layout()->dim()[1], solverDesc.mesher->layout()->dim()[0]) {
44
45
0
        x_.reserve(solverDesc.mesher->layout()->dim()[0]);
46
0
        y_.reserve(solverDesc.mesher->layout()->dim()[1]);
47
48
0
        for (const auto& iter : *solverDesc.mesher->layout()) {
49
0
            initialValues_[iter.index()]
50
0
                 = solverDesc_.calculator->avgInnerValue(iter,
51
0
                                                         solverDesc.maturity);
52
53
0
            if (iter.coordinates()[1] == 0U) {
54
0
                x_.push_back(solverDesc.mesher->location(iter, 0));
55
0
            }
56
0
            if (iter.coordinates()[0] == 0U) {
57
0
                y_.push_back(solverDesc.mesher->location(iter, 1));
58
0
            }
59
0
        }
60
0
    }
Unexecuted instantiation: QuantLib::Fdm2DimSolver::Fdm2DimSolver(QuantLib::FdmSolverDesc const&, QuantLib::FdmSchemeDesc const&, boost::shared_ptr<QuantLib::FdmLinearOpComposite>)
Unexecuted instantiation: QuantLib::Fdm2DimSolver::Fdm2DimSolver(QuantLib::FdmSolverDesc const&, QuantLib::FdmSchemeDesc const&, boost::shared_ptr<QuantLib::FdmLinearOpComposite>)
61
62
63
0
    void Fdm2DimSolver::performCalculations() const {
64
0
        Array rhs(initialValues_.size());
65
0
        std::copy(initialValues_.begin(), initialValues_.end(), rhs.begin());
66
67
0
        FdmBackwardSolver(op_, solverDesc_.bcSet, conditions_, schemeDesc_)
68
0
            .rollback(rhs, solverDesc_.maturity, 0.0,
69
0
                      solverDesc_.timeSteps, solverDesc_.dampingSteps);
70
71
0
        std::copy(rhs.begin(), rhs.end(), resultValues_.begin());
72
0
        interpolation_ = ext::make_shared<BicubicSpline>(x_.begin(), x_.end(),
73
0
                              y_.begin(), y_.end(),
74
0
                              resultValues_);
75
0
    }
76
77
0
    Real Fdm2DimSolver::interpolateAt(Real x, Real y) const {
78
0
        calculate();
79
0
        return (*interpolation_)(x, y);
80
0
    }
81
82
0
    Real Fdm2DimSolver::thetaAt(Real x, Real y) const {
83
0
        if (conditions_->stoppingTimes().front() == 0.0)
84
0
            return Null<Real>();
85
86
0
        calculate();
87
0
        Matrix thetaValues(resultValues_.rows(), resultValues_.columns());
88
89
0
        const Array& rhs = thetaCondition_->getValues();
90
0
        std::copy(rhs.begin(), rhs.end(), thetaValues.begin());
91
92
0
        return (BicubicSpline(x_.begin(), x_.end(), y_.begin(), y_.end(),
93
0
                              thetaValues)(x, y) - interpolateAt(x, y))
94
0
              / thetaCondition_->getTime();
95
0
    }
96
97
98
0
    Real Fdm2DimSolver::derivativeX(Real x, Real y) const {
99
0
        calculate();
100
0
        return interpolation_->derivativeX(x, y);
101
0
    }
102
103
0
    Real Fdm2DimSolver::derivativeY(Real x, Real y) const {
104
0
        calculate();
105
0
        return interpolation_->derivativeY(x, y);
106
0
    }
107
108
0
    Real Fdm2DimSolver::derivativeXX(Real x, Real y) const {
109
0
        calculate();
110
0
        return interpolation_->secondDerivativeX(x, y);
111
0
    }
112
113
0
    Real Fdm2DimSolver::derivativeYY(Real x, Real y) const {
114
0
        calculate();
115
0
        return interpolation_->secondDerivativeY(x, y);
116
0
    }
117
118
0
    Real Fdm2DimSolver::derivativeXY(Real x, Real y) const {
119
0
        calculate();
120
0
        return interpolation_->derivativeXY(x, y);
121
0
    }
122
123
}