Skip to content

Commit

Permalink
Remove malloc in src/solver/hydro/ (#1626)
Browse files Browse the repository at this point in the history
  • Loading branch information
payetvin authored Sep 25, 2023
1 parent a9062f8 commit bf54e4e
Show file tree
Hide file tree
Showing 29 changed files with 438 additions and 938 deletions.
2 changes: 0 additions & 2 deletions src/solver/hydro/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ set(SRC_EXT_SOLVER_H2O_DAILY
daily/h2o_j_optimiser_un_mois.cpp
daily/h2o_j_resoudre_le_probleme_lineaire.cpp
daily/h2o_j_lisser_les_sur_turbines.cpp
daily/h2o_j_sys.h
daily/h2o_j_ajouter_bruit_au_cout.cpp
)

Expand All @@ -30,7 +29,6 @@ set(SRC_EXT_SOLVER_H2O_MONTHLY
monthly/h2o_m_optimiser_une_annee.cpp
monthly/h2o_m_ajouter_bruit.cpp
monthly/h2o_m_resoudre_le_probleme_lineaire.cpp
monthly/h2o_m_sys.h
)

set(SRC_EXT_SOLVER_H2O2_DAILY
Expand Down
15 changes: 5 additions & 10 deletions src/solver/hydro/daily/h2o_j_construire_les_contraintes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,12 @@ void H2O_J_ConstruireLesContraintes(int NbPdt,
std::vector<int>& IndicesColonnes,
CORRESPONDANCE_DES_CONTRAINTES& CorrespondanceDesContraintes)
{
int NombreDeContraintes;
int il;
int Pdt;
int NombreDeTermes;
int NombreDeContraintes = 0;
int NombreDeTermes = 0;
int il = 0;

NombreDeContraintes = 0;
il = 0;

NombreDeTermes = 0;
IndicesDebutDeLigne[NombreDeContraintes] = il;
for (Pdt = 0; Pdt < NbPdt; Pdt++)
for (int Pdt = 0; Pdt < NbPdt; Pdt++)
{
CoefficientsDeLaMatriceDesContraintes[il] = 1.0;
IndicesColonnes[il] = NumeroDeVariableTurbine[Pdt];
Expand All @@ -67,7 +62,7 @@ void H2O_J_ConstruireLesContraintes(int NbPdt,
CorrespondanceDesContraintes.NumeroDeContrainteDEnergieMensuelle = NombreDeContraintes;
NombreDeContraintes++;

for (Pdt = 0; Pdt < NbPdt; Pdt++)
for (int Pdt = 0; Pdt < NbPdt; Pdt++)
{
NombreDeTermes = 0;
IndicesDebutDeLigne[NombreDeContraintes] = il;
Expand Down
7 changes: 2 additions & 5 deletions src/solver/hydro/daily/h2o_j_construire_les_variables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,9 @@ void H2O_j_ConstruireLesVariables(int NbPdt,
std::vector<double*>& AdresseOuPlacerLaValeurDesVariablesOptimisees,
CORRESPONDANCE_DES_VARIABLES& CorrespondanceDesVariables)
{
int Var;
int Pdt;
int Var = 0;

Var = 0;

for (Pdt = 0; Pdt < NbPdt; Pdt++)
for (int Pdt = 0; Pdt < NbPdt; Pdt++)
{
NumeroDeVariableTurbine[Pdt] = Var;
Xmin[Var] = 0.0;
Expand Down
2 changes: 0 additions & 2 deletions src/solver/hydro/daily/h2o_j_donnees_optimisation.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
#ifndef __SOLVER_H2O_J_STRUCTURE_INTERNE__
#define __SOLVER_H2O_J_STRUCTURE_INTERNE__

#include "h2o_j_sys.h"

#define LINFINI 1.e+80

#define JOURS_28 28
Expand Down
2 changes: 1 addition & 1 deletion src/solver/hydro/daily/h2o_j_fonctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#ifndef __SOLVER_H2O_J_FONCTIONS__
#define __SOLVER_H2O_J_FONCTIONS__

DONNEES_MENSUELLES H2O_J_Instanciation(void);
DONNEES_MENSUELLES* H2O_J_Instanciation(void);

void H2O_J_OptimiserUnMois(DONNEES_MENSUELLES*);
void H2O_J_Free(DONNEES_MENSUELLES*);
Expand Down
2 changes: 0 additions & 2 deletions src/solver/hydro/daily/h2o_j_free.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,5 @@ void H2O_J_Free(DONNEES_MENSUELLES* DonneesMensuelles)
SPX_LibererProbleme(ProbSpx);
}

free(ProblemeHydraulique.Probleme);

return;
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,14 @@
void H2O_J_InitialiserLesBornesdesVariables(DONNEES_MENSUELLES* DonneesMensuelles,
int NumeroDeProbleme)
{
int NbPdt;
int Var;


const std::vector<double>& TurbineMax = DonneesMensuelles->TurbineMax;
const std::vector<double>& TurbineMin = DonneesMensuelles->TurbineMin;
const std::vector<double>& TurbineCible = DonneesMensuelles->TurbineCible;
std::vector<double>& Turbine = DonneesMensuelles->Turbine;

PROBLEME_HYDRAULIQUE& ProblemeHydraulique = DonneesMensuelles->ProblemeHydraulique;

NbPdt = ProblemeHydraulique.NbJoursDUnProbleme[NumeroDeProbleme];
const int NbPdt = ProblemeHydraulique.NbJoursDUnProbleme[NumeroDeProbleme];

const CORRESPONDANCE_DES_VARIABLES& CorrespondanceDesVariables
= ProblemeHydraulique.CorrespondanceDesVariables[NumeroDeProbleme];
Expand All @@ -58,7 +54,7 @@ void H2O_J_InitialiserLesBornesdesVariables(DONNEES_MENSUELLES* DonneesMensuelle

for (int Pdt = 0; Pdt < NbPdt; Pdt++)
{
Var = CorrespondanceDesVariables.NumeroDeVariableTurbine[Pdt];
int Var = CorrespondanceDesVariables.NumeroDeVariableTurbine[Pdt];
Xmax[Var] = TurbineMax[Pdt];

Xmin[Var] = std::min(TurbineMax[Pdt], std::max(TurbineCible[Pdt], TurbineMin[Pdt]));
Expand Down
15 changes: 4 additions & 11 deletions src/solver/hydro/daily/h2o_j_initialiser_les_second_membre.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,23 @@

void H2O_J_InitialiserLeSecondMembre(DONNEES_MENSUELLES* DonneesMensuelles, int NumeroDeProbleme)
{
int Pdt;
int Cnt;
int NbPdt;

int NumeroDeContrainteDEnergieMensuelle;

PROBLEME_HYDRAULIQUE& ProblemeHydraulique = DonneesMensuelles->ProblemeHydraulique;

NbPdt = ProblemeHydraulique.NbJoursDUnProbleme[NumeroDeProbleme];

CORRESPONDANCE_DES_CONTRAINTES& CorrespondanceDesContraintes
= ProblemeHydraulique.CorrespondanceDesContraintes[NumeroDeProbleme];

NumeroDeContrainteDEnergieMensuelle
int NumeroDeContrainteDEnergieMensuelle
= CorrespondanceDesContraintes.NumeroDeContrainteDEnergieMensuelle;

std::vector<double>& SecondMembre
= ProblemeHydraulique.ProblemeLineairePartieVariable[NumeroDeProbleme].SecondMembre;

SecondMembre[NumeroDeContrainteDEnergieMensuelle] = DonneesMensuelles->TurbineDuMois;

for (Pdt = 0; Pdt < NbPdt; Pdt++)
const int NbPdt = ProblemeHydraulique.NbJoursDUnProbleme[NumeroDeProbleme];
for (int Pdt = 0; Pdt < NbPdt; Pdt++)
{
Cnt = CorrespondanceDesContraintes.NumeroDeContrainteSurXi[Pdt];
int Cnt = CorrespondanceDesContraintes.NumeroDeContrainteSurXi[Pdt];
SecondMembre[Cnt] = DonneesMensuelles->TurbineCible[Pdt];
}

Expand Down
16 changes: 8 additions & 8 deletions src/solver/hydro/daily/h2o_j_instanciation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
#include "h2o_j_donnees_mensuelles.h"
#include "h2o_j_fonctions.h"

DONNEES_MENSUELLES H2O_J_Instanciation(void)
DONNEES_MENSUELLES* H2O_J_Instanciation(void)
{
DONNEES_MENSUELLES DonneesMensuelles;
DONNEES_MENSUELLES* DonneesMensuelles = new DONNEES_MENSUELLES;

PROBLEME_HYDRAULIQUE& ProblemeHydraulique = DonneesMensuelles.ProblemeHydraulique;
PROBLEME_HYDRAULIQUE& ProblemeHydraulique = DonneesMensuelles->ProblemeHydraulique;

ProblemeHydraulique.NombreDeProblemes = 4;

Expand All @@ -44,10 +44,10 @@ DONNEES_MENSUELLES H2O_J_Instanciation(void)
NbJoursDUnProbleme[2] = 30;
NbJoursDUnProbleme[3] = 31;

DonneesMensuelles.TurbineMax.assign(NbJoursDUnProbleme[3], 0.);
DonneesMensuelles.TurbineMin.assign(NbJoursDUnProbleme[3], 0.);
DonneesMensuelles.TurbineCible.assign(NbJoursDUnProbleme[3], 0.);
DonneesMensuelles.Turbine.assign(NbJoursDUnProbleme[3], 0.);
DonneesMensuelles->TurbineMax.assign(NbJoursDUnProbleme[3], 0.);
DonneesMensuelles->TurbineMin.assign(NbJoursDUnProbleme[3], 0.);
DonneesMensuelles->TurbineCible.assign(NbJoursDUnProbleme[3], 0.);
DonneesMensuelles->Turbine.assign(NbJoursDUnProbleme[3], 0.);

int NombreDeProblemes = ProblemeHydraulique.NombreDeProblemes;

Expand Down Expand Up @@ -77,7 +77,7 @@ DONNEES_MENSUELLES H2O_J_Instanciation(void)

for (int i = 0; i < NombreDeProblemes; i++)
{
int NbPdt = NbJoursDUnProbleme[i];
const int NbPdt = NbJoursDUnProbleme[i];

CorrespondanceDesVariables[i].NumeroDeVariableTurbine.assign(NbPdt, 0);
CorrespondanceDesContraintes[i].NumeroDeContrainteSurXi.assign(NbPdt, 0);
Expand Down
94 changes: 32 additions & 62 deletions src/solver/hydro/daily/h2o_j_lisser_les_sur_turbines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,102 +28,72 @@
#include "h2o_j_donnees_mensuelles.h"
#include "h2o_j_fonctions.h"

#include <algorithm>

#define ZERO 1.e-9

void H2O_J_LisserLesSurTurbines(DONNEES_MENSUELLES* DonneesMensuelles, int NumeroDeProbleme)
{
int Pdt;
int Cnt;
int NbPdt;
double X;
int Np;
char* Flag;
char LimiteAtteinte;
int NbCycles;
double SurTurbineARepartir;
double Xmoy;
double MargeMin;
double SurTurbine;
PROBLEME_HYDRAULIQUE& ProblemeHydraulique = DonneesMensuelles->ProblemeHydraulique;

std::vector<double>& TurbineMax = DonneesMensuelles->TurbineMax;
std::vector<double>& Turbine = DonneesMensuelles->Turbine;
std::vector<double>& TurbineCible = DonneesMensuelles->TurbineCible;
auto& Turbine = DonneesMensuelles->Turbine;
const auto& TurbineMax = DonneesMensuelles->TurbineMax;
const auto& TurbineCible = DonneesMensuelles->TurbineCible;

Flag = (char*) ProblemeHydraulique.ProblemeLineairePartieVariable[NumeroDeProbleme].Xmax.data();
const int NbPdt = ProblemeHydraulique.NbJoursDUnProbleme[NumeroDeProbleme];

NbPdt = ProblemeHydraulique.NbJoursDUnProbleme[NumeroDeProbleme];
std::vector<bool> flag(NbPdt);

SurTurbineARepartir = 0.0;
for (Pdt = 0; Pdt < NbPdt; Pdt++)
{
Flag[Pdt] = 0;
if (Turbine[Pdt] - TurbineCible[Pdt] > ZERO)
Flag[Pdt] = 1;
}
double SurTurbineARepartir = 0.0;
for (int Pdt = 0; Pdt < NbPdt; Pdt++)
flag[Pdt] = (Turbine[Pdt] - TurbineCible[Pdt] > ZERO);

for (Pdt = 0; Pdt < NbPdt; Pdt++)
{
if (Flag[Pdt] == 1)
for (int Pdt = 0; Pdt < NbPdt; Pdt++)
if (flag[Pdt])
SurTurbineARepartir += Turbine[Pdt] - TurbineCible[Pdt];
}

for (Pdt = 0; Pdt < NbPdt; Pdt++)
Flag[Pdt] = 0;
for (Pdt = 0; Pdt < NbPdt; Pdt++)
{
if (TurbineMax[Pdt] - TurbineCible[Pdt] > ZERO)
Flag[Pdt] = 1;
}
for (int Pdt = 0; Pdt < NbPdt; Pdt++)
flag[Pdt] = (TurbineMax[Pdt] - TurbineCible[Pdt] > ZERO);;

NbCycles = 0;
int NbCycles = 0;
BoucleDeRepartition:

Np = 0;
for (Pdt = 0; Pdt < NbPdt; Pdt++)
{
if (Flag[Pdt] == 1)
Np++;
}
if (Np <= 0)
{
const int Np = std::count(flag.begin(), flag.end(), true);

if (Np == 0)
return;
}

MargeMin = 0.;
for (Pdt = 0; Pdt < NbPdt; Pdt++)
double MargeMin = 0.;
for (int Pdt = 0; Pdt < NbPdt; Pdt++)
MargeMin += TurbineMax[Pdt];
for (Pdt = 0; Pdt < NbPdt; Pdt++)
{
if (Flag[Pdt] == 1)
{
if (TurbineMax[Pdt] - TurbineCible[Pdt] < MargeMin)
MargeMin = TurbineMax[Pdt] - TurbineCible[Pdt];
}
}

Xmoy = SurTurbineARepartir / Np;
for (int Pdt = 0; Pdt < NbPdt; Pdt++)
if (flag[Pdt] && TurbineMax[Pdt] - TurbineCible[Pdt] < MargeMin)
MargeMin = TurbineMax[Pdt] - TurbineCible[Pdt];

double Xmoy = SurTurbineARepartir / Np;
double SurTurbine;
if (Xmoy <= MargeMin)
SurTurbine = Xmoy;
else
SurTurbine = MargeMin;

LimiteAtteinte = 0;
for (Pdt = 0; Pdt < NbPdt; Pdt++)
bool limiteAtteinte = false;
for (int Pdt = 0; Pdt < NbPdt; Pdt++)
{
if (Flag[Pdt] == 0)
if (!flag[Pdt])
continue;

Turbine[Pdt] = TurbineCible[Pdt] + SurTurbine;
if (TurbineMax[Pdt] - Turbine[Pdt] <= ZERO)
{
SurTurbineARepartir -= SurTurbine;
LimiteAtteinte = 1;
Flag[Pdt] = 0;
limiteAtteinte = true;
flag[Pdt] = false;
}
}

if (LimiteAtteinte == 1 && SurTurbineARepartir > 0.0)
if (limiteAtteinte && SurTurbineARepartir > 0.0)
{
NbCycles++;
if (NbCycles <= NbPdt)
Expand Down
10 changes: 2 additions & 8 deletions src/solver/hydro/daily/h2o_j_optimiser_un_mois.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,10 @@

void H2O_J_OptimiserUnMois(DONNEES_MENSUELLES* DonneesMensuelles)
{
int NumeroDeProbleme;
int i;

PROBLEME_HYDRAULIQUE& ProblemeHydraulique = DonneesMensuelles->ProblemeHydraulique;

NumeroDeProbleme = -1;
for (i = 0; i < ProblemeHydraulique.NombreDeProblemes; i++)
int NumeroDeProbleme = -1;
for (int i = 0; i < ProblemeHydraulique.NombreDeProblemes; i++)
{
if (DonneesMensuelles->NombreDeJoursDuMois == ProblemeHydraulique.NbJoursDUnProbleme[i])
{
Expand All @@ -53,11 +50,8 @@ void H2O_J_OptimiserUnMois(DONNEES_MENSUELLES* DonneesMensuelles)
DonneesMensuelles->ResultatsValides = NON;

H2O_J_InitialiserLeSecondMembre(DonneesMensuelles, NumeroDeProbleme);

H2O_J_InitialiserLesBornesdesVariables(DonneesMensuelles, NumeroDeProbleme);

H2O_J_ResoudreLeProblemeLineaire(DonneesMensuelles, NumeroDeProbleme);

H2O_J_LisserLesSurTurbines(DonneesMensuelles, NumeroDeProbleme);

return;
Expand Down
Loading

0 comments on commit bf54e4e

Please sign in to comment.