Skip to content

Instantly share code, notes, and snippets.

@Denzo77
Created March 25, 2020 12:43
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Denzo77/ce5d15509be6017884d981e31a4cd2eb to your computer and use it in GitHub Desktop.
Save Denzo77/ce5d15509be6017884d981e31a4cd2eb to your computer and use it in GitHub Desktop.
Example usage of EnergyPlus hosted by PyFMI, including changing variables.
from pyfmi import load_fmu
import numpy as np
import matplotlib.pyplot as plt
# Runs the "Actuator" example provided by EnergyPlusToFMU
# Adapted from https://gist.github.com/TStesco/d02d19e7e382ab8c37d51c96d7891f6d
# Tested with:
# - Fedora 31 (up to date as of 2020-03-25)
# - EnergyPlus v9.0.1
# - EnergyPlusToFMU v2.1.0
# - Python 3.8.2
# - PyFMI v2.6
# - installed with `conda install -c conda-forge pyfmi`
# - Note: the package on the `chria` channel seems to be abandoned and did
# work for me.
# Using the following input files:
# - EnergyPlusToFMU-v2.1.0/Examples/Actuator/_fmu-export-actuator.idf
# - EnergyPlus-9-0-1/WeatherData/USA_IL_Chicago-OHare.Intl.AP.725300_TMY3.epw
#
# The FMU was compiled with the following command:
# > $ pwd
# >> .../EnergyPlusToFMU-v2.1.0/Examples/Actuator
# > $
# > $ ls -l
# >> -rwxrwxr-x. ... _fmu-export-actuator.idf
# >> -rw-rw-r--. ... pyfmi_do_sim.py
# >> -rw-rw-r--. ... requirements.txt
# >> -rw-r--r--. ... USA_IL_Chicago-OHare.Intl.AP.725300_TMY3.epw
# > $
# > $ python2 ../../Scripts/EnergyPlusToFMU.py \
# > -i ~/.local/share/energyplus/Energy+.idd \
# > -w USA_IL_Chicago-OHare.Intl.AP.725300_TMY3.epw \
# > _fmu-export-actuator.idf`
#
# Notes:
# - The simulation be run for 3 days in order to for differences to be visible
# in the graphs.
# - Changes to room temperature should be visible when changing `yShade`, but
# I could only see them on the third day.
# - When `yShade` is set to -1, 0 or 2, room temp peaks at above 21 C.
# - Other settings either fail or peak at around 20.5 C.
# - `yShade` must be passed in as a 'per step' variable - just initing it to a
# value is not enough.
# ----- Config Variables -----
# Path to FMU
fmu_path = '_fmu_export_actuator.fmu'
# Value of the input variable to the FMU.
#
# Tested [-1,7].
# Value 1 seems to be invalid and fail with:
# > Starting Simulation at 01/01/2011 for RUNPERIOD 1
# > ExternalInterface starts first data exchange.
# > **FATAL:Preceding condition terminates program.
# > EnergyPlus Run Time=00hr 00min 0.44sec
# > Program terminated: EnergyPlus Terminated--Error(s) Detected.
# > Error: No digits were found in getIntCheckErrorFMU.
# > Further characters after number:
# >
# > Sending EXIT_FAILURE = : 1
#
# Value 3 fails with:
# > Starting Simulation at 01/01/2011 for RUNPERIOD 1
# > ExternalInterface starts first data exchange.
# > **FATAL:Program halted because of convergence error in SolveForWindowTemperatures for window ZN001:WALL001:WIN001
# > EnergyPlus Run Time=00hr 00min 0.47sec
# > Program terminated: EnergyPlus Terminated--Error(s) Detected.
# > Error: No digits were found in getIntCheckErrorFMU.
# > Further characters after number:
# >
# > Sending EXIT_FAILURE = : 1
#
# Value 4 does not report an error, but produces all 0 values.
yShade = 0
# Setup simulation
seconds_in_hour = 60 * 60
start_time = 0
final_time = seconds_in_hour * 24 * 3 # 72 hour simulation
idf_steps_per_hour = 6
# ----- Setup Simulation -----
# load .fmu with pyfmi
model = load_fmu(fmu=fmu_path)
# set number of communication points dependent on final_time and .idf steps per hour
# ncp must be an int or it will crash pyfmi.
ncp = int(final_time/(seconds_in_hour/idf_steps_per_hour))
# get options object
opts = model.simulate_options()
opts['ncp'] = ncp
# Setup the input array.
step_size = seconds_in_hour / idf_steps_per_hour
shade_val = np.array([(i*step_size, yShade) for i in range(ncp)])
shade_val = ('yShade', shade_val)
# Set initial value of yShade
model.set(shade_val[0], shade_val[1][0][1])
# ----- Run Simulation And Plot -----
# run simulation and return results
res = model.simulate(start_time=0,
final_time=final_time,
input=shade_val,
options=opts)
print(res.keys()) # show result variables names
# plot results
fig, ax1 = plt.subplots()
ax1.plot(res['time'], res['TRoo'], 'b-')
ax1.set_xlabel('time (s)')
ax1.set_ylabel('Room Temperature', color='b')
ax1.tick_params('y', colors='b')
ax2 = ax1.twinx()
ax2.plot(res['time'], res['ISolExt'], 'r.')
ax2.set_ylabel('Solar Radiation', color='r')
ax2.tick_params('y', colors='r')
fig.tight_layout()
plt.show()
@Denzo77
Copy link
Author

Denzo77 commented Mar 25, 2020

Graphs for different values of yShade:

yShade = -1

yShade_-1

yShade = 0

yShade_0

yShade = 1

yShade_1

yShade = 2

yShade_2

yShade = 3

yShade_3

yShade = 4

yShade_4

yShade = 5

yShade_5

yShade = 6

yShade_6

yShade = 7

yShade_7

@jiahuizi
Copy link

jiahuizi commented Jan 6, 2024

Uploading 图片1.png…

@jiahuizi
Copy link

jiahuizi commented Jan 6, 2024

How to solve this problem?

@Denzo77
Copy link
Author

Denzo77 commented Jan 8, 2024

I can't see the image you've uploaded, and I'm afraid I haven't touched this in ~3 years so I probably can't help you.

I'd suggest asking for help here:
https://unmethours.com/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment