#include "Solver.hpp"
void Solver::SolveExplicit(System& program, double tstop) const {
	std::ofstream ExplicitOut(_name_1);
	for (double t = 0.0; t < tstop; t += delta) {
		/* Обработка узлов по оси X */
		for (int i = 1; i < program.LineX().size() - 1; i++) {
			std::vector<Node*> temperature;
			Node* cur = program.LineX()[i];
			while (cur) {
				/* Проверка на существование узла */
				if (cur->r() && cur->r()->X() - cur->X() > program.step()) {
					temperature.push_back(cur);
					SolveLine(program, temperature);
					temperature.clear();
					cur = cur->r();
				}
				else {
					temperature.push_back(cur);
					cur = cur->r();
				}
			}
			SolveLine(program, temperature);
		}
		/* Обработка узлов по оси Y */
		for (int i = 1; i < program.LineY().size() - 1; i++) {
			std::vector<Node*> temperature;
			Node* cur = program.LineY()[i];
			while (cur) {
				/* Проверка на существование узла */
				if (cur->u() && cur->u()->Y() - cur->Y() > program.step()) {
					temperature.push_back(cur);
					SolveLine(program, temperature);
					temperature.clear();
					cur = cur->u();
				}
				else {
					temperature.push_back(cur);
					cur = cur->u();
				}
			}
			SolveLine(program, temperature);
		}
		for (auto line : program.Nodes()) {
			for (auto node : line)
				ExplicitOut << node->X() << ' ' << node->Y() << ' ' << node->T() << '\n';
		}
		ExplicitOut << "\n\n";
	}
}

void Solver::SolveImplicit(System& sys, double tstop) const {
	std::ofstream EmplicitOut(_name_2);
	for (double t = 0.; t < tstop; t += delta) {
		for (auto line : sys.Nodes())
			for (auto node : line) {
				/* Проверка на внутренний узел */
				if (!node->IsBound()) {
					/* Tx = T_right - 2T_current + T_left / delta_x ^ 2 */
					/* Ty = T_upper - 2T_current + T_down / delta_y ^ 2*/
					/* T_new = delta_t * a * (delta_x + delta_y) + T_current 
					(для удобства коээфициент a = 1) */

					double tx = (node->r()->T() - 2 * node->T() + node->l()->T()) / pow(sys.step(), 2);
					double ty = (node->u()->T() - 2 * node->T() + node->d()->T()) / pow(sys.step(), 2);
					double t = delta * (tx + ty) + node->T();
					node->SetT(t);
				}
			}
		for (auto line : sys.Nodes()) {
			for (auto node : line)
				EmplicitOut << node->X() << ' ' << node->Y() << ' ' << node->T() << '\n';
		}
		EmplicitOut << "\n\n";
	}
}

void Solver::SolveLine(System& sys, std::vector<Node*>& n) const {
	int size = n.size() - 2;
	double mu1 = n.front()->Dist(n[1]) / sys.step();
	double mu2 = n.back()->Dist(n[n.size() - 2]) / sys.step();
	/* Защита от нуля */
	if (mu2 == 0.)
		mu2 = .1;
	double val2 = -(2 * sys.a1()) / (pow(sys.step(), 2)) - 1 / delta;
	double val1 = sys.a1() / (pow(sys.step(), 2));
	std::vector<std::vector<double>> next(size);
	std::vector<double> right(size);
	for (int i = 0; i < next.size(); i++)
		next[i].resize(3, 0.0);
	next[0][0] = -(2 * sys.a1()) / (mu1 * pow(sys.step(), 2)) - 1 / delta;//val2;
	next[0][1] = (2 * sys.a1()) / ((mu1 + 1) * pow(sys.step(), 2));// val1;
	next.back()[1] = (2 * sys.a1()) / ((mu2 + 1) * pow(sys.step(), 2));// val1;
	next.back()[2] = -(2 * sys.a1()) / (mu2 * pow(sys.step(), 2)) - 1 / delta;//val2;
	for (int i = 1; i < size - 1; i++) {
		next[i][0] = val1;
		next[i][1] = val2;
		next[i][2] = val1;
	}

	for (int i = 0; i < right.size(); i++)
		right[i] = -n[i + 1]->T() / delta;
	right.front() += -(2 * sys.a1() * n.front()->T()) / (mu1 * (mu1 + 1) * pow(sys.step(), 2));
	right.back() += -(2 * sys.a1() * n.back()->T()) / (mu2 * (mu2 + 1) * pow(sys.step(), 2));
	std::vector<double> tmps = ThomasMethod(next, right);
	for (int i = 0; i < tmps.size(); i++)
		n[i + 1]->SetT(tmps[i]);
}


std::vector<double> Solver::ThomasMethod(std::vector<std::vector<double>>& A, std::vector<double>& b) const {
	int row = b.size() - 1;

	std::vector<double>alpha(row);
	std::vector<double>beta(row);

	alpha[0] = -A[0][1] / A[0][0];
	beta[0] = b[0] / A[0][0];

	for (int i = 1; i < row; i++) {
		double a = A[i][0];
		double b1 = A[i][1];
		double c = A[i][2];
		alpha[i] = -c / (a * alpha[i - 1] + b1);
		beta[i] = (b[i] - a * beta[i - 1]) / (a * alpha[i - 1] + b1);
	}

	std::vector<double> result(b.size());

	result.back() = (b.back() - A.back()[1] * beta.back()) / (A.back()[2] + A.back()[1] * alpha.back());
	for (int i = row - 1; i > -1; i--)
		result[i] = alpha[i] * result[i + 1] + beta[i];
	return result;
}
