def test_regression_output(self): """ @return: @rtype: """ self.addTypeEqualityFunc(D, self.assertRoundDecimalEqual) assertTupleEqual = self.assertTupleEqual # results = [] for args, expected in self.exp: args.delay = 0 init_args = args.get_args() nsteps, step_size = args.get_step() sim = TempSim(*init_args) if step_size: result = sim.iterate(nsteps, step_size) else: result = sim.iterate(nsteps) # assertEqual(expected, result) for exp_step, r_step in zip(expected, result): assertTupleEqual(exp_step, r_step, init_args) # results.append((args, result)) # use SimArgs as a dummy TempSim to compare. # Fill in missing attrs using result data args.seconds, args.current_temp = result[-1] # noinspection PyTypeChecker self.assertTempSimEqual(args, sim)
def plotpid2(n=15000): import sys try: del sys.modules['scripts.run.temp_sim'] except KeyError: pass from scripts.run.temp_sim import TempSim, PIDController sim = TempSim(D('28.183533'), 19, 0) pid = PIDController(37, 40, D('0.55')) # pid = PIDController(37, 40, 0.5) pid.off_to_auto(sim.current_temp) pv = sim.current_temp #: @type: list[tuple[D]] steps = [('0', '0', '0', '0', '0') for _ in range(11)] for _ in range(n): hd = pid.step_output(pv) t, pv = sim.step_heat(hd) steps.append((t, pv, pid.last_error, pid.accumulated_error, hd)) from officelib.xllib.xlcom import xlBook2 xl, wb = xlBook2('PID.xlsx') ws = wb.Worksheets(2) cells = ws.Cells firstcol = 30 cells.Range(cells(1, firstcol), cells(1, firstcol + 4)).Value = ( "Time", "Temp", "LastErr", "AccumErr", "HeatDuty%") cells.Range(cells(2, firstcol), cells(len(steps) + 1, len(steps[0]) + firstcol - 1)).Value = \ [tuple(map(str, data)) for data in steps]
def test_iterate(self): """ @return: None @rtype: None """ assertEqual = self.assertEqual for args in self.init_args: exp_sim = TempSim(*args, leak_const=0) test_sim = TempSim(*args, leak_const=0) step = exp_sim.step n = 10000 exp_steps = [step() for _ in range(n)] res_steps = test_sim.iterate(n) # unloop assert otherwise hangup caused # by trying to process too long of a diff in # repr string for step_no, (exp_step, res_step) in enumerate(zip(exp_steps, res_steps)): assertEqual(exp_step, res_step, step_no) assertEqual(exp_sim, test_sim) # repeat the same thing to ensure iterate works # on subsequent calls. exp_steps = [step() for _ in range(n)] res_steps = test_sim.iterate(n) for step_no, (exp_step, res_step) in enumerate(zip(exp_steps, res_steps)): assertEqual(exp_step, res_step, step_no) assertEqual(exp_sim, test_sim)
def run_decay_test(n=5400, start_temp=D('37.04'), c=TempSim.DEFAULT_COOL_CONSTANT): """ @return: @rtype: """ sim = TempSim(start_temp, 19, 0, D(c)) steps = sim.iterate(n) return steps
def test_cooling(c=D('-0.000039')): delsim() from scripts.run.temp_sim import TempSim sim = TempSim(42, 15, 0, c) steps = sim.iterate(40000) # _str = str # steps = [(_str(x), _str(y)) for x, y in steps] return steps
def manyboth(): import sys try: del sys.modules['scripts.run.temp_sim'] except KeyError: pass from scripts.run.temp_sim import TempSim from random import uniform Decimal = D rnd_2_const = Decimal('-1e-5') repr_quant = Decimal("1.00000000000") tcs = [] kps = [] cs = [] hd1 = Decimal(6.8) hd2 = Decimal(0) try: while True: rnd = uniform(4, 7.5) cconst = Decimal(rnd) * rnd_2_const sim = TempSim(D('37.114'), 19, hd1, cconst) sim.quietiter(250000) temp1 = sim.current_temp tstart = sim.seconds sim.heat_duty = hd2 bump_steps = sim.iterate(250000) temp2 = sim.current_temp dt = temp2 - temp1 t63 = temp1 + dt * Decimal('0.63') if dt > 0: cmp = t63.__lt__ else: cmp = t63.__gt__ tend = next(i for i, t in bump_steps if cmp(t)) dPV = temp2 - temp1 dCO = hd2 - hd1 tc = tend - tstart kp = dPV / dCO tcs.append(tc) kps.append(kp) cs.append(sim.cool_rate) print("Const:", sim.cool_rate.quantize(repr_quant), "Ti:", int(tc), "Kp:", kp.quantize(repr_quant)) except KeyboardInterrupt: return list(zip(cs, tcs, kps))
def check(): import sys n = 15000 try: del sys.modules['scripts.run.temp_sim'] except KeyError: pass from scripts.run.temp_sim import TempSim, PIDController sim = TempSim(D('28.183533'), 19, 0) pid = PIDController(37, 40, D('0.55')) pid.off_to_auto(sim.current_temp) pv = sim.current_temp #: @type: list[tuple[D]] steps1 = [] ap = steps1.append for _ in range(n): hd = pid.step_output(pv) t, pv = sim.step_heat(hd) ap((t, pv)) sim = TempSim(D('28.183533'), 19, 0) def auto_mode(self, pv): e_t = self.set_point - pv self.accumulated_error = - e_t * self.pgain * self.itime PIDController.off_to_auto = auto_mode pid = PIDController(37, 40, D('0.55')) pid.off_to_auto(sim.current_temp) pv = sim.current_temp #: @type: list[tuple[D]] steps2 = [] ap = steps2.append for _ in range(n): hd = pid.step_output(pv) t, pv = sim.step_heat(hd) ap((t, pv)) from decimal import Context ctxt = Context(prec=3) def eq(one, two, ctxt=ctxt, compare=D.compare): return not (compare(one[0], two[0], ctxt) and compare(one[1], two[1], ctxt)) diff = [(i, one, two) for i, (one, two) in enumerate(zip(steps1, steps2)) if not eq(one, two)] return diff
def plotsim(n=7200, c=D('-0.00004679011328')): try: import sys del sys.modules['scripts.run.temp_sim'] except KeyError: pass from scripts.run.temp_sim import TempSim sim = TempSim(D('37.115'), 19, 0, c) steps = sim.iterate(n) from officelib.xllib.xlcom import xlBook2 xl, wb = xlBook2('PID.xlsx') ws = wb.Worksheets(2) cells = ws.Cells firstcol = 19 cells.Range(cells(2, firstcol), cells(len(steps) + 1, firstcol + 1)).Value = [(str(t), str(v)) for t, v in steps]
def plot_m(h, linest_formula, linest_range, plotcell, xcol, ycol): from scripts.run.temp_sim import TempSim from officelib.xllib.xladdress import cellRangeStr sim = TempSim(28, 19, 50, heat_constant=h) data = sim.iterate(30000) xs, ys = zip(*data) start = next(i for i, pv in enumerate(ys) if pv > 30) end = next((i for i, pv in enumerate(ys, start) if pv > 36), len(ys) - 1) assert ys[start] > 30 assert ys[end] > 36 xldata = [(str(x), str(y)) for x, y in zip(xs[start:end], ys[start:end])] plotxl_by_cell(xldata, plotcell) linest_args = linest_formula % ( cellRangeStr( (2, ycol), (end - start, ycol) ), cellRangeStr( (2, xcol), (end - start, xcol) ) ) # print(linest_args) linest_range.FormulaArray = linest_args
def test_quiet_iter(self): """ @return: @rtype: """ for args in self.init_args: exp_sim = TempSim(*args, leak_const=0) test_sim = TempSim(*args, leak_const=0) step = exp_sim.step n = 10000 for _ in range(n): step() test_sim.quietiter(n) self.assertEqual(exp_sim, test_sim) # test quiet step while we're here step() test_sim.quietstep() self.assertEqual(exp_sim, test_sim) # repeat the same thing to ensure iterate works # on subsequent calls. for _ in range(n): step() test_sim.quietiter(n) self.assertEqual(exp_sim, test_sim) # test quiet step while we're here step() test_sim.quietstep() self.assertEqual(exp_sim, test_sim)