Jump to content



Featured Articles

Check out the latest featured articles.

File Library

Check out the latest downloads available in the File Library.

New Article

Product Viscosity vs. Shear

Featured File

Vertical Tank Selection

New Blog Entry

Low Flow in Pipes- posted in Ankur's blog

0

Creating A Reactor In Aspen Plus Through Vba For Rl

aspen vba vb rl ai reactor rxn_id

5 replies to this topic
Share this topic:
| More

#1 A.D.Christos

A.D.Christos

    Brand New Member

  • Members
  • 3 posts

Posted 07 May 2021 - 02:58 PM

Hello everyone, I am new to this forum!
So my name is Chris and I am currently developing a Reinforcement Learning Model in Python that uses Aspen Plus as the simulator and VBA as a means of connection between the two.

 

Learning Python, VBA and Aspen has been fun, however I have faced the following issue, whilst trying to make a PFR block through VBA.

As part of the needed input variables for creating the PFR, we need to select the reaction taking place inside the vessel, in my case its named "REAC" as it can be seen in the Figure1 presented below.

 

As you can see from the VBA code below, I tried inputting "REAC" as a Value to the "RXN_ID" module found through the Variable Explorer:

 

Sub Add_PFR3(Bcounter, R_Type, Op_Cond, T_PFR, L_PFR, D_PFR, Phase_PFR, Reac_PFR)

Pcounter = "P" & CStr(Bcounter) 'Internal Function to Count the Reaction Modules'
Dim AspenObject As IHapp
Dim Pfr As IHNode
'Chose ASPEN bkp File'
Set AspenObject = GetObject(ThisWorkbook.Path & "\" & Sheet1.Cells(1, 1)) 'Get the name of the bkp file'
Set Pfr = AspenObject.Tree.Elements("Data").Elements("Blocks")
Pfr.Elements.Add (Pcounter & "!RPlug") 'Add PFR in the Flowsheet'
Pfr.Elements(Pcounter).Input.Elements("TYPE").Value = R_Type
Pfr.Elements(Pcounter).Input.OPT_TSPEC.Value = Op_Cond
Pfr.Elements(Pcounter).Input.REAC_TEMP.Value = T_PFR
Pfr.Elements(Pcounter).Input.Elements("LENGTH").Value = L_PFR
Pfr.Elements(Pcounter).Input.Elements("DIAM").Value = D_PFR
Pfr.Elements(Pcounter).Input.Elements("PHASE").Value = Phase_PFR
Pfr.Elements(Pcounter).Input.RXN_ID.Value = Reac_PFR
End Sub
 
All the variable and code work, except for being able to add Reac_PFR = "REAC" as the Value of the RXN_ID.
 
I found by choosing the "REAC" reaction manually through the GUI, creates a subfolder in the RXN_ID As it can be seen from the Before and After pictures of the variable explorer in the Attached Figure2.
 
Hence I thought a solution was to create the node my self as a means of being able to choose the reaction, changing:
Pfr.Elements(Pcounter).Input.RXN_ID.Elements.Add("#0!REAC")
Pfr.Elements(Pcounter).Input.RXN_ID.Elements("#0").Value = Reac_PFR
 
 
However this doesn't seem to work.
I have gone through most of the Aspen documentation concerning the creation of block through VBA, but there seems to be no documentation on how to make this exact choice.
 
Your help would be much appreciated
Chris

 

Attached Files


Edited by A.D.Christos, 07 May 2021 - 03:11 PM.


#2 lnbsak

lnbsak

    Gold Member

  • Members
  • 53 posts

Posted 08 May 2021 - 02:55 AM

First few question

 

Why you would need VBA? When you can easily connect python and Aspen Plus.

 

Then why do you need PFR in VBA when Aspen Plus has already built PFR block?

 

If you want to create a custom user block why not you are creating with Aspen Custom modeler?

 

If you want to create a custom input file why not create and manipulate a text based (bkp) file?


Edited by lnbsak, 08 May 2021 - 02:56 AM.


#3 A.D.Christos

A.D.Christos

    Brand New Member

  • Members
  • 3 posts

Posted 08 May 2021 - 06:31 AM

First few question

 

Why you would need VBA? When you can easily connect python and Aspen Plus.

 

Then why do you need PFR in VBA when Aspen Plus has already built PFR block?

 

If you want to create a custom user block why not you are creating with Aspen Custom modeler?

 

If you want to create a custom input file why not create and manipulate a text based (bkp) file?

Good Morning and thank you for the interest,

 

I took your comments in mind and converted all my code to python and, connected it to aspen via COM.
In this case I am not creating a new custom PFR block, but i am inputting the needed variables for the Unit to operate.

 

The case example which we try to showcase is extremely simple, so we just need to create a PFR whose specification adhere to the theory.

 

However, my question still persists, how could we select "REAC" to be the RXN_ID of the PFR block through COM?

 

 

Christos
 



#4 A.D.Christos

A.D.Christos

    Brand New Member

  • Members
  • 3 posts

Posted 08 May 2021 - 06:36 AM

New Python Code for reference.
 

import os 
import win32com.client as win32

Pcounter = 1 #for enumeration of the Blocks
Reactor_Type = "T-SPEC"
Operating_Condition = "CONST-TEMP"
Temperature_PFR = 298 #K
Length_Reactor = 1 #m
Diameter_Reactor = 0.12433 #m
Phase_PFR = "L"
Reaction_in_PFR = "REAC"
aspen = win32.Dispatch('Apwn.Document')
aspen.InitFromArchive2(os.path.abspath('C:/Users/xxx/Desktop/PFR_CSTR/Reactor.bkp'))
aspen.Visible = True
Pcounter = 1
Pcounter = "P" + str(Pcounter)
Pfr = aspen.Tree.Elements("Data").Elements("Blocks")
Pfr.Elements.Add(Pcounter+"!RPlug")
Pfr.Elements(Pcounter).Elements("Input").Elements("TYPE").Value = Reactor_Type
Pfr.Elements(Pcounter).Elements("Input").Elements("OPT_TSPEC").Value = Operating_Condition
Pfr.Elements(Pcounter).Elements("Input").Elements("REAC_TEMP").Value = Temperature_PFR
Pfr.Elements(Pcounter).Elements("Input").Elements("LENGTH").Value = Length_Reactor
Pfr.Elements(Pcounter).Elements("Input").Elements("DIAM").Value = Diameter_Reactor
Pfr.Elements(Pcounter).Elements("Input").Elements("PHASE").Value = Phase_PFR
Pfr.Elements(Pcounter).Elements("Input").Elements("RXN_ID").Value = "REAC"
 
The bolded line is supposed to choose REAC as the Reaction inside of the the PFR however, similar to VBA it doesn't work

Edited by A.D.Christos, 08 May 2021 - 06:36 AM.


#5 ChinmoyBasak

ChinmoyBasak

    Gold Member

  • Members
  • 61 posts

Posted 04 September 2021 - 10:30 PM

Did you solve the problem?

#6 user9999999

user9999999

    Brand New Member

  • Members
  • 1 posts

Posted 20 June 2022 - 08:47 AM

Hey I have actually done exactly this kind of thing in my Bachelor thesis. [Thesis] https://github.com/Y...illationColumns

 

 

I would not advise you to use the code I wrote since its rubbish.... but I have created a new library which can be used to controle Aspen Plus via Python: https://github.com/Y...ython-Interface It is currently not completed yet but until 01.07.22 its finished. There is also some tutorials, documentation, cases and such.
You are able to edit the flowsheet, run, set values, get values, debug, automatically extract all outputs and insert all inputs into blocks and much more.
This library is quite extensive and maybe you will find it useful.





Similar Topics