if varType == 'dis':
		inputData = extract_input_data(inputInfos)
	else:
		inputData = generate_input_data(inputInfos)
	
	
	# run simulation with Aspen
	try:
		aspenModel = Aspen(aspenFile)
		calculator = Excel(calculatorFile)

		simResults = simulate_using_aspen(aspenModel, calculator, inputData, outputInfos, outDir, nruns)
	
	finally:
		aspenModel.close()
		calculator.close()

	
	# save and plot results
	save_simulation_results(simResults, outDir)
	
	plot_hist(simResults, outputInfos[['Output', 'Unit']], outDir)
	






Esempio n. 2
0
def calculate_margin(inputInfos, outputInfos, aspenFiles, calculatorFiles,
                     marketPriceFiles, rinPriceFile, capital, credits):
    '''
	Parameters
	inputInfos: df, input infos for optimization, columns are ['Model', 'Input', 'Location', 'InputPath']
	outputInfos: df, output infos, columns are ['Model', 'Output', 'Unit', 'Location'] 
	aspenFiles: dict, keys are model names, values are aspen model path
	calculatorFiles: dict, keys are model names, values are calculator path
	marketPriceFiles: dict, keys are model names, values are market price file path
	rinPriceFile: str or None, rin price file, None if no subtractor rin price
	capital: str, whether to include the capital investment
	credits: str, whether to include the by-product credits in sugar model
	
	Returns
	margins: dict, keys are model names, values are df (index are time, columns are ['output (unit)', 'market price (unit)', 'margin (unit)'])
	totalMargins: df, index are time, columns are models
	'''

    margins = {}
    totalMargins = {}
    for model in inputInfos['Model'].unique():
        if model is np.nan: continue

        inputInfo = inputInfos[inputInfos['Model'] == model]
        inputInfo.index = range(inputInfo.shape[0])

        inputData = pd.DataFrame(index=inputInfo.index,
                                 columns=['Input', 'Location', 'Data'],
                                 dtype=object)
        for i, (input, location,
                inputPath) in inputInfo[['Input', 'Location',
                                         'InputPath']].iterrows():
            inputData.loc[i, :] = [
                input, location,
                pd.read_csv(inputPath, sep='\t',
                            index_col=0).values.reshape(-1)
            ]

        if capital == 'no':
            inputData.loc[inputData.shape[0], :] = [
                'Capital', 'DCFROR!B18',
                np.zeros(inputData.loc[0, 'Data'].size)
            ]

        if credits == 'no' and model == 'sugar':
            inputData.loc[inputData.shape[0], :] = [
                'Credits', 'OPEX!I48',
                np.zeros(inputData.loc[0, 'Data'].size)
            ]

        outputInfo = outputInfos.loc[outputInfos['Model'] == model,
                                     ['Output', 'Unit', 'Location']]
        outputInfo.index = [0]  # index start from 0
        output, unit = outputInfo.loc[0, :][['Output', 'Unit']]

        ###
        if output == 'MESP':
            outputInfo.loc[1, :] = ['Production', 'gal/yr', 'OPEX!B18']
        elif output == 'MSSP':
            outputInfo.loc[1, :] = ['Production', 'kg/yr', 'OPEX!B17']
        ###

        aspenFile = aspenFiles[model]
        calculatorFile = calculatorFiles[model]

        aspenModel = Aspen(aspenFile)
        calculator = Excel(calculatorFile)

        simResults = simulate_using_calculator(aspenModel, calculator,
                                               inputData, outputInfo)

        aspenModel.close()
        calculator.close()

        priceFile = marketPriceFiles[model]
        marketPrices = pd.read_csv(priceFile, sep='\t', index_col=0)

        data = pd.DataFrame(index=marketPrices.index)
        if rinPriceFile:
            rinPrice = pd.read_csv(rinPriceFile, sep='\t',
                                   index_col=0).values.reshape(-1)
            data['%s (%s)' %
                 (output, unit)] = simResults[output].values - rinPrice
        else:
            data['%s (%s)' % (output, unit)] = simResults[output].values
        data['market price (%s)' % unit] = marketPrices
        data['margin (%s)' %
             unit] = data['market price (%s)' % unit] - data['%s (%s)' %
                                                             (output, unit)]
        margins[model] = data

        ###
        totalMargin = data['margin (%s)' %
                           unit].values * simResults['Production'].values
        if output == 'MESP':
            totalMargin = totalMargin / 52
        elif output == 'MSSP':
            totalMargin = totalMargin / 52 * 2.2045
        totalMargins[model] = totalMargin

    totalMargins = pd.DataFrame(totalMargins, index=marketPrices.index)
    ###

    return margins, totalMargins