Coverage Report

Created: 2025-08-11 06:28

/src/quantlib/ql/experimental/finitedifferences/fdmvppstepcondition.cpp
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) 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
23
#include <ql/experimental/finitedifferences/fdmvppstepcondition.hpp>
24
#include <ql/math/array.hpp>
25
#include <ql/methods/finitedifferences/meshers/fdmmesher.hpp>
26
#include <ql/methods/finitedifferences/operators/fdmlinearopiterator.hpp>
27
#include <ql/methods/finitedifferences/operators/fdmlinearoplayout.hpp>
28
#include <ql/methods/finitedifferences/utilities/fdminnervaluecalculator.hpp>
29
#include <utility>
30
31
namespace QuantLib {
32
    FdmVPPStepCondition::FdmVPPStepCondition(
33
        const FdmVPPStepConditionParams& params,
34
        Size nStates,
35
        const FdmVPPStepConditionMesher& mesh,
36
        ext::shared_ptr<FdmInnerValueCalculator> gasPrice,
37
        ext::shared_ptr<FdmInnerValueCalculator> sparkSpreadPrice)
38
0
    : heatRate_(params.heatRate), pMin_(params.pMin), pMax_(params.pMax), tMinUp_(params.tMinUp),
39
0
      tMinDown_(params.tMinDown), startUpFuel_(params.startUpFuel),
40
0
      startUpFixCost_(params.startUpFixCost), fuelCostAddon_(params.fuelCostAddon),
41
0
      stateDirection_(mesh.stateDirection), nStates_(nStates), mesher_(mesh.mesher),
42
0
      gasPrice_(std::move(gasPrice)), sparkSpreadPrice_(std::move(sparkSpreadPrice)),
43
0
      stateEvolveFcts_(nStates_) {
44
45
0
        QL_REQUIRE(nStates_ == mesher_->layout()->dim()[stateDirection_],
46
0
                   "mesher does not fit to vpp arguments");
47
48
0
        for (Size i=0; i < nStates_; ++i) {
49
0
            const Size j = i % (2*tMinUp_ + tMinDown_);
50
51
0
            if (j < tMinUp_) {
52
0
                stateEvolveFcts_[i] = [&](Real x){ return evolveAtPMin(x); };
53
0
            }
54
0
            else if (j < 2*tMinUp_){
55
0
                stateEvolveFcts_[i] = [&](Real x) { return evolveAtPMax(x); };
56
0
            }
57
0
        }
58
0
    }
59
60
61
0
    Size FdmVPPStepCondition::nStates() const {
62
0
        return nStates_;
63
0
    }
64
65
66
0
    void FdmVPPStepCondition::applyTo(Array& a, Time t) const {
67
0
        const Size nStates = mesher_->layout()->dim()[stateDirection_];
68
69
0
        for (const auto& iter : *mesher_->layout()) {
70
0
            a[iter.index()] += evolve(iter, t);
71
0
        }
72
73
0
        for (const auto& iter : *mesher_->layout()) {
74
0
            if (iter.coordinates()[stateDirection_] == 0U) {
75
76
0
                Array x(nStates);
77
0
                for (Size i=0; i < nStates; ++i) {
78
0
                    x[i] = a[mesher_->layout()->neighbourhood(iter, stateDirection_, i)];
79
0
                }
80
81
0
                const Real gasPrice = gasPrice_->innerValue(iter, t);
82
0
                x = changeState(gasPrice, x, t);
83
0
                for (Size i=0; i < nStates; ++i) {
84
0
                    a[mesher_->layout()->neighbourhood(iter, stateDirection_, i)] = x[i];
85
0
                }
86
0
            }
87
0
        }
88
0
    }
89
90
    Real FdmVPPStepCondition::evolve(
91
0
        const FdmLinearOpIterator& iter, Time t) const {
92
93
0
        const Size state = iter.coordinates()[stateDirection_];
94
95
0
        if (!(stateEvolveFcts_[state])) {
96
0
            return 0.0;
97
0
        }
98
0
        else {
99
0
            const Real sparkSpread = sparkSpreadPrice_->innerValue(iter, t);
100
0
            return stateEvolveFcts_[state](sparkSpread);
101
0
        }
102
0
    }
103
104
105
0
    Real FdmVPPStepCondition::evolveAtPMin(Real sparkSpread) const {
106
0
        return pMin_*(sparkSpread - heatRate_*fuelCostAddon_);
107
0
    }
108
109
0
    Real FdmVPPStepCondition::evolveAtPMax(Real sparkSpread) const {
110
0
        return pMax_*(sparkSpread - heatRate_*fuelCostAddon_);
111
0
    }
112
}