### Creating your first mathematical model in EMSO

Hi everyone, in this occasion I am going to talk about some basic concepts involved in the process of building models in EMSO. If you don’t know what I’m talking about, I recommend you to read my previous post (*Introducing EMSO – An open-source software for modelling, simulation and optimization*).The idea behind the current post is to illustrate the process of building mathematical models in EMSO.

To start building models in EMSO, you need to create a file. So, just click on *File>>New File *(you can also click on the* New File* icon available on the standard toolbar). Once you do that, the following window appears:

in which you have to select the file type you want to create. As you can see in the previous figure, there are 4 possibilities, so let’s describe each one of them:

. Allows you to create a model,*Model**i.e.*, a mathematical abstraction comprising a set of variables, parameters and equations which altogether permits the user to describe a system (e.g. a phenomenon, equipment, etc.). This kind of file cannot be simulated in EMSO.: In contrast to Model, this allows you to create simulable entities,*FlowSheet**i.e.*, to create either simple models or complete process flow diagrams which can be simulated. Both*Model*and*FlowSheet*files have the same extension in EMSO,.**.mso*. Both Model and FlowSheet are code-based files. Each of them, as we will see, has a specific structure (the well-known EMSO entity concept), so, if you select whichever of them it will show you a basic template illustrating its use. In case that you don’t want to use any of them, you can select a blank template by choosing*Empty**Empty*.. Allows you to create block-oriented simulable model by simple drag-and-drop and linking operations. This kind of file is the*Diagram**raison d’être*of the*Palette*tab mentioned in my EMSO previous introductory post. The extension of this file type once saved is.***.pfd**

So let’s suppose we want to create a simple model in EMSO for describing the filling in (and filling out) of a cylindrical tank. For the sake of simplicity, suppose that the equations of the model have been deduced previously in the paper but we want to write our model in EMSO. So you have deduced that the model can be represented by the following set of equations:

Mass balance:

Cross-sectional area:

Outlet flow:

where,

: inlet and outlet flowrates, in

: tank level, in

: tank diameter, in

: constant of the outlet valve, in

When creating a model in EMSO, the first we have to do is to distinguish which of the previous symbols denote variables and which ones, parameters. In this part, EMSO can be a little tricky, so we can use the following rule:

* Variables*. Use variables for any magnitude that needs to be computed in the model but that only depends on other variables. In this sense, if you have a parameter of the original model which depends on a variable, you should create it in EMSO as it were a variable.

* Parameters*. In the same sense, use parameters for any magnitude that stays constant along the computations or that depends only on other parameters.

According to this we can differentiate the following variables and parameters:

Variables:

Parameters:

Once done the previous work, let’s start to work in EMSO:

- Open EMSO and create a Model, assign it a name (in this case we will use
*simpleTank.mso*). Once done, you will see the following template (click on the image to zoom): - From where, we can establish different facts:

*Comments*are shown in green color, are enclosed between**#***and***#**, and can include multiple lines.*Strings of text*are shown in purple between quotes (“).- The modelling language keywords are shown in blue.
- Regular text is shown in black.
- Almost all lines must be finished with a semicolon (;).

- Models in EMSO are constituted by several sections denoted by the blue capitalized words PARAMETERS, VARIABLES, EQUATIONS, and INITIAL. Each end of section is limited by the beginning of the next one,
*i.e.*,*end-words*between sections are not required. The**word***end*is reserved for establishing the end point of the Model entity. - To declare parameters and variables, we must use the syntax , where refers to a base type in EMSO, which basically denotes a kind of magnitude that has as attributes a maximum value, a minimum value, units, and a default value, just for naming a few. You can find more info in the
guide available in the EMSO program folder. As you can see in the previous image, in the template appear some base types (*EMSO_QuickRef.pdf**length*,*area*, and*volume*). In case you don’t know what base type to use, you can always use theand*Real*base types and assign units to them, after the*Integer*attribute, using the syntax , remeber to separate the attributes by using commas. As we don’t know yet the base types, we can use the*Brief*base type for all the variables and parameters of our model assigning them their respective units.*Real* - After erasing all the comments, our model would look like this:
- Notice that EMSO counts with some special funcions to calculate temporal derivatives () and the square roots (). You can find other EMSO Built-In functions in the
guide.*EMSO_QuickRef. pdf*

I hope this post had been helpful for those who wants either to learn or to revisit EMSO. I my next post I would like to talk in-deep about the Model entity we just exemplified in here.

Best regards,

Jonathan