#include "base.h"
#include "core.h"
#include "d1q3.h"

struct LBM_local : LBM<D1Q3_M1> // any MM matrix works here
{
	FUN fun;
	LBM_local() : LBM(27)
	{
	        fun.rho = frho(t, x, y, z);
	        fun.vel[0]=fu(t, x, y, z);
	        fun.vel[1]=fv(t, x, y, z);
	        fun.vel[2]=fw(t, x, y, z);
	}
};

void export_latex(const char *dirname, int MO=4, int MFCO=2)
{
	// MO and MFCO needs to be specified as well as basis functions
	LBM_local lbm;
	BASE B;
	char subdir[200];
	symtab table;
	table[lbm.sym_o.get_name()] = lbm.sym_o;
	table[lbm.dt.get_name()] = lbm.dt;
	table[lbm.dl.get_name()] = lbm.dl;
	table[lbm.sym_cs.get_name()] = lbm.sym_cs;
	table[lbm.t.get_name()] = lbm.t;
	table[lbm.x.get_name()] = lbm.x;
	table[lbm.y.get_name()] = lbm.y;
	table[lbm.z.get_name()] = lbm.z;
//	symbol ou("u","\\ou");
//	symbol ov("v","\\ov");
//	symbol ow("w","\\ow");
//	table["u"] = ou;
//	table["v"] = ov;
//	table["w"] = ow;
	table["rho"] = lbm.fun.rho;
	table["u"] = lbm.fun.vel[0];
	table["v"] = lbm.fun.vel[1];
	table["w"] = lbm.fun.vel[2];
	for (int i=0;i<MQ;i++)
		table[lbm.sym_os[i].get_name()] = lbm.sym_os[i];

	if (B.file_exists(dirname))
	{
		// browse pdes
		for (int pi=0;pi<4;pi++) // maximum is D+1 for D=3
		{
			sprintf(subdir,"%s/pde_%02d",dirname,pi);
			if (B.file_exists(subdir))
			{
				cout << "working on "<<subdir<<endl;

				vct bas;
				bas.push_back( lbm.fun.rho );
				bas.push_back( lbm.fun.vel[0] );
				bas.push_back( lbm.fun.vel[1] );
				bas.push_back( lbm.fun.vel[2] );

				TPDE S;
				S.allocate(bas, MO, MFCO, lbm.t, lbm.x, lbm.y, lbm.z);
				S.load(subdir, table);
				S.save_tex2(dirname, "spatial_EPDE","pde_%02d", pi);
				S.save_raw(dirname, "spatial_EPDE","pde_%02d", pi);
			}
		}
	}

}


int main(int argc, char* argv[])
{
	if (argc<2) { cerr << "directory name is missing" << endl; return 0; }
	auto start = chrono::steady_clock::now();
	export_latex(argv[1]);
 	auto end = chrono::steady_clock::now();
	cout<<endl;
	cout<<chrono::duration_cast<chrono::milliseconds>(end-start).count()<<" milliseconds"<<endl;
}
