Coverage Report

Created: 2025-09-04 07:11

/src/quantlib/ql/methods/finitedifferences/boundarycondition.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*
4
 Copyright (C) 2000, 2001, 2002, 2003 RiskMap srl
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/methods/finitedifferences/boundarycondition.hpp>
21
22
namespace QuantLib {
23
24
    NeumannBC::NeumannBC(Real value, NeumannBC::Side side)
25
0
    : value_(value), side_(side) {}
26
27
0
    void NeumannBC::applyBeforeApplying(TridiagonalOperator& L) const {
28
0
        switch (side_) {
29
0
          case Lower:
30
0
            L.setFirstRow(-1.0,1.0);
31
0
            break;
32
0
          case Upper:
33
0
            L.setLastRow(-1.0,1.0);
34
0
            break;
35
0
          default:
36
0
            QL_FAIL("unknown side for Neumann boundary condition");
37
0
        }
38
0
    }
39
40
0
    void NeumannBC::applyAfterApplying(Array& u) const {
41
0
        switch (side_) {
42
0
          case Lower:
43
0
            u[0] = u[1] - value_;
44
0
            break;
45
0
          case Upper:
46
0
            u[u.size()-1] = u[u.size()-2] + value_;
47
0
            break;
48
0
          default:
49
0
            QL_FAIL("unknown side for Neumann boundary condition");
50
0
        }
51
0
    }
52
53
    void NeumannBC::applyBeforeSolving(TridiagonalOperator& L,
54
0
                                       Array& rhs) const {
55
0
        switch (side_) {
56
0
          case Lower:
57
0
            L.setFirstRow(-1.0,1.0);
58
0
            rhs[0] = value_;
59
0
            break;
60
0
          case Upper:
61
0
            L.setLastRow(-1.0,1.0);
62
0
            rhs[rhs.size()-1] = value_;
63
0
            break;
64
0
          default:
65
0
            QL_FAIL("unknown side for Neumann boundary condition");
66
0
        }
67
0
    }
68
69
0
    void NeumannBC::applyAfterSolving(Array&) const {}
70
71
72
73
    DirichletBC::DirichletBC(Real value, DirichletBC::Side side)
74
0
    : value_(value), side_(side) {}
75
76
0
    void DirichletBC::applyBeforeApplying(TridiagonalOperator& L) const {
77
0
        switch (side_) {
78
0
          case Lower:
79
0
            L.setFirstRow(1.0,0.0);
80
0
            break;
81
0
          case Upper:
82
0
            L.setLastRow(0.0,1.0);
83
0
            break;
84
0
          default:
85
0
            QL_FAIL("unknown side for Neumann boundary condition");
86
0
        }
87
0
    }
88
89
0
    void DirichletBC::applyAfterApplying(Array& u) const {
90
0
        switch (side_) {
91
0
          case Lower:
92
0
            u[0] = value_;
93
0
            break;
94
0
          case Upper:
95
0
            u[u.size()-1] = value_;
96
0
            break;
97
0
          default:
98
0
            QL_FAIL("unknown side for Neumann boundary condition");
99
0
        }
100
0
    }
101
102
    void DirichletBC::applyBeforeSolving(TridiagonalOperator& L,
103
0
                                         Array& rhs) const {
104
0
        switch (side_) {
105
0
          case Lower:
106
0
            L.setFirstRow(1.0,0.0);
107
0
            rhs[0] = value_;
108
0
            break;
109
0
          case Upper:
110
0
            L.setLastRow(0.0,1.0);
111
0
            rhs[rhs.size()-1] = value_;
112
0
            break;
113
0
          default:
114
0
            QL_FAIL("unknown side for Neumann boundary condition");
115
0
        }
116
0
    }
117
118
0
    void DirichletBC::applyAfterSolving(Array&) const {}
119
120
}