<?xml version="1.0"?>

<!--
        Description: Rolls-Royce Avon 114 turbojet engine simulation - additions to JSBSim model.
                     based on F100 version; 
                     * uses pressures to determine augmentation
                     * provides fuel flow and EGT
                     
        Author: Richard Harrison (rjh@zaretto.com)
        References: 
        1. Ján Janikovič, Gas Turbine Transient Performance Modeling for Engine Flight Path Cycle Analysis P201 https://core.ac.uk/download/pdf/14343101.pdf
        2. NASA TM-86042 Minimum Time and Fuel Flight Profiles for an F-15 Airplane with a Highly Integrated Digital Electronic Control System. Edward A. Haering, Jr., and Frank W. Burcham, Jr.
        3. NASA TP-1069 Airflow and thrust calibration of an F100 Engine S/N p680059 at selected flight conditions. Thomas J.  Biesiadny,  Douglas  Lee,  and Jose  R.  Rodriguez 
        3. NASA TP-1228 Altitude calibration of an F100 Engine S/N p680063, Turbofan Engine. Thomas J.  Biesiadny,  Douglas  Lee,  and Jose  R.  Rodriguez 
        
   -->

<system name="ENGINES">

    <channel name="engine-control">
        <scheduled_gain name="Throttle">
            <input>fcs/throttle-pos-norm[0]</input>
            <table>
                <independentVar>fcs/throttle-pos-norm[0]</independentVar>
                <tableData>
                    <!-- 5 stage non linear augmentation-->
                    0.00    0.0
                    0.8999  0.999
                    0.91    1.0
                    0.93    1.25
                    0.95    1.50
                    0.97    1.75
                    1.00    2.0
                </tableData>
            </table>
            <output>fcs/throttle-pos-norm[0]</output>
        </scheduled_gain>
    </channel>

    <channel name="Avon114extra">
        <fcs_function name="propulsion/engine[0]/c-egt-lead">
            <description>EGT LEAD N1 %</description>
            <function>
                <table>
                    <independentVar lookup="row">propulsion/engine[0]/n1</independentVar>
                    <tableData>
                        0	18.2
                        10	12.7
                        24	8.45
                        42	5.25
                        56	4.77
                        100	4.4
                    </tableData>
                </table>
            </function>
        </fcs_function >
        <fcs_function name="propulsion/engine[0]/c-egt-lag">
            <description>EGT LAG N1 %</description>
            <function>
                <table>
                    <independentVar lookup="row">propulsion/engine[0]/n1</independentVar>
                    <tableData>
                        0	3.20
                        10	2.15
                        24	0.99
                        42	0.56
                        56	0.25
                        100	0.21
                    </tableData>
                </table>
            </function>
        </fcs_function >

        <fcs_function name="propulsion/engine[0]/fuel-flow-rate-norm">
            <description>Fuel Flow normalized</description>
            <function>
                <quotient>
                    <property>propulsion/engine[0]/fuel-flow-rate-pps</property>
                    <value>3</value>
                </quotient>
            </function>
            <clipto>
                <min>0</min>
                <max>1.43</max>
            </clipto>
        </fcs_function>

        <fcs_function name="propulsion/engine[0]/EGT-C-dmd">
            <description>Engine 0 EGT Degrees C</description>
            <function>
                <table>
                    <independentVar lookup="row">velocities/mach</independentVar>
                    <independentVar lookup="column">propulsion/engine[0]/fuel-flow-rate-norm</independentVar>
                    <tableData>
                        0.000	 0.001	0.009	0.020	0.030	0.040	0.050	0.060	0.070	0.080	0.100	0.120	0.140	0.160	0.200	0.240	0.320	0.400	0.560	0.800	0.984
                        0.0     0.00	271.00	281.00	292.00	301.00	310.00	319.00	328.00	336.00	344.00	359.00	374.00	387.00	400.00	424.00	447.00	492.00	537.00	641.00	808.00	870.00
                        0.2     0.00	268.00	277.00	288.00	297.00	307.00	315.00	324.00	332.00	340.00	355.00	369.00	383.00	395.00	419.00	442.00	486.00	531.00	633.00	798.00	860.00
                        0.3     0.00	260.00	269.00	279.00	288.00	297.00	306.00	314.00	322.00	330.00	344.00	358.00	371.00	383.00	406.00	428.00	471.00	515.00	614.00	774.00	833.00
                        0.5     0.00	249.00	257.00	267.00	276.00	285.00	293.00	301.00	308.00	316.00	330.00	343.00	355.00	367.00	389.00	410.00	451.00	493.00	588.00	741.00	798.00
                        0.6     0.00	232.00	240.00	250.00	258.00	266.00	274.00	281.00	288.00	295.00	308.00	320.00	332.00	343.00	364.00	383.00	421.00	460.00	549.00	692.00	746.00
                        0.8     0.00	216.00	223.00	232.00	240.00	247.00	254.00	261.00	268.00	274.00	286.00	298.00	308.00	319.00	338.00	356.00	391.00	428.00	510.00	643.00	693.00
                        0.9     0.00	208.00	215.00	223.00	231.00	238.00	245.00	251.00	258.00	264.00	275.00	286.00	297.00	307.00	325.00	343.00	377.00	412.00	491.00	619.00	667.00

                    </tableData>
                </table>
            </function>
        </fcs_function>

        <fcs_function name="propulsion/engine[0]/PB">
            <description>Engine 0 SS Burner pressure ratio</description>
            <function>
                <table>
                    <independentVar lookup="row">propulsion/engine[0]/n2</independentVar>
                    <independentVar lookup="column">velocities/mach</independentVar>
                    <tableData>
                        0.00000	0.20000	0.30000	0.45000	0.60000	0.80000	0.90000	3.00000
                        0	1.00000	0.93963	0.93963	0.93963	0.93963	0.93963	0.93963	0.93963
                        8	1.09966	0.93963	0.93963	0.93963	0.93963	0.93963	0.93963	0.93963
                        16	1.26926	0.97069	0.93186	0.93963	0.93963	0.93963	0.93963	0.93963
                        24	1.37453	0.99197	0.95229	0.92944	0.93963	0.93963	0.93963	0.93963
                        32	1.56636	1.00992	0.96952	0.92105	0.89341	0.93963	0.93963	0.93963
                        40	1.87550	1.03223	0.99094	0.94140	0.91316	0.91316	0.93963	0.93963
                        48	2.25353	1.06785	1.02514	0.95338	0.91524	0.91835	0.97369	0.97369
                        56	2.61381	1.12695	1.08188	1.00614	0.96590	0.96918	0.97369	0.97369
                        63	2.84760	1.22096	1.17212	1.09007	1.04647	1.05002	0.99630	0.99630
                        71	2.98150	1.34177	1.28810	1.19793	1.15002	1.15392	1.09488	1.09488
                        79	3.00594	1.53633	1.47488	1.37164	1.31677	1.32124	1.25365	1.25365
                        87	3.07269	1.80555	1.73333	1.61200	1.54752	1.55278	1.47333	1.47333
                        95	3.49481	2.16568	2.07905	1.93352	1.85618	1.86248	1.76719	1.76719
                        97	3.71122	2.63418	2.52881	2.35179	2.25772	2.26539	2.14949	2.14949
                        98	3.84257	2.77040	2.65959	2.47341	2.37448	2.38255	2.26065	2.26065
                        100	4.15840	2.84159	2.72792	2.53697	2.43549	2.44376	2.31873	2.31873
                    </tableData>
                </table>
            </function>
        </fcs_function>
        <fcs_function name="propulsion/engine[0]/augmentation-stage">
            <function>
                <integer>
                    <sum>
                        <value>1.0</value>
                        <product>
                            <sum>
                                <property>fcs/throttle-pos-norm[0]</property>
                                <value>-1</value>
                            </sum>
                            <value>5</value>
                        </product>
                    </sum>
                </integer>
            </function>
            <clipto>
                <min>0</min>
                <max>5</max>
            </clipto>
        </fcs_function>
        <switch name="propulsion/engine[0]/augmentation-ignition">
            <default value="0"/>
            <test value="1">
                propulsion/engine[0]/PB gt 2.14
                propulsion/engine[0]/augmentation-stage gt 0
                propulsion/engine[0]/augmentation-alight eq 0
            </test>
        </switch>
        <switch name="propulsion/engine[0]/augmentation-igniting">
            <default value="0"/>
            <test value="1">
                propulsion/engine[0]/augmentation-ignition gt 0
                propulsion/engine[0]/augmentation-alight eq 0
            </test>
        </switch>

        <actuator name="propulsion/engine[0]/augmentation-ignition-sequence">
            <input> propulsion/engine[0]/augmentation-igniting </input>
            <rate_limit> 1.2 </rate_limit>
            <clipto>
                <min> 0 </min>
                <max>  1</max>
            </clipto>
        </actuator>

        <switch name="propulsion/engine[0]/augmentation-alight-dmd">
            <default value="0"/>
            <test value="propulsion/engine[0]/augmentation-stage">
                propulsion/engine[0]/augmentation-stage ne propulsion/engine[0]/augmentation-alight
                propulsion/engine[0]/augmentation-alight ne 0
                propulsion/engine[0]/thrust-lbs gt 100 <!-- engine alight -->
            </test>
            <test value="propulsion/engine[0]/augmentation-stage">
                propulsion/engine[0]/augmentation-ignition-sequence gt 0.9
                propulsion/engine[0]/thrust-lbs gt 100 <!-- engine alight -->
            </test>
            <test value="propulsion/engine[0]/augmentation-alight">
                propulsion/engine[0]/augmentation-stage gt 0
                propulsion/engine[0]/augmentation-alight ne 0
                propulsion/engine[0]/thrust-lbs gt 100 <!-- engine alight -->
            </test>
        </switch>
        <switch name="propulsion/engine[0]/augmentation-burner">
            <default value="0"/>
            <test value="1">
                propulsion/engine[0]/augmentation-alight ne 0
                propulsion/engine[0]/thrust-lbs gt 100 <!-- engine alight -->
            </test>
            <test value="propulsion/engine[0]/augmentation-ignition-sequence">
                propulsion/engine[0]/augmentation-ignition-sequence ne 0
                propulsion/engine[0]/thrust-lbs gt 100 <!-- engine alight -->
            </test>
        </switch>

        <actuator name="propulsion/engine[0]/augmentation-alight">
            <input> propulsion/engine[0]/augmentation-alight-dmd </input>
            <rate_limit> 10 </rate_limit>
            <clipto>
                <min> 0 </min>
                <max>  5</max>
            </clipto>
        </actuator>

        <!-- EGT equations -->
        <fcs_function name="propulsion/engine[0]/EGTaug-C-dmd">
            <description>Engine 0 EGT increment due to augmentation</description>
            <function>
                <table>
                    <independentVar lookup="row">propulsion/engine[0]/augmentation-stage</independentVar>
                    <tableData>
                        0.00	0
                        1.00	86
                        5.00	171
                    </tableData>
                </table>
            </function>
        </fcs_function>

        <lag_filter name="propulsion/engine[0]/EGTaug-C">
            <input>propulsion/engine[0]/EGTaug-C-dmd</input>
            <output>propulsion/engine[0]/EGTaug-C</output>
            <c1>0.03</c1>
        </lag_filter >
        <fcs_function name="propulsion/engine[0]/pt">
            <description>Total Pressure</description>
            <function>
                <!--double pt = Math.Pow(pambient * (1.0 + 0.2 * velocities_mach * velocities_mach), 3.5);-->
                <pow>
                    <product>
                        <product>
                            <property>atmosphere/P-psf</property>
                            <value>144</value>
                        </product>
                        <sum>
                            <value>1.0</value>
                            <product>
                                <value>0.2</value>
                                <property>velocities/mach</property>
                                <property>velocities/mach</property>
                            </product>
                        </sum>
                    </product>
                    <value>3.5</value>
                </pow>
            </function>
        </fcs_function>
        <fcs_function name="propulsion/engine[0]/delta">
            <description>Delta</description>
            <function>
                <quotient>
                    <property>propulsion/engine[0]/pt</property>
                    <value>14.696</value>
                </quotient>
            </function>
        </fcs_function>

        <lead_lag_filter name="propulsion/engine[0]/EGT-C">
            <input>propulsion/engine[0]/EGT-C-dmd</input>
            <output>propulsion/engine[0]/EGT-C</output>
            <c1>propulsion/engine[0]/delta</c1>
            <c2>propulsion/engine[0]/c-egt-lead</c2>
            <c3>propulsion/engine[0]/delta</c3>
            <c4>propulsion/engine[0]/c-egt-lead</c4>
        </lead_lag_filter >

        <lag_filter name="propulsion/engine[0]/casing-temperature-C">
            <input>propulsion/engine[0]/EGT-C</input>
            <output>propulsion/engine[0]/casing-temperature-C</output>
            <c1>0.01</c1>
        </lag_filter>

        <switch name="propulsion/engine[0]/casing-overheat">
            <default value="0"/>
            <test value="1">
                propulsion/engine[0]/casing-temperature-C gt 780
            </test>
        </switch>

        <fcs_function name="propulsion/engine[0]/alt/nozzle-dmd-norm">
            <function>
                <description>PLA Vs Nozzle open area Exhaust Nozzle Control TO 15-15A-1 page 1-8 </description>
                <table>
                    <independentVar lookup="row">fcs/throttle-pos-norm[0]</independentVar>
                    <tableData>
                        0.0 	0.81807
                        0.2235	0.81807
                        1.0000	0.15248
                    </tableData>
                </table>
            </function>
        </fcs_function>

        <switch name="propulsion/engine[0]/alt/nozzle-cmd-norm">
            <default value="propulsion/engine[0]/alt/nozzle-dmd-norm"/>
            <test value="1.0" logic="OR">
                propulsion/engine[0]/augmentation-alight gt 0
                propulsion/engine[0]/augmentation-ignition gt 0.4
                propulsion/engine[0]/n2 lt 60
            </test>
        </switch>

        <kinematic name="propulsion/engine[0]/alt/nozzle-pos-norm">
            <input>propulsion/engine[0]/alt/nozzle-cmd-norm</input>
            <traverse>
                <setting>
                    <position>0</position>
                    <time>1</time>
                </setting>
                <setting>
                    <position>1</position>
                    <time>0.4</time>
                </setting>
            </traverse>
        </kinematic>

        <fcs_function name="propulsion/engine[0]/oil-pressure-psi">
            <function>
                <description>OIL pressure </description>
                <table>
                    <independentVar lookup="row">propulsion/engine[0]/n2</independentVar>
                    <tableData>
                        0   0
                        22  12
                        50  30
                        100 48
                    </tableData>
                </table>
            </function>
        </fcs_function>

        <fcs_function name="/engines/engine[0]/rpm">
            <function>
                <product>
                    <property>propulsion/engine[0]/n2</property>
                    <v>160</v>
                </product>
            </function>
            <output>/engines/engine[0]/rpm</output>
        </fcs_function>

        <fcs_function name="/engines/engine[0]/reheat">
            <function>
                <product>
                    <property>propulsion/engine[0]/augmentation-alight</property>
                    <value>0.2</value>
                </product>
            </function>
            <output>/engines/engine[0]/reheat</output>
        </fcs_function>
    </channel>
</system>