Using a PID Controller Script to Control Boundary Conditions
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.
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.
- Save the pid_controller.zip file into the simulation directory, unzip it, and make the script executable using: chmod +x pid_controller.py
- 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 ReactorA 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 = 0integral = 0start:error = setpoint - measured_valueintegral = integral + error*dtderivative = (error - previous_error)/dtoutput = Kp*error + Ki*integral + Kd*derivativeprevious_error = errorwait(dt)goto start - 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
- 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.
- 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:
- 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.
- Start the Barracuda simulation and let it run at least 10 time-steps.
- Start the PID controller script.
- 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.