<!-- Airbus A320 APU -->

<!-- Copyright (c) 2020 Jonathan Redpath -->

<system name="A320: APU">

    <channel name="APU" execrate="8">
        <!-- bleed 1.8 kg / sec @ 42 psig -->
        <!-- fuel 148 kg/hr -->
        <!-- generator 24034 rpm, 10129 rpm fuel ctl, 32045 rpm starter, 51965 rpm cooling, 4137rpm oil pump -->

        <fcs_function name="/systems/apu/bleed-load-kw">
            <function>
                <ifthen>
                    <eq>
                        <property>/controls/pneumatics/switches/apu</property>
                        <value>1</value>
                    </eq>
                    <value>252</value>
                    <value>0</value>
                </ifthen>
            </function>
        </fcs_function>
        
        
        <fcs_function name="/systems/apu/electrical-load-kw">
            <function>
                <ifthen>
                    <gt>
                        <property>/systems/electrical/relay/apu-glc/output</property>
                        <value>110</value>
                    </gt>
                    <value>132</value>
                    <value>0</value>
                </ifthen>
            </function>
        </fcs_function>
        
        <fcs_function name="/systems/apu/apu-load-cmd">
            <function>
                <quotient>
                    <sum>
                        <property>/systems/apu/electrical-load-kw</property>
                        <property>/systems/apu/bleed-load-kw</property>
                    </sum>
                    <value>400</value>
                </quotient>
            </function>
        </fcs_function>
        
        <actuator name="/systems/apu/apu-load">
            <input>/systems/apu/apu-load-cmd</input>
            <rate_limit>0.01</rate_limit> <!-- 60 seconds cooling period for bleed which is ~ 60% -->
        </actuator>
        
        <washout_filter name="/systems/apu/dn-dt">
            <input>/engines/engine[2]/n1</input>
            <c1>1</c1>
        </washout_filter>
        
        <fcs_function name="/systems/apu/egt-degC">
            <function>
                <sum>
                    <product>
                        <property>/systems/apu/apu-load</property>
                        <value>50</value>
                    </product>
                    <ifthen>
                        <or>
                            <lt>
                                <property>/engines/engine[2]/n1</property>
                                <value>0.5</value>
                            </lt>
                            <gt>
                                <property>/systems/apu/dn-dt</property>
                                <value>0</value>
                            </gt>
                        </or>
                        <table>
                            <independentVar lookup="row">/engines/engine[2]/n1</independentVar>
                            <tableData>
                                5.0     20.0
                                8.0     45.0
                                10.0    120.0
                                12.0    225.0
                                18.0    430.0
                                25.0    500.0
                                27.0    560.0
                                30.0    720.0
                                35.0    785.0
                                40.0    800.0
                                45.0    795.0
                                50.0    785.0
                                60.0    755.0
                                75.0    645.0
                                90.0    520.0
                                100.0   415.0
                            </tableData>
                        </table>
                        <table>
                            <independentVar lookup="row">/engines/engine[2]/n1</independentVar>
                            <tableData>
                                2.0     200.0
                                8.0     220.0
                                12.0    225.0
                                18.0    240.0
                                30.0    250.0
                                50.0    265.0
                                60.0    295.0
                                75.0    380.0
                                90.0    410.0
                                100.0   415.0
                            </tableData>
                        </table>
                    </ifthen>
                </sum>
            </function>
        </fcs_function>
        
        <fcs_function name="/systems/apu/oil/oil-pressure-psi">
            <function>
                <table>
                    <independentVar lookup="row">/engines/engine[2]/n1</independentVar>
                    <independentVar lookup="column">/systems/apu/oil/level-l</independentVar>
                    <tableData>
                              0   3.6   5.4
                        8     0   0     0
                        12    0   1     10
                        18    0   5     22
                        30    0   22    41
                        50    0   25    45
                        60    0   28    48
                        75    0   33    52
                        90    0   35    54
                        100   0   36    55
                    </tableData>
                </table>
            </function>
        </fcs_function>
        
        <fcs_function name="/systems/apu/oil/oil-temperature-degC">
            <function>
                <product>
                    <sum>
                        <value>1.0</value> <!-- tune -->
                        <product>
                            <property>/systems/apu/apu-load</property>
                            <value>0.3</value>
                        </product>
                    </sum>
                    <table>
                        <independentVar lookup="row">/systems/apu/oil/oil-pressure-psi</independentVar>
                        <tableData>
                            0   0
                            25  135
                            35  105
                            55  85
                        </tableData>
                    </table>
                    <quotient>
                        <property>/engines/engine[2]/n1</property>
                        <value>100</value>
                    </quotient>
                </product>
            </function>
        </fcs_function>
        
        <fcs_function name="/systems/apu/oil/calc-oil-level">
            <function>
                <sum>
                    <property>/systems/apu/oil/level-l</property>
                    <product>
                        <quotient>
                            <property>/engines/engine[2]/n1</property>
                            <value>100</value>
                        </quotient>
                        <property>/systems/apu/oil/allow-oil-consumption</property>
                        <value>-0.00000131437</value>
                        <property>simulation/channel-dt</property>
                    </product>
                </sum>
            </function>
            <output>/systems/apu/oil/level-l</output>
        </fcs_function>
        
        <switch name="/systems/apu/shutdown-power-loss-cmd">
            <default value="0"/>
            <test logic="AND" value="1">
                /systems/electrical/bus/dc-bat lt 25
            </test>
        </switch>
        
        <actuator name="/systems/apu/shutdown-power-loss-timer">
			<input>/systems/apu/shutdown-power-loss-cmd</input>
            <rate_limit sense="incr">5</rate_limit>
            <rate_limit sense="decr">100</rate_limit>
        </actuator>
        
        <switch name="/systems/apu/shutdown-power-loss">
            <default value="0"/>
            <test logic="AND" value="1">
                /systems/apu/shutdown-power-loss-timer eq 1
            </test>
        </switch>
        
		
    </channel>

</system>