def do_simulation():
    global num_sample_id

    # set Samples Path
    sample_path = r'./Sample_9bus_' + input('请输入样本集编号:')
    if os.path.exists(sample_path):
        print('该样本集已存在,请重新输入...\n')
        sample_path = r'./Sample_9bus_' + input('请重新输入样本集编号:')
    else:
        os.makedirs(sample_path)

    # # 1 母线故障——样本编号:1~12*num_bus -> [1,  108]
    # for i in range(num_bus):
    # # for i in range(1):
    # 	for j in range(len(clear_time)):
    # 		# init
    # 		psspy.psseinit(50)
    # 		psspy.case(r""".\Models\9Bus-test\ieee9bus_v32.sav""")
    # 		psspy.fnsl([0, 0, 0, 1, 1, 0, 99, 0])
    # 		psspy.cong(0)
    # 		psspy.conl(0, 1, 1, [0, 0], [100.0, 0.0, 0.0, 100.0])
    # 		psspy.conl(0, 1, 2, [0, 0], [100.0, 0.0, 0.0, 100.0])
    # 		psspy.conl(0, 1, 3, [0, 0], [100.0, 0.0, 0.0, 100.0])
    # 		psspy.fact()
    # 		psspy.tysl(0)
    # 		psspy.dyre_new([1, 1, 1, 1], r""".\Models\9Bus-test\ieee9bus.dyr""", "", "", "")
    #
    # 		# perform Dynamic Simulation
    # 		# 1) set "Channal Setup Wizard"
    # 		psspy.chsb(0, 1, [-1, -1, -1, 1, 1, 0])    # Angle
    # 		psspy.chsb(0, 1, [-1, -1, -1, 1, 7, 0])    #
    # 		psspy.chsb(0, 1, [-1, -1, -1, 1, 13, 0])  #
    #
    # 		# 2) name output file
    # 		psspy.set_chnfil_type(0)  # 1 for OUTX format, 0 for (old) OUT format
    # 		psspy.strt_2([0, 1], sample_path + '\\t9Bus-PY-00' + str(num_sample_id) + '.out')
    # 		num_sample_id += 1
    #
    # 		# 3) in normal stat, run network to 1 seconds
    # 		psspy.run(0, 1.0, 0, 1, 1)
    #
    # 		# 4) set Balanced_bus_fault with R,X -> [0, 0]
    # 		psspy.dist_bus_fault(bus_id[i], 1, bus_kv[i], [0.0, -2E+2])		# --------  ! important
    #
    # 		# 5) set fault run time
    # 		psspy.run(0, 1 + clear_time[j], 0, 1, 1)
    #
    # 		# 6) clear fault, and run net to 10 seconds
    # 		psspy.dist_clear_fault(1)
    # 		psspy.run(0, 10.0, 0, 1, 1)
    #
    # 		# 7) 关闭本次仿真相关的文件, importaant !!!!!!!!!!!!!!!!!!!!!!
    # 		psspy.pssehalt_2()

    # 2 非变压器支路故障——样本编号: [108+1,  108+360]
    for i in range(num_notrans_brch):
        # for i in range(1):
        for k in range(len(fault_position)):
            # for k in range(1):
            for j in range(len(clear_time)):
                print('\n\n\n\n----------------我是分割线--------------\n')
                print('已仿真第 ' + str(num_sample_id) + ' 条样本\n\n\n\n')

                # init
                psspy.psseinit(50)
                psspy.case(r""".\Models\9Bus-test\ieee9bus_v32.sav""")
                psspy.fnsl([0, 0, 0, 1, 1, 0, 99, 0])
                psspy.cong(0)
                psspy.conl(0, 1, 1, [0, 0], [100.0, 0.0, 0.0, 100.0])
                psspy.conl(0, 1, 2, [0, 0], [100.0, 0.0, 0.0, 100.0])
                psspy.conl(0, 1, 3, [0, 0], [100.0, 0.0, 0.0, 100.0])
                psspy.fact()
                psspy.tysl(0)
                psspy.dyre_new([1, 1, 1, 1],
                               r""".\Models\9Bus-test\ieee9bus.dyr""", "", "",
                               "")

                # perform Dynamic Simulation
                # 1) set "Channal Setup Wizard"
                psspy.chsb(0, 1, [-1, -1, -1, 1, 1, 0])  # Angle
                psspy.chsb(0, 1, [-1, -1, -1, 1, 7, 0])  #
                psspy.chsb(0, 1, [-1, -1, -1, 1, 13, 0])  #

                # 1.1) set Relative Machine Angle
                psspy.set_relang(1, 1, r"""1""")

                # 2) name output file
                psspy.set_chnfil_type(
                    0)  # 1 for OUTX format, 0 for (old) OUT format
                psspy.strt_2([0, 1], sample_path + '//t9Bus-PY-00' +
                             str(num_sample_id) + '.out')
                num_sample_id += 1

                # 3) in normal stat, run network to 1 seconds
                psspy.run(0, 1.0, 0, 1, 1)

                # 4) set unbalanced_Branch_fault -- 3 Phase -- with R,X -> [0, 0]
                psspy.dist_spcb_fault_2(
                    notrans_brch[i][0], notrans_brch[i][1], r"""1""",
                    [3, 0, 3, 1, 0, 0, 1],
                    [fault_position[k], 0.0, 0.0, 0.0, 0.0
                     ])  # --------  ! important

                # 5) set fault run time
                psspy.run(0, 1 + clear_time[j], 0, 1, 1)

                # 6) clear fault, and run net to 10 seconds
                psspy.dist_clear_fault(1)
                psspy.run(0, 10.0, 0, 1, 1)

                # 7) 关闭本次仿真相关的文件, importaant !!!!!!!!!!!!!!!!!!!!!!
                psspy.pssehalt_2()
                0, 1, [1, 1, 1, 0, 0, 0, 0], 0, scopfaddress + '\\' + caseX +
                '\\' + caseX + "_" + str(cont) + '.raw')

        # if cont_con_array is not empty, which means ACCC ignores some contingencies,
        # output solution1 as solution2 for these contingencies
        if cont_con_array:
            print(
                '!!!!!!!!!!!!------------cont_con_array is not empty!--------------'
            )
            for icon in range(0, len(cont_con_array)):
                conttmp = cont_con_array[icon]
                System = COMPET_FORM(conttmp,solutin1file, solutin2file, vbusno_basecse, vbusmag_basecse,vbusangle_basecse, \
                                     vgenbusno_basecse,vgenid_basecse, vgenp_basecse, vgenq_basecse, vbus_shunt_val_basecse,totalgendelta_basecse)

        # for each case, need to clear PSS/E memory to start a new one
        psspy.pssehalt_2()

csv_scopf_obj.close()
workbookscopf.close()

if os.path.exists(fileINL[:-4] + '_mod.inl'):
    os.remove(fileINL[:-4] + '_mod.inl')
    os.rename(fileINL, fileINL[:-4] + '_mod.inl')
else:
    os.rename(fileINL, fileINL[:-4] + '_mod.inl')

os.rename(fileINLorg, fileINL)

for casetmp in cases_delta_dict.keys():
    print(casetmp + '  delta:  ' + str(cases_delta_dict[casetmp]))
def do_simulation(sav_path, dyr_path):
    global num_sample_id

    # set Samples Path
    sample_path = r'./Sample_14bus_' + input('请输入样本集编号:')
    if os.path.exists(sample_path):
        print('该样本集已存在,请重新输入...\n')
        sample_path = r'./Sample_14bus_' + input('请重新输入样本集编号:')
    else:
        os.makedirs(sample_path)

    # # 1 母线故障——样本编号:1~12*num_bus -> [1, 168]
    # for i in range(num_bus):
    # # for i in range(1):
    # 	for j in range(len(clear_time)):
    # 		# init
    # 		psspy.psseinit(50)
    # 		psspy.case(sav_path)
    # 		psspy.fnsl([0, 0, 0, 1, 1, 0, 99, 0])
    # 		psspy.cong(0)
    # 		psspy.conl(0, 1, 1, [0, 0], [100.0, 0.0, 0.0, 100.0])
    # 		psspy.conl(0, 1, 2, [0, 0], [100.0, 0.0, 0.0, 100.0])
    # 		psspy.conl(0, 1, 3, [0, 0], [100.0, 0.0, 0.0, 100.0])
    # 		psspy.fact()
    # 		psspy.tysl(0)
    # 		psspy.dyre_new([1, 1, 1, 1], dyr_path, "", "", "")
    #
    # 		# perform Dynamic Simulation
    # 		# 1) set "Channal Setup Wizard"
    # 		psspy.chsb(0, 1, [-1, -1, -1, 1, 1, 0])     # Angle -> 发电机功角
    # 		psspy.chsb(0, 1, [-1, -1, -1, 1, 14, 0])    # Volt & Angle -> 电压&相角
    # 		psspy.chsb(0, 1, [-1, -1, -1, 1, 16, 0])    # Flow (P & Q) -> 潮流
    #
    # 		# 2) name output file
    # 		psspy.set_chnfil_type(0)  # 1 for OUTX format, 0 for (old) OUT format
    # 		psspy.strt_2([0, 1], sample_path + '\\t14Bus-PY-00' + str(num_sample_id) + '.out')
    # 		num_sample_id += 1
    #
    # 		# 3) in normal stat, run network to 1 seconds
    # 		psspy.run(0, 1.0, 0, 1, 1)
    #
    # 		# 4) set Balanced_bus_fault; Default with R,X -> [0, -2e+009]
    # 		psspy.dist_bus_fault(bus_id[i], 1, bus_kv[i], [0.0, -2E9])		# --------  ! important
    #
    # 		# bus5 unbalanced fault with [R-LL, X-LL, R-LG, X-LG], 是单相故障 !!!!!!!
    # 		# 事实证明设置LL的RX对稳定毫无影响!!
    # 		# psspy.dist_scmu_fault_2([0, 0, 2, bus_id[i], ], [2E5, 3E5, 0, 0])
    #
    # 		# 5) set fault run time
    # 		psspy.run(0, 1 + clear_time[j], 0, 1, 1)
    #
    # 		# 5) clear fault, and run net to 10 seconds
    # 		psspy.dist_clear_fault(1)
    # 		psspy.run(0, 10.0, 0, 1, 1)

    # 2 非变压器支路故障——样本编号: [bus_id ,bus_id + 960]
    # 根据本循环写的嵌套次序,故障时间每12条仿真为一个循环
    # for i in range(num_notrans_brch):
    for i in range(1):
        # for k in range(len(fault_position)):
        for k in range(3):
            # for j in range(len(clear_time)):
            for j in range(12):
                print('\n\n\n\n----------------我是分割线--------------\n\n\n\n\n')
                print('已仿真第 ' + str(num_sample_id) + ' 条样本')

                # init
                psspy.psseinit(10)
                psspy.case(sav_path)
                psspy.fnsl([0, 0, 0, 1, 1, 0, 99, 0])
                psspy.cong(0)
                psspy.conl(0, 1, 1, [0, 0], [100.0, 0.0, 0.0, 100.0])
                psspy.conl(0, 1, 2, [0, 0], [100.0, 0.0, 0.0, 100.0])
                psspy.conl(0, 1, 3, [0, 0], [100.0, 0.0, 0.0, 100.0])
                psspy.fact()
                psspy.tysl(0)
                psspy.dyre_new([1, 1, 1, 1], dyr_path, "", "", "")

                # perform Dynamic Simulation
                # 1) set "Channal Setup Wizard"
                psspy.chsb(0, 1, [-1, -1, -1, 1, 1, 0])  # Angle -> 发电机功角
                psspy.chsb(0, 1,
                           [-1, -1, -1, 1, 14, 0])  # Volt & Angle -> 电压&相角
                psspy.chsb(0, 1, [-1, -1, -1, 1, 16, 0])  # Flow (P & Q) -> 潮流

                # 2) name output file
                psspy.set_chnfil_type(
                    0)  # 1 for OUTX format, 0 for (old) OUT format
                psspy.strt_2([0, 1], sample_path + '\\t14Bus-PY-00' +
                             str(num_sample_id) + '.out')
                num_sample_id += 1

                # 3) in normal stat, run network to 1 seconds
                psspy.run(0, 1.0, 0, 1, 1)

                # 4) set unbalanced_Branch_fault -- 3 Phase -- with R,X -> [0, 0]
                psspy.dist_spcb_fault_2(
                    notrans_brch[i][0], notrans_brch[i][1], r"""1""",
                    [3, 0, 3, 1, 0, 0, 1],
                    [fault_position[k], 0.0, 0.0, 0.0, 0.0
                     ])  # --------  ! important

                # 5) set fault run time
                psspy.run(0, 1 + clear_time[j], 0, 1, 1)

                # 6) clear fault, and run net to 10 seconds
                psspy.dist_clear_fault(1)
                psspy.run(0, 10.0, 0, 1, 1)

                # 7) 关闭本次仿真相关的文件, importaant !!!!!!!!!!!!!!!!!!!!!!
                psspy.pssehalt_2()