5.2. Supplementary material: data for parmest tutorial#

Created by Kanishka Ghosh, Jialu Wang, and Prof. Alex Dowling at the University of Notre Dame.

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from pyomo.environ import *

# Define the directory to save/read the data files
data_dir = https://raw.githubusercontent.com/ndcbe/optimization/main/notebooks/data/parmest_tutorial/'

The purpose of this notebook is to generate data files used for the parmest tutorial.

5.2.1. Reaction Kinetics Example#

The following code calculates the concentrations \(C_A\), \(C_B\), and \(C_C\) as a function of time for experimental conditions \(T\) and \(C_{A0}\) given model parameters \(A_1\), \(A_2\), \(E_1\), and \(E_2\). See the parmest tutorial notebook for a full description of the mathematical model.

def kinetics(A, E, T):
    ''' Computes kinetics from Arrhenius equation
    
    Arguments:
        A: pre-exponential factor, [1 / hr]
        E: activation energy, [kJ / mol]
        T: temperature, [K]
        
    Returns:
        k: reaction rate coefficient, [1 / hr]
    
    '''
    R = 8.31446261815324 # J / K / mole
    
    return A * np.exp(-E*1000/(R*T))

def concentrations(t,k,CA0):
    '''
    Returns concentrations at time t
    
    Arguments:
        t: time, [hr]
        k: reaction rate coefficient, [1 / hr]
        CA0: initial concentration of A, [mol / L]
    
    Returns:
        CA, CB, CC: concentrations of A, B, and C at time t, [mol / L]
    '''
    CA = CA0 * np.exp(-k[0]*t);
    CB = k[0]*CA0/(k[1]-k[0]) * (np.exp(-k[0]*t) - np.exp(-k[1]*t));
    CC = CA0 - CA - CB;
    
    return CA, CB, CC
CA0 = 1 # Moles/L
k = [3, 0.7] # 1/hr

t = np.linspace(0,1,51)
CA, CB, CC = concentrations(t,k,CA0)
plt.plot(t, CA, label="$C_{A}$",linestyle="-",color="blue")
plt.plot(t, CB, label="$C_{B}$",linestyle="-.",color="green")
plt.plot(t, CC, label="$C_{C}$",linestyle="--",color="red")
plt.xlabel("Time [hours]")
plt.ylabel("Concentration [mol/L]")
plt.title("Batch Reactor Model")
plt.legend()
plt.show()
../../_images/9387db91a99552703ecb8f9d267637a95d2c91224e045f967041a35b4197d499.png

5.2.1.1. Simulating experimental data#

We start by simulating experiments in the batch reactor. Here is the experimental procedure:

  1. Specify the reaction temperature

  2. Load the reactor with species \(A\) concentration of \(C_{A0}\) mol/L at time \(t=0\).

  3. Measure the concentrations \(C_A\), \(C_B\), and \(C_C\) at times 0.0, 0.125, 0.25, 0.375, 0.5, 0.625, 0.75, 0.875, and 1.0 hours. These experiments are subject to measurement uncertainty.

Below is Python code to generate data for these simulated experiments.

## General setup

# declare "true" model parameters
A = np.array([200, 400]) # 1/hr
E = np.array([10, 15]) # KJ/mol

# declare time for parameter estimation
t_est = np.array([0.0, 0.125, 0.25, 0.375,0.5,0.625, 0.75, 0.875, 1.0])
n = len(t_est)

# declare measurement uncertainty
stdev_m_error = 0.1

# define function to plot
def plot_exp(k, CA0, data, text):
    ''' 
    Plot concentration profiles
    Arguments:
        k: kinetic parameters
        CA0: initial concentration
        data: Pandas data frame
        text: plot title
    
    '''
    # evaluate models
    t = np.linspace(0,1,51)
    CA, CB, CC = concentrations(t,k,CA0)
    
    # plot model-generated and 'experimental' data
    # symbols for 'experimental' data
    # solid and dashed lines for model-generated data
    plt.plot(t, CA,label="$C_{A}$",linestyle="-",color="blue")
    plt.plot(data.time, data.CA, marker='o',linestyle="",color="blue",label=str())
    plt.plot(t, CB, label="$C_{B}$",linestyle="-.",color="green")
    plt.plot(data.time, data.CB, marker='s',linestyle="",color="green",label=str())
    plt.plot(t, CC, label="$C_{C}$",linestyle="--",color="red")
    plt.plot(data.time, data.CC, marker='^',linestyle="",color="red",label=str())
    plt.xlabel("Time [hours]")
    plt.ylabel("Concentration [mol/L]")
    plt.title(text)
    plt.legend()
    plt.show()

# define function to accept set of T and C_A0 values as input and generate true parameters and data with noise
def gen_data(T_vals,CA0_vals,t_est,std_error,n):
    '''
    function to accept set of T and C_A0 values as input and generate and store data with noise to csv files
    Arguments:
        T_vals: list of temperature, K
        CA0_vals: list of inlet concentrations of A, mol/L
        t_est: time steps for experiment, hrs
        std_error: uncertainty added to data
        n: number of data points = length of t_est
    Return:
        filename_list: list of filenames
    '''
    filename_list = []
    # experiment number counter
    counter = 0
    for T in T_vals:
        for CA0 in CA0_vals:
            counter += 1
            # define "true" kinetic parameters at this temperature
            k = kinetics(A, E, T)

            # evaluate model to generate data
            CA_exp, CB_exp, CC_exp = concentrations(t_est,k,CA0)

            # add measurement error
            # Note: adding random errors to concentration can result in negative concentrations
            # To ensure the data seems realistic, we need to ignore negative concentration data points
            
            CA_exp += np.random.normal(0, std_error, n)
            CB_exp += np.random.normal(0, std_error, n)
            CC_exp += np.random.normal(0, std_error, n)
            
            for i,CA_exp_i in enumerate(CA_exp):
                if CA_exp[i] < 0.0:
                    CA_exp[i] = 0.0
                if CB_exp[i] < 0.0:
                    CB_exp[i] = 0.0
                if CC_exp[i] < 0.0:
                    CC_exp[i] = 0.0

            # store dictionary of 'experimental' data in Pandas dataframe
            data = pd.DataFrame({"time":t_est,"T":T,"CA0":CA0,"CA":CA_exp,"CB":CB_exp,"CC":CC_exp})
            # generate filename to store data in
            file_name = data_dir + '20210609_data_exp{}.csv'.format(counter)
            # store data in csv file segregated by 'experiment' number
            data.to_csv(file_name)
            # adding file name to list of file names to be used later to read-in data from the csv files
            filename_list.append(file_name)
            # plot model-generated and 'experimental' data
            # symbols for 'experimental' data
            # solid and dashed lines for model-generated data
            plot_exp(k, CA0, data, "Experiment {}: T = {} K and $C_{}$ = {} mol/L".format(counter,T,'A0',CA0))
    return filename_list

5.2.1.2. Simulate data for multiple experiments#

Here, we define lists of temperatures and initial concentrations of A to be used to simulate multiple experimental datasets

# list of temperatures
T_vals = [250,300,350,400] # K

# list of initial concentrations of A
CA0_vals = [0.5,1.0,1.5,2.0] # mol/L

# generating 'experimental' data and storing list of filenames to be used to load data later
file_list = gen_data(T_vals,CA0_vals,t_est,stdev_m_error,n)

# creating a log file to store file names
log_file_name = data_dir + 'log_file.csv'
# create a pandas dataframe to store file names
file_list_df = pd.DataFrame.from_dict({'File name':file_list})
# save dataframe as a separate csv file
file_list_df.to_csv(log_file_name,index=False)
../../_images/268c6c3280de9a6448eace464ba696c6513df1f250797127e39a8200b86d524a.png ../../_images/e9316e2951bcc7de4ed820c7582814459f0a5911f416998ff9adee8d9954a2b2.png ../../_images/566c0fe8a9d17a2a93fd3c2ebcba2694646d7569b1206999e7f0757419531784.png ../../_images/099bae50ff620b413713e1e1bbb7cd5ef81694a7522d2065ec3bd5f65cd66c65.png ../../_images/8df5687b1bd84ebf495d10107232fe96bf3e6799e89a07ce7580d652e8abd32c.png ../../_images/4accbebf6f770a56a99b7615555fb3f1db35011595f091efd73c96674dedd542.png ../../_images/0028736c4c13fdadf70314e23517f0ed40781766194946cf0327e34897359d23.png ../../_images/ded869541f9f4d95f08213ed9b21445df2bb073d56c71e467d6b8ff0fac28e7f.png ../../_images/595b8e2221ec24762d8033f7343a11ce6f2e585fda0c60698629516276107a80.png ../../_images/b3556d4ea8982ca7a0829a3609475e3a1ad10fbbc805eb195aa11986e4e597ec.png ../../_images/ab37f89908efae84818557c440915975d074e763b8fd15fd12012d6814475e8b.png ../../_images/61a48c23baa7f3f171cb99481fa9b1e10373579360b9278c5a41a6fdb7912727.png ../../_images/96d6c8dd5df7e0f1346b387be5b5255af53439153350dcaf835af2ec87f0eb1b.png ../../_images/a29727b12563e684adaaba23c16ed648ae5f1f3dee33547d370d282900ef0142.png ../../_images/621305131d60817a630d167369025737c27b264d46e19a72ec0d7a1a5c5ca165.png ../../_images/ac6ba966156bde9fcf298236deebd918716c8b7555eb30b7a2172c99475c7e90.png

We will imagine these data came from laboratory experiments.