void SetpointManagerMixedAir::updateFanInletOutletNodes(AirLoopHVAC & airLoopHVAC)
{
  boost::optional<Node> fanInletNode;
  boost::optional<Node> fanOutletNode;

  std::vector<StraightComponent> fans;

  std::vector<ModelObject> supplyComponents = airLoopHVAC.supplyComponents();

  for( const auto & supplyComponent : supplyComponents )
  {
    if( boost::optional<FanVariableVolume> variableFan = supplyComponent.optionalCast<FanVariableVolume>() ) {
      fans.insert(fans.begin(), *variableFan);
    }
    else if( boost::optional<FanConstantVolume> constantFan = supplyComponent.optionalCast<FanConstantVolume>() ) {
      fans.insert(fans.begin(), *constantFan);
    }
    else if( boost::optional<FanOnOff> onOffFan = supplyComponent.optionalCast<FanOnOff>() ) {
      fans.insert(fans.begin(), *onOffFan);
    }
  } 

  if( fans.size() > 0 )
  {
    boost::optional<ModelObject> mo;

    mo = fans.front().inletModelObject();
    if( mo )
    {
      fanInletNode = mo->optionalCast<Node>();
    }

    mo = fans.front().outletModelObject();
    if( mo )
    {
      fanOutletNode = mo->optionalCast<Node>();
    }
  }

  if( fanInletNode && fanOutletNode )
  {
    std::vector<model::Node> nodes = subsetCastVector<model::Node>(airLoopHVAC.supplyComponents());

    for( auto & node : nodes )
    {
      std::vector<SetpointManagerMixedAir> setpointManagers = subsetCastVector<SetpointManagerMixedAir>(node.setpointManagers());
      if( ! setpointManagers.empty() ) {
        SetpointManagerMixedAir spm = setpointManagers.front();
        spm.setFanInletNode(fanInletNode.get());
        spm.setFanOutletNode(fanOutletNode.get());
      }
    }
  }
}
void SetpointManagerMixedAir::updateFanInletOutletNodes(AirLoopHVAC & airLoopHVAC)
{
  boost::optional<Node> fanInletNode;
  boost::optional<Node> fanOutletNode;

  std::vector<StraightComponent> fans;

  std::vector<ModelObject> supplyComponents = airLoopHVAC.supplyComponents();

  std::vector<FanConstantVolume> constantFans = subsetCastVector<FanConstantVolume>(supplyComponents);
  std::vector<FanVariableVolume> variableFans = subsetCastVector<FanVariableVolume>(supplyComponents);
  std::vector<FanOnOff> onoffFans = subsetCastVector<FanOnOff>(supplyComponents);

  fans.insert(fans.begin(),constantFans.begin(),constantFans.end());
  fans.insert(fans.begin(),variableFans.begin(),variableFans.end());
  fans.insert(fans.begin(),onoffFans.begin(),onoffFans.end());

  if( fans.size() > 0 )
  {
    boost::optional<ModelObject> mo;

    mo = fans.front().inletModelObject();
    if( mo )
    {
      fanInletNode = mo->optionalCast<Node>();
    }

    mo = fans.front().outletModelObject();
    if( mo )
    {
      fanOutletNode = mo->optionalCast<Node>();
    }
  }

  if( fanInletNode && fanOutletNode )
  {
    std::vector<model::Node> nodes = subsetCastVector<model::Node>(airLoopHVAC.supplyComponents());

    for( std::vector<model::Node>::iterator it = nodes.begin();
         it != nodes.end();
         ++it )
    {
      if( boost::optional<model::SetpointManagerMixedAir> spm = it->getSetpointManagerMixedAir() )
      {
        spm->setFanInletNode(fanInletNode.get());

        spm->setFanOutletNode(fanOutletNode.get());
      }
    }
  }
}