Skip to main content
1-Visitor
March 22, 2015
Solved

"Modularization" of the program

  • March 22, 2015
  • 4 replies
  • 12775 views

Hello all,

I work on modeling / simulation of some thermal energy system in Mathcad 15. The existing model works on the equation oriented approach, at the end all the mass and energy balance equations are gathered in the given-find block. Now I want to "modularize" the program - the components of the system are to be black-boxes, which have inputs and outputs (more precisely input and output streams, which are vectors of parameters). I was thinking about defining modules as functions, but the problem is I can not use boolean equal sign inside a function, that I need, since the whole system of equations / functions can only be solved numerically, in case of Mathcad in Given-Find block. So, every black-box would be expressed as

Function(X,U) = Y

where

X - vector of input streams (if particular black-box has more than one input stream this vector will often contain nested vectors)

U - vector of internal parameters

Y - vector of output streams (if particular black-box has more than one output stream this vector will often contain nested vectors)

As each module is connected to some other by stream(s), output stream of one module can be at the same time inlet stream of the subsequent module. For example, output stream of the superheater is simultaneously input stream of the turbine.

I would be very grateful to any ideas how to resolve the above described problem. Thanks in advance for help.

Piotr

Best answer by HarveyHensley

I think all you need to do is parameterize the solve block for a unit. The parameterized function that is created can then be used in a program function. You can build program functions for each unit and then a master function for a process loop.

I have built a process flowsheet similar to what you are attempting. You may get some ideas by going to my blog and selecting the "process simulation" category on the right side of the page.

4 replies

12-Amethyst
March 23, 2015

I think all you need to do is parameterize the solve block for a unit. The parameterized function that is created can then be used in a program function. You can build program functions for each unit and then a master function for a process loop.

I have built a process flowsheet similar to what you are attempting. You may get some ideas by going to my blog and selecting the "process simulation" category on the right side of the page.

23-Emerald I
March 23, 2015

It is possible to define a solve block as a function

pw_011-VisitorAuthor
1-Visitor
March 23, 2015

Thanks for reply. Harvey, do you think about the same as Fred? I suppose yes. I start dealing with it. One problem I see now are variables names, that in every given-find block must be unique, because they are globally visible. When dealing with functions, variables have local names, so there is no problem. Is there any way to solve this problem?

Harvey, I have visited your blog, it is very interesting, however I have not find any Mathcad files or pdfs. I would like to see the process flowsheet that you mentioned above - is it possible?

Also, I would like to ask you how would you model thermal energy system (combined cycle in my example). The purpose of the model is mass and energy balance. There are no chemical reactions and change of working media composition (except gas turbine combustion chamber). The working media are air, methane, combustion / flue gas, and water / steam. Air and combustion / flue gas are modeled as 5 components vectors (N2, O2, CO2, Ar and H2O(g) fractions). I have properties libraries for all the working media (dll files). Philosophy that I figured out is shortly described in my first post (black-boxes (modules) linked witch each other by streams). Every mass stream is characterized by 3 independent parameters of state: mass flow rate, pressure, and specific enthalpy, expressed as 3 element vector. In the system there are also heat rate streams and mechanical work streams. When using EO approach it takes too much time to re-model the program if configuration of the system is changed - that is why I want to change the "philosophy" of modeling, for example by closing the units in functions or solve blocks. Can you propose some elegant, coherent "mind model"? From your experience, what would you advise? I will be very grateful for your help.

If anybody has something to add, please feel free

Piotr

12-Amethyst
March 24, 2015

Piotr,

I don't think my example program is quite what you are looking for after all. I used a lot of Prode Properties functions for one thing. Also, I didn't use any solve blocks it appears. But I think the direction you plan to go looks sound, and in fact I might want to see if it would work for my example also. Here's how your case would work.

You have a solve block for a unit.

Given

equations

At the end of the block you have:

Unit-A(X,U) := Find(Y)

This just establishes the function Unit-A, it doesn't actually solve it.

So anytime you want the matrix Y from that type of unit, you have a statement like:

Y1 := Unit-A(X1,U1) which gives you the desired numerical matrix, Y1, for the particular values, X1 and U1.

X and Y are probably matrices because each stream has composition flows, and the other state variables. Each column in X and Y would represent a stream. You may be able to get by without nested arrays unless you want to contain multiple phases in a single stream.

I don't think there will be a problem with the global vs. local names provided you are careful with the new guess values prior to each solve block definition.

If you need additional programming for say a loop, you can then use the Unit-A() function inside the program function.

Hope this helps.

pw_011-VisitorAuthor
1-Visitor
March 28, 2015

Harvey,

I was wonder if it is possible to solve a unit expressing its function as

Unit-A(X,Y,U) = 0

And how would in this case Given-Find structure look like? Unit-A(X,Y,U) = Find(0)? Maybe there is some other way to find the roots of this function?

Piotr

12-Amethyst
March 30, 2015

There was still a problem with my previous file. There needs to be one variable in the Find() arguments for each equation. If you have more, it doesn't know which ones to keep constant. In the original version U was changed by the Find operation. The second, corrected method solves for only the Y's so the solution is different.

This has implications for what you eventually want to do...solve forward and backward. You will have to somehow indicate which variables are to be found and which are to be left as specs or input. That could get messy.

Note that sometimes you need to change the guesses for a variable that is to be solved. You are allowed to have that variable on both sides of the Find definition.

R(X,Y) := Find(Y)

An example where this can be used is for a reactor where Y is a vector of flow rates. In order to provide a good initial guess, Y needs to be changed according to the feed rate or else the problem might not converge.

pw_011-VisitorAuthor
1-Visitor
April 2, 2015

I have noticed that your previous file was broken, but since I was already working on new implementation, it did not bother me. I think about the final concept, and after all, it is not far from the presented one. I will post test case of a single pressure HRSG simulation, I already have written 3 needed functions, and they seems to work bidirectional, which is very important to me. If this concept will work (should be), it greatly shorten time of building simulation models, and they will be less error-prone as well. I will post the file (whether working or not) in a few days, when I find some time to complete it. I hope for an interesting discussion, as before, Harvey. Maybe you will also take advantage from this concept in your process flowsheeting.

Piotr

12-Amethyst
April 3, 2015

Piotr,

You will probably need to specify which of the variables are specs by adding constraint equations. For example, say you have three elements in each of X (the inlets) and Y (the outlets). Also assume you have three equations (e.g. material and energy balances). If you include both X and Y in the Find, eg (Find(X,Y)), you will have 6 unknowns. The program will not know which to adjust, but it appears that doesn't result in termination. It does result in an incorrect result.

To correct the problem, you need to create an equation for each spec variable. Lets assume that the 3rd element of X and the 1st and 2rd elements of Y are specs. Then you would add

X[3 = spec1 Y[1 = spec2 Y[2 = spec3

Now you have 6 equations (3 original + 3 spec constraints) and 6 variables. This will allow you to use Find(X,Y) and it should only change X[1, X[2 and Y[3. See what I mean when I said this can get messy?

You said in your last note that you are getting forward and backward results. Be sure to check to see that the results haven't changed some of your spec variables.

I will look forward to your progress. This is interesting.