<?xml version="1.0" encoding="UTF-8"?>
<!--
**************************************************************************

Beagle Pup Fuel System (JSBSim)

Copyright (c) 2015 Richard Senior

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

**************************************************************************
-->

<system name="Fuel">

  <!-- Tank selector: 0 = OFF, 1 = LEFT, 2 = BOTH, 3 = RIGHT -->
  <property value="0">/controls/fuel/tank-selector</property>

  <channel name="Fuel">

    <fcs_function name="propulsion/tank[0]/fuel-pressure-psi">
      <function>
        <sum>
          <product>
            <gt>
              <property>/systems/electrical/outputs/fuel-pump</property>
              <value>0</value>
            </gt>
            <!--
            Should produce a PSI slightly higher than the engine
            driven pump, limited by its relief valve.
            -->
            <value>1.67</value>
          </product>
          <quotient>
            <property>propulsion/engine/engine-rpm</property>
            <value>400</value>
          </quotient>
        </sum>
      </function>
      <clipto>
        <!-- Pressure relief valve -->
        <min>0.0</min>
        <max>7.8</max>
      </clipto>
    </fcs_function>

    <!-- Return into tank 1 (LEFT) -->
    <switch name="propulsion/tank[1]/inflow-rate-pps">
      <default value="0.05"/>
      <test logic="OR" value="0.00">
        propulsion/tank[0]/contents-lbs LE 0.0
        propulsion/tank[0]/fuel-pressure-psi GT 1
      </test>
    </switch>

    <!-- Return into tank 2 (RIGHT) -->
    <switch name="propulsion/tank[2]/inflow-rate-pps">
      <default value="0.05"/>
      <test logic="OR" value="0.00">
        propulsion/tank[0]/contents-lbs LE 0.0
        propulsion/tank[0]/fuel-pressure-psi GT 1
      </test>
    </switch>

    <!-- Feed from tank 1 (LEFT) -->
    <switch name="propulsion/tank[1]/outflow-rate-pps">
      <default value="0.2"/>
      <test logic="OR" value="0.0">
        /controls/fuel/tank-selector EQ 0
        /controls/fuel/tank-selector EQ 3
        propulsion/tank[0]/fuel-pressure-psi LE 1
        propulsion/tank[0]/contents-lbs GE 0.8
        propulsion/tank[1]/contents-lbs LE 0.0
      </test>
    </switch>

    <!-- Feed from tank 2 (RIGHT) -->
    <switch name="propulsion/tank[2]/outflow-rate-pps">
      <default value="0.2"/>
      <test logic="OR" value="0.0">
        /controls/fuel/tank-selector EQ 0
        /controls/fuel/tank-selector EQ 1
        propulsion/tank[0]/fuel-pressure-psi LE 1
        propulsion/tank[0]/contents-lbs GE 0.8
        propulsion/tank[2]/contents-lbs LE 0.0
      </test>
    </switch>

    <summer name="Tank 1 Net Flow">
      <input>propulsion/tank[1]/inflow-rate-pps</input>
      <input>-propulsion/tank[1]/outflow-rate-pps</input>
      <output>propulsion/tank[1]/external-flow-rate-pps</output>
    </summer>

    <summer name="Tank 2 Net Flow">
      <input>propulsion/tank[2]/inflow-rate-pps</input>
      <input>-propulsion/tank[2]/outflow-rate-pps</input>
      <output>propulsion/tank[2]/external-flow-rate-pps</output>
    </summer>

    <summer name="Manifold Net Flow">
      <input>-propulsion/tank[1]/external-flow-rate-pps</input>
      <input>-propulsion/tank[2]/external-flow-rate-pps</input>
      <output>propulsion/tank[0]/external-flow-rate-pps</output>
    </summer>

  </channel>

  <channel name="Engine">

    <fcs_function name="propulsion/engine/cht-degC">
      <function>
        <quotient>
          <difference>
            <property>propulsion/engine/cht-degF</property>
            <value>32</value>
          </difference>
          <value>1.8</value>
        </quotient>
      </function>
    </fcs_function>

    <fcs_function name="propulsion/engine/egt-degC">
      <function>
        <quotient>
          <difference>
            <property>propulsion/engine/egt-degF</property>
            <value>32</value>
          </difference>
          <value>1.8</value>
        </quotient>
      </function>
    </fcs_function>

    <fcs_function name="propulsion/engine/oil-temperature-degC">
      <function>
        <quotient>
          <difference>
            <property>propulsion/engine/oil-temperature-degF</property>
            <value>32</value>
          </difference>
          <value>1.8</value>
        </quotient>
      </function>
    </fcs_function>

  </channel>

  <channel name="Carburettor">

    <!-- Temperature -->

    <!--
    Continental engines have the carburettor mounted away from the
    heat of the engine and are prone to carb icing, whereas the
    Lycoming engines fitted to the Pup 150 and 160 have the
    carburettor mounted close to the engine, which keeps it warm.
    -->
    <switch name="systems/carburettor/compartment-coefficient">
      <default value="15"/>
      <test value="5">
        propulsion/engine/manufacturer-code EQ 0
      </test>
    </switch>

    <fcs_function name="systems/carburettor/compartment-effect-degC">
      <function>
        <product>
          <quotient>
            <difference>
              <property>propulsion/engine/cht-degC</property>
              <property>/environment/temperature-degc</property>
            </difference>
            <value>220</value> <!-- Max CHT -->
          </quotient>
          <property>systems/carburettor/compartment-coefficient</property>
        </product>
      </function>
      <clipto>
        <min>0.0</min>
        <max>30.0</max>
      </clipto>
    </fcs_function>

    <fcs_function name="systems/carburettor/venturi-effect-degC">
      <function>
        <product>
          <difference>
            <property>/engines/engine/mp-inhg</property>
            <property>/environment/pressure-inhg</property>
          </difference>
          <quotient>
            <value>21</value>   <!-- Max temp drop -->
            <difference>
              <value>30</value> <!-- Max MP -->
              <value>10</value> <!-- Min MP -->
            </difference>
          </quotient>
        </product>
      </function>
      <clipto>
        <min>-21.0</min>
        <max>0.0</max>
      </clipto>
    </fcs_function>

    <fcs_function name="systems/carburettor/carb-heat-effect-degC">
      <function>
        <product>
          <quotient>
            <property>propulsion/engine/egt-degC</property>
            <value>860</value> <!-- Max EGT -->
          </quotient>
          <value>45</value>
          <property>/controls/anti-ice/engine/carb-heat-norm</property>
        </product>
      </function>
      <clipto>
        <min>0.0</min>
        <max>30.0</max>
      </clipto>
    </fcs_function>

    <summer name="systems/carburettor/carb-temp-target-degC">
      <input>systems/carburettor/carb-heat-effect-degC</input>
      <input>systems/carburettor/compartment-effect-degC</input>
      <input>systems/carburettor/venturi-effect-degC</input>
      <input>/environment/temperature-degc</input>
    </summer>

    <fcs_function name="systems/carburettor/carb-temp-degC">
      <function>
        <sum>
          <property>systems/carburettor/carb-temp-degC</property>
          <quotient>
            <difference>
              <property>systems/carburettor/carb-temp-target-degC</property>
              <property>systems/carburettor/carb-temp-degC</property>
            </difference>
            <product>
              <value>60</value>
              <value>120</value>
            </product>
          </quotient>
        </sum>
      </function>
    </fcs_function>

    <!-- Icing -->

    <switch name="systems/carburettor/icing-phase">
      <!-- Ice thawing if present -->
      <default value="0"/>
      <test logic="AND" value="1">
        <!-- Ice not forming, but not thawing -->
        /sim/model/preferences/realism/carb-ice NE 0
        systems/carburettor/carb-temp-degC LE 0.0
        systems/carburettor/carb-temp-degC GT /environment/dewpoint-degc
      </test>
      <test logic="AND" value="2">
        <!-- Ice forming -->
        /sim/model/preferences/realism/carb-ice NE 0
        systems/carburettor/carb-temp-degC LE 0.0
        systems/carburettor/carb-temp-degC LE /environment/dewpoint-degc
      </test>
    </switch>

    <switch name="systems/carburettor/icing-delta">
      <default value="0"/>
      <test logic="AND" value="systems/carburettor/carb-temp-degC">
        /sim/model/preferences/realism/carb-ice NE 0
        systems/carburettor/icing-phase NE 1
      </test>
    </switch>

    <fcs_function name="systems/carburettor/carb-ice-norm">
      <function>
        <sum>
          <property>systems/carburettor/carb-ice-norm</property>
          <quotient>
            <product>
              <property>systems/carburettor/icing-delta</property>
              <value>-1.0</value>
            </product>
            <product>
              <value>600</value>
              <value>120</value>
            </product>
          </quotient>
        </sum>
      </function>
      <clipto>
        <min>0.0</min>
        <max>1.0</max>
      </clipto>
    </fcs_function>

    <!-- Air Intake Impedance -->

    <pure_gain name="Intake Impedance">
      <input>systems/carburettor/carb-ice-norm</input>
      <output>propulsion/engine/air-intake-impedance-factor</output>
      <gain>10.0</gain>
      <clipto>
        <min>0.05</min> <!-- Should match engine definition -->
        <max>10.0</max>
      </clipto>
    </pure_gain>

    <!--
    Adding hot air to the carburettor effectively leans the mixture and
    reduces RPM and power. Model this by modifying the efficiency.
    -->
    <fcs_function name="Volumetric Efficiency">
      <function>
        <difference>
          <value>0.89</value> <!-- Base volumetric efficiency -->
          <product>
            <property>systems/carburettor/carb-heat-effect-degC</property>
            <value>0.004</value>
          </product>
        </difference>
      </function>
      <output>propulsion/engine/volumetric-efficiency</output>
    </fcs_function>

  </channel>

</system>
