-
Notifications
You must be signed in to change notification settings - Fork 8
/
CrossSectionExamples.cs
429 lines (353 loc) · 16.8 KB
/
CrossSectionExamples.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DHI.Mike1D.CrossSectionModule;
using DHI.Mike1D.Generic;
using DHI.Mike1D.Mike1DDataAccess;
namespace DHI.Mike1D.Examples
{
/// <summary>
/// Examples of handling cross sections
/// </summary>
public class CrossSectionExamples
{
/// <summary>
/// Static constructor, setting up search paths for MIKE assemblies
/// </summary>
static CrossSectionExamples()
{
// The setup method will make your application find the MIKE assemblies at runtime.
// The first call of the setup method takes precedense. Any subsequent calls will be ignored.
// It must be called BEFORE any method using MIKE libraries is called, i.e. it is not sufficient
// to call it as the first thing in that method using the MIKE libraries. Often this can be achieved
// by having this code in the static constructor.
// This is not required by plugins and scripts, only by standalone applications using MIKE 1D components
if (!DHI.Mike.Install.MikeImport.Setup(19, DHI.Mike.Install.MikeProducts.Mike1D))
throw new Exception("Could not find a MIKE installation");
}
#region Navigation
/// <summary>
/// Example of how to navigate and find cross sections.
/// <para>
/// There are two ways to get cross sections: Find or Get
/// </para>
/// <para>
/// Find will search the cross section data object for a cross
/// section at the specified location. If no cross section is
/// found, null is returned.
/// </para>
/// <para>
/// Get will search the cross section data object for a cross
/// section at the specified location. If no cross section is
/// found, it will interpolate/extrapolate if possible, and only
/// if that interpolation/extrapolation is not possible, null
/// is returned. The get methods are used by the engine.
/// </para>
/// <para>
/// Users that want to update or in other ways process the cross
/// sections should always use the Find methods.
/// </para>
/// </summary>
/// <param name="xns11Filepath">Filepath to xns11 file</param>
public static void Navigation(string xns11Filepath)
{
// Load cross section data
Diagnostics diagnostics = new Diagnostics("Errors");
CrossSectionDataFactory csDataFactory = new CrossSectionDataFactory();
CrossSectionData csData = csDataFactory.Open(Connection.Create(xns11Filepath), diagnostics);
ICrossSection crossSection;
// Find a cross section on a given location
crossSection = csData.FindCrossSection(new Location("LINDSKOV", 1370), "TOPO-95");
if (crossSection == null) throw new Exception("Could not find cross section at location");
// Find a cross section closest to a given location
crossSection = csData.FindClosestCrossSection(new Location("LINDSKOV", 1250), "TOPO-95");
if (crossSection == null || crossSection.Location.Chainage != 1172) throw new Exception("Expected cross section at chainage 1172");
// Find all cross sections inside a given location span
IList<ICrossSection> css = csData.FindCrossSectionsForLocationSpan(new LocationSpan("LINDSKOV", 1000, 2000), "TOPO-95", true);
if (css.Count != 6) throw new Exception("Expected 6 cross sections");
// Find cross sections that are neighbours to a given locatin.
ICrossSection csAfter;
ICrossSection csBefore;
csData.FindNeighborCrossSections(new Location("LINDSKOV", 1250), "TOPO-95", true, out csBefore, out csAfter);
if (csBefore == null || csBefore.Location.Chainage != 1172) throw new Exception("Expected cross section at chainage 1172");
if (csAfter == null || csAfter.Location.Chainage != 1370) throw new Exception("Expected cross section at chainage 1370");
}
/// <summary>
/// Example of how to loop over all cross sections
/// </summary>
/// <param name="csData">Cross section data object</param>
public static void LoopAllCrossSections(CrossSectionData csData)
{
// Loop over all cross sections
foreach (ICrossSection cs in csData)
{
// Check if cross section has raw data:
XSBaseRaw xsBaseRaw = cs.BaseCrossSection as XSBaseRaw;
if (xsBaseRaw == null)
continue; // It dit not have raw data
// Add additional 0.4 to all z values in the raw points
foreach (ICrossSectionPoint xsPoint in xsBaseRaw.Points)
{
xsPoint.Z += 0.4;
}
// Update all markers to defaul
xsBaseRaw.UpdateMarkersToDefaults(true, true, true);
// Calculates the processed levels, storage areas, radii, etc, ie, fill in all
// ProcessedXXX properties.
xsBaseRaw.CalculateProcessedData();
}
}
#endregion
#region AddCrossSection
/// <summary>
/// Example of how to load an xns11 file, add a cross section and save the
/// file again, adding a "-csAdd" to the end of the new filename.
/// </summary>
/// <param name="xns11Filepath">Filepath to xns11 file</param>
public static void AddCrossSectionAndSave(string xns11Filepath)
{
// Load cross section data
Diagnostics diagnostics = new Diagnostics("Errors");
CrossSectionDataFactory csDataFactory = new CrossSectionDataFactory();
CrossSectionData csData = csDataFactory.Open(Connection.Create(xns11Filepath), diagnostics);
// Add cross section
CreateAndAddOpenCrossSection(csData);
CreateAndAddCircularCrossSection(csData);
// Save the cross section as a new file name
csData.Connection.FilePath.FileNameWithoutExtension += "-csAdd";
CrossSectionDataFactory.Save(csData);
}
/// <summary>
/// Example of how to add a cross section after loading a complete
/// setup, and running the simulation with the new cross section
/// included. It is not necessary to save the modified cross sections
/// to file before running.
/// </summary>
/// <param name="setupFilepath">A .sim11 setup path</param>
public static void AddCrossSectionAndRun(string setupFilepath)
{
Mike1DControllerFactory controllerFactory = new Mike1DControllerFactory();
IMike1DController controller = null;
try
{
// creates a new Mike 1D controller and load the setup
Diagnostics diagnostics = new Diagnostics("My Diagnostics");
controller = controllerFactory.OpenAndCreate(Connection.Create(setupFilepath), diagnostics);
if (diagnostics.ErrorCountRecursive > 0)
throw new Exception("Loading errors, aborting");
// Add an additional cross section
CreateAndAddOpenCrossSection(controller.Mike1DData.CrossSections);
// Change the output file name, so we can compare results with/without change
controller.Mike1DData.ResultSpecifications[0].Connection.FilePath.FileNameWithoutExtension += "-csAdd";
IDiagnostics validated = controller.Validate();
if (validated.ErrorCountRecursive > 0)
throw new Exception("Validation errors, aborting");
// run the simulation with the new cross section included
controller.Initialize(diagnostics);
if (diagnostics.ErrorCountRecursive > 0)
throw new Exception("Initialization errors, aborting");
controller.Prepare();
controller.Run();
controller.Finish();
}
catch (Exception e)
{
// Write exception to log file
if (controllerFactory.LogFileWriter != null)
controllerFactory.LogFileWriter.ExceptionToLogFile(e);
// Call Finish, which should make sure to close down properly, and release any licences.
if (controller != null)
{
try { controller.Finish(); }
catch { }
}
// Rethrow exception
throw;
}
}
/// <summary>
/// Create a new open cross section and add it to the CrossSectionData object
/// </summary>
/// <param name="csData">Cross section data object</param>
public static void CreateAndAddOpenCrossSection(CrossSectionData csData)
{
// Note: Raw data must be ordered from left to right in order for hydraulic radius to be processed correctly
// Creates a class representing a cross section with raw data attached.
CrossSectionFactory builder = new CrossSectionFactory();
builder.BuildOpen("");
// Defines the location of the current cross section. The Z-coordinate
// is the bottom level of the cross section (unless defined by the
// raw data (the open cross sections)).
builder.SetLocation(new ZLocation("river B", 500) { Z = 0 });
// Create a number of points
CrossSectionPointList points = new CrossSectionPointList();
points.Add(new CrossSectionPoint(-1.0, 2.0){DistributedResistance = 2});
points.Add(new CrossSectionPoint(0.0, 1.0) {DistributedResistance = 2});
points.Add(new CrossSectionPoint(0.0, 0.0) {DistributedResistance = 1});
points.Add(new CrossSectionPoint(1.0, 0.0) {DistributedResistance = 1});
points.Add(new CrossSectionPoint(1.0, 1.0) {DistributedResistance = 1});
points.Add(new CrossSectionPoint(2.0, 2.0) {DistributedResistance = 2});
points.Add(new CrossSectionPoint(3.0, 2.0) {DistributedResistance = 2}); // dummy point, outside right levee bank marker
// Sets the markers at left/right side and lowest point.
builder.SetRawPoints(points);
builder.SetLeftLeveeBank(points[0]);
builder.SetLowestPoint(points[3]);
builder.SetRightLeveeBank(points[5]);
// Set flow resistance
FlowResistance flowResistance = new FlowResistance();
flowResistance.ResistanceDistribution = ResistanceDistribution.Uniform;
flowResistance.ResistanceValue = 1;
flowResistance.Formulation = ResistanceFormulation.Relative;
builder.SetFlowResistance(flowResistance);
builder.SetRadiusType(RadiusType.ResistanceRadius);
// Get cross section from builder
CrossSectionLocated cs = builder.GetCrossSection();
cs.TopoID = "1";
// Calculates the processed levels, storage areas, radii, etc, ie, fill in all
// ProcessedXXX properties.
cs.BaseCrossSection.CalculateProcessedData();
// Validates the data. The constraints are that the levels and the areas after sorting
// must be monotonically increasing.
IDiagnostics diagnostics = cs.Validate();
if (diagnostics.ErrorCountRecursive > 0)
{
throw new Exception(String.Format("Number of errors: {0}", diagnostics.Errors.Count));
}
// Add the cross section
csData.Add(cs);
}
/// <summary>
/// Create a new circular cross section and add it to the CrossSectionData object
/// </summary>
/// <param name="csData">Cross section data object</param>
public static void CreateAndAddCircularCrossSection(CrossSectionData csData)
{
// Creates a class representing a cross section with raw data attached.
CrossSectionFactory builder = new CrossSectionFactory();
builder.BuildCircular(2);
// Defines the location of the current cross section. The Z-coordinate
// is the bottom level of the cross section (unless defined by the
// raw data (the open cross sections)).
builder.SetLocation(new ZLocation("pipe A", 10) { Z = 0 });
// Set flow resistance
FlowResistance flowResistance = new FlowResistance();
flowResistance.ResistanceDistribution = ResistanceDistribution.ExponentVarying;
flowResistance.ResistanceValue = 75;
flowResistance.ResistanceTopValue = 85;
flowResistance.ExpDepExponent = 1.5;
flowResistance.Formulation = ResistanceFormulation.Manning_M;
builder.SetFlowResistance(flowResistance);
// Get cross section from builder
CrossSectionLocated cs = builder.GetCrossSection();
cs.TopoID = "1";
// Calculates the processed levels, storage areas, radii, etc, ie, fill in all
// ProcessedXXX properties.
cs.BaseCrossSection.CalculateProcessedData();
// Validates the data. The constraints are that the levels and the areas after sorting
// must be monotonically increasing.
IDiagnostics diagnostics = cs.Validate();
if (diagnostics.ErrorCountRecursive > 0)
{
throw new Exception(String.Format("Number of errors: {0}", diagnostics.Errors.Count));
}
// Add the cross section
csData.Add(cs);
}
#endregion
#region ModifyCrossSectionRaw
/// <summary>
/// Load an xns11 file, add a cross section and save the file again,
/// adding a "-csModRaw" to the end of the filename.
/// </summary>
/// <param name="xns11Filepath">Filepath to xns11 file from the CrossSection example data</param>
public static void ModifyCrossSectionRawAndSave(string xns11Filepath)
{
// Load cross section data
Diagnostics diagnostics = new Diagnostics("Errors");
CrossSectionDataFactory csDataFactory = new CrossSectionDataFactory();
CrossSectionData csData = csDataFactory.Open(Connection.Create(xns11Filepath), diagnostics);
// Modify cross section
ModifyCrossSectionRaw(csData);
// Save the cross section as a new file name
csData.Connection.FilePath.FileNameWithoutExtension += "-csModRaw";
CrossSectionDataFactory.Save(csData);
}
/// <summary>
/// Modify cross section
/// </summary>
/// <param name="csData">Cross section data from the CrossSection example data</param>
public static void ModifyCrossSectionRaw(CrossSectionData csData)
{
ICrossSection crossSection = csData.FindCrossSection(new Location("river B", 1000), "1");
if (crossSection == null)
throw new Exception("Cross section not found at location");
// This is a cross section with raw data
XSBaseRaw xsBaseRaw = crossSection.BaseCrossSection as XSBaseRaw;
if (xsBaseRaw == null)
throw new Exception("Not a cross section with raw data");
// This cross section has 3 points.
// Update the first two points
xsBaseRaw.Points[1].X = 0.5;
xsBaseRaw.Points[1].Z = 0.5;
xsBaseRaw.Points[2] = new CrossSectionPoint(0.5, 0.0);
// Add two additional points
xsBaseRaw.Points.Add(new CrossSectionPoint(1.0, 0.2));
xsBaseRaw.Points.Add(new CrossSectionPoint(1.0, 1.0));
// Update marker to default
xsBaseRaw.UpdateMarkersToDefaults(true, true, true);
// Calculates the processed levels, storage areas, radii, etc, ie, fill in all
// ProcessedXXX properties.
xsBaseRaw.CalculateProcessedData();
// Validates the data. The constraints are that the levels and the areas after sorting
// must be monotonically increasing.
IDiagnostics diagnostics = xsBaseRaw.Validate();
if (diagnostics.ErrorCountRecursive > 0)
{
throw new Exception(String.Format("Number of errors: {0}", diagnostics.Errors.Count));
}
}
#endregion
#region ModifyCrossSectionProcessed
/// <summary>
/// Load an xns11 file, add a cross section and save the file again,
/// adding a "-csModPro" to the end of the filename.
/// </summary>
/// <param name="xns11Filepath">Filepath to xns11 file from the CrossSection example data</param>
public static void ModifyCrossSectionProcessedAndSave(string xns11Filepath)
{
// Load cross section data
Diagnostics diagnostics = new Diagnostics("Errors");
CrossSectionDataFactory csDataFactory = new CrossSectionDataFactory();
CrossSectionData csData = csDataFactory.Open(Connection.Create(xns11Filepath), diagnostics);
// Modify cross section
ModifyCrossSectionProcessed(csData);
// Save the cross section as a new file name
csData.Connection.FilePath.FileNameWithoutExtension += "-csModPro";
CrossSectionDataFactory.Save(csData);
}
/// <summary>
/// Modify cross section
/// </summary>
/// <param name="csData">Cross section data from the CrossSection example data</param>
public static void ModifyCrossSectionProcessed(CrossSectionData csData)
{
ICrossSection crossSection = csData.FindCrossSection(new Location("river B", 1000), "1");
if (crossSection == null)
throw new Exception("Cross section not found at location");
// This is a cross section with raw data
XSBase xsBase = crossSection.BaseCrossSection;
if (xsBase == null)
throw new Exception("Not a cross section with processed data");
for (int i = 0; i < xsBase.NumberOfProcessedLevels; i++)
{
xsBase.ProcessedResistanceFactors[i] *= 1.1;
}
// Do not call CalculateProcessedData in this case, because that will reset the
// processed data. Instead set the protected flag, such that data is not updated
// automatically
xsBase.ProcessedDataProtected = true;
}
#endregion
}
}