Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update examples #2

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 25 additions & 22 deletions examples/bore_field_thermal_resistance.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@
rates.

"""
import matplotlib.pyplot as plt
try:
import matplotlib.pyplot as plt
except ModuleNotFoundError:
pass

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Myoldmopar, can you take a look here before I push it all the way through?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might consider storing a variable to indicate whether it has been properly imported or not.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
try:
import matplotlib.pyplot as plt
except ModuleNotFoundError:
pass
try:
import matplotlib.pyplot as plt
plotting_enabled = True
except ModuleNotFoundError:
plotting_enabled = False

import numpy as np
from matplotlib.ticker import AutoMinorLocator
from scipy import pi
from scipy.constants import pi

import pygfunction as gt


def main():
def main(make_plots=True):
# -------------------------------------------------------------------------
# Simulation parameters
# -------------------------------------------------------------------------
Expand Down Expand Up @@ -114,28 +117,28 @@ def main():
# Plot bore field thermal resistances
# -------------------------------------------------------------------------

# Configure figure and axes
fig = gt.utilities._initialize_figure()
if make_plots:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely like this as an optional thing 👍


ax1 = fig.add_subplot(111)
# Axis labels
ax1.set_xlabel(r'$\dot{m}$ [kg/s]')
ax1.set_ylabel(r'$R^*_{field}$ [m.K/W]')
# Axis limits
ax1.set_xlim([0., 1.])
ax1.set_ylim([0., 1.])
# Configure figure and axes
fig = gt.utilities._initialize_figure()

gt.utilities._format_axes(ax1)
ax1 = fig.add_subplot(111)
# Axis labels
ax1.set_xlabel(r'$\dot{m}$ [kg/s]')
ax1.set_ylabel(r'$R^*_{field}$ [m.K/W]')
# Axis limits
ax1.set_xlim([0., 1.])
ax1.set_ylim([0., 1.])

# Bore field thermal resistances
ax1.plot(m_flow_network, R[0,:], '-', label='1 borehole')
ax1.plot(m_flow_network, R[2,:], '--', label='3 boreholes')
ax1.plot(m_flow_network, R[4,:], '-.', label='5 boreholes')
ax1.legend()
# Adjust to plot window
plt.tight_layout()
gt.utilities._format_axes(ax1)

return
# Bore field thermal resistances
ax1.plot(m_flow_network, R[0,:], '-', label='1 borehole')
ax1.plot(m_flow_network, R[2,:], '--', label='3 boreholes')
ax1.plot(m_flow_network, R[4,:], '-.', label='5 boreholes')
ax1.legend()
# Adjust to plot window
plt.tight_layout()


# Main function
Expand Down
204 changes: 105 additions & 99 deletions examples/comparison_gfunction_solvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,18 @@
speed while maintaining reasonable accuracy.

"""
import matplotlib.pyplot as plt
try:
import matplotlib.pyplot as plt
except ModuleNotFoundError:
pass

import numpy as np
from time import perf_counter

import pygfunction as gt


def main():
def main(make_plots=True):
# -------------------------------------------------------------------------
# Simulation parameters
# -------------------------------------------------------------------------
Expand Down Expand Up @@ -77,57 +81,59 @@ def main():
t3 = perf_counter()
t_equivalent = t3 - t2

# -------------------------------------------------------------------------
# Plot results
# -------------------------------------------------------------------------
# Draw g-functions
ax = gfunc_detailed.visualize_g_function().axes[0]
ax.plot(lntts, gfunc_similarities.gFunc, 'bx')
ax.plot(lntts, gfunc_equivalent.gFunc, 'ro')
ax.legend([f'detailed (t = {t_detailed:.3f} sec)',
f'similarities (t = {t_similarities:.3f} sec)',
f'equivalent (t = {t_equivalent:.3f} sec)'])
ax.set_title(f'Field of {N_1} by {N_2} boreholes')
plt.tight_layout()

# Draw absolute error
# Configure figure and axes
fig = gt.utilities._initialize_figure()
ax = fig.add_subplot(111)
# Axis labels
ax.set_xlabel(r'ln$(t/t_s)$')
ax.set_ylabel(r'Absolute error')
gt.utilities._format_axes(ax)
# Absolute error
ax.plot(lntts, np.abs(gfunc_similarities.gFunc - gfunc_detailed.gFunc),
'-', label='similarities')
ax.plot(lntts, np.abs(gfunc_equivalent.gFunc - gfunc_detailed.gFunc),
'--', label='equivalent')
ax.legend()
ax.set_title(f"Absolute error relative to the 'detailed' solver "
f"(Field of {N_1} by {N_2} boreholes)")
# Adjust to plot window
fig.tight_layout()

# Draw relative error
# Configure figure and axes
fig = gt.utilities._initialize_figure()
ax = fig.add_subplot(111)
# Axis labels
ax.set_xlabel(r'ln$(t/t_s)$')
ax.set_ylabel(r'Relative error')
gt.utilities._format_axes(ax)
# Relative error
gFunc_ref = gfunc_detailed.gFunc # reference g-function
ax.plot(lntts, (gfunc_similarities.gFunc - gFunc_ref) / gFunc_ref,
'-', label='similarities')
ax.plot(lntts, (gfunc_equivalent.gFunc - gFunc_ref) / gFunc_ref,
'--', label='equivalent')
ax.legend()
ax.set_title(f"Relative error relative to the 'detailed' solver "
f"(Field of {N_1} by {N_2} boreholes)")
# Adjust to plot window
fig.tight_layout()
if make_plots:

# -------------------------------------------------------------------------
# Plot results
# -------------------------------------------------------------------------
# Draw g-functions
ax = gfunc_detailed.visualize_g_function().axes[0]
ax.plot(lntts, gfunc_similarities.gFunc, 'bx')
ax.plot(lntts, gfunc_equivalent.gFunc, 'ro')
ax.legend([f'detailed (t = {t_detailed:.3f} sec)',
f'similarities (t = {t_similarities:.3f} sec)',
f'equivalent (t = {t_equivalent:.3f} sec)'])
ax.set_title(f'Field of {N_1} by {N_2} boreholes')
plt.tight_layout()

# Draw absolute error
# Configure figure and axes
fig = gt.utilities._initialize_figure()
ax = fig.add_subplot(111)
# Axis labels
ax.set_xlabel(r'ln$(t/t_s)$')
ax.set_ylabel(r'Absolute error')
gt.utilities._format_axes(ax)
# Absolute error
ax.plot(lntts, np.abs(gfunc_similarities.gFunc - gfunc_detailed.gFunc),
'-', label='similarities')
ax.plot(lntts, np.abs(gfunc_equivalent.gFunc - gfunc_detailed.gFunc),
'--', label='equivalent')
ax.legend()
ax.set_title(f"Absolute error relative to the 'detailed' solver "
f"(Field of {N_1} by {N_2} boreholes)")
# Adjust to plot window
fig.tight_layout()

# Draw relative error
# Configure figure and axes
fig = gt.utilities._initialize_figure()
ax = fig.add_subplot(111)
# Axis labels
ax.set_xlabel(r'ln$(t/t_s)$')
ax.set_ylabel(r'Relative error')
gt.utilities._format_axes(ax)
# Relative error
gFunc_ref = gfunc_detailed.gFunc # reference g-function
ax.plot(lntts, (gfunc_similarities.gFunc - gFunc_ref) / gFunc_ref,
'-', label='similarities')
ax.plot(lntts, (gfunc_equivalent.gFunc - gFunc_ref) / gFunc_ref,
'--', label='equivalent')
ax.legend()
ax.set_title(f"Relative error relative to the 'detailed' solver "
f"(Field of {N_1} by {N_2} boreholes)")
# Adjust to plot window
fig.tight_layout()

# -------------------------------------------------------------------------
# Borehole field (Second bore field)
Expand All @@ -150,52 +156,52 @@ def main():
t3 = perf_counter()
t_equivalent = t3 - t2

# -------------------------------------------------------------------------
# Plot results
# -------------------------------------------------------------------------
# Draw g-functions
ax = gfunc_similarities.visualize_g_function().axes[0]
ax.plot(lntts, gfunc_equivalent.gFunc, 'ro')
ax.legend([f'similarities (t = {t_similarities:.3f} sec)',
f'equivalent (t = {t_equivalent:.3f} sec)'])
ax.set_title(f'Field of {N_1} by {N_2} boreholes')
plt.tight_layout()

# Draw absolute error
# Configure figure and axes
fig = gt.utilities._initialize_figure()
ax = fig.add_subplot(111)
# Axis labels
ax.set_xlabel(r'ln$(t/t_s)$')
ax.set_ylabel(r'Absolute error')
gt.utilities._format_axes(ax)
# Absolute error
ax.plot(lntts, np.abs(gfunc_equivalent.gFunc - gfunc_similarities.gFunc),
label='equivalent')
ax.legend()
ax.set_title(f"Absolute error relative to the 'similarities' solver "
f"(Field of {N_1} by {N_2} boreholes)")
# Adjust to plot window
fig.tight_layout()

# Draw relative error
# Configure figure and axes
fig = gt.utilities._initialize_figure()
ax = fig.add_subplot(111)
# Axis labels
ax.set_xlabel(r'ln$(t/t_s)$')
ax.set_ylabel(r'Relative error')
gt.utilities._format_axes(ax)
# Relative error
ax.plot(lntts, (gfunc_equivalent.gFunc - gfunc_similarities.gFunc) / gfunc_similarities.gFunc,
label='equivalent')
ax.legend()
ax.set_title(f"Relative error relative to the 'similarities' solver "
f"(Field of {N_1} by {N_2} boreholes)")
# Adjust to plot window
fig.tight_layout()

return
if make_plots:

# -------------------------------------------------------------------------
# Plot results
# -------------------------------------------------------------------------
# Draw g-functions
ax = gfunc_similarities.visualize_g_function().axes[0]
ax.plot(lntts, gfunc_equivalent.gFunc, 'ro')
ax.legend([f'similarities (t = {t_similarities:.3f} sec)',
f'equivalent (t = {t_equivalent:.3f} sec)'])
ax.set_title(f'Field of {N_1} by {N_2} boreholes')
plt.tight_layout()

# Draw absolute error
# Configure figure and axes
fig = gt.utilities._initialize_figure()
ax = fig.add_subplot(111)
# Axis labels
ax.set_xlabel(r'ln$(t/t_s)$')
ax.set_ylabel(r'Absolute error')
gt.utilities._format_axes(ax)
# Absolute error
ax.plot(lntts, np.abs(gfunc_equivalent.gFunc - gfunc_similarities.gFunc),
label='equivalent')
ax.legend()
ax.set_title(f"Absolute error relative to the 'similarities' solver "
f"(Field of {N_1} by {N_2} boreholes)")
# Adjust to plot window
fig.tight_layout()

# Draw relative error
# Configure figure and axes
fig = gt.utilities._initialize_figure()
ax = fig.add_subplot(111)
# Axis labels
ax.set_xlabel(r'ln$(t/t_s)$')
ax.set_ylabel(r'Relative error')
gt.utilities._format_axes(ax)
# Relative error
ax.plot(lntts, (gfunc_equivalent.gFunc - gfunc_similarities.gFunc) / gfunc_similarities.gFunc,
label='equivalent')
ax.legend()
ax.set_title(f"Relative error relative to the 'similarities' solver "
f"(Field of {N_1} by {N_2} boreholes)")
# Adjust to plot window
fig.tight_layout()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any chance to compile all the plotting into one place? Maybe a single file/class called plotting.py? There seems to be quite a bit of repetition, but each of these also seems a little different. So no biggie if not. Just a thought.



# Main function
Expand Down
78 changes: 42 additions & 36 deletions examples/comparison_load_aggregation.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
"""
from time import perf_counter

import matplotlib.pyplot as plt
try:
import matplotlib.pyplot as plt
except ModuleNotFoundError:
pass

import numpy as np
from scipy.constants import pi
from scipy.interpolate import interp1d
Expand All @@ -23,7 +27,7 @@
import pygfunction as gt


def main():
def main(make_plots=True):
# -------------------------------------------------------------------------
# Simulation parameters
# -------------------------------------------------------------------------
Expand Down Expand Up @@ -118,40 +122,42 @@ def main():
# Convolution in Fourier domain
T_b_exact = T_g - fftconvolve(dQ, g/(2.0*pi*k_s*H), mode='full')[0:Nt]

# -------------------------------------------------------------------------
# plot results
# -------------------------------------------------------------------------

# Configure figure and axes
fig = gt.utilities._initialize_figure()

ax1 = fig.add_subplot(311)
# Axis labels
ax1.set_xlabel(r'$t$ [hours]')
ax1.set_ylabel(r'$Q_b$ [W]')
gt.utilities._format_axes(ax1)
hours = np.array([(j+1)*dt/3600. for j in range(Nt)])
ax1.plot(hours, Q_b)

ax2 = fig.add_subplot(312)
# Axis labels
ax2.set_xlabel(r'$t$ [hours]')
ax2.set_ylabel(r'$T_b$ [degC]')
gt.utilities._format_axes(ax2)
for T_b_n, line, label in zip(T_b, loadAgg_lines, loadAgg_labels):
ax2.plot(hours, T_b_n, line, label=label)
ax2.plot(hours, T_b_exact, 'k.', label='exact')
ax2.legend()

ax3 = fig.add_subplot(313)
# Axis labels
ax3.set_xlabel(r'$t$ [hours]')
ax3.set_ylabel(r'Error [degC]')
gt.utilities._format_axes(ax3)
for T_b_n, line, label in zip(T_b, loadAgg_lines, loadAgg_labels):
ax3.plot(hours, T_b_n - T_b_exact, line, label=label)
# Adjust to plot window
plt.tight_layout()
if make_plots:

# -------------------------------------------------------------------------
# plot results
# -------------------------------------------------------------------------

# Configure figure and axes
fig = gt.utilities._initialize_figure()

ax1 = fig.add_subplot(311)
# Axis labels
ax1.set_xlabel(r'$t$ [hours]')
ax1.set_ylabel(r'$Q_b$ [W]')
gt.utilities._format_axes(ax1)
hours = np.array([(j+1)*dt/3600. for j in range(Nt)])
ax1.plot(hours, Q_b)

ax2 = fig.add_subplot(312)
# Axis labels
ax2.set_xlabel(r'$t$ [hours]')
ax2.set_ylabel(r'$T_b$ [degC]')
gt.utilities._format_axes(ax2)
for T_b_n, line, label in zip(T_b, loadAgg_lines, loadAgg_labels):
ax2.plot(hours, T_b_n, line, label=label)
ax2.plot(hours, T_b_exact, 'k.', label='exact')
ax2.legend()

ax3 = fig.add_subplot(313)
# Axis labels
ax3.set_xlabel(r'$t$ [hours]')
ax3.set_ylabel(r'Error [degC]')
gt.utilities._format_axes(ax3)
for T_b_n, line, label in zip(T_b, loadAgg_lines, loadAgg_labels):
ax3.plot(hours, T_b_n - T_b_exact, line, label=label)
# Adjust to plot window
plt.tight_layout()

# -------------------------------------------------------------------------
# Print performance metrics
Expand Down
Loading
Loading