Using an external solver with Aspen

| categories: programming | tags: aspen

One reason to interact with Aspen via python is to use external solvers to drive the simulations. Aspen has some built-in solvers, but it does not have everything. You may also want to integrate additional calculations, e.g. capital costs, water usage, etc… and integrate those results into a report.

Here is a simple example where we use fsolve to find the temperature of the flash tank that will give a vapor phase mole fraction of ethanol of 0.8. It is a simple example, but it illustrates the possibility.

import os
import win32com.client as win32
aspen = win32.Dispatch('Apwn.Document')

aspen.InitFromArchive2(os.path.abspath('data\Flash_Example.bkp'))

from scipy.optimize import fsolve

def func(flashT):
    flashT = float(flashT) # COM objects do not understand numpy types
    aspen.Tree.FindNode('\Data\Blocks\FLASH\Input\TEMP').Value = flashT
    aspen.Engine.Run2()
    y = aspen.Tree.FindNode('\Data\Streams\VAPOR\Output\MOLEFRAC\MIXED\ETHANOL').Value
    return y - 0.8

sol, = fsolve(func, 150.0)
print 'A flash temperature of {0:1.2f} degF will have y_ethanol = 0.8'.format(sol)
A flash temperature of 157.38 degF will have y_ethanol = 0.8

One unexpected detail was that the Aspen COM objects cannot be assigned numpy number types, so it was necessary to recast the argument as a float. Otherwise, this worked about as expected for an fsolve problem.

Copyright (C) 2013 by John Kitchin. See the License for information about copying.

org-mode source

Discuss on Twitter