-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New hydro remix : setting unit tests environment
- Loading branch information
1 parent
001e6d5
commit 6871e98
Showing
3 changed files
with
289 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
#include <algorithm> | ||
#include <cmath> | ||
#include <iomanip> | ||
#include <iostream> | ||
#include <limits> | ||
#include <numeric> | ||
#include <vector> | ||
|
||
using namespace std; | ||
|
||
namespace Antares::Solver::Simulation | ||
{ | ||
|
||
bool new_remix_hydro() | ||
{ | ||
return true; | ||
} | ||
|
||
int find_min_index(const vector<double>& G_plus_H, | ||
const vector<double>& new_D, | ||
const vector<double>& new_H, | ||
const vector<int>& tried_creux, | ||
const vector<double>& P_max, | ||
double top) | ||
{ | ||
double min_val = top; | ||
int min_idx = -1; | ||
for (size_t i = 0; i < G_plus_H.size(); ++i) | ||
{ | ||
if (new_D[i] > 0 && new_H[i] < P_max[i] && tried_creux[i] == 0) | ||
{ | ||
if (G_plus_H[i] < min_val) | ||
{ | ||
min_val = G_plus_H[i]; | ||
min_idx = i; | ||
} | ||
} | ||
} | ||
return min_idx; | ||
} | ||
|
||
int find_max_index(const vector<double>& G_plus_H, | ||
const vector<double>& new_H, | ||
const vector<int>& tried_pic, | ||
const vector<double>& P_min, | ||
double ref_value, | ||
double eps) | ||
{ | ||
double max_val = 0; | ||
int max_idx = -1; | ||
for (size_t i = 0; i < G_plus_H.size(); ++i) | ||
{ | ||
if (new_H[i] > P_min[i] && G_plus_H[i] >= ref_value + eps && tried_pic[i] == 0) | ||
{ | ||
if (G_plus_H[i] > max_val) | ||
{ | ||
max_val = G_plus_H[i]; | ||
max_idx = i; | ||
} | ||
} | ||
} | ||
return max_idx; | ||
} | ||
|
||
pair<vector<double>, vector<double>> new_remix_hydro(const vector<double>& G, | ||
const vector<double>& H, | ||
const vector<double>& D, | ||
const vector<double>& P_max, | ||
const vector<double>& P_min, | ||
double initial_level, | ||
double capa, | ||
const vector<double>& inflow) | ||
{ | ||
vector<double> new_H = H; | ||
vector<double> new_D = D; | ||
|
||
int loop = 1000; | ||
double eps = 1e-2; | ||
double top = *max_element(G.begin(), G.end()) + *max_element(H.begin(), H.end()) | ||
+ *max_element(D.begin(), D.end()) + 1; | ||
|
||
vector<double> G_plus_H(G.size()); | ||
transform(G.begin(), G.end(), new_H.begin(), G_plus_H.begin(), plus<>()); | ||
|
||
vector<double> level(G.size()); | ||
level[0] = initial_level + inflow[0] - new_H[0]; | ||
for (size_t i = 1; i < level.size(); ++i) | ||
{ | ||
level[i] = level[i - 1] + inflow[i] - new_H[i]; | ||
} | ||
|
||
while (loop-- > 0) | ||
{ | ||
vector<int> tried_creux(G.size(), 0); | ||
double delta = 0; | ||
|
||
while (true) | ||
{ | ||
int idx_creux = find_min_index(G_plus_H, new_D, new_H, tried_creux, P_max, top); | ||
if (idx_creux == -1) | ||
{ | ||
break; | ||
} | ||
|
||
vector<int> tried_pic(G.size(), 0); | ||
while (true) | ||
{ | ||
int idx_pic = find_max_index(G_plus_H, | ||
new_H, | ||
tried_pic, | ||
P_min, | ||
G_plus_H[idx_creux], | ||
eps); | ||
if (idx_pic == -1) | ||
{ | ||
break; | ||
} | ||
|
||
vector<double> intermediate_level(level.begin() + min(idx_creux, idx_pic), | ||
level.begin() + max(idx_creux, idx_pic)); | ||
|
||
double max_pic = min(new_H[idx_pic] - P_min[idx_pic], | ||
capa | ||
- *max_element(intermediate_level.begin(), | ||
intermediate_level.end())); | ||
double max_creux = min( | ||
{P_max[idx_creux] - new_H[idx_creux], | ||
new_D[idx_creux], | ||
*min_element(intermediate_level.begin(), intermediate_level.end())}); | ||
double dif_pic_creux = max(G_plus_H[idx_pic] - G_plus_H[idx_creux], 0.0); | ||
|
||
delta = max(min({max_pic, max_creux, dif_pic_creux / 2.0}), 0.0); | ||
|
||
if (delta > 0) | ||
{ | ||
new_H[idx_pic] -= delta; | ||
new_H[idx_creux] += delta; | ||
new_D[idx_pic] = H[idx_pic] + D[idx_pic] - new_H[idx_pic]; | ||
new_D[idx_creux] = H[idx_creux] + D[idx_creux] - new_H[idx_creux]; | ||
break; | ||
} | ||
else | ||
{ | ||
tried_pic[idx_pic] = 1; | ||
} | ||
} | ||
|
||
if (delta > 0) | ||
{ | ||
break; | ||
} | ||
tried_creux[idx_creux] = 1; | ||
} | ||
|
||
if (delta == 0) | ||
{ | ||
break; | ||
} | ||
|
||
transform(G.begin(), G.end(), new_H.begin(), G_plus_H.begin(), plus<>()); | ||
level[0] = initial_level + inflow[0] - new_H[0]; | ||
for (size_t i = 1; i < level.size(); ++i) | ||
{ | ||
level[i] = level[i - 1] + inflow[i] - new_H[i]; | ||
} | ||
} | ||
|
||
return {new_H, new_D}; | ||
} | ||
|
||
} // End namespace Antares::Solver::Simulation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#define BOOST_TEST_MODULE hydro remix | ||
|
||
#define WIN32_LEAN_AND_MEAN | ||
|
||
#include <vector> | ||
|
||
#include <boost/test/unit_test.hpp> | ||
|
||
namespace Antares::Solver::Simulation | ||
{ | ||
bool new_remix_hydro(); | ||
std::pair<std::vector<double>, std::vector<double>> new_remix_hydro( | ||
const std::vector<double>& G, | ||
const std::vector<double>& H, | ||
const std::vector<double>& D, | ||
const std::vector<double>& P_max, | ||
const std::vector<double>& P_min, | ||
double initial_level, | ||
double capa, | ||
const std::vector<double>& inflow); | ||
} // namespace Antares::Solver::Simulation | ||
|
||
using namespace Antares::Solver::Simulation; | ||
using namespace std; | ||
|
||
BOOST_AUTO_TEST_CASE(my_first_unit_test) | ||
{ | ||
BOOST_CHECK(new_remix_hydro()); | ||
} | ||
|
||
BOOST_AUTO_TEST_CASE(my_second_unit_test) | ||
{ | ||
vector<double> G = {1.0, 2.0, 3.0, 4.0, 5.0}; | ||
vector<double> H = {2.0, 3.0, 4.0, 5.0, 6.0}; | ||
vector<double> D = {1.0, 1.5, 2.0, 2.5, 3.0}; | ||
vector<double> P_max = {10.0, 10.0, 10.0, 10.0, 10.0}; | ||
vector<double> P_min = {0.0, 0.0, 0.0, 0.0, 0.0}; | ||
double initial_level = 5.0; | ||
double capa = 20.0; | ||
vector<double> inflow = {3.0, 3.0, 3.0, 3.0, 3.0}; | ||
|
||
// Call the function | ||
auto [new_H, new_D] = new_remix_hydro(G, H, D, P_max, P_min, initial_level, capa, inflow); | ||
|
||
BOOST_CHECK(true); | ||
} |