-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdemo_002.js
164 lines (143 loc) · 6.46 KB
/
demo_002.js
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
// This is a support file for the demo_001.html page.
// This is test code that will be considered for incorporation to presentation scripts.
//
// This code should:
// - Load up data for a given model (demo_001.inp, demo_001.rpt, demo_001.out).
// - Present the model in a spatial display.
// - Describe the demo.
// - Show the input rainfail pattern.
// - Show the output pipe flow for a given pipe.
// When the document is loaded:
// Create some data
// Draw a line chart with the data.
document.addEventListener("DOMContentLoaded", function() {
// Open up the file for the article.
// This should not be hardcoded, it should be found in the article markdown file.
// Yes, this is an indicator of the need for proper file interaction.
// Open the input file.
jQuery.get('./data/demo_001.inp', function(contents){
processInput(contents);
buildSwitchList();
demoRun();
})
})
function demoRun(){
// dataObj is an array of dataElement objects.
dataObj = [];
let inpText = null;
// Create a set of dataElements.
//Get the input file for parsing:
// Since we are running a model, it would be a good idea to
// write the current model objects into a string field,
// then send that string field to the executable.
// --1: Translate the model to a string vSia svg.save() in swmm.js
// --2: Modify svg.save to instead call a string creation function.
// This function can then be called by this click event as well, so no files
// need to be saved (though it would be a good idea to save a file before you run it, right?)
// --3: New function is called svg.dataToInpString().
// --4: To send the inpString to the swmm_run file, inpText can be used.
fetch('data/info.json')
.then(response => response.text())
.then((data) => {
inpText = swmmjs.svg.dataToInpString();
try
{
FS.createPath('/', '/', true, true);
FS.ignorePermissions = true;
var f = FS.findObject('input.inp');
if (f) {
FS.unlink('input.inp');
}
FS.createDataFile('/', 'input.inp', inpText, true, true);
async function processModel(){
swmm_run("/input.inp", "data/Example1x.rpt", "data/out.out");
return 1;
}
async function createFile(){
FS.createDataFile('/', 'input.inp', inpText, true, true)
return 0;
}
createFile().then(
processModel().then(function (){
demoChart();
})
)
} catch (e) {
console.log('/input.inp creation failed');
// Remove the processing modal.
$('#modalSpinner').modal('hide')
} finally{
// Remove the processing modal.
$('#modalSpinner').modal('hide')
}
console.log('runran')
})
}
// When results target has been fully identified by the user,
// clicking this button will show a time-based plot of the results for that object.
function demoChart(){
let dataObj = [];
// Identify where the chart will be drawn.
let viz_svg01 = d3.select("#demo_svg_01");
// Get the swmmresult. This should be redone once I can actually drag in single variables instead of
// reading the whole file into objects.
input = new d3.swmmresult();
// Once again, this should be a model-associated constant.
val = input.parse('data/out.out');
// Get the ID and type of the object that will be charted, and get the type of information that is necessary as well.
let objectName = '10'
let objectType = 'LINK'
let variable = '1'
// First: Utilize the start and end times of the model, along with the time step in order to create the fully realized chart.
let reportStartDate = '01/01/1998';
let reportStartTime = '00:00:00';
let reportStep = '00:01:00';
// Create a date object using the reportStartDate and reportStartTime.
let thisDateStep = moment(reportStartDate + ' ' + reportStartTime);
// Create a duration object using reportStep.
let stepDuration = moment.duration(reportStep)
// For every step, increase the time by the value of reportStep.
for(let i = 1; !!val[i]; i++){
// Use the incremented date object
let thisDate = moment(new Date(thisDateStep));
//dataObj.push(new DataElement('00:'+i.toString().padStart(2, '0'), val[i][objectType][parseInt(objectName)][parseInt(variable)]));
dataObj.push(new DataElement(thisDate._d, val[i][objectType][objectName][parseInt(variable)]));
// increment thisDateStep
thisDateStep.add(stepDuration)
}
// Create a new chartSpecs object and populate it with the data.
theseSpecs = new ChartSpecs(dataObj);
//$("#viz_svgTS").empty();
$('#demo_svg_01').empty();
// Prepare the chart and draw it.
representData(viz_svg01, theseSpecs);
}
// Call this function after the model has loaded.
// For every junction, add an <li><div><input><label> to the #demo001-list <ul>
// After adding the <li>, add a .change() to the <input> that
// references the name of the node.
function buildSwitchList(){
// For every conduit,
swmmjs.model.CONDUITS.forEach((cond, index) => {
// Add a <li><div><input><label> to the list
document.getElementById('demo001-list').insertAdjacentHTML('beforeend', `<li style="margin-top: 5px; padding-top: 5px;">
<div class="custom-control custom-switch">
<input class="custom-control-input" type="checkbox" id="option`+ index +`">
<label class="custom-control-label" style="transform: scale(1.5)" stylefor="option`+ index +`" for="option`+ index +`">Link `+ index +`</label>
</div>
</li>`)
// Add a toggle function to new element
// On use of the demo toggle, include inflow to a node in the model
$('#option'+index).change(function(){
// if the toggle is on, add the node inflow marker to the node.
if($(this).prop('checked') === true){
cond.Roughness = 0.024;
} else {
// remove any inflow to the node
cond.Roughness = 0.012;
}
// Rerun the model:
demoRun();
})
})
}