Skip to content

Commit aeec3d6

Browse files
committed
v495 - support for management in FEWS
-read Named constant as string (ParseFEWSRunInfo.cpp) -pass Options to AddReservoirConstraints (DemandOptimization.h/.cpp) -handle final blank line in FEWS csv in :LookupTableFromCSV command (ParseManagementFile.cpp) -properly handle diversion indices in Demand Optimization (DemandOptimization.cpp)
1 parent 2e43f41 commit aeec3d6

File tree

4 files changed

+35
-15
lines changed

4 files changed

+35
-15
lines changed

src/DemandOptimization.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,7 @@ void CDemandOptimizer::InitializePostRVMRead(CModel* pModel, const optStruct& Op
742742
// Convert reservoir commands to user-specified constraints
743743
// allowed here because it doesnt introduce new DVs
744744
//------------------------------------------------------------------
745-
AddReservoirConstraints();
745+
AddReservoirConstraints(Options);
746746

747747
// Calculate penalty units correction for reservoirs
748748
//------------------------------------------------------------------
@@ -858,8 +858,9 @@ void CDemandOptimizer::InitializePostRVMRead(CModel* pModel, const optStruct& Op
858858
}
859859

860860
string tmpstr,tmpstr2;
861-
cout<<" # History Items: " <<_nHistoryItems<<endl;
861+
cout<<" # Lookback intervals: "<<_nHistoryItems<<endl;
862862
cout<<" # Slack Vars: " <<_nSlackVars<<endl;
863+
cout<<" # Named constants : " <<_nUserConstants<<endl;
863864
cout<<" # Constraints/Goals: " <<_nGoals<<endl;
864865
for (int i = 0; i < _nGoals; i++) {
865866
if (_pGoals[i]->is_goal){tmpstr="[GOAL] "; }
@@ -935,7 +936,7 @@ void TokenizeString(string instring, char **s, int &Len)
935936
// these are done as if the corresponding constraints were read in as expressions from the .rvm file
936937
// initialization step: called from InitializePostRVMFileRead()
937938
//
938-
void CDemandOptimizer::AddReservoirConstraints()
939+
void CDemandOptimizer::AddReservoirConstraints(const optStruct &Options)
939940
{
940941
int p;
941942
CSubBasin *pSB;
@@ -960,7 +961,7 @@ void CDemandOptimizer::AddReservoirConstraints()
960961
int k=pSB->GetReservoir()->GetHRUIndex();
961962
if (k==DOESNT_EXIST){
962963
advice="AddReservoirConstraints: The reservoir in subbasin "+SBIDs+" doesnt have an :HRUID - this will negatively impact the units scaling of management optimization penalties.";
963-
ExitGracefully(advice.c_str(), BAD_DATA_WARN);
964+
WriteAdvisory(advice.c_str(), Options.noisy);
964965
}
965966

966967
//Max Stage constraints
@@ -1301,6 +1302,7 @@ void CDemandOptimizer::SolveDemandProblem(CModel *pModel, const optStruct &Optio
13011302
double *h_iter =new double [_pModel->GetNumSubBasins()];
13021303
double *Q_iter =new double [_pModel->GetNumSubBasins()];
13031304
int *lprow =new int [_pModel->GetNumSubBasins()]; //index of goal equation for non-linear reservoir stage discharge curve in subbasin p
1305+
int *lpsbrow=new int [_pModel->GetNumSubBasins()]; //index of constraint equation for subbasin reaches
13041306

13051307
// instantiate linear programming solver
13061308
// ----------------------------------------------------------------
@@ -1642,6 +1644,8 @@ void CDemandOptimizer::SolveDemandProblem(CModel *pModel, const optStruct &Optio
16421644
retval = lp_lib::add_constraintex(pLinProg,i,row_val,col_ind,ROWTYPE_EQ,RHS);
16431645
ExitGracefullyIf(retval==0,"SolveDemandProblem::Error adding mass balance constraint",RUNTIME_ERR);
16441646
IncrementAndSetRowName(pLinProg,rowcount,"reach_MB_"+to_string(pSB->GetID()));
1647+
1648+
lpsbrow[p]=lp_lib::get_Nrows(pLinProg);
16451649
}
16461650
}
16471651

@@ -2051,12 +2055,12 @@ void CDemandOptimizer::SolveDemandProblem(CModel *pModel, const optStruct &Optio
20512055
sum_diverted+=div_Q;
20522056
}
20532057

2054-
RHS=lp_lib::get_rh(pLinProg,lprow[p]);//TMP DEBUG - NOT SAME LPROW AS RESERVOIRS !!!
2058+
RHS=lp_lib::get_rh(pLinProg,lpsbrow[p]);
20552059

20562060
RHS+=(sum_diverted-aDivGuess[p]);
20572061
aDivGuess[p]=sum_diverted;
20582062

2059-
lp_lib::set_rh (pLinProg,lprow[p],RHS);
2063+
lp_lib::set_rh (pLinProg,lpsbrow[p],RHS);
20602064
}
20612065
}
20622066
}/*end iteration loop*/
@@ -2076,6 +2080,7 @@ void CDemandOptimizer::SolveDemandProblem(CModel *pModel, const optStruct &Optio
20762080
delete [] h_iter;
20772081
delete [] Q_iter;
20782082
delete [] lprow;
2083+
delete [] lpsbrow;
20792084
delete [] aDivert;
20802085
delete [] aDivGuess;
20812086

src/DemandOptimization.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ class CDemandOptimizer
270270
//Called during initialization
271271
void AddDecisionVar(const decision_var *pDV);
272272
bool UserTimeSeriesExists(string TSname) const;
273-
void AddReservoirConstraints();
273+
void AddReservoirConstraints(const optStruct &Options);
274274
void IdentifyUpstreamDemands();
275275
bool VariableNameExists(const string &name) const;
276276

src/ParseFEWSRunInfo.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -293,14 +293,21 @@ bool ParseNetCDFRunInfoFile(CModel *&pModel, optStruct &Options, bool runname_ov
293293

294294
// NamedConstant(s) ----------------------------------------------------
295295

296-
if (att_name_s.substr(0, 14) == "NamedConstant_") {
296+
if (att_name_s.substr(0, 14) == "NamedConstant_")
297+
{
297298
string name=att_name_s.substr(14,att_name_s.length());
298299

299-
retval=nc_get_att_double(ncid,varid_props,att_name,&att_value);
300+
retval = nc_inq_attlen(ncid,varid_props,att_name,&att_len); HandleNetCDFErrors(retval);
301+
char* my_value = new char[att_len + 1];
302+
retval = nc_get_att_text(ncid,varid_props,att_name,my_value); HandleNetCDFErrors(retval);// read attribute text
303+
my_value[att_len] = '\0';// add string determining character
304+
att_value=s_to_d(my_value);
305+
delete my_value;
300306

301307
cout<<"RUN INFO NAMED CONSTANT = "<<name<<" value = "<<att_value <<endl;
302-
303-
pModel->GetManagementOptimizer()->AddUserConstant(att_name,att_value);
308+
if (Options.management_optimization){
309+
pModel->GetManagementOptimizer()->AddUserConstant(name,att_value);
310+
}
304311
}
305312

306313
// TargetStageSet_ ----------------------------------------------------
@@ -726,6 +733,11 @@ bool ParseNetCDFFlowStateFile(CModel*& pModel,const optStruct& Options)
726733
bool ParseNetCDFManagementFile(CModel*& pModel,const optStruct& Options)
727734
{
728735
if(Options.maninfo_filename=="") { return true; }
736+
if (!Options.management_optimization)
737+
{
738+
WriteWarning(":FEWSManagmentInfoFile command will be ignored; Management optimization is not enabled.",Options.noisy);
739+
return true;
740+
}
729741

730742
#ifdef _RVNETCDF_
731743
int ncid; // NetCDF file id
@@ -797,8 +809,10 @@ bool ParseNetCDFManagementFile(CModel*& pModel,const optStruct& Options)
797809
manfile, "UserTimeSeries_"+userTSnames[i],
798810
"None", "time", //this assumes "time" is FEWS standard for time dimension
799811
0, 0.0, 1.0, 0.0);
800-
801-
pModel->GetManagementOptimizer()->AddUserTimeSeries(pTS);
812+
if (Options.management_optimization){
813+
cout<<"ADDING USER TIME SERIES "<<endl;
814+
pModel->GetManagementOptimizer()->AddUserTimeSeries(pTS);
815+
}
802816
}
803817
delete [] userTSnames;
804818

src/ParseManagementFile.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -851,26 +851,27 @@ bool ParseManagementFile(CModel *&pModel,const optStruct &Options)
851851
N=0;
852852
do {
853853
eof=ppCSV->Tokenize(s,Len);
854-
if(IsComment(s[0],Len)) { }
854+
if ((eof) || (IsComment(s[0],Len))) { }
855855
else {
856856
if(Len>=2) {
857857
aX[N] = s_to_d(s[0]);
858858
aY[N] = s_to_d(s[1]);
859859
N++;
860860
}
861861
else {
862+
delete [] aX;delete [] aY;delete ppCSV;
862863
WriteWarning("Incorrect line length (<2) in :LookupTableFromCSV table",Options.noisy);
863864
break;
864865
}
865866
}
866867
} while (!eof);
867868

868869
LUCSV.close();
869-
870870
CLookupTable *pLUT = new CLookupTable(name,aX,aY,N);
871871
pDO->AddUserLookupTable(pLUT);
872872
delete [] aX;
873873
delete [] aY;
874+
delete ppCSV;
874875
break;
875876
}
876877

0 commit comments

Comments
 (0)