Coverage for gpkit/tests/t_solution_array.py: 100%

92 statements

, created at 2022-07-28 12:35 -0400

1"""Tests for SolutionArray class"""

2import unittest

3import numpy as np

4from gpkit import Variable, VectorVariable, Model, SignomialsEnabled

5import gpkit

6from gpkit.solution_array import var_table

7from gpkit.varkey import VarKey

8from gpkit.small_classes import Strings, Quantity

11class TestSolutionArray(unittest.TestCase):

12 """Unit tests for the SolutionArray class"""

14 def test_call(self):

15 A = Variable('A', '-', 'Test Variable')

16 prob = Model(A, [A >= 1])

17 sol = prob.solve(verbosity=0)

18 self.assertAlmostEqual(sol(A), 1.0, 8)

20 def test_call_units(self):

21 # test from issue541

22 x = Variable("x", 10, "ft")

23 y = Variable("y", "m")

24 m = Model(y, [y >= x])

25 sol = m.solve(verbosity=0)

26 self.assertAlmostEqual(sol("y")/sol("x"), 1.0, 6)

27 self.assertAlmostEqual(sol(x)/sol(y), 1.0, 6)

29 def test_call_vector(self):

30 n = 5

31 x = VectorVariable(n, 'x')

32 prob = Model(sum(x), [x >= 2.5])

33 sol = prob.solve(verbosity=0)

34 solx = sol(x)

35 self.assertEqual(type(solx), Quantity)

36 self.assertEqual(type(sol["variables"][x]), np.ndarray)

37 self.assertEqual(solx.shape, (n,))

38 for i in range(n):

39 self.assertAlmostEqual(solx[i], 2.5, places=4)

41 def test_subinto(self):

42 Nsweep = 20

43 Pvals = np.linspace(13, 24, Nsweep)

44 H_max = Variable("H_max", 10, "m", "Length")

45 A_min = Variable("A_min", 10, "m^2", "Area")

46 P_max = Variable("P", Pvals, "m", "Perimeter")

47 H = Variable("H", "m", "Length")

48 W = Variable("W", "m", "Width")

49 m = Model(12/(W*H**3),

50 [H <= H_max,

51 H*W >= A_min,

52 P_max >= 2*H + 2*W])

53 sol = m.solve(verbosity=0)

54 Psol = sol.subinto(P_max)

55 self.assertEqual(len(Psol), Nsweep)

56 self.assertAlmostEqual(0*gpkit.ureg.m,

57 np.max(np.abs(Pvals*gpkit.ureg.m - Psol)))

58 self.assertAlmostEqual(0*gpkit.ureg.m,

59 np.max(np.abs(Psol - sol(P_max))))

61 def test_table(self):

62 x = Variable('x')

63 gp = Model(x, [x >= 12])

64 sol = gp.solve(verbosity=0)

65 tab = sol.table()

66 self.assertTrue(isinstance(tab, Strings))

68 def test_units_sub(self):

69 # issue 809

70 T = Variable("T", "N", "thrust")

71 Tmin = Variable("T_{min}", "N", "minimum thrust")

72 m = Model(T, [T >= Tmin])

73 tminsub = 1000 * gpkit.ureg.lbf

74 m.substitutions.update({Tmin: tminsub})

75 sol = m.solve(verbosity=0)

76 self.assertAlmostEqual(sol(Tmin), tminsub)

77 self.assertFalse(

78 "1000N" in

79 sol.table().replace(" ", "").replace("[", "").replace("]", ""))

81 def test_key_options(self):

82 # issue 993

83 x = Variable("x")

84 y = Variable("y")

85 with SignomialsEnabled():

86 m = Model(y, [y + 6*x >= 13 + x**2])

87 msol = m.localsolve(verbosity=0)

88 spsol = m.sp().localsolve(verbosity=0) # pylint: disable=no-member

89 gpsol = m.program.gps[-1].solve(verbosity=0)

90 self.assertEqual(msol(x), msol("x"))

91 self.assertEqual(spsol(x), spsol("x"))

92 self.assertEqual(gpsol(x), gpsol("x"))

93 self.assertEqual(msol(x), spsol(x))

94 self.assertEqual(msol(x), gpsol(x))

97class TestResultsTable(unittest.TestCase):

98 """TestCase for var_table()"""

100 def test_nan_printing(self):

101 """Test that solution prints when it contains nans"""

102 x = VarKey(name='x')

103 data = {x: np.array([np.nan, 1, 1, 1, 1])}

104 title = "Free variables"

105 printstr = "\n".join(var_table(data, title))

106 self.assertTrue(" - " in printstr) # nan is printed as " - "

107 self.assertTrue(title in printstr)

109 def test_result_access(self):

110 x = Variable("x")

111 y = Variable("y")

112 with SignomialsEnabled():

113 sig = (y + 6*x >= 13 + x**2)

114 m = Model(y, [sig])

115 sol = m.localsolve(verbosity=0)

116 self.assertTrue(all([isinstance(gp.result.table(), Strings)

117 for gp in m.program.gps]))

118 self.assertAlmostEqual(sol["cost"]/4.0, 1.0, 5)

119 self.assertAlmostEqual(sol("x")/3.0, 1.0, 3)

121TESTS = [TestSolutionArray, TestResultsTable]

123if __name__ == "__main__": # pragma: no cover

124 # pylint: disable=wrong-import-position

125 from gpkit.tests.helpers import run_tests

126 run_tests(TESTS)