<commit_before>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NLog;
using SlimeSimulation.Model;

namespace SlimeSimulation.FlowCalculation.LinearEquations {
    class FlowCalculator {
        private static Logger logger = LogManager.GetCurrentClassLogger();

        public FlowResult CalculateFlow(ISet<Edge> edges, ISet<Node> nodes, Node source, Node sink, int flowAmount) {
            Graph graph = new Graph(edges, nodes);
            List<Node> nodeList = new List<Node>(nodes);
            GaussianSolver solver = new GaussianSolver();
            double[][] A = GetSystemOfEquations(graph, nodeList);
            double[] B = GetMatrixOfFlowGainedAtNodeFromZeroToN(flowAmount, nodes.Count - 1);
            double[] solution = solver.FindX(A, B);
            Pressures pressures = new Pressures(solution, nodeList);
            FlowOnEdges flowOnEdges = GetFlowOnEdges(graph, pressures, nodeList);
            return new FlowResult(edges, nodeList.First(), nodeList.Last(), flowAmount, flowOnEdges);
        }

        private FlowOnEdges GetFlowOnEdges(Graph graph, Pressures pressures, List<Node> nodes) {
            FlowOnEdges result = new FlowOnEdges(graph.Edges);
            foreach (Edge edge in graph.Edges) {
                double pi = pressures.PressureAt(edge.A);
                double pj = pressures.PressureAt(edge.B);
                double flow = edge.Connectivity * (pi - pj);
                result.IncreaseFlowOnEdgeBy(edge, Math.Abs(flow));
                logger.Debug("For edge {0}, got pi {1}, pj {2}, and flow {3}", edge, pi, pj, flow);
            }
            return result;
        }

        private double GetFlowForEdge(double connectivityOnEdge, int i, int j, Pressures pressures) {
            return connectivityOnEdge * (pressures[i] - pressures[j]);
        }<commit_msg>Now make flowCalculator use the source and sink provided<commit_after>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NLog;
using SlimeSimulation.Model;

namespace SlimeSimulation.FlowCalculation.LinearEquations {
    class FlowCalculator {
        private static Logger logger = LogManager.GetCurrentClassLogger();

        public FlowResult CalculateFlow(ISet<Edge> edges, ISet<Node> nodes, Node source, Node sink, int flowAmount) {
            Graph graph = new Graph(edges, nodes);
            List<Node> nodeList = new List<Node>(nodes);
            EnsureSourceSinkInCorrectPositions(nodeList, source, sink);
            GaussianSolver solver = new GaussianSolver();
            double[][] A = GetSystemOfEquations(graph, nodeList);
            double[] B = GetMatrixOfFlowGainedAtNodeFromZeroToN(flowAmount, nodes.Count - 1);
            double[] solution = solver.FindX(A, B);
            Pressures pressures = new Pressures(solution, nodeList);
            FlowOnEdges flowOnEdges = GetFlowOnEdges(graph, pressures, nodeList);
            return new FlowResult(edges, source, sink, flowAmount, flowOnEdges);
        }

        private void EnsureSourceSinkInCorrectPositions(List<Node> nodeList, Node source, Node sink) {
            Swap(nodeList, 0, nodeList.IndexOf(source));
            Swap(nodeList, nodeList.Count - 1, nodeList.IndexOf(sink));
        }

        private void Swap(List<Node> nodeList, int a, int b) {
            if (a != b) {
                Node tmp = nodeList[a];
                nodeList[a] = nodeList[b];
                nodeList[b] = tmp;
            }
        }

        private FlowOnEdges GetFlowOnEdges(Graph graph, Pressures pressures, List<Node> nodes) {
            FlowOnEdges result = new FlowOnEdges(graph.Edges);
            foreach (Edge edge in graph.Edges) {
                double pi = pressures.PressureAt(edge.A);
                double pj = pressures.PressureAt(edge.B);
                double flow = edge.Connectivity * (pi - pj);
                result.IncreaseFlowOnEdgeBy(edge, Math.Abs(flow));
                logger.Debug("For edge {0}, got pi {1}, pj {2}, and flow {3}", edge, pi, pj, flow);
            }
            return result;
        }

        private double GetFlowForEdge(double connectivityOnEdge, int i, int j, Pressures pressures) {
            return connectivityOnEdge * (pressures[i] - pressures[j]);
        }
{'fraction_non_alphanumeric': 0.09841706813489333, 'fraction_numerical': 0.0027529249827942187, 'mean_word_length': 2.603305785123967, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1564484', 'n_tokens_mistral': 1244, 'n_tokens_neox': 1191, 'n_words': 322}