Coverage Report

Created: 2025-08-05 06:45

/src/quantlib/ql/methods/finitedifferences/boundarycondition.hpp
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
 <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
/*! \file boundarycondition.hpp
21
    \brief boundary conditions for differential operators
22
*/
23
24
#ifndef quantlib_boundary_condition_hpp
25
#define quantlib_boundary_condition_hpp
26
27
#include <ql/utilities/null.hpp>
28
#include <ql/methods/finitedifferences/tridiagonaloperator.hpp>
29
30
namespace QuantLib {
31
32
    //! Abstract boundary condition class for finite difference problems
33
    /*! \ingroup findiff */
34
    template <class Operator>
35
    class BoundaryCondition {
36
      public:
37
        // types and enumerations
38
        typedef Operator operator_type;
39
        typedef typename Operator::array_type array_type;
40
        //! \todo Generalize for n-dimensional conditions
41
        enum Side { None, Upper, Lower };
42
        // destructor
43
0
        virtual ~BoundaryCondition() = default;
Unexecuted instantiation: QuantLib::BoundaryCondition<QuantLib::TridiagonalOperator>::~BoundaryCondition()
Unexecuted instantiation: QuantLib::BoundaryCondition<QuantLib::FdmLinearOp>::~BoundaryCondition()
44
        // interface
45
        /*! This method modifies an operator \f$ L \f$ before it is
46
            applied to an array \f$ u \f$ so that \f$ v = Lu \f$ will
47
            satisfy the given condition. */
48
        virtual void applyBeforeApplying(operator_type&) const = 0;
49
        /*! This method modifies an array \f$ u \f$ so that it satisfies
50
            the given condition. */
51
        virtual void applyAfterApplying(array_type&) const = 0;
52
        /*! This method modifies an operator \f$ L \f$ before the linear
53
            system \f$ Lu' = u \f$ is solved so that \f$ u' \f$ will
54
            satisfy the given condition. */
55
        virtual void applyBeforeSolving(operator_type&,
56
                                        array_type& rhs) const = 0;
57
        /*! This method modifies an array \f$ u \f$ so that it satisfies
58
            the given condition. */
59
        virtual void applyAfterSolving(array_type&) const = 0;
60
        /*! This method sets the current time for time-dependent
61
            boundary conditions. */
62
        virtual void setTime(Time t) = 0;
63
    };
64
65
    // Time-independent boundary conditions for tridiagonal operators
66
67
    //! Neumann boundary condition (i.e., constant derivative)
68
    /*! \warning The value passed must not be the value of the derivative.
69
                 Instead, it must be comprehensive of the grid step
70
                 between the first two points--i.e., it must be the
71
                 difference between f[0] and f[1].
72
        \todo generalize to time-dependent conditions.
73
74
        \ingroup findiff
75
    */
76
    class NeumannBC : public BoundaryCondition<TridiagonalOperator> {
77
      public:
78
        NeumannBC(Real value, Side side);
79
        // interface
80
        void applyBeforeApplying(TridiagonalOperator&) const override;
81
        void applyAfterApplying(Array&) const override;
82
        void applyBeforeSolving(TridiagonalOperator&, Array& rhs) const override;
83
        void applyAfterSolving(Array&) const override;
84
0
        void setTime(Time) override {}
85
86
      private:
87
        Real value_;
88
        Side side_;
89
    };
90
91
    //! Dirichlet boundary condition (i.e., constant value)
92
    /*! \todo generalize to time-dependent conditions.
93
94
        \ingroup findiff
95
    */
96
    class DirichletBC : public BoundaryCondition<TridiagonalOperator> {
97
      public:
98
        DirichletBC(Real value, Side side);
99
        // interface
100
        void applyBeforeApplying(TridiagonalOperator&) const override;
101
        void applyAfterApplying(Array&) const override;
102
        void applyBeforeSolving(TridiagonalOperator&, Array& rhs) const override;
103
        void applyAfterSolving(Array&) const override;
104
0
        void setTime(Time) override {}
105
106
      private:
107
        Real value_;
108
        Side side_;
109
    };
110
111
}
112
113
114
115
116
#endif