PID Controllers and InfluxDB: Part 1 - Background

Navigate to:

In the fast-evolving chemical industry, maintaining precise control over Chemical reactors like a continuous stirred-tank reactor (CSTRs) is paramount to ensuring optimal performance and product quality. This blog post delves into integrating advanced data tools and techniques to achieve this control. We’ll explore how to leverage InfluxDB, Kafka, and Faust streaming, along with Telegraf, to effectively model and manage a CSTR and its PID controller (Proportional-Integral-Derivative Controller). By the end of this series, you’ll gain insights into creating a robust data pipeline that enables real-time monitoring and control, enhancing your process efficiency and reliability.

To effectively model and manage a CSTR and its PID controller, we utilize a suite of powerful tools. InfluxDB is a time series database optimized for high-write and query loads, perfect for handling the real-time data generated by our chemical processes. Kafka is a distributed streaming platform that allows us to build real-time data pipelines, ensuring seamless data flow between our system components. Faust is a Python stream processing library providing a high-level API for building complex streaming applications, essential for real-time data transformation and analysis. Lastly, Telegraf is an agent for collecting, processing, and writing metrics, which acts as a bridge, feeding our real-time data into InfluxDB. Together, these tools create a comprehensive ecosystem for monitoring and controlling chemical processes with precision and efficiency. The corresponding repo for this blog post can be found here.

Screenshot 2024-08-28 at 11.16.39 AM

Architecture drawing of the project described in this blog. A digital twin of a CSTR produces data, which Kafka collects. Faust acts as a PID controller. Telegraf subscribes to a Kafka topic to send sensor and control data to InfluxDB. Grafana monitors the reactor and cooling jacket.

What is a CSTR?

A Continuous Stirred-Tank Reactor (CSTR) is a common type of chemical reactor used in industrial processes. It maintains a uniform composition throughout the reactor by continuously mixing the reactants and products. The primary advantage of a CSTR is its ability to handle large-scale reactions with consistent quality, making it ideal for processes that require steady-state operation.

To model a CSTR, we use a set of differential equations that describe the change in concentration of reactants and products over time. A Proportional-Integral-Derivative (PID) controller is used to maintain the desired setpoint in a system by adjusting control variables, usually a cooling jacket. The PID controller calculates an error value as the difference between a desired setpoint and a measured process variable and applies corrections based on proportional, integral, and derivative terms.

One typical example of CSTR usage is in the production of ethylene glycol, a key component in antifreeze and polyester fibers. The production process involves the hydrolysis of ethylene oxide in the presence of water. In this example we can think of ethylene oxide as chemical A and water as chemical B. A schematic diagram of a CSTR from APMonitor.

You can learn about the specific math to model this type of reactor here. But essentially, the CSTR model is described using ordinary differential equations (ODEs) that account for the mass and energy balances in the reactor where:

  • Ca is the concentration of reactant A leaving the reactor.
  • Cb is the concentration of reactant B leaving the reactor.
  • Caf is the concentration of reactant A into the reactor, a constant.
  • Tf is the temperature of reactant A into the reactor, a constant.
  • Tc or u is the temperature of the cooling jacket or control variable, which is determined by the PID controller.

Essentially, many chemical reactions in a CSTR are exothermic and produce heat over time, as in the case of the production of ethylene glycol. As the reaction progresses and heat is produced, the temperature of the reactor increases, further accelerating the reaction and potentially producing even more heat. This temperature needs to be controlled for many reasons:

  1. Safety
    • Uncontrolled heat production can lead to runaway reactions, where the reaction rate increases uncontrollably, leading to dangerously high temperatures and pressures.
    • This can cause equipment failure, release of hazardous substances, or even explosions.
  2. Product Quality
    • Many reactions have optimal temperature ranges that maximize the desired product yield and selectivity.
    • Deviations from this temperature range can lead to the formation of unwanted byproducts, reducing the overall quality and yield of the desired product.
  3. Process Efficiency
    • Maintaining the reaction within a specific temperature range ensures that resources are used efficiently.
    • Excess heat might require additional cooling, increasing operational costs, while insufficient temperature control can slow down the reaction, reducing throughput.

    So, the PID controller continuously calculates the error between the desired temperature (setpoint) and the actual reactor temperature and adjusts the cooling jacket temperature to minimize this error. Setpoints are changed in a CSTR and PID control system for several reasons:

    • Maximize Yield and Quality: Different reaction stages might have different optimal temperatures for maximum yield of product quality. Adjusting the setpoint can help maintain optimal conditions throughout the process.
    • Some reactions proceed faster at higher temperatures while others might produce unwanted byproducts if the temperature is too high. Adjusting the setpoint helps optimize these reaction kinetics.
    • Safety: Prevent runaway reactions and explosions.
    • Process Transitions: In processes involving multiple steps or phases, each step might require a different temperature for optimal performance. Setpoints enable smooth transitions.
    • Research and Development: Changing setpoints allows researchers to study the effects of temperature on reaction kinetics and yields when developing new processes or optimizing existing ones.

Project overview and requirements

The corresponding repo for this blog post can be found here. It consists of three directories:

  • without_kafka: This directory contains an example of how to model a CSTR and a PID controller without using InfluxDB, Kafka, or Telegraf with Python.
  • cstr_kafka_influxdb: This directory contains an example of modeling a CSTR using Kafka and Python.
  • cstr_kafka_influxdb_docker: This directory contains an example of modeling a CSTR that is dockerized. This is the recommended environment for you to try this out independently.

Each directory contains a README.md with instructions for how to run these demos yourself.

This project provides a CSTR Digital Twin that mimics executing a reaction (A → B) under provided temperature setpoint conditions. We produce data that looks like this:

The result of the CSTR digital twin. The top graph shows the concentration of reactant A (Ca) over time; the middle graph shows the temperature of the reactor (T) and the set point (setpoint), and the bottom graph shows the temperature of the cooling jacket (u).

Conclusion

This blog post helps set the stage for creating a digital twin of a CSTR and using Kafka and Faust as a PID controller while storing temperature data in InfluxDB. We use Telegraf to get data from the Kafka topic. Then, we use Grafana to visualize the results. As always, get started with InfluxDB v3 Cloud here. In the next post, we’ll cover how to run the project, dive into the architecture and logic, and talk about some of the pros and cons of the selected stack. If you need help, please contact us on our community site or Slack channel. If you are also working on a data processing project with InfluxDB, I’d love to hear from you!