/src/quantlib/ql/methods/finitedifferences/utilities/fdmdirichletboundary.cpp
Line | Count | Source |
1 | | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* |
4 | | Copyright (C) 2008 Andreas Gaida |
5 | | Copyright (C) 2008 Ralph Schreyer |
6 | | Copyright (C) 2008 Klaus Spanderen |
7 | | |
8 | | This file is part of QuantLib, a free-software/open-source library |
9 | | for financial quantitative analysts and developers - http://quantlib.org/ |
10 | | |
11 | | QuantLib is free software: you can redistribute it and/or modify it |
12 | | under the terms of the QuantLib license. You should have received a |
13 | | copy of the license along with this program; if not, please email |
14 | | <quantlib-dev@lists.sf.net>. The license is also available online at |
15 | | <https://www.quantlib.org/license.shtml>. |
16 | | |
17 | | This program is distributed in the hope that it will be useful, but WITHOUT |
18 | | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
19 | | FOR A PARTICULAR PURPOSE. See the license for more details. |
20 | | */ |
21 | | |
22 | | |
23 | | #include <ql/methods/finitedifferences/meshers/fdmmesher.hpp> |
24 | | #include <ql/methods/finitedifferences/operators/fdmlinearoplayout.hpp> |
25 | | #include <ql/methods/finitedifferences/utilities/fdmdirichletboundary.hpp> |
26 | | #include <ql/methods/finitedifferences/utilities/fdmindicesonboundary.hpp> |
27 | | |
28 | | namespace QuantLib { |
29 | | |
30 | | FdmDirichletBoundary::FdmDirichletBoundary( |
31 | | const ext::shared_ptr<FdmMesher>& mesher, |
32 | | Real valueOnBoundary, Size direction, |
33 | | FdmDirichletBoundary::Side side) |
34 | 0 | : side_(side), |
35 | 0 | valueOnBoundary_(valueOnBoundary), |
36 | 0 | indices_(FdmIndicesOnBoundary(mesher->layout(), |
37 | 0 | direction, side).getIndices()) { |
38 | |
|
39 | 0 | if (side_ == Lower) { |
40 | 0 | xExtreme_ = mesher->locations(direction)[0]; |
41 | 0 | } |
42 | 0 | else if (side_ == Upper) { |
43 | 0 | xExtreme_ = mesher |
44 | 0 | ->locations(direction)[mesher->layout()->dim()[direction]-1]; |
45 | 0 | } |
46 | 0 | else { |
47 | 0 | QL_FAIL("internal error"); |
48 | 0 | } |
49 | 0 | } |
50 | | |
51 | 0 | void FdmDirichletBoundary::applyBeforeApplying(operator_type&) const { |
52 | 0 | } |
53 | | |
54 | 0 | void FdmDirichletBoundary::applyAfterApplying(Array& x) const { |
55 | 0 | for (unsigned long indice : indices_) { |
56 | 0 | x[indice] = valueOnBoundary_; |
57 | 0 | } |
58 | 0 | } |
59 | | |
60 | | void FdmDirichletBoundary::applyBeforeSolving(operator_type&, |
61 | 0 | array_type&) const { |
62 | 0 | } |
63 | | |
64 | 0 | void FdmDirichletBoundary::applyAfterSolving(Array& rhs) const { |
65 | 0 | this->applyAfterApplying(rhs); |
66 | 0 | } |
67 | | |
68 | 0 | Real FdmDirichletBoundary::applyAfterApplying(Real x, Real value) const { |
69 | 0 | return ( (side_ == Lower && x < xExtreme_) |
70 | 0 | || (side_ == Upper && x > xExtreme_)) ? valueOnBoundary_ : value; |
71 | 0 | } |
72 | | } |