diff --git a/Battery.py b/Battery.py index 4ba7d6e..d471055 100644 --- a/Battery.py +++ b/Battery.py @@ -43,9 +43,15 @@ class CombustionChamber: def solve (self, ): """ Iteratively solve for outlet temperature that balance with heat loss to walls """ - f_found = optimize.root_scalar(self.energy_balance_equation, - bracket=[max(self.Twall0, self.Twall1), self.T0]) - self.T1 = f_found.root + + meanTwall = (self.Twall0 + self.Twall1) / 2 + T_low = meanTwall - (self.T0 - meanTwall) + try: + f_found = optimize.root_scalar(self.energy_balance_equation, + bracket=[T_low, self.T0]) + self.T1 = f_found.root + except ValueError: + self.T1 = meanTwall return f_found.root @@ -218,7 +224,20 @@ def wall_solve_wrapper(t_range, wall): class Battery: - def __init__ (self, name, size, heat_program, charge_program, burned_gas_state, hv): + def load_state(self): + with open('gas.history', 'rb') as gas_history_file: + self.gas_t_history = pickle.load(gas_history_file) + + with open('wall.history', 'rb') as wall_history_file: + self.wall_t_history = pickle.load(wall_history_file) + + with open('coke.history', 'rb') as coke_history_file: + self.product = pickle.load(coke_history_file) + + with open('oven.state', 'rb') as coke_state_file: + self.processing = pickle.load(coke_state_file) + + def __init__ (self, name, size, heat_program, charge_program, burned_gas_state, hv, init_from_file=False): self.name = name # Battery name self.size = size # Size of battery, number of ovens self.heat_program = heat_program # Heat program or schedule object @@ -273,20 +292,46 @@ class Battery: start_indices = [1, 3, 5, 2, 4] self.oven_idx_order = np.concatenate([np.array(range(i0 - 1, self.size, 5)) for i0 in start_indices]) - # 정상 상태 만들기: 모든 문에 n_cycle 회 장입 - n_cycle = 3 # 모든 문 장입 반복 횟수 - period_over_dt = 6. # period/dt, 장입 간격 / 초기화 time step 크기 - normal_period = self.charge_program.period(-1) # 감산 전 장입 간격 (주기) - dt = normal_period / period_over_dt # Simulation Time Step + if init_from_file: + print("Initializaton from file") + + self.load_state() + latest_chamber = self.gas_t_history[-1] + latest_wall = self.wall_t_history[-1] + + # Last Record Time + self.t = latest_chamber[0] + self.t_last = self.processing[-1].t_charge + + # Recover Chamber State + for chmbr, T1 in zip(self.chambers, latest_chamber[1]): + chmbr.T1 = T1 + (wl.T_chamber, wl.T_internal.data, wl.T_oven, wu.T_oven, wu.T_internal.data, wu.T_chamber) + + # Recover Wall State + for wl, wu, wallT in zip(self.walls_0, self.walls_1, latest_wall[1]): + wl.T_chamber, wl.T_internal.data, wl.T_oven, wu.T_oven, wu.T_internal.data, wu.T_chamber = wallT + + # Recover Oven State + for coal in self.processing: + self.ovens[coal.idx_oven].content = coal + + else: + print("Initializaton Start") + # 정상 상태 만들기: 모든 문에 n_cycle 회 장입 + n_cycle = 3 # 모든 문 장입 반복 횟수 + period_over_dt = 6. # period/dt, 장입 간격 / 초기화 time step 크기 + normal_period = self.charge_program.period(-1) # 감산 전 장입 간격 (주기) + dt = normal_period / period_over_dt # Simulation Time Step + + self.t = - normal_period * self.size * n_cycle # 정상상태 생성 모사 시간 = 장입 간격 * 총 장입 횟수 + self.t_last = self.t # 마지막 장입을 정상상태 시뮬레이션 시작 시각으로 설정 - self.t = - normal_period * self.size * n_cycle # 정상상태 생성 모사 시간 = 장입 간격 * 총 장입 횟수 - self.t_last = self.t # 마지막 장입을 정상상태 시뮬레이션 시작 시각으로 설정 - - # initialization time loop - for i in range(int(np.ceil(self.size * period_over_dt * n_cycle))): - # for i in range(3): - """ Fill battety with normal charge rate """ - self.update(dt) # Time adavancement + # initialization time loop + for i in range(int(np.ceil(self.size * period_over_dt * n_cycle))): + # for i in range(3): + """ Fill battety with normal charge rate """ + self.update(dt) # Time adavancement def mdot (self, t): return self.mdot0 * self.heat_program.f(t) / self.normal_heat @@ -338,7 +383,7 @@ class Battery: wall_lower.update_bc(T_oven=T_oven) wall_upper.update_bc(T_oven=T_oven) - with Pool(16) as pool: + with Pool(32) as pool: wall_sln = pool.starmap(wall_solve_wrapper, [((dt*60*60), w) for w in self.walls_0+self.walls_1]) # wall_lower.solve(dt * 60 * 60) # convert hours to seconds @@ -550,19 +595,21 @@ if __name__ == "__main__": charging_plan = ChargeSchedule( 81, 9, 9, 1e-12, 24+13, 3, 1e-12 ) n_doors = 66 - bat3A = Battery("3A", n_doors, heating_plan, charging_plan, gas_in_state, hv) + load_state = False + bat3A = Battery("3A", n_doors, heating_plan, charging_plan, gas_in_state, hv, init_from_file=load_state) - with open('gas.history', 'wb') as gas_history_file: - pickle.dump(bat3A.gas_t_history, gas_history_file) + if not load_state: + with open('gas.history', 'wb') as gas_history_file: + pickle.dump(bat3A.gas_t_history, gas_history_file) - with open('wall.history', 'wb') as wall_history_file: - pickle.dump(bat3A.wall_t_history, wall_history_file) + with open('wall.history', 'wb') as wall_history_file: + pickle.dump(bat3A.wall_t_history, wall_history_file) - with open('coke.history', 'wb') as wall_history_file: - pickle.dump(bat3A.product, wall_history_file) + with open('coke.history', 'wb') as wall_history_file: + pickle.dump(bat3A.product, wall_history_file) - with open('oven.state', 'wb') as wall_history_file: - pickle.dump(bat3A.processing, wall_history_file) + with open('oven.state', 'wb') as wall_history_file: + pickle.dump(bat3A.processing, wall_history_file) dt = 5. * 1./60. # 5 min for it in range (int(60/dt)): # 시뮬레이션 시간 도메인 = 60시간