-
Notifications
You must be signed in to change notification settings - Fork 5
/
npvobjective.cpp
150 lines (107 loc) · 4.04 KB
/
npvobjective.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* This file is part of the ResOpt project.
*
* Copyright (C) 2011-2012 Aleksander O. Juell <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "npvobjective.h"
#include "stream.h"
#include "cost.h"
#include <math.h>
#include <iostream>
#include <QString>
using std::cout;
using std::endl;
namespace ResOpt
{
NpvObjective::NpvObjective()
: m_dcf(0.0),
m_price_oil(0.0),
m_price_gas(0.0),
m_price_water(0.0)
{
}
//-----------------------------------------------------------------------------------------------
// Calculates the net present value
//-----------------------------------------------------------------------------------------------
void NpvObjective::calculateValue(QVector<Stream *> s, QVector<Cost *> c)
{
// checking if the discount factor is entered as fraction or percent
if(m_dcf >= 1.0)
{
cout << "The discount factor was entered as: " << m_dcf << ". Assuming this is a percentage..." << endl;
m_dcf = m_dcf / 100.0;
}
double npv = 0;
int cost_place = 0;
bool add_more_costs = true;
// first adding the up front costs (costs where t < t0)
while(cost_place < c.size() && add_more_costs)
{
if(c.at(cost_place)->time() < s.at(0)->time())
{
npv -= c.at(cost_place)->value();
++cost_place;
}
else add_more_costs = false;
}
for(int i = 0; i < s.size(); i++) // looping through each time step
{
double dt; // time step duration
double cf = 0.0; // cash flow over time step
// calculate time step duration
if(i == 0) dt = s.at(i)->time();
else dt = s.at(i)->time() - s.at(i-1)->time();
// calculate cash flow
cf = dt * (s.at(i)->gasRate(s.at(i)->inputUnits()) * gasPrice()); // gas
cf += dt * (s.at(i)->oilRate(s.at(i)->inputUnits()) * oilPrice()); // oil
cf += dt * (s.at(i)->waterRate(s.at(i)->inputUnits()) * waterPrice()); // water
// adding costs that fall within this time step
double ts_cost = 0;
add_more_costs = true;
while(cost_place < c.size() && add_more_costs)
{
if(c.at(cost_place)->time() <= s.at(i)->time())
{
ts_cost += c.at(cost_place)->value();
++cost_place;
}
else add_more_costs = false;
}
// subtracting the costs from the cash flow
cf -= ts_cost;
// add and discount cash flow to NPV
double time_yr = s.at(i)->time() / 365;
npv += cf / pow(1+dcf(), time_yr);
}
// setting the NPV as the objective value
setValue(npv);
}
//-----------------------------------------------------------------------------------------------
// generates a description for driver file
//-----------------------------------------------------------------------------------------------
QString NpvObjective::description() const
{
QString str("START OBJECTIVE\n");
str.append(" TYPE NPV \n\n");
str.append(" DCF " + QString::number(dcf()) + "\n");
str.append(" OILPRICE " + QString::number(oilPrice()) + "\n");
str.append(" GASPRICE " + QString::number(gasPrice()) + "\n");
str.append(" WATERPRICE " + QString::number(waterPrice()) + "\n");
str.append("END OBJECTIVE\n\n");
return str;
}
} // namespace ResOpt