Technical Article

Using Python for Subsea Simulation and Control

May 26, 2021 by Anthony King Ho

A deep dive into the internal of subsea simulation and control using the programming language Python.

Background of the Subsea Simulation Development 

Many traditional simulation packages are developed with Windows programming languages such as C#C++, and VB.NET. While these languages are powerful, they also present a few drawbacks and disadvantages when they are used for simulation purposes.

I have recently developed a full subsea simulation application with Python, in which the application was previously written in C#. In this article, I will describe the advantages that I learned from this development. But first, let’s take a look at the main differences between Python and a traditional programming language such as C#.

 

Head-to-Head Comparison of Python and C# 

Python is a free, open-source, and general-purpose programming language that powers websites such as Google search engine. It is an all-purpose taken, interactive, object-oriented, and high-level scripting language.

Python is designed to be easy to learn and for speedy development. It uses English keywords instead of using punctuation and maintains fewer syntactic constructions. It follows C and Java in most of its features and can be run on different platforms such as Windows and Linux.

C#, although recently has been open-sourced, was developed by Microsoft as part of the .NET framework, which comes with a license. C# is a general-purpose programming language designed for the Common Language Infrastructure (CLI). It is a language that closely follows the traditional C and C++ constructs.

Both Python and C# are object-oriented, have a huge set of library support, and support garbage collection. While Python is fully object-oriented, developers can also take advantage of Python to write procedural code.

In comparison, Python is a dynamically interpreted language that is processed at runtime by its interpreter, whereas C# is a statically typed compiled language that was translated into machine code before execution. That means the C# software will perform better in applications such as games but will not beat Python’s fast development time.

 

A Deep Look into Python

Python can be very portable and easy to adopt because the language has all high-level programming capabilities. It is fast and intuitive to develop an application with Python. For a simple example, the code can be written in C# in Figure 1 to ask and print a username.

 

Figure 1. Simple C# Code

 

In Python, the code in Figure 1 can be reduced to just 2 lines of code in Figure 2.

 

Figure 2. Simple Python Code

 

As you can see in the Python example, there is no need for type declarations and imports. Also, there is no need for braces {} in Python, indentation will do the trick. Since Python is an object-oriented language, it is an excellent candidate for developing a subsea simulation package since every subsea equipment can be represented as an object. 

For example, a subsea valve can be defined as an object. It is then labeled with a class of valve objects that determines its properties and any logic sequences that can manipulate it. The logic sequence is known as a method. For example, a property of a valve identifies if it is a fail-open or fail-close valve, and a method can be defined for opening or closing the valve.

 

Building Subsea Objects with Python 

Valve instances are instantiated from the valve class. Different valve instances will have different properties, and the method can be called to operate that particular instance and will not affect other instances.

Different valve instances can also communicate using interfaces called messages, such as current valve status, for interlock purposes. Because of these features, the benefits of using object-oriented language include code reusability, scalability, and efficiency.

Inheritance is also another quick way to create objects. For example, a fast choke can be inherited from a traditional choke with additional attributes (properties) and behavior (methods). That is, the fast choke inherits all the properties and methods from a traditional choke plus additional ones that only pertained to the fast choke.

To create a Valve class, Python provides a fast and easy way to create the class, as illustrated in Figure 3.

 

Figure 3. A Valve class and its implementation

 

After running this piece of code, it yields the result in Figure 4.

 

Figure 4. Result of running the Valve Class implementation

 

Python and PLCs 

To write to the PLC, the programmer only needs to fill in the PLC’s IP address and the register addresses of the valve status (valve_status_plc_addr) and pressure (valve_pressure_plc_addr).

As you can see from the valve example, I used pylogix in my project. Pylogix is a free library that allows the application to easily read/write values from tags in Rockwell Automation Logix based PLCs such as CompactLogix and ControlLogix over Ethernet I/P using Python.

Python2 and Python3 are both supported by pylogix. An alternative library called pycomm3, which is a Python 3 fork of pycomm, is also a native Python library for communicating with PLCs using Ethernet/IP.

For Siemens S7 PLC, using Python wrapper for python-snap7 will achieve the same goal. These free libraries can be found and downloaded from GitHub, which is an open-source repository hosting service, like a cloud for code.

 

Graphical Interface for Python

To build a graphical interface for the simulation package, I used Python Tkinter graphical user interface (GUI) libraries. Tkinter is a free and standard GUI library for Python. The library provides a quick and easy way to create a powerful object-oriented graphical user interface.

There are only a few steps to create a GUI application. First, import the Tkinter module. Next, create the GUI application main window. 

Then add widgets to the GUI application. Finally, enter the main event loop to take action against each event triggered by the user or another event. Common widgets include a button, canvas, frame, label, checkbox, menu, message box, etc.

As a result, a simulation for a typical subsea field was created, and the GUI of a subsea production tree is shown in Figure 5. The GUI for each Production Tree contains various object instances, such as valve, choke, pressure/temperature transmitter, multi-phase flowmeter (MPFM), sand detector, and downhole gauge for pressure and temperature (DHPT), etc. These objects were built using the same process as building the valve object.

 

Figure 5. The GUI of a typical subsea production tree

 

The diagram depicts one Production Tree. Different Production Trees can be chosen using the dropdown box on the top left corner. In this prototype, I created 100 Production Trees with minimum effort as the production trees are identical in structure. Furthermore, the number of Production Trees in the simulation package did not significantly impact the overall simulation performance.

Popups are used when the user clicks on the object instances. 

Replication of subsea trees is straightforward by copying one tree to another and changing the object instance PLC addresses. Customizing the simulation package to a specific subsea field will simply involve re-arranging the objects and changing the equipment labels and PLC addresses, etc.

In conclusion, when compared to writing the simulation package in C#, switching to Python reduced the time to develop the package by about 70% because of the simplicity of Python and its huge set of free library support.

When developing the simulation in C#, I also resorted to using open platform communications (OPC) application programming interface (API) within the program to communicate with the PLC. This unavoidably added time and cost to the development.

While C# still has a ton of benefits when developing applications in Windows, Python provides a fast alternative to developers for writing applications like the simulation package described in this article. 


 

Featured image used courtesy of Siemens AG

2 Comments
  • E
    Edemilso Ilha May 28, 2021

    Thank you so much by this article very well writed.
    Edemilso Farias Ilha
    Brazil

    Like. Reply
  • pnachtwey May 28, 2021

    I am a Python user but the article is biased.  When I program in Python I must import numpy, scipy and matplotlib as a minimum.
    I often must import even more like tkinter, functools, hmi_widgets jsonfile and menubar.

    While I do agree Python is a great tool I think the article must be honest.
    What I think is great is jupyter lab and jupyter hub.  These interfaces make Python usable for the average person.
    There are a few good forums where one can get answers for Python, Jupyter Lab and Jupyter hub.
    A good site to watch is
    https://www.youtube.com/channel/UC2GuY-AxnNxIJFAVfEW0QFA
    http://apmonitor.com/
    I am not affiliated with apmonitor.  I just think they do great work in the area of optimization and simulation.

    Like. Reply