def test_checkinputs_scatter_2d_reqFOnly(self):
     PTM = np.empty((1, ), np.object)
     PTM[0] = (15, 40)
     with self.assertRaisesRegex(
             ValueError, 'You cannot calculate {\'mus\'} with a spline ' +
             'that does not include concentration. Remove those statevars and all their dependencies, '
             + 'or supply a spline that includes concentration.'):
         eg.evalSolutionGibbsScatter(self.puresubstancespline['sp'],
                                     PTM,
                                     'mus',
                                     MWu=1)
 def test_checkinputs_grid_2d_extrapTOnly(self):
     PTM = np.empty((1, ), np.object)
     PTM[0] = (3000, 600)
     with self.assertRaisesRegex(
             ValueError, 'Dimensions {\'T\'} ' +
             'contain values that fall outside the knot sequence for the given spline, '
             +
             'which will result in extrapolation, which may not produce meaningful values.'
     ):
         eg.evalSolutionGibbsScatter(self.puresubstancespline['sp'],
                                     PTM,
                                     'G',
                                     failOnExtrapolate=True)
 def test_checkinputs_scatter_2d_extrapAllDims(self):
     PTM = np.empty((1, ), np.object)
     PTM[0] = (5000, 600)
     # no order imposed on list of failed dimensions so accommodate either order
     with self.assertRaisesRegex(
             ValueError, 'Dimensions {\'[PT]\', \'[PT]\'} ' +
             'contain values that fall outside the knot sequence for the given spline, '
             +
             'which will result in extrapolation, which may not produce meaningful values.'
     ):
         eg.evalSolutionGibbsScatter(self.puresubstancespline['sp'],
                                     PTM,
                                     'G',
                                     failOnExtrapolate=True)
 def test_evalgibbs_singlesolute_scatter_singlepoint_allmeasures(self):
     pidx = 0
     tidx = 0
     midx = 0
     PTM = np.empty((1, ), np.object)
     PTM[0] = (self.P[pidx], self.T[tidx], self.M[midx])
     out = eg.evalSolutionGibbsScatter(self.spline['sp'],
                                       PTM,
                                       MWv=self.spline['MW'][0],
                                       MWu=self.spline['MW'][1])
     valErrs = ''
     # check all values and output just one error for all of them
     for tdv in vars(out).keys():
         outfield = getattr(out, tdv)
         self.assertEqual(1, outfield.size,
                          'Output for ' + tdv + ' has too many values')
         if tdv not in self.mlout.dtype.fields:
             warnings.warn('Matlab output does not include tdv ' + tdv)
         else:
             mloutfield = self.mlout[tdv][pidx][tidx][midx]
             self.assertEqual(
                 1, outfield.size,
                 tdv + ' output not the same shape as MatLab output')
             if not (np.allclose(
                     outfield, mloutfield, rtol=relTolerance,
                     atol=0)  # check both abs and rel differences
                     and np.allclose(
                         outfield, mloutfield, rtol=0, atol=absTolerance)):
                 absDiffs = np.absolute(outfield - mloutfield)
                 relDiffs = absDiffs / np.absolute(mloutfield)
                 valErrs = valErrs + 'Output for ' + tdv + ' has absolute differences as large as ' + str(
                     np.max(absDiffs)) + \
                           ' and relative differences as large as ' + str(np.max(relDiffs)) + '.\n'
     if valErrs:
         self.fail(valErrs)
 def test_evalgibbs_singlesolute_scatter_singlepoint_Va_no0M(self):
     pidx = 1
     tidx = 2
     midx = 3
     PTM = np.empty((1, ), np.object)
     PTM[0] = (self.P[pidx], self.T[tidx], self.M[midx])
     out = eg.evalSolutionGibbsScatter(self.spline['sp'],
                                       PTM,
                                       'Va',
                                       MWv=self.spline['MW'][0],
                                       MWu=self.spline['MW'][1])
     valErrs = ''
     for tdv in vars(out).keys():
         outfield = getattr(out, tdv)
         self.assertEqual(1, outfield.size,
                          'Output for ' + tdv + ' has too many values')
         if tdv in self.mlout.dtype.fields:
             mloutfield = self.mlout[tdv][pidx][tidx][midx]
             if not (np.allclose(
                     outfield, mloutfield, rtol=relTolerance,
                     atol=0)  # check both abs & rel differences
                     and np.allclose(
                         outfield, mloutfield, rtol=0, atol=absTolerance)):
                 absDiffs = np.absolute(outfield - mloutfield)
                 relDiffs = absDiffs / np.absolute(mloutfield)
                 valErrs = valErrs + 'Output for ' + tdv + ' has absolute differences as large as ' + str(
                     np.max(absDiffs)) + \
                           ' and relative differences as large as ' + str(np.max(relDiffs)) + '.\n'
     if valErrs:
         self.fail(valErrs)
 def test_evalgibbs_singlesolute_scatter_multipoint_allmeasures(self):
     numpts = 4
     ptindices = np.empty((numpts, ), np.object)
     PTM = np.empty((numpts, ), np.object)
     for i in np.arange(
             0, numpts):  # get random data points (pick P/T separately)
         pidx = randint(0, len(self.P) - 1)
         tidx = randint(0, len(self.T) - 1)
         midx = randint(0, len(self.M) - 1)
         ptindices[i] = (pidx, tidx, midx)
         PTM[i] = (self.P[pidx], self.T[tidx], self.M[midx])
     out = eg.evalSolutionGibbsScatter(self.spline['sp'],
                                       PTM,
                                       MWv=self.spline['MW'][0],
                                       MWu=self.spline['MW'][1])
     valErrs = ''
     # check all values and output just one error for all of them
     for tdv in vars(out).keys():
         outfield = getattr(out, tdv)
         self.assertEqual(numpts, outfield.size,
                          'Output for ' + tdv + ' has too many values')
         if tdv not in self.mlout.dtype.fields:
             warnings.warn('Matlab output does not include tdv ' + tdv)
         else:
             # get randomly scattered outputs
             mlout = np.empty((numpts, ), float)
             for i in np.arange(0, numpts):
                 mlout[i] = self.mlout[tdv][ptindices[i]]
             if not (np.allclose(
                     outfield, mlout, rtol=relTolerance,
                     atol=0)  # check both abs and rel differences
                     and np.allclose(
                         outfield, mlout, rtol=0, atol=absTolerance)):
                 absDiffs = np.absolute(outfield - mlout)
                 relDiffs = absDiffs / np.absolute(mlout)
                 valErrs = valErrs + 'Output for ' + tdv + ' has absolute differences as large as ' + str(
                     np.max(absDiffs)) + \
                           ' and relative differences as large as ' + str(np.max(relDiffs)) + '.\n'
     if valErrs:
         self.fail(valErrs)
Example #7
0
from lbftd import statevars, loadGibbs as lg, evalGibbs as eg

# generate n scattered data points
n = 500
minT = 250
maxT = 350
minP = 0
maxP = 1000
toP = lambda r: (maxP - minP) * r + minP
toT = lambda r: (maxT - minT) * r + minT
PT = np.empty(n, np.object)
for i in np.arange(0, n):
    PT[i] = (toP(random()), toT(random()))

water_spline = lg.loadGibbsSpline('water_demo_spline.mat')
tdstate = eg.evalSolutionGibbsScatter(water_spline['sp'], PT, 'rho', 'Cp',
                                      'Kt', 'alpha')

# graph the output
rcParams[
    'axes.labelpad'] = 10  # add some padding to prevents axis labels from covering ticks
fig = plt.figure()
# sorted sets of P and T for creating the meshgrid for plotting
P = [pt[0] for pt in PT]
T = [pt[1] for pt in PT]

rho_ax = fig.add_subplot(221, projection='3d')
rho_ax.set_xlabel('Pressure ($MPa$)')
rho_ax.set_ylabel('Temperature ($K$)')
rho_ax.set_zlabel('Density ($kg\: m^{-3}$)')  # use LaTex coding in labels
rho_surf = rho_ax.scatter(P, T, tdstate.rho)
rho_ax.invert_yaxis()