#pragma once

#include "utils/vector.h"
#include "modules/grid1d.h"


namespace fvm {

	template <typename T>

	struct Field1D{

	    const Grid1D* grid = nullptr;   // not owning
	    utils::Vector<T> u; 			// size = grid->N
		                          
		Field1D() = default;

		explicit Field1D(const Grid1D& g, double init = 0.0) : grid(&g), u(g.N){
			
		}

		void generate_vertices(){
			vertices.resize(N_vertices);
			vertices[0] = centers[0] - ((centers[1] - centers[0])*0.5);	
			vertices[N_vertices-1] = centers[N_centers-1] + ((centers[N_centers-1] - centers[N_centers-2])*0.5);
			for (uint64_t i = 1; i < N_centers; ++i){
				vertices[i] = (centers[i-1] + centers[i])*0.5;
			}
		}

		T dx(uint64_t i) const { check(i); return vertices(i+1) - vertices(i); }
		T center(uint64_t i) const { check(i); return centers(i); }
private:
	    void check(uint64_t i) const {
	        if (i >= N_centers) throw std::runtime_error("Grid1D: cell index out of range");
	    }
	};


}