-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathBedResistanceToolbox.cs
435 lines (364 loc) · 14 KB
/
BedResistanceToolbox.cs
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
using System;
using System.Collections.Generic;
using DHI.Mike1D.Engine;
using DHI.Mike1D.Engine.ModuleData;
using DHI.Mike1D.Engine.ModuleHD;
using DHI.Mike1D.Generic;
using DHI.Mike1D.Mike1DDataAccess;
using DHI.Mike1D.Plugins;
namespace Scripts
{
/// <summary>
/// Modifies bed resistance values.
/// </summary>
public class BedResistanceToolboxRunner
{
private EngineNet _engineNet;
private List<BedResistanceCalculatorBase> _bedResistanceCalculators = new List<BedResistanceCalculatorBase>();
/// <summary>
/// Method called when IMike1DController is available.
/// </summary>
[Script]
public void Initialize(IMike1DController controller)
{
controller.ControllerEvent += ControllerOnControllerEvent;
}
private void ControllerOnControllerEvent(object sender, ControllerEventArgs e)
{
if (e.State != ControllerState.Prepared)
return;
var controller = (IMike1DController)sender;
_engineNet = controller.EngineNet;
CreateBedResistanceCalculators();
InitializeBedResistanceCalculators();
_engineNet.PostTimeStepEvent += ChangeBedResistance;
}
/// <summary>
/// Populates the list of the bed resistance calculators.
/// </summary>
private void CreateBedResistanceCalculators()
{
// To add 1/M=a V^b bed resistance velocity V dependence with
//
// a = 0.05
// b = 1.0
// minimum resistance M_min = 10
// maximum resistance M_max = 40
//
// on a reach with name MyReach from chainage 1000 to 2000 add a line:
// -----------------------
// AddFormula(FormulaType.Velocity, 0.05, 1.0, 10, 40, "MyReach", 1000, 2000);
// -----------------------
// To apply the same formula on the whole reach use a simplified method:
// -----------------------
// AddFormula(FormulaType.Velocity, 0.05, 1.0, 10, 40, "MyReach");
// -----------------------
// If the formula needs to be applied globally then add a line:
// -----------------------
// AddFormula(FormulaType.Velocity, 0.05, 1.0, 10, 40);
// -----------------------
// The velocity dependence of bed resistance using a velocity-resistance table:
// can be done using:
// -----------------------
// double[] velocityValues = { 0.0, 1.0, 2.0 };
// double[] resistanceValues = { 15.0, 20.0, 40.0 };
// AddTable(velocityValues, resistanceValues, "MyReach", 1000, 2000);
// -----------------------
}
private void AddFormula(FormulaType formulaType, double coefficientA, double coefficientB, double minimumResistance, double maximumResistance,
string reachId=null, double startChainage=Constants.DOUBLE_DELETE_VALUE, double endChainage=Constants.DOUBLE_DELETE_VALUE)
{
var formula = GetFormulaCalculator(formulaType);
formula.SetCoefficients(coefficientA, coefficientB, minimumResistance, maximumResistance);
if (reachId != null)
formula.Add(reachId, startChainage, endChainage);
_bedResistanceCalculators.Add(formula);
}
private void AddTable(double[] velocityValues, double[] resistanceValues,
string reachId=null, double startChainage=Constants.DOUBLE_DELETE_VALUE, double endChainage=Constants.DOUBLE_DELETE_VALUE)
{
var tableBedResistance = new TableBedResistance(_engineNet);
tableBedResistance.SetTable(velocityValues, resistanceValues);
if (reachId != null)
tableBedResistance.Add(reachId, startChainage, endChainage);
_bedResistanceCalculators.Add(tableBedResistance);
}
private BedResistanceFormulaBase GetFormulaCalculator(FormulaType formulaType)
{
switch (formulaType)
{
case FormulaType.VelocityHydraulicRadius:
return new FormulaVelocityHydraulicRadius(_engineNet);
case FormulaType.HydraulicDepth:
return new FormulaHydraulicDepth(_engineNet);
case FormulaType.Velocity:
return new FormulaVelocity(_engineNet);
default:
throw new ArgumentOutOfRangeException("Not supported bed resistance formula type.");
}
}
private void InitializeBedResistanceCalculators()
{
foreach (var calculator in _bedResistanceCalculators)
calculator.Initialize();
}
private void ChangeBedResistance(DateTime time)
{
foreach (var bedResistanceCalculator in _bedResistanceCalculators)
bedResistanceCalculator.ChangeBedResistance(time);
}
}
/// <summary>
/// Enumeration for supported bed resistance formula types
/// </summary>
public enum FormulaType
{
VelocityHydraulicRadius,
HydraulicDepth,
Velocity,
}
#region Abstract base bed resistance calculators
/// <summary>
/// Helper class to store reach grid points for which bed resistance is calculated.
/// If bed resistance is calculated for whole reach then GridPoints stores all the H points.
/// </summary>
public class BedResistanceLocation
{
public IHDReach Reach;
public List<IHDHGridPoint> GridPoints = new List<IHDHGridPoint>();
}
/// <summary>
/// Base class for bed resistance calculations.
/// </summary>
public abstract class BedResistanceCalculatorBase
{
protected List<LocationSpan> _locationSpans = new List<LocationSpan>();
protected List<BedResistanceLocation> _locations = new List<BedResistanceLocation>();
protected EngineNet _engineNet;
protected EngineDataItemAll<double> _velocityDataItem;
protected double _waterLevel;
protected double _hydraulicRadius;
protected double _hydraulicDepth;
protected double _velocity;
public double MinVelocity = 1e-6;
public double MinHydraulicRadius = 0.001;
public double MinHydraulicDepth = 0;
/// <summary>
/// Provides Manning's M bed resistance value.
/// </summary>
protected abstract double BedResistanceFormula();
/// <summary>
/// Determines the state variables needed for calculation of bed resistance.
/// </summary>
protected abstract void DetermineStateVariables(IHDReach hdReach, IHDHGridPoint hdhGridPoint);
public BedResistanceCalculatorBase(EngineNet engineNet)
{
_engineNet = engineNet;
}
public void Add(string reachId)
{
var locationSpan = new LocationSpan() { ID = reachId};
_locationSpans.Add(locationSpan);
}
public void Add(string reachId, double startChainage, double endChainage)
{
var locationSpan = new LocationSpan(reachId, startChainage, endChainage);
_locationSpans.Add(locationSpan);
}
public void Initialize()
{
_velocityDataItem = _engineNet.HDModule.DataItems.Velocity(true, true);
if (_locationSpans.Count == 0)
UseAllNetwork();
else
PopulateLocations(_locationSpans);
SetResistanceToConstant();
// Make sure that initial values are determined correctly.
ChangeBedResistance(_engineNet.EngineTime.TimeN);
}
private void UseAllNetwork()
{
var locationSpan = new LocationSpan();
foreach (var reach in _engineNet.Reaches)
AddLocation(reach, locationSpan);
}
private void PopulateLocations(List<LocationSpan> locationSpans)
{
foreach (var locationSpan in locationSpans)
{
var reachName = locationSpan.ID;
var reaches = _engineNet.FindAllReaches(reachName);
if (reaches == null)
throw new Mike1DException("Cannot find a reach with ID " + reachName + " for bed resistance toolbox calculations");
foreach (var engineReach in reaches)
AddLocation(engineReach, locationSpan);
}
}
private void AddLocation(EngineReach engineReach, LocationSpan locationsSpan)
{
var hdReach = _engineNet.HDModule.GetReach(engineReach);
if (hdReach == null)
return;
var useAllReach = locationsSpan.StartChainage == Constants.DOUBLE_DELETE_VALUE
|| locationsSpan.EndChainage == Constants.DOUBLE_DELETE_VALUE;
var location = new BedResistanceLocation() { Reach = hdReach };
// Bed resistance toolbox modifies the resistance only on the H grid points.
foreach (var hdGridPoint in hdReach.GridPoints)
{
var hdhGridPoint = hdGridPoint as IHDHGridPoint;
if (hdhGridPoint == null)
continue;
if (!locationsSpan.ContainsChainage(hdhGridPoint.GridPoint.Location.Chainage) && !useAllReach)
continue;
location.GridPoints.Add(hdhGridPoint);
}
if (location.GridPoints.Count > 0)
_locations.Add(location);
}
private void SetResistanceToConstant()
{
foreach (var location in _locations)
{
foreach (var hdhGridPoint in location.GridPoints)
{
var baseCrossSection = hdhGridPoint.GridPoint.EngineCrossSection.BaseCrossSection;
baseCrossSection.ModifiedFormulation = ResistanceFormulation.Manning_M;
var flowResistance = baseCrossSection.FlowResistance;
flowResistance.ResistanceDistribution = ResistanceDistribution.Constant;
flowResistance.Formulation = ResistanceFormulation.Manning_M;
}
}
}
public void ChangeBedResistance(DateTime time)
{
foreach (var location in _locations)
{
foreach (var hdhGridPoint in location.GridPoints)
{
DetermineStateVariables(location.Reach, hdhGridPoint);
var engineCrossSection = hdhGridPoint.GridPoint.EngineCrossSection;
var flowResistance = engineCrossSection.BaseCrossSection.FlowResistance;
flowResistance.ResistanceValue = BedResistanceFormula();
}
}
}
protected void DetermineVelocity(IHDReach hdReach, IHDHGridPoint hdhGridPoint)
{
var reachIndex = hdReach.EngineReach.ReachListIndex;
var velocityCalculator = _velocityDataItem.ReachesData[reachIndex];
var gridPointIndex = hdhGridPoint.GridPoint.PointIndex;
_velocity = Math.Max(Math.Abs(velocityCalculator.GetValue(gridPointIndex)), MinVelocity);
}
protected void DetermineHydraulicRadius(IHDReach hdReach, IHDHGridPoint hdhGridPoint)
{
_waterLevel = hdhGridPoint.WaterLevelNp1;
var engineCrossSection = hdhGridPoint.GridPoint.EngineCrossSection;
_hydraulicRadius = Math.Max(engineCrossSection.GetHydraulicRadius(_waterLevel), MinHydraulicRadius);
}
protected void DetermineHydraulicDepth(IHDReach hdReach, IHDHGridPoint hdhGridPoint)
{
_waterLevel = hdhGridPoint.WaterLevelNp1;
var engineCrossSection = hdhGridPoint.GridPoint.EngineCrossSection;
var area = engineCrossSection.GetArea(_waterLevel);
var width = engineCrossSection.GetStorageWidth(_waterLevel);
_hydraulicDepth = Math.Max(area/width, MinHydraulicDepth);
}
}
/// <summary>
/// Base class for bed resistance calculations using formulas with
/// CoefficientA (a) and CoefficientB (b).
/// </summary>
public abstract class BedResistanceFormulaBase : BedResistanceCalculatorBase
{
public double CoefficientA { get; set; }
public double CoefficientB { get; set; }
public double MinimumResistance { get; set; }
public double MaximumResistance { get; set; }
public BedResistanceFormulaBase(EngineNet engineNet) : base(engineNet)
{
CoefficientA = 0.0;
CoefficientB = 1.0;
MinimumResistance = 0.0;
MaximumResistance = Double.MaxValue;
}
public void SetCoefficients(double coefficientA, double coefficientB, double minimumResistance, double maximumResistance)
{
CoefficientA = coefficientA;
CoefficientB = coefficientB;
MinimumResistance = minimumResistance;
MaximumResistance = maximumResistance;
}
public double LimitToMinOrMax(double bedResistance)
{
return Math.Max(Math.Min(bedResistance, MaximumResistance), MinimumResistance);
}
}
#endregion
#region Particular bed resistance calculator implementations
public class FormulaVelocityHydraulicRadius : BedResistanceFormulaBase
{
public FormulaVelocityHydraulicRadius(EngineNet engineNet) : base(engineNet)
{
}
protected override double BedResistanceFormula()
{
var manningN = CoefficientA * Math.Log(_velocity * _hydraulicRadius) + CoefficientB;
return LimitToMinOrMax(1/manningN);
}
protected override void DetermineStateVariables(IHDReach hdReach, IHDHGridPoint hdhGridPoint)
{
DetermineVelocity(hdReach, hdhGridPoint);
DetermineHydraulicRadius(hdReach, hdhGridPoint);
}
}
public class FormulaHydraulicDepth : BedResistanceFormulaBase
{
public FormulaHydraulicDepth(EngineNet engineNet) : base(engineNet)
{
}
protected override double BedResistanceFormula()
{
var manningN = CoefficientA * Math.Pow(_hydraulicDepth, CoefficientB);
return LimitToMinOrMax(1/manningN);
}
protected override void DetermineStateVariables(IHDReach hdReach, IHDHGridPoint hdhGridPoint)
{
DetermineHydraulicDepth(hdReach, hdhGridPoint);
}
}
public class FormulaVelocity : BedResistanceFormulaBase
{
public FormulaVelocity(EngineNet engineNet) : base(engineNet)
{
}
protected override double BedResistanceFormula()
{
var manningN = CoefficientA * Math.Pow(_velocity, CoefficientB);
return LimitToMinOrMax(1/manningN);
}
protected override void DetermineStateVariables(IHDReach hdReach, IHDHGridPoint hdhGridPoint)
{
DetermineVelocity(hdReach, hdhGridPoint);
}
}
public class TableBedResistance : BedResistanceCalculatorBase
{
public IXYTable VelocityResistanceTable { get; set; }
public TableBedResistance(EngineNet engineNet) : base(engineNet)
{
}
public void SetTable(double[] velocityValues, double[] resistanceValues)
{
VelocityResistanceTable = new XYTable(velocityValues, resistanceValues);
}
protected override double BedResistanceFormula()
{
return VelocityResistanceTable.YFromX(_velocity);
}
protected override void DetermineStateVariables(IHDReach hdReach, IHDHGridPoint hdhGridPoint)
{
DetermineVelocity(hdReach, hdhGridPoint);
}
}
#endregion
}