-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathComputeRunner.cs
More file actions
368 lines (318 loc) · 16.2 KB
/
ComputeRunner.cs
File metadata and controls
368 lines (318 loc) · 16.2 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
using System.Diagnostics;
using HEC.FDA.Model.metrics;
using HEC.FDA.Model.paireddata;
using HEC.FDA.TestingUtility.Configuration;
using HEC.FDA.TestingUtility.Reporting;
using HEC.FDA.TestingUtility.Services;
using HEC.FDA.ViewModel;
using HEC.FDA.ViewModel.AggregatedStageDamage;
using HEC.FDA.ViewModel.Alternatives;
using HEC.FDA.ViewModel.AlternativeComparisonReport;
using HEC.FDA.ViewModel.ImpactAreaScenario;
using HEC.FDA.ViewModel.ImpactArea;
using HEC.FDA.ViewModel.Saving;
using HEC.FDA.ViewModel.TableWithPlot;
using HEC.FDA.ViewModel.Utilities;
namespace HEC.FDA.TestingUtility;
/// <summary>
/// Runs FDA computations and generates CSV result reports.
/// </summary>
public class ComputeRunner
{
private readonly TestConfiguration _config;
private readonly string _outputDir;
private readonly string[]? _studyFilter;
private readonly CancellationTokenSource _cts;
private readonly CsvReportFactory _csvReportFactory = new();
public ComputeRunner(TestConfiguration config, string outputDir, string[]? studyFilter)
{
_config = config;
_outputDir = outputDir;
_studyFilter = studyFilter;
_cts = new CancellationTokenSource();
if (_config.GlobalSettings.TimeoutMinutes > 0)
{
_cts.CancelAfter(TimeSpan.FromMinutes(_config.GlobalSettings.TimeoutMinutes));
}
}
public async Task<int> RunAsync()
{
int errors = 0;
int completed = 0;
Stopwatch totalStopwatch = Stopwatch.StartNew();
List<(string StudyId, TimeSpan Duration, int ComputeCount, int ErrorCount)> studyTimings = new();
Console.WriteLine($"Configuration: {_config.TestSuiteId}");
Console.WriteLine($"Output directory: {_outputDir}");
Console.WriteLine();
List<StudyConfiguration> studiesToRun = _config.Studies;
if (_studyFilter != null && _studyFilter.Length > 0)
{
studiesToRun = studiesToRun
.Where(s => _studyFilter.Contains(s.StudyId, StringComparer.OrdinalIgnoreCase))
.ToList();
if (studiesToRun.Count == 0)
{
Console.WriteLine($"No studies match the filter: {string.Join(", ", _studyFilter)}");
return 1;
}
}
foreach (StudyConfiguration study in studiesToRun)
{
Console.WriteLine($"=== Computing: {study.StudyName} ({study.StudyId}) ===");
Stopwatch studyStopwatch = Stopwatch.StartNew();
int studyErrors = 0;
int studyCompleted = 0;
try
{
using StudyLoader loader = new();
loader.LoadStudy(study.NetworkSourcePath, _config.GlobalSettings.LocalTempDirectory);
List<ComputeConfiguration> computations = BuildComputationList(study);
Console.WriteLine($" Found {computations.Count} computations to run.");
foreach (ComputeConfiguration compute in computations)
{
_cts.Token.ThrowIfCancellationRequested();
Stopwatch computeStopwatch = Stopwatch.StartNew();
try
{
switch (compute.Type.ToLowerInvariant())
{
case "stagedamage":
StageDamageResult sdResult = StageDamageRunner.RunStageDamage(compute.ElementName);
SaveStageDamageResults(compute.ElementName, sdResult.StageDamageFunctions);
WriteStructureDetails(study.StudyId, compute.ElementName, sdResult);
_csvReportFactory.AddStageDamageSummary(study.StudyId, compute.ElementName, sdResult.StageDamageFunctions);
break;
case "scenario":
ScenarioResults scenarioResults = ScenarioRunner.RunScenario(compute.ElementName, _cts.Token);
IASElement scenarioElement = SaveScenarioResults(compute.ElementName, scenarioResults);
_csvReportFactory.AddScenarioResults(study.StudyId, scenarioElement);
break;
case "alternative":
AlternativeResults altResults = AlternativeRunner.RunAlternative(compute.ElementName, _cts.Token);
AlternativeElement altElement = SaveAlternativeResults(compute.ElementName, altResults);
_csvReportFactory.AddAlternativeResults(study.StudyId, altElement);
break;
case "alternativecomparison":
(AlternativeComparisonReportResults compResults, List<(int altId, string altName)> withProjAlts) = RunAlternativeComparisonWithMetadata(compute.ElementName, _cts.Token);
_csvReportFactory.AddAlternativeComparisonResults(study.StudyId, compute.ElementName, compResults, withProjAlts);
break;
default:
Console.WriteLine($" SKIP: Unknown compute type '{compute.Type}'");
continue;
}
computeStopwatch.Stop();
studyCompleted++;
Console.WriteLine($" OK: {compute.Type} '{compute.ElementName}' [{FormatDuration(computeStopwatch.Elapsed)}]");
}
catch (Exception ex)
{
computeStopwatch.Stop();
studyErrors++;
Console.WriteLine($" ERROR: {compute.Type} '{compute.ElementName}' [{FormatDuration(computeStopwatch.Elapsed)}]");
Console.WriteLine($" {ex.Message}");
Console.WriteLine($" {ex.StackTrace}");
}
}
}
catch (OperationCanceledException)
{
Console.WriteLine(" TIMEOUT: Computation exceeded time limit.");
studyErrors++;
break;
}
catch (Exception ex)
{
Console.WriteLine($" ERROR loading study: {ex.Message}");
Console.WriteLine($" {ex.StackTrace}");
studyErrors++;
}
studyStopwatch.Stop();
studyTimings.Add((study.StudyId, studyStopwatch.Elapsed, studyCompleted, studyErrors));
completed += studyCompleted;
errors += studyErrors;
Console.WriteLine($" Completed in {FormatDuration(studyStopwatch.Elapsed)} ({studyCompleted} succeeded, {studyErrors} failed)");
Console.WriteLine();
}
totalStopwatch.Stop();
// Summary
Console.WriteLine("=== Summary ===");
Console.WriteLine($"Completed: {completed}");
Console.WriteLine($"Errors: {errors}");
Console.WriteLine($"Duration: {FormatDuration(totalStopwatch.Elapsed)}");
Console.WriteLine();
// Save CSV report
string csvPath = Path.Combine(_outputDir, "results_report.csv");
_csvReportFactory.SaveReport(csvPath);
return errors > 0 ? 1 : 0;
}
private static string FormatDuration(TimeSpan duration)
{
if (duration.TotalHours >= 1)
{
return $"{duration.Hours}h {duration.Minutes}m {duration.Seconds}s";
}
else if (duration.TotalMinutes >= 1)
{
return $"{duration.Minutes}m {duration.Seconds}.{duration.Milliseconds / 100}s";
}
else
{
return $"{duration.Seconds}.{duration.Milliseconds:D3}s";
}
}
private static List<ComputeConfiguration> BuildComputationList(StudyConfiguration study)
{
List<ComputeConfiguration> computations = new(study.Computations);
if (study.RunAllStageDamage)
{
List<AggregatedStageDamageElement> stageDamages = BaseViewModel.StudyCache.GetChildElementsOfType<AggregatedStageDamageElement>();
foreach (AggregatedStageDamageElement sd in stageDamages)
{
if (!computations.Any(c => c.Type.Equals("stagedamage", StringComparison.OrdinalIgnoreCase)
&& c.ElementName.Equals(sd.Name, StringComparison.OrdinalIgnoreCase)))
{
computations.Add(new ComputeConfiguration { Type = "stagedamage", ElementName = sd.Name });
Console.WriteLine($" Auto-discovered stage damage: {sd.Name}");
}
}
}
if (study.RunAllScenarios)
{
List<IASElement> scenarios = BaseViewModel.StudyCache.GetChildElementsOfType<IASElement>();
foreach (IASElement scenario in scenarios)
{
if (!computations.Any(c => c.Type.Equals("scenario", StringComparison.OrdinalIgnoreCase)
&& c.ElementName.Equals(scenario.Name, StringComparison.OrdinalIgnoreCase)))
{
computations.Add(new ComputeConfiguration { Type = "scenario", ElementName = scenario.Name });
Console.WriteLine($" Auto-discovered scenario: {scenario.Name}");
}
}
}
if (study.RunAllAlternatives)
{
List<AlternativeElement> alternatives = BaseViewModel.StudyCache.GetChildElementsOfType<AlternativeElement>();
foreach (AlternativeElement alt in alternatives)
{
if (!computations.Any(c => c.Type.Equals("alternative", StringComparison.OrdinalIgnoreCase)
&& c.ElementName.Equals(alt.Name, StringComparison.OrdinalIgnoreCase)))
{
computations.Add(new ComputeConfiguration { Type = "alternative", ElementName = alt.Name });
Console.WriteLine($" Auto-discovered alternative: {alt.Name}");
}
}
}
if (study.RunAllAlternativeComparisons)
{
List<AlternativeComparisonReportElement> altCompReports = BaseViewModel.StudyCache.GetChildElementsOfType<AlternativeComparisonReportElement>();
foreach (AlternativeComparisonReportElement report in altCompReports)
{
if (!computations.Any(c => c.Type.Equals("alternativecomparison", StringComparison.OrdinalIgnoreCase)
&& c.ElementName.Equals(report.Name, StringComparison.OrdinalIgnoreCase)))
{
computations.Add(new ComputeConfiguration { Type = "alternativecomparison", ElementName = report.Name });
Console.WriteLine($" Auto-discovered alternative comparison: {report.Name}");
}
}
}
return SortByDependencyOrder(computations);
}
private static List<ComputeConfiguration> SortByDependencyOrder(List<ComputeConfiguration> computations)
{
int GetOrder(string type) => type.ToLowerInvariant() switch
{
"stagedamage" => 0,
"scenario" => 1,
"alternative" => 2,
"alternativecomparison" => 3,
_ => 99
};
return computations.OrderBy(c => GetOrder(c.Type)).ToList();
}
private static (AlternativeComparisonReportResults results, List<(int altId, string altName)> withProjectAlternatives) RunAlternativeComparisonWithMetadata(string elementName, CancellationToken cancellationToken)
{
AlternativeComparisonReportElement element = ScenarioRunner.FindElement<AlternativeComparisonReportElement>(elementName);
List<(int altId, string altName)> withProjectAlternatives = new();
List<AlternativeElement> allAlternatives = BaseViewModel.StudyCache.GetChildElementsOfType<AlternativeElement>();
foreach (int altId in element.WithProjAltIDs)
{
AlternativeElement? alt = allAlternatives.FirstOrDefault(a => a.ID == altId);
string altName = alt?.Name ?? $"Alternative_{altId}";
withProjectAlternatives.Add((altId, altName));
}
AlternativeComparisonReportResults results = AlternativeComparisonRunner.RunAlternativeComparison(elementName, cancellationToken);
return (results, withProjectAlternatives);
}
private static IASElement SaveScenarioResults(string elementName, ScenarioResults results)
{
IASElement element = ScenarioRunner.FindElement<IASElement>(elementName);
element.Results = results;
PersistenceFactory.GetIASManager().SaveExisting(element);
Console.WriteLine($" Saved to temp database.");
return element;
}
private static AlternativeElement SaveAlternativeResults(string elementName, AlternativeResults results)
{
AlternativeElement element = ScenarioRunner.FindElement<AlternativeElement>(elementName);
element.Results = results;
PersistenceFactory.GetElementManager<AlternativeElement>().SaveExisting(element);
Console.WriteLine($" Saved to temp database.");
return element;
}
private static void SaveStageDamageResults(string elementName, List<UncertainPairedData> curves)
{
AggregatedStageDamageElement element = ScenarioRunner.FindElement<AggregatedStageDamageElement>(elementName);
List<ImpactAreaElement> impactAreaElements = BaseViewModel.StudyCache.GetChildElementsOfType<ImpactAreaElement>();
ImpactAreaElement? impactAreaElement = impactAreaElements.Count > 0 ? impactAreaElements[0] : null;
List<StageDamageCurve> stageDamageCurves = new();
foreach (UncertainPairedData upd in curves)
{
CurveComponentVM curveComponent = new(StringConstants.STAGE_DAMAGE, StringConstants.STAGE, StringConstants.DAMAGE, DistributionOptions.HISTOGRAM_ONLY);
curveComponent.SetPairedData(upd);
ImpactAreaRowItem impactAreaRowItem = impactAreaElement?.GetImpactAreaRow(upd.ImpactAreaID)
?? new ImpactAreaRowItem(upd.ImpactAreaID, "");
StageDamageCurve sdCurve = new(impactAreaRowItem, upd.DamageCategory, curveComponent, upd.AssetCategory, StageDamageConstructionType.COMPUTED);
stageDamageCurves.Add(sdCurve);
}
element.Curves.Clear();
element.Curves.AddRange(stageDamageCurves);
PersistenceFactory.GetElementManager<AggregatedStageDamageElement>().SaveExisting(element);
Console.WriteLine($" Saved {curves.Count} curves to temp database.");
}
private void WriteStructureDetails(string studyId, string elementName, StageDamageResult sdResult)
{
if (sdResult.ScenarioStageDamage == null)
{
Console.WriteLine($" Skipping structure details for manual stage damage '{elementName}'.");
return;
}
string detailsDir = Path.Combine(_outputDir, studyId, "StructureDetails");
Directory.CreateDirectory(detailsDir);
Dictionary<int, string> iaNames = [];
List<ImpactAreaRowItem> iaRows = sdResult.ImpactAreaElement.ImpactAreaRows;
for (int i = 0; i < iaRows.Count; i++)
{
iaNames[i] = iaRows[i].Name;
}
string detailsPath = Path.Combine(detailsDir, $"{elementName}_StructureStageDamageDetails.csv");
List<string> structureDetails = sdResult.ScenarioStageDamage.ProduceStructureDetails(iaNames);
using (StreamWriter writer = new(File.Create(detailsPath)))
{
foreach (string line in structureDetails)
{
writer.WriteLine(line);
}
}
Console.WriteLine($" Wrote structure details to {detailsPath}");
string damagedElesPath = Path.Combine(detailsDir, $"{elementName}_DamagedElementCountsByStage.csv");
List<string> damagedElementCounts = UncertainPairedData.ConvertDamagedElementCountToText(sdResult.DamagedElementCounts, iaNames);
using (StreamWriter writer = new(File.Create(damagedElesPath)))
{
foreach (string line in damagedElementCounts)
{
writer.WriteLine(line);
}
}
Console.WriteLine($" Wrote damaged element counts to {damagedElesPath}");
}
}