/src/quantlib/ql/methods/finitedifferences/utilities/fdmtimedepdirichletboundary.cpp
Line | Count | Source |
1 | | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* |
4 | | Copyright (C) 2012 Peter Caspers |
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 | | |
21 | | /*! \file fdmtimedepdirichletboundary.cpp |
22 | | */ |
23 | | |
24 | | #include <ql/methods/finitedifferences/meshers/fdmmesher.hpp> |
25 | | #include <ql/methods/finitedifferences/operators/fdmlinearop.hpp> |
26 | | #include <ql/methods/finitedifferences/utilities/fdmindicesonboundary.hpp> |
27 | | #include <ql/methods/finitedifferences/utilities/fdmtimedepdirichletboundary.hpp> |
28 | | #include <algorithm> |
29 | | #include <utility> |
30 | | |
31 | | namespace QuantLib { |
32 | | |
33 | | FdmTimeDepDirichletBoundary::FdmTimeDepDirichletBoundary( |
34 | | const ext::shared_ptr<FdmMesher>& mesher, |
35 | | std::function<Real(Real)> valueOnBoundary, |
36 | | Size direction, |
37 | | Side side) |
38 | 0 | : indices_(FdmIndicesOnBoundary(mesher->layout(), direction, side).getIndices()), |
39 | 0 | valueOnBoundary_(std::move(valueOnBoundary)), values_(indices_.size()) {} |
40 | | |
41 | | FdmTimeDepDirichletBoundary::FdmTimeDepDirichletBoundary( |
42 | | const ext::shared_ptr<FdmMesher>& mesher, |
43 | | std::function<Array(Real)> valuesOnBoundary, |
44 | | Size direction, |
45 | | Side side) |
46 | 0 | : indices_(FdmIndicesOnBoundary(mesher->layout(), direction, side).getIndices()), |
47 | 0 | valuesOnBoundary_(std::move(valuesOnBoundary)), values_(indices_.size()) {} |
48 | | |
49 | 0 | void FdmTimeDepDirichletBoundary::setTime(Time t) { |
50 | 0 | if (valueOnBoundary_) { |
51 | 0 | std::fill(values_.begin(), values_.end(), valueOnBoundary_(t)); |
52 | 0 | } else if (valuesOnBoundary_) { |
53 | 0 | values_ = valuesOnBoundary_(t); |
54 | 0 | } else { |
55 | 0 | QL_FAIL("no boundary values defined"); |
56 | 0 | } |
57 | 0 | } |
58 | | |
59 | 0 | void FdmTimeDepDirichletBoundary::applyAfterApplying(array_type& a) const { |
60 | 0 | QL_REQUIRE(indices_.size() == values_.size(), |
61 | 0 | "values on boundary size (" << values_.size() |
62 | 0 | << ") does not match hypersurface size (" |
63 | 0 | << indices_.size() << ")"); |
64 | 0 | for (auto iter = indices_.begin(); iter != indices_.end(); ++iter) { |
65 | 0 | a[*iter] = values_[iter - indices_.begin()]; |
66 | 0 | } |
67 | 0 | } |
68 | | |
69 | 0 | void FdmTimeDepDirichletBoundary::applyAfterSolving(array_type& a) const { |
70 | 0 | this->applyAfterApplying(a); |
71 | 0 | } |
72 | | } |