-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuildModel.mos.tpl
184 lines (165 loc) · 8.95 KB
/
BuildModel.mos.tpl
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
echo(false);
system("mkdir -p files");
alarm(#ulimitOmc#);
removeTempFiles := true;
if removeTempFiles then
setCommandLineOptions("--running-testsuite=#fileName#.tmpfiles");
system("rm -f #fileName#.tmpfiles");
end if;
statFile := "files/#fileName#.stat";
writeFile("#logFile#","#fileName#\n",append=true);
#customCommands#
outputFormat:="mat";
mslRegressionOutput:="";
reference_reltol:=#reference_reltol#;
reference_reltolDiffMinMax:=#reference_reltolDiffMinMax#;
reference_rangeDelta:=#reference_rangeDelta#;
referenceOK := false;
referenceFiles := "#referenceFiles#";
referenceCell := if referenceFiles == "" then "" else "<td> </td>";
reference := "#referenceFiles#/"+OpenModelica.Scripting.stringReplace("#modelName#",".","#referenceFileNameDelimiter#")+".#referenceFileExtension#";
referenceExists := referenceFiles <> "" and regularFileExists(reference);
if not referenceExists then
outputFormat := "empty";
end if;
compareVarsUri := "modelica://" + /*libraryString*/ "Buildings" + "/Resources/Scripts/OpenModelica/compareVars/#modelName#.mos";
(compareVarsFile,compareVarsFileMessages) := uriToFilename(compareVarsUri);
if regularFileExists(compareVarsFile) then
runScript(compareVarsFile);
vars := compareVars;
variableFilter := sum(stringReplace(stringReplace(s,"[","."),"]",".") + "|" for s in vars) + "time";
numCompared := size(vars,1);
emit_protected := " -emit_protected";
elseif referenceExists then
vars := readSimulationResultVars(reference, readParameters=true, openmodelicaStyle=true);
variableFilter := sum(stringReplace(stringReplace(stringReplace(stringReplace(s,"[","."),"]","."),"(","."),")",".") + "|" for s in vars);
numCompared := size(vars,1);
emit_protected := " -emit_protected";
else
variableFilter := "";
outputFormat := "empty";
emit_protected := "";
end if;
OpenModelica.Scripting.Internal.Time.timerTick(OpenModelica.Scripting.Internal.Time.RT_CLOCK_USER_RESERVED);
if not loadModel(#library#, {"#modelVersion#"}) then
print(getErrorString());
exit(1);
end if;
timeParsing:=OpenModelica.Scripting.Internal.Time.timerTock(OpenModelica.Scripting.Internal.Time.RT_CLOCK_USER_RESERVED);
alarm(#ulimitOmc#); // Reset the alarm in case the other parts took a long time (reading simulation results)
// Use twice as many output points as the experiment annotation suggests. Else aim for 5000 points.
(startTime,stopTime,tolerance,numberOfIntervals,stepSize):=getSimulationOptions(#modelName#,defaultTolerance=#default_tolerance#,defaultNumberOfIntervals=2500);
res:=buildModel(#modelName#,tolerance=tolerance,outputFormat=outputFormat,numberOfIntervals=2*numberOfIntervals,variableFilter=variableFilter,fileNamePrefix="#fileName#");
clearProgram();
// We built the model fine, so reset the alarm. The simulation executable will also have an alarm, making only result verification a potential to stall.
alarm(0);
errFile:="files/#fileName#.err";
simFile:="files/#fileName#.sim";
(nmessage,nerror,nwarning) := countMessages();
errorLinkClass := if nerror>0 then "messagesError" elseif nwarning>0 then "messagesWarning" else "messagesInfo";
err:=getErrorString();
system("rm -f " + errFile);
writeFile(simFile,"");
if err <> "" then
writeFile(errFile,err);
end if;
build := OpenModelica.Scripting.Internal.Time.timerTock(OpenModelica.Scripting.Internal.Time.RT_CLOCK_BUILD_MODEL);
total := OpenModelica.Scripting.Internal.Time.timerTock(OpenModelica.Scripting.Internal.Time.RT_CLOCK_SIMULATE_TOTAL);
templates:= OpenModelica.Scripting.Internal.Time.timerTock(OpenModelica.Scripting.Internal.Time.RT_CLOCK_TEMPLATES);
simcode := OpenModelica.Scripting.Internal.Time.timerTock(OpenModelica.Scripting.Internal.Time.RT_CLOCK_SIMCODE);
backend := OpenModelica.Scripting.Internal.Time.timerTock(OpenModelica.Scripting.Internal.Time.RT_CLOCK_BACKEND);
frontend := OpenModelica.Scripting.Internal.Time.timerTock(OpenModelica.Scripting.Internal.Time.RT_CLOCK_FRONTEND);
frontend :=if backend <> -1.0 then frontend-backend else frontend;
backend :=if simcode <> -1.0 then backend-simcode else backend;
simcode :=if templates <> -1.0 then simcode-templates else simcode;
templates:=if build <> -1.0 then templates-build else templates;
timeDiff := -1.0;
buildRes := res[1] <> "";
OpenModelica.Scripting.Internal.Time.timerTick(OpenModelica.Scripting.Internal.Time.RT_CLOCK_USER_RESERVED);
simRes := if not buildRes then false else 0 == system("./#fileName# #simFlags# "+emit_protected+" > "+simFile+" 2>&1");
timeSim := OpenModelica.Scripting.Internal.Time.timerTock(OpenModelica.Scripting.Internal.Time.RT_CLOCK_USER_RESERVED);
resFile := "#fileName#_res." + outputFormat;
system("sed -i '300,$ d' '" + simFile + "'"); // Only keep the top 300 lines
if not loadFile("toJSON.mo") then
print("Failed to load toJSON.mo: " + getErrorString());
exit(1);
end if;
if not loadFile("csvFileToVariable.mo") then
print("Failed to load csvFileToVariable.mo: " + getErrorString());
exit(1);
end if;
json := toJSON(timeParsing, frontend, backend, simcode, templates, build, buildRes, timeSim, simRes);
writeFile("files/#fileName#.stat.json", json);
if simRes then
system("touch #fileName#.simsuccess");
prefix := "files/#fileName#.diff";
if referenceExists then
OpenModelica.Scripting.Internal.Time.timerTick(OpenModelica.Scripting.Internal.Time.RT_CLOCK_USER_RESERVED);
getErrorString();
(referenceOK,diffVars) := diffSimulationResults(resFile,reference,prefix,relTol=reference_reltol,relTolDiffMinMax=reference_reltolDiffMinMax,rangeDelta=reference_rangeDelta);
errVerify := getErrorString();
if errVerify <> "" then
writeFile(errFile, "\nVariables in the reference:"+sum(var+"," for var in OpenModelica.Scripting.readSimulationResultVars(reference, openmodelicaStyle=true)), append=true);
writeFile(errFile, "\nVariables in the result:"+sum(var+"," for var in OpenModelica.Scripting.readSimulationResultVars(resFile))+"\n" + errVerify, append=true);
end if;
if referenceOK then
system("touch #fileName#.verifysuccess");
end if;
timeDiff := OpenModelica.Scripting.Internal.Time.timerTock(OpenModelica.Scripting.Internal.Time.RT_CLOCK_USER_RESERVED);
diffFiles := {prefix + "." + var for var in diffVars};
// Create a file containing only the calibrated variables, for easy display
if not referenceOK then
referenceCell := "<td bgcolor=\"#FF0000\">"+OpenModelica.Scripting.Internal.Time.readableTime(timeDiff)+", <a href=\"files/#fileName#.diff.html\">"+String(size(diffFiles,1))+"/"+String(numCompared)+" signals failed</a></td>";
OpenModelica.Scripting.writeFile("files/#fileName#.diff.html","<html><body><h1>#modelName# differences from the reference file</h1><p>startTime: "+String(startTime)+"</p><p>stopTime: "+String(stopTime)+"</p><p>Simulated using tolerance: "+String(tolerance)+"</p><ul>" + sum("<li>"+csvFileToVariable(file)+" <a href=\""+OpenModelica.Scripting.basename(file)+".html\">(javascript)</a> <a href=\""+OpenModelica.Scripting.basename(file)+".csv\">(csv)</a></li>" for file in diffFiles) + "</ul></body></html>");
{writeFile(prefix + "." + var + ".html","<html>
<head>
<script type=\"text/javascript\" src=\"dygraph-combined.js\"></script>
<style type=\"text/css\">
#graphdiv {
position: absolute;
left: 10px;
right: 10px;
top: 40px;
bottom: 10px;
}
</style>
</head>
<body>
<div id=\"graphdiv\"></div>
<p><input type=checkbox id=\"0\" checked onClick=\"change(this)\">
<label for=\"0\">reference</label>
<input type=checkbox id=\"1\" checked onClick=\"change(this)\">
<label for=\"1\">actual</label>
<input type=checkbox id=\"2\" checked onClick=\"change(this)\">
<label for=\"2\">high</label>
<input type=checkbox id=\"3\" checked onClick=\"change(this)\">
<label for=\"3\">low</label>
<input type=checkbox id=\"4\" checked onClick=\"change(this)\">
<label for=\"4\">error</label>
<input type=checkbox id=\"5\" onClick=\"change(this)\">
<label for=\"5\">actual (original)</label>
Parameters used for the comparison: Relative tolerance "+String(reference_reltol)+" (local), "+String(reference_reltolDiffMinMax)+" (relative to max-min). Range delta "+String(reference_rangeDelta)+".</p>
<script type=\"text/javascript\">
g = new Dygraph(document.getElementById(\"graphdiv\"),
\""+OpenModelica.Scripting.basename(prefix + "." + var + ".csv")+"\",{title: '"+var+"',
legend: 'always',
connectSeparatedPoints: true,
xlabel: ['time'],
y2label: ['error'],
series : { 'error': { axis: 'y2' } },
colors: ['blue','red','teal','lightblue','orange','black'],
visibility: [true,true,true,true,true,false]
});
function change(el) {
g.setVisibility(parseInt(el.id), el.checked);
}
</script>
</body>
</html>") for var in diffVars};
else
referenceCell := "<td bgcolor=\"#00FF00\">"+OpenModelica.Scripting.Internal.Time.readableTime(timeDiff)+" ("+String(numCompared)+" signals)</td>";
end if;
json := toJSON(timeParsing, frontend, backend, simcode, templates, build, true, timeSim, true, timeDiff, diffVars, numCompared);
writeFile("files/#fileName#.stat.json", json);
end if;
end if;