Skip to content

Commit

Permalink
Harmonize time constants (#2203)
Browse files Browse the repository at this point in the history
close #2188

---------

Co-authored-by: Florian OMNES <[email protected]>
  • Loading branch information
payetvin and flomnes authored Jul 1, 2024
1 parent 5207775 commit ff40ec2
Show file tree
Hide file tree
Showing 36 changed files with 330 additions and 426 deletions.
29 changes: 16 additions & 13 deletions src/libs/antares/date/date.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

#include <cassert>

#include <boost/algorithm/string/case_conv.hpp>

#include <yuni/yuni.h>
#include <yuni/core/string.h>
#include <yuni/io/file.h>
Expand All @@ -32,7 +34,7 @@ using namespace Yuni;

namespace Antares::Date
{
static const uint StandardDaysPerMonths[12] = {
static const uint StandardDaysPerMonths[MONTHS_PER_YEAR] = {
31, // january
28, // february
31, // march
Expand Down Expand Up @@ -137,12 +139,13 @@ bool StringToMonth(MonthName& out, AnyString text)
return false;
}
text.trim();
CString<12, false> t = text;
t.toLower();
std::string t = text;
boost::algorithm::to_lower(t);
/* t.toLower(); */

if (t.size() == 3)
{
for (uint m = 0; m != 12; ++m)
for (uint m = 0; m != MONTHS_PER_YEAR; ++m)
{
if (monthShortLowerNames[m] == t)
{
Expand All @@ -153,7 +156,7 @@ bool StringToMonth(MonthName& out, AnyString text)
}
else
{
for (uint m = 0; m != 12; ++m)
for (uint m = 0; m != MONTHS_PER_YEAR; ++m)
{
if (monthNamesLower[m] == t)
{
Expand All @@ -168,22 +171,22 @@ bool StringToMonth(MonthName& out, AnyString text)

const char* MonthToString(int m, int offset)
{
return monthNames[(m - offset) % 12];
return monthNames[(m - offset) % MONTHS_PER_YEAR];
}

const char* MonthToLowerString(int m, int offset)
{
return monthNamesLower[(m - offset) % 12];
return monthNamesLower[(m - offset) % MONTHS_PER_YEAR];
}

const char* MonthToShortString(int m, int offset)
{
return monthShortNames[(m - offset) % 12];
return monthShortNames[(m - offset) % MONTHS_PER_YEAR];
}

const char* MonthToUpperShortString(int m, int offset)
{
return monthShortUpperNames[(m - offset) % 12];
return monthShortUpperNames[(m - offset) % MONTHS_PER_YEAR];
}

const char* WeekdayToString(int m)
Expand Down Expand Up @@ -424,7 +427,7 @@ bool Calendar::saveToCSVFile(const AnyString& filename) const
line.clear() << "hour begin\thour end\tdays\tday uear begin\tday year end\tfirst weekday";
report.months.push_back(line);

for (uint m = 0; m != 12; ++m)
for (uint m = 0; m != MONTHS_PER_YEAR; ++m)
{
auto& month = months[m];
line.clear();
Expand Down Expand Up @@ -494,10 +497,10 @@ void Calendar::reset()
(void)::memset(months, '\0', sizeof(months));

// Reset months relationship
for (uint m = 0; m != 12 + 1; ++m)
for (uint m = 0; m != MONTHS_PER_YEAR + 1; ++m)
{
uint realmonth = (m + (uint)settings_.firstMonth) % 12;
if (m < 12)
uint realmonth = (m + (uint)settings_.firstMonth) % MONTHS_PER_YEAR;
if (m < MONTHS_PER_YEAR)
{
mapping.months[realmonth] = m;
}
Expand Down
5 changes: 4 additions & 1 deletion src/libs/antares/include/antares/antares/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,15 @@
#include <array>

const unsigned int HOURS_PER_DAY = 24;

const unsigned int MONTHS_PER_YEAR = 12;
const unsigned int WEEKS_PER_YEAR = 53;
const unsigned int DAYS_PER_YEAR = 365;
const unsigned int HOURS_PER_YEAR = 8760;

namespace Antares::Constants
{
extern const std::array<unsigned int, 12> daysPerMonth;
extern const std::array<unsigned int, MONTHS_PER_YEAR> daysPerMonth;
extern const unsigned int nbHoursInAWeek;
} // namespace Antares::Constants

Expand Down
3 changes: 1 addition & 2 deletions src/libs/antares/study/area/scratchpad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ AreaScratchpad::AreaScratchpad(const StudyRuntimeInfos& rinfos, Area& area):
{
// alias to the simulation mode
auto mode = rinfos.mode;
uint nbMonthsPerYear = 12;

for (uint i = 0; i != 168; ++i)
{
Expand Down Expand Up @@ -168,7 +167,7 @@ AreaScratchpad::AreaScratchpad(const StudyRuntimeInfos& rinfos, Area& area):
auto& colPowerOverWater = m[PreproHydro::powerOverWater];
auto& colMaxEnergy = m[PreproHydro::maximumEnergy];

for (uint month = 0; month < nbMonthsPerYear; ++month)
for (uint month = 0; month < MONTHS_PER_YEAR; ++month)
{
valueCol += colMaxEnergy[month] * (1. - colPowerOverWater[month]);
}
Expand Down
30 changes: 15 additions & 15 deletions src/solver/hydro/management/daily.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ struct DebugData
std::array<double, 365> OVF{0};
std::array<double, 365> DEV{0};
std::array<double, 365> VIO{0};
std::array<double, 12> deviationMax{0};
std::array<double, 12> violationMax{0};
std::array<double, 12> WASTE{0};
std::array<double, 12> CoutTotal{0};
std::array<double, 12> previousMonthWaste{0};
std::array<double, MONTHS_PER_YEAR> deviationMax{0};
std::array<double, MONTHS_PER_YEAR> violationMax{0};
std::array<double, MONTHS_PER_YEAR> WASTE{0};
std::array<double, MONTHS_PER_YEAR> CoutTotal{0};
std::array<double, MONTHS_PER_YEAR> previousMonthWaste{0};

Solver::IResultWriter& pWriter;
const Antares::Data::AreaDependantHydroManagementData& data;
Expand Down Expand Up @@ -163,9 +163,9 @@ struct DebugData
<< ".txt";

buffer << "\tNiveau init : " << hydro_specific.monthly[initReservoirLvlMonth].MOL << "\n";
for (uint month = 0; month != 12; ++month)
for (uint month = 0; month != MONTHS_PER_YEAR; ++month)
{
uint realmonth = (initReservoirLvlMonth + month) % 12;
uint realmonth = (initReservoirLvlMonth + month) % MONTHS_PER_YEAR;
uint simulationMonth = calendar.mapping.months[realmonth];

auto daysPerMonth = calendar.months[simulationMonth].days;
Expand Down Expand Up @@ -272,7 +272,7 @@ inline void HydroManagement::prepareDailyOptimalGenerations(
hydro_specific);
}

for (uint month = 0; month != 12; ++month)
for (uint month = 0; month != MONTHS_PER_YEAR; ++month)
{
auto daysPerMonth = calendar_.months[month].days;
assert(daysPerMonth <= maxOPP);
Expand All @@ -298,7 +298,7 @@ inline void HydroManagement::prepareDailyOptimalGenerations(
|| (area.hydro.useHeuristicTarget && !area.hydro.followLoadModulations))
{
dayYear = 0;
for (uint month = 0; month != 12; ++month)
for (uint month = 0; month != MONTHS_PER_YEAR; ++month)
{
auto daysPerMonth = calendar_.months[month].days;
for (uint day = 0; day != daysPerMonth; ++day)
Expand All @@ -313,7 +313,7 @@ inline void HydroManagement::prepareDailyOptimalGenerations(
else
{
dayYear = 0;
for (uint month = 0; month != 12; ++month)
for (uint month = 0; month != MONTHS_PER_YEAR; ++month)
{
uint realmonth = calendar_.months[month].realmonth;
auto daysPerMonth = calendar_.months[month].days;
Expand Down Expand Up @@ -371,7 +371,7 @@ inline void HydroManagement::prepareDailyOptimalGenerations(
if (debugData)
{
dayYear = 0;
for (uint month = 0; month != 12; ++month)
for (uint month = 0; month != MONTHS_PER_YEAR; ++month)
{
auto daysPerMonth = calendar_.months[month].days;

Expand All @@ -386,9 +386,9 @@ inline void HydroManagement::prepareDailyOptimalGenerations(

if (not area.hydro.reservoirManagement)
{
for (uint month = 0; month != 12; ++month)
for (uint month = 0; month != MONTHS_PER_YEAR; ++month)
{
uint realmonth = (initReservoirLvlMonth + month) % 12;
uint realmonth = (initReservoirLvlMonth + month) % MONTHS_PER_YEAR;
uint simulationMonth = calendar_.mapping.months[realmonth];

auto daysPerMonth = calendar_.months[simulationMonth].days;
Expand Down Expand Up @@ -453,9 +453,9 @@ inline void HydroManagement::prepareDailyOptimalGenerations(

Hydro_problem_costs h2o2_optim_costs(parameters_);

for (uint month = 0; month != 12; ++month)
for (uint month = 0; month != MONTHS_PER_YEAR; ++month)
{
uint realmonth = (initReservoirLvlMonth + month) % 12;
uint realmonth = (initReservoirLvlMonth + month) % MONTHS_PER_YEAR;
uint simulationMonth = calendar_.mapping.months[realmonth];

auto daysPerMonth = calendar_.months[simulationMonth].days;
Expand Down
36 changes: 18 additions & 18 deletions src/solver/hydro/management/monthly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ static void CheckHydroAllocationProblem(Data::Area& area,
{
logs.warning() << area.id << ": lvi = " << lvi;
logs.warning() << area.id << ": cost = " << problem.CoutDepassementVolume;
for (uint month = 0; month != 12; ++month)
for (uint month = 0; month != MONTHS_PER_YEAR; ++month)
{
uint realmonth = (initLevelMonth + month) % 12;
uint realmonth = (initLevelMonth + month) % MONTHS_PER_YEAR;
logs.warning() << "month: " << ((realmonth < 10) ? "0" : "") << realmonth
<< ", turb.max: " << problem.TurbineMax[realmonth]
<< ", turb.cible: " << problem.TurbineCible[realmonth]
Expand All @@ -72,9 +72,9 @@ static void CheckHydroAllocationProblem(Data::Area& area,

logs.info();
problem.Volume[initLevelMonth] = lvi;
for (uint month = 0; month != 12; ++month)
for (uint month = 0; month != MONTHS_PER_YEAR; ++month)
{
uint realmonth = (initLevelMonth + month) % 12;
uint realmonth = (initLevelMonth + month) % MONTHS_PER_YEAR;
logs.warning() << "month: " << ((realmonth < 10) ? "0" : "") << realmonth
<< ", turbine: " << problem.Turbine[realmonth]
<< ", volume: " << problem.Volume[realmonth];
Expand All @@ -89,14 +89,14 @@ double HydroManagement::prepareMonthlyTargetGenerations(
{
double total = 0;

for (uint realmonth = 0; realmonth != 12; ++realmonth)
for (uint realmonth = 0; realmonth != MONTHS_PER_YEAR; ++realmonth)
{
total += data.inflows[realmonth];
}

if (not area.hydro.followLoadModulations)
{
for (uint realmonth = 0; realmonth != 12; ++realmonth)
for (uint realmonth = 0; realmonth != MONTHS_PER_YEAR; ++realmonth)
{
hydro_specific.monthly[realmonth].MTG = data.inflows[realmonth];
}
Expand All @@ -106,7 +106,7 @@ double HydroManagement::prepareMonthlyTargetGenerations(

double monthlyMaxDemand = -std::numeric_limits<double>::infinity();

for (uint realmonth = 0; realmonth != 12; ++realmonth)
for (uint realmonth = 0; realmonth != MONTHS_PER_YEAR; ++realmonth)
{
if (hydro_specific.monthly[realmonth].MLE > monthlyMaxDemand)
{
Expand All @@ -117,7 +117,7 @@ double HydroManagement::prepareMonthlyTargetGenerations(
if (!Utils::isZero(monthlyMaxDemand))
{
double coeff = 0.;
for (uint realmonth = 0; realmonth != 12; ++realmonth)
for (uint realmonth = 0; realmonth != MONTHS_PER_YEAR; ++realmonth)
{
assert(hydro_specific.monthly[realmonth].MLE / monthlyMaxDemand >= 0.);
coeff += std::pow(hydro_specific.monthly[realmonth].MLE / monthlyMaxDemand,
Expand All @@ -129,7 +129,7 @@ double HydroManagement::prepareMonthlyTargetGenerations(
coeff = total / coeff;
}

for (uint realmonth = 0; realmonth != 12; ++realmonth)
for (uint realmonth = 0; realmonth != MONTHS_PER_YEAR; ++realmonth)
{
assert(hydro_specific.monthly[realmonth].MLE / monthlyMaxDemand >= 0.);
hydro_specific.monthly[realmonth].MTG = coeff
Expand All @@ -142,7 +142,7 @@ double HydroManagement::prepareMonthlyTargetGenerations(
{
double coeff = total / 12.;

for (uint realmonth = 0; realmonth != 12; ++realmonth)
for (uint realmonth = 0; realmonth != MONTHS_PER_YEAR; ++realmonth)
{
hydro_specific.monthly[realmonth].MTG = coeff;
}
Expand Down Expand Up @@ -187,9 +187,9 @@ void HydroManagement::prepareMonthlyOptimalGenerations(double* random_reservoir_
problem.CoutViolMaxDuVolumeMin = 1e5;
problem.VolumeInitial = lvi;

for (unsigned month = 0; month != 12; ++month)
for (unsigned month = 0; month != MONTHS_PER_YEAR; ++month)
{
uint realmonth = (initReservoirLvlMonth + month) % 12;
uint realmonth = (initReservoirLvlMonth + month) % MONTHS_PER_YEAR;

uint simulationMonth = calendar_.mapping.months[realmonth];
uint firstDay = calendar_.months[simulationMonth].daysYear.first;
Expand All @@ -212,9 +212,9 @@ void HydroManagement::prepareMonthlyOptimalGenerations(double* random_reservoir_
CheckHydroAllocationProblem(area, problem, initReservoirLvlMonth, lvi);
}

for (uint month = 0; month != 12; ++month)
for (uint month = 0; month != MONTHS_PER_YEAR; ++month)
{
uint realmonth = (initReservoirLvlMonth + month) % 12;
uint realmonth = (initReservoirLvlMonth + month) % MONTHS_PER_YEAR;

hydro_specific.monthly[realmonth].MOG = problem.Turbine[month]
* area.hydro.reservoirCapacity;
Expand Down Expand Up @@ -248,15 +248,15 @@ void HydroManagement::prepareMonthlyOptimalGenerations(double* random_reservoir_
{
auto& reservoirLevel = area.hydro.reservoirLevel[Data::PartHydro::average];

for (uint realmonth = 0; realmonth != 12; ++realmonth)
for (uint realmonth = 0; realmonth != MONTHS_PER_YEAR; ++realmonth)
{
hydro_specific.monthly[realmonth].MOG = data.inflows[realmonth];
hydro_specific.monthly[realmonth].MOL = reservoirLevel[realmonth];
}
}

#ifndef NDEBUG
for (uint realmonth = 0; realmonth != 12; ++realmonth)
for (uint realmonth = 0; realmonth != MONTHS_PER_YEAR; ++realmonth)
{
assert(!std::isnan(hydro_specific.monthly[realmonth].MOG)
&& "nan value detected for MOG");
Expand Down Expand Up @@ -292,9 +292,9 @@ void HydroManagement::prepareMonthlyOptimalGenerations(double* random_reservoir_

buffer << '\t' << "\tInflows" << '\t' << "\tTarget Gen." << "\tTurbined" << "\tLevels"
<< '\t' << "\tLvl min" << '\t' << "\tLvl max\n";
for (uint month = 0; month != 12; ++month)
for (uint month = 0; month != MONTHS_PER_YEAR; ++month)
{
uint realmonth = (initReservoirLvlMonth + month) % 12;
uint realmonth = (initReservoirLvlMonth + month) % MONTHS_PER_YEAR;

uint simulationMonth = calendar_.mapping.months[realmonth];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
#include "antares/solver/simulation/opt_time_writer.h"
#include "antares/solver/simulation/timeseries-numbers.h"
#include "antares/solver/ts-generator/generator.h"
#include "antares/solver/variable/constants.h"
#include "antares/solver/variable/print.h"

namespace Antares::Solver::Simulation
Expand Down
Loading

0 comments on commit ff40ec2

Please sign in to comment.