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

_check_coefficients is slow in Network and _BasePipe #118

Closed
MassimoCimmino opened this issue May 30, 2021 · 1 comment · Fixed by #122
Closed

_check_coefficients is slow in Network and _BasePipe #118

MassimoCimmino opened this issue May 30, 2021 · 1 comment · Fixed by #122
Assignees

Comments

@MassimoCimmino
Copy link
Owner

The methods Network._check_coefficents, _BasePipe._check_coefficents, and _BasePipe._check_model_variables are slowed down by the use of numpy.allclose.

Some timing comparisons:

>>> import numpy as np
>>> a = np.random.rand(5); b = np.random.rand(5)
>>> %timeit np.allclose(a, b, rtol=1e-6)
25.8 µs ± 257 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
>>> %timeit np.all(np.abs(a - b) / (np.abs(b) + 1e-10) < 1e-6)
6.4 µs ± 21.8 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

Other options may be available for these checks.

@MassimoCimmino
Copy link
Owner Author

MassimoCimmino commented Jun 4, 2021

Timings for PR #122

After initializing a network:

import matplotlib.pyplot as plt
import numpy as np
from scipy.constants import pi

import pygfunction as gt

# Borehole dimensions
D = 4.0             # Borehole buried depth (m)
H = 150.0           # Borehole length (m)
r_b = 0.075         # Borehole radius (m)

# Bore field geometry (rectangular array)
N_1 = 10            # Number of boreholes in the x-direction (columns)
N_2 = 10            # Number of boreholes in the y-direction (rows)
B = 7.5             # Borehole spacing, in both directions (m)

# The field is a retangular array
boreField = gt.boreholes.rectangle_field(N_1, N_2, B, B, H, D, r_b)

# Pipe dimensions
rp_out = 0.0211     # Pipe outer radius (m)
rp_in = 0.0147      # Pipe inner radius (m)
D_s = 0.052         # Shank spacing (m)
epsilon = 1.0e-6    # Pipe roughness (m)

# Pipe positions
# Single U-tube [(x_in1, y_in1), (x_out1, y_out1)]
pos = [(-D_s, 0.), (0., D_s)]

# Ground properties
k_s = 2.0           # Ground thermal conductivity (W/m.K)
T_g = 10.0          # Undisturbed ground temperature (degC)

# Grout properties
k_g = 1.0           # Grout thermal conductivity (W/m.K)

# Pipe properties
k_p = 0.4           # Pipe thermal conductivity (W/m.K)

# Fluid properties
m_flow_borehole = 0.25      # Total fluid mass flow rate per borehole (kg/s)
m_flow = m_flow_borehole*N_1*N_2    # Total fluid mass flow rate (kg/s)
# The fluid is propylene-glycol (20 %) at 20 degC
fluid = gt.media.Fluid('MPG', 20.)
cp_f = fluid.cp     # Fluid specific isobaric heat capacity (J/kg.K)
den_f = fluid.rho   # Fluid density (kg/m3)
visc_f = fluid.mu   # Fluid dynamic viscosity (kg/m.s)
k_f = fluid.k       # Fluid thermal conductivity (W/m.K)

# Single U-tube, same for all boreholes in the bore field
UTubes = []
for borehole in boreField:
	UTube = gt.pipes.SingleUTube(
		pos, rp_in, rp_out, borehole, k_s, k_g, 1e-6)
	UTubes.append(UTube)
# Build a network object from the list of UTubes
network = gt.networks.Network(boreField, UTubes, m_flow=m_flow, cp=cp_f)

Timing after changes:

>>> np.random.seed(seed=123456)
>>> m_flow_random = (np.random.rand(100) + 1.) * m_flow
>>> %timeit for m_flow_i in m_flow_random: network.get_network_inlet_temperature(1., 1., m_flow_i, cp_f, nSegments=1)
5.07 s ± 13 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
>>> %timeit for m_flow_i in m_flow_random: network._check_coefficients(m_flow_i, cp_f, 1, 1)
803 µs ± 1.35 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

Timing before changes:

>>> np.random.seed(seed=123456)
>>> m_flow_random = (np.random.rand(100) + 1.) * m_flow
>>> %timeit for m_flow_i in m_flow_random: network.get_network_inlet_temperature(1., 1., m_flow_i, cp_f, nSegments=1)
6.72 s ± 10.3 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
>>> %timeit for m_flow_i in m_flow_random: network._check_coefficients(m_flow_i, cp_f, 1, 1)
6.38 ms ± 21.5 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

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

Successfully merging a pull request may close this issue.

1 participant