#pragma once

#include "modules/grid1d.h"


namespace fd1d {

	// -----------------------------------------------------------------------------
	// Second derivative (u_xx) at interior cell i, central difference
	// Works on NON-uniform grids
	// On uniform: (u[i-1] - 2 u[i] + u[i+1]) / dx^2
	// -----------------------------------------------------------------------------
	template <typename T>
	void inplace_Build_CentralDerivative_Matrix(const fvm::Grid1D<T>& g, utils::Matrix<T>& A, utils::Vector<T>& b, const utils::Vector<T>& s, const T& c){

		for (uint64_t i = 1; i < g.center_idx; ++i){

			A(i,i-1) = -(c/(g.centers[i] - g.centers[i-1]));
			A(i,i) = -((c/(g.centers[i+1] - g.centers[i])) + (c/(g.centers[i] - g.centers[i-1])));
			A(i,i+1) = -(c/(g.centers[i+1] - g.centers[i]));
			b[i] = -s[i]*(g.vertices[i+1] - g.vertices[i]);
		}
    }
	template <typename T>
	utils::Matrix<T> Build_CentralDerivative_Matrix(const fvm::Grid1D<T>& g, utils::Vector<T>& b, const utils::Vector<T>& s, const T& c){
		utils::Matrix<T> A(g.center_idx+1, g.center_idx+1, T{0});
		inplace_Build_CentralDerivative_Matrix(g, A, b, s, c);
		return A;
    }


	template <typename T>
	void inplace_BC_Dirichlet(const fvm::Grid1D<T>& g, utils::Matrix<T>& A, utils::Vector<T>& b, const utils::Vector<T>& s, const T& c){

		A(0,0) = -((c/(g.centers[1] - g.centers[0])) + (c/(g.centers[0] - g.vertices[0])));
		A(0,1) = c/(g.centers[1] - g.centers[0]);

		A(g.center_idx, g.center_idx-1) = c/(g.centers[g.center_idx]-g.centers[g.center_idx-1]);
		A(g.center_idx, g.center_idx) = -((c/(g.vertices[g.vertices_idx] - g.centers[g.center_idx])) + (c/(g.centers[g.center_idx] - g.centers[g.center_idx-1])));
    }


	template <typename T>
	void inplace_BC_Neumann(const fvm::Grid1D<T>& g, utils::Matrix<T>& A, const T& c){

		A(0,0) = -c/(g.centers[1]-g.centers[0]);
		A(0,1) = c/(g.centers[1]-g.centers[0]);

		A(g.center_idx, g.center_idx-1) = c/(g.centers[g.center_idx]-g.centers[g.center_idx-1]);
		A(g.center_idx, g.center_idx) = -c/(g.centers[g.center_idx]-g.centers[g.center_idx-1]);
    }






}