Skip to content

Example implementation of the globalMOO API applied as a multi-variable multi-objective control system for a chemical reactor.

License

Notifications You must be signed in to change notification settings

globalMOO/gmoo-application-reactor

Repository files navigation

globalMOO Application - Chemical Reactor Simulator

MIT License

This repository demonstrates the capabilities of the globalMOO optimizer using a chemical reactor simulation as an example application. The reactor simulator models a Continuous Stirred Tank Reactor (CSTR) for Steam Methane Reforming (SMR), but the main purpose is to showcase how globalMOO can be used for multi-objective optimization and control.

Reactor Simulation Example

How globalMOO Works with the Reactor

The globalMOO optimizer functions as an intelligent control system that determines the optimal input parameters to achieve desired output conditions. The diagram below provides a conceptual view of how the optimizer interacts with the reactor system:

Reactor Optimization Loop

This simplified visualization shows some key inputs (outflow valve position, heat input, feed rates) and outputs (H₂ flow, CH₄ conversion, H₂ yield, temperature). In reality, the model handles 12 input parameters and 36 output parameters, making this a complex multivariable optimization problem that globalMOO efficiently solves.

The globalMOO optimizer:

  1. Evaluates the current state of the system
  2. Determines optimal input adjustments to reach target outputs
  3. Applies these adjustments while respecting physical constraints
  4. Iteratively refines the solution until convergence

Overview

The globalMOO optimizer is demonstrated through:

  1. Single-step inverse solution - Find reactor inputs that produce specific outputs. This is an inverse solution that determines the required input adjustments to achieve a set of desired objectives for a specific time increment.
  2. Multi-timestep sequential optimization - Use the optimizer as a dynamic multivariable, multiobjective controller over multiple simulation periods.

Repository Structure

  • src/gmoo_reactor/smr_cstr_simulator.py - Core reactor simulator implementing realistic chemical kinetics
  • examples/reactor_api.py - Single-step inverse solution demonstration
  • examples/reactor_sequential_api.py - Multi-timestep sequential optimization and control
  • src/gmoo_reactor/helper_functions.py - Utility functions for handling simulator I/O
  • tests/ - Unit tests for validating simulator behavior

Chemical Reactor Model

The simulator models a Steam Methane Reforming (SMR) CSTR with the following key features:

  • SMR reaction: CH₄ + H₂O ⟶ CO + 3H₂
  • Water-Gas Shift (WGS): CO + H₂O ⟶ CO₂ + H₂
  • Realistic reaction kinetics including temperature-dependent rate constants
  • Heat and mass transfer with external heating
  • Dynamic pressure control system with an inner loop PID controller
  • Dynamic temperature and concentration modeling

The pressure control system uses a dynamic inner loop PID controller that operates at a faster timescale than the outer (globalMOO) controller. This creates a more challenging control problem with nonlinear and dynamic behaviors, yet globalMOO is still able to stably converge and control the system, demonstrating its robustness.

Key Model Parameters

  • 12 Input parameters (feed rates, temperatures, pressures, etc.)
  • 36 Output parameters (concentrations, temperature, reaction rates, etc.)
  • Simulation handles both short-term and long-term (10x duration) results

Single-Step Inverse Solution

The reactor_api.py script demonstrates how globalMOO can solve the inverse problem of determining what input parameters are needed to achieve specific output conditions. This implements the optimization loop shown in the diagram above:

# Create a model
model = client.execute_request(CreateModel(
    name=MODEL_NAME,
    description="Chemical reactor control system simulation"
))

# Create a project
project = client.execute_request(CreateProject(
    model_id=model.id,
    name=PROJECT_NAME,
    input_count=INPUT_COUNT,
    minimums=MIN_VALUES,
    maximums=MAX_VALUES,
    input_types=INPUT_TYPES,
    categories=[]
))

# Run optimization loop to find inputs that produce target outputs
for iteration in range(max_iterations):
    inverse = client.execute_request(SuggestInverse(
        objective_id=objective.id
    ))
    
    next_output = cstr_final_state_wrapped(inverse_clamped)
    
    inverse = client.execute_request(LoadInversedOutput(
        inverse_id=inverse.id,
        output=next_output
    ))
    
    if inverse.should_stop():
        break

Key Features:

  • Uses globalMOO to find inputs that will produce target reactor conditions
  • Optimizes a high-dimensional problem with physical constraints
  • Provides detailed error metrics and convergence status

Multi-Timestep Sequential Optimization

The reactor_sequential_api.py script demonstrates a more advanced use case, where globalMOO acts as a dynamic multivariable controller across multiple timesteps:

# Initialize the sequential controller
controller = SequentialReactorController(
    epoch_duration=10.0,
    timesteps_per_epoch=7,
    trial_id=trial_id,
    use_optimization=True
)

# Define a sequence of target conditions to achieve
target_sequences = [
    {
        'T': 971.0,        # Target temperature
        'C_H2': 150.3,     # Target H2 concentration
        'P_total': 30.0,   # Target pressure
        'F_H2_out': 0.4637 # Target H2 outflow
    }
]

# Run the simulation to reach each target in sequence
controller.run_target_sequence(
    initial_input=input_vector,
    target_sequences=target_sequences,
    epochs_per_target=1,
    use_optimization=True,
    objective_types=objective_types,
    max_iterations=10
)

Key Features:

  • Uses optimization to drive the reactor toward target conditions over time
  • Maintains physical continuity between timesteps
  • Enables complex multi-objective control scenarios
  • Handles both standard and extended-duration objectives

Best Practices for Controller Implementation

When using globalMOO as a controller, the best practice is to:

  1. Treat all input variables as objectives
  2. Fix/clamp variables associated with initial conditions at the outcome conditions of the previous step
  3. For objectives where no constraints or satisfaction criteria are imposed, set wide tolerance bounds (as seen in the percentage bounds tolerances in the code)
  4. Implement lookahead prediction by computing the reactor state at a future time point (10x the current duration in this implementation) to improve stability over time

The lookahead feature is particularly important for stability - by allowing globalMOO to optimize not just for the immediate next state, but also for how the system will behave in the future. The lookahead time point (10x duration in our implementation) can be adjusted to balance between short-term responsiveness and long-term stability.

This approach ensures physical continuity between timesteps and allows the optimization to focus on variables that can actually be controlled, while respecting the physical state of the system and avoiding oscillations.

Testing

The simulator includes comprehensive unit tests in the /tests directory that verify:

  • Proper initialization of parameters
  • Reaction kinetics and equilibrium calculations
  • System ODEs and output vector calculations
  • Pressure control algorithm
  • Numerical stability under various conditions
  • Physically realistic behavior in edge cases

Run the tests with pytest:

pytest tests/

Visualization

Both examples include visualization capabilities to show the reactor's behavior over time:

# Plot the results
controller.plot_results()

The plots include:

  • Temperature profiles
  • Species concentrations
  • Partial pressures
  • Reaction rates
  • Heat flow
  • Flow rates for each species

Getting Started

  1. Clone this repository
  2. Install dependencies using one of these methods:
    # Using pip with requirements.txt
    pip install -r requirements.txt
    
    # Or install as a package
    pip install -e .
  3. Set up your globalMOO API key (required):
    # Copy the template .env file and add your API key
    cp .env.dist .env
    # Edit the .env file with your API key
    Get your API key from: https://app.globalmoo.com/accounts/settings
  4. Run example scripts:
    # Run the single-step inverse solution example
    python examples/reactor_api.py
    
    # Run the multi-timestep controller example
    python examples/reactor_sequential_api.py

For more detailed setup instructions, see docs/SETUP.md.

Note: This demo requires a globalMOO Standard tier subscription as it uses up to 12 input variables (Lite tier only supports up to 10).

Note on globalMOO

The globalMOO optimizer is a multi-objective optimization library that is demonstrated in this repository but is not included in it. The examples show how globalMOO can be used for various optimization tasks, with the chemical reactor serving as a complex application example.

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

Example implementation of the globalMOO API applied as a multi-variable multi-objective control system for a chemical reactor.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages