Using a PID Controller Script to Control Boundary Conditions

Shane Lanzon_20859
Shane Lanzon_20859
Altair Employee
edited April 12 in Altair Partner Alliance

In some Barracuda Virtual Reactor simulations, it is useful to dynamically and automatically control flow rates, pressures, temperature, or other parameters at boundary conditions (BCs) based on information from data points, flux planes, or other output files.  It is possible to achieve such control of BCs by using a script running alongside the simulation.  CPFD has written a proportional-integral-derivative (PID) controller script specifically designed to work with the input and output files used by Virtual Reactor.  Download the script using the button below, and read through the rest of this support site post to learn how to use it.

 
The PID controller script reads data from a user-specified file (usually a flux plane or data point file) and adjusts a BC SFF file to respond to a defined condition (usually to increase or decrease a flow rate or velocity, adjust a pressure, or change the temperature of a thermal wall) in order to achieve a certain goal (maintain an internal temperature, pressure, velocity, etc.). The script utilizes the Barracuda Virtual Reactor Interact command to reread the SFF file after adjusting it. The script uses proportional, integral, and derivative gain to make adjustments to meet the desired goal. This Wikipedia article explains basic PID controller theory, which applies to the use of this script.
 
As an example to illustrate how it works, the script was used to maintain a heat transfer flux (J/(s*m^2)) from one heating coil of the Gasifier training problem by adjusting the temperature (K) in the BC_heating_coil.sff file.
 

In order to use the controller script, Python must be installed on the machine. See this post on how to install the recommended version of Python.

  1. Save the pid_controller.zip file into the simulation directory, unzip it, and make the script executable using:
    chmod +x pid_controller.py
  2. Open a command terminal through the Barracuda GUI. Type the following:
    ./pid_controller.py

              Which will output the help information:

    pid_controller.py -- a proportional integral derivative control script to use with Barracuda Virtual Reactor
     
    A measured variable affects the change of a control variable, and Interact is called to re-read BCs.
     
    Summary of options / required arguments:
    pid_controller.py
    -p / --Kp-gain 0.05 (proportional gain)
    -i / --Ki-gain 0.8 (integral gain)
    -d / --Kd-gain 0.0 (derivative gain)
    -s / --sleep-seconds 600 (sleep for 10 minute)
    -r / --ramp-seconds 0.02 (simulation time over which changes are ramped)
    -f / --measure-file trans.data00 (file containing measured variable)
    -c / --measure-column 2 (column of measured variable in measure file)
    -t / --measure-target 1290 (target of measured variable)
    -F / --control-file my_bc.sff (file containing control variable)
    -C / --control-column 3 (column of control variable in control file)
    -N / --control-min 200 (if specified, enforce minimum value for control variable)
    -X / --control-max 1000 (if specified, enforce maximum value for control variable)
     
    Based on discretized PID pseudocode found on Wikipedia on Monday, June 23, 2014:
     
     
    previous_error = 0
    integral = 0
    start:
    error = setpoint - measured_value
    integral = integral + error*dt
    derivative = (error - previous_error)/dt
    output = Kp*error + Ki*integral + Kd*derivative
    previous_error = error
    wait(dt)
    goto start
  3. Decide on values for each required input argument. The example command used for the Gasifier training example is shown below, but each command should include values and file names for your particular simulation:
    ./pid_controller.py -p 0.001 -i 0.001 -d 0 -s 60 -r 0.02 -f trans.data01 -c 9 -t 5000 -F BC_heating_coil.sff -C 2 -N 1200 -X 1400
  4. Start the simulation and let it run for at least 10 times steps so that there is data in the files before the controller script starts.
  5. After 10 solver time-steps, start the PID controller script in a command terminal from the Barracuda GUI, or in a terminal you have opened manually in the simulation directory. The script will run concurrently with the Barracuda solver and adjust the sff file as needed to move the controlled variable toward the desired target.

As is the case for PID controllers in real-world physical systems, determining good values for the proportional, integral, and derivative gains can sometimes be an iterative process.  Therefore, as you first begin to use the PID controller script, you may need to try several different values for the -p, -i, and -d terms.  When you do this, the general process is:

  1. Clean up the SFF file that is specified by the -F flag, so that only the time zero line is present.  If you’ve previously run the simulation with the PID controller, there will be lines present that will prevent new settings for -p, -i, and -d from being used as expected.
  2. Start the Barracuda simulation and let it run at least 10 time-steps.
  3. Start the PID controller script.
  4. Monitor the script’s performance to ensure that there is no significant “ringing” of the controlled parameter, and that the target is being approached at a reasonable rate.