def main(): """ Make the flowsheet object and solve """ ss_flowsheet = ss_sim.main() flowsheet = Flowsheet(name='MB_Model') # fill in values of IC parameters from steady state solve setICs(flowsheet, ss_flowsheet) # Fix variables setInputs(flowsheet) # Initialize at steady state initialize_ss(flowsheet, ss_flowsheet) mb = flowsheet.MB_fuel write_differential_equations(flowsheet) # Then perturb solid_x_ptb = {'Fe2O3': 0.25, 'Fe3O4': 0.01, 'Al2O3': 0.74} gas_y_ptb = {'CO2': 0.03999, 'H2O': 0.00001, 'CH4': 0.96} #perturbInputs(flowsheet,0,Solid_M=691.4,Solid_T=1283,Solid_x=solid_x_ptb, # Gas_F=150,Gas_T=350,Gas_y=gas_y_ptb) for t in mb.t: perturbInputs(flowsheet, t, Solid_M=691.4) # should put this in a dedicated ~intialize~ function # that also intelligently initializes the model after perturbation mb.eq_d4.deactivate() mb.eq_d5.deactivate() mb.eq_d8.deactivate() mb.eq_d9.deactivate() mb.eq_d10.deactivate() mb.eq_g7.deactivate() mb.eq_g8.deactivate() mb.eq_g10.deactivate() mb.eq_g11.deactivate() mb.eq_g12.deactivate() mb.eq_g13.deactivate() mb.eq_g14.deactivate() mb.eq_g4.deactivate() mb.eq_g5.deactivate() mb.eq_g2.deactivate() mb.Tg_GW.fix(0.0) mb.Tw_GW.fix(0.0) mb.Tg_refractory.fix(0.0) mb.Tw_Wamb.fix() mb.Tw.fix() mb.Nuw.fix() mb.Nu_ext.fix() mb.hw.fix() mb.hext.fix() mb.hext2.fix() mb.U.fix() mb.Uw.fix() mb.Pr_ext.fix() mb.Ra.fix() mb.Re.fix() ### # other tentatively unused variables: mb.mFe_mAl.fix(0.0) mb.Solid_Out_M_Comp.fix() mb.eq_c5.deactivate() # Create a solver tol = 1e-8 opt = SolverFactory('ipopt') opt.options = { 'tol': tol, 'linear_solver': 'ma57', 'bound_push': 1e-8, 'max_cpu_time': 600, 'print_level': 5 } #'halt_on_ampl_error': 'yes'} # initialized at steady state, works regardless: flowsheet.strip_bounds() #for z in mb.z: # for t in mb.t: # mb.Cg[z,'CH4',t].setlb(1e-8) for t in mb.t: alg_update(flowsheet, t) update_time_derivatives(flowsheet, t) print_violated_constraints(flowsheet, tol) nlp_ss = PyomoNLP(ss_flowsheet) x_ss = get_vector_from_flowsheet(nlp_ss, ss_flowsheet) jac_ss = nlp_ss.jacobian_g(x_ss) print('calculating steady state condition number...') ss_condition = np.linalg.cond(jac_ss.toarray()) print('steady state condition number: ', ss_condition) fig1, ax1 = plt.subplots() ax1.jac_ss = plt.spy(jac_ss) ax1.set_facecolor('none') fig1.savefig('jac_ss.png', facecolor='none', edgecolor='none') #'#f2f2f2',edgecolor='none') nlp = PyomoNLP(flowsheet) v_order = nlp.variable_order() c_order = nlp.constraint_order() x = get_vector_from_flowsheet(nlp, flowsheet) lam = nlp.create_vector_y() jac_c = nlp.jacobian_g(x) hess_lag = nlp.hessian_lag(x, lam) kkt = BlockSymMatrix(2) kkt[0, 0] = hess_lag kkt[1, 0] = jac_c fig2, ax2 = plt.subplots() ax2.jac_c = plt.spy(jac_c) ax2.set_facecolor('none') fig2.savefig('jac_c.png', facecolor='none', edgecolor='none') #MA27 = hsl.MA27_LinearSolver() #jac_row_fortran = np.zeros(jac_c.nnz,dtype=np.intc) #jac_col_fortran = np.zeros(jac_c.nnz,dtype=np.intc) #values = jac_c.data #for i in range(0,jac_c.nnz): # jac_row_fortran[i] = int(jac_c.row[i] + 1) # jac_col_fortran[i] = int(jac_c.col[i] + 1) #print('Doing symbolic factorization...') #MA27.DoSymbolicFactorization(nlp.nx,jac_row_fortran,jac_col_fortran) #print(jac_row_fortran) #print(jac_col_fortran) #print('Doing numeric factorization...') #num_status = MA27.DoNumericFactorization(nlp.nx,values) #print('Status: ',num_status) #jac_indices = range(0,jac_c.nnz) #for i in range(0,jac_c.nnz): # if np.abs(values[i]) <= 1e-6: # print('%0.2e'%values[i],str(jac_indices[i])+'-th nonzero.',jac_c.row[i],jac_c.col[i], # c_order[jac_c.row[i]],v_order[jac_c.col[i]]) #plot_switch = 0 #if plot_switch == 1: # fig,ax = plt.subplots() # jac_value_plot = ax.bar(jac_indices,values) # ax.set_yscale('log') # fig.savefig('plots/jac_values.png') print('calculating condition number...') condition = np.linalg.cond(jac_c.toarray()) print('condition number: ', condition) #mb.input_objective = Objective(expr=sum((mb.Solid_In_M[t] -601.4)**2 for t in mb.t)) flowsheet.write('fs_dyn.nl') #with open('factorized_fs.txt','w') as f: # flowsheet.display(ostream=f) return flowsheet
def main(): """ Make the flowsheet object and solve """ ss_flowsheet = ss_sim.main() flowsheet = Flowsheet(name='MB_Model') # fill in values of IC parameters from steady state solve setICs(flowsheet, ss_flowsheet) # Fix variables setInputs(flowsheet) # Initialize at steady state initialize_ss(flowsheet, ss_flowsheet) mb = flowsheet.MB_fuel # Then perturb # ^ that function should go in this file, probably # input: dict mapping state names (strings) to new values # this seems like as much work as doing the perturbation... # maybe just make a self contained function #input_perturbation = { ('Solid_In_x',mb.t) : { ['Fe2O3'] : 0.25 }, # ('Solid_In_x',mb.t) : { ['Al2O3'] : 0.75 } } solid_x_ptb = {'Fe2O3': 0.25, 'Fe3O4': 0.01, 'Al2O3': 0.74} gas_y_ptb = {'CO2': 0.03999, 'H2O': 0.00001, 'CH4': 0.96} perturbInputs(flowsheet, 0, Solid_M=691.4, Solid_T=1283, Solid_x=solid_x_ptb, Gas_F=150, Gas_T=350, Gas_y=gas_y_ptb) #perturbInputs(flowsheet,0,Solid_M=691.4,Solid_T=1283,Solid_x=solid_x_ptb) #perturbInputs(flowsheet,0,Solid_M=691.4,Solid_T=1283,Solid_x=solid_x_ptb, # Gas_y=gas_y_ptb) # perturb states # should put this in a dedicated ~intialize~ function # that also intelligently initializes the model after perturbation mb.eq_d4.deactivate() mb.eq_d5.deactivate() mb.eq_d8.deactivate() mb.eq_d9.deactivate() mb.eq_d10.deactivate() mb.eq_g7.deactivate() mb.eq_g8.deactivate() mb.eq_g10.deactivate() mb.eq_g11.deactivate() mb.eq_g12.deactivate() mb.eq_g13.deactivate() mb.eq_g14.deactivate() mb.eq_g4.deactivate() mb.eq_g5.deactivate() mb.eq_g2.deactivate() mb.Tg_GW.fix(0.0) mb.Tw_GW.fix(0.0) mb.Tg_refractory.fix(0.0) mb.Tw_Wamb.fix() mb.Tw.fix() mb.Nuw.fix() mb.Nu_ext.fix() mb.hw.fix() mb.hext.fix() mb.hext2.fix() mb.U.fix() mb.Uw.fix() mb.Pr_ext.fix() mb.Ra.fix() mb.Re.fix() ### # other tentatively unused variables: mb.mFe_mAl.fix(0.0) mb.Solid_Out_M_Comp.fix() ''' ts = time.time() ''' print('\nBefore update:') print_violated_constraints(flowsheet) with open('pre_alg_update.txt', 'w') as f: flowsheet.display(ostream=f) ### ## shouldn't do this because it will be applied for every point in time... ## should do this before performing an algebraic update for all time... #dif_var_list = [ m.Cg.name, m.q.name, m.Tg.name, m.Ts.name ] #for var in m.component_objects(Var): # # fixed = True # for index in var: # # if any of the coordinates of the variable are unfixed, then consider the variable unfixed # # otherwise it is fixed, and disregard it as a possible algebraic variable # if var[index].fixed == False: # fixed = False # break # stale = True # for index in var: # if var[index].stale == False: # stale = False # break # # is_time_derivative = False # if isinstance(var,DerivativeVar): # if m.t in var.get_continuousset_list(): # is_time_derivative = True # if fixed == False and (var.name not in dif_var_list) and is_time_derivative == False: # if isinstance(var,SimpleVar): # var.set_value(None) # else: # for index in var: # var[index].set_value(None) # print(var.local_name) ### alg_update(flowsheet, 0) update_time_derivatives(flowsheet, 0) print('\nAfter update:') print_violated_constraints(flowsheet) with open('post_alg_update.txt', 'w') as f: flowsheet.display(ostream=f) ''' print("\n") print("----------------------------------------------------------") print('Total simulation time: ', value(time.time() - ts), " s") print("----------------------------------------------------------") # Print some variables print_summary_fuel_reactor(flowsheet) # Plot some variables #results_plot_fuel_reactor(flowsheet) #with open('m_fs.txt','w') as f: # flowsheet.display(ostream=f) # Store the flowsheet ''' return flowsheet
def main(): """ Make the flowsheet object and solve """ ss_flowsheet = ss_sim.main() flowsheet = Flowsheet(name='MB_Model') # fill in values of IC parameters from steady state solve setICs(flowsheet, ss_flowsheet) # Fix variables setInputs(flowsheet) # Initialize at steady state initialize_ss(flowsheet, ss_flowsheet) mb = flowsheet.MB_fuel # Then perturb solid_x_ptb = {'Fe2O3': 0.25, 'Fe3O4': 0.01, 'Al2O3': 0.74} gas_y_ptb = {'CO2': 0.03999, 'H2O': 0.00001, 'CH4': 0.96} #perturbInputs(flowsheet,0,Solid_M=691.4,Solid_T=1283,Solid_x=solid_x_ptb, # Gas_F=150,Gas_T=350,Gas_y=gas_y_ptb) for t in mb.t: perturbInputs(flowsheet, t, Solid_M=691.4) # should put this in a dedicated ~intialize~ function # that also intelligently initializes the model after perturbation mb.eq_d4.deactivate() mb.eq_d5.deactivate() mb.eq_d8.deactivate() mb.eq_d9.deactivate() mb.eq_d10.deactivate() mb.eq_g7.deactivate() mb.eq_g8.deactivate() mb.eq_g10.deactivate() mb.eq_g11.deactivate() mb.eq_g12.deactivate() mb.eq_g13.deactivate() mb.eq_g14.deactivate() mb.eq_g4.deactivate() mb.eq_g5.deactivate() mb.eq_g2.deactivate() mb.Tg_GW.fix(0.0) mb.Tw_GW.fix(0.0) mb.Tg_refractory.fix(0.0) mb.Tw_Wamb.fix() mb.Tw.fix() mb.Nuw.fix() mb.Nu_ext.fix() mb.hw.fix() mb.hext.fix() mb.hext2.fix() mb.U.fix() mb.Uw.fix() mb.Pr_ext.fix() mb.Ra.fix() mb.Re.fix() ### # other tentatively unused variables: mb.mFe_mAl.fix(0.0) mb.Solid_Out_M_Comp.fix() for z in mb.z: mb.dldz[z].fix() mb.l[z].fix() mb.dldz_disc_eq.deactivate() mb.eq_a1.deactivate() mb.eq_c5.deactivate() # initialized at steady state, works regardless: flowsheet.strip_bounds() #for z in mb.z: # for t in mb.t: # mb.Cg[z,'CH4',t].setlb(1e-8) diff_vars = [mb.Cg.name, mb.q.name, mb.Tg.name, mb.Ts.name] time_derivatives = [ mb.dCgdt.name, mb.dqdt.name, mb.dTgdt.name, mb.dTsdt.name ] inputs = [ mb.Gas_In_F.name, mb.Gas_In_y.name, mb.Solid_In_M.name, mb.Solid_In_x.name, mb.Gas_In_Tg.name, mb.Solid_In_Ts.name, mb.Gas_In_P.name ] disturbances = [] # ^ appears there are no disturbance variables (Ta is not a variable) geometry = [mb.l.name, mb.L.name, mb.Dr.name, mb.A_bed.name, mb.eps.name] # ^ will be fixed; really should not even be variables (should be parameters), but w/e # (are these the only variables that aren't indexed by time?) alg_vars = find_algebraic_variables(flowsheet, diff_vars, time_derivatives, inputs, disturbances, geometry) for var in alg_vars: print(var.name) for t in mb.t: fix_inlet_conditions(flowsheet, t) fix_z0(flowsheet) alg_var_data = get_alg_var_data(alg_vars) print('Generating map') avc_map = make_alg_var_const_map(flowsheet) print('\nNot in avc_map:') for var in alg_var_data: if var.name not in avc_map: print(var) print('- - -') for var in alg_var_data: var.fix() if not avc_map[var.name].active: print(avc_map[var.name].name, 'already deactivated') avc_map[var.name].deactivate() tol = 1e-8 opt = SolverFactory('ipopt') opt.options = { 'tol': tol, 'linear_solver': 'ma57', 'bound_push': 1e-8, 'max_cpu_time': 600, 'print_level': 5, 'output_file': 'ipopt_out.txt', 'linear_system_scaling': 'mc19', 'linear_scaling_on_demand': 'no', 'halt_on_ampl_error': 'yes' } flowsheet.write('sm_init.nl') with open('sm_init.txt', 'w') as f: flowsheet.display(ostream=f) print_violated_constraints(flowsheet) results = opt.solve(flowsheet, tee=True, symbolic_solver_labels=False, keepfiles=False) ### unfix z-derivatives ### for index in mb.dG_fluxdz_disc_eq: mb.dG_fluxdz[index].unfix() mb.dG_fluxdz_disc_eq[index].activate() for index in mb.dS_fluxdz_disc_eq: mb.dS_fluxdz[index].unfix() mb.dS_fluxdz_disc_eq[index].activate() for index in mb.dGh_fluxdz_disc_eq: mb.dGh_fluxdz[index].unfix() mb.dGh_fluxdz_disc_eq[index].activate() for index in mb.dSh_fluxdz_disc_eq: mb.dSh_fluxdz[index].unfix() mb.dSh_fluxdz_disc_eq[index].activate() for index in mb.S_flux: mb.S_flux[index].unfix() avc_map[mb.S_flux[index].name].activate() print_violated_constraints(flowsheet) results = opt.solve(flowsheet, tee=True, symbolic_solver_labels=False, keepfiles=False) # ^ this converges (quickly) to an infeasible point print_violated_constraints(flowsheet) with open('sm_sol.txt', 'w') as f: flowsheet.display(ostream=f) return flowsheet
def main(): """ Make the flowsheet object and solve """ ss_flowsheet = ss_sim.main() flowsheet = Flowsheet(name='MB_Model') # fill in values of IC parameters from steady state solve setICs(flowsheet,ss_flowsheet) # Fix variables setInputs(flowsheet) # Initialize at steady state initialize_ss(flowsheet,ss_flowsheet) mb = flowsheet.MB_fuel #write_differential_equations(flowsheet) # Then perturb ptb = {} solid_x_ptb = {'Fe2O3':0.25, 'Fe3O4':0.01, 'Al2O3':0.74} gas_y_ptb = {'CO2':0.04999, 'H2O':0.00001, 'CH4':0.95} #perturbInputs(flowsheet,0,Solid_M=691.4,Solid_T=1283.15,Solid_x=solid_x_ptb, # Gas_F=150,Gas_T=350,Gas_y=gas_y_ptb) for t in mb.t: ptb_inputs(flowsheet, t, Gas_T=350) # should put this in a dedicated ~intialize~ function # that also intelligently initializes the model after perturbation mb.eq_d4.deactivate() mb.eq_d5.deactivate() mb.eq_d8.deactivate() mb.eq_d9.deactivate() mb.eq_d10.deactivate() mb.eq_g7.deactivate() mb.eq_g8.deactivate() mb.eq_g10.deactivate() mb.eq_g11.deactivate() mb.eq_g12.deactivate() mb.eq_g13.deactivate() mb.eq_g14.deactivate() mb.eq_g4.deactivate() mb.eq_g5.deactivate() mb.eq_g2.deactivate() mb.Tg_GW.fix(0.0) mb.Tw_GW.fix(0.0) mb.Tg_refractory.fix(0.0) mb.Tw_Wamb.fix() mb.Tw.fix() mb.Nuw.fix() mb.Nu_ext.fix() mb.hw.fix() mb.hext.fix() mb.hext2.fix() mb.U.fix() mb.Uw.fix() mb.Pr_ext.fix() mb.Ra.fix() mb.Re.fix() ### # other tentatively unused variables: mb.mFe_mAl.fix(0.0) mb.Solid_Out_M_Comp.fix() # choose how to calculate certain algebraic variables: mb.eq_c5.deactivate() with open('dyn_fs_init.txt','w') as f: flowsheet.display(ostream=f) # Create a solver tol = 1e-8 opt = SolverFactory('ipopt') opt.options = {'tol': tol, 'linear_solver' : 'ma57', 'bound_push': 1e-8, 'max_cpu_time': 600, 'print_level': 5, 'output_file': 'ipopt_out.txt', 'linear_system_scaling': 'mc19', 'linear_scaling_on_demand' : 'no', 'halt_on_ampl_error': 'yes'} flowsheet.write('fs_dyn.nl') # initialized at steady state, works regardless: flowsheet.strip_bounds() print_violated_constraints(flowsheet,tol) fs_list = clc_integrate(mb) for t in fs_list: load_fe(fs_list[t], flowsheet, t) #for z in mb.z: # for t in mb.t: # mb.Cg[z,'CH4',t].setlb(1e-8) #for t in mb.t: # alg_update(flowsheet,t) # update_time_derivatives(flowsheet,t) with open('dyn_fs_init.txt','w') as f: flowsheet.display(ostream=f) print_violated_constraints(flowsheet,tol) #mb.input_objective = Objective(expr=sum((mb.Solid_In_M[t] -601.4)**2 for t in mb.t)) #mb.cont_param.set_value(0) #mb.dCgdt_disc_eq.deactivate() #for index in mb.dCgdt: mb.dCgdt[index].fix(0) #mb.dqdt_disc_eq.deactivate() #for index in mb.dqdt: mb.dqdt[index].fix(0) #mb.dTsdt_disc_eq.deactivate() #for index in mb.dTsdt: mb.dTsdt[index].fix(0) #mb.dTgdt_disc_eq.deactivate() #for index in mb.dTgdt: mb.dTgdt[index].fix(0) #mb.eq_h1.deactivate() #mb.eq_h2.deactivate() #mb.eq_h3.deactivate() #mb.eq_h4.deactivate() #flowsheet.write('fs_dyn.nl') #results = opt.solve(flowsheet,tee=True,symbolic_solver_labels=False, # keepfiles=False) #mb.dCgdt_disc_eq.activate() #for index in mb.dCgdt: mb.dCgdt[index].unfix() #mb.dqdt_disc_eq.activate() #for index in mb.dqdt: mb.dqdt[index].unfix() #mb.dTsdt_disc_eq.activate() #for index in mb.dTsdt: mb.dTsdt[index].unfix() #mb.dTgdt_disc_eq.activate() #for index in mb.dTsdt: mb.dTgdt[index].unfix() #mb.eq_h1.activate() #mb.eq_h2.activate() #mb.eq_h3.activate() #mb.eq_h4.activate() #print_violated_constraints(flowsheet,t) #print('\n-----------------') #print('#\epsilon = 1e-5#') #print('-----------------\n') #mb.cont_param.set_value(1e-5) #results = opt.solve(flowsheet,tee=True,symbolic_solver_labels=False, # keepfiles=False) #print('\n-----------------') #print('#\epsilon = 1e-4#') #print('-----------------\n') #mb.cont_param.set_value(1e-4) #results = opt.solve(flowsheet,tee=True,symbolic_solver_labels=False, # keepfiles=False) #print('\n-----------------') #print('#\epsilon = 1e-3#') #print('-----------------\n') #mb.cont_param.set_value(1e-3) #results = opt.solve(flowsheet,tee=True,symbolic_solver_labels=False, # keepfiles=False) #print('\n-----------------') #print('#\epsilon = 1e-2#') #print('-----------------\n') #mb.cont_param.set_value(1e-2) #results = opt.solve(flowsheet,tee=True,symbolic_solver_labels=False, # keepfiles=False) #results = opt.solve(flowsheet,tee=True,symbolic_solver_labels=False, # keepfiles=False) #print('\n-----------------') #print('#\epsilon = 2e-2#') #print('-----------------\n') #mb.cont_param.set_value(2e-2) #results = opt.solve(flowsheet,tee=True,symbolic_solver_labels=False, # keepfiles=False) #results = opt.solve(flowsheet,tee=True,symbolic_solver_labels=False, # keepfiles=False) #print('\n-----------------') #print('#\epsilon = 5e-2#') #print('-----------------\n') #mb.cont_param.set_value(5e-2) #results = opt.solve(flowsheet,tee=True,symbolic_solver_labels=False, # keepfiles=False) #results = opt.solve(flowsheet,tee=True,symbolic_solver_labels=False, # keepfiles=False) #print('\n-----------------') #print('#\epsilon = 8e-2#') #print('-----------------\n') #mb.cont_param.set_value(5e-2) #results = opt.solve(flowsheet,tee=True,symbolic_solver_labels=False, # keepfiles=False) #results = opt.solve(flowsheet,tee=True,symbolic_solver_labels=False, # keepfiles=False) #print('\n-----------------') #print('#\epsilon = 1e-1#') #print('-----------------\n') #mb.cont_param.set_value(1e-1) #results = opt.solve(flowsheet,tee=True,symbolic_solver_labels=False, # keepfiles=False) #results = opt.solve(flowsheet,tee=True,symbolic_solver_labels=False, # keepfiles=False) #print('\n-----------------') #print('#\epsilon = 2e-1#') #print('-----------------\n') #mb.cont_param.set_value(2e-1) #results = opt.solve(flowsheet,tee=True,symbolic_solver_labels=False, # keepfiles=False) #results = opt.solve(flowsheet,tee=True,symbolic_solver_labels=False, # keepfiles=False) #print('\n-----------------') #print('#\epsilon = 4e-1#') #print('-----------------\n') #mb.cont_param.set_value(4e-1) #results = opt.solve(flowsheet,tee=True,symbolic_solver_labels=False, # keepfiles=False) #results = opt.solve(flowsheet,tee=True,symbolic_solver_labels=False, # keepfiles=False) # #print('\n-----------------') #print('#\epsilon = 5e-1#') #print('-----------------\n') #mb.cont_param.set_value(5e-1) #results = opt.solve(flowsheet,tee=True,symbolic_solver_labels=False, # keepfiles=False) #results = opt.solve(flowsheet,tee=True,symbolic_solver_labels=False, # keepfiles=False) #print('\n-----------------') #print('#\epsilon = 6e-1#') #print('-----------------\n') #mb.cont_param.set_value(6e-1) #results = opt.solve(flowsheet,tee=True,symbolic_solver_labels=False, # keepfiles=False) #results = opt.solve(flowsheet,tee=True,symbolic_solver_labels=False, # keepfiles=False) #print('\n-----------------') #print('#\epsilon = 7e-1#') #print('-----------------\n') #mb.cont_param.set_value(7e-1) #results = opt.solve(flowsheet,tee=True,symbolic_solver_labels=False, # keepfiles=False) #results = opt.solve(flowsheet,tee=True,symbolic_solver_labels=False, # keepfiles=False) #print('\n-----------------') #print('#\epsilon = 7.5e-1#') #print('-----------------\n') #mb.cont_param.set_value(7.5e-1) #results = opt.solve(flowsheet,tee=True,symbolic_solver_labels=False, # keepfiles=False) #results = opt.solve(flowsheet,tee=True,symbolic_solver_labels=False, # keepfiles=False) #print('\n-----------------') #print('#\epsilon = 8e-1#') #print('-----------------\n') #mb.cont_param.set_value(8e-1) #results = opt.solve(flowsheet,tee=True,symbolic_solver_labels=False, # keepfiles=False) #results = opt.solve(flowsheet,tee=True,symbolic_solver_labels=False, # keepfiles=False) # #print('\n-----------------') #print('#\epsilon = 8.5e-1#') #print('-----------------\n') #mb.cont_param.set_value(8.5e-1) #results = opt.solve(flowsheet,tee=True,symbolic_solver_labels=False, # keepfiles=False) #results = opt.solve(flowsheet,tee=True,symbolic_solver_labels=False, # keepfiles=False) #print('\n-----------------') #print('#\epsilon = 8.8e-1#') #print('-----------------\n') #mb.cont_param.set_value(8.8e-1) #results = opt.solve(flowsheet,tee=True,symbolic_solver_labels=False, # keepfiles=False) #results = opt.solve(flowsheet,tee=True,symbolic_solver_labels=False, # keepfiles=False) #print('\n-----------------') #print('#\epsilon = 9e-1#') #print('-----------------\n') #mb.cont_param.set_value(9e-1) #results = opt.solve(flowsheet,tee=True,symbolic_solver_labels=False, # keepfiles=False) #results = opt.solve(flowsheet,tee=True,symbolic_solver_labels=False, # keepfiles=False) #print('\n-----------------') #print('#\epsilon = 9.1e-1#') #print('-----------------\n') #mb.cont_param.set_value(9.1e-1) #results = opt.solve(flowsheet,tee=True,symbolic_solver_labels=False, # keepfiles=False) #results = opt.solve(flowsheet,tee=True,symbolic_solver_labels=False, # keepfiles=False) #print('\n-----------------') #print('#\epsilon = 9.2e-1#') #print('-----------------\n') #mb.cont_param.set_value(9.2e-1) #results = opt.solve(flowsheet,tee=True,symbolic_solver_labels=False, # keepfiles=False) #results = opt.solve(flowsheet,tee=True,symbolic_solver_labels=False, # keepfiles=False) #print('\n-----------------') #print('#\epsilon = 9.3e-1#') #print('-----------------\n') #mb.cont_param.set_value(9.3e-1) #results = opt.solve(flowsheet,tee=True,symbolic_solver_labels=False, # keepfiles=False) #results = opt.solve(flowsheet,tee=True,symbolic_solver_labels=False, # keepfiles=False) #print('\n-----------------') #print('#\epsilon = 9.4e-1#') #print('-----------------\n') #mb.cont_param.set_value(9.4e-1) #results = opt.solve(flowsheet,tee=True,symbolic_solver_labels=False, # keepfiles=False) #results = opt.solve(flowsheet,tee=True,symbolic_solver_labels=False, # keepfiles=False) #print('\n-----------------') #print('#\epsilon = 9.5e-1#') #print('-----------------\n') #mb.cont_param.set_value(9.5e-1) #results = opt.solve(flowsheet,tee=True,symbolic_solver_labels=False, # keepfiles=False) #results = opt.solve(flowsheet,tee=True,symbolic_solver_labels=False, # keepfiles=False) #print('\n-----------------') #print('#\epsilon = 9.6e-1#') #print('-----------------\n') #mb.cont_param.set_value(9.6e-1) #results = opt.solve(flowsheet,tee=True,symbolic_solver_labels=False, # keepfiles=False) #results = opt.solve(flowsheet,tee=True,symbolic_solver_labels=False, # keepfiles=False) #print('\n-----------------') #print('#\epsilon = 9.7e-1#') #print('-----------------\n') #mb.cont_param.set_value(9.7e-1) #results = opt.solve(flowsheet,tee=True,symbolic_solver_labels=False, # keepfiles=False) #results = opt.solve(flowsheet,tee=True,symbolic_solver_labels=False, # keepfiles=False) #print('\n-----------------') #print('#\epsilon = 9.8e-1#') #print('-----------------\n') #mb.cont_param.set_value(9.8e-1) #results = opt.solve(flowsheet,tee=True,symbolic_solver_labels=False, # keepfiles=False) #results = opt.solve(flowsheet,tee=True,symbolic_solver_labels=False, # keepfiles=False) #print('\n-----------------') #print('#\epsilon = 9.9e-1#') #print('-----------------\n') #mb.cont_param.set_value(9.9e-1) #results = opt.solve(flowsheet,tee=True,symbolic_solver_labels=False, # keepfiles=False) #results = opt.solve(flowsheet,tee=True,symbolic_solver_labels=False, # keepfiles=False) print('\n-----------------') print('#\epsilon = 1#') print('-----------------\n') mb.cont_param.set_value(1) results = opt.solve(flowsheet,tee=True,symbolic_solver_labels=False, keepfiles=False) results = opt.solve(flowsheet,tee=True,symbolic_solver_labels=False, keepfiles=False) print_violated_constraints(flowsheet,tol) diff_vars = [mb.Cg, mb.q, mb.Tg, mb.Ts] write_results(flowsheet, 'gas_T.csv', diff_vars) #wd = os.getcwd() # #if not os.path.isdir(wd+'/results'): # os.mkdir(wd+'/results') #else: # print('results directory already exists') #with open('results/solid_m.csv', 'w', newline='') as f: # writer = csv.writer(f, delimiter=',') # row0 = [] # row0.append('name') # # should really do this systematically, finding some 'max no. indices' # max_n_idx = 3 # row0.append('idx1') # row0.append('idx2') # for t in mb.t: # row0.append(str(t)) # writer.writerow(row0) # for v in diff_vars: # name = v.getname() # if isinstance(v,SimpleVar): # continue # n_idx = v.index_set().dimen # print(v.index_set().set_tuple) # if mb.t not in v.index_set().set_tuple: # continue # idx_list = [] # if v.index_set() == mb.t: # row = [] # row.append(name) # idx_list.append('') # idx_list.append('') # for idx in idx_list: # row.append(idx) # for t in mb.t: # row.append(v[t].value) # writer.writerow(row) # continue # elif n_idx >= 2: # if v.index_set().set_tuple[0] != mb.t: # product = v.index_set().set_tuple[0] # else: # raise ValueError('Inconsistency, fix code') # # now product is a generator: # # either a set or a set product # for s in v.index_set().set_tuple: # if s != mb.t and s != product: # product = product *s # #for index in product: # # print(index) # for index in product: # # index is either a hashable value or a tuple # row = [] # row.append(name) # if isinstance(index,tuple): # for i in range(0,max_n_idx-1): # try: # row.append(index[i]) # except IndexError: # row.append('') # else: # row.append(index) # for i in range(1, max_n_idx-1): # row.append('') # # for t in mb.t: # if isinstance(index, tuple): # full_index = index + (t,) # else: # full_index = (index,t) # val = v[full_index].value # row.append(val) # writer.writerow(row) # for index in v: # row = [] # row.append(v.getname()) # idx_list = [] # for i in range(0,n_idx-1): # # append the non-time indices of the variable # idx_list.append(index[i]) # if n_idx < 3: # # ^3 should be replaced with n_max, or something # for i in range(0,3-n_index): # # fill in the remaning indices # idx_list.append('') # # better way to do this, maybe: # # for i in range(0,max): # # try row.append(idx_list[i]) # # except oob error: row.append('') # for idx in idx_list: # row.append(idx) # for t in m.t: #for t in mb.t: # alg_update(flowsheet,t) # update_time_derivatives(flowsheet,t) #print_violated_constraints(flowsheet,t) #with open('dyn_fs_sol.txt','w') as f: # flowsheet.display(ostream=f) ''' print("\n") print("----------------------------------------------------------") print('Total simulation time: ', value(time.time() - ts), " s") print("----------------------------------------------------------") # Print some variables print_summary_fuel_reactor(flowsheet) # Plot some variables #results_plot_fuel_reactor(flowsheet) #with open('m_fs.txt','w') as f: # flowsheet.display(ostream=f) # Store the flowsheet ''' return flowsheet
def main(): """ Make the flowsheet object and solve """ ss_flowsheet = ss_sim.main() flowsheet = Flowsheet(name='MB_Model') # fill in values of IC parameters from steady state solve setICs(flowsheet, ss_flowsheet) # Fix variables setInputs(flowsheet) # Initialize at steady state initialize_ss(flowsheet, ss_flowsheet) mb = flowsheet.MB_fuel # Then perturb # ^ that function should go in this file, probably # input: dict mapping state names (strings) to new values # this seems like as much work as doing the perturbation... # maybe just make a self contained function #input_perturbation = { ('Solid_In_x',mb.t) : { ['Fe2O3'] : 0.25 }, # ('Solid_In_x',mb.t) : { ['Al2O3'] : 0.75 } } perturbInputs(flowsheet) # perturb states # should put this in a dedicated ~intialize~ function # that also intelligently initializes the model after perturbation mb.eq_d4.deactivate() mb.eq_d5.deactivate() mb.eq_d8.deactivate() mb.eq_d9.deactivate() mb.eq_d10.deactivate() mb.eq_g7.deactivate() mb.eq_g8.deactivate() mb.eq_g10.deactivate() mb.eq_g11.deactivate() mb.eq_g12.deactivate() mb.eq_g13.deactivate() mb.eq_g14.deactivate() mb.eq_g4.deactivate() mb.eq_g5.deactivate() mb.eq_g2.deactivate() mb.Tg_GW.fix(0.0) mb.Tg_refractory.fix(0.0) mb.Tw_Wamb.fix() mb.Tw.fix() mb.Nuw.fix() mb.Nu_ext.fix() mb.hw.fix() mb.hext.fix() mb.hext2.fix() mb.U.fix() mb.Uw.fix() mb.Pr_ext.fix() mb.Ra.fix() mb.Re.fix() ### ''' ts = time.time() # Initialize fuel reactor flowsheet.MB_fuel._initialize(outlvl=1, optarg={"tol" : 1e-8, "max_cpu_time" : 600, "print_level" : 5, "halt_on_ampl_error": 'yes'}) ''' # Create a solver opt = SolverFactory('ipopt') opt.options = { 'tol': 1e-8, 'linear_solver': 'ma27', 'bound_push': 1e-8, 'max_cpu_time': 600, 'print_level': 5, 'halt_on_ampl_error': 'yes' } flowsheet.write('fs.nl') with open('dyn_fs.txt', 'w') as f: flowsheet.display(ostream=f) print('Constraints violated pre-solve:') for const in flowsheet.MB_fuel.component_objects(Constraint, active=True): if not isinstance(const, SimpleConstraint): for idx in const: if (value(const[idx].body) > value(const[idx].upper) + 1.0e-7) or \ (value(const[idx].body) < value(const[idx].lower) - 1.0e-7): print(const.name, idx) else: if (value(const.body) > value(const.upper) + 1.0e-7) or \ (value(const.body) < value(const.lower) - 1.0e-7): print(const.name) print('- - -\n') print('Variable bounds violated pre-solve:') for var in flowsheet.MB_fuel.component_objects(Var, active=True): # don't use IndexedComponent here, variables are always indexed components # could also do this by iterating over component_objects(SimpleVar)...? if not isinstance(var, SimpleVar): for idx in var: if not (var[idx].lb is None): if (var[idx].value < var[idx].lb - 1.0e-7): pdb.set_trace() print(var.name, idx) if not (var[idx].ub is None): if (var[idx].value > var[idx].ub + 1.0e-7): pdb.set_trace() print(var.name, idx) else: if (var.value > var.ub + 1.0e-7) or \ (var.value < var.lb - 1.0e-7): print(var.name) print('- - -\n') # initialized at steady state, works regardless: flowsheet.strip_bounds() for z in mb.z: for t in mb.t: mb.Cg[z, 'CH4', t].setlb(1e-6) # want a function to integrate the model one step from a specified time point # will call for all t integrate(flowsheet, 0) #results = opt.solve(flowsheet,tee=True,symbolic_solver_labels=False, # keepfiles=False) #with open('dyn_fs_sol.txt','w') as f: # flowsheet.display(ostream=f) ''' print("\n") print("----------------------------------------------------------") print('Total simulation time: ', value(time.time() - ts), " s") print("----------------------------------------------------------") # Print some variables print_summary_fuel_reactor(flowsheet) # Plot some variables #results_plot_fuel_reactor(flowsheet) #with open('m_fs.txt','w') as f: # flowsheet.display(ostream=f) # Store the flowsheet ''' return flowsheet
def main(): # main routine print('executing main routine') ss_flowsheet = ss_sim.main() time_set = [0,5,10,15,20,25,30, 35,40,45,50,55,60,65,70, 75,80,85,90,95,100,105,110,115,120, 125,130,135,140,145,150,155,160,165] fs_list = {} for t in time_set: fs_list[t] = Flowsheet() # set initial conditions to steady state for fe in time_set: setICs(fs_list[fe],ss_flowsheet) # initialize variables to steady state values for fe in time_set: initialize_ss(fs_list[fe],ss_flowsheet) # set input values for each finite element model for fe in time_set: setInputs(fs_list[fe]) # perturb inputs for fe in time_set: for t in fs_list[fe].MB_fuel.t: perturbInputs(fs_list[fe],t,Solid_M=691.4) # fix and deactivate unused stuff for t in time_set: # should really put all this in its own utility function... fs_list[t].MB_fuel.eq_d4.deactivate() fs_list[t].MB_fuel.eq_d5.deactivate() fs_list[t].MB_fuel.eq_d8.deactivate() fs_list[t].MB_fuel.eq_d9.deactivate() fs_list[t].MB_fuel.eq_d10.deactivate() fs_list[t].MB_fuel.eq_g7.deactivate() fs_list[t].MB_fuel.eq_g8.deactivate() fs_list[t].MB_fuel.eq_g10.deactivate() fs_list[t].MB_fuel.eq_g11.deactivate() fs_list[t].MB_fuel.eq_g12.deactivate() fs_list[t].MB_fuel.eq_g13.deactivate() fs_list[t].MB_fuel.eq_g14.deactivate() fs_list[t].MB_fuel.eq_g4.deactivate() fs_list[t].MB_fuel.eq_g5.deactivate() fs_list[t].MB_fuel.eq_g2.deactivate() fs_list[t].MB_fuel.Tg_GW.fix(0.0) fs_list[t].MB_fuel.Tw_GW.fix(0.0) fs_list[t].MB_fuel.Tg_refractory.fix(0.0) fs_list[t].MB_fuel.Tw_Wamb.fix() fs_list[t].MB_fuel.Tw.fix() fs_list[t].MB_fuel.Nuw.fix() fs_list[t].MB_fuel.Nu_ext.fix() fs_list[t].MB_fuel.hw.fix() fs_list[t].MB_fuel.hext.fix() fs_list[t].MB_fuel.hext2.fix() fs_list[t].MB_fuel.U.fix() fs_list[t].MB_fuel.Uw.fix() fs_list[t].MB_fuel.Pr_ext.fix() fs_list[t].MB_fuel.Ra.fix() fs_list[t].MB_fuel.Re.fix() ### # other tentatively unused variables: fs_list[t].MB_fuel.mFe_mAl.fix(0.0) fs_list[t].MB_fuel.Solid_Out_M_Comp.fix() # choose how to calculate certain algebraic variables: fs_list[t].MB_fuel.eq_c5.deactivate() #implicit_integrate(fs_list[0]) #set_fe_ICs(fs_list[0], fs_list[5]) #initialize_next_fe(fs_list[0], fs_list[5]) with open('one_fe.txt','w') as f: fs_list[0].display(ostream=f) for i in range(0,len(time_set)): print('hello') print_violated_constraints(fs_list[time_set[i]]) implicit_integrate(fs_list[time_set[i]]) if i != len(time_set)-1: set_fe_ICs(fs_list[time_set[i]],fs_list[time_set[i+1]]) initialize_next_fe(fs_list[time_set[i]],fs_list[time_set[i+1]]) # set initial conditions of next fe to final values of current fe # initialize next fe, somehow # a) initialize to initial conditions b) continuation c) explicit integrator print([ fs_list[t].MB_fuel.Cg[0.00062,'H2O',5].value for t in time_set ] ) return fs_list
def clc_integrate(mb): ss_flowsheet = ss_sim.main() time_set = mb.t.get_finite_elements() print(time_set) ncp = 0 for t in mb.t: print(t) if t < time_set[1]: ncp = ncp + 1 else: break print(ncp) if ncp == 1: method = 'BFD1' elif ncp > 1: method = 'OCLR' else: raise ValueError('Bad value of ncp when parsing full-horizon model') #time_set = [0,5,10,15,20,25,30] #35,40,45,50,55,60,65,70, #75,80,85,90,95,100,105,110,115,120, #125,130,135,140,145,150,155,160,165] # Create a custom grid, fe_set nfe = 6 fe_a = 1/4.0 fe_b = 0.2 fe_set = [0, 0.004] for i in range(1,nfe+1): if i < nfe*fe_a: fe_set.append(i*fe_b/(nfe*fe_a)) elif i == nfe: fe_set.append(1) else: fe_set.append(fe_b + (i-nfe*fe_a)*(1-fe_b)/(nfe*(1-fe_a))) fs_list = {} for i in range(0,len(time_set)): if i != len(time_set)-1: # assumption here that the last element of the time_set # is the end of the horizon. Could this ever not be the case? H = time_set[i+1] - time_set[i] fs_list[time_set[i]] = make_flowsheet(press_drop ='Ergun', t_dae_method = method, horizon = H, ncp_t = ncp) # fs_list[t] = MB_CLC_fuel.MB( # parent = None, # z_dae_method = 'OCLR', # #t_dae_method = 'OCLR', # t_dae_method = method, # press_drop = 'Ergun', # fe_set = fe_set, # ncp = 3, # horizon = H, # nfe_t = 1, # ncp_t = ncp) time_set.pop() # set initial conditions to steady state for fe in time_set: setICs(fs_list[fe],ss_flowsheet) # initialize variables to steady state values for fe in time_set: initialize_ss(fs_list[fe],ss_flowsheet) # set input values for each finite element model for fe in time_set: setInputs(fs_list[fe]) # perturb inputs for fe in time_set: for t in fs_list[fe].MB_fuel.t: perturbInputs(fs_list[fe],t,Solid_M=691.4) # fix and deactivate unused stuff #for t in time_set: # # should really put all this in its own utility function... # fs_list[t].MB_fuel.eq_d4.deactivate() # fs_list[t].MB_fuel.eq_d5.deactivate() # fs_list[t].MB_fuel.eq_d8.deactivate() # fs_list[t].MB_fuel.eq_d9.deactivate() # fs_list[t].MB_fuel.eq_d10.deactivate() # fs_list[t].MB_fuel.eq_g7.deactivate() # fs_list[t].MB_fuel.eq_g8.deactivate() # fs_list[t].MB_fuel.eq_g10.deactivate() # fs_list[t].MB_fuel.eq_g11.deactivate() # fs_list[t].MB_fuel.eq_g12.deactivate() # fs_list[t].MB_fuel.eq_g13.deactivate() # fs_list[t].MB_fuel.eq_g14.deactivate() # fs_list[t].MB_fuel.eq_g4.deactivate() # fs_list[t].MB_fuel.eq_g5.deactivate() # fs_list[t].MB_fuel.eq_g2.deactivate() # fs_list[t].MB_fuel.Tg_GW.fix(0.0) # fs_list[t].MB_fuel.Tw_GW.fix(0.0) # fs_list[t].MB_fuel.Tg_refractory.fix(0.0) # fs_list[t].MB_fuel.Tw_Wamb.fix() # fs_list[t].MB_fuel.Tw.fix() # fs_list[t].MB_fuel.Nuw.fix() # fs_list[t].MB_fuel.Nu_ext.fix() # fs_list[t].MB_fuel.hw.fix() # fs_list[t].MB_fuel.hext.fix() # fs_list[t].MB_fuel.hext2.fix() # fs_list[t].MB_fuel.U.fix() # fs_list[t].MB_fuel.Uw.fix() # fs_list[t].MB_fuel.Pr_ext.fix() # fs_list[t].MB_fuel.Ra.fix() # fs_list[t].MB_fuel.Re.fix() # ### # # other tentatively unused variables: # fs_list[t].MB_fuel.mFe_mAl.fix(0.0) # fs_list[t].MB_fuel.Solid_Out_M_Comp.fix() # # choose how to calculate certain algebraic variables: # fs_list[t].MB_fuel.eq_c5.deactivate() for t in time_set: make_square(fs_list[t].MB_fuel) #with open('one_fe.txt','w') as f: # fs_list[0].display(ostream=f) for i in range(0,len(time_set)): implicit_integrate(fs_list[time_set[i]]) if i != len(time_set)-1: set_fe_ICs(fs_list[time_set[i]],fs_list[time_set[i+1]]) initialize_next_fe(fs_list[time_set[i]],fs_list[time_set[i+1]]) # set initial conditions of next fe to final values of current fe # initialize next fe, somehow # a) initialize to initial conditions b) continuation c) explicit integrator print([ fs_list[t].MB_fuel.Cg[0.00062,'H2O',5].value for t in time_set ] ) return fs_list
def clc_integrate(mb): ss_flowsheet = ss_sim.main() time_set = mb.t.get_finite_elements() ncp = 0 for t in mb.t: if t < time_set[1]: ncp = ncp + 1 else: break if ncp == 1: method = 'BFD1' elif ncp > 1: method = 'OCLR' else: raise ValueError('Bad value of ncp when parsing full-horizon model') #time_set = [0,5,10,15,20,25,30] #35,40,45,50,55,60,65,70, #75,80,85,90,95,100,105,110,115,120, #125,130,135,140,145,150,155,160,165] # Create a custom grid, fe_set nfe = 6 fe_a = 1/4.0 fe_b = 0.2 fe_set = [0, 0.004] for i in range(1,nfe+1): if i < nfe*fe_a: fe_set.append(i*fe_b/(nfe*fe_a)) elif i == nfe: fe_set.append(1) else: fe_set.append(fe_b + (i-nfe*fe_a)*(1-fe_b)/(nfe*(1-fe_a))) fs_list = {} for i in range(0,len(time_set)): if i != len(time_set)-1: # assumption here that the last element of the time_set # is the end of the horizon. Could this ever not be the case? H = time_set[i+1] - time_set[i] fs_list[time_set[i]] = make_flowsheet(press_drop ='Ergun', t_dae_method = method, horizon = H, ncp_t = ncp) # fs_list[t] = MB_CLC_fuel.MB( # parent = None, # z_dae_method = 'OCLR', # #t_dae_method = 'OCLR', # t_dae_method = method, # press_drop = 'Ergun', # fe_set = fe_set, # ncp = 3, # horizon = H, # nfe_t = 1, # ncp_t = ncp) time_set.pop() # set initial conditions to steady state for fe in time_set: setICs(fs_list[fe],ss_flowsheet) # initialize variables to steady state values for fe in time_set: initialize_ss(fs_list[fe],ss_flowsheet) # set input values for each finite element model for fe in time_set: setInputs(fs_list[fe]) gas_y_ptb = {'CO2': 0.04999, 'H2O': 0.00001, 'CH4': 0.95} # perturb inputs copy_inputs(mb, fs_list) #for fe in time_set: # for t in fs_list[fe].MB_fuel.t: # copy_inputs(fs_list[fe].MB_fuel, t, mb, fe+t) #perturbInputs(fs_list[fe], t, Gas_y=gas_y_ptb) for t in time_set: make_square(fs_list[t].MB_fuel) #with open('one_fe.txt','w') as f: # fs_list[0].display(ostream=f) for i in range(0,len(time_set)): # should initialization occur here or in the imp_int function? implicit_integrate(fs_list[time_set[i]]) if i != len(time_set)-1: set_fe_ICs(fs_list[time_set[i]],fs_list[time_set[i+1]]) initialize_next_fe(fs_list[time_set[i]],fs_list[time_set[i+1]]) # set initial conditions of next fe to final values of current fe # initialize next fe, somehow # a) initialize to initial conditions b) continuation c) explicit integrator fe_width = time_set[0] # ^ assumes a fixed finite element width print([ fs_list[t].MB_fuel.Cg[0.00062,'H2O',fe_width].value for t in time_set ] ) return fs_list
def main(): """ Make the flowsheet object and solve """ ss_init = ss_sim.main() flowsheet = Flowsheet(name='MB_Model') # fill in values of IC parameters from steady state solve setICs(flowsheet, ss_init) # Fix variables setInputs(flowsheet) # Initialize at steady state initialize_ss(flowsheet, ss_init) mb = flowsheet.MB_fuel #write_differential_equations(flowsheet) # Then perturb ptb = {} solid_x_ptb = {'Fe2O3': 0.25, 'Fe3O4': 0.01, 'Al2O3': 0.74} gas_y_ptb = {'CO2': 0.04999, 'H2O': 0.00001, 'CH4': 0.95} #perturbInputs(flowsheet,0,Solid_M=691.4,Solid_T=1283.15,Solid_x=solid_x_ptb, # Gas_F=150,Gas_T=350,Gas_y=gas_y_ptb) for t in mb.t: ptb_inputs(flowsheet, t, Solid_M=691.4) ss_final = ss_sim.main(Solid_M=691.4) with open('ss_init.txt', 'w') as f: ss_init.display(ostream=f) with open('ss_fin.txt', 'w') as f: ss_final.display(ostream=f) # should put this in a dedicated ~intialize~ function # that also intelligently initializes the model after perturbation mb.eq_d4.deactivate() mb.eq_d5.deactivate() mb.eq_d8.deactivate() mb.eq_d9.deactivate() mb.eq_d10.deactivate() mb.eq_g7.deactivate() mb.eq_g8.deactivate() mb.eq_g10.deactivate() mb.eq_g11.deactivate() mb.eq_g12.deactivate() mb.eq_g13.deactivate() mb.eq_g14.deactivate() mb.eq_g4.deactivate() mb.eq_g5.deactivate() mb.eq_g2.deactivate() mb.Tg_GW.fix(0.0) mb.Tw_GW.fix(0.0) mb.Tg_refractory.fix(0.0) mb.Tw_Wamb.fix() mb.Tw.fix() mb.Nuw.fix() mb.Nu_ext.fix() mb.hw.fix() mb.hext.fix() mb.hext2.fix() mb.U.fix() mb.Uw.fix() mb.Pr_ext.fix() mb.Ra.fix() mb.Re.fix() ### # other tentatively unused variables: mb.mFe_mAl.fix(0.0) mb.Solid_Out_M_Comp.fix() # choose how to calculate certain algebraic variables: mb.eq_c5.deactivate() # Create a solver tol = 1e-8 opt = SolverFactory('ipopt') opt.options = { 'tol': tol, 'linear_solver': 'ma57', 'bound_push': 1e-8, 'max_cpu_time': 600, 'print_level': 5, 'output_file': 'ipopt_out.txt', 'linear_system_scaling': 'mc19', #'linear_scaling_on_demand' : 'yes', 'halt_on_ampl_error': 'yes' } # initialized at steady state, works regardless: flowsheet.strip_bounds() print_violated_constraints(flowsheet, tol) for t in mb.t: alg_update(flowsheet, t) update_time_derivatives(flowsheet, t) constraint_list = [] for c in mb.component_objects(Constraint): constraint_list.append(c) create_suffixes(flowsheet) vars_to_scale = [] data_scaled = [] con_data_2update = [] for var in mb.component_objects(Var): # need static list of vars to scale vars_to_scale.append(var) for var in mb.component_data_objects(Var): # should I create parallel blocks of scaled/unscaled constraints? data_scaled.append(var) for con in mb.component_data_objects(Constraint): con_data_2update.append(con) # should separate variables into input/response variables # only scale response variables, or have separate function to # scale input variables # # or, an is_alg_fcn_of() function would be nice, but probably very # difficult to implement # # should be able to partition variables into useful categories... # differential, algebraic-important, algebraic-auxiliary, time-derivative, # space-derivative, geometric, (parameter,) input, input-algebraic # # such a partition would be incredibly powerful for a variety of applications # e.g. identifying degrees of freedom, index reduction, initialization, # model simplification diff_vars = [mb.Cg, mb.q, mb.Tg, mb.Ts] alg_vars = [ mb.Gas_Out_P, mb.Solid_Out_M, mb.Solid_Out_Ts, mb.Solid_Out_x, mb.CgT, mb.Ctrans, mb.G_flux, mb.X_gas, mb.y, mb.ytot, mb.Ftotal, mb.F, mb.Gas_M, mb.qT, mb.qtrans, mb.S_flux, mb.X_OC, mb.x, mb.xtot, mb.Solid_M_total, mb.Solid_M, mb.Solid_F_total, mb.Solid_F, mb.mFe_mAl, mb.P, mb.Tg_GS, mb.Ts_dHr, mb.vg, mb.umf, mb.v_diff, mb.Rep, mb.Pr, mb.Pr_ext, mb.Ra, mb.Nu, mb.hf, mb.Gh_flux, mb.Sh_flux, mb.DH_rxn_s, mb.cp_sol, mb.MW_vap, mb.rho_vap, mb.mu_vap, mb.cp_gas, mb.cp_vap, mb.k_cpcv, mb.k_vap, mb.X, mb.X_term, mb.k, mb.r_gen, mb.rg, mb.rs, mb.dG_fluxdz, mb.dS_fluxdz, mb.dPdz, mb.dGh_fluxdz, mb.dSh_fluxdz ] dyn_vars = diff_vars + alg_vars constraints_to_scan = [] for con in mb.component_objects(Constraint): constraints_to_scan.append(con) for var in dyn_vars: print(var.name) create_scale_values(var, flowsheet, ss_init, ss_final) #create_scale_values(mb.Cg, flowsheet, ss_init, ss_final) #with open('pre_constraint_update.txt', 'w') as f: # flowsheet.display(ostream=f) for con in constraints_to_scan: # probably need to selectively update constraints as well print(con.name) update_constraint(con, flowsheet) # TODO: -check that constraints have been properly updated, # -check for violated constraints (in this single element), # ^ should only be discretization equations... # -try to solve model # -have not scaled constraints yet, but should still solve... #update_constraint(mb.eq_c4, flowsheet) #update_constraint(mb.eq_b1, flowsheet) #update_constraint(mb.eq_a2, flowsheet) #update_constraint(mb.eq_p2, flowsheet) with open('scale_vars.txt', 'w') as f: # per custom, I should replace this with a 'write_scale_vars()' function # line = '' # for var in m.component_objects(Var): # if isinstance(var, SimpleVar): # line = line + var.name + ' ' + str(var.value) # # ^probably need to re-format this value string # try: # if var.has_dev_exp == True: # line = line + '\t' + var.dev.name + ' ' + str(value(var.dev)) # except AttributeError: # pass # else: # try: # if var.has_dev_exp == True: # for index in var: for var in data_scaled: line = var.local_name + ' ' try: if var.parent_component().has_dev_exp == True: line = (line + var.parent_component().dev_exp[ var.index()].expr.to_string()) except AttributeError: line = line + ' has no deviation expression' line = line + '\n' f.write(line) write_constraint_expressions(con_data_2update) #print(mb.eq_p2[0.00062,0].expr.to_string()) #print(mb.eq_p2.dev_con[0.00062,0].expr.to_string()) with open('dyn_scaled_init.txt', 'w') as f: flowsheet.display(ostream=f) mb.cont_param.set_value(1) return flowsheet