Skip to content

Commit

Permalink
make all examples run during testing, eliminate dependence on matplotlib
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchute committed Jul 23, 2024
1 parent 56d9ebc commit 8c48424
Show file tree
Hide file tree
Showing 25 changed files with 725 additions and 625 deletions.
5 changes: 3 additions & 2 deletions examples/bore_field_thermal_resistance.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
"""
try:
import matplotlib.pyplot as plt
enable_plotting = True
except ModuleNotFoundError:
pass
enable_plotting = False

import numpy as np
from scipy.constants import pi
Expand Down Expand Up @@ -117,7 +118,7 @@ def main(make_plots=True):
# Plot bore field thermal resistances
# -------------------------------------------------------------------------

if make_plots:
if enable_plotting and make_plots:

# Configure figure and axes
fig = gt.utilities._initialize_figure()
Expand Down
7 changes: 4 additions & 3 deletions examples/comparison_gfunction_solvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
"""
try:
import matplotlib.pyplot as plt
enable_plotting = True
except ModuleNotFoundError:
pass
enable_plotting = False

import numpy as np
from time import perf_counter
Expand Down Expand Up @@ -81,7 +82,7 @@ def main(make_plots=True):
t3 = perf_counter()
t_equivalent = t3 - t2

if make_plots:
if enable_plotting and make_plots:

# -------------------------------------------------------------------------
# Plot results
Expand Down Expand Up @@ -156,7 +157,7 @@ def main(make_plots=True):
t3 = perf_counter()
t_equivalent = t3 - t2

if make_plots:
if enable_plotting and make_plots:

# -------------------------------------------------------------------------
# Plot results
Expand Down
36 changes: 4 additions & 32 deletions examples/comparison_load_aggregation.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@

try:
import matplotlib.pyplot as plt
enable_plotting = True
except ModuleNotFoundError:
pass
enable_plotting = False

import numpy as np
from scipy.constants import pi
Expand Down Expand Up @@ -53,7 +54,7 @@ def main(make_plots=True):
time = dt * np.arange(1, Nt+1)

# Evaluate heat extraction rate
Q_b = synthetic_load(time/3600.)
Q_b = gt.utilities.synthetic_load(time/3600.)

# Load aggregation schemes
ClaessonJaved = gt.load_aggregation.ClaessonJaved(dt, tmax)
Expand Down Expand Up @@ -122,7 +123,7 @@ def main(make_plots=True):
# Convolution in Fourier domain
T_b_exact = T_g - fftconvolve(dQ, g/(2.0*pi*k_s*H), mode='full')[0:Nt]

if make_plots:
if enable_plotting and make_plots:

# -------------------------------------------------------------------------
# plot results
Expand Down Expand Up @@ -173,35 +174,6 @@ def main(make_plots=True):
print(f'Maximum absolute error : {maxError_n:.3f} degC')
print(f'Calculation time : {t_calc_n:.3f} sec')

return


def synthetic_load(x):
"""
Synthetic load profile of Bernier et al. (2004).
Returns load y (in watts) at time x (in hours).
"""
A = 2000.0
B = 2190.0
C = 80.0
D = 2.0
E = 0.01
F = 0.0
G = 0.95

func = (168.0-C)/168.0
for i in [1, 2, 3]:
func += 1.0/(i*pi)*(np.cos(C*pi*i/84.0) - 1.0) \
*(np.sin(pi*i/84.0*(x - B)))
func = func*A*np.sin(pi/12.0*(x - B)) \
*np.sin(pi/4380.0*(x - B))

y = func + (-1.0)**np.floor(D/8760.0*(x - B))*abs(func) \
+ E*(-1.0)**np.floor(D/8760.0*(x - B)) \
/np.sign(np.cos(D*pi/4380.0*(x - F)) + G)
return -y


# Main function
if __name__ == '__main__':
Expand Down
7 changes: 3 additions & 4 deletions examples/custom_bore_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pygfunction as gt


def main():
def main(make_plots=True):
# -------------------------------------------------------------------------
# Parameters
# -------------------------------------------------------------------------
Expand Down Expand Up @@ -45,9 +45,8 @@ def main():
# Draw bore field
# -------------------------------------------------------------------------

gt.boreholes.visualize_field(field)

return
if make_plots:
gt.boreholes.visualize_field(field)


# Main function
Expand Down
15 changes: 7 additions & 8 deletions examples/custom_bore_field_from_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
import pygfunction as gt


def main():
def main(make_plots=True):
# -------------------------------------------------------------------------
# Parameters
# -------------------------------------------------------------------------

# Filepath to bore field text file
filename = Path(__file__).parent / "data", "custom_field_32_boreholes.txt"
filename = Path(__file__).parent / "data" / "custom_field_32_boreholes.txt"

# -------------------------------------------------------------------------
# Borehole field
Expand All @@ -23,13 +23,12 @@ def main():
# Build list of boreholes
field = gt.boreholes.field_from_file(filename)

# -------------------------------------------------------------------------
# Draw bore field
# -------------------------------------------------------------------------

gt.boreholes.visualize_field(field)
if make_plots:
# -------------------------------------------------------------------------
# Draw bore field
# -------------------------------------------------------------------------

return
gt.boreholes.visualize_field(field)


# Main function
Expand Down
23 changes: 13 additions & 10 deletions examples/custom_borehole.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import pygfunction as gt


def main():
def main(make_plots=True):
# -------------------------------------------------------------------------
# Simulation parameters
# -------------------------------------------------------------------------
Expand Down Expand Up @@ -100,9 +100,10 @@ def main():
print(f'Single U-tube Borehole thermal resistance: '
f'{R_b:.4f} m.K/W')

# Visualize the borehole geometry and save the figure
fig_single = SingleUTube.visualize_pipes()
fig_single.savefig('single-u-tube-borehole.png')
if make_plots:
# Visualize the borehole geometry and save the figure
fig_single = SingleUTube.visualize_pipes()
fig_single.savefig('single-u-tube-borehole.png')

# -------------------------------------------------------------------------
# Define a double U-tube borehole
Expand Down Expand Up @@ -152,9 +153,10 @@ def main():
print(f'Double U-tube (parallel) Borehole thermal resistance: '
f'{R_b_parallel:.4f} m.K/W')

# Visualize the borehole geometry and save the figure
fig_double = DoubleUTube_series.visualize_pipes()
fig_double.savefig('double-u-tube-borehole.png')
if make_plots:
# Visualize the borehole geometry and save the figure
fig_double = DoubleUTube_series.visualize_pipes()
fig_double.savefig('double-u-tube-borehole.png')

# -------------------------------------------------------------------------
# Define a coaxial borehole
Expand Down Expand Up @@ -197,9 +199,10 @@ def main():
m_flow_borehole, fluid.cp)
print(f'Coaxial tube Borehole thermal resistance: {R_b:.4f} m.K/W')

# Visualize the borehole geometry and save the figure
fig_coaxial = Coaxial.visualize_pipes()
fig_coaxial.savefig('coaxial-borehole.png')
if make_plots:
# Visualize the borehole geometry and save the figure
fig_coaxial = Coaxial.visualize_pipes()
fig_coaxial.savefig('coaxial-borehole.png')


if __name__ == '__main__':
Expand Down
45 changes: 27 additions & 18 deletions examples/discretize_boreholes.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@
can be calculated accurately using a small number of segments.
"""

try:
import matplotlib.pyplot as plt
enable_plotting = True
except ModuleNotFoundError:
enable_plotting = False

import pygfunction as gt
from numpy import pi
import matplotlib.pyplot as plt
import numpy as np


def main():
def main(make_plots=True):
# -------------------------------------------------------------------------
# Simulation parameters
# -------------------------------------------------------------------------
Expand Down Expand Up @@ -89,7 +94,10 @@ def main():
N_1 = 6
N_2 = 4
boreField = gt.boreholes.rectangle_field(N_1, N_2, B, B, H, D, r_b)
gt.boreholes.visualize_field(boreField)

if make_plots:
gt.boreholes.visualize_field(boreField)

nBoreholes = len(boreField)

# -------------------------------------------------------------------------
Expand Down Expand Up @@ -150,22 +158,23 @@ def main():
RMSE_UBWT = RMSE(gfunc_UBWT_uniform.gFunc, gfunc_UBWT_unequal.gFunc)
print(f'RMSE (UBWT) = {RMSE_UBWT:.5f}')

# -------------------------------------------------------------------------
# Plot g-functions
# -------------------------------------------------------------------------
if enable_plotting and make_plots:

# -------------------------------------------------------------------------
# Plot g-functions
# -------------------------------------------------------------------------

ax = gfunc_MIFT_uniform.visualize_g_function().axes[0]
ax.plot(np.log(time / ts), gfunc_UBWT_uniform.gFunc)
ax.plot(np.log(time / ts), gfunc_MIFT_unequal.gFunc, 'o')
ax.plot(np.log(time / ts), gfunc_UBWT_unequal.gFunc, 'o')
ax.legend(
['Equal inlet temperature (uniform segments)',
'Uniform borehole wall temperature (uniform segments)',
'Equal inlet temperature (non-uniform segments)',
'Uniform borehole wall temperature (non-uniform segments)'])
plt.tight_layout()

ax = gfunc_MIFT_uniform.visualize_g_function().axes[0]
ax.plot(np.log(time / ts), gfunc_UBWT_uniform.gFunc)
ax.plot(np.log(time / ts), gfunc_MIFT_unequal.gFunc, 'o')
ax.plot(np.log(time / ts), gfunc_UBWT_unequal.gFunc, 'o')
ax.legend(
['Equal inlet temperature (uniform segments)',
'Uniform borehole wall temperature (uniform segments)',
'Equal inlet temperature (non-uniform segments)',
'Uniform borehole wall temperature (non-uniform segments)'])
plt.tight_layout()

return


def RMSE(reference, predicted):
Expand Down
34 changes: 19 additions & 15 deletions examples/equal_inlet_temperature.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@
boreholes, and (c) equal inlet fluid temperature into all boreholes.
"""
import matplotlib.pyplot as plt
try:
import matplotlib.pyplot as plt
enable_plotting = True
except ModuleNotFoundError:
enable_plotting = False

import numpy as np
from scipy.constants import pi

import pygfunction as gt


def main():
def main(make_plots=True):
# -------------------------------------------------------------------------
# Simulation parameters
# -------------------------------------------------------------------------
Expand Down Expand Up @@ -116,19 +121,18 @@ def main():
gfunc_equal_Tf_in = gt.gfunction.gFunction(
network, alpha, time=time, boundary_condition='MIFT', options=options)

# -------------------------------------------------------------------------
# Plot g-functions
# -------------------------------------------------------------------------

ax = gfunc_uniform_Q.visualize_g_function().axes[0]
ax.plot(np.log(time/ts), gfunc_uniform_T.gFunc, 'k--')
ax.plot(np.log(time/ts), gfunc_equal_Tf_in.gFunc, 'r-.')
ax.legend(['Uniform heat extraction rate',
'Uniform borehole wall temperature',
'Equal inlet temperature'])
plt.tight_layout()

return
if enable_plotting and make_plots:
# -------------------------------------------------------------------------
# Plot g-functions
# -------------------------------------------------------------------------

ax = gfunc_uniform_Q.visualize_g_function().axes[0]
ax.plot(np.log(time/ts), gfunc_uniform_T.gFunc, 'k--')
ax.plot(np.log(time/ts), gfunc_equal_Tf_in.gFunc, 'r-.')
ax.legend(['Uniform heat extraction rate',
'Uniform borehole wall temperature',
'Equal inlet temperature'])
plt.tight_layout()


# Main function
Expand Down
Loading

0 comments on commit 8c48424

Please sign in to comment.