@@ -27,6 +27,7 @@ CmvLatFlush::CmvLatFlush(int from_sv_ind,
2727 _kk_to =to_HRU_grp;
2828 _constrain_to_SBs=constrain_to_SBs;
2929 _divert =divert;
30+ _aFrac =NULL ;
3031
3132 DynamicSpecifyConnections (0 ); // purely lateral flow, no vertical
3233
@@ -44,7 +45,9 @@ CmvLatFlush::CmvLatFlush(int from_sv_ind,
4445// ////////////////////////////////////////////////////////////////
4546// / \brief Implementation of the default destructor
4647//
47- CmvLatFlush::~CmvLatFlush (){}
48+ CmvLatFlush::~CmvLatFlush (){
49+ delete [] _aFrac;
50+ }
4851
4952// ////////////////////////////////////////////////////////////////
5053// / \brief Initialization (prior to solution)
@@ -56,6 +59,7 @@ void CmvLatFlush::Initialize()
5659 int *kTo =new int [_pModel->GetNumHRUs ()];
5760 string fromHRUGrp=_pModel->GetHRUGroup (_kk_from)->GetName ();
5861 string toHRUGrp =_pModel->GetHRUGroup ( _kk_to)->GetName ();
62+ _aFrac=new double [MAX_LAT_CONNECTIONS];
5963 int q=0 ;
6064 int k;
6165
@@ -92,29 +96,42 @@ void CmvLatFlush::Initialize()
9296 // Alternate approach: multiple recipient stores
9397 bool source_sink_issue=false ;
9498 int k1,k2;
99+ int nRecipients;
100+ double Asum,area;
95101 for (int p=0 ;p<_pModel->GetNumSubBasins ();p++)
96102 {
97- for (int ks=0 ; ks<_pModel->GetSubBasin (p)->GetNumHRUs (); ks++)
103+
104+ for (int ks=0 ; ks<_pModel->GetSubBasin (p)->GetNumHRUs (); ks++) // sources
98105 {
99106 k1=_pModel->GetSubBasin (p)->GetHRU (ks)->GetGlobalIndex ();
100- for (int ks2=0 ; ks2<_pModel->GetSubBasin (p)->GetNumHRUs (); ks2++)
101- {
102- k2=_pModel->GetSubBasin (p)->GetHRU (ks2)->GetGlobalIndex ();
103-
104- if ((_pModel->IsInHRUGroup (k1,toHRUGrp)) && (_pModel->IsInHRUGroup (k2,fromHRUGrp)))
107+ Asum=0.0 ;
108+ nRecipients=0 ;
109+ if (_pModel->IsInHRUGroup (k1,fromHRUGrp)){
110+ for (int ks2=0 ; ks2<_pModel->GetSubBasin (p)->GetNumHRUs (); ks2++) // recipients
105111 {
106- if (k1!=k2){
107- kTo [q]=k1;
108- kFrom [q]=k2;
109- q++;
110- ExitGracefullyIf (q>=MAX_LAT_CONNECTIONS," Number of HRU connections in LateralFlush or LateralDivert exceeds MAX_LAT_CONNECTIONS limit." ,BAD_DATA);
111- }
112- else {
113- // may want to throw warning if source is also sink
114- source_sink_issue=true ;
112+ k2=_pModel->GetSubBasin (p)->GetHRU (ks2)->GetGlobalIndex ();
113+
114+ if (_pModel->IsInHRUGroup (k2,toHRUGrp))
115+ {
116+ if (k1!=k2){
117+ kFrom [q]=k1;
118+ kTo [q]=k2;
119+ area=_pModel->GetSubBasin (p)->GetHRU (ks2)->GetArea ();
120+ _aFrac[q]=area;
121+ Asum+=area;// sum of recipient areas
122+ nRecipients++;
123+ q++;
124+ ExitGracefullyIf (q>=MAX_LAT_CONNECTIONS," Number of HRU connections in LateralFlush or LateralDivert exceeds MAX_LAT_CONNECTIONS limit." ,BAD_DATA);
125+ }
126+ else {
127+ source_sink_issue=true ;// throw warning if source is also sink
128+ }
115129 }
116130 }
117131 }
132+ for (int qq = 0 ; qq < nRecipients; qq++) {// once sum is known for each source, calculate fraction
133+ _aFrac[q-qq]=_aFrac[q-qq]/Asum;
134+ }
118135 }
119136 }
120137
@@ -145,6 +162,7 @@ void CmvLatFlush::Initialize()
145162 if (_pModel->IsInHRUGroup (k,fromHRUGrp) && (kToSB !=DOESNT_EXIST)) {
146163 kFrom [q]=k;
147164 kTo [q]=kToSB ;
165+ _aFrac[q]=1.0 ; // only a single recipient
148166 q++;
149167 }
150168 }
@@ -219,7 +237,7 @@ void CmvLatFlush::GetLateralExchange( const double * const *state_vars, //ar
219237 mult=pHRUs[_kFrom[q]]->GetSurfaceProps ()->divert_fract ;
220238 }
221239
222- exchange_rates[q]=max (mult*stor,0.0 )/Options.timestep *Afrom; // [mm-m2/d]
240+ exchange_rates[q]=max (mult*stor,0.0 )/Options.timestep *Afrom*_aFrac[q] ; // [mm-m2/d]
223241 exchange_rates[q]=min (exchange_rates[q],max_rate); // constrains so that it does not overfill receiving compartment
224242 }
225243}
0 commit comments