QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
mixedscheme.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2002, 2003 Ferdinando Ametrano
5 Copyright (C) 2000, 2001, 2002, 2003 RiskMap srl
6
7 This file is part of QuantLib, a free-software/open-source library
8 for financial quantitative analysts and developers - http://quantlib.org/
9
10 QuantLib is free software: you can redistribute it and/or modify it
11 under the terms of the QuantLib license. You should have received a
12 copy of the license along with this program; if not, please email
13 <quantlib-dev@lists.sf.net>. The license is also available online at
14 <http://quantlib.org/license.shtml>.
15
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the license for more details.
19*/
20
25#ifndef quantlib_mixed_scheme_hpp
26#define quantlib_mixed_scheme_hpp
27
28#include <ql/methods/finitedifferences/finitedifferencemodel.hpp>
29#include <utility>
30
31namespace QuantLib {
32
34
72 template <class Operator>
74 public:
75 // typedefs
79 typedef typename traits::bc_set bc_set;
81 // constructors
82 MixedScheme(const operator_type& L, Real theta, bc_set bcs)
83 : L_(L), I_(operator_type::identity(L.size())), dt_(0.0), theta_(theta),
84 bcs_(std::move(bcs)) {}
85 void step(array_type& a,
86 Time t);
87 void setStep(Time dt) {
88 dt_ = dt;
89 if (theta_!=1.0) // there is an explicit part
90 explicitPart_ = I_-((1.0-theta_) * dt_)*L_;
91 if (theta_!=0.0) // there is an implicit part
93 }
94 protected:
99 };
100
101
102 // inline definitions
103
104 template <class Operator>
106 Size i;
107 for (i=0; i<bcs_.size(); i++)
108 bcs_[i]->setTime(t);
109 if (theta_!=1.0) { // there is an explicit part
110 if (L_.isTimeDependent()) {
111 L_.setTime(t);
112 explicitPart_ = I_-((1.0-theta_) * dt_)*L_;
113 }
114 for (i=0; i<bcs_.size(); i++)
115 bcs_[i]->applyBeforeApplying(explicitPart_);
116 a = explicitPart_.applyTo(a);
117 for (i=0; i<bcs_.size(); i++)
118 bcs_[i]->applyAfterApplying(a);
119 }
120 if (theta_!=0.0) { // there is an implicit part
121 if (L_.isTimeDependent()) {
122 L_.setTime(t-dt_);
123 implicitPart_ = I_+(theta_ * dt_)*L_;
124 }
125 for (i=0; i<bcs_.size(); i++)
126 bcs_[i]->applyBeforeSolving(implicitPart_,a);
127 implicitPart_.solveFor(a, a);
128 for (i=0; i<bcs_.size(); i++)
129 bcs_[i]->applyAfterSolving(a);
130 }
131 }
132
133}
134
135
136#endif
Mixed (explicit/implicit) scheme for finite difference methods.
Definition: mixedscheme.hpp:73
traits::operator_type operator_type
Definition: mixedscheme.hpp:77
operator_type explicitPart_
Definition: mixedscheme.hpp:95
operator_type implicitPart_
Definition: mixedscheme.hpp:95
void setStep(Time dt)
Definition: mixedscheme.hpp:87
traits::bc_set bc_set
Definition: mixedscheme.hpp:79
traits::condition_type condition_type
Definition: mixedscheme.hpp:80
OperatorTraits< Operator > traits
Definition: mixedscheme.hpp:76
traits::array_type array_type
Definition: mixedscheme.hpp:78
void step(array_type &a, Time t)
MixedScheme(const operator_type &L, Real theta, bc_set bcs)
Definition: mixedscheme.hpp:82
std::vector< ext::shared_ptr< bc_type > > bc_set
Operator::array_type array_type
condition to be applied at every time step
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
STL namespace.