def __init__(self, streamlines, streamlineData):
self.streamlineCoordinates = streamlines
self.streamlineData = streamlineData
# Get the streamline parameter in a single array.
self.parameterisedStreamline = self.streamlineCoordinates[:, 0, 3]
# Calculate the velocity along the streamline
streamlineVelocity = np.linalg.norm(self.streamlineData[:, 2:4], axis=1)
# Fit a cubic spline to the streamline velocity
# Need this to calculate velocity derivatives
self.parameterisedVelocity = interpolate.CubicSpline(self.parameterisedStreamline, streamlineVelocity, extrapolate=1)
# Calculate the first derivative
self.parameterisedvelocityPrime = self.parameterisedVelocity.derivative(nu=1)
# Calculate the second derivative
self.parameterisedvelocityDoublePrime = self.parameterisedVelocity.derivative(nu=2)
# Parameterise temperature, pressure and density along the streamline
self.parameterisedM = interpolate.interp1d(self.parameterisedStreamline, self.streamlineData[:, 0], kind='linear', fill_value='extrapolate')
self.parameterisedT = interpolate.interp1d(self.parameterisedStreamline, self.streamlineData[:, 1], kind='linear', fill_value='extrapolate')
self.parameterisedP = interpolate.interp1d(self.parameterisedStreamline, self.streamlineData[:, 5], kind='linear', fill_value='extrapolate')
self.parameterisedRho = interpolate.interp1d(self.parameterisedStreamline, self.streamlineData[:, 6], kind='linear', fill_value='extrapolate')
python类CubicSpline()的实例源码
def branch_current(self):
"""
"branch_current" computes the branch currents
:return:
* self.ib
"""
# check is branch voltages are available
if self.vb is None:
self.branch_voltage()
ibranch = np.zeros_like(self.vb)
cnt_l = 0
cnt_v = 0
for k, (name, val, voltage) in enumerate(zip(self.names, self.values, self.vb)):
if name[0].upper() == 'R':
ibranch[k, ...] = voltage / val
elif name[0].upper() == 'L':
ibranch[k, ...] = self.x[self.node_num + cnt_l, ...]
cnt_l += 1
elif name[0].upper() == 'C':
if self.analysis[0].lower() == '.op': # the current is zero, hence, do nothing
pass
elif self.analysis[0].lower() == '.ac':
f = float(self.analysis[-1])
Xc = -1.0 / (2 * np.pi * f * val)
ibranch[k] = voltage / (Xc * 1j)
elif self.analysis[0].lower() == '.tran':
from scipy.interpolate import CubicSpline
cs = CubicSpline(self.t, voltage)
csd = cs.derivative()
ibranch[k, ...] = val * csd(self.t)
elif name[0].upper() == 'V':
ibranch[k, ...] = self.x[self.node_num + len(self.isort[1]) + cnt_v, ...]
cnt_v += 1
elif name[0].upper() == 'I':
ibranch[k, ...] = val
self.ib = ibranch
def __init__(self, Z_map, extrapolate0=False):
"""
Construct a cubic-spline 3-D E-M transfer function interpolater
using the information in *Z_map* returned from
:func:`parse_xml` as the function samples. If *extrapolate0*,
then 0s are inserted in the transfer function response where
extrapolation would occur (this happens when transfer function
response is requested at frequencies outside the range
provided in the .XML file record).
"""
self.Z_map = Z_map
periods = Z_map.keys()
self.f = NP.array([1/x for x in periods[::-1]])
self.omega = 2 * math.pi * self.f
self.Zxx_interp = CubicSpline(self.omega, [x[0, 0] for x in Z_map.values()[::-1]],
extrapolate=False)
self.Zxy_interp = CubicSpline(self.omega, [x[0, 1] for x in Z_map.values()[::-1]],
extrapolate=False)
self.Zyx_interp = CubicSpline(self.omega, [x[1, 0] for x in Z_map.values()[::-1]],
extrapolate=False)
self.Zyy_interp = CubicSpline(self.omega, [x[1, 1] for x in Z_map.values()[::-1]],
extrapolate=False)
self.key_map = {'xx': self.Zxx_interp,
'xy': self.Zxy_interp,
'yx': self.Zyx_interp,
'yy': self.Zyy_interp}
self.extrapolate0 = extrapolate0
def __init__(self, ss, qs):
# This class receives only normalized path position
assert np.allclose(ss[-1], 1)
self.cspl = CubicSpline(ss, qs)
self.cspld = self.cspl.derivative()
self.cspldd = self.cspld.derivative()
if np.isscalar(qs[0]):
self.dof = 1
else:
self.dof = qs[0].shape[0]
def initialisation(self):
# Get the streamline parameter in a single array.
self.parameterisedStreamline = self.streamlineCoordinates[:, 0, 3]
# Calculate the velocity along the streamline
streamlineVelocity = np.linalg.norm(self.streamlineData[:, 2:4], axis=1)
inverseStreamlineVelocity = 1./streamlineVelocity
# Fit a cubic spline to the streamline velocity
# Need this to calculate velocity derivatives
self.parameterisedVelocity = interpolate.CubicSpline(self.parameterisedStreamline, streamlineVelocity, extrapolate=1)
self.parameterisedInverseVelocity = interpolate.CubicSpline(self.parameterisedStreamline, inverseStreamlineVelocity, extrapolate=1)
# Calculate the first derivative
self.parameterisedInverseVelocityPrime = self.parameterisedInverseVelocity.derivative(nu=1)
# Calculate the second derivative
self.parameterisedInverseVelocityDoublePrime = self.parameterisedInverseVelocity.derivative(nu=2)
return self.parameterisedStreamline, streamlineVelocity, self.parameterisedVelocity, self.parameterisedInverseVelocityPrime, self.parameterisedInverseVelocityDoublePrime
def density_water(temp):
"""Return the density of water at a given temperature.
If given units, the function will automatically convert to Kelvin.
If not given units, the function will assume Kelvin.
"""
ut.check_range([temp, ">0", "Temperature in Kelvin"])
rhointerpolated = interpolate.CubicSpline(WATER_DENSITY_TABLE[0],
WATER_DENSITY_TABLE[1])
return rhointerpolated(temp)