Пример #1
0
print("Problem statistics:")
print("----------------------")
print("Number of variables: {:>25d}".format(nlp.nx))
print("Number of equality constraints: {:>14d}".format(nlp.nc))
print("Number of inequality constraints: {:>11d}".format(nlp.nd))
print("Total number of constraints: {:>17d}".format(nlp.ng))
print("Number of nnz in Jacobian: {:>20d}".format(nlp.nnz_jacobian_g))
print("Number of nnz in hessian of Lagrange: {:>8d}".format(
    nlp.nnz_hessian_lag))

x = nlp.x_init()
y = nlp.create_vector_y()
y.fill(1.0)

# Evaluate jacobian of all constraints
jac_g = nlp.jacobian_g(x)
plt.spy(jac_g)
plt.title('Jacobian of the all constraints\n')
plt.show()

# Evaluate jacobian of equality constraints
jac_c = nlp.jacobian_c(x)
plt.title('Jacobian of the equality constraints\n')
plt.spy(jac_c)
plt.show()

# Evaluate jacobian of equality constraints
jac_d = nlp.jacobian_d(x)
plt.title('Jacobian of the inequality constraints\n')
plt.spy(jac_d)
plt.show()
Пример #2
0
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
Пример #3
0
print("\n----------------------")
print("Problem statistics:")
print("----------------------")
print("Number of variables: {:>25d}".format(nlp.nx))
print("Number of equality constraints: {:>14d}".format(nlp.nc))
print("Number of inequality constraints: {:>11d}".format(nlp.nd))
print("Total number of constraints: {:>17d}".format(nlp.ng))
print("Number of nnz in Jacobian: {:>20d}".format(nlp.nnz_jacobian_g))
print("Number of nnz in hessian of Lagrange: {:>8d}".format(nlp.nnz_hessian_lag))

x = nlp.x_init()
y = nlp.create_vector_y()
y.fill(1.0)

# Evaluate jacobian of all constraints
jac_g = nlp.jacobian_g(x)
plt.spy(jac_g)
plt.title('Jacobian of the all constraints\n')
plt.show()

# Evaluate jacobian of equality constraints
jac_c = nlp.jacobian_c(x)
plt.title('Jacobian of the equality constraints\n')
plt.spy(jac_c)
plt.show()

# Evaluate jacobian of equality constraints
jac_d = nlp.jacobian_d(x)
plt.title('Jacobian of the inequality constraints\n')
plt.spy(jac_d)
plt.show()