Esempio n. 1
0
 def test_solver_logging(self):
     Logger.cleanup()
     mock = MockFile()
     Logger.log_file_handles = {'step': mock}
     obj = EquationSolver()
     obj.TraceStep = 2
     obj.RunEquationReduction = False
     # By forcing 't' into the variable list, no automatic creation of time variables
     obj.ParseString("""
          x=t
          z=x+1
          z(0) = 2.
          exogenous
          t=[10.]*20
          MaxTime=3""")
     obj.ExtractVariableList()
     obj.SetInitialConditions()
     self.assertEqual([], mock.buffer)
     obj.SolveStep(1)
     self.assertEqual([], mock.buffer)
     obj.SolveStep(2)
     # I do not care what the contents are; I just want to validate
     # that it triggers
     self.assertTrue(len(mock.buffer) > 0)
     # Reset the buffer
     mock.buffer = []
     obj.SolveStep(3)
     self.assertEqual([], mock.buffer)
Esempio n. 2
0
 def test_SolveEquation(self):
     obj = EquationSolver()
     obj.RunEquationReduction = False
     # By forcing 't' into the variable list, no automatic creation of time variables
     obj.ParseString("""
      x=t
      z=x+1
      z(0) = 2.
      exogenous
      t=[10.]*20
      MaxTime=3""")
     obj.ExtractVariableList()
     obj.SetInitialConditions()
     obj.SolveStep(1)
     obj.SolveStep(2)
     obj.SolveStep(3)
     obj2 = EquationSolver()
     obj2.RunEquationReduction = False
     # By forcing 't' into the variable list, no automatic creation of time variables
     obj2.ParseString("""
      x=t
      z=x+1
      z(0) = 2.
      exogenous
      t=[10.]*20
      MaxTime=3""")
     obj2.SolveEquation()
     self.assertEqual(obj.TimeSeries['x'], obj2.TimeSeries['x'])
Esempio n. 3
0
 def test_SolveStep_1(self):
     obj = EquationSolver()
     obj.RunEquationReduction = False
     # By forcing 't' into the variable list, no automatic creation of time variables
     obj.ParseString("""
      x=t
      z=x+1
      z(0) = 2.
      exogenous
      t=[10.]*20
      MaxTime=3""")
     obj.ExtractVariableList()
     obj.SetInitialConditions()
     obj.SolveStep(1)
     self.assertEqual([10., 10.], obj.TimeSeries['x'])
     # Note that equation does not hold at t=0
     self.assertEqual([2., 11.], obj.TimeSeries['z'])
     obj.SolveStep(2)
     self.assertEqual([10., 10., 10.], obj.TimeSeries['x'])
     # Note that equation does not hold at t=0
     self.assertEqual([2., 11., 11.], obj.TimeSeries['z'])
Esempio n. 4
0
    def test_SolveWithFunction(self):
        obj = EquationSolver()
        obj.RunEquationReduction = False
        # By forcing 't' into the variable list, no automatic creation of time variables
        obj.ParseString("""
          x=t
          z=f(x)
          exogenous
          t=[10.]*20
          MaxTime=3""")
        obj.ExtractVariableList()
        obj.SetInitialConditions()

        def squarer(x):
            return x * x

        obj.AddFunction('f', squarer)
        obj.SolveStep(1)
        self.assertEqual([10., 10.], obj.TimeSeries['x'])
        # Note that equation does not hold at t=0
        self.assertEqual([0., 100.], obj.TimeSeries['z'])
        obj.SolveStep(2)
Esempio n. 5
0
 def test_FailLog0(self):
     obj = EquationSolver()
     obj.RunEquationReduction = False
     # This equation will initially have a divide by zero error; the algorithm will step over that.
     # This is because the initial guess for Z = 0.
     obj.ParseString("""
      x = log10(0)
      exogenous
      t=[10.]*20
      MaxTime=3""")
     obj.ExtractVariableList()
     obj.SetInitialConditions()
     with self.assertRaises(ValueError):
         obj.SolveStep(1)
Esempio n. 6
0
 def test_SolveStep_3(self):
     obj = EquationSolver()
     obj.RunEquationReduction = False
     # By forcing 't' into the variable list, no automatic creation of time variables
     obj.ParseString("""
      x=t
      z=x(k-1)
      exogenous
      t=[10.]*20
      MaxTime=3""")
     obj.ExtractVariableList()
     obj.SetInitialConditions()
     obj.MaxIterations = 0
     with self.assertRaises(ConvergenceError):
         obj.SolveStep(1)
Esempio n. 7
0
 def test_DivideZeroSkip(self):
     obj = EquationSolver()
     obj.RunEquationReduction = False
     # This equation will initially have a divide by zero error; the algorithm will step over that.
     # This is because the initial guess for Z = 0.
     obj.ParseString("""
      z = t
      x=1/z
      exogenous
      t=[0., 10., 10., 10.]
      MaxTime=3""")
     obj.ExtractVariableList()
     obj.SetInitialConditions()
     obj.SolveStep(1)
     obj.SolveStep(2)
     self.assertEqual([0., .1, .1], obj.TimeSeries['x'])
Esempio n. 8
0
 def test_decoration_fail(self):
     obj = EquationSolver()
     obj.RunEquationReduction = True
     # By forcing 't' into the variable list, no automatic creation of time variables
     obj.ParseString("""
        x=2*t
        y=z
        z=x
        exogenous
        t=1.
        MaxTime=3""")
     # Need to break the decoration values;
     obj.Parser.Decoration = [('y', 'x'), ('z', 'foo')]
     obj.ExtractVariableList()
     obj.SetInitialConditions()
     with self.assertRaises(ValueError):
         obj.SolveStep(1)
Esempio n. 9
0
 def test_decoration1(self):
     obj = EquationSolver()
     obj.RunEquationReduction = True
     # By forcing 't' into the variable list, no automatic creation of time variables
     obj.ParseString("""
       x=t
       w=y
       y=z
       z=x
       exogenous
       t=1.
       MaxTime=3""")
     obj.ExtractVariableList()
     obj.SetInitialConditions()
     obj.SolveStep(1)
     obj.SolveStep(2)
     self.assertEqual([1., 1., 1.], obj.TimeSeries['w'])
     self.assertEqual([1., 1., 1.], obj.TimeSeries['y'])
     self.assertEqual([1., 1., 1.], obj.TimeSeries['z'])
Esempio n. 10
0
 def test_FailLog0_maxiter(self):
     obj = EquationSolver()
     obj.RunEquationReduction = False
     # This equation will initially have a divide by zero error; the algorithm will step over that.
     # This is because the initial guess for Z = 0.
     # This test is used to hit the case where we do not converge
     obj.ParseString("""
      z = t
      y = z
      w = y
      x = log10(0)
      exogenous
      t=[10., 11., 12.]
      MaxTime=2""")
     obj.ExtractVariableList()
     obj.SetInitialConditions()
     obj.MaxIterations = 1
     #obj.SolveStep(1)
     with self.assertRaises(ValueError):
         obj.SolveStep(1)