FlowDirectionPositive ::
  (Ord node, Show node) =>
  Params.System node a ->
  Idx.State ->
  TopoIdx.Position node ->
  EnvResult node a ->
  Bool
isFlowDirectionPositive sysParams state (TopoIdx.Position f t) graph =
  case Set.toList es of
       [Graph.DirEdge fe te] ->
         case flowTopoEs of
              Just set ->
                case ( Set.member (Graph.EDirEdge $ Graph.DirEdge fe te) set,
                       Set.member (Graph.EDirEdge $ Graph.DirEdge te fe) set ) of
                     (True, False)  -> True
                     (False, True)  -> False
                     tf -> error $ "isFlowDirectionPositive: "
                                   ++ "inconsisten flow graph " ++ show tf
              _ -> error $ "State (" ++ show state ++ ") not found"
       _ -> error $ "More or less than exactly one edge between nodes "
                    ++ show f ++ " and " ++ show t ++ " in " ++ show es
  where flowTopoEs = fmap Graph.edgeSet $ ModUt.getFlowTopology state graph
        topo = Params.systemTopology sysParams
        es = Graph.adjacentEdges topo f
               `Set.intersection` Graph.adjacentEdges topo t


isFlowDirectionPositive ::
  (Ord node, Show node) =>
  Params.System node a ->
  Idx.State ->
  TopoIdx.Position node ->
  EnvResult node a ->
  Bool
isFlowDirectionPositive sysParams state (TopoIdx.Position f t) graph =
  case Set.toList es of
       [Graph.DirEdge fe te] ->
         case flowTopoEs of
              Just set ->
                case ( Set.member (Graph.EDirEdge $ Graph.DirEdge fe te) set,
                       Set.member (Graph.EDirEdge $ Graph.DirEdge te fe) set ) of
                     (True, False)  -> True
                     (False, True)  -> False
                     tf -> error $ "isFlowDirectionPositive: "
                                   ++ "inconsisten flow graph " ++ show tf
              _ -> error $ "State (" ++ show state ++ ") not found"
       _ -> error $ "More or less than exactly one edge between nodes "
                    ++ show f ++ " and " ++ show t ++ " in " ++ show es
  where flowTopoEs = fmap Graph.edgeSet $ ModUt.getFlowTopology state graph
        topo = Params.systemTopology sysParams
        es = Graph.adjacentEdges topo f
               `Set.intersection` Graph.adjacentEdges topo t
isFlowDirectionPositive sysParams state (TopoIdx.Position f t) graph =
  case Set.toList es of
       [Graph.DirEdge fe te] ->
         case flowTopoEs of
              Just set ->
                case ( Set.member (Graph.EDirEdge $ Graph.DirEdge fe te) set,
                       Set.member (Graph.EDirEdge $ Graph.DirEdge te fe) set ) of
                     (True, False)  -> True
                     (False, True)  -> False
                     tf -> error $ "isFlowDirectionPositive: "
                                   ++ "inconsisten flow graph " ++ show tf
              _ -> error $ "State (" ++ show state ++ ") not found"
       _ -> error $ "More or less than exactly one edge between nodes "
                    ++ show f ++ " and " ++ show t ++ " in " ++ show es
  where flowTopoEs = fmap Graph.edgeSet $ ModUt.getFlowTopology state graph
        topo = Params.systemTopology sysParams
        es = Graph.adjacentEdges topo f
               `Set.intersection` Graph.adjacentEdges topo t