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 151 152 153 154 155 156 157
|
#include <iostream>
#include <iomanip>
#include <vector>
#include <cmath>
#include <math.h>
//#include <nlopt.h>
using namespace std;
typedef double real_t;
//typedef float real_t;
real_t xyz(real_t P2,real_t P3)
{
real_t v{}, v1{}, v2{}, v3{};
constexpr real_t dv[] = { 0.00001, 0.00001, 0.000001, 0.00001 };
constexpr real_t zt[] = { 0.01, 0.01, 0.001, 0.001 };
constexpr real_t value[][7]
{
{ -292860, 550, 2, 8.2445, 0.8951, 59.8465, 2 },
{ -687248.2, 550, 2, 7.90694, 0.866, 49.02654, 2 },
{ -970688.6, 550, 2, 7.15059, 0.76984, 6.90224, 1 },
{ -1280557, 550, 2, 7.94986, 0.96455, 0, 2 },
{ -1524891, 550, 2, 7.33129, 0.89143, 0, 2 },
{ -1778901, 550, 2, 6.96783, 0.84634, 0, 2 },
{ -2013803, 550, 2, 6.52914, 0.79543, 0, 2 }
};
auto init_func = [](real_t P, const real_t value[])
{
return
value[0]/pow(value[1], value[2]) +
value[3] -
value[4]*log(P) +
value[5]/pow(P, value[6]);
};
auto init_vect = [=](real_t P, const real_t value[][7])
{
vector<real_t> values
{
exp(init_func(P, value[0])),
exp(init_func(P, value[1])),
exp(init_func(P, value[2])),
exp(init_func(P, value[3])),
exp(init_func(P, value[4])),
exp(init_func(P, value[5])),
exp(init_func(P, value[6]))
};
return values;
};
vector<real_t> c{ 0.4, 0.2, 0.1, 0.1, 0.1, 0.05, 0.05 };
{
constexpr real_t P1{ 514.7 };
const vector<real_t> k{ init_vect(P1, value) };
real_t z;
size_t cnt{};
do
{
z = {};
for (size_t i = 0; i != k.size(); ++i)
z += (k[i]-1)*c[i] / ((1-v) + (k[i]*v));
v = v+dv[0];
++cnt;
}
while (abs(z)>zt[0]);
for (size_t i = 0; i != c.size(); ++i)
c[i] = c[i] / (1 + v*(k[i] - 1));
}
vector<real_t> C(c.size());
{
const vector<real_t> k{ init_vect(P2, value) };
real_t z;
size_t cnt{};
do
{
z = {};
for (size_t i = 0; i != c.size(); ++i)
z += (k[i]-1)*c[i] / ((1-v1) + (k[i]*v1));
v1 = v1+dv[1];
++cnt;
}
while (abs(z)>zt[1]);
for (size_t i = 0; i != c.size(); ++i)
C[i] = c[i] / (1 + v1*(k[i] - 1));
}
vector<real_t> Cf(c.size());
{
const vector<real_t> K{ init_vect(P3, value) };
real_t z;
size_t cnt{};
do
{
z = {};
for (size_t i = 0; i != K.size(); ++i)
z += (K[i]-1)*C[i] / ((1-v2) + (K[i]*v2));
v2 = v2+dv[2];
++cnt;
}
while (abs(z)>zt[2]);
for (size_t i = 0; i != c.size(); ++i)
Cf[i] = C[i] / (1 + v2*(K[i] - 1));
}
{
constexpr real_t P4{ 14.7 };
const vector<real_t> Kf{ init_vect(P4, value) };
real_t z;
size_t cnt{};
do
{
z = {};
for (size_t i = 0; i != Kf.size(); ++i)
z += (Kf[i]-1)*Cf[i] / ((1-v3) + (Kf[i]*v3));
v3 = v3+dv[3];
++cnt;
}
while (abs(z)>zt[3]);
}
real_t xyz =
(v + (v1*(1-v)) + (v2*(1-v)*(1-v1)) + ((v3*(1-v)*(1-v1)*(1-v2)))) /
((1-v) + ((1-v)*(1-v1)) + ((1-v)*(1-v1)*(1-v2)) + ((1-v)*(1-v1)*(1-v2)*(1-v3)));
return xyz;
}
// parameters for contraint
struct my_constraint_data
{
real_t P2, P3;
};
//setup contraint function
//setup Obj function
int main ()
{
real_t GOR = xyz(200, 100);
cout << GOR << endl;
}
|