Skip to content

Commit

Permalink
Merge pull request #1609 from lbl-srg/issue1587_kalmanFilterFix
Browse files Browse the repository at this point in the history
Issue1587 kalman filter fix
  • Loading branch information
mwetter authored Oct 25, 2019
2 parents 465b55a + a32f2cc commit d535f4d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 16 deletions.
14 changes: 6 additions & 8 deletions Buildings/Resources/Python-Sources/KalmanFilter.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,16 @@ def filter(u):
# Science, TR 95-041,
# http://www.cs.unc.edu/~welch/kalman/kalmanIntro.html
# by Andrew D. Straw
import json
import os
import pickle

temporaryFile = "tmp-kalman.pkl"
temporaryFile = "tmp-kalman.json"

# Read past observations from file, if the file exists
# Otherwise, create a new array
if os.path.exists( temporaryFile ):
pkl_file = open(temporaryFile, 'rb')
d = pickle.load(pkl_file)
pkl_file.close()
with open(temporaryFile, 'r') as fh:
d = json.load(fh)
xhat = d['xhat']
P = d['P']
else:
Expand All @@ -54,8 +53,7 @@ def filter(u):

# Store observations to file for use in next iteration
d = {'xhat': xhat, 'P': P}
pkl_file = open(temporaryFile, 'wb')
pickle.dump(d, pkl_file)
pkl_file.close()
with open(temporaryFile, 'w') as fh:
json.dump(d, fh)

return xhat
6 changes: 0 additions & 6 deletions Buildings/Resources/Scripts/BuildingsPy/conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@
"jmodelica": {
"simulate": false
},
"model_name": "Buildings.Utilities.IO.Python27.Examples.KalmanFilter"
},
{
"jmodelica": {
"simulate": false
},
"model_name": "Buildings.Utilities.IO.Python27.Functions.Examples.Exchange"
},
{
Expand Down
10 changes: 8 additions & 2 deletions Buildings/Utilities/IO/Python27/Examples/KalmanFilter.mo
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ equation
// Delete the temporary file generated by the Python file
// at the start and end of the simulation.
when {initial(), terminal()} then
Modelica.Utilities.Files.removeFile("tmp-kalman.pkl");
end when;
Modelica.Utilities.Files.remove("tmp-kalman.json");
end when;

connect(clock.y, ran.uR[1]) annotation (Line(
points={{-59,10},{-42,10}},
Expand Down Expand Up @@ -84,6 +84,12 @@ http://www.scipy.org/Cookbook/KalmanFiltering</a>.
</html>", revisions="<html>
<ul>
<li>
October 24, 2019, by Antoine Gautier:<br/>
Changed the temporary file format from <code>pickle</code> to <code>json</code> as the former can trigger a
segfault with JModelica simulation run in a subprocess.<br/>
This is for <a href=\"https://github.com/lbl-srg/modelica-buildings/issues/1587\">Buildings, #1587</a>.
</li>
<li>
February 5, 2013, by Michael Wetter:<br/>
First implementation.
</li>
Expand Down
11 changes: 11 additions & 0 deletions Buildings/package.mo
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,17 @@ its class name ends with the string <code>Beta</code>.
This is for <a href=\"https://github.com/lbl-srg/modelica-buildings/issues/1577\">1577</a>.
</td>
</tr>
<tr><td colspan=\"2\"><b>Buildings.Utilities</b>
</td>
</tr>
<tr><td valign=\"top\">Buildings/Resources/Python-Sources/KalmanFilter.py<br/>
Buildings.Utilities.IO.Python27.Examples.KalmanFilter
</td>
<td valign=\"top\">Changed the temporary file format from <code>pickle</code> to <code>json</code> as the former can trigger a
segfault with JModelica simulation run in a subprocess.<br/>
This is for <a href=\"https://github.com/lbl-srg/modelica-buildings/issues/1587\">Buildings, #1587</a>.
</td>
</tr>
</table>
<!-- Uncritical errors -->
<p>
Expand Down

0 comments on commit d535f4d

Please sign in to comment.