forked from CSHS-CWRA/RavenHydroFramework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.cpp
More file actions
3091 lines (2839 loc) · 120 KB
/
Copy pathModel.cpp
File metadata and controls
3091 lines (2839 loc) · 120 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*----------------------------------------------------------------
Raven Library Source Code
Copyright (c) 2008-2024 the Raven Development Team
----------------------------------------------------------------*/
#include "Model.h"
#include "EnergyTransport.h"
bool IsContinuousFlowObs(const CTimeSeriesABC *pObs,long long SBID);
/*****************************************************************
Constructor/Destructor
------------------------------------------------------------------
*****************************************************************/
//////////////////////////////////////////////////////////////////
/// \brief Implementation of the Model constructor
///
/// \param SM [in] Input soil model object
/// \param nsoillayers [in] Integer number of soil layers
//
CModel::CModel(const int nsoillayers,
const optStruct &Options)
{
int i;
_nSubBasins=0; _pSubBasins=NULL;
_nHydroUnits=0; _pHydroUnits=NULL;
_nHRUGroups=0; _pHRUGroups=NULL;
_nSBGroups=0; _pSBGroups=NULL;
_nGauges=0; _pGauges=NULL;
_nForcingGrids=0; _pForcingGrids=NULL;
_nProcesses=0; _pProcesses=NULL;
_nCustomOutputs=0; _pCustomOutputs=NULL;
_nTransParams=0; _pTransParams=NULL;
_nClassChanges=0; _pClassChanges=NULL;
_nParamOverrides=0; _pParamOverrides=NULL;
_nStateVarOverrides=0;_pStateVarOverrides=NULL;
_nObservedTS=0; _pObservedTS=NULL; _pModeledTS=NULL; _aObsIndex=NULL;
_nObsWeightTS =0; _pObsWeightTS=NULL;
_nDiagnostics=0; _pDiagnostics=NULL;
_nDiagPeriods=0; _pDiagPeriods=NULL;
_nAggDiagnostics=0; _pAggDiagnostics=NULL;
_nPerturbations=0; _pPerturbations=NULL;
_nLandUseClasses=0; _pLandUseClasses=NULL;
_nAllSoilClasses = 0; _pAllSoilClasses = NULL;
_numVegClasses = 0; _pAllVegClasses = NULL;
_nAllTerrainClasses = 0; _pAllTerrainClasses = NULL;
_nAllSoilProfiles = 0; _pAllSoilProfiles = NULL;
_nAllChannelXSects = 0; _pAllChannelXSects = NULL;
_nConvVariables = 0;
_nTotalConnections=0;
_nTotalLatConnections=0;
_WatershedArea=0;
_aSubBasinOrder =NULL; _maxSubBasinOrder=0;
_aOrderedSBind =NULL;
_aDownstreamInds=NULL;
_aDAscale =NULL; //Initialized in InitializeDataAssimilation
_aDAscale_last =NULL;
_aDAQadjust =NULL;
_aDADrainSum =NULL;
_aDADownSum =NULL;
_aDAlength =NULL;
_aDAtimesince =NULL;
_aDAoverride =NULL;
_aDAobsQ =NULL;
_aDAobsQ2 =NULL;
_aDASinceLastBlank=NULL;
_pOptStruct = &Options;
_pGlobalParams = new CGlobalParams();
_HYDRO_ncid =-9;
_STORAGE_ncid =-9;
_FORCINGS_ncid=-9;
_RESSTAGE_ncid=-9;
_RESMB_ncid =-9;
_PETBlends_N=0;
_PETBlends_type=NULL;
_PETBlends_wts=NULL;
_PotMeltBlends_N=0;
_PotMeltBlends_type=NULL;
_PotMeltBlends_wts=NULL;
ExitGracefullyIf(nsoillayers<1,
"CModel constructor::improper number of soil layers. SoilModel not specified?",BAD_DATA);
//Initialize Lookup table for state variable indices
for (i=0;i< MAX_STATE_VAR_TYPES;i++){
for (int m=0;m<MAX_SV_LAYERS;m++){
_aStateVarIndices[i][m]=DOESNT_EXIST;
}
}
//determine first group of state variables based upon soil model
//SW, atmosphere, atmos_precip always present, one for each soil layer, and 1 for GW (unless lumped)
_nStateVars=5+nsoillayers;
_aStateVarType =new sv_type [_nStateVars];
_aStateVarLayer=new int [_nStateVars];
_nSoilVars =nsoillayers;
_aStateVarType[0]=SURFACE_WATER; _aStateVarLayer[0]=DOESNT_EXIST; _aStateVarIndices[(int)(SURFACE_WATER)][0]=0;
_aStateVarType[1]=ATMOSPHERE; _aStateVarLayer[1]=DOESNT_EXIST; _aStateVarIndices[(int)(ATMOSPHERE )][0]=1;
_aStateVarType[2]=ATMOS_PRECIP; _aStateVarLayer[2]=DOESNT_EXIST; _aStateVarIndices[(int)(ATMOS_PRECIP )][0]=2;
_aStateVarType[3]=PONDED_WATER; _aStateVarLayer[3]=DOESNT_EXIST; _aStateVarIndices[(int)(PONDED_WATER )][0]=3;
_aStateVarType[4]=RUNOFF; _aStateVarLayer[4]=RUNOFF; _aStateVarIndices[(int)(RUNOFF )][0]=4;
int count=0;
for (i=5;i<5+_nSoilVars;i++)
{
_aStateVarType [i]=SOIL;
_aStateVarLayer[i]=count;
_aStateVarIndices[(int)(SOIL)][count]=i;
count++;
}
_lake_sv=0; //by default, rain on lake goes direct to surface storage [0]
_aGaugeWeights =NULL; //Initialized in Initialize
_aGaugeWtTemp =NULL;
_aGaugeWtPrecip =NULL;
_aCumulativeBal =NULL;
_aFlowBal =NULL;
_aCumulativeLatBal=NULL;
_aFlowLatBal =NULL;
_CumulInput =0.0;
_CumulOutput =0.0;
_initWater =0.0;
_UTM_zone=-1;
_nOutputTimes=0; _aOutputTimes=NULL;
_currOutputTimeInd=0;
_pOutputGroup=NULL;
_aShouldApplyProcess=NULL; //Initialized in Initialize
_pTransModel=new CTransportModel(this);
_pGWModel = NULL; //GW MIGRATE -should initialize with empty GW model
_pDO =NULL;
#ifdef _MODFLOW_USG_
_pGWModel = new CGroundwaterModel(this);
#endif
_pEnsemble = NULL;
_pStateVar = NULL;
}
/////////////////////////////////////////////////////////////////
/// \brief Implementation of the default destructor
//
CModel::~CModel()
{
if (DESTRUCTOR_DEBUG){cout<<"DELETING MODEL"<<endl;}
int c,f,g,i,j,k,kk,p;
CloseOutputStreams();
for (p=0;p<_nSubBasins; p++){delete _pSubBasins [p];} delete [] _pSubBasins; _pSubBasins=NULL;
for (k=0;k<_nHydroUnits; k++){delete _pHydroUnits [k];} delete [] _pHydroUnits; _pHydroUnits=NULL;
for (g=0;g<_nGauges; g++){delete _pGauges [g];} delete [] _pGauges; _pGauges=NULL;
for (f=0;f<_nForcingGrids; f++){delete _pForcingGrids [f];} delete [] _pForcingGrids; _pForcingGrids=NULL;
for (j=0;j<_nProcesses; j++){delete _pProcesses [j];} delete [] _pProcesses; _pProcesses=NULL;
for (c=0;c<_nCustomOutputs;c++){delete _pCustomOutputs[c];} delete [] _pCustomOutputs;_pCustomOutputs=NULL;
for (i=0;i<_nObservedTS; i++){delete _pObservedTS [i];} delete [] _pObservedTS; _pObservedTS=NULL;
if (_pModeledTS != NULL){
for (i = 0; i < _nObservedTS; i++){ delete _pModeledTS[i]; } delete[] _pModeledTS; _pModeledTS = NULL;
}
for (i=0;i<_nObsWeightTS; i++){delete _pObsWeightTS [i];} delete [] _pObsWeightTS; _pObsWeightTS=NULL;
for (j=0;j<_nDiagnostics; j++){delete _pDiagnostics [j];} delete [] _pDiagnostics; _pDiagnostics=NULL;
for (j=0;j<_nDiagPeriods; j++){delete _pDiagPeriods [j];} delete [] _pDiagPeriods; _pDiagPeriods=NULL;
for (j=0;j<_nAggDiagnostics; j++){delete _pAggDiagnostics[j];} delete [] _pAggDiagnostics; _pAggDiagnostics=NULL;
if (_aCumulativeBal!=NULL){
for (k=0;k<_nHydroUnits; k++){delete [] _aCumulativeBal[k];} delete [] _aCumulativeBal; _aCumulativeBal=NULL;
}
if (_aFlowBal!=NULL){
for (k=0;k<_nHydroUnits; k++){delete [] _aFlowBal[k]; } delete [] _aFlowBal; _aFlowBal=NULL;
}
if (_aCumulativeLatBal!=NULL){delete [] _aCumulativeLatBal; _aCumulativeLatBal=NULL;}
if (_aFlowLatBal !=NULL){delete [] _aFlowLatBal; _aFlowLatBal=NULL;}
if (_aGaugeWeights!=NULL){
for (k=0;k<_nHydroUnits; k++){delete [] _aGaugeWeights[k]; } delete [] _aGaugeWeights; _aGaugeWeights=NULL;
}
if (_aGaugeWtPrecip!=NULL){
for (k=0;k<_nHydroUnits; k++){delete [] _aGaugeWtPrecip[k]; } delete [] _aGaugeWtPrecip; _aGaugeWtPrecip=NULL;
}
if (_aGaugeWtTemp!=NULL){
for (k=0;k<_nHydroUnits; k++){delete [] _aGaugeWtTemp[k]; } delete [] _aGaugeWtTemp; _aGaugeWtTemp=NULL;
}
if (_aShouldApplyProcess!=NULL){
for (k=0;k<_nProcesses; k++){delete [] _aShouldApplyProcess[k]; } delete [] _aShouldApplyProcess; _aShouldApplyProcess=NULL;
}
for (kk=0;kk<_nHRUGroups;kk++) {delete _pHRUGroups[kk]; } delete [] _pHRUGroups; _pHRUGroups =NULL;
for (kk=0;kk<_nSBGroups;kk++ ) {delete _pSBGroups[kk]; } delete [] _pSBGroups; _pSBGroups =NULL;
for (j=0;j<_nTransParams;j++) {delete _pTransParams[j]; } delete [] _pTransParams; _pTransParams=NULL;
for (j=0;j<_nClassChanges;j++) {delete _pClassChanges[j]; } delete [] _pClassChanges; _pClassChanges=NULL;
for (j=0;j<_nParamOverrides;j++) {delete _pParamOverrides[j]; } delete [] _pParamOverrides; _pParamOverrides=NULL;
for (j=0;j<_nStateVarOverrides;j++){delete _pStateVarOverrides[j];} delete [] _pStateVarOverrides; _pStateVarOverrides=NULL;
for (i=0;i<_nPerturbations; i++)
{
delete [] _pPerturbations[i]->eps;
delete _pPerturbations [i];
}
delete [] _pPerturbations;
delete [] _aStateVarType; _aStateVarType=NULL;
delete [] _aStateVarLayer; _aStateVarLayer=NULL;
delete [] _aSubBasinOrder; _aSubBasinOrder=NULL;
delete [] _aOrderedSBind; _aOrderedSBind=NULL;
delete [] _aDownstreamInds;_aDownstreamInds=NULL;
delete [] _aOutputTimes; _aOutputTimes=NULL;
delete [] _aObsIndex; _aObsIndex=NULL;
delete [] _aDAscale; _aDAscale=NULL;
delete [] _aDAscale_last; _aDAscale_last=NULL;
delete [] _aDAQadjust; _aDAQadjust=NULL;
delete [] _aDADrainSum; _aDADrainSum=NULL;
delete [] _aDADownSum; _aDADownSum=NULL;
delete [] _aDAlength; _aDAlength=NULL;
delete [] _aDAtimesince; _aDAtimesince=NULL;
delete [] _aDAoverride; _aDAoverride=NULL;
delete [] _aDAobsQ; _aDAobsQ=NULL;
delete [] _aDAobsQ2; _aDAobsQ2=NULL;
delete [] _aDASinceLastBlank; _aDASinceLastBlank=NULL;
this->DestroyAllLanduseClasses();
this->DestroyAllSoilClasses();
this->DestroyAllVegClasses();
this->DestroyAllTerrainClasses();
this->DestroyAllSoilProfiles();
this->DestroyAllChannelXSections();
delete _pTransModel;
delete _pEnsemble;
delete _pGWModel;
delete _pStateVar;
delete _pDO;
delete [] _PETBlends_type;
delete [] _PETBlends_wts;
delete [] _PotMeltBlends_type;
delete [] _PotMeltBlends_wts;
}
/*****************************************************************
Accessors
------------------------------------------------------------------
*****************************************************************/
//////////////////////////////////////////////////////////////////
/// \brief Returns pointer to global parameters object
/// \return Pointer to global parameters object
//
CGlobalParams* CModel::GetGlobalParams() const { return _pGlobalParams;}
//////////////////////////////////////////////////////////////////
/// \brief Returns number of sub basins in model
/// \return Integer number of sub basins
//
int CModel::GetNumSubBasins () const{return _nSubBasins;}
//////////////////////////////////////////////////////////////////
/// \brief Returns number of SB groups
/// \return Integer number of SB groups
//
int CModel::GetNumSubBasinGroups() const { return _nSBGroups; }
//////////////////////////////////////////////////////////////////
/// \brief Returns number of HRUs in model
/// \return Integer number of HRUs
//
int CModel::GetNumHRUs () const{return _nHydroUnits;}
//////////////////////////////////////////////////////////////////
/// \brief Returns number of HRU groups
/// \return Integer number of HRU groups
//
int CModel::GetNumHRUGroups () const{return _nHRUGroups;}
//////////////////////////////////////////////////////////////////
/// \brief Returns number of gauges in model
/// \return Integer number of gauges in model
//
int CModel::GetNumGauges () const{return _nGauges;}
//////////////////////////////////////////////////////////////////
/// \brief Returns number of gridded forcings in model
/// \return Integer number of gridded forcings in model
//
int CModel::GetNumForcingGrids () const{return _nForcingGrids;}
//////////////////////////////////////////////////////////////////
/// \brief Returns number of state variables per HRU in model
/// \return Integer number of state variables per HRU in model
//
int CModel::GetNumStateVars () const{return _nStateVars;}
//////////////////////////////////////////////////////////////////
/// \brief Returns number of soil layers
/// \return Integer number of soil layers
//
int CModel::GetNumSoilLayers () const{return _nSoilVars;}
//////////////////////////////////////////////////////////////////
/// \brief Returns number of hydrologic processes simulated by model
/// \return Integer number of hydrologic processes simulated by model
//
int CModel::GetNumProcesses () const{return _nProcesses;}
//////////////////////////////////////////////////////////////////
/// \brief Returns total modeled watershed area
/// \return total modeled watershed area [km2]
//
double CModel::GetWatershedArea () const{return _WatershedArea;}
//////////////////////////////////////////////////////////////////
/// \brief Returns number of observation time series
/// \return number of observation time series
//
int CModel::GetNumObservedTS() const { return _nObservedTS; }
//////////////////////////////////////////////////////////////////
/// \brief Returns observation time series i
/// \param i [in] index of observation time series
/// \return pointer to observation time series i
//
const CTimeSeriesABC *CModel::GetObservedTS(const int i) const
{
return _pObservedTS[i];
}
//////////////////////////////////////////////////////////////////
/// \brief Returns observed flow in basin p at time step n
/// \param p [in] subbasin index
/// \param n [in] time step index
/// \return observed flow in basin p at time step n, or RAV_BLANK_DATA if no observation available
/// \todo[optimize] - this call could be slow with lots of observations
//
double CModel::GetObservedFlow(const int p, const int n) const
{
long long SBID=_pSubBasins[p]->GetID();
for(int i=0; i<_nObservedTS; i++) {
if(IsContinuousFlowObs(_pObservedTS[i], SBID)) {
return _pObservedTS[i]->GetSampledValue(n);
}
}
return RAV_BLANK_DATA;
}
//////////////////////////////////////////////////////////////////
/// \brief Returns simulated equivalent of observation time series i
/// \param i [in] index of observation time series
/// \return pointer to simulated equivalent of observation time series i
//
const CTimeSeriesABC* CModel::GetSimulatedTS(const int i) const {
return _pModeledTS[i];
}
//////////////////////////////////////////////////////////////////
/// \brief Returns specific hydrologic process denoted by parameter
/// \param j [in] Process index
/// \return pointer to hydrologic process corresponding to passed index j
//
CHydroProcessABC *CModel::GetProcess(const int j) const
{
#ifdef _STRICTCHECK_
ExitGracefullyIf((j<0) || (j>=_nProcesses),"CModel GetProcess::improper index",BAD_DATA);
#endif
return _pProcesses[j];
}
//////////////////////////////////////////////////////////////////
/// \brief Returns specific gauge denoted by index
/// \param g [in] Gauge index
/// \return pointer to gauge corresponding to passed index g
//
CGauge *CModel::GetGauge(const int g) const
{
#ifdef _STRICTCHECK_
ExitGracefullyIf((g<0) || (g>=_nGauges),"CModel GetGauge::improper index",BAD_DATA);
#endif
return _pGauges[g];
}
//////////////////////////////////////////////////////////////////
/// \brief Returns specific forcing grid denoted by index
/// \param f [in] Forcing Grid index
/// \return pointer to gauge corresponding to passed index g
//
CForcingGrid *CModel::GetForcingGrid(const forcing_type &ftyp) const
{
int f=GetForcingGridIndexFromType(ftyp);
#ifdef _STRICTCHECK_
if((f<0) || (f>=_nForcingGrids)) { cout<<"Invalid forcing type: "<<ForcingToString(ftyp)<<" ("<<f<<")"<<endl; }
ExitGracefullyIf((f<0) || (f>=_nForcingGrids),"CModel GetForcingGrid::improper index",RUNTIME_ERR);
ExitGracefullyIf(_pForcingGrids[f]==NULL,"CModel GetForcingGrid:: NULL forcing grid",RUNTIME_ERR);
#endif
return _pForcingGrids[f];
}
//////////////////////////////////////////////////////////////////
/// \brief Returns specific HRU denoted by index k
/// \param k [in] HRU index
/// \return pointer to HRU corresponding to passed index k
//
CHydroUnit *CModel::GetHydroUnit(const int k) const
{
#ifdef _STRICTCHECK_
ExitGracefullyIf((k<0) || (k>=_nHydroUnits),"CModel GetHydroUnit::improper index",BAD_DATA);
#endif
return _pHydroUnits[k];
}
//////////////////////////////////////////////////////////////////
/// \brief Returns specific HRU with HRU identifier HRUID
/// \param HRUID [in] HRU identifier
/// \return pointer to HRU corresponding to passed ID HRUID, NULL if no such HRU exists
//
CHydroUnit *CModel::GetHRUByID(const long long int HRUID) const
{
static int last_k=0;
//smart find
int k;
for (int i=0;i<_nHydroUnits;i++){
k=NearSearchIndex(i,last_k,_nHydroUnits);
if (HRUID==_pHydroUnits[k]->GetHRUID()){ last_k=k; return _pHydroUnits[k];}
}
return NULL;
}
//////////////////////////////////////////////////////////////////
/// \brief Returns specific HRU group denoted by parameter kk
/// \param kk [in] HRU group index
/// \return pointer to HRU group corresponding to passed index kk
//
CHRUGroup *CModel::GetHRUGroup(const int kk) const
{
#ifdef _STRICTCHECK_
ExitGracefullyIf((kk<0) || (kk>=_nHRUGroups),"CModel GetHRUGroup::improper index",BAD_DATA);
#endif
return _pHRUGroups[kk];
}
//////////////////////////////////////////////////////////////////
/// \brief Returns specific HRU group denoted by string parameter
/// \param name [in] String name of HRU group
/// \return pointer to HRU group corresponding to passed name, or NULL if this group doesn't exist
//
CHRUGroup *CModel::GetHRUGroup(const string name) const
{
for (int kk=0;kk<_nHRUGroups;kk++){
if (!name.compare(_pHRUGroups[kk]->GetName())){
return _pHRUGroups[kk];
}
}
return NULL;
}
//////////////////////////////////////////////////////////////////
/// \brief Returns true if HRU with global index k is in specified HRU Group
///
/// \param k [in] HRU global index
/// \param HRUGroupName [in] String name of HRU group
/// \return true if HRU k is in HRU Group specified by HRUGroupName
//
bool CModel::IsInHRUGroup(const int k, const string HRUGroupName) const
{
CHRUGroup *pGrp=NULL;
pGrp=GetHRUGroup(HRUGroupName);
if (pGrp == NULL){ return false; }//throw warning?
int kk = pGrp->GetGlobalIndex();
for (int k_loc=0; k_loc<_pHRUGroups[kk]->GetNumHRUs(); k_loc++)
{
if (_pHRUGroups[kk]->GetHRU(k_loc)->GetGlobalIndex()==k){return true;}
}
return false;
}
//////////////////////////////////////////////////////////////////
/// \brief Returns specific Sub basin denoted by index parameter
///
/// \param p [in] Sub basin index
/// \return pointer to the Sub basin object corresponding to passed index
//
CSubBasin *CModel::GetSubBasin(const int p) const
{
#ifdef _STRICTCHECK_
ExitGracefullyIf((p<0) || (p>=_nSubBasins),"CModel GetSubBasin::improper index",BAD_DATA);
#endif
return _pSubBasins[p];
}
//////////////////////////////////////////////////////////////////
/// \brief Returns index of subbasin downstream from subbasin referred to by index
/// \param p [in] List index for accessing subbasin
/// \return downstream subbasin index, if input index is valid; -1 if there is no downstream basin
//
int CModel::GetDownstreamBasin(const int p) const
{
#ifdef _STRICTCHECK_
ExitGracefullyIf((p<0) || (p>=_nSubBasins),"GetDownstreamBasin: Invalid index",BAD_DATA);
#endif
return _aDownstreamInds[p];
}
//////////////////////////////////////////////////////////////////
/// \brief Returns subbasin object corresponding to passed subbasin ID
/// \param SBID [in] long long integer sub basin ID
/// \return pointer to Sub basin object corresponding to passed ID, if ID is valid
//
CSubBasin *CModel::GetSubBasinByID(const long long SBID) const
{
static int last_p=0;
int p;
if (SBID < 0) { return NULL; }
//smart find
for (int i=0;i<_nSubBasins;i++){
p = NearSearchIndex(i, last_p, _nSubBasins);
if (_pSubBasins[p]->GetID()==SBID){last_p=p; return _pSubBasins[p];}
}
return NULL;
}
//////////////////////////////////////////////////////////////////
/// \brief Returns sub basin index corresponding to passed subbasin ID
/// \param SBID [in] Integer subbasin ID
/// \return SubBasin index corresponding to passed ID, if ID is valid
//
int CModel::GetSubBasinIndex(const long long SBID) const
{
static int last_p = 0;
int p;
if (SBID<0){return DOESNT_EXIST;}
//smart find
for (int i = 0; i < _nSubBasins; i++) {
p = NearSearchIndex(i, last_p, _nSubBasins);
if (_pSubBasins[p]->GetID() == SBID) { last_p = p; return p; }
}
return INDEX_NOT_FOUND;
}
//////////////////////////////////////////////////////////////////
/// \brief Returns array of pointers to subbasins upstream of subbasin SBID, including that subbasin
/// \param SBID [in] long long int subbasin ID
/// \param nUpstream [out] size of array of pointers of subbasins
/// \return array of pointers to subbasins upstream of subbasin SBID, including that subbasin
//
const CSubBasin **CModel::GetUpstreamSubbasins(const long long SBID,int &nUpstream) const
{
static const CSubBasin **pSBs=new const CSubBasin *[_nSubBasins];
bool *isUpstr=new bool [_nSubBasins];
for(int p=0;p<_nSubBasins;p++) { isUpstr[p]=false; }
int p=GetSubBasinIndex(SBID);
if((p==DOESNT_EXIST) || (p==INDEX_NOT_FOUND)) {
string warn="CModel::GetUpstreamSubbasins: invalid subbasin ID "+to_string(SBID)+" (:ReservoirDownstreamDemand command ? )";
ExitGracefully(warn.c_str(),BAD_DATA);return NULL;
}
isUpstr[p]=true;
const int MAX_ITER=1000;
int numUpstr=0;
int numUpstrOld=1;
int iter=0;
int down_p;
do
{
numUpstrOld=numUpstr;
for(p=0;p<_nSubBasins;p++) {
down_p=GetSubBasinIndex(_pSubBasins[p]->GetDownstreamID());
if(down_p!=DOESNT_EXIST) {
if(isUpstr[down_p]==true) { isUpstr[p]=true;}
}
}
numUpstr=0;
for(p=0;p<_nSubBasins;p++) {
if(isUpstr[p]==true) { numUpstr++; }
}
iter++;
} while ((iter<MAX_ITER) && (numUpstr!=numUpstrOld));
//cout<<"upstream basin calculations iterations = "<<iter<<" "<<numUpstr<<" basins found upstream of basin "<<SBID<<endl;
nUpstream=numUpstr;
int count=0;
for(p=0;p<_nSubBasins;p++) {
if (isUpstr[p]==true){pSBs[count]=_pSubBasins[p];count++; }
}
delete [] isUpstr;
return pSBs;
}
//////////////////////////////////////////////////////////////////
/// \brief Returns true if subbasin with ID SBID is upstream of (or is) basin with subbasin SBIDdown
/// \notes recursive call, keeps marching downstream until outlet or SBIDdown is encounterd
/// \param SBID [in] ID of subbasin being queried
/// \param SBIDdown [in] subbasin ID basis of query
/// \return true if subbasin with ID SBID is upstream of (or is) basin with subbasin SBIDdown
//
bool CModel::IsSubBasinUpstream(const long long SBID,const long long SBIDdown) const
{
if (SBID==DOESNT_EXIST ) { return false;} //end of the recursion line
else if (SBIDdown==SBID) { return true; } //a subbasin is upstream of itself (even handles loops on bad networks)
else if (SBIDdown==DOESNT_EXIST) { return true; } //everything is upstream of an outlet
else if (GetSubBasinByID(SBID)->GetDownstreamID()==SBIDdown){return true;} //directly upstream
else {
return IsSubBasinUpstream(GetSubBasinByID(SBID)->GetDownstreamID(),SBIDdown);
}
}
//////////////////////////////////////////////////////////////////
/// \brief Returns specific subbasin group denoted by parameter pp
///
/// \param pp [in] subbasin group index
/// \return pointer to subbasin group corresponding to passed index pp
//
CSubbasinGroup *CModel::GetSubBasinGroup(const int pp) const
{
#ifdef _STRICTCHECK_
ExitGracefullyIf((pp<0) || (pp>=_nSBGroups),"CModel GetSubBasinGroup::improper index",BAD_DATA);
#endif
return _pSBGroups[pp];
}
//////////////////////////////////////////////////////////////////
/// \brief Returns specific subbasin group denoted by string parameter
///
/// \param name [in] String name of subbasin group
/// \return pointer to subbasin group corresponding to passed name, or NULL if this group doesn't exist
//
CSubbasinGroup *CModel::GetSubBasinGroup(const string name) const
{
for(int pp=0;pp<_nSBGroups;pp++) {
if(!name.compare(_pSBGroups[pp]->GetName())) {
return _pSBGroups[pp];
}
}
return NULL;
}
//////////////////////////////////////////////////////////////////
/// \brief Returns true if subbasin with subbasin ID SBID is in specified subbasin Group
///
/// \param SBID [in] subbasin identifier
/// \param SBGroupName [in] String name of subbasin group
/// \return true if subbasin SBID is in subbasin Group specified by SBGroupName
//
bool CModel::IsInSubBasinGroup(const long long SBID,const string SBGroupName) const
{
CSubbasinGroup *pGrp=NULL;
pGrp=GetSubBasinGroup(SBGroupName);
if(pGrp == NULL) { return false; }//throw warning?
int pp = pGrp->GetGlobalIndex();
for(int p_loc=0; p_loc<_pSBGroups[pp]->GetNumSubbasins(); p_loc++)
{
if(_pSBGroups[pp]->GetSubBasin(p_loc)->GetID()==SBID) { return true; }
}
return false;
}
//////////////////////////////////////////////////////////////////
/// \brief Returns hydrologic process type corresponding to passed index
///
/// \param j [in] Integer index
/// \return Process type corresponding to process with passed index
//
process_type CModel::GetProcessType(const int j) const
{
#ifdef _STRICTCHECK_
ExitGracefullyIf((j<0) || (j>=_nProcesses),"CModel GetProcessType::improper index",BAD_DATA);
#endif
return _pProcesses[j]->GetProcessType();
}
//////////////////////////////////////////////////////////////////
/// \brief Returns number of connections of hydrological process associated with passed index
///
/// \param j [in] Integer index corresponding to a hydrological process
/// \return Number of connections associated with hydrological process symbolized by index
/// \note should be called only by solver
//
int CModel::GetNumConnections (const int j) const
{
#ifdef _STRICTCHECK_
ExitGracefullyIf((j<0) || (j>=_nProcesses),"CModel GetNumConnections::improper index",BAD_DATA);
#endif
return _pProcesses[j]->GetNumConnections();
}
//////////////////////////////////////////////////////////////////
/// \brief Returns number of forcing perturbations in model
//
int CModel::GetNumForcingPerturbations() const {return _nPerturbations;}
//////////////////////////////////////////////////////////////////
/// \brief Returns state variable type corresponding to passed state variable array index
///
/// \param i [in] state variable array index (>=0, <nStateVariables)
/// \return State variable type corresponding to passed state variable array index
//
sv_type CModel::GetStateVarType(const int i) const
{
#ifdef _STRICTCHECK_
string warn="CModel GetStateVarType::improper index ("+to_string(i)+")";
ExitGracefullyIf((i<0) || (i>=_nStateVars),warn.c_str(),BAD_DATA);
#endif
return _aStateVarType[i];
}
//////////////////////////////////////////////////////////////////
/// \brief Returns index of state variable type passed
///
/// \param type [in] State variable type
/// \return Index which corresponds to the state variable type passed, if it exists; DOESNT_EXIST (-1) otherwise
/// \note should only be used for state variable types without multiple levels; issues for soils, e.g.
//
int CModel::GetStateVarIndex(sv_type type) const
{
return _aStateVarIndices[(int)(type)][0];
}
//////////////////////////////////////////////////////////////////
/// \brief Returns index of state variable type passed (for repeated state variables)
///
/// \param type [in] State variable type
/// \param layer [in] Integer identifier of the layer of interest (or, possibly DOESNT_EXIST if variable doesn't have layers)
/// \return Index which corresponds to the state variable type passed, if it exists; DOESNT_EXIST (-1) otherwise
//
int CModel::GetStateVarIndex(sv_type type, int layer) const
{
#ifdef _STRICTCHECK_
ExitGracefullyIf((layer!=DOESNT_EXIST) && ((layer<0) || (layer>=MAX_SV_LAYERS)),
"CModel GetStateVarIndex::improper layer",BAD_DATA);
#endif
if (layer==DOESNT_EXIST){return _aStateVarIndices[(int)(type)][0]; }
else {return _aStateVarIndices[(int)(type)][layer];}
}
//////////////////////////////////////////////////////////////////
/// \brief Uses state variable type index to access index of layer to which it corresponds
///
/// \param ii [in] Index referencing a type of state variable
/// \return Index which corresponds to soil layer, or 0 for a non-layered state variable
//
int CModel::GetStateVarLayer(const int ii) const
{
int count=0;
for (int i=0;i<ii;i++){
if (_aStateVarType[i]==_aStateVarType[ii]){count++;}
}
return count;
}
//////////////////////////////////////////////////////////////////
/// \brief Checks if state variable passed exists in model
/// \param typ [in] State variable type
/// \return Boolean indicating whether state variable exists in model
//
bool CModel::StateVarExists(sv_type typ) const
{
return (GetStateVarIndex(typ)!=DOESNT_EXIST);
}
//////////////////////////////////////////////////////////////////
/// \brief Returns lake storage variable index
/// \return Integer index of lake storage variable
//
int CModel::GetLakeStorageIndex() const{return _lake_sv;}
//////////////////////////////////////////////////////////////////
/// \brief Returns gauge index of gauge with specified name
/// \return Integer index of gauge
/// \param name [in] specified name
//
int CModel::GetGaugeIndexFromName (const string name) const
{
for (int g=0;g<_nGauges;g++){
if (name==_pGauges[g]->GetName()){return g;}
}
return DOESNT_EXIST;
}
//////////////////////////////////////////////////////////////////
/// \brief Returns forcing grid index of forcing grid with specified type
/// \return Integer index of forcing grid
/// \param name [in] specified type
//
int CModel::GetForcingGridIndexFromType (const forcing_type &typ) const
{
for (int f=0;f<_nForcingGrids;f++){
if (typ==_pForcingGrids[f]->GetForcingType()){return f;}
}
return DOESNT_EXIST;
}
//////////////////////////////////////////////////////////////////
/// \brief Returns current mass/energy flux (mm/d, MJ/m2/d, mg/m2/d) between two storage compartments iFrom and iTo
/// \details required for advective transport processes
/// \param k [in] HRU index
/// \param js [in] index of process connection (i.e., j*)
/// \param &Options [in] Global model options information
//
double CModel::GetFlux(const int k, const int js, const optStruct &Options) const
{
#ifdef _STRICTCHECK_
ExitGracefullyIf((k<0) || (k>=_nHydroUnits),"CModel::GetFlux: bad HRU index",RUNTIME_ERR);
ExitGracefullyIf((js<0) || (js>=_nTotalConnections),"CModel::GetFlux: bad connection index",RUNTIME_ERR);
#endif
return _aFlowBal[k][js]/Options.timestep;
}
//////////////////////////////////////////////////////////////////
/// \brief returns concentration or temperature within hru k with storage index i
/// \param k [in] HRU index
/// \param i [in] mass/energy state variable index
//
double CModel::GetConcentration(const int k, const int i) const
{
return _pTransModel->GetConcentration(k,i);
}
//////////////////////////////////////////////////////////////////
/// \brief Returns current mass/energy flow (mm-m2/d, MJ/d, mg/d) between two storage compartments iFrom and iTo
/// \details required for advective transport processes
/// \param k [in] HRU index
/// \param qs [in] global index of process connection (i.e., q*)
/// \param &Options [in] Global model options information
//
double CModel::GetLatFlow(const int qs,const optStruct &Options) const
{
#ifdef _STRICTCHECK_
ExitGracefullyIf((qs<0) || (qs>=_nTotalConnections),"CModel::GetFlux: bad connection index",RUNTIME_ERR);
#endif
return _aFlowLatBal[qs]/Options.timestep;
}
//////////////////////////////////////////////////////////////////
/// \brief Returns cumulative flux to/from storage unit i
/// \param k [in] index of HRU
/// \param i [in] index of storage compartment
/// \param to [in] true if evaluating cumulative flux to storage compartment, false for 'from'
/// \return cumulative flux to storage compartment i in hru K
//
double CModel::GetCumulativeFlux(const int k, const int i, const bool to) const
{
#ifdef _STRICTCHECK_
ExitGracefullyIf((k<0) || (k>=_nHydroUnits),"CModel::GetCumulativeFlux: bad HRU index",RUNTIME_ERR);
ExitGracefullyIf((i<0) || (i>=_nStateVars),"CModel::GetCumulativeFlux: bad state var index",RUNTIME_ERR);
#endif
int js=0;
double sum=0;
double area=_pHydroUnits[k]->GetArea();
int jss=0;
for(int j = 0; j < _nProcesses; j++)
{
for(int q = 0; q < _pProcesses[j]->GetNumConnections(); q++)//each process may have multiple connections
{
if(( to) && (_pProcesses[j]->GetToIndices()[q] == i)){ sum+=_aCumulativeBal[k][js]; }
if((!to) && (_pProcesses[j]->GetFromIndices()[q] == i)){ sum+=_aCumulativeBal[k][js]; }
js++;
}
if(_pProcesses[j]->GetNumLatConnections()>0)
{
CLateralExchangeProcessABC *pProc=(CLateralExchangeProcessABC*)_pProcesses[j];
for(int q = 0; q < _pProcesses[j]->GetNumLatConnections(); q++)//each process may have multiple connections
{
if(( to) && (pProc->GetToHRUIndices()[q] ==k) && (pProc->GetLateralToIndices()[q] ==i)){sum+=_aCumulativeLatBal[jss]/area; }
if((!to) && (pProc->GetFromHRUIndices()[q]==k) && (pProc->GetLateralFromIndices()[q]==i)){sum+=_aCumulativeLatBal[jss]/area; }
jss++;
}
}
}
return sum;
}
//////////////////////////////////////////////////////////////////
/// \brief Returns cumulative gross flux between unit iFrom and iTo in HRU k
/// \param k [in] index of HRU
/// \param iFrom [in] index of storage compartment
/// \param iTo [in] index of storage compartment
/// \return cumulative gross flux between unit iFrom and iTo in HRU k
// does not address fluxes due to lateral flux
double CModel::GetCumulFluxBetween(const int k,const int iFrom,const int iTo) const
{
#ifdef _STRICTCHECK_
ExitGracefullyIf((k<0) || (k>=_nHydroUnits),"CModel::GetCumulativeFlux: bad HRU index",RUNTIME_ERR);
ExitGracefullyIf((iFrom<0) || (iTo>=_nStateVars),"CModel::GetCumulativeFlux: bad state var index",RUNTIME_ERR);
#endif
int q,js=0;
double sum=0;
const int *iFromp;
const int *iTop;
int nConn;
for(int j = 0; j < _nProcesses; j++)
{
iFromp=_pProcesses[j]->GetFromIndices();
iTop =_pProcesses[j]->GetToIndices();
nConn =_pProcesses[j]->GetNumConnections();
for (q = 0; q < nConn; q++)//each process may have multiple connections
{
if( (iTop [q]== iTo) && (iFromp[q]== iFrom)){ sum+=_aCumulativeBal[k][js]; }
if( (iFromp[q]== iTo) && (iTop [q]== iFrom)){ sum-=_aCumulativeBal[k][js]; }
js++;
}
}
return sum;
}
//////////////////////////////////////////////////////////////////
/// \brief Returns area-weighted average of specified cumulative flux over watershed
///
/// \param i [in] index of storage compartment
/// \param to [in] true if evaluating cumulative flux to storage compartment, false for 'from'
/// \return Area-weighted average of cumulative flux to storage compartment i
//
double CModel::GetAvgCumulFlux(const int i,const bool to) const
{
//Area-weighted average
double sum=0.0;
for(int k=0;k<_nHydroUnits;k++)
{
if(_pHydroUnits[k]->IsEnabled())
{
sum +=GetCumulativeFlux(k,i,to)*_pHydroUnits[k]->GetArea();
}
}
return sum/_WatershedArea;
}
//////////////////////////////////////////////////////////////////
/// \brief Returns area-weighted average of cumulative flux between two compartments over watershed
///
/// \param iFrom [in] index of 'from' storage compartment
/// \param iTo [in] index of 'to' storage compartment
/// \return Area-weighted average of cumulative flux between two compartments over watershed
//
double CModel::GetAvgCumulFluxBet(const int iFrom,const int iTo) const
{
//Area-weighted average
double sum=0.0;
for(int k=0;k<_nHydroUnits;k++)
{
if(_pHydroUnits[k]->IsEnabled())
{
sum +=GetCumulFluxBetween(k,iFrom,iTo)*_pHydroUnits[k]->GetArea();
}
}
return sum/_WatershedArea;
}
//////////////////////////////////////////////////////////////////
/// \brief Returns options structure model
/// \return pointer to transport model
//
const optStruct *CModel::GetOptStruct() const{ return _pOptStruct; }
//////////////////////////////////////////////////////////////////
/// \brief Returns transport model
/// \return pointer to transport model
//
CTransportModel *CModel::GetTransportModel() const{return _pTransModel;}
//////////////////////////////////////////////////////////////////
/// \brief Returns groundwater model
/// \return pointer to groundwater model
//
CGroundwaterModel*CModel::GetGroundwaterModel() const{return _pGWModel;}
//////////////////////////////////////////////////////////////////
/// \brief Returns ensemble setup
/// \return pointer to ensemble setup
//
CEnsemble *CModel::GetEnsemble() const { return _pEnsemble; }
//////////////////////////////////////////////////////////////////
/// \brief Returns demand optimizer
/// \return pointer to demand optimizer
//
CDemandOptimizer *CModel::GetManagementOptimizer() const { return _pDO; }
/*****************************************************************
Watershed Diagnostic Functions
-aggregate data from subbasins and HRUs
*****************************************************************/
//////////////////////////////////////////////////////////////////
/// \brief Returns area-weighted average total precipitation+irrigation rate at all HRUs [mm/d]
///
/// \return Area-weighted average of total precipitation rate [mm/d] over all HRUs
//
double CModel::GetAveragePrecip() const
{
double sum(0);
for (int k=0;k<_nHydroUnits;k++)
{
if(_pHydroUnits[k]->IsEnabled())
{
sum+=(_pHydroUnits[k]->GetForcingFunctions()->precip+
_pHydroUnits[k]->GetForcingFunctions()->irrigation)*_pHydroUnits[k]->GetArea();
}
}
return sum/_WatershedArea;
}