QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
fdmvppstepcondition.cpp
Go to the documentation of this file.
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2011, 2012 Klaus Spanderen
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 fdmvppstepcondition.cpp
21*/
22
24#include <ql/math/array.hpp>
29#include <utility>
30
31namespace QuantLib {
33 const FdmVPPStepConditionParams& params,
34 Size nStates,
35 const FdmVPPStepConditionMesher& mesh,
36 ext::shared_ptr<FdmInnerValueCalculator> gasPrice,
37 ext::shared_ptr<FdmInnerValueCalculator> sparkSpreadPrice)
38 : heatRate_(params.heatRate), pMin_(params.pMin), pMax_(params.pMax), tMinUp_(params.tMinUp),
39 tMinDown_(params.tMinDown), startUpFuel_(params.startUpFuel),
40 startUpFixCost_(params.startUpFixCost), fuelCostAddon_(params.fuelCostAddon),
41 stateDirection_(mesh.stateDirection), nStates_(nStates), mesher_(mesh.mesher),
42 gasPrice_(std::move(gasPrice)), sparkSpreadPrice_(std::move(sparkSpreadPrice)),
43 stateEvolveFcts_(nStates_) {
44
45 QL_REQUIRE(nStates_ == mesher_->layout()->dim()[stateDirection_],
46 "mesher does not fit to vpp arguments");
47
48 for (Size i=0; i < nStates_; ++i) {
49 const Size j = i % (2*tMinUp_ + tMinDown_);
50
51 if (j < tMinUp_) {
52 stateEvolveFcts_[i] = [&](Real x){ return evolveAtPMin(x); };
53 }
54 else if (j < 2*tMinUp_){
55 stateEvolveFcts_[i] = [&](Real x) { return evolveAtPMax(x); };
56 }
57 }
58 }
59
60
62 return nStates_;
63 }
64
65
67 const Size nStates = mesher_->layout()->dim()[stateDirection_];
68
69 for (const auto& iter : *mesher_->layout()) {
70 a[iter.index()] += evolve(iter, t);
71 }
72
73 for (const auto& iter : *mesher_->layout()) {
74 if (iter.coordinates()[stateDirection_] == 0U) {
75
76 Array x(nStates);
77 for (Size i=0; i < nStates; ++i) {
78 x[i] = a[mesher_->layout()->neighbourhood(iter, stateDirection_, i)];
79 }
80
81 const Real gasPrice = gasPrice_->innerValue(iter, t);
82 x = changeState(gasPrice, x, t);
83 for (Size i=0; i < nStates; ++i) {
84 a[mesher_->layout()->neighbourhood(iter, stateDirection_, i)] = x[i];
85 }
86 }
87 }
88 }
89
91 const FdmLinearOpIterator& iter, Time t) const {
92
93 const Size state = iter.coordinates()[stateDirection_];
94
95 if (!(stateEvolveFcts_[state])) {
96 return 0.0;
97 }
98 else {
99 const Real sparkSpread = sparkSpreadPrice_->innerValue(iter, t);
100 return stateEvolveFcts_[state](sparkSpread);
101 }
102 }
103
104
106 return pMin_*(sparkSpread - heatRate_*fuelCostAddon_);
107 }
108
110 return pMax_*(sparkSpread - heatRate_*fuelCostAddon_);
111 }
112}
1-D array used in linear algebra.
1-D array used in linear algebra.
Definition: array.hpp:52
const std::vector< Size > & coordinates() const
FdmVPPStepCondition(const FdmVPPStepConditionParams &params, Size nStates, const FdmVPPStepConditionMesher &mesh, ext::shared_ptr< FdmInnerValueCalculator > gasPrice, ext::shared_ptr< FdmInnerValueCalculator > sparkSpreadPrice)
Real evolveAtPMax(Real sparkSpread) const
virtual Array changeState(Real gasPrice, const Array &state, Time t) const =0
Real evolve(const FdmLinearOpIterator &iter, Time t) const
const ext::shared_ptr< FdmMesher > mesher_
const ext::shared_ptr< FdmInnerValueCalculator > gasPrice_
void applyTo(Array &a, Time t) const override
Real evolveAtPMin(Real sparkSpread) const
const ext::shared_ptr< FdmInnerValueCalculator > sparkSpreadPrice_
std::vector< ext::function< Real(Real)> > stateEvolveFcts_
const DefaultType & t
const Real heatRate_
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
layer of abstraction to calculate the inner value
iterator for a linear fdm operator
memory layout of a fdm linear operator
mesher for a fdm grid
VPP step condition for FD models.
const ext::shared_ptr< FdmMesher > mesher_
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.